示例#1
0
 def connectproc(self):
     self.conninprogress = True
     prevstate = self.connected
     try:
         misc.addLog(
             rpieGlobals.LOG_LEVEL_DEBUG, "BLE connection initiated to " +
             str(self.taskdevicepluginconfig[0]))
         self.BLEPeripheral = btle.Peripheral(
             str(self.taskdevicepluginconfig[0]))
         self.connected = True
         self.failures = 0
         self.BLEPeripheral.setDelegate(Lywsd02Delegate(self.callbackfunc))
     except:
         self.connected = False
     self.conninprogress = False
     self.isconnected()
     if self.connected == False:
         misc.addLog(
             rpieGlobals.LOG_LEVEL_ERROR,
             "BLE connection failed " + str(self.taskdevicepluginconfig[0]))
         self.failures = self.failures + 1
         if self.failures > 5:
             self._nextdataservetime = rpieTime.millis() + (self.interval *
                                                            5000)
             self._lastdataservetime = self._nextdataservetime
         return False
     else:
         misc.addLog(
             rpieGlobals.LOG_LEVEL_DEBUG,
             "BLE connected to " + str(self.taskdevicepluginconfig[0]))
         self.waitnotifications = True
         self.get_battery_value()
         rpieTime.addsystemtimer(3, self.isconnected, [-1])
示例#2
0
 def plugin_read(self):
     result = False
     if self.initialized and self.enabled:
         if str(self.taskdevicepluginconfig[0]) == "1":
             try:
                 prox = self.apds.readProximity()
                 prox = int((255 - prox) * 0.78)  # convert to mm
                 if prox < 0:
                     prox = 0
                 elif prox > 200:
                     prox = 200
                 if prox != self.uservar[0]:
                     self.set_value(1, (prox), False)
                 val = self.apds.readAmbientLight()
                 if val != self.uservar[1]:
                     self.set_value(2, (val), False)
             except Exception as e:
                 self.set_value(1, 0, False)
                 self.set_value(2, 0, False)
         else:
             self.uservar[0] = 0
             self.p064_get_gesture()
         self.plugin_senddata()
         rpieTime.addsystemtimer(2, self.p064_timercb, [-1])
         self._lastdataservetime = rpieTime.millis()
         result = True
     return result
示例#3
0
 def pinstate_check(self, postcheck=False):
     if self.initialized and self.enabled:
         prevval = int(float(self.uservar[0]))
         inval = gpios.HWPorts.input(int(self.taskdevicepin[0]))
         if self.pininversed:
             prevval = 1 - int(prevval)
         outval = prevval
         if int(self.taskdevicepluginconfig[2]) == 0:  # normal switch
             outval = int(inval)
         elif int(self.taskdevicepluginconfig[2]) == 1:  # active low button
             if inval == 0:  # if low
                 outval = 1 - int(prevval)  # negate
         elif int(
                 self.taskdevicepluginconfig[2]) == 2:  # active high button
             if inval == 1:  # if high
                 outval = 1 - int(prevval)  # negate
         if prevval != outval:
             self.set_value(1, int(outval), True)
             self._lastdataservetime = rpieTime.millis()
             if self.taskdevicepluginconfig[2] > 0 and self.timer100ms:
                 time.sleep(
                     self.taskdevicepluginconfig[1] /
                     1000)  # force debounce if not event driven detection
             if postcheck:
                 rpieTime.addsystemtimer(1, self.postchecker, [
                     int(self.taskdevicepin[0]),
                     int(float(self.uservar[0]))
                 ])  # failsafe check
示例#4
0
 def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1):
  if tasknum is None:
   return False
  if self.enabled and self.initialized:
    if tasknum!=-1:
     if time.time()-self.lastsend<self.defaultdelay:
      misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"Sending too soon for ThingSpeak, skip")
      rpieTime.addsystemtimer(3,self.retransmit,[tasknum,0])
      return False
     jdata = {}
     jdata['key'] = self.controllerpassword
     if changedvalue==-1:
      for u in range(Settings.Tasks[tasknum].valuecount):
       vname = Settings.Tasks[tasknum].valuenames[u].strip().lower()
       if vname.startswith('field')==False:
        vname = "field"+str(u+1)
       jdata[vname] = str(value[u])
     else:
      u = changedvalue-1
      vname = Settings.Tasks[tasknum].valuenames[u].strip().lower()
      if vname.startswith('field')==False:
        vname = "field"+str(u+1)
      jdata[vname] = str(value[u])

     jdata = urllib.parse.urlencode(jdata).encode("utf-8")
     urlstr = "http://"+str(self.controllerip)+":"+str(self.controllerport)+"/update" # create destination url
     misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"Sending task "+str(tasknum+1)+" data to ThingSpeak at "+str(urlstr))
     self.lastsend = time.time()
     httpproc = threading.Thread(target=self.urlpost, args=(urlstr,jdata,))  # use threading to avoid blocking
     httpproc.daemon = True
     httpproc.start()
     return True
  return False
