meteo_worker.py
3.46 KB
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
import logging
import threading
import time
from datetime import datetime
logger = logging.getLogger(__name__)
class WorkerState(object):
"""
worker: start/stop
status: wait/init/work...
"""
def __init__(self):
self.worker = None
self.status = None
self.status_time = str(datetime.now())
class WorkerLog(threading.Thread):
def __init__(self, shared, msg_struct):
threading.Thread.__init__(self)
self.shared = shared
self.msg_struct = msg_struct
self.rain = False
self.log_file = "meteo_20180917.log"
self.log_input = open(self.log_file, 'r')
self.time_step = 1
# call by thread.start()
def run(self):
self.shared.worker = 'start'
self.shared.status = 'init'
logger.info('Worker up')
# as lonq worker = start
while self.shared.worker == 'start':
# status (init, write)
if self.shared.status == 'wait':
pass
elif self.shared.status == 'init':
self.try_init()
elif self.shared.status == 'work':
self.try_work()
time.sleep(self.time_step)
# End work
logger.info('Worker down')
self.shared.status = "off"
return
def try_init(self):
try:
self.init_read()
self.shared.status = 'work'
self.shared.status_time = str(datetime.now())
except Exception as e:
logger.info('Worker init error ' + str(e))
self.shared.status = 'wait'
def try_work(self):
try:
self.work()
except Exception as e:
logger.info('Worker error ' + str(e))
self.shared.status = 'wait'
def work(self):
#logger.info('Working '+ str(self.item))
self.read_line()
def stop(self):
self.shared.worker = 'stop'
def init_read(self):
"""
# vantagepro_StationTime (julian day)
...
2458312.1792823728 17.4 21.1 87.0 67.0 914.8 0.0 0.00 195.0 89.20 18.60 65.40 19.50 1 45.87 7.69 0.21 289.00 0
"""
line = self.log_input.readline().split(" ")
while line[0] == "#":
line = self.log_input.readline().split(" ")
self.set_values(line)
def read_line(self):
line1 = self.log_input.readline()
if line1 == '':
self.shared.worker = 'stop'
self.log_input.close()
print(self.log_file + ' rain ' + str(self.rain))
line2 = line1.split(" ")
self.set_values(line2)
#time.sleep(0.1)
def set_values(self, line):
moment = datetime.fromtimestamp(float(line[1]))
# update message structure
print(str(moment)+ ' '+ line[2]+ ' '+ line[4]+ ' '+ line[6]+ ' '+ line[7]+ ' '+ line[8])
# resultat = get_device_attribute_value("WXT520","14656423"","OutsideTemp")
self.msg_struct['devices'][0]['device_values'][0]['value'] = line[2] #OutsideTemp
self.msg_struct['devices'][0]['device_values'][1]['value'] = line[4] #OutsideHumidity
self.msg_struct['devices'][0]['device_values'][2]['value'] = line[6] #Pressure
self.msg_struct['devices'][0]['device_values'][3]['value'] = line[7] #RainRate
self.msg_struct['devices'][0]['device_values'][4]['value'] = line[8] #WindSpeed
self.msg_struct['devices'][0]['device_values'][5]['value'] = line[9] #WindDir
if line[7] != "0.0":
self.rain = True