Browse Source

battery stats callbacks

main
Hendrik Langer 4 years ago
parent
commit
94440a733e
  1. 12
      raspberry/roberto/Serial.py
  2. 4
      raspberry/roberto/views/frontend/templates/gamepad.js
  3. 3
      raspberry/roberto/views/frontend/templates/index.html
  4. 18
      raspberry/roberto/views/websocket/routes.py

12
raspberry/roberto/Serial.py

@ -55,8 +55,8 @@ class Serial(object):
argument = data[2:].strip()
if command and command in self._callbacks:
callbacks = self._callbacks[command]
for cb in callbacks:
cb(argument)
for cb, cb_arg in callbacks:
cb(argument, cb_arg)
else:
print("no callback found for command '%s'" % command)
@ -65,11 +65,11 @@ class Serial(object):
#self.alive = False
#raise
def add_callback(self, cmd, callback):
def add_callback(self, cmd, callback, arg):
if not cmd in self._callbacks:
self._callbacks[cmd] = []
self._callbacks[cmd].append(callback)
self._callbacks[cmd].append( (callback, arg) )
def del_callback(self, cmd, callback):
def del_callback(self, cmd, callback, arg):
if cmd in self._callbacks:
self._callbacks[cmd].remove(callback)
self._callbacks[cmd].remove( (callback, arg) )

4
raspberry/roberto/views/frontend/templates/gamepad.js

@ -10,10 +10,6 @@
//namespace = '/gamepad';
//var gp_socket = io(namespace, { transports: [ 'websocket' ] });
socket.on('connect', function() {
socket.emit('my_event', {data: 'I\'m connected!'});
});
var haveEvents = 'GamepadEvent' in window;
var haveWebkitEvents = 'WebKitGamepadEvent' in window;
var controllers = {};

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

@ -196,9 +196,10 @@ socket.on('battery', (data) => {
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) {
gp_socket.emit('text', {data: $('#message_data').val()});
socket.emit('text', {data: $('#message_data').val()});
$('#message_data').val("");
return false;
});

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

@ -74,6 +74,14 @@ 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):
sid = request.sid
print("client %s requesting stats" % sid)
from roberto import serial
if sid != webrtccamera.sid:
serial.add_callback('B', display_battery, sid)
@socketio.on('disconnect')
def disconnect():
sid = request.sid
@ -81,8 +89,7 @@ def disconnect():
leave_room(ROOM)
webrtccamera.disconnect_client(sid, ROOM)
from roberto import serial
if sid != webrtccamera.sid:
serial.del_callback('B', display_battery)
serial.del_callback('B', display_battery, sid)
@socketio.on('connect')
def connect():
@ -90,9 +97,6 @@ def connect():
print("Received Connect message from %s" % sid)
join_room(ROOM)
webrtccamera.connect_client(sid, ROOM)
from roberto import serial
if sid != webrtccamera.sid:
serial.add_callback('B', display_battery)
@socketio.on_error_default
def default_error_handler(e):
@ -103,6 +107,6 @@ def default_error_handler(e):
for key in request:
print('%s: %s' % (key, repr(request[key])))
def display_battery(val):
socketio.emit('battery', data=val, room=ROOM)
def display_battery(val, sid=ROOM):
socketio.emit('battery', data=val, room=sid)
#socketio.emit('battery', data=val)

Loading…
Cancel
Save