示例#5
0
 def connectproc(self):
   try:
    if self.blestatus.isscaninprogress():
     self.blestatus.requeststopscan(self.taskindex)
     return False
   except Exception as e:
    return False
   self.conninprogress = True
   self.blestatus.registerdataprogress(self.taskindex)
   prevstate = self.connected
   try:
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BLE connection initiated to "+str(self.taskdevicepluginconfig[0]))
    time.sleep(uniform(0.4,1.8))
    self.BLEPeripheral = btle.Peripheral(str(self.taskdevicepluginconfig[0]),iface=self.taskdevicepluginconfig[2])
    self.connected = True
    self.failures = 0
    self.BLEPeripheral.setDelegate( TempHumDelegate(self.callbackfunc) )
   except:
    self.connected = False
   self.conninprogress = False
   self.isconnected()
   if self.connected==False:
    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BLE connection failed "+str(self.taskdevicepluginconfig[0]))
    self.blestatus.unregisterdataprogress(self.taskindex)
    self.failures =  self.failures +1
    if self.failures>5:
     self._nextdataservetime = rpieTime.millis()+(self.interval*5000)
     self._lastdataservetime = self._nextdataservetime
    return False
   else:
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BLE connected to "+str(self.taskdevicepluginconfig[0]))
    self.waitnotifications = True
    self.get_battery_value()
    rpieTime.addsystemtimer(3,self.isconnected,[-1])
示例#6
0
 def timer_once_per_second(self):
     if self.enabled and self.initialized:
         try:
             self.mpu.buf_read()
         except Exception as e:
             misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "MPU6050: " + str(e))
             self.initialized = False
         if int(self.taskdevicepluginconfig[1]) == 0:  # motion detection
             motion = 0
             if self.taskdevicepluginconfig[
                     2] != 0 and self.mpu.ax > self.taskdevicepluginconfig[
                         2]:
                 motion = 1
             else:
                 if self.taskdevicepluginconfig[
                         3] != 0 and self.mpu.ay > self.taskdevicepluginconfig[
                             3]:
                     motion = 1
                 else:
                     if self.taskdevicepluginconfig[
                             4] != 0 and self.mpu.az > self.taskdevicepluginconfig[
                                 4]:
                         motion = 1
             if int(motion) != int(float(self.uservar[0])):
                 if motion == 1:
                     self.lastmove = time.time()
                     rpieTime.addsystemtimer(
                         int(self.taskdevicepluginconfig[6]),
                         self.p045_timercb, [-1])
                 elif (time.time() -
                       self.lastmove) < self.taskdevicepluginconfig[6]:
                     return False
                 self.set_value(1, motion, True)
示例#7
0
 def setdisconnectstate(self, tryreconn=True):
     if self.connected:
         self.connected = False
         self.set_value(1, 0, False)
         self.set_value(2, 0, True, suserssi=-100, susebattery=0)
     if tryreconn and self.enabled:
         rpieTime.addsystemtimer(int(self.taskdevicepluginconfig[1]),
                                 self.reconnect, [-1])
示例#8
0
 def p001_handler(self,channel):
  if self.initialized and self.enabled:
   val = gpios.HWPorts.input(int(self.taskdevicepin[0]))
#   print("GPIO",int(self.taskdevicepin[0]),"v:",val)
   if int(val) != int(float(self.uservar[0])):
    self.set_value(1,val,True)
    self._lastdataservetime = rpieTime.millis()
    rpieTime.addsystemtimer(1,self.postchecker,[int(self.taskdevicepin[0]),int(float(self.uservar[0]))]) # failsafe check
