forked from barnybug-archive/wmr100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmr-send.py
executable file
·267 lines (222 loc) · 6.6 KB
/
wmr-send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
# read wmr100-pi data from stdin, send arrregate to rs.angara.net
#
# on.environ {HWID,PSW}
#
# /etc/rc.local:
# (export WD="/home/pi/wmr100-pi"; export PSW="qwe123"; $WD/wmr100 | $WD/wmr-send.py;)&
from __future__ import print_function
import hashlib
import httplib
import time
import threading
import subprocess
import sys
import re
import os
#----#
host = "rs.angara.net"
port = 80
uri = "/dat?"
timeout = 30 # seconds
b_fix = 30 # wind bearing correction value
log_file = "/tmp/wmr100.log"
log_size = 100*1000
cycle_file = "/tmp/wmr100.cycle"
SEND_INTERVAL = 280
#----#
def perr(msg):
print(msg, file=sys.stderr)
#--
def get_hwid():
try:
s = subprocess.Popen(["ip", "link"], stdout=subprocess.PIPE).communicate()
return re.search("link/ether (..:..:..:..:..:..) ", s[0]).group(1)
except Exception as ex:
perr(str(ex))
return "???"
#--
hwid = os.environ.get("HWID") or get_hwid()
psw = os.environ.get("PSW") or ""
def calc_b(rhc):
if not rhc:
return None
print("rhc:", rhc)
m = 1
p = -1
for i,r in enumerate(rhc):
if r >= m:
m = r
p = i
#-
if p < 0:
return None
m = float(m)
m0 = float(rhc[p-1])
m1 = float(rhc[(p+1) % len(rhc)])
return int((p-(m0/m*0.5)+(m1/m*0.5))*22.5)
#-
def make_avg(d):
if not d:
return None
c = d.get('count')
if not c:
return None
v = d.get('sum')
if v is None:
return None
s = "{:.1f}".format(float(v) / c)
if s.endswith(".0"): return s[0:-2]
else: return s
#--
def sender(collected_data):
global cycle
while True:
cycle += 1
try:
if collected_data:
d = collected_data.copy()
collected_data.clear()
logf = open(log_file, "a")
print(time.strftime("%Y-%m-%d %H:%M:%S")," - ", cycle, file=logf)
qs = "hwid="+hwid+"&cycle="+str(cycle)
t = t0 = "???"
x = make_avg(d.get('t1'))
if x is not None:
qs += "&t="+x
t = x
x = make_avg(d.get('t0'))
if x is not None:
qs += "&t0="+x
t0 = x
print(" t:", str(t), str(t0), file=logf)
h = h0 = "???"
x = make_avg(d.get('h1'))
if x is not None:
qs += "&h="+x
h = x
x = make_avg(d.get('h0'))
if x is not None:
qs += "&h0="+x
h0 = x
print(" h:", str(h), str(h0), file=logf)
dp = dp0 = "???"
x = make_avg(d.get('d1'))
if x is not None:
qs += "&d="+x
dp = x
x = make_avg(d.get('d0'))
if x is not None:
qs += "&d0="+x
dp0 = x
print(" d:", str(dp), str(dp0), file=logf)
p = "???"
x = make_avg(d.get('p'))
if x is not None:
qs += "&p="+x
p = x
print(" p:", p, file=logf)
w = g = b = "???"
x = make_avg(d.get('w'))
if x is not None:
qs += "&w="+x
w = x
x = d.get('w')
if x is not None:
g = x.get('max')
if g is not None:
qs += "&g="+"{:.1f}".format(float(g))
g = "{:.1f}".format(float(g))
#
x = calc_b(d.get("rhc"))
if x is not None:
qs += "&b="+str((x+b_fix) % 360)
b = str((x+b_fix) % 360)
print( " w:", w, g, b, file=logf)
print( " rhc:", d.get("rhc","-"), file=logf)
x = d.get("rf")
if x: qs += "&rf="+x
x = d.get("pwr")
if x: qs += "&pwr="+x
print( " pwr/rf:", d.get("pwr","-"), d.get("rf","-"), file=logf)
sha1 = hashlib.sha1()
sha1.update(qs+psw)
qs += "&_hkey="+sha1.hexdigest()
conn = httplib.HTTPConnection(host, port, timeout=timeout)
conn.request("GET", uri+qs)
resp = conn.getresponse()
if resp and resp.status == 200:
print(" http: ok", file=logf)
cf = open(cycle_file, "w")
print(cycle, file=cf)
cf.close()
else:
perr("resp:"+str(resp))
print(" http:", str(resp), file=logf)
logf.close()
if os.stat(log_file).st_size > log_size:
os.rename(log_file, log_file+".old")
#
except Exception as ex:
perr("error: "+str(ex))
#
time.sleep(SEND_INTERVAL)
#
#--
def update_data(data, k, v):
v = float(v)
d = data.get(k)
if d:
if v < d['min']: d['min'] = v
if v > d['max']: d['max'] = v
d['sum'] += v
d['count'] += 1
else:
data[k] = {'min':v, 'max':v, 'sum':v, 'count':1}
#
#--
# global data
RH_NUM = 16
cycle = 0
data = {}
ct = threading.Thread(target=sender, args=(data,))
ct.start()
while True:
try:
s = sys.stdin.readline()
if s: print(s.strip())
if s[0] == '*':
sn = '0'
pwr = ''
rf = ''
for i in s[1:].split():
[k,v] = i.split('=')
if k == 'sn':
sn = v
elif k == 'rf':
if v != '0': rf = v
elif k == 'pwr':
if v != '0': pwr = v
elif k == 'r':
rhc = data.get('rhc')
if not rhc:
rhc = [0 for j in range(RH_NUM)]
data['rhc'] = rhc
#-
rhc[int(v)] += 1
elif k in 'pw': update_data(data, k, v)
elif k in 'thd': update_data(data, k+sn, v)
#
#
if pwr: data['pwr'+sn] = pwr
if rf: data['rf'] = rf
# print(data)
#
except KeyboardInterrupt:
perr("sigterm.")
import os
os._exit(0)
except Exception as e:
perr("error: "+str(e))
#-
#.