#!/usr/bin/env python # -*- coding: UTF-8 -*- try: import pifacedigitalio except ImportError: pifacedigitalio = None import config import geo import json import logging import mqtt import state_machine import task import random import socket import time try: from config import APP_NAME as ROOT_LOGGER_NAME except ImportError: ROOT_LOGGER_NAME = 'root' logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) geo_position = geo.gps.coordinate(lat=49.976596, lon=9.1481443) class pi_face(object): LOG_PREFIX = 'PiFace:' PF_OUT_NAMES = ['Output 0', # 0 'Output 1', # 1 'Output 2', # 2 'Ploenlein', # 3 'Bakery', # 4 'Mill', # 5 'Reese House', # 6 'Bake House'] # 7 def __init__(self, client): self.__client__ = client if pifacedigitalio is not None: pifacedigitalio.init() pi = pifacedigitalio.PiFaceDigital() self.__pf_outputs__ = pi.output_pins self.__pf_output_states__ = 8 * [False] # self._reload = 0 self._task_10ms = task.periodic(0.01 - 0.007, self.task_10ms) # for i in range(0, 8): self.set_output(i, self.__pf_output_states__[i]) def set_output(self, index, state, tries=5): tries = min(max(1, tries), 5) try_txt = ['1st', '2nd', '3rd', '4th', '5th'] state = state is True if pifacedigitalio is not None: cnt = 0 while (cnt < tries) and (self.__pf_outputs__[index].value != state): self.__pf_outputs__[index].value = 1 * state time.sleep(0.1) cnt += 1 if self.__pf_outputs__[index].value != state: logger.warning('%s Control of output[%d] (%s) after %s try not successfull!', self.LOG_PREFIX, index, self.PF_OUT_NAMES[index], try_txt[cnt - 1]) else: logger.info('%s Set output "%s" to %s.', self.LOG_PREFIX, self.PF_OUT_NAMES[index], repr(state)) self.__pf_output_states__[index] = self.__pf_outputs__[index].value == 1 else: logger.info('%s Set virtual output "%s" to %s.', self.LOG_PREFIX, self.PF_OUT_NAMES[index], repr(state)) self.__pf_output_states__[index] = state def get_output(self, index): return self.__pf_output_states__[index] def get_ploenlein(self): return self.get_output(3) def set_bakery(self, state): self.set_output(4, state) def get_bakery(self): return self.get_output(4) def set_mill(self, state): self.set_output(5, state) def get_mill(self): return self.get_output(5) def set_ploenlein(self, state): self.set_output(3, state) def set_reese_house(self, state): self.set_output(6, state) def get_reese_house(self): return self.get_output(6) def set_bake_house(self, state): if state is True: self.set_output(7, state) self._task_10ms.run() elif state is False: self._task_10ms.stop() self.join() time.sleep(0.1) self.set_output(7, state) def get_bake_house(self): return not self._task_10ms._stopped def task_10ms(self, task_inst): if self._reload <= 0: if pifacedigitalio is not None: self.__pf_outputs__[7].turn_on() self._reload = random.choice(2 * [0] + 20 * [1] + 1 * [2]) else: if pifacedigitalio is not None: self.__pf_outputs__[7].turn_off() self._reload -= 1 def join(self): self._task_10ms.join() def stop(self): self._task_10ms.stop() self._task_10ms.join() for i in range(0, 8): self.set_output(i, False) pifacedigitalio.deinit() def __del__(self): self.stop() class state_machine_mode(state_machine.state_machine): LOG_PREFIX = 'LeykMode:' STATE_AUTOMATIC = 'automatic' STATE_MANUAL = 'manual' CONDITION_EXTERNAL_TRIGGER = 'external_trigger' TRANSITIONS = { STATE_AUTOMATIC: ( (CONDITION_EXTERNAL_TRIGGER, 1, STATE_MANUAL), ), STATE_MANUAL: ( (CONDITION_EXTERNAL_TRIGGER, 1, STATE_AUTOMATIC), ), } def __init__(self, **kwargs): state_machine.state_machine.__init__(self, self.STATE_AUTOMATIC, logging.INFO) self.__reset_triggers__() def __reset_triggers__(self): self.__to_automatic__ = False self.__to_manual__ = False def external_trigger(self): rv = False if self.this_state() == self.STATE_AUTOMATIC: rv = self.__to_manual__ elif self.this_state() == self.STATE_MANUAL: rv = self.__to_automatic__ self.__reset_triggers__() return rv def trigger_to_maual(self): if self.this_state() == self.STATE_AUTOMATIC: self.__to_manual__ = True return True return False def trigger_to_automatic(self): if self.this_state() == self.STATE_MANUAL: self.__to_automatic__ = True return True return False class state_machine_day_state(state_machine.state_machine): LOG_PREFIX = 'LeykState:' STATE_IDLE = 'idle' STATE_WAKE = 'wake' STATE_SUNRISE = 'sunrise' STATE_SUNSET = 'sunset' STATE_SLEEP = 'sleep' CONDITION_WAKE = 'condition_wake' CONDITION_SUNRISE = 'condition_sunrise' CONDITION_SUNSET = 'condition_sunset' CONDITION_SLEEP = 'condition_sleep' CONDITION_IDLE = 'condition_idle' TRANSITIONS = { STATE_IDLE: ( (CONDITION_WAKE, 1, STATE_WAKE), (CONDITION_SUNRISE, 1, STATE_SUNRISE), (CONDITION_SUNSET, 1, STATE_SUNSET), (CONDITION_SLEEP, 1, STATE_SLEEP), ), STATE_WAKE: ( (CONDITION_SUNRISE, 1, STATE_SUNRISE), (CONDITION_IDLE, 1, STATE_IDLE), ), STATE_SUNRISE: ( (CONDITION_SLEEP, 1, STATE_SLEEP), (CONDITION_SUNSET, 1, STATE_SUNSET), (CONDITION_IDLE, 1, STATE_IDLE), ), STATE_SUNSET: ( (CONDITION_SLEEP, 1, STATE_SLEEP), (CONDITION_IDLE, 1, STATE_IDLE), ), STATE_SLEEP: ( (CONDITION_WAKE, 1, STATE_WAKE), (CONDITION_SUNRISE, 1, STATE_SUNRISE), (CONDITION_IDLE, 1, STATE_IDLE), ), } def __init__(self, **kwargs): state_machine.state_machine.__init__(self, self.STATE_IDLE, logging.INFO, **kwargs) def __current_state_calc__(self): def wake_time(): tm = time.localtime() tm = list(tm) tm[3] = 6 # tm_hour tm[4] = 0 # tm_min tm[5] = 0 # tm_sec=0 return time.mktime(time.struct_time(tm)) def sunrise_time(): return time.mktime(geo.sun.sunrise(geo_position)) + 30 * 60 def sunset_time(): return time.mktime(geo.sun.sunset(geo_position)) - 30 * 60 def sleep_time(): tm = time.localtime() tm = list(tm) tm[3] = 21 # tm_hour tm[4] = 15 # tm_min tm[5] = 0 # tm_sec=0 return time.mktime(time.struct_time(tm)) now = time.mktime(time.localtime()) if now > sleep_time(): return self.STATE_SLEEP elif now > sunset_time(): return self.STATE_SUNSET elif now > sunrise_time(): return self.STATE_SUNRISE elif now > wake_time(): return self.STATE_WAKE else: return self.STATE_IDLE def condition_wake(self): if self.condition_idle(): return False return self.__current_state_calc__() == self.STATE_WAKE def condition_sunrise(self): if self.condition_idle(): return False return self.__current_state_calc__() == self.STATE_SUNRISE def condition_sunset(self): if self.condition_idle(): return False return self.__current_state_calc__() == self.STATE_SUNSET def condition_sleep(self): if self.condition_idle(): return False return self.__current_state_calc__() == self.STATE_SLEEP def condition_idle(self): return not self.sm_mode.this_state_is(self.sm_mode.STATE_AUTOMATIC) def report_sunset_sunrise(self): state_machine.logger.debug('Sunrise: %s - Sunset: %s;', time.strftime("%H:%M", geo.sun.sunrise(geo_position)), time.strftime("%H:%M", geo.sun.sunset(geo_position)) ) class leyk(object): LOG_PREFIX = 'Leyk:' TOPIC_BAKE_HOUSE = config.MQTT_TOPIC + '/status/Bake House' TOPIC_BAKERY = config.MQTT_TOPIC + '/status/Bakery' TOPIC_MILL = config.MQTT_TOPIC + '/status/Mill' TOPIC_MODE = config.MQTT_TOPIC + '/status/mode' TOPIC_MODE_BOOL = config.MQTT_TOPIC + '/status/mode_bool' TOPIC_PLOENLEIN = config.MQTT_TOPIC + '/status/Ploenlein' TOPIC_REESE_HOUSE = config.MQTT_TOPIC + '/status/Reese House' TOPIC_STATE = config.MQTT_TOPIC + '/status/state' RX_TOPIC_BAKE_HOUSE = config.MQTT_TOPIC + '/set/Bake House' RX_TOPIC_BAKERY = config.MQTT_TOPIC + '/set/Bakery' RX_TOPIC_MILL = config.MQTT_TOPIC + '/set/Mill' RX_TOPIC_MODE = config.MQTT_TOPIC + '/set/mode' RX_TOPIC_PLOENLEIN = config.MQTT_TOPIC + '/set/Ploenlein' RX_TOPIC_REESE_HOUSE = config.MQTT_TOPIC + '/set/Reese House' RX_TOPICS = [ RX_TOPIC_BAKE_HOUSE, RX_TOPIC_BAKERY, RX_TOPIC_MILL, RX_TOPIC_MODE, RX_TOPIC_PLOENLEIN, RX_TOPIC_REESE_HOUSE ] def __init__(self): self.__client__ = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS) self.__client__.add_callback(self.RX_TOPIC_BAKE_HOUSE, self.__rx_bake_house__) self.__client__.add_callback(self.RX_TOPIC_BAKERY, self.__rx_bakery__) self.__client__.add_callback(self.RX_TOPIC_MILL, self.__rx_mill__) self.__client__.add_callback(self.RX_TOPIC_PLOENLEIN, self.__rx_ploenlein__) self.__client__.add_callback(self.RX_TOPIC_REESE_HOUSE, self.__rx_reese_house__) self.__client__.add_callback(self.RX_TOPIC_MODE, self.__rx_mode__) self.TOPIC_DATA = { self.TOPIC_BAKE_HOUSE: self.get_bake_house, self.TOPIC_BAKERY: self.get_bakery, self.TOPIC_MILL: self.get_mill, self.TOPIC_MODE: self.get_mode, self.TOPIC_MODE_BOOL: self.get_mode_bool, self.TOPIC_PLOENLEIN: self.get_ploenlein, self.TOPIC_REESE_HOUSE: self.get_reese_house, self.TOPIC_STATE: self.get_state } self.sm_mode = state_machine_mode() self.sm_day_state = state_machine_day_state(sm_mode=self.sm_mode) self.__pf__ = pi_face(self.__client__) self._queue = task.threaded_queue() self._queue.run() self._task_1s = task.periodic(1, self.task_1s) self._task_1s.run() self.sm_mode.register_state_change_callback(self.sm_mode.STATE_MANUAL, None, self._queue.clean_queue) self.sm_mode.register_state_change_callback(None, None, self.publish, self.TOPIC_MODE) self.sm_mode.register_state_change_callback(None, None, self.publish, self.TOPIC_MODE_BOOL) self.sm_day_state.register_state_change_callback(None, None, self.clean_queue) self.sm_day_state.register_state_change_callback(None, None, self.publish, self.TOPIC_STATE) self.sm_day_state.register_state_change_callback(self.sm_day_state.STATE_SLEEP, None, self.sm_day_state.report_sunset_sunrise) self.sm_day_state.register_state_change_callback(self.sm_day_state.STATE_SLEEP, None, self.fill_sleep_queue) self.sm_day_state.register_state_change_callback(self.sm_day_state.STATE_SUNRISE, None, self.sm_day_state.report_sunset_sunrise) self.sm_day_state.register_state_change_callback(self.sm_day_state.STATE_SUNRISE, None, self.fill_sunrise_queue) self.sm_day_state.register_state_change_callback(self.sm_day_state.STATE_SUNSET, None, self.sm_day_state.report_sunset_sunrise) self.sm_day_state.register_state_change_callback(self.sm_day_state.STATE_SUNSET, None, self.fill_sunset_queue) self.sm_day_state.register_state_change_callback(self.sm_day_state.STATE_WAKE, None, self.sm_day_state.report_sunset_sunrise) self.sm_day_state.register_state_change_callback(self.sm_day_state.STATE_WAKE, None, self.fill_wake_queue) self.publish(self.TOPIC_MODE) self.publish(self.TOPIC_MODE_BOOL) self.publish(self.TOPIC_STATE) def __rx_bake_house__(self, client, userdata, message): self.set_bake_house(message.payload == b'true') def __rx_bakery__(self, client, userdata, message): self.set_bakery(message.payload == b'true') def __rx_mill__(self, client, userdata, message): self.set_mill(message.payload == b'true') def __rx_ploenlein__(self, client, userdata, message): self.set_ploenlein(message.payload == b'true') def __rx_reese_house__(self, client, userdata, message): self.set_reese_house(message.payload == b'true') def __rx_mode__(self, client, userdata, message): self.set_mode(self.sm_mode.STATE_AUTOMATIC if message.payload == b'true' else self.sm_mode.STATE_MANUAL) def publish_states(self): for name in self.TOPIC_DATA: self.publish(name) def publish(self, topic): logger.info("Sending Leyk status information to mqtt %s = %s", topic, json.dumps(self.TOPIC_DATA[topic]())) try: self.__client__.send(topic, json.dumps(self.TOPIC_DATA[topic]())) except (socket.timeout, OSError) as e: logger.warning("Error while sending state information information") def set_mode(self, mode): if mode == self.sm_mode.STATE_AUTOMATIC: rv = self.sm_mode.trigger_to_automatic() elif mode == self.sm_mode.STATE_MANUAL: rv = self.sm_mode.trigger_to_maual() else: rv = False return rv def get_mode(self): return self.sm_mode.this_state() def get_mode_bool(self): return self.get_mode() == self.sm_mode.STATE_AUTOMATIC def get_state(self): return self.sm_day_state.this_state() def __queue_wrapper__(self, queue_inst, function, *args, **kwargs): function(*args, **kwargs) def set_ploenlein(self, state, force=False): if force or self.sm_mode.this_state_is(self.sm_mode.STATE_MANUAL): self.__pf__.set_ploenlein(state) self.publish(self.TOPIC_PLOENLEIN) return True else: return False def get_ploenlein(self): return self.__pf__.get_ploenlein() def set_bakery(self, state, force=False): if force or self.sm_mode.this_state_is(self.sm_mode.STATE_MANUAL): self.__pf__.set_bakery(state) self.publish(self.TOPIC_BAKERY) return True else: return False def get_bakery(self): return self.__pf__.get_bakery() def set_bake_house(self, state, force=False): if force or self.sm_mode.this_state_is(self.sm_mode.STATE_MANUAL): self.__pf__.set_bake_house(state) self.publish(self.TOPIC_BAKE_HOUSE) return True else: return False def get_bake_house(self): return self.__pf__.get_bake_house() def set_mill(self, state, force=False): if force or self.sm_mode.this_state_is(self.sm_mode.STATE_MANUAL): self.__pf__.set_mill(state) self.publish(self.TOPIC_MILL) return True else: return False def get_mill(self): return self.__pf__.get_mill() def set_reese_house(self, state, force=False): if force or self.sm_mode.this_state_is(self.sm_mode.STATE_MANUAL): self.__pf__.set_reese_house(state) self.publish(self.TOPIC_REESE_HOUSE) return True else: return False def get_reese_house(self): return self.__pf__.get_reese_house() def wake_time(self): tm = time.localtime() tm = list(tm) tm[3] = 6 # tm_hour tm[4] = 0 # tm_min tm[5] = 0 # tm_sec=0 return time.mktime(time.struct_time(tm)) def sunrise_time(self): return time.mktime(geo.sun.sunrise(geo_position)) + 30 * 60 def sunset_time(self): return time.mktime(geo.sun.sunset(geo_position)) - 30 * 60 def sleep_time(self): tm = time.localtime() tm = list(tm) tm[3] = 21 # tm_hour tm[4] = 15 # tm_min tm[5] = 0 # tm_sec=0 return time.mktime(time.struct_time(tm)) def identify_current_state(self): now = time.mktime(time.localtime()) if now > self.sleep_time(): return self.ST_SLEEP elif now > self.sunset_time(): return self.ST_SUNSET elif now > self.sunrise_time(): return self.ST_SUNRISE elif now > self.wake_time(): return self.ST_WAKE else: return self.ST_SLEEP def wait(self, queue_inst, delay): if delay > 0: logger.debug('%s Wait for %d seconds initiated. %d elements left in queue.', self.LOG_PREFIX, delay, queue_inst.qsize()) cnt = 0 while cnt < delay * 5: if queue_inst.qsize() == 0: logger.debug('%s Quit wait for %d seconds. %d elements left in queue.', self.LOG_PREFIX, delay, queue_inst.qsize()) break time.sleep(0.2) cnt += 1 def clean_queue(self): if self._queue.qsize() > 0: logger.info('Cleaning up remaining %d elements from queue', self._queue.qsize()) self._queue.clean_queue() def fill_wake_queue(self): no_delay = self.sm_day_state.previous_state_was(self.sm_day_state.STATE_IDLE) # WAKE self._queue.enqueue(1, self.__queue_wrapper__, self.set_bakery, True, True) self._queue.enqueue(2, self.wait, 0 if no_delay else 5 * 60) self._queue.enqueue(3, self.__queue_wrapper__, self.set_bake_house, True, True) self._queue.enqueue(4, self.wait, 0 if no_delay else 10 * 60) self._queue.enqueue(5, self.__queue_wrapper__, self.set_reese_house, True, True) self._queue.enqueue(6, self.wait, 0 if no_delay else 7 * 60) self._queue.enqueue(7, self.__queue_wrapper__, self.set_ploenlein, True, True) self._queue.enqueue(8, self.wait, 0 if no_delay else 8 * 60) self._queue.enqueue(9, self.__queue_wrapper__, self.set_mill, True, True) def fill_sunrise_queue(self): no_delay = self.sm_day_state.previous_state_was(self.sm_day_state.STATE_IDLE) # SUNRISE self._queue.enqueue(1, self.__queue_wrapper__, self.set_bake_house, True, True) self._queue.enqueue(2, self.__queue_wrapper__, self.set_ploenlein, False, True) self._queue.enqueue(3, self.wait, 0 if no_delay else 5 * 60) self._queue.enqueue(4, self.__queue_wrapper__, self.set_mill, False, True) self._queue.enqueue(5, self.wait, 0 if no_delay else 8 * 60) self._queue.enqueue(6, self.__queue_wrapper__, self.set_bakery, False, True) self._queue.enqueue(7, self.wait, 0 if no_delay else 12 * 60) self._queue.enqueue(8, self.__queue_wrapper__, self.set_reese_house, False, True) def fill_sunset_queue(self): no_delay = self.sm_day_state.previous_state_was(self.sm_day_state.STATE_IDLE) # SUNSET self._queue.enqueue(1, self.__queue_wrapper__, self.set_bake_house, True, True) self._queue.enqueue(2, self.__queue_wrapper__, self.set_bakery, True, True) self._queue.enqueue(3, self.wait, 0 if no_delay else 10 * 60) self._queue.enqueue(4, self.__queue_wrapper__, self.set_reese_house, True, True) self._queue.enqueue(5, self.wait, 0 if no_delay else 8 * 60) self._queue.enqueue(6, self.__queue_wrapper__, self.set_mill, True, True) self._queue.enqueue(7, self.wait, 0 if no_delay else 7 * 60) self._queue.enqueue(8, self.__queue_wrapper__, self.set_ploenlein, True, True) def fill_sleep_queue(self): no_delay = self.sm_day_state.previous_state_was(self.sm_day_state.STATE_IDLE) # SLEEP self._queue.enqueue(1, self.__queue_wrapper__, self.set_bake_house, False, True) self._queue.enqueue(2, self.wait, 0 if no_delay else 5 * 60) self._queue.enqueue(3, self.__queue_wrapper__, self.set_bakery, False, True) self._queue.enqueue(4, self.wait, 0 if no_delay else 9 * 60) self._queue.enqueue(5, self.__queue_wrapper__, self.set_ploenlein, False, True) self._queue.enqueue(6, self.wait, 0 if no_delay else 9 * 60) self._queue.enqueue(7, self.__queue_wrapper__, self.set_mill, False, True) self._queue.enqueue(8, self.wait, 0 if no_delay else 6 * 60) self._queue.enqueue(9, self.__queue_wrapper__, self.set_reese_house, False, True) def task_1s(self, task_inst): self.sm_mode.work() self.sm_day_state.work() def join(self): self._task_1s.join() self._queue.join() def stop(self): self._task_1s.stop() self._queue.stop() def __del__(self): self.__client__.loop_stop() # stop the loop self.stop()