Beispiel #1
0
class IfkAmqpAgent(object):
    """ Interface to AMQP
    
        Publishes the Ifk 'io' messages
        on AMQP fabric through the "org.phidgets" exchange
    """
    EXCH="org.phidgets"
    
    LCONFIG = {"%conn-open": 0
               ,"%conn-error" :  4*60 
               ,"%json-encode": 4*60*60
                }  
    
    def __init__(self):
        self.config=None
        self.comm=None
        Bus.publish(self, "%llconfig", self.LCONFIG)
    
    def _hconfig(self, config):
        self.config=config
    
    def Error(self, dic):
        """Generated when an error on a device is detected"""
        if self.comm:
            self.comm.publish("device.error", dic)

    def Din(self, dic):
        """Generated when the state of a digital input changes"""
        if self.comm:
            self.comm.publish("device.io.din", dic)

    def Dout(self, dic):
        """Generated when the state of a digital output changes"""
        if self.comm:
            self.comm.publish("device.io.dout", dic)

    def Ain(self, dic):
        """Generated when the state of an analog input changes"""
        if self.comm:
            self.comm.publish("device.io.ain", dic)
        

    def _hpoll(self, pc):
        if self.config is None:
            Bus.publish(self, "%config-amqp?")

        if self.comm is not None:
            if not self.comm.isOk():
                del self.comm
                self.comm=None
            
        if self.comm is None:
            self.comm=AMQPCommTx(self.config, self.EXCH)
            self.comm.connect()
class SensorsHandlerAgent(object):
    
    EXCH="org.sensors"
    
    def __init__(self):
        self.config=None
        self.comm=None
    
    def _hConfig(self, config):
        self.config=config
    
    def _hPoll(self, pc):
        if not self.config:
            Bus.publish(self, "%config-amqp?")
        
        if not self.comm:
            self.comm=AMQPCommTx(self.config, self.EXCH)
            self.comm.connect()
            
        if not self.comm.isOk():
            del self.comm
            self.comm=None

    
    def StateChanged(self, sensor_type, dic):
        """Generated when a sensor changes state"""
        if self.comm:
            self.comm.publish("state.io.%s" % sensor_type, dic) 

    def ConfigSensors(self, config):
        """Generated when sensor configuration changess and
            also periodically
        """
        if self.comm:
            self.comm.publish("config", config)
 def _hPoll(self, pc):
     if not self.config:
         Bus.publish(self, "%config-amqp?")
     
     if not self.comm:
         self.comm=AMQPCommTx(self.config, self.EXCH)
         self.comm.connect()
         
     if not self.comm.isOk():
         del self.comm
         self.comm=None
Beispiel #4
0
    def _hpoll(self, pc):
        if self.config is None:
            Bus.publish(self, "%config-amqp?")

        if self.comm is not None:
            if not self.comm.isOk():
                del self.comm
                self.comm=None
            
        if self.comm is None:
            self.comm=AMQPCommTx(self.config, self.EXCH)
            self.comm.connect()