예제 #1
0
    def senddata(self, idx, taskobj, changedvalue=-1):
        if self.enabled:
            mStates = ["Off", "On"]
            #   domomsg = '{{ "idx": {0}, "nvalue": {1:0.2f}, "svalue": "{2}" }}'
            domomsgw = '{{ "idx": {0}, "nvalue": {1:0.2f}, "svalue": "{2}", "RSSI": {3} }}'
            domomsgwb = '{{ "idx": {0}, "nvalue": {1:0.2f}, "svalue": "{2}", "RSSI": {3}, "Battery": {4} }}'
            domosmsgw = '{{"command": "switchlight", "idx": {0}, "switchcmd": "Set Level", "level":"{1}", "RSSI": {2} }}'
            domosmsgwb = '{{"command": "switchlight", "idx": {0}, "switchcmd": "Set Level", "level":"{1}", "RSSI": {2}, "Battery": {3} }}'
            if self._connected:
                try:
                    usebattery = float(str(taskobj.battery).strip())
                except Exception as e:
                    usebattery = -1
                if int(idx) > 0:
                    if usebattery != -1 and usebattery != 255:
                        bval = int(usebattery)
                    else:
                        bval = int(misc.get_battery_value())
                    msg = ""
                    if (int(taskobj.vtype) == pglobals.SENSOR_TYPE_SWITCH):
                        try:
                            stateid = round(float(taskobj.uservar[0]))
                        except:
                            stateid = 0
                        if stateid < 0:
                            stateid = 0
                        if stateid > 1:
                            stateid = 1
                        msg = domomsgwb.format(str(idx), int(stateid),
                                               mStates[stateid],
                                               mapRSSItoDomoticz(taskobj.rssi),
                                               str(bval))
                    elif (int(taskobj.vtype) == pglobals.SENSOR_TYPE_DIMMER):
                        msg = domosmsgwb.format(
                            str(idx), str(taskobj.uservar[0]),
                            mapRSSItoDomoticz(taskobj.rssi), str(bval))
                    else:
                        msg = domomsgwb.format(
                            str(idx), 0,
                            formatDomoticzSensorType(taskobj.vtype,
                                                     taskobj.uservar),
                            mapRSSItoDomoticz(taskobj.rssi), str(bval))
                    try:
                        self._mqttclient.publish(self.inchannel.encode(),
                                                 msg.encode())
                    except:
                        self._connected = False

                else:
                    misc.addLog(pglobals.LOG_LEVEL_ERROR,
                                "MQTT idx error, sending failed.")
            else:
                misc.addLog(pglobals.LOG_LEVEL_ERROR,
                            "MQTT not connected, sending failed.")
                if ((utime.time() - self.lastreconnect) > 30):
                    #    if ((time.time()-self.lastreconnect)>30) and (self.connectinprogress==0):
                    self.connect()
예제 #2
0
 def senddata(self,
              idx,
              sensortype,
              value,
              userssi=-1,
              usebattery=-1,
              tasknum=-1,
              changedvalue=-1):
     if self.enabled:
         if int(idx) != 0:
             try:
                 usebattery = float(usebattery)
             except:
                 usebattery = -1
             if int(sensortype) == rpieGlobals.SENSOR_TYPE_SWITCH:
                 url = "/json.htm?type=command&param=switchlight&idx="
                 url += str(idx)
                 url += "&switchcmd="
                 if round(float(value[0])) == 0:
                     url += "Off"
                 else:
                     url += "On"
             elif int(sensortype) == rpieGlobals.SENSOR_TYPE_DIMMER:
                 url = "/json.htm?type=command&param=switchlight&idx="
                 url += str(idx)
                 url += "&switchcmd="
                 if float(value[0]) == 0:
                     url += "Off"
                 else:
                     url += "Set%20Level&level="
                     url += str(value[0])
             else:
                 url = "/json.htm?type=command&param=udevice&idx="
                 url += str(idx)
                 url += "&nvalue=0&svalue="
                 url += formatDomoticzSensorType(sensortype, value)
             url += "&rssi="
             url += mapRSSItoDomoticz(userssi)
             if int(usebattery) != -1 and int(
                     usebattery
             ) != 255:  # battery input 0..100%, 255 means not supported
                 url += "&battery="
                 url += str(int(usebattery))
             else:
                 bval = misc.get_battery_value()
                 url += "&battery="
                 url += str(int(bval))
             urlstr = self.controllerip + ":" + self.controllerport + url + self.getaccountstr(
             )
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, urlstr)  # sendviahttp
             httpproc = Process(
                 target=self.urlget,
                 args=(urlstr, ))  # use multiprocess to avoid blocking
             httpproc.start()
         else:
             misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                         "MQTT : IDX cannot be zero!")
