Skip to content

Commit

Permalink
Issue #105 - Store last packet on backend; send only changes to front…
Browse files Browse the repository at this point in the history
… end
  • Loading branch information
aywaldron authored and Futabay committed May 19, 2020
1 parent 9f48361 commit 1f930d4
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion ait/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,33 @@ def handle():
pass


last_packets = { }
def get_packet_delta(pkt_defn, packet):
"""
Keeps track of last packets recieved of all types recieved
and checks returns only fields that have changed since last
packet.
Params:
pkt_defn: Packet definition
packet: Packet in JSON format
Returns:
delta: JSON of packet fields that have changed
"""
if pkt_defn.name not in last_packets:
last_packets[pkt_defn.name] = packet
delta = packet
else:
delta = { }
for field, new_value in packet.items():
last_value = last_packets[pkt_defn.name][field]
if new_value != last_value:
delta[field] = new_value
last_packets[pkt_defn.name][field] = new_value

return delta


@App.route('/tlm/realtime')
def handle():
"""Return telemetry packets in realtime to client"""
Expand All @@ -704,9 +731,12 @@ def handle():
pkt_defn = v
break

json_pkt = ait.core.tlm.Packet(pkt_defn, data=data).toJSON()
delta = get_packet_delta(pkt_defn, json_pkt)

wsock.send(json.dumps({
'packet': pkt_defn.name,
'data': ait.core.tlm.Packet(pkt_defn, data=data).toJSON()
'data': delta
}))

except IndexError:
Expand Down

0 comments on commit 1f930d4

Please sign in to comment.