def connect_to_wifi(self, essid, password, persistent=False): if persistent: persistent = "1" else: persistent = "" subprocess.check_call([ get_resource_path('point_of_sale', 'tools/posbox/configuration/connect_to_wifi.sh'), essid, password, persistent ]) server = helpers.get_harpiya_server_url() res_payload = { 'message': 'Connecting to ' + essid, } if server: res_payload['server'] = { 'url': server, 'message': 'Redirect to Harpiya Server' } else: res_payload['server'] = { 'url': 'http://' + helpers.get_ip() + ':8069', 'message': 'Redirect to IoT Box' } return json.dumps(res_payload)
def connect_box(self, token): """ This route is called when we want that a IoT Box will be connected to a Harpiya DB token is a base 64 encoded string and have 2 argument separate by | 1 - url of harpiya DB 2 - token. This token will be compared to the token of Harpiya. He have 1 hour lifetime """ server = helpers.get_harpiya_server_url() image = get_resource_path('hw_drivers', 'static/img', 'False.jpg') if not server: credential = b64decode(token).decode('utf-8').split('|') url = credential[0] token = credential[1] if len(credential) > 2: # IoT Box send token with db_uuid and enterprise_code only since V13 db_uuid = credential[2] enterprise_code = credential[3] helpers.add_credential(db_uuid, enterprise_code) try: subprocess.check_call([ get_resource_path( 'point_of_sale', 'tools/posbox/configuration/connect_to_server.sh'), url, '', token, 'noreboot' ]) m.send_alldevices() image = get_resource_path('hw_drivers', 'static/img', 'True.jpg') helpers.harpiya_restart(3) except subprocess.CalledProcessError as e: _logger.error('A error encountered : %s ' % e.output) if os.path.isfile(image): with open(image, 'rb') as f: return f.read()
def run(self): if not helpers.get_harpiya_server_url(): end_time = datetime.now() + timedelta(minutes=5) while (datetime.now() < end_time): self._connect_box() time.sleep(10) self.pairing_code = False self.pairing_uuid = False self._refresh_displays()
def load_url(self): url = None if helpers.get_harpiya_server_url(): # disable certifiacte verification urllib3.disable_warnings() http = urllib3.PoolManager(cert_reqs='CERT_NONE') try: response = http.request( 'GET', "%s/iot/box/%s/screen_url" % (helpers.get_harpiya_server_url(), helpers.get_mac_address())) if response.status == 200: data = json.loads(response.data.decode('utf8')) url = data[self.device_identifier] except json.decoder.JSONDecodeError: url = response.data.decode('utf8') except Exception: pass return self.update_url(url)
def server(self): return server_config_template.render({ 'title': 'IoT -> Harpiya server configuration', 'breadcrumb': 'Configure Harpiya Server', 'hostname': subprocess.check_output('hostname').decode('utf-8').strip('\n'), 'server_status': helpers.get_harpiya_server_url() or 'Not configured yet', 'loading_message': 'Configure Domain Server' })
def send_alldevices(self): """ This method send IoT Box and devices informations to Harpiya database """ server = helpers.get_harpiya_server_url() if server: subject = helpers.read_file_first_line('harpiya-subject.conf') if subject: domain = helpers.get_ip().replace('.', '-') + subject.strip('*') else: domain = helpers.get_ip() iot_box = { 'name': socket.gethostname(), 'identifier': helpers.get_mac_address(), 'ip': domain, 'token': helpers.get_token(), 'version': helpers.get_version() } devices_list = {} for device in iot_devices: identifier = iot_devices[device].device_identifier devices_list[identifier] = { 'name': iot_devices[device].device_name, 'type': iot_devices[device].device_type, 'manufacturer': iot_devices[device].device_manufacturer, 'connection': iot_devices[device].device_connection, } data = { 'params': { 'iot_box': iot_box, 'devices': devices_list, } } # disable certifiacte verification urllib3.disable_warnings() http = urllib3.PoolManager(cert_reqs='CERT_NONE') try: http.request('POST', server + "/iot/setup", body=json.dumps(data).encode('utf8'), headers={ 'Content-type': 'application/json', 'Accept': 'text/plain' }) except Exception as e: _logger.error('Could not reach configured server') _logger.error('A error encountered : %s ' % e) else: _logger.warning('Harpiya server not set')
def step_by_step_configure_page(self): return configure_wizard_template.render({ 'title': 'Configure IoT Box', 'breadcrumb': 'Configure IoT Box', 'loading_message': 'Configuring your IoT Box', 'ssid': helpers.get_wifi_essid(), 'server': helpers.get_harpiya_server_url() or '', 'hostname': subprocess.check_output('hostname').decode('utf-8').strip('\n'), })
def list_drivers(self): drivers_list = [] for driver in os.listdir(get_resource_path('hw_drivers', 'drivers')): if driver != '__pycache__': drivers_list.append(driver) return driver_list_template.render({ 'title': "Harpiya's IoT Box - Drivers list", 'breadcrumb': 'Drivers list', 'drivers_list': drivers_list, 'server': helpers.get_harpiya_server_url() })
def send_layouts_list(cls): server = helpers.get_harpiya_server_url() if server: urllib3.disable_warnings() pm = urllib3.PoolManager(cert_reqs='CERT_NONE') server = server + '/iot/keyboard_layouts' try: pm.request('POST', server, fields={ 'available_layouts': json.dumps(cls.available_layouts) }) except Exception as e: _logger.error('Could not reach configured server') _logger.error('A error encountered : %s ' % e)
def connect_to_server(self, token, iotname): if token: credential = token.split('|') url = credential[0] token = credential[1] if len(credential) > 2: # IoT Box send token with db_uuid and enterprise_code only since V13 db_uuid = credential[2] enterprise_code = credential[3] helpers.add_credential(db_uuid, enterprise_code) else: url = helpers.get_harpiya_server_url() token = helpers.get_token() reboot = 'reboot' subprocess.check_call([ get_resource_path( 'point_of_sale', 'tools/posbox/configuration/connect_to_server.sh'), url, iotname, token, reboot ]) return 'http://' + helpers.get_ip() + ':8069'
def get_homepage_data(self): hostname = str(socket.gethostname()) ssid = helpers.get_ssid() wired = subprocess.check_output( ['cat', '/sys/class/net/eth0/operstate']).decode('utf-8').strip('\n') if wired == 'up': network = 'Ethernet' elif ssid: if helpers.access_point(): network = 'Wifi access point' else: network = 'Wifi : ' + ssid else: network = 'Not Connected' iot_device = [] for device in iot_devices: iot_device.append({ 'name': iot_devices[device].device_name + ' : ' + str(iot_devices[device].data['value']), 'type': iot_devices[device].device_type.replace('_', ' '), 'message': iot_devices[device].device_identifier + iot_devices[device].get_message() }) return { 'hostname': hostname, 'ip': helpers.get_ip(), 'mac': helpers.get_mac_address(), 'iot_device_status': iot_device, 'server_status': helpers.get_harpiya_server_url() or 'Not Configured', 'six_terminal': self.get_six_terminal(), 'network_status': network, 'version': helpers.get_version(), }