示例#9
0
 def p064_handler(
         self,
         channel):  # this function called when interrupt pin pulled low
     self.p064_get_gesture()
     self.plugin_senddata()
     rpieTime.addsystemtimer(3, self.p064_timercb,
                             [-1])  # reset gesture to None (0) after 3 sec
     self._lastdataservetime = rpieTime.millis()
示例#10
0
 def setdisconnectstate(self,tryreconn=True):
  self.blestatus.unregisterdataprogress(self.taskindex)
  if self.connected:
    self.connected = False
    self.handlevalue(0,0)
#    self.set_value(1,0,False)
#    self.set_value(2,0,False,suserssi=-100,susebattery=0)
    self.plugin_senddata()
  if tryreconn and self.enabled:
   rpieTime.addsystemtimer(int(self.taskdevicepluginconfig[1]),self.reconnect,[-1])
示例#11
0
 def timer_ten_per_second(self):  # called 10 times per second (best effort)
     if self.initialized and self.enabled:
         tvar = self.uservar[0]
         self.p064_get_gesture()
         if int(tvar) != int(
                 self.uservar[0]
         ):  # publish changes if different gesture received than previous
             self.plugin_senddata()
             rpieTime.addsystemtimer(
                 3, self.p064_timercb,
                 [-1])  # reset gesture to None (0) after 3 sec
     return True
示例#12
0
 def pinstate_check(self,postcheck=False):
  if self.initialized and self.enabled:
   prevval = int(float(self.uservar[0]))
   try:
    inval = gpios.HWPorts.input(int(self.taskdevicepin[0]))
   except Exception as e:
    self.enabled = False
    misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Task now disabled, Pin"+ str(self.taskdevicepin[0])+" cannot be read!")
    return False
   if self.pininversed:
    prevval=1-int(prevval)
   outval = prevval
   if int(self.taskdevicepluginconfig[2])==0: # normal switch
    outval = int(inval)
   elif int(self.taskdevicepluginconfig[2])==1: # active low button
    if inval==0:             # if low
     outval = 1-int(prevval) # negate
   elif int(self.taskdevicepluginconfig[2])==2: # active high button
    if inval==1:             # if high
     outval = 1-int(prevval) # negate
   if prevval != outval:
    if self.taskdevicepluginconfig[4]>0 and self._lastdataservetime>0: # check for longpress
     docheck = False
     if self.taskdevicepluginconfig[4]==3:
      docheck = True
     elif self.taskdevicepluginconfig[4]==1 and int(prevval)==1 and int(outval)==0:
      docheck = True
     elif self.taskdevicepluginconfig[4]==2 and int(prevval)==0 and int(outval)==1:
      docheck = True
     self.set_value(1,int(outval),False)
     diff = (rpieTime.millis()-self._lastdataservetime)
     dolong = False
     if docheck:
       if diff > self.taskdevicepluginconfig[5]:
        dolong = True
     if dolong:
      self.set_value(2,1,False)
     else:
      self.set_value(2,0,False)
     if docheck:
      self.set_value(3,diff,False)
     else:
      self.set_value(3,0,False)
     self.plugin_senddata()
    else:
     self.set_value(1,int(outval),True)
    self._lastdataservetime = rpieTime.millis()
    if self.taskdevicepluginconfig[2]>0 and self.timer100ms:
      time.sleep(self.taskdevicepluginconfig[1]/1000) # force debounce if not event driven detection
    if postcheck:
     rpieTime.addsystemtimer(1,self.postchecker,[int(self.taskdevicepin[0]),int(float(self.uservar[0]))]) # failsafe check
