Ejemplo n.º 1
0
def ipv6_subnet_calc(ipv6, prefix):
    """
    calculates the ipv6 subnet
    """
    ipv6_split = ipv6.split(':')
    active_hex = prefix // 16
    subnet = ipv6_split[active_hex]
    active_hex_bits = prefix % 16
    active_hex_index = active_hex_bits // 4
    active_bits = active_hex_bits % 4

    subnet_result = ""
    if subnet == "":
        subnet_result = "0000"
    else:
        for character_index, character in enumerate(subnet):
            if character_index > active_hex_index - 1:
                subnet_result += "0000"

            elif character_index <= active_hex_index - 1:
                hex_binary = utils.hex_to_binary(character)[2:]
                for bit in hex_binary:
                    if int(bit) & 1:
                        subnet_result += "1"
                    else:
                        subnet_result += "0"
            else:
                hex_binary = utils.hex_to_binary(character)[2:]
                for index, bit in enumerate(hex_binary):
                    if index < active_bits:
                        subnet_result += "0"
                    else:
                        if int(bit) & 1:
                            subnet_result += "1"
                        else:
                            subnet_result += "0"
    result = ""
    for index, hex_value in enumerate(ipv6_split):
        if index == active_hex:
            result += str(hex(int(subnet_result, 2)))[2:]
        elif index < active_hex:
            result += hex_value
        else:
            result += "0000"
        if index < len(ipv6_split) - 1:
            result += ":"

    return result
Ejemplo n.º 2
0
 def hex_key_press(self, instance):
     """
     On hex keyboard press, a thread verifies if RAM is ready to be written.
     Uses a shared queue to enqueue pressed keys that are to be written to RAM.
     :param instance: obj
     """
     thread = Thread(target=self.is_ram_ready)
     if not self.queue.full():
         self.queue.put(hex_to_binary(instance.text))
         thread.start()
Ejemplo n.º 3
0
 def testHexToBinary(self):
     self.assertEquals("0b0000", utils.hex_to_binary("0"))
     self.assertEquals("0b0001", utils.hex_to_binary("1"))
     self.assertEquals("0b0010", utils.hex_to_binary("2"))
     self.assertEquals("0b0011", utils.hex_to_binary("3"))
     self.assertEquals("0b0100", utils.hex_to_binary("4"))
     self.assertEquals("0b0101", utils.hex_to_binary("5"))
     self.assertEquals("0b0110", utils.hex_to_binary("6"))
     self.assertEquals("0b0111", utils.hex_to_binary("7"))
     self.assertEquals("0b1000", utils.hex_to_binary("8"))
     self.assertEquals("0b1001", utils.hex_to_binary("9"))
     self.assertEquals("0b1010", utils.hex_to_binary("A"))
     self.assertEquals("0b1011", utils.hex_to_binary("B"))
     self.assertEquals("0b1100", utils.hex_to_binary("C"))
     self.assertEquals("0b1101", utils.hex_to_binary("D"))
     self.assertEquals("0b1110", utils.hex_to_binary("E"))
     self.assertEquals("0b1111", utils.hex_to_binary("F"))
