-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonVideoRecorderClient.py
98 lines (75 loc) · 4.32 KB
/
PythonVideoRecorderClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import asyncio
import json
import os
import time
import websockets
from EventEnums import Events, Devices
from EventInfo import EventInfo, Header, Data, BayInfo
from EventInfo import getEventInfoObject, getEventInfoDict
from RecordVideoAndUploadUtils import (uploadVideo, checkIfBetterShot, getAllPlayerVideoUrls,
recordVideoUsingNetworkCameraWithLogo)
CONFIG_PATH = "./config.json"
fp = open(CONFIG_PATH)
config = json.load(fp)
bayId = config["bayId"]
CURRENT_VIDEO_FILE_NAME = config["currentShotVideoName"]
CURRENT_VIDEO_DIR_PATH = os.path.join(bayId, str(config["currentVideoDirectory"]))
CURRENT_VIDEO_FULL_PATH = os.path.join(CURRENT_VIDEO_DIR_PATH, CURRENT_VIDEO_FILE_NAME)
ALL_PLAYERS_VIDEOS_DIR_PATH = os.path.join(bayId, str(config["allPlayersVideoDirectory"]))
LOGO_PATH = config["logoPath"]
RTSP_URL = config["cameraRTSP"]
WS_URI = config["websocketServerURI"]
TIME_INTERVAL_BETWEEN_EVENTS = config["timeIntervalBetweenEvents"]
VIDEO_LENGTH = config["videoLength"]
lastEvent = {}
async def getVideoRecordedEvent(forBay):
bayInfo = BayInfo(isForAllBays=False, bayId=forBay)
header = Header(Devices.RECORDER.value, [Devices.UNITY.value], Events.PLAY_VIDEO.value, bayInfo)
data = Data({"value": "1"})
event = EventInfo(header, data)
return json.dumps(event, default=vars)
async def client():
async with websockets.connect(WS_URI) as websocket:
while True:
try:
message = await websocket.recv()
eventInfo = getEventInfoObject(message)
eventInfoDict = getEventInfoDict(message)
if not eventInfo.header.bayInfo.isForAllBays:
if eventInfo.header.bayInfo.bayId != bayId:
continue
if Devices.RECORDER.value in eventInfo.header.sentTo:
if eventInfo.header.eventName in lastEvent:
if time.time() - lastEvent[eventInfo.header.eventName] < TIME_INTERVAL_BETWEEN_EVENTS:
print("Skipping event", eventInfo.header.eventName)
print("Time Difference: ", time.time() - lastEvent[eventInfo.header.eventName])
continue
lastEvent[eventInfo.header.eventName] = time.time()
if eventInfo.header.eventName == Events.THROW_BALL.value:
recordVideoUsingNetworkCameraWithLogo(CURRENT_VIDEO_FULL_PATH, LOGO_PATH, RTSP_URL, VIDEO_LENGTH)
recordedEventJson = await getVideoRecordedEvent(eventInfo.header.bayInfo.bayId)
await websocket.send(recordedEventJson)
elif eventInfo.header.eventName == Events.CURRENT_BALL_INFO.value:
scoreOnCurrentBall = eventInfo.data.value.score
currentPlayerId = eventInfo.data.value.scoredBy.id
checkIfBetterShot(ALL_PLAYERS_VIDEOS_DIR_PATH, CURRENT_VIDEO_FULL_PATH, CURRENT_VIDEO_FILE_NAME,
"Player_" + currentPlayerId, scoreOnCurrentBall)
elif eventInfo.header.eventName == Events.CURRENT_BALL_VIDEO_URL.value:
videoUrl = uploadVideo(CURRENT_VIDEO_FULL_PATH, CURRENT_VIDEO_FILE_NAME)
eventInfoDict["header"]["sentBy"] = Devices.RECORDER.value
eventInfoDict["header"]["sentTo"] = [Devices.PLAYER_APP.value]
eventInfoDict["data"]["value"]["videoUrl"] = videoUrl
await websocket.send(json.dumps(eventInfoDict))
elif eventInfo.header.eventName == Events.GAME_ENDED.value:
playerIds = getAllPlayerVideoUrls(ALL_PLAYERS_VIDEOS_DIR_PATH)
eventInfoDict["header"]["sentBy"] = Devices.RECORDER.value
eventInfoDict["header"]["sentTo"] = [Devices.PLAYER_APP.value]
eventInfoDict["data"]["value"] = playerIds
await websocket.send(json.dumps(eventInfoDict))
except ConnectionError as e:
print(f"Connection Error: {e}")
except json.decoder.JSONDecodeError as e:
print(f"Invalid JSON: {e}")
except Exception as e:
print(f"Error: {e}")
asyncio.run(client())