def __init__(self, port = '/dev/ttyUSB6'): TemperatureController.__init__(self, port) """ the Temperature serial connection is on the third port at MAGIK, which is /dev/ttyUSB2 """ self.setpoint = 0.0 self.serial_eol = '\n' self.settings = { 'sample_sensor': 'B', 'control_sensor': 'A', 'record': 'all', 'units': 3, 'control_loop': 1, 'thermometer_calibration': 1, # applied to sample sensor 'serial_port': '/dev/ttyUSB6' } self.sensors = {'A': "Sensor A", 'B':"Sensor B"} valid_record = self.sensors.copy() valid_record.update({'setpoint':"Setpoint", 'RH': 'RH', 'T_raw': 'T_raw', 'T_cal': 'T_cal', 'all':"Record all"}) self.valid_settings = { 'sample_sensor': self.sensors, 'control_sensor': self.sensors, 'record': valid_record, 'units': {1: "Kelvin", 2: "Celsius", 3: "Sensor units"}, 'control_loop': {1: "1", 2: "2"}, 'thermometer_calibration': {1: "T_raw*0.97877 - 0.86433", 2: "T_raw*0.97398 - 0.58107"}, 'serial_port': dict([('/dev/ttyUSB%d' % i, 'Serial port %d' % (i+1)) for i in range(4, 16)]), #'serial_port': {"/dev/ttyS0": "/dev/ttyS0"} }
def test_get_mode_set_mode(): temp_controller = TemperatureController(20, 1, Mode.OFF) assert temp_controller.get_mode() == Mode.OFF temp_controller.set_mode(Mode.HEATER) assert temp_controller.get_mode() == Mode.HEATER temp_controller.set_mode(Mode.COOLER) assert temp_controller.get_mode() == Mode.COOLER
def __init__(self, update_server): self.temperature_controller = TemperatureController(set_temperature=20, mode=Mode.COOLER) self.sensor_controller = SensorController() self.mqtt_controller = DataController() self.user_interface = UserInterface() self.unit_controller = UnitController() self.update_server = update_server self.command_subscriber = CommandSubscriber("192.168.3.2", self.handle_command) self.current_display_data = None
def test_handle_new_reading_off(): temp_controller = TemperatureController(20, 1, Mode.OFF) assert temp_controller.handle_new_reading(25) == State.ALL_OFF assert temp_controller.handle_new_reading(22) == State.ALL_OFF assert temp_controller.handle_new_reading(20.1) == State.ALL_OFF assert temp_controller.handle_new_reading(20) == State.ALL_OFF assert temp_controller.handle_new_reading(20.1) == State.ALL_OFF assert temp_controller.handle_new_reading(20.9) == State.ALL_OFF assert temp_controller.handle_new_reading(21) == State.ALL_OFF temp_controller.shutdown()
def test_handle_new_reading_cooler(): temp_controller = TemperatureController(20, 1, Mode.COOLER) assert temp_controller.handle_new_reading(25) == State.COOLER_ON assert temp_controller.handle_new_reading(22) == State.COOLER_ON assert temp_controller.handle_new_reading(20.1) == State.COOLER_ON assert temp_controller.handle_new_reading(20) == State.ALL_OFF assert temp_controller.handle_new_reading(20.1) == State.ALL_OFF assert temp_controller.handle_new_reading(20.9) == State.ALL_OFF assert temp_controller.handle_new_reading(21) == State.COOLER_ON temp_controller.shutdown()
def test_handle_new_reading_heater(): temp_controller = TemperatureController(20, 1, Mode.HEATER) assert temp_controller.handle_new_reading(15) == State.HEATER_ON assert temp_controller.handle_new_reading(19) == State.HEATER_ON assert temp_controller.handle_new_reading(19.9) == State.HEATER_ON assert temp_controller.handle_new_reading(20.1) == State.ALL_OFF assert temp_controller.handle_new_reading(21) == State.ALL_OFF assert temp_controller.handle_new_reading(19.9) == State.ALL_OFF assert temp_controller.handle_new_reading(19.1) == State.ALL_OFF assert temp_controller.handle_new_reading(19.0) == State.HEATER_ON temp_controller.shutdown()
def __init__(self, port = '/dev/ttyUSB2'): TemperatureController.__init__(self, port) """ the Temperature serial connection is on the second port at AND/R, which is /dev/ttyUSB1 """ self.settings = { 'sample_sensor': 'I', 'control_sensor': 'I', 'record': 'all' } self.sensors = {'I': "Internal", 'X':"External"} valid_record = self.sensors.copy() valid_record.update({'Setpoint':"Setpoint", 'all':"Record all"}) self.valid_settings = { 'sample_sensor': self.sensors, 'control_sensor': self.sensors, 'record': valid_record, }
def __init__(self, port = '/dev/ttyUSB2'): TemperatureController.__init__(self, port) """ the Temperature serial connection is on the third port at MAGIK, which is /dev/ttyUSB2 """ self.setpoint = 0.0 self.serial_eol = '\n' self.settings = { 'sample_sensor': 'A', 'control_sensor': 'A', 'record': 'B', 'units': 1, 'control_loop': 1, 'serial_port': '/dev/ttyUSB6' } valid_record = self.sensors.copy() valid_record.update({'Setpoint':"Setpoint", 'all':"Record all"}) self.valid_settings = { 'sample_sensor': self.sensors, 'control_sensor': self.sensors, 'record': valid_record, 'units': {1: "Kelvin", 2: "Celsius", 3: "Sensor units"}, 'control_loop': {1: "1", 2: "2"}, 'serial_port': dict([('/dev/ttyUSB%d' % i, 'Serial port %d' % (i+1)) for i in range(4, 16)]), }
def __init__(self, port = '/dev/ttyUSB7'): TemperatureController.__init__(self, port) """ the Temperature serial connection is on the third port at MAGIK, which is /dev/ttyUSB2 """ self.setpoint = 0.0 self.wait_time = 0.0 # give the bath a moment to reply self.serial_eol = '\r\n' self.settings = { 'sample_sensor': 2, 'control_sensor': 0, 'record': 'all', 'control_loop': 0, 'serial_port': '/dev/ttyUSB7' } self.sensors = {0: "Bath temp", 2:"Process temp"} valid_record = self.sensors.copy() valid_record.update({'setpoint':"Setpoint", 'all':"Record all"}) self.valid_settings = { 'sample_sensor': self.sensors, 'control_sensor': self.sensors, 'record': valid_record, 'control_loop': {0: "0", 1: "1", 2: "2"}, 'serial_port': dict([('/dev/ttyUSB%d' % i, 'Serial port %d' % (i+1)) for i in range(4, 16)]), } self.port = self.settings['serial_port']
class Thermostat(): def __init__(self, update_server): self.temperature_controller = TemperatureController(set_temperature=20, mode=Mode.COOLER) self.sensor_controller = SensorController() self.mqtt_controller = DataController() self.user_interface = UserInterface() self.unit_controller = UnitController() self.update_server = update_server self.command_subscriber = CommandSubscriber("192.168.3.2", self.handle_command) self.current_display_data = None def startup(self): pass def run(self): self.command_subscriber.subscribe() while True: current_temperature = self.sensor_controller.get_temperature() current_state = self.temperature_controller.handle_new_reading( current_temperature) current_humidity = self.sensor_controller.get_humidity() converted_data = self.unit_controller.convert({ "temperature": current_temperature, "set_temperature": self.temperature_controller.get_set_temperature(), "humidity": current_humidity, "mode": self.temperature_controller.get_mode(), "state": current_state }) self.mqtt_controller.publish_data(converted_data) self.update_server(converted_data) self.command_subscriber.command_loop() self.current_display_data = converted_data time.sleep(1) def handle_command(self, command): command_type = command.get('command') if command_type == "SET_TEMPERATURE": self.temperature_controller.update_set_temperature( self.unit_controller.convert_incomming( command['data']['temperature'])) elif command_type == "SET_MODE": self.temperature_controller.set_mode(Mode(command['data']['mode'])) def update_set_temperature(self, set_temperature): converted_temp = UnitController.convert_f_to_c(set_temperature) self.temperature_controller.update_set_temperature(converted_temp) self.update_server({ "temperature": self.current_display_data.get('temperature'), "set_temperature": set_temperature, "humidity": self.current_display_data.get('humidity'), "mode": self.temperature_controller.get_mode(), "state": self.current_display_data.get('state') }) def shutdown(self): print('Shutting Down') self.temperature_controller.shutdown()
def test_update_set_temperature(): temp_controller = TemperatureController() interface = UserInterface(TemperatureUnits.C) interface.update_set_temperature(70, temp_controller)
def test_get_set_temperature(): temp_controller = TemperatureController(20, 1, Mode.OFF) assert temp_controller.get_set_temperature() == 20
def test_update_set_temperature(): temp_controller = TemperatureController(20, 1, Mode.OFF) assert temp_controller.update_set_temperature(25) == 25