def __init__(self, config, update_freq_in_s): if config and 'mqtt_logger' in config: if self.process_config( config['mqtt_logger']) and self.config['enabled']: self.update_freq_in_s = update_freq_in_s self.client = mqttClient.Client("Python") self.translator = Translator() try: if 'user_name' in self.config and self.config[ 'user_name'] is not None: password = None if 'password' not in self.config else self.config[ 'password'] self.client.username_pw_set(self.config['user_name'], password=password) self.client.connect(host=self.config['host'], port=self.config['port']) except Exception: self.client = None self.frame_decoder = None if self.client: self.frame_decoder = FrameDecoder() self.client.loop_start() self.previous_values = {} self.last_known_duration = {} else: log.info("mqtt_logger disabled in config") self.client = None else: log.error('"mqtt_logger" section in config missing')
def __init__(self, config, update_freq_in_s, function_callback=None): self.client = None if config and 'mqtt_logger' in config: if self.process_config( config['mqtt_logger']) and self.config['enabled']: self.update_freq_in_s = update_freq_in_s self.client = mqttClient.Client("remeha logger") self.translator = Translator() try: if 'user_name' in self.config and self.config[ 'user_name'] is not None: password = None if 'password' not in self.config else self.config[ 'password'] self.client.username_pw_set(self.config['user_name'], password=password) self.client.connect(host=self.config['host'], port=self.config['port']) except Exception: self.client = None self.frame_decoder = None if self.client: self.frame_decoder = FrameDecoder() self.client.on_message = on_message self.client.user_data_set(function_callback) self.client.subscribe("{}/manual_log".format(self.topic)) self.client.loop_start() self.previous_values = {} self.last_known_duration = {} else: log.info("mqtt_logger disabled in config") self.client = None else: log.error('"mqtt_logger" section in config missing')
def convert(csv_reader): first_row = True frame_decoder = FrameDecoder() translator = Translator() for row in csv_reader: if row[2] == "258" and len(row) >= 68: integer_list = [int(value) for value in row[4:]] packed_data = bytes( struct.pack(str(len(row) - 4) + "B", *integer_list)) list_of_valuetype_and_value = frame_decoder.decode_all(packed_data) if first_row: first_row = False print_head_row(list_of_valuetype_and_value, translator) print_sample_data(list_of_valuetype_and_value, row)
def __init__(self, config): self.database = None if config and 'database_logger' in config: self.database_name = "boiler_log" self.table_name = "boiler_data" self.database = self.__check_and_init_db(config['database_logger']) if self.database: self.insert_cursor = self.database.cursor(prepared=True) self.prepared_query = {} self.number_of_uncommitted_records = 0 self.frame_decoder = FrameDecoder()
class LogToMQtt: def __init__(self, config, update_freq_in_s): if config and 'mqtt_logger' in config: if self.process_config( config['mqtt_logger']) and self.config['enabled']: self.update_freq_in_s = update_freq_in_s self.client = mqttClient.Client("Python") self.translator = Translator() try: if 'user_name' in self.config and self.config[ 'user_name'] is not None: password = None if 'password' not in self.config else self.config[ 'password'] self.client.username_pw_set(self.config['user_name'], password=password) self.client.connect(host=self.config['host'], port=self.config['port']) except Exception: self.client = None self.frame_decoder = None if self.client: self.frame_decoder = FrameDecoder() self.client.loop_start() self.previous_values = {} self.last_known_duration = {} else: log.info("mqtt_logger disabled in config") self.client = None else: log.error('"mqtt_logger" section in config missing') def process_config(self, config): self.config = config self.log_single_value_list = [] self.log_with_timestamp_list = [] self.log_duration_list = [] self.scaled_values = {} if 'enabled' not in config: self.config = None log.error('missing "enabled" in "mqtt_logger" config section') if 'host' not in config: self.config = None log.error('missing "host" in "mqtt_logger" config section') if 'port' not in config: self.config = None log.error('missing "port" in "mqtt_logger" config section') if 'password' in config and config['password'] is not None and ( 'user_name' not in config or config['user_name'] is None): self.config = None log.error('missing "user_name" in "mqtt_logger" config section') if 'log_values' in config: self.log_single_value_list = config['log_values'] if 'log_values_with_duration' in config: self.log_duration_list = config['log_values_with_duration'] if 'log_values_with_timestamp' in config: self.log_with_timestamp_list = config['log_values_with_timestamp'] if 'scale_to_percent' in config: for scales in config['scale_to_percent']: self.scaled_values[scales['value_name']] = [ scales['lower_limit'], scales['upper_limit'] ] if not self.log_duration_list and not self.log_single_value_list and not self.log_with_timestamp_list: self.config = None log.error( 'Nothing to log. Specified "log_values", "log_values_with_timestamp" or "log_values_with_duration" to "mqtt_logger" config section' ) return self.config def log(self, frame, runtime_seconds): if runtime_seconds % self.update_freq_in_s == 0: unpacked_data = frame.get_parseddata() if self.client: for value_name in self.log_single_value_list: if value_name in self.scaled_values: self.log_single_value( value_name, unpacked_data, scale_to_percent=[ self.scaled_values[value_name][0], self.scaled_values[value_name][1] ]) else: self.log_single_value(value_name, unpacked_data) for value_name in self.log_with_timestamp_list: self.log_single_value(value_name, unpacked_data, frame.timestamp) for entry in self.log_duration_list: self.log_duration_of_value(entry['value_name'], entry['expected_value'], unpacked_data, frame.timestamp) else: print("Temp: %d" % self.frame_decoder.decode(unpacked_data, 'outside_temp')) def log_single_value(self, value_name, unpacked_data, current_time=None, scale_to_percent=None): value = self.frame_decoder.decode(unpacked_data, value_name) mqtt_topic = "boiler/" + value_name if scale_to_percent: value = ((value - scale_to_percent[0]) / (scale_to_percent[1] - scale_to_percent[0])) * 100 value_format = '{:1.1f}' if isinstance(value, str): value_format = '{}' value = self.translator.translate(value) if current_time: if value_name not in self.previous_values: self.previous_values[value_name] = [current_time, '-'] previous_value = self.previous_values[value_name] if previous_value[1] != value: previous_value[1] = value previous_value[0] = current_time time_delta, unit = get_human_readable_duration_and_unit( current_time - previous_value[0]) self.client.publish(mqtt_topic, (value_format + ' ({:0.3g}{})').format( value, time_delta, unit), retain=True) else: self.client.publish(mqtt_topic, value_format.format(value), retain=True) def log_duration_of_value(self, value_name, expected_value, unpacked_data, current_time): value = self.frame_decoder.decode(unpacked_data, value_name) if value_name not in self.last_known_duration: self.last_known_duration[value_name] = None last_known_duration = self.last_known_duration[value_name] if expected_value == value: if last_known_duration is None: # start measurement self.last_known_duration[value_name] = current_time elif last_known_duration is not None: # end measurement time_delta, unit = get_human_readable_duration_and_unit( current_time - last_known_duration) self.client.publish("boiler/" + value_name + '_' + expected_value + '_duration', '{:0.3g}{}'.format(time_delta, unit), retain=True) self.last_known_duration[value_name] = None def close(self): pass