Skip to content

Commit

Permalink
log replay script, bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
natronics committed May 19, 2014
1 parent e8c3f06 commit 890ced8
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion psas_packet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__version__ = '0.1.9'
__version__ = '0.1.10'
14 changes: 14 additions & 0 deletions psas_packet/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"""
import socket

TELEMETRY_IP = "127.0.0.1"
TELEMETRY_PORT = 35001


class SendUDP(object):
"""UDP socket sender context
Expand Down Expand Up @@ -48,6 +51,17 @@ def send_message(self, msgtype, data):
"""
self.socket.send(msgtype.encode(data))

def send_raw(self, raw):
"""Send raw bytes using this connection
:param raw bytes: bytes to send
"""
try:
self.socket.send(raw)
except:
pass


class ListenUDP(object):
"""UDP socket listener context
Expand Down
82 changes: 82 additions & 0 deletions scripts/replaylog
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import argparse
import struct
import time
from psas_packet import io
from psas_packet import messages
from psas_packet import network

PSAS = {cls.fourcc: cls for cls in messages.PSAS_MESSAGES}
HEAD = messages.Head()

def replay(log):

with network.SendUDP(network.TELEMETRY_IP, network.TELEMETRY_PORT) as udp:

with io.BinFile(log) as log:
seq = 0
cur_seq = -1
file_time = 0
last_seq_time = 0
now = time.time()
last_pack_sent = 0
buff = b''
for data in log.read():
for key in data:

# scan sequnce numbers
if key == 'SEQN':
seq = data[key]['Sequence']
if seq != cur_seq:
# Wait and send
if len(buff) > 0:
now = time.time()
packet_delay = (file_time - last_seq_time)/1e9
real_delay = now - last_pack_sent
wait_time = packet_delay - real_delay
if wait_time > 0:
time.sleep(wait_time)
udp.send_raw(buff)
#print(cur_seq, len(buff), packet_delay, real_delay, wait_time)
last_pack_sent = time.time()

# reset
last_seq_time = file_time
buff = b''
buff += struct.pack('!L', seq)
cur_seq = seq


else:
# copy log
if key in PSAS:
msg = PSAS[key]
dat = data[key]
buff += HEAD.encode(msg, data[key]['timestamp'])
buff += msg.encode(data[key])
file_time = data[key]['timestamp']

# flush last packet
now = time.time()
packet_delay = (file_time - last_seq_time)/1e9
real_delay = now - last_pack_sent
wait_time = packet_delay - real_delay
if wait_time > 0:
time.sleep(wait_time)
udp.send_raw(buff)

#print(cur_seq, len(buff), packet_delay, real_delay, wait_time)




if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='replaylog')
parser.add_argument('logfile', type=argparse.FileType('rb'), help="Log file to read")

args = vars(parser.parse_args())

replay(args['logfile'])
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='psas_packet',
version='0.1.9',
version='0.1.10',
description='serializer for PSAS data standards',
long_description=open('README.rst').read(),
author='Nathan Bergey',
Expand All @@ -22,6 +22,7 @@
'scripts/gen-psas-types',
'scripts/log2csv',
'scripts/slicelog',
'scripts/replaylog',
],
license=open('LICENSE').read(),
zip_safe=False,
Expand Down

0 comments on commit 890ced8

Please sign in to comment.