def _http_json_set_value(uids, locations, names, values): """ Set single or multiple endpoint values on data source @param uids: list of endpoint ids @param locations: list of endpoint locations @param names: list of endpoint names @param values: list of endpoint values @return list of endpoint values in JSON format """ # Create list of JSON endpoint data req = [] if values is not None: if len(values) > 0: for i, val in enumerate(values): if len(locations) == len(names) > i: endp = LagartoEndpoint(location = locations[i], name=names[i], value=val) elif len(uids) > i: endp = LagartoEndpoint(endp_id=uids[i], value=val) req.append(endp.dumps()) else: return None status = LagartoHttpServer.data_server.set_status(req) if status is not None: if len(status) > 0: # Create complete Lagarto status message http_server = LagartoHttpServer.address + ":" + str(LagartoHttpServer.port) response = LagartoMessage(proc_name=LagartoHttpServer.proc_name, http_server=http_server, status=status) return json.dumps(response.dumps()) return None
def publish_status(self, status_data=None, endp=None): """ Publish network status (collection of endpoint data) @param status_data network status to be transmitted @param endp Endpoint data """ self.publish_lock.acquire() try: http_server = self.address + ":" + str(self.httpport) if endp is not None: status_data = [endp.dumps()] # Message in JSON format lagarto_msg = LagartoMessage(proc_name=self.procname, http_server=http_server, status=status_data) msg = json.dumps(lagarto_msg.dumps()) topic = self.procname + "/json/status" self.mqtt_client.publish(topic, payload=msg, qos=0, retain=False) # Message in simple format if status_data is not None: for endp_data in status_data: topic = self.procname + "/simple/status/" + endp_data[ "location"] + "/" + endp_data["name"] msg = endp_data["value"] self.mqtt_client.publish(topic, payload=msg, qos=0, retain=False) finally: self.publish_lock.release()
def publish_status(self, status_data=None, endp=None): """ Publish network status (collection of endpoint data) @param status_data network status to be transmitted @param endp Endpoint data """ self.publish_lock.acquire() try: http_server = self.address + ":" + str(self.httpport) if endp is not None: status_data = [endp.dumps()] # Message in JSON format lagarto_msg = LagartoMessage(proc_name=self.procname, http_server=http_server, status=status_data) msg = json.dumps(lagarto_msg.dumps()) topic = self.procname + "/json/status" self.mqtt_client.publish(topic, payload=msg, qos=0, retain=False) # Message in simple format if status_data is not None: for endp_data in status_data: topic = self.procname + "/simple/status/" + endp_data["location"] + "/" + endp_data["name"] msg = endp_data["value"] self.mqtt_client.publish(topic, payload=msg, qos=0, retain=False) finally: self.publish_lock.release()
def _http_json_get_value(uids=None, locations=None, names=None): """ Get single or multiple endpoint values from data source @param uids: list of endpoint ids @param locations: list of endpoint locations @param names: list of endpoint names @return list of endpoint values in JSON format """ # Create list of JSON endpoint data req = [] if uids is not None and locations is not None and names is not None: if len(uids) > 0: for uid in uids: endp = LagartoEndpoint(endp_id=uid) req.append(endp.dumps()) elif len(locations) == len(names) > 0: for i, loc in enumerate(locations): endp = LagartoEndpoint(location=loc, name=names[i]) req.append(endp.dumps()) else: # Return whole network status req = None status = LagartoHttpServer.data_server.get_status(req) if status is not None: if len(status) > 0: # Create complete Lagarto status message http_server = LagartoHttpServer.address + ":" + str(LagartoHttpServer.port) response = LagartoMessage(proc_name=LagartoHttpServer.proc_name, http_server=http_server, status=status) return json.dumps(response.dumps()) return None
def _http_json_get_value(self, uid=None, location=None, name=None): """ Get single or multiple endpoint values from data source @param uid: endpoint uid @param location: endpoint locations @param name: endpoint name @return list of endpoint values in JSON format """ # Create list of JSON endpoint data req = [] if uid is not None: endp = LagartoEndpoint(endp_id=uid) req.append(endp.dumps()) elif location is not None and name is not None: endp = LagartoEndpoint(location=location, name=name) req.append(endp.dumps()) else: # Return whole network status req = None status = self.data_server.get_status(req) if status is not None: if len(status) > 0: # Create complete Lagarto status message http_server = self.address + ":" + str(self.port) response = LagartoMessage(proc_name=self.proc_name, http_server=http_server, status=status) return json.dumps(response.dumps()) return None
def _http_json_set_value(self, uid, location, name, value): """ Set single or multiple endpoint values from data source @param uids: endpoint id @param locations: endpoint location @param names: endpoint name @param values: endpoint value @return list of endpoint values in JSON format """ # Create list of JSON endpoint data req = [] if value is not None: if uid is not None: endp = LagartoEndpoint(endp_id=uid, value=value) elif location is not None and name is not None: endp = LagartoEndpoint(location = location, name=name, value=value) req.append(endp.dumps()) else: return None status = self.data_server.set_status(req) if status is not None: if len(status) > 0: # Create complete Lagarto status message http_server = self.address + ":" + str(self.port) response = LagartoMessage(proc_name=self.proc_name, http_server=http_server, status=status) return json.dumps(response.dumps()) return None
def publish_status(self, status_data=None): """ Broadcast network status (collection of endpoint data) @param status_data network status to be transmitted """ self.publish_lock.acquire() try: http_server = self.config.address + ":" + str(self.config.httpport) lagarto_msg = LagartoMessage(proc_name=self.config.procname, http_server=http_server, status=status_data) msg = json.dumps(lagarto_msg.dumps()) self.pub_socket.send(msg) finally: self.publish_lock.release()
def publish_status(self, status_data=None): """ Broadcast network status (collection of endpoint data) @param status_data list of network status to be transmitted """ self.publish_lock.acquire() try: http_server = self.config.address + ":" + str(self.config.httpport) lagarto_msg = LagartoMessage(proc_name=self.config.procname, http_server=http_server, status=status_data) msg = json.dumps(lagarto_msg.dumps()) # send to zmq if self.pub_socket: self.pub_socket.send(msg) # mqtt if self.mqtt_client and status_data: self.mqtt_client.handle_swap_message(status_data) elif self.mqtt_client and not status_data: # probably HBEAT message, skipping it at mqtt level pass finally: self.publish_lock.release()
def request_status(self, procname, req): """ Query/command network/endpoint status from server @param procname: name of the process to be queried @param req: queried/controlled endpoints @return status """ if len(req) > 0: control = False if "value" in req[0]: control = True cmd_list = [] for endp in req: if endp["location"] is not None and endp["name"] is not None: cmd = "location=" + endp[ "location"] + "&" + "name=" + endp["name"] elif endp["id"] is not None: cmd = "id=" + endp["id"] else: raise LagartoException( "Insufficient data to identify endpoint") if control: cmd += "&value=" + endp["value"] cmd_list.append(cmd) if procname in self.http_servers: try: conn = httplib.HTTPConnection(self.http_servers[procname], timeout=5) conn.request("GET", "/values/?" + "&".join(cmd_list)) response = conn.getresponse() if response.reason == "OK": status_response = json.loads(response.read()) status_msg = LagartoMessage(status_response) return status_msg.status except: raise LagartoException( "Unable to get response from HTTP server") return None