def writedata(self, deviceId, *datanodevalueobj): """Function to write data to server.""" dv = [] for dvo in datanodevalueobj: if (validate(dvo)): dv.append(dvo.__dict__) if len(dv) > 0: try: j = json.dumps(dv) data = j.encode("utf8") pathUrl = self.baseUrl + self.writedataresourceformat.format( deviceId) #create request with header req = url.Request(pathUrl, data=data, headers={'content-type': 'application/json'}) response = url.urlopen(req) response = response.read() except err.HTTPError as e: raise self.get_errorinfo(e.code, e.read()) from None else: return self.get_response(response, "iotticket.models.writeresults") else: return "All datanodes are not valid"
def registerdevice(self, deviceobj): """Register new device.""" if (validate(deviceobj)): try: # parse to json and encode device_dict = deviceobj.__dict__ enterprise = device_dict["enterpriseId"] print(device_dict) if enterprise == "": device_dict.pop("enterpriseId") j = json.dumps(device_dict, sort_keys=True, indent=4) data = j.encode("utf8") pathUrl = self.baseUrl + self.deviceresource req = urllib.request.Request(pathUrl,data=data, headers=self.headers) with urllib.request.urlopen(req, context=self.context) as response: response = response.read() except err.HTTPError as e: raise self.get_errorinfo(e.code, e.read()) from None else: return self.get_response(response, "iotticket.models.device") else: return "Device is not valid."
def registerdevice(self, deviceobj): """Register new device.""" if (validate(deviceobj)): try: # parse to json and encode device_dict = deviceobj.__dict__ enterprise = device_dict["enterpriseId"] print(device_dict) if enterprise == "": device_dict.pop("enterpriseId") j = json.dumps(device_dict, sort_keys=True, indent=4) data = j.encode("utf8") pathUrl = self.baseUrl + self.deviceresource req = urllib.request.Request(pathUrl, data=data, headers=self.headers) with urllib.request.urlopen(req, context=self.context) as response: response = response.read() except err.HTTPError as e: raise self.get_errorinfo(e.code, e.read()) from None else: return self.get_response(response, "iotticket.models.device") else: return "Device is not valid."
def writedata(self, deviceId, *datanodevalueobj): """Function to write data to server.""" dv = [] for dvo in datanodevalueobj: if (validate(dvo)): dv.append(dvo.__dict__) for d in dv: del d["crit"] del d["values"] if len(dv) > 0: try: j = json.dumps(dv) data = j.encode("utf8") pathUrl = self.baseUrl + self.writedataresourceformat.format(deviceId) req = urllib.request.Request(pathUrl, data=data, headers=self.headers) with urllib.request.urlopen(req, context=self.context) as response: response = response.read() except err.HTTPError as e: raise self.get_errorinfo(e.code, e.read()) from None else: return self.get_response(response, "iotticket.models.writeresults") else: return "All datanodes are not valid"
def set_attributes(self, *new_deviceattribute): for a in new_deviceattribute: if (validate(a)): j_data = {"key": a.key, "value": a.value} self.j_list.append(j_data) self.attributes = self.j_list else: print("Attribute : ", a, " is not valid.")
def set_attributes(self, *new_deviceattribute): for a in new_deviceattribute: if(validate(a)): j_data = {"key":a.key,"value":a.value} self.j_list.append(j_data) self.attributes = self.j_list else: print("Attribute : ", a , " is not valid.")
def registerdevice(self, deviceobj): """Register new device.""" if (validate(deviceobj)): try: #parse to json and encode j = json.dumps(deviceobj.__dict__, sort_keys=True, indent=4) data = j.encode("utf8") pathUrl = self.baseUrl + self.deviceresource #create request with header req = url.Request(pathUrl, data=data, headers={'content-type': 'application/json'}) response = url.urlopen(req) response = response.read() except err.HTTPError as e: raise self.get_errorinfo(e.code, e.read()) from None else: return self.get_response(response, "iotticket.models.device") else: return "Device is not valid."
def writedata(self, deviceId, *datanodevalueobj): """Function to write data to server.""" dv = [] for dvo in datanodevalueobj: if (validate(dvo)): dv.append(dvo.__dict__) if len(dv) > 0: try: j = json.dumps(dv) data = j.encode("utf8") pathUrl = self.baseUrl + self.writedataresourceformat.format(deviceId) req = urllib.request.Request(pathUrl, data=data, headers=self.headers) with urllib.request.urlopen(req, context=self.context) as response: response = response.read() except err.HTTPError as e: raise self.get_errorinfo(e.code, e.read()) from None else: return self.get_response(response, "iotticket.models.writeresults") else: return "All datanodes are not valid"