def setUpClass(self, num_things=1): self.config = GatewayConfig() self.config.set_root('gateways', 'testgateway') self.gw = Gateway(CONFIG['gateway']['url'], self.config) self.gw.login(CONFIG['gateway']['user'], CONFIG['gateway']['password']) self.msgq = Queue() self.tws = {} self.new = {} warnings.filterwarnings("ignore", category=DeprecationWarning) self.loop = loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # Start num_things native webthing instances on localhost port_end = CONFIG['things']['port_start'] + num_things for port in range(CONFIG['things']['port_start'], port_end): tt = testThing(port, msgq=self.msgq) tws = testWebThingServer('native webthing on port %s' % port, tt, port) tws.start() self.tws[port] = (tt, tws) self.new[port] = False # Update the config for thing-url-adapter to speed up discovery self.gw.setAdapterConfig(CONFIG['things']['port_start'], num_things) # Wait for gateway websocket to indicate that all things are ready print('{} Waiting for {} webthings'.format(datetime.now(), len(list(self.tws.keys())))) skt = loop.run_until_complete(self.gw.newThingsWebsocket()) loop.run_until_complete(self.wait_for_new_things(self, skt)) print('{} All webthings added'.format(datetime.now()))
def run_sync(args): logger.debug("Starting Event Loop") loop = asyncio.get_event_loop() # Gateways are the link between the generic interfaces of Major Tom and the specifics of your # satellite(s) and groundstation(s). They can be designed to handle one or more satellites and # one or more groundstations. logger.debug("Setting up Gateway") gateway = Gateway() # Gateways use a websocket API, and we have a library to make the interface easier. # We instantiate the API, making sure to specify both sides of the connection: # - The Major Tom side, which requires a host and authentication # - The Gateway side, which specifies all the callbacks to be used when Major Tom communicates with this Gateway logger.debug("Setting up websocket connection") websocket_connection = GatewayAPI( host=args.majortomhost, gateway_token=args.gatewaytoken, basic_auth=args.basicauth, http=args.http, command_callback=gateway.command_callback, error_callback=gateway.error_callback, rate_limit_callback=gateway.rate_limit_callback, cancel_callback=gateway.cancel_callback, transit_callback=gateway.transit_callback, received_blob_callback=gateway.received_blob_callback, ) # It is useful to have a reference to the websocket api within your Gateway gateway.api = websocket_connection # Connect to MT asyncio.ensure_future(websocket_connection.connect_with_retries()) # To make it easier to interact with this Gateway, we are going to configure a bunch of commands for a satellite # called "Example FlatSat". Please see the associated json file to see the list of commands. logger.debug( "Setting up Example Flatsat satellite and associated commands") with open('satellite/example_commands.json', 'r') as f: command_defs = json.loads(f.read()) asyncio.ensure_future( websocket_connection.update_command_definitions( system="Example FlatSat", definitions=command_defs["definitions"])) try: loop.run_forever() except KeyboardInterrupt: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def cleanup_all_webthings(): config = GatewayConfig() config.set_root('gateways', 'testgateway') gw = Gateway(CONFIG['gateway']['url'], config) gw.login(CONFIG['gateway']['user'], CONFIG['gateway']['password']) things = gw.things() for thing in things: gw.deleteThing(thing) gw.clearAdapterConfig()
def main(): gw = Gateway() hist = Historian() gateway = threading.Thread(target=gw.up_server) historian_gateway = threading.Thread(target=hist.up_server) historian_server_app = threading.Thread(target=hist.up_server_app) gateway.start() historian_gateway.start() historian_server_app.start() gateway.join() historian_gateway.join() historian_server_app.join() return 0
class GatewayTest(unittest.TestCase): @asyncio.coroutine async def wait_for_new_things(self, skt): '''Wait for gateway to indicate that new webthings are ready''' while not all(self.new.values()): msg = await skt.read_message() msgdata = json.loads(msg) port = msgdata['id'].split('-')[-1] self.new[int(port)] = True return @classmethod def setUpClass(self, num_things=1): self.config = GatewayConfig() self.config.set_root('gateways', 'testgateway') self.gw = Gateway(CONFIG['gateway']['url'], self.config) self.gw.login(CONFIG['gateway']['user'], CONFIG['gateway']['password']) self.msgq = Queue() self.tws = {} self.new = {} warnings.filterwarnings("ignore", category=DeprecationWarning) self.loop = loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # Start num_things native webthing instances on localhost port_end = CONFIG['things']['port_start'] + num_things for port in range(CONFIG['things']['port_start'], port_end): cmdq = Queue() tt = testThing(port, msgq=self.msgq) tws = testWebThingServer('native webthing on port %s' % port, tt, port, msgq=cmdq) tws.start() self.tws[port] = (tt, tws, cmdq) self.new[port] = False # Update the config for thing-url-adapter to speed up discovery self.gw.setAdapterConfig(CONFIG['things']['port_start'], num_things) # Wait for gateway websocket to indicate that all things are ready print('{} Waiting for {} webthings'.format(datetime.now(), len(list(self.tws.keys())))) skt = loop.run_until_complete(self.gw.newThingsWebsocket()) loop.run_until_complete(self.wait_for_new_things(self, skt)) print('{} All webthings added'.format(datetime.now())) @classmethod def tearDownClass(self): self.gw.clearAdapterConfig() # Delete things from gateway, if they're in the list of things we added print('Deleting webthings') things = self.gw.things() for thing in things: port = int(thing.split('-')[-1]) if port in self.tws: if thing == self.tws[port][0].get_tid(): self.gw.deleteThing(thing) # Stop native webthing instances print('Stopping webthings') for port, (tt, tws, cmdq) in self.tws.items(): tws.kill() print('Waiting for webthing processes to exit') # Wait for native webthing instances to join for port, (tt, tws, cmdq) in self.tws.items(): tws.join()
def gateway_run(ctx, shard_id): from gateway.gateway import Gateway gateway = Gateway(ctx.obj['config'], shard_id) gateway.run()