def delete_output(id): # noqa: E501 """Deletes the output of the framework # noqa: E501 :param id: Name of the registry to be deleted :type id: str :rtype: None """ redisDB = RedisDB() output_keys = redisDB.get_keys_for_pattern("o:" + id + ":*") if output_keys is not None: for key in output_keys: redisDB.remove(key) return "success"
def get_output(id): # noqa: E501 """Get ouput of the optimization # noqa: E501 :param id: Name of the registry to be actualized :type id: str :rtype: OptimizationOutput """ result = {} redisDB = RedisDB() output_keys = redisDB.get_keys_for_pattern("o:" + id + ":*") if output_keys is not None: meta = redisDB.get("id_meta:" + id) if meta is not None: meta = json.loads(meta) dT = meta["dT_in_seconds"] for key in output_keys: sub_key = key.split(":") topic = sub_key[2] index = sub_key[3] json_value = redisDB.get(key) json_value = json.loads(json_value) time = None value = 0 for t, v in json_value.items(): time = t value = v break if topic not in result.keys(): result[topic] = {} if time is not None: t = float(time) + int(index) * dT result[topic][t] = float(value) logger.debug(result) return OptimizationOutput.from_dict(result)
class OutputController: def __init__(self, id=None, output_config=None): self.logger = MessageLogger.get_logger(__name__, id) self.logger.info("Output Class started") self.output_config = output_config self.mqtt = {} self.redisDB = RedisDB() self.mqtt_params = {} self.output_mqtt = {} self.id = id self.config_parser_utils = ConfigParserUtils() self.logger.debug("output_config: " + str(self.output_config) + " " + str(type(self.output_config))) if self.output_config is not None: self.extract_mqtt_params() self.init_mqtt() def extract_mqtt_params(self): self.logger.debug("Output config = " + str(self.output_config)) for key, value in self.output_config.items(): self.logger.debug("key " + str(key) + " value " + str(value)) for key2, value2 in value.items(): self.logger.debug("key2 " + str(key2) + " value2 " + str(value2)) mqtt = self.config_parser_utils.get_mqtt(value2) unit, horizon_values = self.read_extra_values(value2) if mqtt is not None: self.mqtt_params[key2] = mqtt.copy() self.mqtt_params[key2]["unit"] = unit self.mqtt_params[key2]["horizon_values"] = horizon_values self.logger.debug("params = " + str(self.mqtt_params)) def read_extra_values(self, value2): unit = None horizon_values = False if isinstance(value2, dict): if "unit" in value2.keys(): unit = value2["unit"] if "horizon_values" in value2.keys(): horizon_values = value2["horizon_values"] return unit, horizon_values def init_mqtt(self): ###Connection to the mqtt broker self.logger.debug("Starting init mqtt") self.redisDB.set("Error mqtt" + self.id, False) try: for key, value in self.mqtt_params.items(): self.logger.debug("key " + str(key) + " value " + str(value)) # self.output_mqtt[key2] = {"host":host, "topic":topic, "qos":qos} client_id = "client_publish" + str(randrange(100000)) + str(time.time()).replace(".", "") host = str(value["host"]) port = value["mqtt.port"] self.logger.debug("client " + client_id) self.logger.debug("host " + host) self.logger.debug("port " + str(port)) client_key = host+":"+str(port) if client_key not in self.mqtt.keys(): self.mqtt[client_key] = MQTTClient(str(host), port, client_id, username=value["username"], password=value["password"], ca_cert_path=value["ca_cert_path"], set_insecure=value["insecure"], id=self.id) self.logger.info("successfully subscribed") except Exception as e: self.logger.debug("Exception while starting mqtt") self.redisDB.set("Error mqtt" + self.id, True) self.logger.error(e) def publish_data(self, id, data, dT): self.logger.debug("output data : "+ json.dumps(data, indent=4)) current_time = int(time.time()) try: senml_data = self.senml_message_format(data, current_time, dT) for mqtt_key, value in senml_data.items(): v = json.dumps(value) # self.logger.debug("key: "+str(key)) # self.logger.debug("mqtt params: " + str(self.mqtt_params.keys())) if mqtt_key in self.mqtt_params.keys(): value2 = self.mqtt_params[mqtt_key] topic = value2["topic"] host = value2["host"] port = value2["mqtt.port"] qos = value2["qos"] client_key = host + ":" + str(port) self.mqtt[client_key].sendResults(topic, v, qos) except Exception as e: self.logger.error("error in publish data ", e) self.save_to_redis(id, data, current_time) def Stop(self): self.stop_request = True try: for key, value in self.mqtt_params.items(): self.logger.debug("key " + str(key) + " value " + str(value)) self.mqtt[key].MQTTExit() self.logger.info("OutputController safe exit") except Exception as e: self.logger.error(e) 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 save_to_redis(self, id, data, time): try: part_key = "o:" + id + ":" output_keys = self.redisDB.get_keys_for_pattern(part_key+"*") if output_keys is not None: for key in output_keys: self.redisDB.remove(key) for key, value in data.items(): key = key.replace("~","/") if isinstance(value, dict): bn, n, val = self.get_names(value) else: bn, n, val = None, key, value if bn: n = bn + "/" + n index = 0 for v in val: k = part_key + n + ":" + str(index) self.redisDB.set(k, json.dumps({str(time): v})) index += 1 except Exception as e: self.logger.error("error adding to redis " + str(e)) def get_names(self, dict): bn = None n = None v = None if "bn" in dict.keys(): bn = dict["bn"] if "n" in dict.keys(): n = dict["n"] if "v" in dict.keys(): v = dict["v"] return bn,n,v
class CommandController: _instance = None _lock = threading.Lock() def __new__(cls): if CommandController._instance is None: with CommandController._lock: if CommandController._instance is None: CommandController._instance = super( CommandController, cls).__new__(cls) return CommandController._instance def __init__(self): self.factory = {} self.statusThread = {} self.running = {} self.redisDB = RedisDB() self.lock_key = "id_lock" def set(self, id, object): self.factory[id] = object def get_length_factory(self): return len(self.factory) def get(self, id): return self.factory[id] def set_isRunning(self, id, bool): self.running[id] = bool def isRunningExists(self): logger.debug("IsRunning exists: " + str(len(self.running))) if len(self.running): return True else: return False def get_isRunning(self, id): if id in self.running.keys(): return self.running[id] else: return False def get_running(self): return self.running def get_statusThread(self, id): return self.statusThread[id] def start(self, id, json_object, dict_object=None): logger.debug(str(json_object)) if json_object is not None: self.model_name = json_object.model_name self.control_frequency = json_object.control_frequency self.horizon_in_steps = json_object.horizon_in_steps self.dT_in_seconds = json_object.d_t_in_seconds self.repetition = json_object.repetition self.solver = json_object.solver self.optimization_type = json_object.optimization_type self.single_ev = json_object.single_ev elif dict_object is not None: self.model_name = dict_object["model"] self.control_frequency = dict_object["control_frequency"] self.horizon_in_steps = dict_object["horizon_in_steps"] self.dT_in_seconds = dict_object["dT_in_seconds"] self.repetition = dict_object["repetition"] self.solver = dict_object["solver"] self.optimization_type = dict_object["optimization_type"] self.single_ev = dict_object["single_ev"] self.set( id, ThreadFactory(self.model_name, self.control_frequency, self.horizon_in_steps, self.dT_in_seconds, self.repetition, self.solver, id, self.optimization_type, self.single_ev)) logger.info("Thread: " + str(self.get(id))) self.redisDB.set("run:" + id, "starting") msg = self.get(id).startOptControllerThread() logger.debug("Answer from Thread factory" + str(msg)) if msg == 0: self.set_isRunning(id, True) logger.debug("Flag isRunning set to True") self.statusThread[id] = threading.Thread(target=self.run_status, args=(id, )) logger.debug("Status of the Thread started") self.statusThread[id].start() meta_data = { "id": id, "model": self.model_name, "control_frequency": self.control_frequency, "horizon_in_steps": self.horizon_in_steps, "dT_in_seconds": self.dT_in_seconds, "repetition": self.repetition, "solver": self.solver, "optimization_type": self.optimization_type, "single_ev": self.single_ev, "ztarttime": time.time() } self.redisDB.set("run:" + id, "running") IDStatusManager.persist_id(id, True, meta_data, self.redisDB) logger.info("running status " + str(self.running)) logger.debug("Command controller start finished") return 0 else: self.set_isRunning(id, False) logger.debug("Flag isRunning set to False") IDStatusManager.persist_id(id, False, None, self.redisDB) self.factory[id].stopOptControllerThread() self.redisDB.set("run:" + id, "stopped") logger.error("Command controller start could not be finished") # logger.debug("System stopped succesfully") return 1 def stop(self, id): logger.debug("Stop signal received") logger.debug("This is the factory object: " + str(self.get(id))) if self.factory[id]: IDStatusManager.persist_id(id, False, None, self.redisDB) self.factory[id].stopOptControllerThread() del self.factory[id] del self.statusThread[id] #self.stop_pyro_servers() #self.stop_name_servers() self.set_isRunning(id, False) message = "System stopped succesfully" self.redisDB.set("run:" + id, "stopped") logger.debug(message) gc.collect() else: message = "No threads found" logger.debug(message) def run_status(self, id): while True: status = self.get(id).is_running() flag = self.redisDB.get("run:" + id) if not status or (flag is not None and flag == "stop"): self.redisDB.set("run:" + id, "stopping") self.stop(id) break time.sleep(1) def restart_ids(self): old_ids, stopped_ids = IDStatusManager.instances_to_restart( self.redisDB) for s in old_ids: val = json.loads(s) try: self.start(val["id"], None, val) except (InvalidModelException, MissingKeysException, InvalidMQTTHostException) as e: # TODO: should we catch these exceptions here? logger.error("Error " + str(e)) self.redisDB.set("run:" + val["id"], "stopped") return str(e) for s in stopped_ids: val = json.loads(s) id = val["id"] self.redisDB.set("run:" + id, "stopped") self.redisDB.set(Constants.id_meta + ":" + id, json.dumps(val)) def get_status(self): status = {} keys = self.redisDB.get_keys_for_pattern("run:*") if keys is not None: for key in keys: value = self.redisDB.get(key) id = key[4:] status[id] = {} if value is None or (value is not None and value == "stopped"): status[id]["status"] = "stopped" elif value == "running": status[id]["status"] = "running" elif value == "stop" or value == "stopping": status[id]["status"] = "stopping" elif value == "starting": status[id]["status"] = "starting" keys = self.redisDB.get_keys_for_pattern(Constants.id_meta + ":*") if keys is not None: for key in keys: value = self.redisDB.get(key) id = key[8:] if id not in status.keys(): status[id] = {} status[id]["status"] = "stopped" status[id]["config"] = {} if value is not None: status[id]["config"].update(json.loads(value)) # logger.debug("status id config "+str(status)) if "ztarttime" in status[id]["config"].keys(): status[id]["start_time"] = status[id]["config"][ "ztarttime"] status[id]["config"].pop("ztarttime") if "model" in status[id]["config"].keys(): status[id]["config"]["model_name"] = status[id][ "config"]["model"] status[id]["config"].pop("model") return status