示例#13
0
 def plugin_init(self,enableplugin=None):
    plugin.PluginProto.plugin_init(self,enableplugin)
    self.initialized = False
    if self.enabled:
     misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Try to init Jami connection ")
     try:
      self.sessid = self.jami.sessid
     except:
      self.sessid = -1

     jamict = self.taskdevicepluginconfig[0]
     if jamict==0:
      try:
       self.jami = dring.request_dring_channel(self.gettaskname(),0,True) # create or get existing Jami handler from library
       self.jami.cb_ring2 = self.cb_ring2
       self.jami.cb_call = self.cb_call
       self.jami.cb_text = self.cb_text
       self.initialized = self.jami.initialized
      except Exception as e:
       misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Jami DBUS init error: "+str(e))
     else: # bridge mode
      try:
       self.jami = dring.request_dring_bridge() # create or get existing Jami handler from library
       self.jami.cb_ring2 = self.cb_ring2
       self.jami.cb_call = self.cb_call
       self.jami.cb_text = self.cb_text
       self.initialized = self.jami.initialized
      except Exception as e:
       misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Jami bridge init error: "+str(e))
     if self.initialized:
       if self.sessid != self.jami.sessid:
        try:
         self.jami.daemon = True          # start it if not yet started
         self.jami.start()
        except:
         pass
        if jamict==0:
         signal.signal(signal.SIGINT, self.signal_handler) # grab back signal handler, otherwise ctrl-c will not work
        self.set_value(1,0,False)
        self.set_value(2,"INACTIVE",False)
        self.set_value(3,0,False)
        self.set_value(4,"",False)
        misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Jami connected: "+str(self.jami.initialized and self.jami.operational))
     else: # not initialized?
      rpieTime.addsystemtimer(30,self.initcheck,[0,0])
示例#14
0
 def plugin_write(self, cmd):  # handle incoming commands
     res = False
     cmdarr = cmd.split(",")
     cmdarr[0] = cmdarr[0].strip().lower(
     )  # implement scanning of devices to sync taskvalues??
     if cmdarr[0] == "extgpio":
         pin = -1
         val = -1
         try:
             pin = int(cmdarr[1].strip())
             val = int(cmdarr[2].strip())
         except:
             pin = -1
         if pin > -1 and val in [0, 1]:
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "EXTGPIO" + str(pin) + " set to " + str(val))
             try:
                 writecmd = self.create_write_buffer(0, pin, val)
                 self.pme_write_retry(writecmd, pin)
             except Exception as e:
                 misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                             "EXTGPIO" + str(pin) + ": " + str(e))
         res = True
     elif cmdarr[0] == "extpwm":
         pin = -1
         prop = -1
         try:
             pin = int(cmdarr[1].strip())
             prop = int(cmdarr[2].strip())
         except:
             pin = -1
             prop = -1
         if pin > -1 and prop > -1:
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "EXTPWM" + str(pin) + ": " + str(prop))
             try:
                 writecmd = self.create_write_buffer(1, pin, prop)
                 self.pme_write_retry(writecmd, pin)
             except Exception as e:
                 misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                             "EXTPWM" + str(pin) + ": " + str(e))
         res = True
     elif cmdarr[0] == "extpulse":
         pin = -1
         val = -1
         try:
             pin = int(cmdarr[1].strip())
             val = int(cmdarr[2].strip())
         except:
             pin = -1
         dur = 100
         try:
             dur = float(cmdarr[3].strip())
         except:
             dur = 100
         if pin > -1 and val in [0, 1]:
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "EXTGPIO" + str(pin) + ": Pulse started")
             try:
                 writecmd = self.create_write_buffer(0, pin, val)
                 self.pme_write_retry(writecmd, pin)
                 s = float(dur / 1000)
                 if s > self.pme.enddelay:
                     s = (s - self.pme.enddelay)  # endtransmission sleep
                     time.sleep(s)
                 writecmd = self.create_write_buffer(0, pin, (1 - val))
                 self.pme_write_retry(writecmd, pin)
             except Exception as e:
                 misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                             "EXTGPIO" + str(pin) + ": " + str(e))
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "EXTGPIO" + str(pin) + ": Pulse ended")
         res = True
     elif cmdarr[0] == "extlongpulse":
         pin = -1
         val = -1
         try:
             pin = int(cmdarr[1].strip())
             val = int(cmdarr[2].strip())
         except:
             pin = -1
         dur = 2
         try:
             dur = float(cmdarr[3].strip())
         except:
             dur = 2
         if pin > -1 and val in [0, 1]:
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "EXTGPIO" + str(pin) + ": LongPulse started")
             try:
                 writecmd = self.create_write_buffer(0, pin, val)
                 self.pme_write_retry(writecmd, pin)
             except Exception as e:
                 misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                             "EXTGPIO" + str(pin) + ": " + str(e))
             rarr = [pin, (1 - val)]
             rpieTime.addsystemtimer(dur, self.p011_timercb, rarr)
         res = True
     return res
