saunacontrol/remotectrl/_RemoteCtrl.py

73 lines
2.3 KiB
Python
Raw Normal View History

2021-04-19 05:57:29 +00:00
import logging
from xmlrpc.server import SimpleXMLRPCServer
import config
import control
class RemoteCtrl:
def __init__(self, host="localhost", port=64001):
self.__log = logging.getLogger()
self.__server = SimpleXMLRPCServer((host, port), allow_none=True)
self.__server.register_function(self.set_runtime, 'set_runtime')
self.__server.register_function(self.get_runtime, 'get_runtime')
self.__server.register_function(self.get_target_temperature, 'get_target_temperature')
self.__server.register_function(self.get_temperature_step, 'get_temperature_step')
self.__server.register_function(self.set_target_temperature, 'set_target_temperature')
self.__server.register_function(self.switch_on, 'switch_on')
self.__server.register_function(self.switch_off, 'switch_off')
self.__server.register_function(self.state, 'state')
self.__server.register_function(self.time_to_switch_off, 'time_to_switch_off')
self.__config = config.Config()
self.__ctrl = control.Control(int(self.__config.heat_pin()))
self.__ctrl.set_target_temperature(self.__config.target_temperature())
self.__ctrl.set_runtime(self.__config.runtime())
def set_runtime(self, runtime):
self.__ctrl.set_runtime(runtime)
def get_runtime(self):
return str(self.__ctrl.get_runtime)
def get_target_temperature(self):
return self.__ctrl.get_target_temperature()
def get_temperature_step(self):
return self.__config.temperature_step()
def set_target_temperature(self, target_temperature):
self.__ctrl.set_target_temperature(target_temperature)
self.__config.update_target_temperature(target_temperature)
def switch_on(self):
self.__ctrl.switch_on()
def switch_off(self):
self.__ctrl.switch_off()
def state(self):
return self.__ctrl.state()
def time_to_switch_off(self):
return str(self.__ctrl.time_to_switch_off())
def start(self):
self.__log.info('Control-c to quit')
self.__ctrl.start()
self.__server.serve_forever()
self.__log.info("Shutting down...")
self.__ctrl.stop()
self.switch_off()
self.__log.info("...done. Exiting...")