Skip to content

Commit

Permalink
Make counters session dependent (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
aywaldron authored and Futabay committed May 19, 2020
1 parent e277c42 commit 52a614f
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions ait/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__ (self, store=None, maxlen=100):
self.events = api.GeventDeque(maxlen=maxlen)
self.messages = api.GeventDeque(maxlen=maxlen)
self.telemetry = api.GeventDeque(maxlen=maxlen)
self.tlm_counters = { }
self._maxlen = maxlen
self._store = store
self._numConnections = 0
Expand Down Expand Up @@ -80,11 +81,36 @@ def __init__ (self, *args, **kwargs):
"""Creates a new SessionStore."""
dict.__init__(self, *args, **kwargs)

def getPacketName(self, uid):
"""
Returns packet defn from tlm dict matching uid.
Logs error if no defn matching uid is found.
"""
tlmdict = ait.core.tlm.getDefaultDict()
for k, v in tlmdict.iteritems():
if v.uid == uid:
return v

log.error('No packet defn matching UID {}'.format(uid))

def addTelemetry (self, uid, packet):
"""Adds a telemetry packet to all Sessions in the store."""
item = (uid, packet)
SessionStore.History.telemetry.append(item)

for session in self.values():
print(session)

pkt_name = self.getPacketName(uid).name
if pkt_name not in session.tlm_counters:
session.tlm_counters[pkt_name] = 0
else:
count = session.tlm_counters[pkt_name]
count = count + 1 if count < 2**31 - 1 else 0
session.tlm_counters[pkt_name] = count

print(session.tlm_counters)
item = (uid, packet, session.tlm_counters[pkt_name])
session.telemetry.append(item)

def addMessage (self, msg):
Expand Down Expand Up @@ -700,7 +726,6 @@ def add_dntoeu_value(field, packet, dntoeus):


packet_states = { }
counters = { }
def get_packet_delta(pkt_defn, packet):
"""
Keeps track of last packets recieved of all types recieved
Expand All @@ -717,7 +742,6 @@ def get_packet_delta(pkt_defn, packet):

# first packet of this type
if pkt_defn.name not in packet_states:
counters[pkt_defn.name] = 0
packet_states[pkt_defn.name] = json_pkt
delta = json_pkt

Expand All @@ -727,9 +751,6 @@ def get_packet_delta(pkt_defn, packet):

# previous packets of this type received
else:
count = counters[pkt_defn.name]
count = count + 1 if count < 2**31 - 1 else 0
counters[pkt_defn.name] = count
delta, dntoeus = {}, {}
for field, new_value in json_pkt.items():
last_value = packet_states[pkt_defn.name][field]
Expand All @@ -738,7 +759,7 @@ def get_packet_delta(pkt_defn, packet):
packet_states[pkt_defn.name][field] = new_value
dntoeus = add_dntoeu_value(field, ait_pkt, dntoeus)

return delta, dntoeus, counters[pkt_defn.name]
return delta, dntoeus


def replace_datetimes(delta):
Expand Down Expand Up @@ -766,14 +787,14 @@ def handle():
tlmdict = ait.core.tlm.getDefaultDict()
while not wsock.closed:
try:
uid, data = session.telemetry.popleft(timeout=30)
uid, data, counter = session.telemetry.popleft(timeout=30)
pkt_defn = None
for k, v in tlmdict.iteritems():
if v.uid == uid:
pkt_defn = v
break

delta, dntoeus, counter = get_packet_delta(pkt_defn, data)
delta, dntoeus = get_packet_delta(pkt_defn, data)
delta = replace_datetimes(delta)
log.info(delta)
log.info('Packet #{}'.format(counter))
Expand Down Expand Up @@ -807,8 +828,10 @@ def handle():
for pkt_type, state in packet_states.items():
packet_states[pkt_type] = replace_datetimes(state)

return json.dumps({'states': packet_states,
'counters': counters})
with Sessions.current() as session:
counters = session.tlm_counters
return json.dumps({'states': packet_states,
'counters': counters})


@App.route('/tlm/query', method='POST')
Expand Down

0 comments on commit 52a614f

Please sign in to comment.