示例#15
0
 def plugin_write(self,cmd):
  res = False
  cmdarr = cmd.split(",")
  cmdarr[0] = cmdarr[0].strip().lower()
  if cmdarr[0] == "gpio":
   pin = -1
   val = -1
   try:
    pin = int(cmdarr[1].strip())
    val = int(cmdarr[2].strip())
   except:
    pin = -1
   if pin>-1 and val in [0,1]:
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BCM"+str(pin)+" set to "+str(val))
    suc = False
    try:
     suc = True
     gpios.HWPorts.output(pin,val)
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BCM"+str(pin)+": "+str(e))
     suc = False
#    if suc == False:
#     try:
#      gpios.HWPorts.output(pin,val,True) # force output?
#     except Exception as e:
#      print("output failed ",pin,val,e)
#     suc = False
   res = True
  elif cmdarr[0]=="pwm":
   pin = -1
   prop = -1
   try:
    pin = int(cmdarr[1].strip())
    prop = int(cmdarr[2].strip())
   except:
    pin = -1
    prop = -1
   freq = 1000
   try:
    freq = int(cmdarr[3].strip())
   except:
    freq = 1000
   if pin>-1 and prop>-1:
    suc = False
    try:
     suc = True
     gpios.HWPorts.output_pwm(pin,prop,freq)
     misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BCM"+str(pin)+" PWM "+str(prop)+"% "+str(freq)+"Hz")
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BCM"+str(pin)+" PWM "+str(e))
     suc = False
   res = True
  elif cmdarr[0]=="pulse":
   pin = -1
   val = -1
   try:
    pin = int(cmdarr[1].strip())
    val = int(cmdarr[2].strip())
   except:
    pin = -1
   dur = 100
   try:
    dur = float(cmdarr[3].strip())
   except:
    dur = 100
   if pin>-1 and val in [0,1]:
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BCM"+str(pin)+": Pulse started")
    try:
     gpios.HWPorts.output(pin,val)
     s = (dur/1000)
     time.sleep(s)
     gpios.HWPorts.output(pin,(1-val))
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BCM"+str(pin)+": "+str(e))
     suc = False
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BCM"+str(pin)+": Pulse ended")
   res = True
  elif cmdarr[0]=="longpulse":
   pin = -1
   val = -1
   try:
    pin = int(cmdarr[1].strip())
    val = int(cmdarr[2].strip())
   except:
    pin = -1
   dur = 2
   try:
    dur = float(cmdarr[3].strip())
   except:
    dur = 2
   if pin>-1 and val in [0,1]:
    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BCM"+str(pin)+": LongPulse started")
    try:
     gpios.HWPorts.output(pin,val)
    except Exception as e:
     misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BCM"+str(pin)+": "+str(e))
     suc = False
    rarr = [pin,(1-val)]
    rpieTime.addsystemtimer(dur,self.p001_timercb,rarr)
   res = True
  return res
示例#16
0
def gpio_commands(cmd):
    res = False
    cmdarr = cmd.split(",")
    cmdarr[0] = cmdarr[0].strip().lower()
    if cmdarr[0] == "gpio":
        pin = -1
        val = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            val = int(cmdarr[2].strip())
        except:
            pin = -1
        if pin > -1 and val in [0, 1]:
            logline = "BCM" + str(pin) + " set to " + str(val)
            misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
            suc = False
            try:
                suc = True
                gpios.HWPorts.output(pin, val)
                syncvalue(pin, val)
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=val,
                                               pluginid=1,
                                               pmode="output",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + ": " + str(e))
                suc = False


