bluepy_scratch_link: Add __main__.py

Add __main__.py to the bluepy-scratch-link package to invoke its feature
using the package name.

Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
This commit is contained in:
Shin'ichiro Kawasaki
2020-09-21 21:08:28 +09:00
parent 76bc9de261
commit abe63b54e5
2 changed files with 31 additions and 24 deletions

View File

@@ -0,0 +1,6 @@
#!/usr/bin/python
from scratch_link import main as _main
if __name__ == '__main__':
_main()

View File

@@ -651,16 +651,6 @@ class BLESession(Session):
self.delegate.restart_notification_event.set()
return self.status == self.DONE
# Prepare certificate of the WSS server
gencert.prep_cert()
# kick start WSS server
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
localhost_cer = gencert.cert_file_path
localhost_key = gencert.key_file_path
ssl_context.load_cert_chain(localhost_cer, localhost_key)
sessionTypes = { '/scratch/ble': BLESession, '/scratch/bt': BTSession }
async def ws_handler(websocket, path):
try:
logger.info(f"Start session for web socket path: {path}")
@@ -671,10 +661,6 @@ async def ws_handler(websocket, path):
logger.error(f"Failure in session for web socket path: {path}")
logger.error(e)
start_server = websockets.serve(
ws_handler, "device-manager.scratch.mit.edu", 20110, ssl=ssl_context
)
def stack_trace():
print("in stack_trace")
code = []
@@ -689,14 +675,29 @@ def stack_trace():
for line in code:
print(line)
while True:
try:
asyncio.get_event_loop().run_until_complete(start_server)
logger.info("Started scratch-link")
asyncio.get_event_loop().run_forever()
except KeyboardInterrupt as e:
stack_trace()
break
except Exception as e:
logger.info("Restarting scratch-link...")
def main():
# Prepare certificate of the WSS server
gencert.prep_cert()
# kick start WSS server
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
localhost_cer = gencert.cert_file_path
localhost_key = gencert.key_file_path
ssl_context.load_cert_chain(localhost_cer, localhost_key)
sessionTypes = { '/scratch/ble': BLESession, '/scratch/bt': BTSession }
start_server = websockets.serve(
ws_handler, "device-manager.scratch.mit.edu", 20110, ssl=ssl_context
)
while True:
try:
asyncio.get_event_loop().run_until_complete(start_server)
logger.info("Started scratch-link")
asyncio.get_event_loop().run_forever()
except KeyboardInterrupt as e:
stack_trace()
break
except Exception as e:
logger.info("Restarting scratch-link...")