a6ca49d10e7a058a4e558319fb93ad7ad0b91d4c
[owSlave2.git] / stest / log_all.py
1 import sys
2 import os
3 import os.path
4 import subprocess
5 import io
6 import time
7 import math
8 import shutil
9
10 toolstr="/home/muto/owtools/src/owtools USB1 DC "
11
12 gplist=[] 
13
14 def id2string(id):
15         s=""
16         for i in range(8):
17                 s=s+"%02X" %(id[7-i])
18         return s
19
20 def string2id(s):
21         for i in range(8):
22                 bl=[]
23                 i2=14-i*2;
24                 bl.append(int(s[i2:i2+2],16))
25         return bl
26
27
28 def calculateValues(code, V, vn):
29         if code==1:
30                 return V[vn] / 16.0; 
31         elif code==2:
32                 return V[vn]/1.6; 
33         elif code==3:
34                 return V[vn]*0.2 + 700; 
35         elif code==4:
36                 return math.exp(V[vn] / 160.0); 
37         elif code==5:
38                 return V[vn]*62.5 + 55000; 
39         elif code==6:
40                 return V[vn] / 256.0; 
41         elif code==7:
42                 if V[1]!=0:
43                         return ((float(V[2]) / float(V[1]) - 0.16) / 0.0062) / (1.0546 - 0.00216*V[0]/256.0); 
44                 else:
45                         return 0
46         elif code==8:
47                 return V[vn] / 100.0; 
48         elif code==9:
49                 return V[vn] / 65535.0*5.1; 
50         elif code==10:
51                 return V[vn] / 65535.0*2.55; 
52         elif code==11:
53                 return V[vn] / 65535.0*1.1; 
54         elif code==12:
55                 return V[vn] / 10.0; 
56         elif code==13:
57                 return V[vn];  
58         elif code==14:
59                 return (V[vn] - 32767.0) / 100.0;  
60         elif code==15:
61                 return math.exp((V[vn]-32767.0)/1000.0); 
62         elif code==16:
63                 return V[vn]/32.0; 
64         else:
65                 return 0;
66
67 def getName(code        ):
68         if code==1:
69                 return ["Temperatur","C"]
70         if code==2:
71                 return ["Druck","hPa"]
72         if code==3:
73                 return ["Beleuchtungsstaerke","lux"]
74         if code==4:
75                 return ["Luftfeuchte","%%"]
76         if code==5:
77                 return ["Konstante",""]
78         if code==6:
79                 return ["Spannung","V"]
80         if code==7:
81                 return ["Strom","mA"]
82         if code==8:
83                 return ["VOC","ppm"]
84         if code==9:
85                 return ["Counter",""]
86         return []
87
88
89 def owCom(dev,send,rcount):
90         cmd=toolstr+id2string(dev)
91         #for i in range(8):
92         #       cmd=cmd+"%02X" %(dev[7-i])
93         cmd=cmd+" "
94         for b in send:
95                 cmd=cmd+"%02X" % (b)
96         for i in range(rcount):
97                 cmd=cmd+"FF"
98         print(cmd.split(' '));
99         p = subprocess.Popen(cmd.split(' '), shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
100         p.wait()
101         s=p.stdout.read().decode("utf-8")
102         l=s.split("\n");
103         #print(l[2])
104         bl=[]
105         for i in range(int(len(l[2])/2)):
106                 i2=i*2;
107                 bl.append(int(l[2][i2:i2+2],16))
108         return bl
109
110 def owComStr(dev,sendstr):
111         cmd=toolstr+id2string(dev)
112         #for i in range(8):
113         #       cmd=cmd+"%02X" %(dev[7-i])
114         cmd=cmd+" "+sendstr
115         p = subprocess.Popen(cmd.split(' '), shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
116         p.wait()
117         s=p.stdout.read().decode("utf-8")
118         #print(s);
119         ll=s.split("\n");
120         bll=[]
121         for l in ll[2:]:
122                 bl=[]
123                 for i in range(int(len(l)/2)):
124                         i2=i*2;
125                         bl.append(int(l[i2:i2+2],16))
126                 bll.append(bl)
127         return bll
128
129 voc_marker=[]
130
131 def owList(): 
132         cmd=toolstr[0:-3]
133         print(cmd)
134         p = subprocess.Popen(["./owtools","USB1"], shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
135         p.wait()
136         s=p.stdout.read()
137         l=s.split("\n")
138         #print(l);
139         l=l[1:-1]
140         devlist=[]
141         for d in l:
142                 dev=[]
143                 bl=[]
144                 for i in range(8):
145                         i2=14-i*2;
146                         bl.append(int(d[i2:i2+2],16))
147                 dev.append(bl)
148                 #print(bl)
149                 config=owCom(bl,[0x85],18)
150                 #print(config)
151                 dev.append(config[1:])
152                 ddev=0
153                 vm=0
154                 if config[1]==0x08:
155                         vm=1
156                 #print("Test_configs")
157                 i=0
158                 for dv in devlist:
159                         #print("c",dv[1][9:16])
160                         #print("d",bl[0:7])
161                         if dv[1][9:16]==bl[0:7]:
162                                 print("Double Device")
163                                 ddev=1
164                                 dv.append(dev[0])
165                                 dv.append(dev[1])
166                                 if vm==1:
167                                         voc_marker[i]=2
168                         i=i+1
169                 if ddev==0:
170                         devlist.append(dev)
171                         voc_marker.append(vm)
172                 #print("devlist",devlist)
173         return devlist
174
175 def getProperties(dev,voc):
176         pl=[]
177         if dev[0][0]==0x26:
178                 pl=["Temperatur","Spannung","Luftfeuchte","Strom"]
179                 if dev[1][0]!=0xFF:
180                         pl=[]
181                         for i in range(4):
182                                 name=getName(dev[1][i*2])
183                                 if len(name)!=0:
184                                         pl.append(name[0])
185                         if voc!=0:
186                                 #R0,VS,Corr,cmode,RS,RSR0,VOC1,VOC2
187                                 pl.append("R0")
188                                 pl.append("VS")
189                                 pl.append("Corr")
190                                 pl.append("cmode")
191                                 pl.append("RS")
192                                 pl.append("RSR0")
193                                 pl.append("VOC1")
194                                 pl.append("VOC2")
195         if dev[0][0]==0x28:
196                 pl=["Temperatur"]
197                 if dev[1][0]!=0xFF:
198                         pl=[]
199                         for i in range(4):
200                                 name=getName(dev[1][i*2])
201                                 if len(name)!=0:
202                                         pl.append(name[0])
203         return pl
204
205 def getValues(dev,voc):
206         pl=[]
207         #print(id2string(dev[0]),voc)
208         if dev[0][0]==0x26:
209                 vnok=1
210                 while (vnok):
211                         sbl=owComStr(dev[0],"4E000F_RB4_R_P7044_R_P70B800_RBE00FFFFFFFFFFFFFFFFFF_R4E0000_RB4_R_P70B800_RBE00FFFFFFFFFFFFFFFFFF_RBE03FFFFFFFFFFFFFFFFFF")
212                         #print(sbl)
213                         sb=sbl[4]
214                         VDD=(sb[5]+sb[6]*256)
215                         sb=sbl[8]
216                         VAD=(sb[5]+sb[6]*256)
217                         Temp=(sb[3]+sb[4]*256)
218                         curr=sb[7]+sb[8]*256
219                         if (sb[8] & 0x80):
220                                 curr=-(curr&0x7FFF)
221                         vals=[Temp,VDD,VAD,curr]
222                         formellist=[6,8,7,8]
223                         if dev[1][0]!=0xFF:
224                                 formellist=[]
225                                 for i in range(4):
226                                         if (dev[1][i*2+1])!=0:
227                                                 formellist.append(dev[1][i*2+1])
228                                         else:
229                                                 break
230                         cvals=[]
231                         for i in range(len(formellist)):
232                                 cvals.append(calculateValues(formellist[i],vals,i))
233                         if (cvals[2]<=110):
234                                 vnok=0
235                         else:
236                                 print(cvals)
237                                 vnok=0
238                         if voc!=0:
239                                 #R0,VS,Corr,cmode,RS,RSR0,VOC1,VOC2
240                                 sb=sbl[9]
241                                 R0=(sb[2]+sb[3]*256)/100.0
242                                 VS=(sb[4]+sb[5]*256)/10000.0
243                                 Corr=(sb[7]+sb[8]*256)/1000.0
244                                 cmode=sb[6]
245                                 RS=(3/VS-1)*30
246                                 RSR0=(RS/R0)
247                                 VOC1=RSR0*Corr
248                                 VOC2=RSR0/Corr
249                                 cvals.append(R0)
250                                 cvals.append(VS)
251                                 cvals.append(Corr)
252                                 cvals.append(cmode)
253                                 cvals.append(RS)
254                                 cvals.append(RSR0)
255                                 cvals.append(VOC1)
256                                 cvals.append(VOC2)
257                 return cvals
258         if dev[0][0]==0x28:
259                 sbl=owComStr(dev[0],"44_R_PFF_PFF_PFF_PFFBEFFFFFFFFFFFFFFFFFF")
260                 Temp=sbl[1][1]+sbl[1][2]*256
261                 if (sbl[1][2] & 0x80):
262                         Temp=-(Temp&0x7FFF)
263                 if dev[1][0]!=0xFF:
264                         return [calculateValues(dev[1][1],[Temp],0)]
265                 else:
266                         return [calculateValues(1,[Temp],0)]
267         return []
268
269
270
271
272 def gp_makefile(name,i):
273         f=open("gpscript%i.dem" %(i),"w")
274         #f.write("set decimalsign ','\n")
275         f.write('set decimalsign locale\n')
276         f.write('set timefmt "%d.%m.%Y %H:%M:%S"\n')
277         f.write('set xdata time\n')
278         f.write('set datafile separator "\\t" \n')
279         f.write('set terminal png size 1024,600\n')
280         f.write('set format x "%d.%m\\n%H:%M"\n')
281         f.write("set output '%s'\n" % (name))
282         f.write("set grid ytics\n")
283         f.write("set terminal postscript landscape\n")
284         f.write('set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgb"#FFFFFF" behind\n')
285         #set output 'Temperatur.ps'
286         return f
287
288
289 def gnuplot():
290         rl=["Temperatur","Luftfeuchte","Druck","VOC","Beleuchtungsstaerke"]
291         rlf=[]
292         rls=[]
293         rllc=[]
294         for i in range(len(rl)):
295                 rlf.append("") #begin of plotline
296                 rls.append('') #plotline
297                 rllc.append('')  #lineconfig
298         li=0
299         for c in gplist:
300                 for i in range(len(rl)):
301                         if c[0]==rl[i]:
302                                 li=li+1
303                                 cl=c[2].split(",")
304                                 rls[i]=rls[i]+" %s using 1:%i with linespoints ls %i title \"%s\" ," % (rlf[i],c[1],li,cl[0])
305                                 rllc[i]=rllc[i]+ "set style line %i %s\n" % (li,cl[1])
306                                 rlf[i]='""'
307         for i in range(len(rl)):
308                 f=gp_makefile(rl[i]+".ps",i)
309                 f.write(rllc[i])
310                 f.write('plot "log22.txt" '+rls[i]+"\n")
311                 f.write("set output '%s'\n" % (rl[i]+"_l.ps"))
312                 f.write('plot "< tail -n 100 log22.txt" '+rls[i]+"\n")
313                 f.close()
314                 p = subprocess.Popen(["gnuplot","gpscript%i.dem" %(i)], shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
315                 p.wait();
316                 cmd="convert -size 2048x1600 -depth 8 %s.ps -rotate 90 -resize 1024x800 -depth 8 %s.png" %(rl[i],rl[i])
317                 p = subprocess.Popen(cmd.split(" "), shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
318                 p.wait();
319                 cmd="convert -size 2048x1600 -depth 8 %s_l.ps -rotate 90 -resize 1024x800 -depth 8 %s_l.png" %(rl[i],rl[i])
320                 print cmd.split(" ")
321                 p = subprocess.Popen(cmd.split(" "), shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
322                 p.wait();
323                 shutil.copyfile(rl[i]+".png","/var/www/html/"+rl[i]+".png")
324                 shutil.copyfile(rl[i]+"_l.png","/var/www/html/"+rl[i]+"_l.png")
325
326 devlist=owList()
327 print(voc_marker)
328 tline1="\t"
329 tline2="time\t"
330 devdict={}
331 devwholist=[]
332 f=open("DEVLIST.txt","r")
333 for l in f.readlines():
334         ls=l.split(" ")
335         if (len(ls)>1):
336                 devdict[ls[0]]=l.replace("\n","")
337                 devwholist.append(ls[0])
338
339 i=0
340 col=1
341
342
343 devlist1=[]
344 devlistn=[]
345 voc_marker1=[]
346
347 for d in devwholist:
348         i=0
349         print(d)
350         for dd in devlist:
351                 if id2string(dd[0])==d:
352                         print("->",id2string(dd[0]))
353                         devlist1.append(dd)
354                         devlist.remove(dd)
355                         voc_marker1.append(voc_marker[i])
356                         del voc_marker[i]
357                         break
358                 i=i+1
359 for d in devlist:
360         devlist1.append(d)
361         
362 devlist=devlist1
363 voc_marker=voc_marker1
364
365
366 print devlist
367
368 print voc_marker
369
370
371
372 for d in devlist:
373         print(d[0])
374         prop=getProperties(d,voc_marker[i])
375         if len(prop)>0:
376                 tline1=tline1+id2string(d[0])
377         for p in prop:
378                 tline2=tline2+p+"\t"
379                 tline1=tline1+"\t"
380                 col=col+1
381                 dn=id2string(d[0])
382                 if dn in devdict:
383                         dn=devdict[dn]
384                 gplist.append((p,col,dn))
385         if (len(d)>2):
386                 print("->",d[2])
387                 prop=getProperties(d[2:],voc_marker[i])
388                 tline1=tline1+id2string(d[2])
389                 for p in prop:
390                         tline2=tline2+p+"\t"
391                         tline1=tline1+"\t"
392                         col=col+1
393                         dn=id2string(d[2])
394                         if dn in devdict:
395                                 dn=devdict[dn]
396                         gplist.append((p,col,dn))
397         i=i+1
398
399 #if os.path.exists("log22.txt"):
400 #       lt = time.localtime()
401 #       ts=time.strftime("%Y%m%d%H%M%S", lt)
402 #       os.rename("log22.txt","log22_"+ts+".txt")
403
404 f=open("log22h.txt","a")
405 f.write(tline1+"\n")
406 f.write(tline2+"\n")
407 f.close()
408 print(tline1)
409 print(tline2)
410
411 while (1):
412         lt = time.localtime()
413         vline=time.strftime("%d#%m#%Y %H:%M:%S\t", lt)
414         i=0
415         for d in devlist:
416                 #print(d[0])
417                 vals=getValues(d,voc_marker[i])
418                 for v in vals:
419                         vline=vline+"%0.5f\t" % (v)
420                 if (len(d)>2):
421                         vals=getValues(d[2:],voc_marker[i])
422                         for v in vals:
423                                 vline=vline+"%0.5f\t" % (v)
424                 i=i+1
425         print(vline)
426         f=open("log22.txt","a")
427         f.write(vline.replace('.',',').replace('#','.')+"\n")
428         #f.write(vline+"\n")
429         f.close()
430         gnuplot()
431         time.sleep(90)
432
433
434
435
436 id=[0x26,0xA3,0xD9,0x84,0x00,0x16,0x05,0x67]
437 print("VDD\tVAD\tTemp\tHum\tR0\tVS\tCorr\tcm\tRS\tRSR0\tVOC1\tVOC2");
438 print(owComStr(id,"85FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
439 #print(owComStr(id,"4E000F_RB4_R_P7044_R_P70B8_RBE00FFFFFFFFFFFFFFFFFF_R4E0000_RB4_R_P70BE00FFFFFFFFFFFFFFFFFF_RBE03FFFFFFFFFFFFFFFFFF"))
440
441 while(0):
442         sbl=owComStr(id,"4E000F_RB4_R_P7044_R_P70B8_RBE00FFFFFFFFFFFFFFFFFF_R4E0000_RB4_R_P70BE00FFFFFFFFFFFFFFFFFF_RBE03FFFFFFFFFFFFFFFFFF")
443         #owCom(id,[0x4E,0x00,0x0F],0)
444         #owCom(id,[0xB4],0)
445         #time.sleep(0.1)
446         #owCom(id,[0x44],0)
447         #time.sleep(0.1)
448         #owCom(id,[0xB8],0)
449         #sb=owCom(id,[0xBE,0x00],11)
450         sb=sbl[4]
451         VDD=(sb[5]+sb[6]*256)/100.0
452         #owCom(id,[0x4E,0x00,0x00],0)
453         #owCom(id,[0xB4],0)
454         #time.sleep(0.1)
455         #owCom(id,[0xB8],0)
456         #sb=owCom(id,[0xBE,0x00],11)
457         sb=sbl[7]
458         VAD=(sb[5]+sb[6]*256)/100.0
459         Temp=(sb[3]+sb[4]*256)/256.0
460         #sb=owCom(id,[0xBE,0x03],11)
461         sb=sbl[8]
462         R0=(sb[2]+sb[3]*256)/100.0
463         VS=(sb[4]+sb[5]*256)/10000.0
464         Corr=(sb[7]+sb[8]*256)/1000.0
465         cmode=sb[6]
466         RS=(3/VS-1)*30
467         RSR0=(RS/R0)
468         VOC1=RSR0*Corr
469         VOC2=RSR0/Corr
470         Hum=(VAD/VDD-0.16)/0.0062
471         Hum=(Hum)/(1.0546-0.00216*(Temp))
472         f=open("log.txt","a")
473         print("%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%i\t%0.3f\t%0.3f\t%0.3f\t%0.3f" %(VDD,VAD,Temp,Hum,R0,VS,Corr,cmode,RS,RSR0,VOC1,VOC2))
474         f.write("%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%i\t%0.3f\t%0.3f\t%0.3f\t%0.3f\n" %(VDD,VAD,Temp,Hum,R0,VS,Corr,cmode,RS,RSR0,VOC1,VOC2))
475         f.close()
476         time.sleep(30)
477
478 #cmd="owtools.exe usb1 DC 6705160084D9A326 BE00FFFFFFFFFFFFFFFFFFFF"
479 #print(owCom([0x26,0xA3,0xD9,0x84,0x00,0x16,0x05,0x67],[0xBE,0x00],11))
480
481
482