예제 #3
0
 def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1):
  if self.enabled:
    sqlstr = "insert into easysensor (time,unit,nodename,taskname,sensortype"
    sqlstr2 = ") values ('"+ str(datetime.datetime.now()) +"',"+str(Settings.Settings["Unit"])+",'"+str(Settings.Settings["Name"])+"','"+str(Settings.Tasks[tasknum].gettaskname())+"',"+str(sensortype)
    vcount = 4
    if tasknum!=-1:
     try:
      sqlstr += ",tasknum"
      sqlstr2 += ","+str(tasknum+1)
      vcount = Settings.Tasks[tasknum].valuecount
     except:
      pass
    if vcount>0:
     if sensortype==rpieGlobals.SENSOR_TYPE_TEXT:
      sqlstr += ",valuetext"
     else:
      sqlstr += ",value1"
     sqlstr2 += ","+str(value[0])
    if vcount>1:
     sqlstr += ",value2"
     sqlstr2 += ","+str(value[1])
    if vcount>2:
     sqlstr += ",value3"
     sqlstr2 += ","+str(value[2])
    if vcount>3:
     sqlstr += ",value4"
     sqlstr2 += ","+str(value[3])

    sqlstr += ",rssi"
    sqlstr2 += ","+str(getCachedRSSI(userssi))
    try:
     usebattery = float(usebattery)
    except:
     usebattery = -1
    if usebattery != -1 and usebattery != 255: # battery input 0..100%, 255 means not supported
     sqlstr += ",battery"
     sqlstr2 += ","+str(usebattery)
    else:
     bval = misc.get_battery_value()
     if bval != 255:
      sqlstr += ",battery"
      sqlstr2 += ","+str(bval)

    sqlstr = sqlstr + sqlstr2+")"
#    print(sqlstr)
    try:
     self.db.sqlexec(sqlstr)
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"DBStore: "+str(e))
예제 #4
0
 def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1):
  if self.enabled:
   mStates = ["Off","On"]
#   domomsg = '{{ "idx": {0}, "nvalue": {1:0.2f}, "svalue": "{2}" }}'
   domomsgw = '{{ "idx": {0}, "nvalue": {1:0.2f}, "svalue": "{2}", "RSSI": {3} }}'
   domomsgwb = '{{ "idx": {0}, "nvalue": {1:0.2f}, "svalue": "{2}", "RSSI": {3}, "Battery": {4} }}'
   domosmsgw = '{{"command": "switchlight", "idx": {0}, "switchcmd": "Set Level", "level":"{1}", "RSSI": {2} }}'
   domosmsgwb = '{{"command": "switchlight", "idx": {0}, "switchcmd": "Set Level", "level":"{1}", "RSSI": {2}, "Battery": {3} }}'
   if self.isconnected(False):
    try:
     usebattery = float(str(usebattery).strip())
    except Exception as e:
     usebattery = -1
    if int(idx) > 0:
     if usebattery != -1 and usebattery != 255:
      bval = usebattery
     else:
      bval = misc.get_battery_value()
     msg = ""
     if (int(sensortype)==rpieGlobals.SENSOR_TYPE_SWITCH):
      try:
       stateid = round(float(value[0]))
      except:
       stateid = 0
      if stateid<0:
       stateid = 0
      if stateid>1:
       stateid = 1
      msg = domomsgwb.format(str(idx), int(stateid), mStates[stateid], mapRSSItoDomoticz(userssi),str(bval))
     elif (int(sensortype)==rpieGlobals.SENSOR_TYPE_DIMMER):
      msg = domosmsgwb.format(str(idx), str(value[0]), mapRSSItoDomoticz(userssi),str(bval))
     else:
      msg = domomsgwb.format(str(idx), 0, formatDomoticzSensorType(sensortype,value), mapRSSItoDomoticz(userssi),str(bval))
     mres = 1
     try:
       (mres,mid) = self.mqttclient.publish(self.inchannel,msg)
     except:
       mres = 1
     if mres!=0:
       self.isconnected()
    else:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT idx error, sending failed.")
   else:
    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT not connected, sending failed.")
    if ((time.time()-self.lastreconnect)>30):
#    if ((time.time()-self.lastreconnect)>30) and (self.connectinprogress==0):
     self.connect()
