def testRepr(self): app = sgframework.App('testapp', 'localhost') app.register_incoming_data('testservice', 'teststate', on_testservice_state_data) output = repr(app) self.assertEqual( "SG App: 'testapp', connecting to host 'localhost', port 1883. Has 1 input signals registered.", output)
def testLoop(self): with open(self.OUTPUT_FILE_SUBSCRIBER, 'w') as subscriber_outputfile: subcriber = subprocess.Popen(['mosquitto_sub', '-v', '-t', '+/#'], stdout=subscriber_outputfile, stderr=subprocess.STDOUT) app = sgframework.App('testapp', 'localhost') app.register_incoming_data('testservice', 'teststate', on_testservice_state_data) app.register_incoming_availability( 'commandavailable', 'testservice', 'teststate', on_testservice_command_availability) app.start() for i in range(5): app.loop() time.sleep(0.1) app.send_command('testservice', 'teststate', 8) for i in range(5): app.loop() time.sleep(0.1) app.send_command('testservice', 'teststate', 4) for i in range(5): app.loop() time.sleep(0.1) # Provoke availability callback pub1 = subprocess.Popen([ 'mosquitto_pub', '-t', 'commandavailable/testservice/teststate', '-m', 'True' ]) for i in range(5): app.loop() time.sleep(0.1) pub1.terminate() # Provoke data callback pub1 = subprocess.Popen([ 'mosquitto_pub', '-t', 'data/testservice/teststate', '-m', '47' ]) for i in range(5): app.loop() time.sleep(0.1) pub1.terminate() # Terminate, and flush files app.stop() subcriber.kill() time.sleep(0.2) subscriber_outputfile.flush() os.fsync(subscriber_outputfile.fileno()) # Verify that the app has emitted the MQTT commands with open(self.OUTPUT_FILE_SUBSCRIBER, 'r') as subscriber_outputfile: text = ' '.join(subscriber_outputfile.readlines()) self.assertIn("command/testservice/teststate 8", text) self.assertIn("command/testservice/teststate 4", text)
def init_taxisignapp(): ## Parse command line and set output verbosity ## epilog = DESCRIPTIVE_TEXT_TEMPLATE.format(sgframework.Resource.CA_CERTS, sgframework.Resource.CERTFILE, sgframework.Resource.KEYFILE) commandlineparser = argparse.ArgumentParser(epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter) commandlineparser.add_argument('-v', action='count', default=0, help="Increase verbosity level. Can be repeated.") commandlineparser.add_argument('-host', default='localhost', help="Broker host name. Defaults to %(default)s.") commandlineparser.add_argument('-port', default=1883, help="Broker port number. Defaults to %(default)s.") commandlineparser.add_argument('-cert', help="Directory for certificate files. Defaults to not using certificates.") commandlineparser.add_argument('-mode', choices=['commandline', 'graphical'], default='commandline', help="Type of use interface. Depends on graphical display. " + "Defaults to '%(default)s'.") commandline = commandlineparser.parse_args() if commandline.v == 1: loglevel = logging.INFO elif commandline.v >= 2: loglevel = logging.DEBUG else: loglevel = logging.WARNING logging.basicConfig(level=loglevel) ## Initialize Secure Gateway app framework ## app = sgframework.App(APPNAME, commandline.host, commandline.port, commandline.cert) app.timeout = TIMEOUT if commandline.mode == 'graphical': displ = GraphicalAppDisplay(app) else: displ = CommandlineAppDisplay(app) displ.loop() app.userdata = displ app.register_incoming_availability(app.PREFIX_RESOURCEAVAILABLE, TAXISIGN_SERVICE_NAME, "", on_resource_presence) app.register_incoming_data(TAXISIGN_SERVICE_NAME, TAXISIGN_STATE_SIGNAL, on_taxisign_state_data) app.on_broker_connectionstatus_info = on_broker_connectionstatus_info app.start() return app
import time import sgframework def on_taxisign_state_data(app, messagetype, servicename, signalname, payload): if payload.strip() == 'True': print("The taxi sign is now on.", flush=True) else: print("The taxi sign is now off.", flush=True) app = sgframework.App('taxiapp', 'localhost') app.register_incoming_data('taxisignservice', 'state', on_taxisign_state_data) app.start() # Not using threaded networking starttime = time.time() command_sent = False while True: app.loop() if time.time() - starttime > 4 and not command_sent: print("Turning on the taxi sign from script...", flush=True) app.send_command('taxisignservice', 'state', 'True') command_sent = True
def testConstructor(self): app = sgframework.App('testapp', 'localhost') app.start() time.sleep(0.1) app.stop()