def testDisconnect(self):
     self._log.debug('\nInitializing WrapperReactor')
     
     wrapperlist=self.makeWrappers(5)
     self._log.debug('\nWrapperlist is %s' %wrapperlist)
     wrapperReactor = WrapperReactor(wrapperlist)
     time.sleep(0.5)
     assert wrapperReactor.running        
     self._log.debug('\nWrapperReactor initialized')
     for wrapper in wrapperlist:
         wrapper['wrapper'].throwexception()
     time.sleep(1)
     for wrapper in wrapperReactor.wrapperList():
         assert wrapper['wrapper'] == None
Пример #2
0
class Serial(BaseHandler):
    """
    
    The configuration for a Serial Event Handler entry is as follows:

    
    
    Setting up serial ports at runtime 
    
    <eventInterface module='EventHandlers.Serial' name='Serial'>
        <serialPorts>
            <serialPort id="serial-1"  
                        type="tcp" 
                        name='LutronTCP' 
                        ipAddress = 'localhost' 
                        port='23'
                        driver = 'drivertype'
                        protocol_handler = 'handler'
                        />
            <serialPort id="serial-2"  
                        type="local" 
                        name='LutronLocal' 
                        uDevID = 'serial'
                        baudRate = '115200'
                        driver = 'drivertype'
                        protocol_handler = 'handler'
                        />
            </serialPort>
            <serialPort id="serial-3"  type="http" name='An http to serial relay' >
                <soonToCome/>
            </serialPort>     
        </serialPorts>
    </eventInterface>
    
    Listening for data via Serial Ports:
     <eventInterface module='EventHandlers.Serial' name='Serial'>
        <eventtype type="internal/test/serial">
            <eventsource source="serial/test/1" >
	        <event>
                    <params>
                    </params>
                    <serial cmd="read" data="64;65;66:" id='serial-1' />
	        </event>
            </eventsource>
            <eventsource source="serial/test/2" >
	        <event>
                    <params>
                    </params>
                    <serial cmd="read" data="64 65 66:" id='serial-2' />
	        </event>
            </eventsource>
        </eventtype>
    </eventInterface>
    
    Sending data via Serial Ports:

    <eventInterface module='EventHandlers.Serial' name='Serial'>
        <eventtype type="internal/test/serial">
            <eventsource source="serial/read/1" >
	        <event>
                    <params>
                    </params>
                    <serial cmd="write" data="64;65;66:" id='serial-1' />
	        </event>
            </eventsource>
            <eventsource source="serial/read/2" >
	        <event>
                    <params>
                    </params>
                    <serial cmd="write" data="64 65 66:" id='serial-2' />
	        </event>
            </eventsource>
        </eventtype>
    </eventInterface>

    """

    def __init__(self, localRouter):
        super(Serial, self).__init__(localRouter)
        self._serialPorts = dict()
        # the reactor starts automatically
        self._wrapperReactor = WrapperReactor()

    def AddConnection(self, PortConfig):
        if (
            PortConfig.has_key("id")
            & PortConfig.has_key("driver")
            & PortConfig.has_key("protocol_handler")
            & (self.isValidTCPCFG(PortConfig) | self.isValidLocalCFG(PortConfig))
        ):
            driver = self.GetDriver(PortConfig["driver"], PortConfig["id"])
            if driver != None:
                pHandler = self.GetProtocolHandler(PortConfig["protocol_handler"])
                if pHandler != None:
                    self._log.debug("Attempting to connect to port %s" % PortConfig)
                    wrapper = self.GetWrapper(PortConfig)
                    serialC = SerialConnection(wrapper, driver, pHandler, PortConfig["id"], PortConfig)
                    self._log.debug("Adding port %s to list" % serialC)
                    self._serialPorts[PortConfig["id"]] = serialC
                    if wrapper != None:
                        self._log.debug('Inserting port "%s" into reactor' % serialC)
                        self._wrapperReactor.addWrapper(
                            serialC.Id, serialC.SerialPort, self.DataReceivedCallback, "read"
                        )
                    else:
                        self._log.error("Failed to initialize wrapper, possible connection problem , will retry later")
                else:
                    self._log.error(
                        "Protocol Handler %s does not exist , invalid Serial Port - Ignoring"
                        % PortConfig["protocol_handler"]
                    )
            else:
                self._log.error("Driver %s does not exist , invalid Serial Port - Ignoring" % PortConfig["driver"])
        else:
            self._log.error("%s is not a valid configuration, please check syntax" % PortConfig)

    def configure(self, cfgDict):
        # NOTE Here we try and initialize the configured serial wrappers,
        # if they fail to initialize they are set to None , and whenever a write command is triggered an attempt to reinitialize them is made
        # also pass the config to the baseclass configure
        super(Serial, self).configure(cfgDict)
        self._log.debug("Configure.cfgDict == %s" % cfgDict)
        if cfgDict.has_key("serialPorts"):
            self._log.debug("Found serialPorts to configure")
            for portConfig in cfgDict["serialPorts"]:
                self.AddConnection(portConfig)

    def configureActions(self, cfgDict):
        self._log.debug("configureActions %s" % cfgDict)
        result = None
        if cfgDict.has_key("serial"):
            if isinstance(cfgDict["serial"], list):
                result = cfgDict["serial"]
            else:
                result = list()
                result.append(cfgDict["serial"])
        self._log.debug("configureActions %s" % result)
        return result

    def WriteSerial(self, SerialC, Bytes):
        if SerialC.SerialPort != None:
            try:
                for byte in Bytes:
                    SerialC.SerialPort.write(chr(byte))
            except Exception, e:
                self._log.error(e)
                SerialC.SerialPort = None
        else:
Пример #3
0
 def __init__(self, localRouter):
     super(Serial, self).__init__(localRouter)
     self._serialPorts = dict()
     # the reactor starts automatically
     self._wrapperReactor = WrapperReactor()