def _load_networking(self, resource_id): if self.debug & 0x01: print("_load_networking {!s} called".format(resource_id)) if not hasattr(self, '_name2id') or not isinstance(self._name2id, dict): self._name2id = dict() xurl = "/rest/networking/{!s}".format(resource_id) if self.debug & 0x02: print("_load_networking : xurl = " + xurl) net_res_tree = self._getXMLetree(xurl) if net_res_tree is None: if (len(self.error_str)): raise IsyE.IsyResponseError(self.error_str) else: raise IsyE.IsyResponseError(xurl) net_dict = dict() name2rid = dict() for netr in net_res_tree.iter('NetRule'): netrule = et2d(netr) if 'id' in netrule: net_dict[netrule['id']] = netrule if 'name' in netrule: n = netrule['name'] name2rid[n] = netrule['id'] # name2id to replace name2var as a global lookup table if n in self._name2id: print("Dup name2id : \"" + n + "\" : " + netrule['id']) print("\tname2id ", self._name2id[n]) else: self._name2id[n] = (resource_id, netrule['id']) return (net_dict, name2rid)
def sendfile(self, src=None, filename="", data=None): """ upload file args: data content for fine to upload src file to load upload content ( if data is None ) filename name for remote file """ if self.debug & 0x01: print("sendfile : ", self.__class__.__name__) if filename[0] != '/': filename = "/USER/WEB/" + filename elif not str(filename).upper().startswith("/USER/WEB/"): raise IsyE.IsyValueError( "sendfile: invalid dst filename : {!s}".format(filename)) # not len(data) if not data: if not src: src = filename if self.debug & 0x20: print("using file {!s} as data src".format(src)) with open(src, 'r') as content_file: data = content_file.read() else: if self.debug & 0x20: print("using provided data as data src") return self._sendfile(filename=filename, data=data, load="n")
def _get_prop(self, prop): # print("IN get_prop ", prop) if prop == "formatted": prop = "ST" value = "formatted" else: value = "value" if prop in self._propalias: prop = self._propalias[prop] if not prop in self._getlist: # if prop in ['parent', 'parent-type']: # return None raise IsyE.IsyPropertyError( "no property Attribute {!s}".format(prop)) # check if we have a property if prop in ['isLoad', 'location', 'description', 'spoken']: if self._nodeprops is None: self._nodenotes = self.isy.node_get_notes( self._mydict["address"]) if self._nodenotes is None: return None if prop in self._nodenotes: return self._nodenotes[prop] else: # return None return "" if prop in ['ST', 'OL', 'RR']: # Scene's do not have property values if "property" in self._mydict and prop in self._mydict["property"]: # print(self._mydict["property"]) # print("prop value", prop, value) return self._mydict["property"][prop][value] else: return None # if self._mydict["property"]["time"] == 0: # self.update() # elif self.isy.cachetime: # if time.gmtime() < (self.cachetime + self._mydict["property"]["time"]): # self.update() else: # if prop in self._mydict: # if prop in self._boollist: # return(val2bool(self._mydict[prop])) # else: # return self._mydict[prop] # else: # return None return super(self.__class__, self)._get_prop(prop)
def _set_prop(self, prop, new_value): """ generic property set """ # print("IN set_prop ", prop, new_value) if self.debug & 0x04: print("_set_prop ", prop, " : ", new_value) if prop in self._propalias: prop = self._propalias[prop] if not prop in self._setlist: if prop == "ST": self.on(new_value) return else: raise IsyE.IsyPropertyError("_set_prop : " \ "Invalid property Attribute " + prop) if prop == 'enable': self._mydict[prop] = bool(new_value) self.isy.node_enable(self._mydict["address"], bool(new_value)) elif prop in ['OL', 'RR']: if not str(new_value).isdigit: raise IsyE.IsyTypeError( "Set Property : Bad Value : node=%s prop=%s val=%s" % self._mydict["address"], prop, str(new_value)) self.isy._node_send(self._mydict["address"], "set", prop, str(new_value)) # self._mydict["property"]["time"] = 0 if prop in self._mydict["property"]: # if isinstance(new_value, (int, float)) : # already checked with isdigit self._mydict["property"][prop]["value"] = new_value # we need to tie this to some action elif prop in self._mydict: # self._mydict[prop] = new_value pass else: #print("_set_prop AttributeError") raise AttributeError("no Attribute " + prop)
def net_resource_get_src(self, rrid): rid = self._net_resource_get_id(rrid) if rid is None: raise IsyE.IsyValueError( "net_resource_get_src: bad network resources ID : " + rrid) r = self.soapcomm("GetSysConf", name="/CONF/NET/" + rrid + ".RES") return r
def net_resource_run(self, rrid): """ Calls and executes net resource args: rrid : network resource ID calls : /rest/networking/resources/<rrid> """ rid = self._net_resource_get_id(rrid) if rid is None: raise IsyE.IsyValueError( "net_resource_run : bad network resources ID : " + rrid) xurl = "/rest/networking/resources/{!s}".format(rid) if self.debug & 0x02: print("wol : xurl = " + xurl) resp = self._getXMLetree(xurl) # self._printXML(resp) if resp is None or resp.attrib["succeeded"] != 'true': raise IsyE.IsyResponseError("ISY network resources error : rid=" + str(rid))
def net_wol(self, wid): """ Send Wake On LAN to registared wol ID args: wid : WOL resource ID calls : /rest/networking/wol/<wol_id> """ wol_id = self._net_wol_get_id(wid) # wol_id = str(wid).upper() if wol_id is None: raise IsyE.IsyValueError("bad wol ID : " + wid) xurl = "/rest/networking/wol/" + wol_id if self.debug & 0x02: print("wol : xurl = " + xurl) resp = self._getXMLetree(xurl) # self._printXML(resp) if resp.attrib["succeeded"] != 'true': raise IsyE.IsyResponseError("ISY command error : cmd=wol wol_id=" \ + str(wol_id))
def _on(self, val, cmd): if not str(val).isdigit: raise IsyE.IsyTypeError( "On Command : Bad Value : node=%s val=%s" % self._mydict["address"], str(val)) if "property" in self._mydict: if "ST" in self._mydict["property"]: self._mydict["property"]["ST"]["value"] = str(val) if self._dimable: self._mydict["property"]["ST"][ "formatted"] = "{:.0%}".format(val / 255) else: self._mydict["property"]["ST"]["formatted"] = "On" self.isy._node_send(self._mydict["address"], "cmd", cmd, val)
def __init__(self, isy, objdict): """ INIT """ self.error_str = "" if isinstance(objdict, dict): self._mydict = objdict else: raise IsyE.IsyValueError("{!s}: called without objdict".format( self.__class__.__name__)) if isinstance(isy, IsyUtil): self.isy = isy self.debug = isy.debug else: # print("error : class " + self.__class__.__name__ + " called without Isy") raise TypeError("IsySubClass: isy arg is not a ISY family class") if self.debug & 0x04: print("IsySubClass: ", end='') self._printdict(self._mydict)
def _sendfile(self, filename="", data="", load="n"): if (filename.startswith('/')): xurl = self.baseurl + "/file/upload" + filename + "?load=" + load else: xurl = self.baseurl + "/file/upload/" + filename + "?load=" + load if self.debug & 0x02: print("{0} xurl : {1}".format(__name__, xurl)) # req = URL.Request(xurl, data, {'Content-Type': 'application/xml; charset="utf-8"'}) req_headers = {'Content-Type': 'application/xml; charset="utf-8"'} try: res = self._req_session.post(xurl, data=data, headers=req_headers) responce = res.text res.close() # print("responce:", res.status_code, len(responce)) except requests.exceptions.RequestException as rex: mess = "{!s} : {!s} : {!s}".format("/file/upload", filename, rex.response.status_code) raise IsyE.IsySoapError(mess, httperr=rex) else: return responce
def format_node_addr(naddr): if not isinstance(naddr, str): raise IsyE.IsyValueError("{0} arg not string".format(__name__)) addr_el = naddr.upper().split() a = "{0:0>2}' '{1:0>2}' '{2:0>2}' ".format(*addr_el) return a
def __delitem__(self, prop): raise IsyE.IsyPropertyError("__delitem__ : can't delete propery : " + str(prop))
def soapcomm(self, cmd, **kwargs): """ takes a command name and a list of keyword arguments. each keyword is converted into a xml element """ if not isinstance(cmd, str) or not cmd: raise IsyE.IsyValueError("SOAP Method name missing") # if self.debug & 0x02: # print("sendcomm : ", cmd) soap_cmd = self._gensoap(cmd, **kwargs) xurl = self.baseurl + "/services" if self.debug & 0x02: # print("xurl = ", xurl) print("soap_cmd = ", soap_cmd) # req_headers = {'content-type': 'application/soap+xml'} # req_headers = {'content-type': 'text/xml'} req_headers = {'Content-Type': 'application/xml; charset="utf-8"'} # req = URL.Request(xurl, soap_cmd, {'Content-Type': 'application/xml; charset="utf-8"'}) data = "" try: res = self._req_session.post(xurl, data=soap_cmd, headers=req_headers) data = res.text # res.content if self.debug & 0x200: print("res.status_code ", res.status_code, len(data)) print("data ", data) res.close() # except URL.HTTPError as e: except requests.exceptions.RequestException as rex: status_code = rex.response.status_code self.error_str = str("Reponse Code : {0} : {1} {2}").format( status_code, xurl, cmd) if ((cmd == "DiscoverNodes" and rex.response.status_code == 803) or (cmd == "CancelNodesDiscovery" and status_code == 501) # or (cmd == "RemoveNode" and status_code == 501) ): if self.debug & 0x02: print("spacial case : {0} : {1}".format(cmd, status_code)) print("status_code = ", status_code) print("response.reason = ", rex.response.reason) print("response.headers = ", rex.response.headers) # print("e.filename = ", e.filename) print("\n") return rex.response.text if self.debug & 0x202: print("status_code = ", status_code) # print("e.read = ", e.read()) print("RequestException = ", rex) print("data = ", data) mess = "{!s} : {!s} : {!s}".format(cmd, kwargs, status_code) # This a messy and should change raise IsyE.IsySoapError(mess, httperr=rex) else: if self.error_str: # len self.error_str = "" if self.debug & 0x200: print(data) return data