#    if suc == False:
#     try:
#      gpios.HWPorts.output(pin,val,True) # force output?
#     except Exception as e:
#      print("output failed ",pin,val,e)
#     suc = False
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True
    elif cmdarr[0] == "pwm":
        pin = -1
        prop = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            prop = int(cmdarr[2].strip())
        except:
            pin = -1
            prop = -1
        freq = 1000
        try:
            freq = int(cmdarr[3].strip())
        except:
            freq = 1000
        if pin > -1 and prop > -1:
            suc = False
            try:
                suc = True
                gpios.HWPorts.output_pwm(pin, prop, freq)
                logline = "BCM" + str(pin) + " PWM " + str(prop) + "% " + str(
                    freq) + "Hz"
                misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=prop,
                                               pluginid=1,
                                               pmode="pwm",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + " PWM " + str(e))
                suc = False
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True
    elif cmdarr[0] == "pulse":
        pin = -1
        val = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            val = int(cmdarr[2].strip())
        except:
            pin = -1
        dur = 100
        try:
            dur = float(cmdarr[3].strip())
        except:
            dur = 100
        if pin > -1 and val in [0, 1]:
            logline = "BCM" + str(pin) + ": Pulse started"
            misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
            try:
                syncvalue(pin, val)
                gpios.HWPorts.output(pin, val)
                s = (dur / 1000)
                time.sleep(s)
                gpios.HWPorts.output(pin, (1 - val))
                syncvalue(pin, (1 - val))
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=(1 - val),
                                               pluginid=1,
                                               pmode="output",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + ": " + str(e))
                suc = False
            misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                        "BCM" + str(pin) + ": Pulse ended")
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True
    elif cmdarr[0] == "longpulse":
        pin = -1
        val = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            val = int(cmdarr[2].strip())
        except:
            pin = -1
        dur = 2
        try:
            dur = float(cmdarr[3].strip())
        except:
            dur = 2
        if pin > -1 and val in [0, 1]:
            logline = "BCM" + str(pin) + ": LongPulse started"
            misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
            try:
                gpios.HWPorts.output(pin, val)
                syncvalue(pin, val)
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=val,
                                               pluginid=1,
                                               pmode="output",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + ": " + str(e))
                suc = False
            rarr = [pin, (1 - val)]
            rpieTime.addsystemtimer(dur, timercb, rarr)
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True
    elif cmdarr[0] == "tone":
        pin = -1
        freq = -1
        dur = 0
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            freq = int(cmdarr[2].strip())
            dur = int(cmdarr[3].strip())
        except:
            pin = -1
            freq = -1
            dur = 0
        if pin > -1 and freq > -1 and dur > 0:
            suc = False
            try:
                suc = True
                logline = "BCM" + str(pin) + " " + str(freq) + "Hz"
                misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
                play_tone(pin, freq, dur)
                gpios.HWPorts.output_pwm(pin, 0, 0)  # stop sound
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=0,
                                               pluginid=1,
                                               pmode="pwm",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + " Tone " + str(e))
                suc = False
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True

    elif cmdarr[0] == "rtttl":
        cmdarr = cmd.replace(":", ",").split(",")
        pin = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
        except:
            pin = -1
        if pin > -1:
            suc = False
            try:
                sp = cmd.find(":")
                if sp > -1:
                    #      play_rtttl(pin,"t"+cmd[sp:])
                    rtproc = threading.Thread(
                        target=play_rtttl,
                        args=(pin, "t" +
                              cmd[sp:]))  # play in background - no blocking
                    rtproc.daemon = True
                    rtproc.start()
                suc = True
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, str(e))
                suc = False
        res = True
    elif cmdarr[0] == "status":
        pin = -1
        subcmd = ""
        try:
            subcmd = str(cmdarr[1].strip()).lower()
            pin = int(cmdarr[2].strip())
        except:
            pin = -1
            print(e)
        if pin > -1 and subcmd == "gpio":
            gi = gpios.GPIO_refresh_status(pin)
            if gi > -1:
                return gpios.GPIO_get_status(gi)
        res = True

    return res
