Blame view

simulators/plc/meteo_worker.py 3.46 KB
8a42ace9   Patrick Maeght   meteo_20180917.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


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())


e355ba3f   Patrick Maeght   PlcSimulator with...
22
class WorkerLog(threading.Thread):
8a42ace9   Patrick Maeght   meteo_20180917.log
23
24
25
26
27
28
29
30

    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')
d18e437e   Patrick Maeght   New json in
31
        self.time_step = 1
8a42ace9   Patrick Maeght   meteo_20180917.log
32
33
34
35

    # call by thread.start()
    def run(self):
        self.shared.worker = 'start'
f6076245   Patrick Maeght   penelope work part 1
36
        self.shared.status = 'init'
8a42ace9   Patrick Maeght   meteo_20180917.log
37
38
39
40
41
42
43
44
45
46
        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()
869dd977   Patrick Maeght   update message st...
47
            time.sleep(self.time_step)
8a42ace9   Patrick Maeght   meteo_20180917.log
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
        # 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'
869dd977   Patrick Maeght   update message st...
92
93
            self.log_input.close()
            print(self.log_file + ' rain ' + str(self.rain))
8a42ace9   Patrick Maeght   meteo_20180917.log
94
95
96
97
98
99
        line2 = line1.split(" ")
        self.set_values(line2)
        #time.sleep(0.1)

    def set_values(self, line):
        moment = datetime.fromtimestamp(float(line[1]))
869dd977   Patrick Maeght   update message st...
100
        # update message structure
8a42ace9   Patrick Maeght   meteo_20180917.log
101
        print(str(moment)+ ' '+ line[2]+ ' '+ line[4]+ ' '+ line[6]+ ' '+ line[7]+ ' '+ line[8])
a38006df   Patrick Maeght   play meteo log
102
        # resultat = get_device_attribute_value("WXT520","14656423"","OutsideTemp")
d18e437e   Patrick Maeght   New json in
103
104
105
106
107
108
        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
8a42ace9   Patrick Maeght   meteo_20180917.log
109
        if line[7] != "0.0":
869dd977   Patrick Maeght   update message st...
110
            self.rain = True