]> git.zndr.dk Git - dotfiles.git/commitdiff
Timestamp in gf.py
authorJannik Zander <jannikz@gmail.com>
Sat, 11 Jun 2016 06:56:49 +0000 (08:56 +0200)
committerJannik Zander <jannikz@gmail.com>
Sat, 11 Jun 2016 06:56:49 +0000 (08:56 +0200)
.local/bin/gf.py

index bf69c6b14b209225530c614368539c01d3ac1135..0a06f012f86cef956756c3fd358812eb314ac786 100755 (executable)
@@ -4,7 +4,10 @@
 import fileinput
 import matplotlib.pyplot as plt
 import numpy as pl
+from datetime import datetime,time
+from math import modf
 
+######################################################
 def readbyte(line):
   # read next byte in line
   byte=256
@@ -18,6 +21,7 @@ def readbyte(line):
       line=line[2:]
   return byte,line
 
+######################################################
 def process_head(line):
   ds,line=readbyte(line)
   if ds==0x27:
@@ -42,6 +46,7 @@ def process_head(line):
   print("Type:",typ,"Length:",le,"Destination:",da,"Source:",sa)
   return typ,ds,le,da,sa,line
 
+######################################################
 def process_apdu(line):
   cl,line=readbyte(line)
   cl=cl&0xF
@@ -51,6 +56,7 @@ def process_apdu(line):
   print("Class:",cl,"Operation:",os,"Length:",le)
   return cl,os,le,line
 
+######################################################
 def process_class5(line,numid):
   id=[0]*numid
   val=[0]*numid
@@ -60,6 +66,7 @@ def process_class5(line,numid):
     print("Id:",id[i],"Value:",val[i])
   return id,val,line
 
+######################################################
 def process_crc(line):
   crc1,line=readbyte(line)
   crc2,line=readbyte(line)
@@ -69,58 +76,72 @@ def process_crc(line):
   print("CRC:",crc)
   return crc
 
+######################################################
 def process_islineok(line):
-  ok=True
-  if "ERR" in line.upper() or len(line) < 12:
-    ok=False
+  ok=False
+  if not "ERR" in line.upper() and len(line)>10:
+    ok=True
   return ok
 
+######################################################
 def process_timestamp(line):
-  #FIXME: does not work in general
   info=line.split(":")
   try:
-    timestamp=int(info[1].strip())
-    print(timestamp)
-    return timestamp
+    timestamp=int(info[1].strip())/1e3
   except:
-    return 0
+    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 timezero
-  global t
-  global speed
+  global td
   global master
+  global speed
 
-  # Some extra info from logging tool
-  ok=process_islineok(line)
-  if not ok: return
+  # Check if timestamp was added to line
+  ts=process_timestamp(line)
 
-  timestamp=process_timestamp(line)
-  if timezero==0:
-    timezero=timestamp
+  # Sanity check line
+  ok=process_islineok(line)
+  if not ok:
+    return
 
   # Process received data
   typ,ds,le,da,sa,line=process_head(line)
   numbytes=le-2
-  while numbytes>0:
+  while numbytes>0 and len(line)>=2*numbytes:
     cl,os,ale,line=process_apdu(line)
     numbytes=numbytes-2
     if cl==5:
-      id,val,line=process_class5(line,int(ale/2))
+      id5,val5,line=process_class5(line,int(ale/2))
       numbytes=numbytes-ale
-      # Debugging info....
+      # Debugging ..........
+      td=td+[ts.timestamp()]
       master=master+[1+(sa-31)/10]
-      t=t+[(timestamp-timezero)/1000]
       for i in range(4):
-        speed[i]=speed[i]+[val[i]/254]
+        speed[i]=speed[i]+[val5[i]/254]
     else:
       line=line[ale*3:]
       numbytes=numbytes-ale
   crc=process_crc(line)
   return
 
-timezero=0
-t=[]
+
+
+######################################################
+
+td=[]
 master=[]
 speed=[]
 speed.append([])
@@ -131,17 +152,23 @@ speed.append([])
 for line in fileinput.input():
   print("====================================================================")
   print(line)
-  print("--------------------------------------------------------------------")
   process(line)
+
+# Calculate timedelta
+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')
 plt.ylabel('speed/upper')
-plt.plot(t,speed[0],'green')
-plt.plot(t,speed[1],'red')
-plt.plot(t,speed[2],'blue')
-plt.plot(t,speed[3],'magenta')
+plt.plot(td,speed[0],'green')
+plt.plot(td,speed[1],'red')
+plt.plot(td,speed[2],'blue')
+plt.plot(td,speed[3],'magenta')
 plt.legend('1234')
-plt.plot(t,master,'black')
+plt.plot(td,master,'black')
 plt.title('multipump')
 plt.show()
 plt.close(fig)