def testReadLocal(self): """ Test recieve callback is working correctly for serial ports """ self._log.debug( "\n\n-------------------------------testReadLocal-------------------------------" ) TCPServ = TestTCPServer(4747) TCPServ.start() try: self._log.debug( "\nInitializing serial eventhandler with tcp connection configured" ) testCfg = getDictFromXmlString(testConfigLocal) eh = Serial(self._router) self.assertNotEqual( eh, None ) eh.configure(testCfg['eventInterface']) eh.start() self._log.debug ( "Sending test data" ) portlist = eh.getSerialPorts() portlist["serial-2"].write("hello\r") portlist["serial-2"].write("world\r") time.sleep(2) self.assertEqual( len(self._router._pubs), 2 ) self.assertEqual( self._router._pubs[0][1].getType(), "http://id.webbrick.co.uk/events/serial/receive" ) self.assertEqual( self._router._pubs[0][1].getSource(), "serial-2/receive" ) self.assertEqual( self._router._pubs[0][1].getPayload()['data'], "104;101;108;108;111:" ) self.assertEqual( self._router._pubs[1][1].getType(), "http://id.webbrick.co.uk/events/serial/receive" ) self.assertEqual( self._router._pubs[1][1].getSource(), "serial-2/receive" ) self.assertEqual( self._router._pubs[1][1].getPayload()['data'], "119;111;114;108;100:" ) eh.stop() finally: TCPServ.stop() time.sleep(1)
def testReadTCP(self): """ Test recieve callback is working correctly for serial ports """ self._log.debug( "\n\n-------------------------------testReadTCP-------------------------------" ) TCPServ = TestTCPServer(4747) TCPServ.start() try: self._log.debug( "\nInitializing serial eventhandler with tcp connection configured" ) testCfg = getDictFromXmlString(testConfigTCP) eh = Serial(self._router) self.assertNotEqual( eh, None ) eh.configure(testCfg['eventInterface']) eh.start() self._log.debug ( "Sending test data" ) TCPServ.send("hello\r") time.sleep(1) self.assertEqual( len(self._router._pubs), 1 ) self.assertEqual( self._router._pubs[0][1].getType(), "lighting/update" ) self.assertEqual( self._router._pubs[0][1].getSource(), "light1/on" ) self.assertEqual( self._router._pubs[0][1].getPayload()['lightval'], "1") eh.stop() finally: TCPServ.stop() time.sleep(1)
def testWriteTCP(self): """ Test what happens when we start a write event to a TCP serial port """ self._log.debug( "\n\n-------------------------------testWriteTCP-------------------------------" ) self._log.debug( "\nInitializng TCP server" ) TCPServ = TestTCPServer(4747) TCPServ.start() try: self._log.debug( "\nInitializing serial eventhandler" ) testCfg = getDictFromXmlString(testConfigTCP) self._log.debug( "\ntestCfg : %s" %testCfg ) eh=Serial(self._router) self.assertNotEqual( eh, None ) eh.configure(testCfg['eventInterface']) eh.start() self._log.debug( "\nCreating test event :" ) testEvent = makeEvent( 'internal/test/serial', 'serial/test/1', {} ) self._log.debug( testEvent ) self._log.debug( "\nTesting eventhandler..." ) eh.handleEvent( testEvent ) time.sleep(1) portlist = eh.getSerialPorts() self._log.debug( "\nList of active ports : %s" %portlist ) data = TCPServ.data self._log.debug( "\nTCP server recieved : ##%s##" %data ) assert data == "turn lights onsuccess!" eh.stop() finally: TCPServ.stop()
def testWriteTCPAndLocal(self): """ Test what happens when we start a write event to a TCP serial port and a Local one at the same time """ self._log.debug( "\n\n-------------------------------testWriteTCPAndLocal-------------------------------" ) self._log.debug( "\nInitializng TCP server" ) TCPServ = TestTCPServer(4747) TCPServ.start() try: self._log.debug( "\nInitializing serial eventhandler" ) testCfg = getDictFromXmlString(testConfigLocalAndTCP) self._log.debug( "\ntestCfg : %s" %testCfg ) eh=Serial(self._router) self.assertNotEqual( eh, None ) self._log.debug( "\n Configuring serial eventhandler" ) eh.configure(testCfg['eventInterface']) eh.start() self._log.debug( "\nCreating test events :" ) TCPSerialevent = makeEvent( 'internal/test/serial', 'serial/test/1', {} ) localSerialevent = makeEvent( 'internal/test/serial', 'serial/test/2', {} ) self._log.debug( "%s and %s" %(localSerialevent,TCPSerialevent) ) self._log.debug( "\nSending TCP event..." ) eh.handleEvent( TCPSerialevent ) self._log.debug( "\nSending local event..." ) eh.handleEvent( localSerialevent ) time.sleep(1) portlist = eh.getSerialPorts() self._log.debug( "\nList of active ports : %s" %portlist ) TCPData = TCPServ.data self._log.debug( "\nTCP server recieved : ##%s##" %TCPData ) assert TCPData == 'ABC' self.assertEqual( len(self._router._pubs), 1 ) self.assertEqual( self._router._pubs[0][1].getType(), "http://id.webbrick.co.uk/events/serial/receive" ) self.assertEqual( self._router._pubs[0][1].getSource(), "serial-2/receive" ) self.assertEqual( self._router._pubs[0][1].getPayload()['data'], "66;67;68:" ) self._log.debug( "\nLocal serial recieved local data ") eh.stop() finally : TCPServ.stop() time.sleep(1)
def testDisconnectRecover(self): """ Test recieve callback is working correctly for serial ports """ self._log.debug( "\n\n-------------------------------testReadTCP-------------------------------" ) TCPServ = TestTCPServer(4747) TCPServ.start() self._log.debug( "\nInitializing serial eventhandler with tcp connection configured" ) testCfg = getDictFromXmlString(testConfigTCP) eh = Serial(self._router) self.assertNotEqual( eh, None ) eh.configure(testCfg['eventInterface']) eh.start() self._log.debug ( "Sending test data" ) TCPServ.send("hello\r") time.sleep(0.5) self.assertEqual( len(self._router._pubs), 1 ) self.assertEqual( self._router._pubs[0][1].getType(), "lighting/update" ) self.assertEqual( self._router._pubs[0][1].getSource(), "light1/on" ) self.assertEqual( self._router._pubs[0][1].getPayload()['lightval'], "1") TCPServ.stop() time.sleep(0.5) TCPServ = TestTCPServer(4747) TCPServ.start() testEvent = makeEvent( 'internal/test/serial', 'serial/test/1', {} ) self._log.debug( testEvent ) self._log.debug( "\nTesting eventhandler..." ) eh.handleEvent( testEvent ) eh.handleEvent( testEvent ) eh.handleEvent( testEvent ) eh.handleEvent( testEvent ) TCPServ.send("hello\r") time.sleep(1) assert TCPServ.read() == "turn lights onsuccess!turn lights onsuccess!" self.assertEqual( len(self._router._pubs), 2 ) self.assertEqual( self._router._pubs[1][1].getType(), "lighting/update" ) self.assertEqual( self._router._pubs[1][1].getSource(), "light1/on" ) self.assertEqual( self._router._pubs[1][1].getPayload()['lightval'], "1") time.sleep(0.5)
def testConfigureLocalAndTCP(self): self._log.debug( "\n\n-------------------------------testconfigureLocalAndTCP-------------------------------" ) self._log.debug( "\nInitializng TCP server" ) TCPServ = TestTCPServer(4747) TCPServ.start() try: self._log.debug( "\nInitializng eventhandler" ) #load the test xml that contains both a local and TCP port testCfg = getDictFromXmlString(testConfigLocalAndTCP) self._log.debug( "testCfg %s" % testCfg ) #initialize the eventhandler and make sure it intialized eh = Serial(self._router) self.assertNotEqual( eh, None) #call the configure method to load the ports from the xml eh.configure(testCfg['eventInterface']) #start the eventhandler eh.start() #get the list of serial ports from our handler configuredPorts = eh.getSerialPorts() self._log.debug( "\n\nconfigured ports are: %s" % configuredPorts ) #check our list of serial ports contains the correct serial ports assert(configuredPorts.has_key("serial-1")) if configuredPorts.has_key("serial-1"): self.assertNotEqual( configuredPorts["serial-1"], None) assert(configuredPorts.has_key("serial-2")) if configuredPorts.has_key("serial-2"): self.assertNotEqual( configuredPorts["serial-2"], None) eh.stop() finally: TCPServ.stop()
def testConfigureTCP(self): self._log.debug( "\n\n-------------------------------testConfigureTCP-------------------------------" ) self._log.debug( "\nInitializng TCP server" ) TCPServ = TestTCPServer(4747) TCPServ.start() try: self._log.debug( "\nInitializing eventhandler" ) testCfg = getDictFromXmlString(testConfigTCP) self._log.debug( "testCfg %s" % testCfg ) eh = Serial(self._router) self.assertNotEqual( eh, None) eh.configure(testCfg['eventInterface']) eh.start() configuredPorts = eh.getSerialPorts() self._log.debug( "\n\nconfigured ports are: %s" % configuredPorts ) assert(configuredPorts.has_key("serial-1")) if configuredPorts.has_key("serial-1"): self.assertNotEqual( configuredPorts["serial-1"], None) eh.stop() finally: TCPServ.stop()