示例#17
0
 def plugin_write(self, cmd):  # handle incoming commands
     res = False
     cmdarr = cmd.split(",")
     cmdarr[0] = cmdarr[0].strip().lower()
     if cmdarr[0] == "mcpgpio":
         pin = -1
         val = -1
         try:
             pin = int(cmdarr[1].strip())
             ti2ca, trpin = lib_mcprouter.get_pin_address(pin)
             val = int(cmdarr[2].strip())
         except:
             pin = -1
             trpin = -1
         if pin > -1 and val in [0, 1] and trpin > -1:
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "MCPGPIO" + str(pin) + " set to " + str(val))
             try:
                 tmcp = lib_mcprouter.request_mcp_device(
                     int(self.i2cport), int(pin))
                 tmcp.set_mode(trpin, 'output')
                 tmcp.output(trpin, val)
             except Exception as e:
                 misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                             "MCPGPIO" + str(pin) + ": " + str(e))
         return True
     elif cmdarr[0] == "mcppulse":
         pin = -1
         val = -1
         try:
             pin = int(cmdarr[1].strip())
             ti2ca, trpin = lib_mcprouter.get_pin_address(pin)
             val = int(cmdarr[2].strip())
         except:
             pin = -1
             trpin = -1
         dur = 100
         try:
             dur = float(cmdarr[3].strip())
         except:
             dur = 100
         if pin > -1 and val in [0, 1] and trpin > -1:
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "MCPGPIO" + str(pin) + ": Pulse started")
             try:
                 tmcp = lib_mcprouter.request_mcp_device(
                     int(self.i2cport), int(pin))
                 tmcp.set_mode(trpin, 'output')
                 tmcp.output(trpin, val)
                 s = float(dur / 1000)
                 time.sleep(s)
                 tmcp.output(trpin, (1 - val))
             except Exception as e:
                 misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                             "MCPGPIO" + str(pin) + ": " + str(e))
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "MCPGPIO" + str(pin) + ": Pulse ended")
         return True
     elif cmdarr[0] == "mcplongpulse":
         pin = -1
         val = -1
         try:
             pin = int(cmdarr[1].strip())
             ti2ca, trpin = lib_mcprouter.get_pin_address(pin)
             val = int(cmdarr[2].strip())
         except:
             pin = -1
             trpin = -1
         dur = 2
         try:
             dur = float(cmdarr[3].strip())
         except:
             dur = 2
         if pin > -1 and val in [0, 1] and trpin > -1:
             misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                         "MCPGPIO" + str(pin) + ": LongPulse started")
             try:
                 tmcp = lib_mcprouter.request_mcp_device(
                     int(self.i2cport), int(pin))
                 tmcp.set_mode(trpin, 'output')
                 tmcp.output(trpin, val)
             except Exception as e:
                 misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                             "MCPGPIO" + str(pin) + ": " + str(e))
             rarr = [trpin, (1 - val)]
             rpieTime.addsystemtimer(dur, self.p009_timercb, rarr)
         return True
     return res
示例#18
0
def gpio_commands(cmd):
    res = False
    cmdarr = cmd.split(",")
    cmdarr[0] = cmdarr[0].strip().lower()
    if cmdarr[0] == "gpio":
        pin = -1
        val = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            val = int(cmdarr[2].strip())
        except:
            pin = -1
        if pin > -1 and val in [0, 1]:
            logline = "BCM" + str(pin) + " set to " + str(val)
            misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
            suc = False
            try:
                suc = True
                gpios.HWPorts.output(pin, val)
                syncvalue(pin, val)
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=val,
                                               pluginid=1,
                                               pmode="output",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + ": " + str(e))
                suc = False


