]> git.zndr.dk Git - dotfiles.git/commitdiff
Fix timestamp
authorJannik Zander <jannikz@gmail.com>
Sat, 11 Jun 2016 12:40:28 +0000 (14:40 +0200)
committerJannik Zander <jannikz@gmail.com>
Sat, 11 Jun 2016 12:40:28 +0000 (14:40 +0200)
.local/bin/gf.py

index 0a06f012f86cef956756c3fd358812eb314ac786..9afc93febf0675921d98971b84615746540dd465 100755 (executable)
@@ -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')