|
| 1 | +import controller |
| 2 | +import pglobals |
| 3 | +from inc.helper_domoticz import * |
| 4 | +import misc |
| 5 | +#from multiprocessing import Process |
| 6 | +#import base64 |
| 7 | +try: |
| 8 | + import ubinascii as binascii |
| 9 | +except ImportError: |
| 10 | + import binascii |
| 11 | +try: |
| 12 | + import urequests |
| 13 | + fake = False |
| 14 | +except: |
| 15 | + import urllib.request as urequests |
| 16 | + fake = True |
| 17 | + |
| 18 | +class Controller(controller.ControllerProto): |
| 19 | + CONTROLLER_ID = 1 |
| 20 | + CONTROLLER_NAME = "Domoticz HTTP" |
| 21 | + |
| 22 | + def __init__(self,controllerindex): |
| 23 | + controller.ControllerProto.__init__(self,controllerindex) |
| 24 | + self.usesID = True |
| 25 | + self.usesAccount = True |
| 26 | + self.usesPassword = True |
| 27 | + |
| 28 | + def senddata(self,idx,taskobj,changedvalue=-1): |
| 29 | + if self.enabled: |
| 30 | + if int(idx) != 0: |
| 31 | + if int(taskobj.vtype)==pglobals.SENSOR_TYPE_SWITCH: |
| 32 | + url = "/json.htm?type=command¶m=switchlight&idx=" |
| 33 | + url += str(idx) |
| 34 | + url += "&switchcmd=" |
| 35 | + if round(float(taskobj.uservar[0])) == 0: |
| 36 | + url += "Off" |
| 37 | + else: |
| 38 | + url += "On" |
| 39 | + elif int(taskobj.vtype)==pglobals.SENSOR_TYPE_DIMMER: |
| 40 | + url = "/json.htm?type=command¶m=switchlight&idx=" |
| 41 | + url += str(idx) |
| 42 | + url += "&switchcmd=" |
| 43 | + if float(taskobj.uservar[0]) == 0: |
| 44 | + url += "Off" |
| 45 | + else: |
| 46 | + url += "Set%20Level&level=" |
| 47 | + url += str(taskobj.uservar[0]) |
| 48 | + else: |
| 49 | + url = "/json.htm?type=command¶m=udevice&idx=" |
| 50 | + url += str(idx) |
| 51 | + url += "&nvalue=0&svalue=" |
| 52 | + url += formatDomoticzSensorType(taskobj.vtype,taskobj.uservar) |
| 53 | + url += "&rssi=" |
| 54 | + url += mapRSSItoDomoticz(taskobj.rssi) |
| 55 | + if taskobj.battery != -1 and taskobj.battery != 255: # battery input 0..100%, 255 means not supported |
| 56 | + url += "&battery=" |
| 57 | + url += str(taskobj.battery) |
| 58 | + else: |
| 59 | + bval = misc.get_battery_value() |
| 60 | + url += "&battery=" |
| 61 | + url += str(bval) |
| 62 | + urlstr = self.controllerip+":"+self.controllerport+url+self.getaccountstr() |
| 63 | + misc.addLog(pglobals.LOG_LEVEL_DEBUG,urlstr) # sendviahttp |
| 64 | + self.urlget(urlstr) |
| 65 | +# httpproc = Process(target=self.urlget, args=(urlstr,)) # use multiprocess to avoid blocking |
| 66 | +# httpproc.start() |
| 67 | + else: |
| 68 | + misc.addLog(pglobals.LOG_LEVEL_ERROR,"MQTT : IDX cannot be zero!") |
| 69 | + |
| 70 | + def urlget(self,url): |
| 71 | + global fake |
| 72 | + url = "http://"+str(url) |
| 73 | + try: |
| 74 | + if fake: |
| 75 | + content = urequests.urlopen(url,None,1) |
| 76 | + else: |
| 77 | + content = urequests.get(url) |
| 78 | + except Exception as e: |
| 79 | + misc.addLog(pglobals.LOG_LEVEL_ERROR,"Controller: "+self.controllerip+" connection failed "+str(e)) |
| 80 | + |
| 81 | + def getaccountstr(self): |
| 82 | + retstr = "" |
| 83 | + if self.controlleruser!="" or self.controllerpassword!="": |
| 84 | + acc = binascii.b2a_base64(bytes(self.controlleruser)) |
| 85 | + pw = binascii.b2a_base64(bytes(self.controllerpassword)) |
| 86 | + retstr = "&username="+ str(acc) +"&password="+ str(pw) |
| 87 | + return retstr |
0 commit comments