Browse Source

send battery stats

main
Hendrik Langer 4 years ago
parent
commit
6e16586c22
  1. 4
      raspberry/roberto/Serial.py
  2. 16
      raspberry/roberto/camera/camera_gstreamer_webrtc.py
  3. 1
      raspberry/roberto/views/frontend/templates/index.html
  4. 19
      raspberry/roberto/views/websocket/routes.py

4
raspberry/roberto/Serial.py

@ -65,11 +65,11 @@ class Serial(object):
#self.alive = False
#raise
def add_callback(self, cmd, callback, arg):
def add_callback(self, cmd, callback, arg=None):
if not cmd in self._callbacks:
self._callbacks[cmd] = []
self._callbacks[cmd].append( (callback, arg) )
def del_callback(self, cmd, callback, arg):
def del_callback(self, cmd, callback, arg=None):
if cmd in self._callbacks:
self._callbacks[cmd].remove( (callback, arg) )

16
raspberry/roberto/camera/camera_gstreamer_webrtc.py

@ -147,6 +147,8 @@ class WebRTCCamera(Thread):
self.close_global_pipeline()
else:
print("%d clients left" % self._num_clients)
elif item['job'] == "emit":
sio.emit(item['event'], item['data'])
else:
print("unknown job: %s" % item['job'])
self._queue.task_done()
@ -167,10 +169,17 @@ class WebRTCCamera(Thread):
print('camera sid is', sio.sid)
self.sid = sio.sid
self.connected = True
from roberto import serial
serial.add_callback('B', self.send_battery, None)
def disconnect(self):
print("camera.disconnect()")
self.connected = False
from roberto import serial
try:
serial.del_callback('B', self.send_battery, None)
except:
pass
sio.disconnect()
def connect_client(self, sid, room):
@ -184,6 +193,13 @@ class WebRTCCamera(Thread):
return
self._queue.put({'job':'disconnect_client', 'sid':sid})
def emit_async(self, event, data):
self._queue.put({'job':'emit', 'event':event, 'data':data})
def send_battery(val, arg=None):
print("sending battery value %s" % val)
self.emit_async('battery', val)
def start_global_pipeline(self):
print("STARTING PIPELINE")
desc = PIPELINE_DESC

1
raspberry/roberto/views/frontend/templates/index.html

@ -215,7 +215,6 @@ function clearText() {
ctx.font = "24px Arial";
ctx.fillStyle = "blue";
//ctx.fillText("Hello World", canvas.width/3, 30);
//socket.emit('get_stats', { "type": ["battery"] });
$('form#message').submit(function(event) {
socket.emit('text', {data: $('#message_data').val()});

19
raspberry/roberto/views/websocket/routes.py

@ -74,12 +74,10 @@ def webrtc_message(data):
print("unknown message type")
socketio.emit('message', data=data, room=ROOM, skip_sid=sid)
@socketio.on('get_stats')
def get_stats(data):
@socketio.on('battery') # ok
def battery_message(data):
sid = request.sid
print("client %s requesting stats" % sid)
from roberto import serial
serial.add_callback('B', display_battery, sid)
socketio.emit('battery', data, room=ROOM, skip_sid=sid)
@socketio.on('disconnect')
def disconnect():
@ -87,11 +85,7 @@ def disconnect():
print("Received Disconnect message from %s" % sid)
leave_room(ROOM)
webrtccamera.disconnect_client(sid, ROOM)
from roberto import serial
try:
serial.del_callback('B', display_battery, sid)
except:
pass
@socketio.on('connect')
def connect():
@ -109,7 +103,4 @@ def default_error_handler(e):
for key in request:
print('%s: %s' % (key, repr(request[key])))
def display_battery(val, sid=ROOM):
print("sending battery value %s to client %s" % (val, sid))
socketio.emit('battery', data=val, room=sid)
#socketio.emit('battery', data=val)

Loading…
Cancel
Save