From: Jannik Zander Date: Sat, 11 Jun 2016 12:40:28 +0000 (+0200) Subject: Fix timestamp X-Git-Url: https://git.zndr.dk/?a=commitdiff_plain;h=d0b1fd3a868066e96b776166e24758cc02c4bb08;p=dotfiles.git Fix timestamp --- diff --git a/.local/bin/gf.py b/.local/bin/gf.py index 0a06f01..9afc93f 100755 --- a/.local/bin/gf.py +++ b/.local/bin/gf.py @@ -4,12 +4,46 @@ import fileinput import matplotlib.pyplot as plt import numpy as pl -from datetime import datetime,time +from datetime import datetime,date,time from math import modf ###################################################### +def process_timestamp(line): + info=line.split(":") + try: + timestamp=int(info[1].strip())/1000 + except: + timestamp=0 + finally: + ts=datetime.fromtimestamp(timestamp) + + if timestamp>1e9: # 2001-09-09 + line=info[0] + else: + try: + h=int(info[0].split(' ')[1]) + m=int(info[1]) + s=info[2].split('.') + u=s[1].split(' ') + ts=datetime.combine(date.today(),time(h,m,int(s[0]),int(u[0])*1000)) + line=s[1][4:] + except: + pass + print("Timestamp:",ts) + print(line) + return ts,line + +###################################################### +def checkline(line): + ok=False + if len(line)>16 and not "ERR" in line.upper(): + ok=True + print("Ok:",ok) + return ok + +###################################################### +# read next byte from line def readbyte(line): - # read next byte in line byte=256 while byte==256 and len(line)>0: try: @@ -76,71 +110,39 @@ def process_crc(line): print("CRC:",crc) return crc -###################################################### -def process_islineok(line): - ok=False - if not "ERR" in line.upper() and len(line)>10: - ok=True - return ok - -###################################################### -def process_timestamp(line): - info=line.split(":") - try: - timestamp=int(info[1].strip())/1e3 - except: - timestamp=0 - finally: - ts=datetime.fromtimestamp(timestamp) - if timestamp<1e9: # 2001-09-09 - try: - h=int(info[0].split(' ')[1]) - m=int(info[1]) - u,s=modf(float(info[2].split(' ')[0])) - ts=time(h,m,int(s),int(u*1e6)) - except: - pass - print("timestamp",ts) - return ts - ###################################################### def process(line): global td global master global speed - # Check if timestamp was added to line - ts=process_timestamp(line) + # Check if timestamp exists + ts,line=process_timestamp(line) - # Sanity check line - ok=process_islineok(line) - if not ok: - return + # Check if line is ok + ok=checkline(line) + if not ok: return - # Process received data + # Process telegram typ,ds,le,da,sa,line=process_head(line) numbytes=le-2 while numbytes>0 and len(line)>=2*numbytes: cl,os,ale,line=process_apdu(line) - numbytes=numbytes-2 + numbytes=numbytes-2-ale if cl==5: id5,val5,line=process_class5(line,int(ale/2)) - numbytes=numbytes-ale - # Debugging .......... + # Debugging td=td+[ts.timestamp()] master=master+[1+(sa-31)/10] for i in range(4): speed[i]=speed[i]+[val5[i]/254] else: line=line[ale*3:] - numbytes=numbytes-ale crc=process_crc(line) return - - ###################################################### - +# Debugging td=[] master=[] speed=[] @@ -155,10 +157,10 @@ for line in fileinput.input(): process(line) # Calculate timedelta -if len(td)==0: - exit() +if len(td)==0: exit() t0=td[0] td[:]=[x-t0 for x in td] + # Make a plot fig=plt.figure() plt.xlabel('time/s')