def to_senml(self, value): meas = senml.SenMLMeasurement() meas.name = self.id meas.value = value meas.time = int(time.time()) doc = senml.SenMLDocument([meas]) return json.dumps(doc.to_json())
def to_senml(self, t, v, u, n): meas = senml.SenMLMeasurement() meas.time = int(t) meas.value = v meas.unit = u meas.name = n return meas
def get_senml_meas(self, value, time): if not isinstance(time, float): time = float(time.timestamp()) meas = senml.SenMLMeasurement() meas.time = time meas.value = value meas.name = self.topic return meas
def get_senml_meas(self, value, time, name): if not isinstance(time, float) and not isinstance( time, int) and isinstance(time, datetime.datetime): time = float(time.timestamp()) meas = senml.SenMLMeasurement() meas.time = time meas.value = value meas.name = name return meas
def __init__(self, receiver_params, publisher_workers, config, house): self.pub_prefix = config.get("IO", "pub.topic.prefix") + str(house) + "/" self.key_level = int(config.get(house, "key.level")) self.key_separator = config.get(house, "key.separator", fallback="/") self.data_type = config.get(house, "data.type", fallback="json") self.key_map = dict(config.items("KEYS")) self.house = house self.base = senml.SenMLMeasurement() self.base.name = house + "/" super().__init__(receiver_params, publisher_workers, config, house)
def senml_message_format(self, data, current_time, dT): new_data = {} # self.logger.debug("data for senml "+str(data)) for key, value in data.items(): flag = False time = current_time u = None base = None if isinstance(value, dict): bn, n, val = self.get_names(value) else: bn, n, val = None, None, value if bn: base = senml.SenMLMeasurement() base.name = bn if key in self.mqtt_params.keys(): if self.mqtt_params[key]["unit"] is not None: u = self.mqtt_params[key]["unit"] """ else: u = "W" """ flag = self.mqtt_params[key]["horizon_values"] meas_list = [] for v in val: meas = senml.SenMLMeasurement() meas.name = n meas.time = time meas.value = v if u: meas.unit = u meas_list.append(meas) time += dT if not flag: break # only want the first value if len(meas_list) > 0: doc = senml.SenMLDocument(meas_list, base=base) new_data[key] = doc.to_json() # self.logger.debug("Topic MQTT Senml message: "+str(new_data)) return new_data
def __init__(self, meta_eq, config, monitor_connector): self.stopRequest = threading.Event() self.meta_eq = meta_eq self.variables = self.meta_eq["variables"] if "base_name" in self.meta_eq.keys(): self.base = senml.SenMLMeasurement() self.base.name = self.meta_eq["base_name"] else: self.base = None super().__init__(meta_eq["topics"], config, monitor_connector) self.sum_data_thread = threading.Thread(target=self.sum_data) self.sum_data_thread.start() logger.debug("###### start equation")
def to_senml(self, name, value, timestamp): meas = senml.SenMLMeasurement() meas.name = name if isinstance(value, str): try: value = float(value) except Exception: pass meas.value = value meas.time = timestamp doc = senml.SenMLDocument([meas], base=self.base) val = doc.to_json() val = json.dumps(val) return val
def to_senml(self, results): meas = [] base = None if "base_name" in self.topic_params: base = senml.SenMLMeasurement() base.name = self.topic_params["base_name"] for time, errors in results.items(): rmse = errors["rmse"] mae = errors["mae"] meas.append( self.get_senml_meas(rmse, time, self.topic_name + "/rmse")) meas.append( self.get_senml_meas(mae, time, self.topic_name + "/mae")) doc = senml.SenMLDocument(meas, base=base) val = doc.to_json() return json.dumps(val)
def get_data(self): try: if self.source == "constant": val = self.constant_value logger.debug("Sent MQTT:" + str(val)) return val else: meas_list = [] current_time = int(math.floor(time.time())) if self.source == "file": vals = self.get_file_line(current_time) logger.debug("Length: " + str(self.length)) if len(vals) < self.length: logger.error( str(self.generic_name) + " mock file has invalid data. Less values than horizon_step" ) return None elif self.source == "random": vals = self.get_random_floats() logger.debug("Vals: " + str(vals)) logger.debug("Length: " + str(self.length)) prev_time = 0 for index in range(self.length): meas = senml.SenMLMeasurement() if self.is_timed: meas.value = vals[index][1] if prev_time > vals[index][0]: meas.time = prev_time + self.delta_time else: meas.time = int(vals[index][0]) prev_time = meas.time else: meas.value = vals[index] meas.time = int(current_time) meas.name = self.generic_name meas_list.append(meas) current_time += self.delta_time doc = senml.SenMLDocument(meas_list) val = doc.to_json() val = json.dumps(val) logger.debug("Sent MQTT:" + str(val)) return val except Exception as e: logger.error(e)