Ejemplo n.º 4
0
 def parse(self, data, mac, name, manuf):
     action = {}
     action['present'] = 1
     unit = 'kg'
     stabilized = 'unstabilized'
     hasimpedance = False
     status = utils.hex_to_binary(data[4:6]).zfill(8)[::-1]
     logging.debug('Status is ' + status + ' from ' + data[6:8])
     if (status[0:1] == '1'):
         unit = 'lbs'
     if (status[4:5] == '1'):
         unit = 'jin'
     if (status[5:6] == '1' and status[7:8] == '0'):
         stabilized = 'stabilized'
     if (status[1:2] == '1'):
         hasimpedance = True
         logging.debug('Data has Impedance')
     logging.debug('Unit is ' + unit + ' and weight is ' + stabilized)
     if (stabilized == 'stabilized'):
         action['year'] = int((data[12:14] + data[10:12]), 16)
         action['month'] = int(data[14:16], 16)
         action['day'] = int(data[16:18], 16)
         action['hour'] = int(data[18:20], 16)
         action['minute'] = int(data[20:22], 16)
         action['second'] = int(data[22:24], 16)
         action['poids'] = round(
             int((data[8:10] + data[6:8]), 16) * 0.01 / 2, 2)
         if (mac.upper() in globals.KNOWN_DEVICES):
             if ('specificconfiguration'
                     in globals.KNOWN_DEVICES[mac.upper()]
                     and len(globals.KNOWN_DEVICES[mac.upper()]
                             ['specificconfiguration']) > 0):
                 logging.debug('Known Users ' + str(globals.KNOWN_DEVICES[
                     mac.upper()]['specificconfiguration']))
                 delta = 999
                 target = ''
                 for user in globals.KNOWN_DEVICES[
                         mac.upper()]['specificconfiguration']:
                     oldweight = globals.KNOWN_DEVICES[mac.upper(
                     )]['specificconfiguration'][user]['weight']
                     currentdelta = abs(
                         float(oldweight) - float(action['poids']))
                     if (currentdelta < delta):
                         delta = currentdelta
                         target = user
                 if target != '':
                     logging.debug('Found target : ' +
                                   str(globals.KNOWN_DEVICES[mac.upper()]
                                       ['specificconfiguration'][target]))
                     action['target'] = target
                     action['poids' + target] = action['poids']
                     try:
                         birthdate = globals.KNOWN_DEVICES[mac.upper(
                         )]['specificconfiguration'][target]['age']
                         born = datetime.strptime(birthdate, '%d-%m-%Y')
                         today = date.today()
                         age = today.year - born.year - (
                             (today.month, today.day) <
                             (born.month, born.day))
                         logging.debug('Birthdate is ' + birthdate +
                                       ' age is ' + str(age))
                         lib = bodyMetrics(
                             action['poids'],
                             float(globals.KNOWN_DEVICES[mac.upper(
                             )]['specificconfiguration'][target]['height']),
                             age, globals.KNOWN_DEVICES[mac.upper()]
                             ['specificconfiguration'][target]['sex'], 0)
                         action['imc' + target] = round(
                             float(lib.getBMI()), 2)
                         action['bmr' + target] = round(
                             float(lib.getBMR()), 2)
                         action['ideal' + target] = round(
                             float(lib.getIdealWeight()), 2)
                         action['imclabel' + target] = lib.getImcLabel()
                         action['imc'] = round(float(lib.getBMI()), 2)
                         action['bmr'] = round(float(lib.getBMR()), 2)
                         action['imclabel'] = lib.getImcLabel()
                         action['ideal'] = round(
                             float(lib.getIdealWeight()), 2)
                     except Exception as e:
                         logging.error(str(e))
     else:
         logging.debug('MISCALE------Miscale this is a dummy measure')
     return action
