class ConfigurationStore: def __init__(self): self.store = Store() def get(self): configuration = Configuration() if not self.store.has_configuration(): return configuration dictionary = self.store.get_configuration() return self._to_configuration(dictionary) def save(self, configuration): dictionary = self._to_dictionary(configuration) self.store.set_configuration(dictionary) @staticmethod def _to_dictionary(configuration): return { MAKER_ID: configuration.get_maker_id(), DEVICE_ID: configuration.get_device_id(), DEVICE_STATUS: configuration.get_device_status(), PUBLIC_KEY: configuration.get_public_key(), PRIVATE_KEY: configuration.get_private_key() } @staticmethod def _to_configuration(dictionary): configuration = Configuration() configuration.initialize( dictionary[MAKER_ID], dictionary[DEVICE_ID], DeviceStatus[dictionary[DEVICE_STATUS]], dictionary[PUBLIC_KEY], dictionary[PRIVATE_KEY], ) return configuration
configuration_service = ConfigurationService() store = Store() LOCATION = 'Server' # Function to validate the given IP Address def is_valid(ip): regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)''' return re.search(regex, ip) if not store.has_configuration(): if len(sys.argv) <= 1: exit('Please add your makerID to configure the SDK: "make server makerID=YOUR_MAKER_ID"') maker_id = sys.argv[1] # 1 -> First argument after server.py configuration_service.initialize_configuration(maker_id) # If OS is windows based, it doesn't support gunicorn so we run waitress if os.name == 'nt': subprocess.run(['waitress-serve', '--port=3001', 'bot_python_sdk.api:api']) else: cmd = subprocess.Popen(['hostname', '-I'], stdout=subprocess.PIPE) ip = cmd.communicate()[0].decode('ascii').split(' ')[0] if is_valid(ip): Logger.info(LOCATION, "Detected IP Address :" + ip) else: Logger.info(LOCATION, "Failed in detecting valid IP Address, using loop back address: 127.0.0.1")