예제 #5
0
 def senddata(self, idx, taskobj, changedvalue=-1):
     if self.enabled:
         if int(idx) != 0:
             if int(taskobj.vtype) == pglobals.SENSOR_TYPE_SWITCH:
                 url = "/json.htm?type=command&param=switchlight&idx="
                 url += str(idx)
                 url += "&switchcmd="
                 if round(float(taskobj.uservar[0])) == 0:
                     url += "Off"
                 else:
                     url += "On"
             elif int(taskobj.vtype) == pglobals.SENSOR_TYPE_DIMMER:
                 url = "/json.htm?type=command&param=switchlight&idx="
                 url += str(idx)
                 url += "&switchcmd="
                 if float(taskobj.uservar[0]) == 0:
                     url += "Off"
                 else:
                     url += "Set%20Level&level="
                     url += str(taskobj.uservar[0])
             else:
                 url = "/json.htm?type=command&param=udevice&idx="
                 url += str(idx)
                 url += "&nvalue=0&svalue="
                 url += formatDomoticzSensorType(taskobj.vtype,
                                                 taskobj.uservar)
             url += "&rssi="
             url += mapRSSItoDomoticz(taskobj.rssi)
             if taskobj.battery != -1 and taskobj.battery != 255:  # battery input 0..100%, 255 means not supported
                 url += "&battery="
                 url += str(int(taskobj.battery))
             else:
                 bval = misc.get_battery_value()
                 url += "&battery="
                 url += str(int(bval))
             urlstr = self.controllerip + ":" + self.controllerport + url + self.getaccountstr(
             )
             misc.addLog(pglobals.LOG_LEVEL_DEBUG, urlstr)  # sendviahttp
             self.urlget(urlstr)
         else:
             misc.addLog(pglobals.LOG_LEVEL_ERROR,
                         "MQTT : IDX cannot be zero!")
예제 #6
0
 def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1):
  if self.enabled:
   success = False
   if self.isconnected(False):
    if self.useJSON:
     changedvalue = 1 # force only one msg sending
    if tasknum!=-1:
     tname = Settings.Tasks[tasknum].gettaskname()
     if changedvalue==-1:
      for u in range(Settings.Tasks[tasknum].valuecount):
       vname = Settings.Tasks[tasknum].valuenames[u]
       if vname != "":
        if ('%t' in self.inch) or ('%v' in self.inch):
         gtopic = self.inch.replace('#/','')
         gtopic = gtopic.replace('#','')
         gtopic = gtopic.replace('%tskname%',tname)
         gtopic = gtopic.replace('%tskid%',str(tasknum+1))
         gtopic = gtopic.replace('%valname%',vname)
        else:
         gtopic = self.inch.replace('#',tname+"/"+vname)
        gval = str(value[u])
        if gval == "":
         gval = "0"
        mres = 1
        try:
         (mres,mid) = self.mqttclient.publish(gtopic,gval)
#         print(gtopic) # DEBUG
        except:
         mres = 1
        if mres!=0:
         self.isconnected()
         break
     else:
      vname = Settings.Tasks[tasknum].valuenames[changedvalue-1]
      if ('%t' in self.inch) or ('%v' in self.inch):
         gtopic = self.inch.replace('#/','')
         gtopic = gtopic.replace('#','')
         gtopic = gtopic.replace('%tskname%',tname)
         gtopic = gtopic.replace('%tskid%',str(tasknum+1))
         gtopic = gtopic.replace('%valname%',vname)
      else:
        if self.useJSON:
         gtopic = self.inch.replace('#',tname) # use only taskname for json reporting
        else:
         gtopic = self.inch.replace('#',tname+"/"+vname)
      if vname != "":
       gval = str(value[changedvalue-1])
       if gval == "":
         gval = "0"
       if self.useJSON: # modify payload
          gval = '{"taskname":"'+ Settings.Tasks[tasknum].taskname +'",'
          for u in range(Settings.Tasks[tasknum].valuecount):
           gval += '"'+ Settings.Tasks[tasknum].valuenames[u] + '":'
           val = value[u]
           if str(val).replace(".","").isnumeric():
            gval += str(val)
           else:
            gval += '"'+ str(val) +'"'
           gval += ","
          try:
           usebattery = float(str(usebattery).strip())
          except Exception as e:
           usebattery = -1
          bval = -1
          if usebattery != -1 and usebattery != 255:
           bval = usebattery
          else:
           bval = misc.get_battery_value()
          if bval != -1 and bval != 255:
           gval += '"battery":'+ str(bval)+ ','
          if userssi != -1:
           gval += '"rssi":'+ str(userssi)+ ','
          ps = str(Settings.Tasks[tasknum].ports)
          if ps != "0" and ps != "":
           gval += '"port":"'+ str(ps)+ '",'
          gval = gval[:-1]+"}"
       mres = 1
       try:
         (mres,mid) = self.mqttclient.publish(gtopic,gval)
#         print(gtopic,gval) # DEBUG
       except:
         mres = 1
       if mres!=0:
         self.isconnected()
    else:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT taskname error, sending failed.")
   else:
    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT not connected, sending failed.")
    if (time.time()-self.lastreconnect)>30:
     self.connect()