Ejemplo n.º 5
0
 def parse(self, data, mac, name, manuf):
     action = {}
     unit = 'kg'
     stabilized = 'unstabilized'
     hasimpedance = False
     action['present'] = 1
     status = utils.hex_to_binary(data[6:8]).zfill(8)[::-1]
     logging.debug('Status is ' + status + ' from ' + data[6:8])
     logging.debug('FAT ' + data[24:26] + data[22:24])
     if (status[0:1] == '1'):
         unit = 'lbs'
     if (status[4:5] == '1'):
         unit = 'jin'
     if (status[5:6] == '1' and status[7:8] == '0'):
         stabilized = 'stabilized'
     if (status[1:2] == '1'):
         hasimpedance = True
         logging.debug('Data has Impedance')
     logging.debug('Unit is ' + unit + ' and weight is ' + stabilized)
     if (stabilized == 'stabilized'):
         action['year'] = int((data[10:12] + data[8:10]), 16)
         action['month'] = int(data[12:14], 16)
         action['day'] = int(data[14:16], 16)
         action['hour'] = int(data[16:18], 16)
         action['minute'] = int(data[18:20], 16)
         action['second'] = int(data[20:22], 16)
         action['poids'] = round(
             int((data[28:30] + data[26:28]), 16) * 0.01 / 2, 2)
         target = ''
         if (mac.upper() in globals.KNOWN_DEVICES):
             if ('specificconfiguration'
                     in globals.KNOWN_DEVICES[mac.upper()]
                     and len(globals.KNOWN_DEVICES[mac.upper()]
                             ['specificconfiguration']) > 0):
                 logging.debug('Known Users ' + str(globals.KNOWN_DEVICES[
                     mac.upper()]['specificconfiguration']))
                 delta = 999
                 for user in globals.KNOWN_DEVICES[
                         mac.upper()]['specificconfiguration']:
                     oldweight = globals.KNOWN_DEVICES[mac.upper(
                     )]['specificconfiguration'][user]['weight']
                     currentdelta = abs(
                         float(oldweight) - float(action['poids']))
                     if (currentdelta < delta):
                         delta = currentdelta
                         target = user
         if target != '':
             logging.debug('Found target : ' + str(globals.KNOWN_DEVICES[
                 mac.upper()]['specificconfiguration'][target]))
             action['poids' + target] = action['poids']
             action['target'] = target
         if hasimpedance:
             action['impedance'] = round(
                 int((data[24:26] + data[22:24]), 16), 2)
             action['impedance' + target] = action['impedance']
             if target != '':
                 try:
                     birthdate = globals.KNOWN_DEVICES[mac.upper(
                     )]['specificconfiguration'][target]['age']
                     born = datetime.strptime(birthdate, '%d-%m-%Y')
                     today = date.today()
                     age = today.year - born.year - (
                         (today.month, today.day) < (born.month, born.day))
                     logging.debug('Birthdate is ' + birthdate +
                                   ' age is ' + str(age))
                     lib = bodyMetrics(
                         action['poids'],
                         float(globals.KNOWN_DEVICES[mac.upper()]
                               ['specificconfiguration'][target]['height']),
                         age, globals.KNOWN_DEVICES[mac.upper()]
                         ['specificconfiguration'][target]['sex'],
                         action['impedance'])
                     action['lbm' + target] = round(
                         float(lib.getLBMCoefficient()), 2)
                     action['fat' + target] = round(
                         float(lib.getFatPercentage()), 2)
                     action['water' + target] = round(
                         float(lib.getWaterPercentage()), 2)
                     action['bones' + target] = round(
                         float(lib.getBoneMass()), 2)
                     action['muscle' + target] = round(
                         float(lib.getMuscleMass()), 2)
                     action['visceral' + target] = round(
                         float(lib.getVisceralFat()), 2)
                     action['imc' + target] = round(float(lib.getBMI()), 2)
                     action['bmr' + target] = round(float(lib.getBMR()), 2)
                     action['ideal' + target] = round(
                         float(lib.getIdealWeight()), 2)
                     action['fatmassideal' + target] = round(
                         float(lib.getFatMassToIdeal()), 2)
                     action['protein' + target] = round(
                         float(lib.getProteinPercentage()), 2)
                     action['body' + target] = lib.getBodyType()
                     action['imclabel' + target] = lib.getImcLabel()
                     action['lbm'] = round(float(lib.getLBMCoefficient()),
                                           2)
                     action['fat'] = round(float(lib.getFatPercentage()), 2)
                     action['water'] = round(
                         float(lib.getWaterPercentage()), 2)
                     action['bones'] = round(float(lib.getBoneMass()), 2)
                     action['muscle'] = round(float(lib.getMuscleMass()), 2)
                     action['visceral'] = round(float(lib.getVisceralFat()),
                                                2)
                     action['imc'] = round(float(lib.getBMI()), 2)
                     action['bmr'] = round(float(lib.getBMR()), 2)
                     action['ideal'] = round(float(lib.getIdealWeight()), 2)
                     action['fatlabel'] = round(float(lib.getIdealWeight()),
                                                2)
                     action['fatmassideal'] = round(
                         float(lib.getFatMassToIdeal()), 2)
                     action['protein'] = round(
                         float(lib.getProteinPercentage()), 2)
                     action['body'] = lib.getBodyType()
                     action['imclabel'] = lib.getImcLabel()
                 except Exception as e:
                     logging.error(str(e))
     else:
         logging.debug('Weight unstabilized skipping')
     return action