56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
|
import random
|
||
|
|
||
|
from CameraCommander import CameraCommander
|
||
|
from CameraModel import CameraModel
|
||
|
from WaitState import WaitState
|
||
|
from WaitActivity import WaitActivity
|
||
|
import CameraContext
|
||
|
|
||
|
|
||
|
class WaitAuthentication(WaitState):
|
||
|
def handle(self, context):
|
||
|
data = self.waitMsg(context)
|
||
|
if not data:
|
||
|
return
|
||
|
words = data.split(":")
|
||
|
if len(words) != 2:
|
||
|
print("[WaitAuthentication] : Invalid data from camera at {}:{}".format(context.connection.addr, context.connection.port))
|
||
|
context.connection.send("invalid_data")
|
||
|
context.close()
|
||
|
return
|
||
|
|
||
|
mdl = CameraModel()
|
||
|
camera = mdl.getById(words[0])
|
||
|
if not camera or camera.product_key != words[1]:
|
||
|
print("[WaitAuthentication] : Authentication failed for camera at {}:{}".format(context.connection.addr, context.connection.port))
|
||
|
context.connection.send("refused")
|
||
|
context.close()
|
||
|
return
|
||
|
|
||
|
try:
|
||
|
CameraCommander.Register(context, camera)
|
||
|
except CameraCommander.InvalidStateException:
|
||
|
context.connection.send("refused")
|
||
|
context.close()
|
||
|
return
|
||
|
|
||
|
context.camera = camera
|
||
|
print("[WaitAuthentication] : Camera at {}:{} successfully authenticated as ({})".format(context.connection.addr, context.connection.port, context.camera.id))
|
||
|
|
||
|
context.camera.available = True
|
||
|
context.camera.rtmp_handle = hex(hash(random.random()))[2:]
|
||
|
mdl.update(context.camera)
|
||
|
if not context.connection.send("accepted:{}".format(context.camera.rtmp_handle)):
|
||
|
print("[WaitAuthentication] : Connection lost with camera ({}) at {}:{}".format(context.camera.id, context.connection.addr, context.connection.port))
|
||
|
context.close()
|
||
|
return
|
||
|
|
||
|
if context.camera.enabled:
|
||
|
context.enable()
|
||
|
return
|
||
|
context.state = WaitActivity()
|
||
|
|
||
|
|
||
|
def tic(self, context):
|
||
|
pass
|