class IRDBInterface(object): def __init__(self): self.oReq = None self.oAccount = None # Call to add an api key to a url, as well as other params if they are provided def prepare_url(self, path, params = None): if self.oAccount: url = BASE_URL + path + '?apikey=' + self.oAccount.ApiKey if params: for key, value in params.items(): url += '&{}={}'.format(key, value) return url return u'' # Login, and save the account information provided (the api key particularly) # This must be called first. def login(self): url = BASE_URL + LOGIN_PATH SetVar('ITach_Mail',oDBSelector.oTxtMail.text) SetVar('ITach_Password',oDBSelector.oTxtPassword.text) Var_Save('ITach_Mail','') Var_Save('ITach_Password','') login_account = Account() login_account.Email = oDBSelector.oTxtMail.text login_account.Password = oDBSelector.oTxtPassword.text self.oReq = UrlRequest(url, req_body=login_account.ToData(), req_headers=HEADERS,on_failure=self.OnError,on_error=self.OnError) self.NewWait(0.05) aResponse = self.oReq.result if aResponse: self.oAccount = Account(aResponse['Account']) def OnError(self,request,error): self.bStopWait = True def NewWait(self,delay): self.bStopWait=False while self.oReq.resp_status is None: self.oReq._dispatch_result(delay) sleep(delay) if self.bStopWait: self.bStopWait=False break # Logout. The apikey is used to identify the account, which is added by prepare_url def logout(self): url = self.prepare_url(LOGOUT_PATH) UrlRequest(url, req_headers=HEADERS) self.oAccount = None # Get a list of all available brands def get_brands(self): url = self.prepare_url(BRANDS_PATH) self.oReq = UrlRequest(url, req_headers=HEADERS,on_failure=self.OnError,on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [Brand(brand_dict) for brand_dict in response_dict] return [] # Get a list of all device types available for the given brand def get_types(self, brandname): url = self.prepare_url(TYPES_PATH.format(brandname)) self.oReq = UrlRequest(url, req_headers=HEADERS,on_failure=self.OnError,on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [Type(type_dict) for type_dict in response_dict] return [] # Get a list of all models for the given brand and type def get_models(self, brandname, typename): brandname=urllib.quote(brandname,safe="") typename=urllib.quote(typename,safe="") url = self.prepare_url(MODELS_PATH.format(brandname, typename)) self.oReq = UrlRequest(url, req_headers=HEADERS,on_failure=self.OnError,on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [Model(model_dict) for model_dict in response_dict] return [] # Get a full set of codes for devices with the provided set id. # This will use one of a limited number of code requests available for an account in a day. def get_codeset(self, setid): url = self.prepare_url(CODESET_PATH.format(setid), {'output':'direct'}) self.oReq = UrlRequest(url, req_headers=HEADERS,on_failure=self.OnError,on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [IRCode(code_dict) for code_dict in response_dict] return [] # Get a list of functions available for devices with the provided set id. def get_functions(self, setid): url = self.prepare_url(FUNCTIONS_PATH.format(setid)) self.oReq = UrlRequest(url, req_headers=HEADERS,on_failure=self.OnError,on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [Function(function_dict) for function_dict in response_dict] return [] # Get a code, for the provided function and device (set id) # This will use one of a limited number of code requests available for an account in a day. def get_code(self, setid, function): url = self.prepare_url(CODE_PATH.format(setid, function), {'output':'direct'}) self.oReq = UrlRequest(url, req_headers=HEADERS,on_failure=self.OnError,on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return IRCode(response_dict) return ""
class IRDBInterface: def __init__(self): self.oReq: Union[UrlRequest, None] = None self.oAccount: Union[Account, None] = None self.bStopWait: bool = False # Call to add an api key to a url, as well as other params if they are provided def PrepareUrl(self, uPath: str, dParams: Dict = None) -> str: uUrl: str if self.oAccount: uUrl = oDBSelector.uHost + uPath + '?apikey=' + self.oAccount.uApiKey if dParams: for key, value in dParams.items(): uUrl += '&{}={}'.format(key, value) return uUrl return u'' # Login, and save the account information provided (the api key particularly) # This must be called first. def Login(self) -> None: uUrl: str = oDBSelector.uHost + LOGIN_PATH SetVar(uVarName='ITach_Mail', oVarValue=oDBSelector.uUser) SetVar(uVarName='ITach_Password', oVarValue=oDBSelector.uPassword) oLoginAccount: Account = Account() oLoginAccount.uEmail = oDBSelector.uUser oLoginAccount.uPassword = oDBSelector.uPassword self.oReq = UrlRequest(uUrl, req_body=oLoginAccount.ToData(), req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) dResponse: Dict = self.oReq.result if dResponse: self.oAccount = Account(dResponse['Account']) # noinspection PyUnusedLocal,PyUnusedLocal def OnError(self, request, error): self.bStopWait = True # noinspection PyProtectedMember def NewWait(self, fDelay: float) -> None: self.bStopWait = False while self.oReq.resp_status is None: self.oReq._dispatch_result(fDelay) sleep(fDelay) if self.bStopWait: self.bStopWait = False break # Logout. The apikey is used to identify the account, which is added by PrepareUrl def Logout(self) -> None: uUrl: str = self.PrepareUrl(LOGOUT_PATH) UrlRequest(uUrl, req_headers=HEADERS) self.oAccount = None # Get a list of all available brands def GetBrands(self) -> List[cBrand]: uUrl: str = self.PrepareUrl(BRANDS_PATH) return self.GetClassListFromUrl(uUrl, cBrand) # Get a list of all device types available for the given brand def GetTypes(self, uBrandName: str) -> List[cType]: uUrl: str = self.PrepareUrl(TYPES_PATH.format(uBrandName)) return self.GetClassListFromUrl(uUrl, cType) # Get a list of all models for the given brand and type def GetModels(self, uBrandName: str, uTypeName: str) -> List[cModel]: uBrandName = urllib.parse.quote(uBrandName, safe="") uTypeName = urllib.parse.quote(uTypeName, safe="") uUrl: str = self.PrepareUrl(MODELS_PATH.format(uBrandName, uTypeName)) return self.GetClassListFromUrl(uUrl, cModel) # Get a full set of codes for devices with the provided set id. # This will use one of a limited number of code requests available for an account in a day. def GetCodeSet(self, uSetId: str) -> List[cIRCode]: uUrl = self.PrepareUrl(CODESET_PATH.format(uSetId), {'output': 'direct'}) return self.GetClassListFromUrl(uUrl, cIRCode) # Get a list of functions available for devices with the provided set id. def GetFunctions(self, uSetId: str) -> List[cFunction]: uUrl: str = self.PrepareUrl(FUNCTIONS_PATH.format(uSetId)) return self.GetClassListFromUrl(uUrl, cFunction) def GetClassListFromUrl(self, uUrl: str, oClass) -> List: self.oReq = UrlRequest(uUrl, req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) dResponse: Dict = self.oReq.result if dResponse: return [oClass(dItem) for dItem in dResponse] return [] '''
class cThread_CheckIP(threading.Thread): oWaitLock = threading.Lock() def __init__(self, uIP, bOnlyOnce, fTimeOut, oCaller): threading.Thread.__init__(self) self.uIP = uIP self.bOnlyOnce = bOnlyOnce self.oCaller = oCaller self.fTimeOut = fTimeOut self.bStopWait = False self.oReq = None def run(self): bReturnNow = False if self.bOnlyOnce: cThread_CheckIP.oWaitLock.acquire() if len(self.oCaller.aResults) > 0: bReturnNow = True cThread_CheckIP.oWaitLock.release() if bReturnNow: return self.SendCommand() def SendCommand(self): self.bStopWait = False uUrlFull = "http://" + self.uIP + "/web/about" try: self.oReq = UrlRequest(uUrlFull, method="GET", timeout=self.fTimeOut, on_error=self.OnError, on_success=self.OnReceive) self.NewWait(0.05) if self.oReq.resp_status is not None: uResult = self.oReq.result if "<e2abouts>" in uResult: if PY2: oXmlRoot = fromstring( uResult.encode('ascii', 'xmlcharrefreplace')) else: oXmlRoot = fromstring(uResult) oXmlAbout = oXmlRoot.find("e2about") uModel = GetXMLTextValue(oXmlAbout, "e2model", False, "Enigma") uFoundHostName = "" try: uFoundHostName = socket.gethostbyaddr(self.uIP)[0] except Exception as e: # Logger.error("Cant get Hostname:"+oRet.sFoundIP+" "+str(e)) pass cThread_CheckIP.oWaitLock.acquire() self.oCaller.aResults.append({ "ip": self.uIP, "port": 80, "model": uModel, "ipversion": "IPv4", "hostname": uFoundHostName }) try: uIP = "" aIPs = socket.getaddrinfo(uFoundHostName, None) for tIP in aIPs: uIP = "[" + tIP[-1][0] + "]" if ":" in uIP: break if ":" in uIP: self.oCaller.aResults.append({ "ip": uIP, "port": 80, "model": uModel, "ipversion": "IPv6", "hostname": uFoundHostName }) except Exception as e: pass cThread_CheckIP.oWaitLock.release() except Exception as e: self.oCaller.ShowError("Error on send:", e) return def NewWait(self, delay): while self.oReq.resp_status is None: self.oReq._dispatch_result(delay) sleep(delay) if self.bStopWait: self.bStopWait = False break def OnError(self, request, error): self.bStopWait = True def OnFailure(self, request, result): self.bStopWait = True def OnReceive(self, oRequest, oResult): self.bStopWait = True
class IRDBInterface(object): def __init__(self): self.oReq = None self.oAccount = None # Call to add an api key to a url, as well as other params if they are provided def prepare_url(self, path, params=None): if self.oAccount: url = oDBSelector.uHost + path + '?apikey=' + self.oAccount.ApiKey if params: for key, value in params.items(): url += '&{}={}'.format(key, value) return url return u'' # Login, and save the account information provided (the api key particularly) # This must be called first. def login(self): url = oDBSelector.uHost + LOGIN_PATH SetVar(uVarName='ITach_Mail', oVarValue=oDBSelector.uUser) SetVar(uVarName='ITach_Password', oVarValue=oDBSelector.uPassword) login_account = Account() login_account.Email = oDBSelector.uUser login_account.Password = oDBSelector.uPassword self.oReq = UrlRequest(url, req_body=login_account.ToData(), req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) aResponse = self.oReq.result if aResponse: self.oAccount = Account(aResponse['Account']) def OnError(self, request, error): self.bStopWait = True def NewWait(self, delay): self.bStopWait = False while self.oReq.resp_status is None: self.oReq._dispatch_result(delay) sleep(delay) if self.bStopWait: self.bStopWait = False break # Logout. The apikey is used to identify the account, which is added by prepare_url def logout(self): url = self.prepare_url(LOGOUT_PATH) UrlRequest(url, req_headers=HEADERS) self.oAccount = None # Get a list of all available brands def get_brands(self): url = self.prepare_url(BRANDS_PATH) self.oReq = UrlRequest(url, req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [Brand(brand_dict) for brand_dict in response_dict] return [] # Get a list of all device types available for the given brand def get_types(self, brandname): url = self.prepare_url(TYPES_PATH.format(brandname)) self.oReq = UrlRequest(url, req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [Type(type_dict) for type_dict in response_dict] return [] # Get a list of all models for the given brand and type def get_models(self, brandname, typename): brandname = urllib.quote(brandname, safe="") typename = urllib.quote(typename, safe="") url = self.prepare_url(MODELS_PATH.format(brandname, typename)) self.oReq = UrlRequest(url, req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [Model(model_dict) for model_dict in response_dict] return [] # Get a full set of codes for devices with the provided set id. # This will use one of a limited number of code requests available for an account in a day. def get_codeset(self, setid): url = self.prepare_url(CODESET_PATH.format(setid), {'output': 'direct'}) self.oReq = UrlRequest(url, req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [IRCode(code_dict) for code_dict in response_dict] return [] # Get a list of functions available for devices with the provided set id. def get_functions(self, setid): url = self.prepare_url(FUNCTIONS_PATH.format(setid)) self.oReq = UrlRequest(url, req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return [Function(function_dict) for function_dict in response_dict] return [] # Get a code, for the provided function and device (set id) # This will use one of a limited number of code requests available for an account in a day. def get_code(self, setid, functionname): url = self.prepare_url(CODE_PATH.format(setid, functionname), {'output': 'direct'}) self.oReq = UrlRequest(url, req_headers=HEADERS, on_failure=self.OnError, on_error=self.OnError) self.NewWait(0.05) response_dict = self.oReq.result if response_dict: return IRCode(response_dict) return ""
class cThread_CheckIP(threading.Thread): def __init__(self, uIP:str, bOnlyOnce:bool,fTimeOut:float,oCaller:cScript): threading.Thread.__init__(self) self.uIP:str = uIP self.bOnlyOnce:bool = bOnlyOnce self.oCaller:cScript = oCaller self.fTimeOut:float = fTimeOut self.bStopWait:bool = False self.oReq:Union[UrlRequest,None] = None def run(self) -> None: if self.bOnlyOnce: if len(self.oCaller.aResults)>0: return self.SendCommand() def SendCommand(self) -> None: self.bStopWait = False uUrlFull:str = "http://"+self.uIP+"/web/about" try: self.oReq = UrlRequest(uUrlFull,method="GET",timeout=self.fTimeOut,on_error=self.OnResponse,on_success=self.OnResponse) self.NewWait(0.05) if self.oReq.resp_status is not None: uResult:str = self.oReq.result if "<e2abouts>" in uResult: oXmlRoot:Element = LoadXMLString(uXML=uResult) oXmlAbout:Element = oXmlRoot.find("e2about") uModel:str = GetXMLTextValue(oXMLNode=oXmlAbout, uTag="e2model", bMandatory=False, vDefault="Enigma") uFoundHostName:str = "" try: uFoundHostName = socket.gethostbyaddr(self.uIP)[0] except Exception: # Logger.error("Cant get Hostname:"+oRet.uFoundIP+" "+str(e)) pass dResult:TypedQueryDict = TypedQueryDict() dResult.uIP = self.uIP dResult.iPort = 80 dResult.uModel = uModel dResult.uIPVersion = "IPv4" dResult.uHostName = uFoundHostName self.oCaller.aResults.append(dResult) Globals.oNotifications.SendNotification(uNotification="DISCOVER_SCRIPTFOUND",**{"script":self,"scriptname":self.oCaller.uObjectName,"line":[dResult.uIP,dResult.uHostName, str(dResult.iPort), dResult.uModel],"device":dResult}) self.oCaller.ShowInfo(uMsg=u'Discovered Enigma device (V4) %s' % dResult.uIP) try: uIP = "" aIPs = socket.getaddrinfo(uFoundHostName,None) for tIP in aIPs: uIP = "["+tIP[-1][0]+"]" if ":" in uIP: break if ":" in uIP: dResult:TypedQueryDict = TypedQueryDict() dResult.uIP = uIP dResult.iPort = 80 dResult.uModel = uModel dResult.uIPVersion = "IPv6" dResult.uHostName = uFoundHostName self.oCaller.aResults.append(dResult) Globals.oNotifications.SendNotification(uNotification="DISCOVER_SCRIPTFOUND",**{"script":self,"scriptname":self.oCaller.uObjectName,"line":[dResult.uIP,dResult.uHostName, str(dResult.iPort), dResult.uModel],"device":dResult}) self.oCaller.ShowInfo(uMsg=u'Discovered Enigma device (V6) %s' % dResult.uIP) except Exception: pass except Exception as e: self.oCaller.ShowError(uMsg="Error on send:", oException=e) return def NewWait(self,delay) -> None: while self.oReq.resp_status is None: # noinspection PyProtectedMember self.oReq._dispatch_result(delay) sleep(delay) if self.bStopWait: self.bStopWait=False break def OnResponse(self,request,error) -> None: self.bStopWait = True