예제 #1
0
    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_odoo_server_url() or 'Not Configured',
            'six_terminal': self.get_six_terminal(),
            'network_status': network,
            'version': helpers.get_version(),
            }
예제 #2
0
 def send_alldevices(self):
     """
     This method send IoT Box and devices informations to Odoo database
     """
     server = helpers.get_odoo_server_url()
     if server:
         subject = helpers.read_file_first_line('odoo-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('Odoo server not set')
예제 #3
0
 def load_url(self):
     if helpers.get_odoo_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_odoo_server_url(), helpers.get_mac_address()))
             urls = json.loads(response.data.decode('utf8'))
             return self.update_url(urls[self.device_identifier])
         except json.decoder.JSONDecodeError:
             return self.update_url(response.data.decode('utf8'))
         except Exception:
             pass
     return self.update_url()
예제 #4
0
    def print_status(self):
        """Prints the status ticket of the IoTBox on the current printer."""
        wlan = ''
        ip = ''
        mac = ''
        homepage = ''
        pairing_code = ''

        ssid = helpers.get_ssid()
        wlan = '\nWireless network:\n%s\n\n' % ssid

        interfaces = ni.interfaces()
        ips = []
        for iface_id in interfaces:
            iface_obj = ni.ifaddresses(iface_id)
            ifconfigs = iface_obj.get(ni.AF_INET, [])
            for conf in ifconfigs:
                if conf.get('addr') and conf.get('addr'):
                    ips.append(conf.get('addr'))
        if len(ips) == 0:
            ip = '\nERROR: Could not connect to LAN\n\nPlease check that the IoTBox is correc-\ntly connected with a network cable,\n that the LAN is setup with DHCP, and\nthat network addresses are available'
        elif len(ips) == 1:
            ip = '\nIP Address:\n%s\n' % ips[0]
        else:
            ip = '\nIP Addresses:\n%s\n' % '\n'.join(ips)

        if len(ips) >= 1:
            ips_filtered = [i for i in ips if i != '127.0.0.1']
            main_ips = ips_filtered and ips_filtered[0] or '127.0.0.1'
            mac = '\nMAC Address:\n%s\n' % helpers.get_mac_address()
            homepage = '\nHomepage:\nhttp://%s:8069\n\n' % main_ips

        code = cm and cm.pairing_code
        if code:
            pairing_code = '\nPairing Code:\n%s\n' % code

        center = b'\x1b\x61\x01'
        title = b'\n\x1b\x21\x30\x1b\x4d\x01IoTBox Status\x1b\x4d\x00\x1b\x21\x00\n'
        cut = b'\x1d\x56\x41'

        self.print_raw(center + title + wlan.encode() + mac.encode() +
                       ip.encode() + homepage.encode() +
                       pairing_code.encode() + cut + b'\n')