70b3e171fcc1cbc78990e36f7187d93ee245bf5e
[owSlave2.git] / stest / log_all1.py
1 import sys\r
2 import os\r
3 import os.path\r
4 import subprocess\r
5 import io\r
6 import time\r
7 import datetime\r
8 import math\r
9 import shutil\r
10 \r
11 toolstr="/home/muto/owtools/src/owtools USB1 DC "\r
12 devdict={}\r
13 devwholist=[]\r
14 f=open("DEVLIST.txt","r")\r
15 for l in f.readlines():\r
16         ls=l.split(" ")\r
17         if (len(ls)>1):\r
18                 devdict[ls[0]]=l.replace("\n","")\r
19                 devwholist.append(ls[0])\r
20 f.close()\r
21 \r
22 def id2string(id):\r
23         s=""\r
24         for i in range(8):\r
25                 s=s+"%02X" %(id[7-i])\r
26         return s\r
27 \r
28 def string2id(s):\r
29         for i in range(8):\r
30                 bl=[]\r
31                 i2=14-i*2;\r
32                 bl.append(int(s[i2:i2+2],16))\r
33         return bl\r
34 \r
35 \r
36 def calculateValues(code, V, vn):\r
37         if code==1:\r
38                 return V[vn] / 16.0; \r
39         elif code==2:\r
40                 return V[vn]/1.6; \r
41         elif code==3:\r
42                 return V[vn]*0.2 + 700; \r
43         elif code==4:\r
44                 return math.exp(V[vn] / 160.0); \r
45         elif code==5:\r
46                 return V[vn]*62.5 + 55000; \r
47         elif code==6:\r
48                 return V[vn] / 256.0; \r
49         elif code==7:\r
50                 if V[1]!=0:\r
51                         return ((float(V[2]) / float(V[1]) - 0.16) / 0.0062) / (1.0546 - 0.00216*V[0]/256.0); \r
52                 else:\r
53                         return 0\r
54         elif code==8:\r
55                 return V[vn] / 100.0; \r
56         elif code==9:\r
57                 return V[vn] / 65535.0*5.1; \r
58         elif code==10:\r
59                 return V[vn] / 65535.0*2.55; \r
60         elif code==11:\r
61                 return V[vn] / 65535.0*1.1; \r
62         elif code==12:\r
63                 return V[vn] / 10.0; \r
64         elif code==13:\r
65                 return V[vn];  \r
66         elif code==14:\r
67                 return (V[vn] - 32767.0) / 100.0;  \r
68         elif code==15:\r
69                 return math.exp((V[vn]-32767.0)/1000.0); \r
70         elif code==16:\r
71                 return V[vn]/32.0; \r
72         else:\r
73                 return 0;\r
74 \r
75 def getName(code        ):\r
76         if code==1:\r
77                 return ["Temperatur","C"]\r
78         if code==2:\r
79                 return ["Druck","hPa"]\r
80         if code==3:\r
81                 return ["Beleuchtungsstaerke","lux"]\r
82         if code==4:\r
83                 return ["Luftfeuchte","%%"]\r
84         if code==5:\r
85                 return ["Konstante",""]\r
86         if code==6:\r
87                 return ["Spannung","V"]\r
88         if code==7:\r
89                 return ["Strom","mA"]\r
90         if code==8:\r
91                 return ["VOC","ppm"]\r
92         if code==9:\r
93                 return ["Counter",""]\r
94         return []\r
95 \r
96 \r
97 def owCom(dev,send,rcount):\r
98         cmd=toolstr+id2string(dev)\r
99         #for i in range(8):\r
100         #       cmd=cmd+"%02X" %(dev[7-i])\r
101         cmd=cmd+" "\r
102         for b in send:\r
103                 cmd=cmd+"%02X" % (b)\r
104         for i in range(rcount):\r
105                 cmd=cmd+"FF"\r
106         #print(cmd.split(' '));\r
107         p = subprocess.Popen(cmd.split(' '), shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE)\r
108         p.wait()\r
109         s=p.stdout.read().decode("utf-8")\r
110         l=s.split("\n");\r
111         #print(l[2])\r
112         bl=[]\r
113         for i in range(int(len(l[2])/2)):\r
114                 i2=i*2;\r
115                 bl.append(int(l[2][i2:i2+2],16))\r
116         return bl\r
117 \r
118 def owComStr(dev,sendstr):\r
119         cmd=toolstr+id2string(dev)\r
120         #for i in range(8):\r
121         #       cmd=cmd+"%02X" %(dev[7-i])\r
122         cmd=cmd+" "+sendstr\r
123         p = subprocess.Popen(cmd.split(' '), shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE)\r
124         p.wait()\r
125         s=p.stdout.read().decode("utf-8")\r
126         #print(s);\r
127         ll=s.split("\n");\r
128         bll=[]\r
129         for l in ll[2:]:\r
130                 bl=[]\r
131                 for i in range(int(len(l)/2)):\r
132                         i2=i*2;\r
133                         bl.append(int(l[i2:i2+2],16))\r
134                 bll.append(bl)\r
135         return bll\r
136 \r
137 def getValues(dev,voc):\r
138         pl=[]\r
139         #print(id2string(dev[0]),voc)\r
140         if dev[0][0]==0x26:\r
141                 vnok=1\r
142                 rc=0\r
143                 while (vnok):\r
144                         sbl=owComStr(dev[0],"4E000F_RB4_R_P7044_R_P70B800_RBE00FFFFFFFFFFFFFFFFFF_R4E0000_RB4_R_P70B800_RBE00FFFFFFFFFFFFFFFFFF_RBE03FFFFFFFFFFFFFFFFFF")\r
145                         #print(sbl)\r
146                         sb=sbl[4]\r
147                         VDD=(sb[5]+sb[6]*256)\r
148                         sb=sbl[8]\r
149                         VAD=(sb[5]+sb[6]*256)\r
150                         Temp=(sb[3]+sb[4]*256)\r
151                         if (sb[4] & 0x80):\r
152                                 Temp-=0x10000\r
153                         curr=sb[7]+sb[8]*256\r
154                         if (sb[8] & 0x80):\r
155                                 curr-=0x10000\r
156                         vals=[Temp,VDD,VAD,curr]\r
157                         formellist=[6,8,7,8]\r
158                         if dev[1][0]!=0xFF:\r
159                                 formellist=[]\r
160                                 for i in range(4):\r
161                                         if (dev[1][i*2+1])!=0:\r
162                                                 formellist.append(dev[1][i*2+1])\r
163                                         else:\r
164                                                 break\r
165                         cvals=[]\r
166                         for i in range(len(formellist)):\r
167                                 cvals.append(calculateValues(formellist[i],vals,i))\r
168                         if (cvals[2]<=110):\r
169                                 vnok=0\r
170                         else:\r
171                                 rc=rc+1\r
172                                 if (rc==3):\r
173                                         vnok=0\r
174                                         cvals[2]=100\r
175                         if voc!=0:\r
176                                 #R0,VS,Corr,cmode,RS,RSR0,VOC1,VOC2\r
177                                 sb=sbl[9]\r
178                                 R0=(sb[2]+sb[3]*256)/100.0\r
179                                 VS=(sb[4]+sb[5]*256)/10000.0\r
180                                 Corr=(sb[7]+sb[8]*256)/1000.0\r
181                                 cmode=sb[6]\r
182                                 RS=(3/VS-1)*30\r
183                                 RSR0=(RS/R0)\r
184                                 VOC1=RSR0*Corr\r
185                                 VOC2=RSR0/Corr\r
186                                 cvals.append(R0)\r
187                                 cvals.append(VS)\r
188                                 cvals.append(Corr)\r
189                                 cvals.append(cmode)\r
190                                 cvals.append(RS)\r
191                                 cvals.append(RSR0)\r
192                                 cvals.append(VOC1)\r
193                                 cvals.append(VOC2)\r
194                 return cvals\r
195         if dev[0][0]==0x28:\r
196                 sbl=owComStr(dev[0],"44_R_PFF_PFF_PFF_PFFBEFFFFFFFFFFFFFFFFFF")\r
197                 Temp=sbl[1][1]+sbl[1][2]*256\r
198                 if (sbl[1][2] & 0x80):\r
199                         Temp=-(Temp&0x7FFF)\r
200                 if dev[1][0]!=0xFF:\r
201                         return [calculateValues(dev[1][1],[Temp],0)]\r
202                 else:\r
203                         return [calculateValues(1,[Temp],0)]\r
204         return []\r
205 \r
206         \r
207 def owList(): \r
208         cmd=toolstr[0:-3]\r
209         print(cmd)\r
210         p = subprocess.Popen(["./owtools","USB1"], shell=False, bufsize=14000, stdin=subprocess.PIPE, stdout=subprocess.PIPE)\r
211         p.wait()\r
212         s=p.stdout.read()\r
213         l=s.split("\n")\r
214         #print(l);\r
215         l=l[1:-1]\r
216         devlist=[]\r
217         ic=0\r
218         for d in l:\r
219                 dev=[]\r
220                 bl=[]\r
221                 for i in range(8):\r
222                         i2=14-i*2;\r
223                         bl.append(int(d[i2:i2+2],16))\r
224                 dev.append(bl)\r
225                 #print(bl)\r
226                 config=owCom(bl,[0x85],18)\r
227                 #print(config)\r
228                 if (config[1]==0xFF):\r
229                         if (bl[0]==0x26):\r
230                                 config=[0,1,6,6,8,4,7,7,8,0]\r
231                         if (bl[0]==0x28):\r
232                                 config=[0,1,1,0,0,0,0,0,0,0]\r
233                 dev.append(config[1:])\r
234                 bls=id2string(bl)\r
235                 cs="%i: " %(ic)\r
236                 ic=ic+1\r
237                 cs=cs+bls+": "\r
238                 if bls in devdict:\r
239                         cs=cs+devdict[bls].split(",")[0].split(" ")[1]+" "\r
240                 for i in range(4):\r
241                         c=config[i*2+1]\r
242                         if (c==0xFF):\r
243                                 cs=cs+"Noconfig "\r
244                                 config=[0,0,0,0,0,0,0,0,0,0]\r
245                                 break\r
246                         if (c==0):\r
247                                 break\r
248                         cs=cs+getName(c)[0]+" "\r
249                 #voc=0\r
250                 #if bls in devdict:\r
251                 #       if "VOC" in devdict[bls]:\r
252                 #               voc=1\r
253                 #l=getValues([bl,config[1:]],voc)\r
254                 #for v in l:\r
255                 #       cs=cs+" %0.2f " %(v)\r
256                 print(cs)\r
257                 ddev=0\r
258                 vm=0\r
259                 if config[1]==0x08:\r
260                         vm=1\r
261                 #print("Test_configs")\r
262                 i=0\r
263                 for dv in devlist:\r
264                         #print("c",dv[1][9:16])\r
265                         #print("d",bl[0:7])\r
266                         if dv[1][9:16]==bl[0:7]:\r
267                                 print("Double Device")\r
268                                 ddev=1\r
269                                 dv.append(dev[0])\r
270                                 dv.append(dev[1])\r
271                                 #if vm==1:\r
272                                         #voc_marker[i]=2\r
273                         i=i+1\r
274                 #if ddev==0:\r
275                 devlist.append(dev)\r
276                         #voc_marker.append(vm)\r
277                 #print("devlist",devlist)\r
278         return devlist\r
279 \r
280 dl=owList()\r
281 #for d in dl:\r
282 #       print(d)\r
283 \r
284 while (1):\r
285         lt = time.localtime()\r
286         vline=time.strftime("%d#%m#%Y %H:%M:%S\t", lt)\r
287         n = datetime.datetime.now()\r
288         unix_timestamp = time.mktime(n.timetuple())# works if Python >= 3.3\r
289         for d in dl:\r
290                 ds=id2string(d[0])\r
291                 bezeichnung="not in List"\r
292                 if ds in devdict:\r
293                         bezeichnung=devdict[ds].split(",")[0].split(" ")[1]+" "\r
294                 vnames=[]\r
295                 for i in range(4):\r
296                         c=d[1][i*2]\r
297                         if (c==0):\r
298                                 break\r
299                         name=getName(c)\r
300                         if name!=[]:\r
301                                 vnames.append(name[0])\r
302                 voc=0\r
303                 if "VOC" in bezeichnung:\r
304                         voc=1\r
305                         vnames.append("R0")\r
306                         vnames.append("VS")\r
307                         vnames.append("Corr")\r
308                         vnames.append("cmode")\r
309                         vnames.append("RS")\r
310                         vnames.append("RSR0")\r
311                         vnames.append("VOC1")\r
312                         vnames.append("VOC2")\r
313                 #print(ds)\r
314                 l=getValues([d[0],d[1]],voc)\r
315                 s=vline+"%i" %(unix_timestamp)\r
316                 for i in range(len(l)):\r
317                         print("%s %s: %0.2f " %(bezeichnung,vnames[i],l[i]))\r
318                         s=s+"\t%0.4f" % (l[i])\r
319                 fname="log_%s.ow1" %(ds)\r
320                 writeheader=1\r
321                 if os.path.exists(fname):\r
322                         writeheader=0\r
323                 f=open(fname,"a")\r
324                 if writeheader==1:\r
325                         sh="time\tunixtime"\r
326                         for i in range(len(l)):\r
327                                 sh=sh+"\t%s" % (vnames[i])\r
328                         f.write(sh+"\n")\r
329                 f.write(s.replace(".",",").replace("#",".")+"\n")\r
330                 f.close()\r
331         #execfile("separate1.py")\r
332         os.system("python separate1.py")\r
333         print("--------------------")\r
334         n = datetime.datetime.now()\r
335         unix_timestamp1 = time.mktime(n.timetuple())# works if Python >= 3.3\r
336         d=unix_timestamp1-unix_timestamp\r
337         print (d)\r
338         print("--------------------")\r
339         time.sleep(120-d)\r
340         \r
341