#    if suc == False:
#     try:
#      gpios.HWPorts.output(pin,val,True) # force output?
#     except Exception as e:
#      print("output failed ",pin,val,e)
#     suc = False
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True
    elif cmdarr[0] == "pwm":
        pin = -1
        prop = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            prop = int(cmdarr[2].strip())
        except:
            pin = -1
            prop = -1
        fade = 0
        try:
            fade = int(cmdarr[3].strip())
        except:
            fade = 0
        freq = 1000
        try:
            freq = int(cmdarr[4].strip())
        except:
            freq = 1000
        if pin > -1 and prop > -1:
            suc = False
            try:
                suc = True
                if fade == 0:
                    gpios.HWPorts.output_pwm(pin, prop, freq)
                    logline = "BCM" + str(pin) + " PWM " + str(
                        prop) + "% " + str(freq) + "Hz"
                    misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
                else:
                    cs = gpios.GPIO_get_statusid(pin)
                    prev_value = 0
                    try:
                        if cs > -1:
                            if gpios.GPIOStatus[cs]["mode"] == "pwm":
                                prev_value = int(gpios.GPIOStatus[cs]["state"])
                    except:
                        prev_value = 0
                    step_value = (int(prop - prev_value) << 12) / fade
                    curr_value = int(prev_value) << 12
                    i = fade
                    while i > 0:
                        curr_value += step_value
                        new_value = int(curr_value) >> 12
                        gpios.HWPorts.output_pwm(pin, new_value, freq)
                        time.sleep(
                            0.001
                        )  # 1 millisecond in theory, more in reality..
                        i -= 1
                    gpios.HWPorts.output_pwm(pin, prop, freq)
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=prop,
                                               pluginid=1,
                                               pmode="pwm",
                                               logtext=logline)
                syncpwm(pin, prop)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + " PWM " + str(e))
                suc = False
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True
    elif cmdarr[0] == "pulse":
        pin = -1
        val = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            val = int(cmdarr[2].strip())
        except:
            pin = -1
        dur = 100
        try:
            dur = float(cmdarr[3].strip())
        except:
            dur = 100
        if pin > -1 and val in [0, 1]:
            logline = "BCM" + str(pin) + ": Pulse started"
            misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
            try:
                syncvalue(pin, val)
                gpios.HWPorts.output(pin, val)
                s = (dur / 1000)
                time.sleep(s)
                gpios.HWPorts.output(pin, (1 - val))
                syncvalue(pin, (1 - val))
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=(1 - val),
                                               pluginid=1,
                                               pmode="output",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + ": " + str(e))
                suc = False
            misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,
                        "BCM" + str(pin) + ": Pulse ended")
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True
    elif cmdarr[0] == "longpulse":
        pin = -1
        val = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            val = int(cmdarr[2].strip())
        except:
            pin = -1
        dur = 2
        try:
            dur = float(cmdarr[3].strip())
        except:
            dur = 2
        if pin > -1 and val in [0, 1]:
            logline = "BCM" + str(pin) + ": LongPulse started"
            misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
            try:
                gpios.HWPorts.output(pin, val)
                syncvalue(pin, val)
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=val,
                                               pluginid=1,
                                               pmode="output",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + ": " + str(e))
                suc = False
            rarr = [pin, (1 - val)]
            rpieTime.addsystemtimer(dur, timercb, rarr)
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True
    elif cmdarr[0] == "tone":
        pin = -1
        freq = -1
        dur = 0
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
            freq = int(cmdarr[2].strip())
            dur = int(cmdarr[3].strip())
        except:
            pin = -1
            freq = -1
            dur = 0
        if pin > -1 and freq > -1 and dur > 0:
            suc = False
            try:
                suc = True
                logline = "BCM" + str(pin) + " " + str(freq) + "Hz"
                misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
                play_tone(pin, freq, dur)
                gpios.HWPorts.output_pwm(pin, 0, 0)  # stop sound
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=0,
                                               pluginid=1,
                                               pmode="pwm",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + " Tone " + str(e))
                suc = False
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True

    elif cmdarr[0] == "rtttl":
        cmdarr = cmd.replace(":", ",").split(",")
        pin = -1
        gi = -1
        logline = ""
        try:
            pin = int(cmdarr[1].strip())
        except:
            pin = -1
        if pin > -1:
            suc = False
            try:
                sp = cmd.find(":")
                if sp > -1:
                    #      play_rtttl(pin,"t"+cmd[sp:])
                    rtproc = threading.Thread(
                        target=play_rtttl,
                        args=(pin, "t" +
                              cmd[sp:]))  # play in background - no blocking
                    rtproc.daemon = True
                    rtproc.start()
                suc = True
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, str(e))
                suc = False
        res = True

    elif cmdarr[0] == "servo":
        snr = -1
        pin = -1
        pos = -1
        gi = -1
        logline = ""
        try:
            snr = int(cmdarr[1].strip())
            pin = int(cmdarr[2].strip())
            pos = int(cmdarr[3].strip())
        except:
            snr = -1
            pin = -1
            pos = 0
        if snr > -1 and pin > -1 and pos > 0:
            suc = False
            try:
                suc = True
                logline = "BCM" + str(pin) + " to servo " + str(pos) + " angle"
                misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG, logline)
                setservoangle(pin, pos)
                gi = gpios.GPIO_refresh_status(pin,
                                               pstate=0,
                                               pluginid=1,
                                               pmode="servo",
                                               logtext=logline)
            except Exception as e:
                misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,
                            "BCM" + str(pin) + " Servo " + str(e))
                suc = False
        if gi > -1:
            return gpios.GPIO_get_status(gi)
        res = True

    elif cmdarr[0] == "status":
        pin = -1
        subcmd = ""
        try:
            subcmd = str(cmdarr[1].strip()).lower()
            pin = int(cmdarr[2].strip())
        except:
            pin = -1
            print(e)
        if pin > -1 and subcmd == "gpio":
            gi = gpios.GPIO_refresh_status(pin)
            if gi > -1:
                return gpios.GPIO_get_status(gi)
        res = True

    return res