예제 #1
0
def generate():
    modbus_maps_expanded = ljmmm.get_device_modbus_maps(src=SRC_FILE,
                                                        expand_names=True,
                                                        expand_alt_names=True)

    constants_contents = json.loads(ljmmm.read_file(src=SRC_FILE))

    with open(OUTPUT_FILE, 'w') as file:
        init(file, constants_contents['header']['version'])

        printed = []
        for device in modbus_maps_expanded:
            for reg in modbus_maps_expanded[device]:

                # Remove duplication by name. By address would omit altnames
                name = reg['name']
                if (not name in printed):
                    printed.append(name)
                    output_reg(file, reg)
                # else:
                #     print "Duplicate: %s" % reg["name"]

        finish(file)

    sanity_test()
예제 #2
0
    def test_get_device_modbus_maps(self):
        EXPECTED_MAPS = {
            'T7': [
                (
                    {
                        'name': 'LED_COMM',
                        'tags': ['DIO'],
                        'readwrite': 'RW',
                        'devices': [
                            {'device': 'T7', 'fwmin': 1.7777},
                            {'device': 'T4', 'fwmin': 1.4444}
                        ],
                        'default': 0,
                        'address': 2990,
                        'type': 'UINT16',
                        'constants': [
                            {'name': 'Off', 'value': 0},
                            {'name': 'On', 'value': 1}
                        ],
                        'description': 'Sets the state of the COMM LED when the LEDs are set to manual, see the POWER_LED register.'
                    },
                    {
                        'streamable': False,
                        'description': 'Sets the state of the COMM LED when the LEDs are set to manual, see the POWER_LED register.',
                        'fwmin': 1.7777,
                        'tags': ['DIO'],
                        'default': 0,
                        'deviceDescription': '',
                        'altnames': [],
                        'write': True,
                        'type_index': '0',
                        'read': True,
                        'constants': [
                            {'name': 'Off', 'value': 0},
                            {'name': 'On', 'value': 1}
                        ],
                        'address': 2990,
                        'type': 'UINT16',
                        'isBuffer': False,
                        'name': 'LED_COMM',
                        'usesRAM': False,
                    }
                )
            ],
            'T4': [
                (
                    {
                        'name': 'LED_COMM',
                        'tags': ['DIO'],
                        'readwrite': 'RW',
                        'devices': [
                            {'device': 'T7', 'fwmin': 1.7777},
                            {'device': 'T4', 'fwmin': 1.4444}
                        ],
                        'default': 0,
                        'address': 2990,
                        'type': 'UINT16',
                        'constants': [
                            {'name': 'Off', 'value': 0},
                            {'name': 'On', 'value': 1}
                        ],
                        'description': 'Sets the state of the COMM LED when the LEDs are set to manual, see the POWER_LED register.'
                    },
                    {
                        'streamable': False,
                        'description': 'Sets the state of the COMM LED when the LEDs are set to manual, see the POWER_LED register.',
                        'fwmin': 1.4444,
                        'tags': ['DIO'],
                        'default': 0,
                        'deviceDescription': '',
                        'altnames': [],
                        'write': True,
                        'type_index': '0',
                        'read': True,
                        'constants': [
                            {'name': 'Off', 'value': 0},
                            {'name': 'On', 'value': 1}
                        ],
                        'address': 2990,
                        'type': 'UINT16',
                        'isBuffer': False,
                        'name': 'LED_COMM',
                        'usesRAM': False,
                    }
                )
            ]
        }

        maps = ljmmm.get_device_modbus_maps(
            src=os.path.join(os.path.split(os.path.realpath(__file__))[0], "ljmmm_test.json"),
            expand_names=True,
            inc_orig=True
        )
        self.assertEqual(EXPECTED_MAPS, maps)
예제 #3
0
def validate(json_file_path):
    """Validates json_file_path as ljm constants JSON. Exits with non-zero on error."""
    print 'Checking JSON file...'
    try:
        json_map = ljmmm.get_device_modbus_maps(json_file_path,
                                                expand_names=True,
                                                inc_orig=True)

    except Exception as e:
        print '[ERROR] JSON file registers could not be parsed. (' + str(
            e) + ')'
        exit(1)

    try:
        errors = ljmmm.get_errors(json_file_path)
    except Exception as e:
        print '[ERROR] JSON file errors could not be parsed. (' + str(e) + ')'
        exit(1)

    err_msgs = []

    print 'Checking register map duplicates and streamable validity...'
    # Track all register names so we do not throw the same error twice for a
    # register (if the register is used with multiple devices)
    all_names = []
    for device in json_map:
        previous_names = []
        previous_addresses = {}
        device_registers = json_map[device]

        for register in device_registers:
            unresolved = register[0]
            resolved = register[1]
            if \
                unresolved.has_key('streamable') and \
                unresolved['streamable'] and \
                not resolved['read']:
                err_msgs.append("Register is streamable but not readable: %s" %
                                (resolved['name']))

            reg_name = resolved['name']

            if reg_name in previous_names:
                err_msgs.append('Duplicate entries for %s found.' % reg_name)
            previous_names.append(reg_name)

            reg_addr = resolved['address']
            if previous_addresses.has_key(reg_addr) and previous_addresses[
                    reg_addr]['name'] != unresolved['name']:
                err_msgs.append(
                    'Duplicate address for %s found:\n'
                    '  %s\n'
                    '  %s' %
                    (reg_addr, str(previous_addresses[reg_addr]['name']),
                     str(resolved['name'])))
            previous_addresses[reg_addr] = unresolved

            if resolved.has_key('description') and \
                len(resolved['description']) == 0 and \
                reg_name not in all_names:
                err_msgs.append('No register description for: %s\n' %
                                str(reg_name))
            elif not resolved.has_key('description') and \
                reg_name not in all_names:
                err_msgs.append('No register description field for: %s\n' %
                                str(reg_name))
            all_names.append(reg_name)

    print 'Checking error duplicates...'
    dup_ierrs = []
    dup_jerrs = []
    for i_it in range(0, len(errors)):
        for j_it in range(i_it + 1, len(errors)):
            ierr = errors[i_it]
            jerr = errors[j_it]
            if ierr['error'] == jerr['error'] and ierr[
                    'error'] not in dup_ierrs:
                dup_ierrs.append(ierr)
                dup_jerrs.append(jerr)

    if dup_ierrs:
        print 'Duplicate errors:'
        for err in range(0, len(dup_ierrs)):
            print '  ' + str(dup_ierrs[err]['error'])
        err_msgs.append('Duplication errors were found (see above)')

    for err in err_msgs:
        print err

    if len(err_msgs) != 0:
        exit(1)

    print json_file_path + ' seems fine.'