Exemple #1
0
def perform_hw_diagnostics(ship=False):  # noqa: C901
    log.info('Running periodic hardware diagnostics')

    diagnostics = {}

    now = datetime.datetime.utcnow()
    diagnostics['last_updated'] = now.strftime("%H:%M UTC %d %b %Y")

    get_ethernet_addresses(diagnostics)
    get_environment_var(diagnostics)
    get_serial_number(diagnostics)
    detect_ecc(diagnostics)
    public_keys = get_public_keys_and_ignore_errors()

    diagnostics['LOR'] = lora_module_test()
    diagnostics['OK'] = public_keys['key']
    diagnostics['PK'] = public_keys['key']
    diagnostics['AN'] = public_keys['name']

    # Fetch data from miner container.
    try:
        diagnostics = fetch_miner_data(diagnostics)
    except MinerFailedFetchData as e:
        log.exception(e)
    except Exception as e:
        log.exception(e)

    # Get the blockchain height from the Helium API
    value = None
    try:
        value = get_helium_blockchain_height()
    except KeyError as e:
        logging.warning(e)
    except (ConnectTimeout, ReadTimeout):
        err_str = ("Request to Helium API timed out."
                   " Will fallback to block height of 1.")
        logging.exception(err_str)
    diagnostics['BCH'] = value

    # Check if the miner height
    # is within 500 blocks and if so say it's synced
    if diagnostics['BCH'] is None or diagnostics['MH'] is None:
        diagnostics['MS'] = False
    elif int(diagnostics['MH']) > (int(diagnostics['BCH']) - 500):
        diagnostics['MS'] = True
    else:
        diagnostics['MS'] = False

    # Calculate a percentage for block sync
    if diagnostics['BCH'] is None or diagnostics['MH'] is None:
        diagnostics['BSP'] = None
    else:
        diag_mh = int(diagnostics['MH'])
        diag_bch = int(diagnostics['BCH'])
        diagnostics['BSP'] = round(diag_mh / diag_bch * 100, 3)

    set_diagnostics_bt_lte(diagnostics)

    # Check if the region has been set
    try:
        with open("/var/pktfwd/region", 'r') as data:
            region = data.read()
            if len(region) > 3:
                log.info("Frequency: " + str(region))
                diagnostics['RE'] = str(region).rstrip('\n')
    except FileNotFoundError:
        # No region found, put a dummy region in
        diagnostics['RE'] = "UN123"

    # Check the basics if they're fine and set an overall value
    # Basics are: ECC valid, Mac addresses aren't FF, BT Is present,
    # and LoRa hasn't failed
    if (
            diagnostics["ECC"] is True
            and diagnostics["E0"] is not None
            and diagnostics["W0"] is not None
            and diagnostics["BT"] is True and diagnostics["LOR"] is True
    ):
        diagnostics["PF"] = True
    else:
        diagnostics["PF"] = False

    # Add variant variables into diagnostics
    # These are variables from the hardware definitions file
    try:
        variant_variables = variant_definitions[diagnostics['VA']]
        diagnostics.update(variant_variables)
    except KeyError:
        pass

    with open('diagnostic_data.json', 'w') as f:
        json.dump(diagnostics, f)

    upload_diagnostics(diagnostics, ship)

    log.info('Diagnostics complete')
 def test_available_file(self):
     with patch("builtins.open", mock_open(read_data=self.TEST_DATA)) as mf:
         fh_mock = mf.return_value.__enter__.return_value
         fh_mock.write.side_effect = FileNotFoundError
         get_serial_number(self.diag)
         self.assertRaises(FileNotFoundError)
 def test_permissions_error(self):
     with patch("builtins.open", mock_open(read_data=self.TEST_DATA)) as mf:
         fh_mock = mf.return_value.__enter__.return_value
         fh_mock.write.side_effect = PermissionError
         get_serial_number(self.diag)
         self.assertRaises(PermissionError)
 def test_strip_serialnumber(self):
     m = mock_open(read_data="%s\x00" % self.TEST_DATA)
     with patch('builtins.open', m):
         get_serial_number(self.diag)
         self.assertEqual(self.diag["serial_number"],
                          self.right_value["serial_number"])