Skip to content

Commit

Permalink
Issue #105 - Send packet as JSON to frontend
Browse files Browse the repository at this point in the history
- Update tlm/realtime route to send packet to front end in JSON format
- Update TelemetryStream on frontend to parse JSON formatted packet
- Update plotting on frontend to work with JSON formatted packet
- Update getting field values from packet on frontend to work with JSON
aywaldron authored and Futabay committed May 19, 2020
1 parent 2f4f262 commit ebd3d67
Showing 4 changed files with 34 additions and 20 deletions.
15 changes: 14 additions & 1 deletion ait/gui/__init__.py
Original file line number Diff line number Diff line change
@@ -696,10 +696,23 @@ def handle():
bottle.abort(400, 'Expected WebSocket request.')

try:
tlmdict = ait.core.tlm.getDefaultDict()
while not wsock.closed:
try:
uid, data = session.telemetry.popleft(timeout=30)
wsock.send(pad + struct.pack('>I', uid) + data)
pkt_defn = None
for k, v in tlmdict.iteritems():
if v.uid == uid:
pkt_defn = v
break
else:
continue

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

except IndexError:
# If no telemetry has been received by the GUI
# server after timeout seconds, "probe" the client
2 changes: 1 addition & 1 deletion ait/gui/static/js/ait/gui/Field.js
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ const Field =
* retrieving the packet value.
*/
getValue (packet, raw=false) {
return packet && packet.__get__(this._fname, raw)
return packet && packet[this._fname]
},


19 changes: 10 additions & 9 deletions ait/gui/static/js/ait/gui/Plot.js
Original file line number Diff line number Diff line change
@@ -78,21 +78,22 @@ class DygraphsBackend
}
}

plot (packet) {
const pname = packet._defn.name
plot (data) {
const pname = data['packet']
let delta = data['data']
const names = this._plot._packets[pname]

if (!names) return

let row = [ this._plot._time.get(packet) ]
let row = [ this._plot._time.get(delta) ]

// For each series of data, if it's in the current packet
// that we're updating, add the associated point. Otherwise,
// add a null value. Dygraphs requires that the data added
// to the plot maintains the same "shape" as the labels.
this._series.forEach((id) => {
if (id.startsWith(pname)) {
row.push(packet.__get__(id.split('.')[1]))
row.push(delta[id.split('.')[1]])
} else {
row.push(null)
}
@@ -207,8 +208,8 @@ class HighchartsBackend
Object.assign(options, overrides)
}

plot(packet) {
const pname = packet._defn.name
plot(delta) {
const pname = delta['packet']
const names = this._plot._packets[pname]
if (!names) return

@@ -354,10 +355,10 @@ class HighchartsBackend
const Plot =
{
/**
* Plots data from the given packet.
* Plots data from the given delta.
*/
plot (packet) {
this._backend.plot(packet)
plot (delta) {
this._backend.plot(delta)
},


18 changes: 9 additions & 9 deletions ait/gui/static/js/ait/tlm.js
Original file line number Diff line number Diff line change
@@ -381,11 +381,12 @@ class TelemetryStream


onMessage (event) {
if ( !(event.data instanceof ArrayBuffer) ) return
if ( !(typeof event.data == "string") ) return

let uid = new DataView(event.data, 1, 4).getUint32(0)
let data = new DataView(event.data, 5)
let defn = this._dict[uid]
let now = Date.now()
let data = JSON.parse(event.data)
let packet_name = data['packet']
let delta = data['data']

// Since WebSockets can stay open indefinitely, the AIT GUI
// server will occasionally probe for dropped client
@@ -399,16 +400,15 @@ class TelemetryStream
// It's also possible that the packet UID is not in the client
// telemetry dictionary (defn === undefined). Without a
// packet definition, the packet cannot be processed further.
if ((uid == 0 && data.byteLength == 0) || !defn) return

let packet = Packet.create(defn, data)

clearInterval(this._interval)
this._stale = 0
this._interval = setInterval(this.onStale.bind(this), 5000)

ait.packets.insert(defn.name, packet)
this._emit('packet', packet)
console.log(packet_name)
console.log(delta)
ait.packets.insert(packet_name, delta)
this._emit('packet', data)
}


0 comments on commit ebd3d67

Please sign in to comment.