Exemplo n.º 1
0
def devices():
    try:  # loads device json into a dictionary
        device_data = ju.open_local_devices()

    except:
        #TODO move into main and check for devices
        os.system("python3 joincliSetup.py")
        device_data = ju.open_local_devices()

    return device_data
Exemplo n.º 2
0
import http.server
import socketserver
import json
import logging
from joincliHandler import handleMessage
import joincliUtils as ju

logger = logging.getLogger(__name__)
logging.basicConfig()
devices = ju.open_local_devices()

Handler = http.server.SimpleHTTPRequestHandler
PORT = 1820


class webServer(Handler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/json')
        self.end_headers()

    def do_POST(self):
        self._set_headers()
        data = self.rfile.read()
        data_str = ju.decode_UTF8(data).replace("'", '"')  #Get data from POST
        data = json.loads(
            json.loads(data_str)['json'])['push']  #dict this bitch
        s = json.dumps(data, sort_keys=True, indent=4)
        print(s)  #print shit to be done
        handleMessage(data)
Exemplo n.º 3
0
    logger.info("Sending request...")

    try:
        response = requests.post(url, json.dumps(reg), headers=headers)
        response = response.json()
    except requests.exceptions.HTTPError:
        logger.warning(f"Error message: {response['errorMessage']}")
        raise

    # TODO handle device already registered
    update_devices(device)


if __name__ == "__main__":

    devices = joincliUtils.open_local_devices()

    if devices is None:
        if arguments.apikey is None:
            logger.info("Apikey is not given, and no Environ value detected.")
            logger.info("Use -ak to set your apikey!")
            sys.exit(1)
        else:
            setup_devices(devices)
    elif arguments.update:
        update_devices(devices)
    elif arguments.register:
        register_new_device(devices)
    else:
        logger.info("No arguments!")
Exemplo n.º 4
0
    """

    :param handler_class:
    :param port:
    """
    try:
        logging.info("Listening on port %d for clients..." % port)
        server_address = ('', port)
        httpd = socketserver.TCPServer(server_address, handler_class)
        httpd.serve_forever()
    except KeyboardInterrupt:
        handleMessage(False)
        httpd.server_close()
        logger.info("Server terminated.")
    except Exception as e:
        handleMessage(False)
        logger.error(str(e), exc_info=True)
        exit(1)


if __name__ == "__main__":

    devices: json = ju.json.loads(ju.open_local_devices().read())

    if os.path.isfile(devices):
        main()
    else:
        print("Devices not found!")
        print("Setup your eviroment first")
        sys.exit(1)