meteo_worker.py 3.46 KB


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