|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import rpyc |
| 4 | +import json |
| 5 | +import click |
| 6 | + |
| 7 | +mesh = None |
| 8 | +MESH_CONNECTED = False |
| 9 | +serverhost = None |
| 10 | +serverport = None |
| 11 | + |
| 12 | +def connectserver(host: str, port: int) -> bool: |
| 13 | + """A function that attempts to connect to the server and returns |
| 14 | + whether the attempt was successful""" |
| 15 | + |
| 16 | + global mesh |
| 17 | + |
| 18 | + try: |
| 19 | + conn = rpyc.connect(host, port) |
| 20 | + mesh = conn.root |
| 21 | + return True |
| 22 | + except Exception: |
| 23 | + return False |
| 24 | + |
| 25 | +def loadserver(): |
| 26 | + """A function that loads the server values from the configuration file and connects to it. |
| 27 | + If the configuration file is not found, it is created. |
| 28 | + """ |
| 29 | + |
| 30 | + global MESH_CONNECTED |
| 31 | + |
| 32 | + if not os.path.isfile(configfile): |
| 33 | + with open(configfile, "w") as fp: |
| 34 | + serverdata = {"host": "localhost", "port": 18000} |
| 35 | + json.dump(serverdata, fp) |
| 36 | + |
| 37 | + else: |
| 38 | + with open(configfile) as fp: |
| 39 | + serverdata = json.load(fp) |
| 40 | + |
| 41 | + serverhost = serverdata['host'] |
| 42 | + serverport = serverdata['port'] |
| 43 | + MESH_CONNECTED = connectserver(serverhost, serverport) |
| 44 | + |
| 45 | +directory = os.path.dirname(os.path.realpath(__file__)) |
| 46 | +serverscript = os.path.join(directory, '..', 'server','server.py') |
| 47 | +configfile = os.path.join(directory, 'cliconfig.json') |
| 48 | + |
| 49 | +loadserver() |
| 50 | + |
| 51 | +def checkconnection(): |
| 52 | + """A function that checks if the connection is already established""" |
| 53 | + if not MESH_CONNECTED: |
| 54 | + bootmessage = ("""The FyrMesh server is not connected.Run 'fyrmesh boot' to start the server on this |
| 55 | + machine or set the host and port for a remote machine with 'fyrmesh setconfig' command""") |
| 56 | + click.echo(bootmessage) |
| 57 | + sys.exit() |
| 58 | + |
| 59 | +@click.group() |
| 60 | +def cli(): |
| 61 | + """FyrMesh CLI to interact with the mesh of sensor nodes""" |
| 62 | + pass |
| 63 | + |
| 64 | +@cli.command() |
| 65 | +def boot(): |
| 66 | + """boots up the FyrMesh server""" |
| 67 | + if MESH_CONNECTED: |
| 68 | + click.echo("The FyrMesh server is already running.") |
| 69 | + sys.exit() |
| 70 | + |
| 71 | + os.system(f"lxterminal -e 'python3 {serverscript}' &") |
| 72 | + |
| 73 | +@cli.command() |
| 74 | +def connect(): |
| 75 | + """connects the server to the mesh""" |
| 76 | + checkconnection() |
| 77 | + response = mesh.connectmesh() |
| 78 | + click.echo(response) |
| 79 | + |
| 80 | +@cli.command() |
| 81 | +def disconnect(): |
| 82 | + """disconnects the server from the mesh""" |
| 83 | + checkconnection() |
| 84 | + response = mesh.disconnectmesh() |
| 85 | + click.echo(response) |
| 86 | + |
| 87 | +@cli.command() |
| 88 | +def status(): |
| 89 | + """returns the current status of the mesh""" |
| 90 | + checkconnection() |
| 91 | + response = mesh.status() |
| 92 | + click.echo(response) |
| 93 | + |
| 94 | +@cli.command() |
| 95 | +def poll(): |
| 96 | + """polls the sensors on all nodes on the mesh""" |
| 97 | + checkconnection() |
| 98 | + mesh.pollmeshsensors() |
| 99 | + |
| 100 | +@cli.command() |
| 101 | +def reloadconfig(): |
| 102 | + """reloads the FyrMesh connection by realoading the configuration values""" |
| 103 | + loadserver() |
| 104 | + |
| 105 | +@cli.command() |
| 106 | +@click.option('--host', default="localhost", help='IP address of the host') |
| 107 | +@click.option('--port', default=18000, help='port of the host') |
| 108 | +def setconfig(host, port): |
| 109 | + """set the FyrMesh server host and port values to a configuration file""" |
| 110 | + with open(configfile, "w") as fp: |
| 111 | + serverdata = {"host": host, "port": port} |
| 112 | + json.dump(serverdata, fp) |
0 commit comments