Exemplo n.º 1
0
    def protected_pubsub_fn(topic,
                            capabilities,
                            topic_regex=None,
                            add_capabilities=False):
        """Returns dict that holds configuration for a protected-pubsub test."""
        agent1, agent2, topic, msgs = build_two_agents_pubsub_agents(topic)
        topic_to_protect = topic_regex if topic_regex else topic
        topic_dict = {
            'write-protect': [{
                'topic': topic_to_protect,
                'capabilities': capabilities
            }]
        }

        topic_file = os.path.join(volttron_instance.volttron_home,
                                  'protected_topics.json')
        with open(topic_file, 'w') as f:
            jsonapi.dump(topic_dict, f)
            gevent.sleep(.5)

        if add_capabilities:
            volttron_instance.add_capabilities(agent2.publickey, capabilities)
            gevent.sleep(.2)

        return {
            'agent1': agent2,
            'agent2': agent2,
            'topic': topic,
            'instance': volttron_instance,
            'messages': msgs,
            'capabilities': capabilities
        }
Exemplo n.º 2
0
    def update_ca_db(self, cert, ca_name, serial):
        """
        Update the CA db with details of the file that the ca signed.
        :param cert: cert that was signed by ca_name
        :param ca_name: name of the ca that signed the cert
        """
        db_file = self.ca_db_file(ca_name)
        ca_db = {}
        dn = "C={}/ST={}/L={}/O={}/OU={}/CN={}".format(
            _get_cert_attribute_value(cert, NameOID.COUNTRY_NAME),
            _get_cert_attribute_value(cert, NameOID.STATE_OR_PROVINCE_NAME),
            _get_cert_attribute_value(cert, NameOID.LOCALITY_NAME),
            _get_cert_attribute_value(cert, NameOID.ORGANIZATION_NAME),
            _get_cert_attribute_value(cert, NameOID.ORGANIZATIONAL_UNIT_NAME),
            _get_cert_attribute_value(cert, NameOID.COMMON_NAME))
        if os.path.exists(db_file):
            with open(db_file, "r") as f:
                ca_db = jsonapi.load(f)
        entries = ca_db.get(dn, {})
        entries['status'] = "valid"
        entries['expiry'] = cert.not_valid_after.strftime("%Y-%m-%d "
                                                          "%H:%M:%S.%f%z")
        entries['serial_number'] = cert.serial_number
        ca_db[dn] = entries
        with open(db_file, 'w+') as outfile:
            jsonapi.dump(ca_db, outfile, indent=4)

        with open(self.ca_serial_file(ca_name), "w+") as f:
            f.write(str(serial+1))  # next available serial is current + 1
Exemplo n.º 3
0
 def _dump(fileobj, contents, format):
     if format == 'csv':
         csv.writer(fileobj).writerows(contents.items())
     elif format == 'json':
         jsonapi.dump(contents, fileobj, separators=(',', ':'))
     elif format == 'pickle':
         pickle.dump(dict(contents), fileobj, 2)
     else:
         raise NotImplementedError('Unknown format: ' + repr(self.format))
Exemplo n.º 4
0
    def _write(self, entries, groups, roles):
        auth = {
            'allow': [vars(x) for x in entries],
            'groups': groups,
            'roles': roles,
            'version': self.version
        }

        with open(self.auth_file, 'w') as fp:
            jsonapi.dump(auth, fp, indent=2)
Exemplo n.º 5
0
    def get_n_volttron_instances(n, should_start=True, address_file=True):
        get_n_volttron_instances.count = n
        instances = []
        vip_addresses = []
        web_addresses = []
        instances = []
        addr_config = dict()
        names = []

        for i in range(0, n):
            address = get_rand_vip()
            web_address = "http://{}".format(get_rand_ip_and_port())
            vip_addresses.append(address)
            web_addresses.append(web_address)
            nm = 'platform{}'.format(i + 1)
            names.append(nm)

        for i in range(0, n):
            address = vip_addresses[i]
            web_address = web_addresses[i]
            wrapper = PlatformWrapper(messagebus='zmq', ssl_auth=False)

            addr_file = os.path.join(wrapper.volttron_home,
                                     'external_address.json')
            if address_file:
                with open(addr_file, 'w') as f:
                    jsonapi.dump(web_addresses, f)
                    gevent.sleep(.1)
            wrapper.startup_platform(address,
                                     bind_web_address=web_address,
                                     setupmode=True)
            wrapper.skip_cleanup = True
            instances.append(wrapper)

        gevent.sleep(30)
        for i in range(0, n):
            instances[i].shutdown_platform()

        gevent.sleep(5)
        # del instances[:]
        for i in range(0, n):
            address = vip_addresses.pop(0)
            web_address = web_addresses.pop(0)
            print(address, web_address)
            instances[i].startup_platform(address,
                                          bind_web_address=web_address)
            instances[i].allow_all_connections()
        gevent.sleep(20)
        instances = instances if n > 1 else instances[0]

        get_n_volttron_instances.instances = instances
        return instances
Exemplo n.º 6
0
def auth_instance(volttron_instance):
    with open(os.path.join(volttron_instance.volttron_home, "auth.json"),
              'r') as f:
        auth_file = jsonapi.load(f)
    print(auth_file)
    try:
        yield volttron_instance
    finally:
        with with_os_environ(volttron_instance.env):
            with open(
                    os.path.join(volttron_instance.volttron_home, "auth.json"),
                    'w') as f:
                jsonapi.dump(auth_file, f)
Exemplo n.º 7
0
    def build_n_volttron_instances(n, bad_config=False, add_my_address=True):
        build_n_volttron_instances.count = n
        instances = []
        vip_addresses = []
        instances = []
        addr_config = dict()

        for i in range(0, n):
            address = get_rand_vip()
            vip_addresses.append(address)

        for i in range(0, n):
            address = vip_addresses[i]
            wrapper = PlatformWrapper(messagebus='zmq', ssl_auth=False)
            wrapper.startup_platform(address)
            wrapper.skip_cleanup = True
            instances.append(wrapper)

        gevent.sleep(1)
        for i in range(0, n):
            instances[i].shutdown_platform()

        for i in range(0, n):
            addr_config.clear()
            for j in range(0, n):
                if j != i or (j == i and add_my_address):
                    name = instances[j].instance_name
                    addr_config[name] = dict()
                    addr_config[name]['instance-name'] = name
                    if bad_config:
                        addr_config[name]['vip-address123'] = vip_addresses[j]
                    else:
                        addr_config[name]['vip-address'] = vip_addresses[j]
                    addr_config[name]['serverkey'] = instances[j].serverkey

            address_file = os.path.join(instances[i].volttron_home,
                                        'external_platform_discovery.json')
            if address_file:
                with open(address_file, 'w') as f:
                    jsonapi.dump(addr_config, f)

        gevent.sleep(1)
        for i in range(0, n):
            address = vip_addresses.pop(0)
            instances[i].startup_platform(address)
            instances[i].allow_all_connections()
        gevent.sleep(11)
        instances = instances if n > 1 else instances[0]

        build_n_volttron_instances.instances = instances
        return instances
Exemplo n.º 8
0
def prep_config(volttron_home):
    src_driver = os.getcwd(
    ) + '/services/core/MasterDriverAgent/example_configurations/test_fakedriver.config'
    new_driver = volttron_home + '/test_fakedriver.config'
    shutil.copy(src_driver, new_driver)

    with open(new_driver, 'r+') as f:
        config = jsonapi.load(f)
        config['registry_config'] = os.getcwd(
        ) + '/services/core/MasterDriverAgent/example_configurations/fake.csv'
        f.seek(0)
        f.truncate()
        jsonapi.dump(config, f)

    master_config = {
        "agentid": "master_driver",
        "driver_config_list": [new_driver]
    }

    return master_config
Exemplo n.º 9
0
        notes.append("Min: " + str(min_value))

    if max_value:
        notes.append("Max: " + str(max_value))

    notes = ", ".join(notes)

    result["Notes"] = notes

    return result


for e in elements:
    row = get_csv_row(e)

    csv_file.writerow(row)

config = {
    "driver_config": {"url": url,
                      "username": username,
                      "password": password},
    "driver_type": "obix",
    "registry_config":"config://obix.csv",
    "interval": 	60,
    "timezone": "UTC"
}

jsonapi.dump(config, args.devicefile, indent=4)


Exemplo n.º 10
0
def main():
    # parse the command line arguments
    arg_parser = ConfigArgumentParser(description=__doc__)
        
    arg_parser.add_argument("device_id", type=int,
                            help="Device ID of the target device")
    
    arg_parser.add_argument("--address",
                            help="Address of target device, may be needed to help route initial request to device.")
    
    arg_parser.add_argument("--registry-out-file", type=argparse.FileType('w'),
                            help="Output registry to CSV file",
                            default=sys.stdout)

    arg_parser.add_argument("--driver-out-file", type=argparse.FileType('w'),
                            help="Output driver configuration to JSON file.",
                            default=sys.stdout)
    
    arg_parser.add_argument("--max-range-report", nargs='?', type=float,
                            help='Affects how very large numbers are reported in the "Unit Details" column of the '
                                 'output. Does not affect driver behavior.',
                            default=1.0e+20)
    
    args = arg_parser.parse_args()

    _log.debug("initialization")
    _log.debug("    - args: %r", args)

    # make a device object
    this_device = LocalDeviceObject(
        objectName=args.ini.objectname,
        objectIdentifier=int(args.ini.objectidentifier),
        maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted),
        segmentationSupported=args.ini.segmentationsupported,
        vendorIdentifier=int(args.ini.vendoridentifier),
        )

    # make a simple application
    this_application = SynchronousApplication(this_device, args.ini.address)

    _log.debug("starting build")
    
    result = get_iam(this_application, args.device_id, args.address)

    target_address = result.pduSource
    device_id = result.iAmDeviceIdentifier[1]
    
    _log.debug('pduSource = ' + repr(result.pduSource))
    _log.debug('iAmDeviceIdentifier = ' + str(result.iAmDeviceIdentifier))
    _log.debug('maxAPDULengthAccepted = ' + str(result.maxAPDULengthAccepted))
    _log.debug('segmentationSupported = ' + str(result.segmentationSupported))
    _log.debug('vendorID = ' + str(result.vendorID))

    config_file_name = basename(args.registry_out_file.name)

    config = {
        "driver_config": {"device_address": str(target_address),
                          "device_id": device_id},
        "driver_type": "bacnet",
        "registry_config": "config://registry_configs/{}".format(config_file_name)
    }

    jsonapi.dump(config, args.driver_out_file, indent=4)
    
    try:
        device_name = read_prop(this_application, target_address, "device", device_id, "objectName")
        _log.debug('device_name = ' + str(device_name))
    except TypeError:
        _log.debug('device missing objectName')
    
    try:
        device_description = read_prop(this_application, target_address, "device", device_id, "description")
        _log.debug('description = ' + str(device_description))
    except TypeError:
        _log.debug('device missing description')
    
    config_writer = DictWriter(args.registry_out_file,
                               ('Reference Point Name',
                                'Volttron Point Name',
                                'Units',
                                'Unit Details',
                                'BACnet Object Type',
                                'Property',
                                'Writable',
                                'Index',
                                'Write Priority',
                                'Notes'))
    
    config_writer.writeheader()

    try:
        object_count = read_prop(this_application, target_address, "device", device_id, "objectList", index=0)
        list_property = "objectList"
    except TypeError:
        object_count = read_prop(this_application, target_address, "device", device_id, "structuredObjectList", index=0)
        list_property = "structuredObjectList"
    
    _log.debug('objectCount = ' + str(object_count))
    
    for object_index in range(1, object_count + 1):
        _log.debug('object_device_index = ' + repr(object_index))
        
        bac_object = read_prop(this_application, target_address, "device", device_id, list_property, index=object_index)
        
        obj_type, index = bac_object

        try:
            process_object(this_application, target_address, obj_type, index, args.max_range_report, config_writer)
        except Exception:
            _log.debug("Unexpected error processing object: {} {}".format(obj_type, index))
            _log.debug(traceback.format_exc())
Exemplo n.º 11
0
    def do_add_driver_config(self, line):
        """
            Add/Edit the driver config <config_dir>/<driver name>.config for selected driver
                Example format:
                    {
                        "driver_config": {"name": "watts_on_1",
                                          "device_type": "watts_on",
                                          "device_address": "/dev/tty.usbserial-AL00IEEY",
                                          "port": 0,
                                          "slave_id": 2,
                                          "baudrate": 115200,
                                          "bytesize": 8,
                                          "parity": "none",
                                          "stopbits": 1,
                                          "xonxoff": 0,
                                          "addressing": "offset",
                                          "endian": "big",
                                          "write_multiple_registers": True,
                                          "register_map": "config://watts_on_map.csv"},
                        "driver_type": "modbus_tk",
                        "registry_config": "config://watts_on.csv",
                        "interval": 120,
                        "timezone": "UTC"
                    }
                If any config info does not match existed options, it'll set to its default value

            Option to select driver if no selected driver found
            Press <Enter> to exit

            Add a specific driver config: add_driver_config <name>
                                          <name>: name of a new driver config to add to config_dir
            Select name after the cmd:    add_driver_config
        """
        # Select device type
        device_type = dict()
        device_type_name = line
        if not device_type_name:
            self.get_device_type_descriptions()
            print('\nEnter device type name: ', end='')
            device_type_name = input().lower()
        for device in self._device_type_maps:
            if device.get('name', None) == device_type_name:
                device_type = device

        # If device type exist, add driver config
        if device_type:
            print("Enter driver name: ", end='')
            name = input().lower()
            config_dir = self.get_existed_directory(
                self._directories['config_dir'], 'config_dir')
            if config_dir:
                cont = True
                while cont:
                    cont = False
                    for f in os.listdir(config_dir):
                        if f.endswith('.config') and f.split('.')[0] == name:
                            self.do_driver_config(name)
                            print(
                                "Driver '{0}' already existed. Continue to edit the driver [y/n]: "
                                .format(name),
                                end='')
                            option = input().lower()
                            if not option or not str2bool(option):
                                print(
                                    "Please choose a different driver name OR press <Enter> to quit: ",
                                    end='')
                                name = input().lower()
                                if not name:
                                    self.do_quit('')
                                cont = True

            print('Enter interval (default to 60 seconds): ', end='')
            try:
                interval = int(input())
            except ValueError:
                interval = 60

            print('Enter device address: ', end='')
            device_address = input().lower()

            print('Enter port (default to 5020 - 0 for no port): ', end='')
            try:
                port = int(input())
            except ValueError:
                port = 5020

            print('Enter description: ', end='')
            description = input()

            addressing = device_type.get('addressing', 'offset')

            endian = device_type.get('endian', 'big')
            print(
                "Default endian for the selected device type '{0}' is '{1}'. Do you want to change it [y/n]: "
                .format(device_type_name, endian),
                end='')
            option = input().lower()
            if option and str2bool(option):
                print('Enter new endian. Press <Enter> if no change needed: ',
                      end='')
                new_endian = input().lower()
                if new_endian in ('big', 'little', 'mixed'):
                    endian = new_endian

            write_multiple_registers = str2bool(
                str(device_type.get('write_multiple_registers', 'True')))

            csv_map = self.get_existed_file(self._directories['csv_dir'],
                                            device_type.get('file'))

            print('Enter CSV config file: ', end='')
            csv_config = input()
            csv_config = csv_config if csv_config.endswith(
                '.csv') else "{0}.csv".format(csv_config)
            csv_config = self.get_existed_file(self._directories['csv_dir'],
                                               csv_config)

            driver_config = {
                "driver_config": {
                    "name": name,
                    "device_type": device_type_name,
                    "device_address": device_address,
                    "port": port,
                    "addressing": addressing,
                    "endian": endian,
                    "write_multiple_registers": write_multiple_registers,
                    "register_map": "config://" + csv_map,
                    "description": description
                },
                "driver_type": "modbus_tk",
                "registry_config": "config://" + csv_config,
                "interval": interval,
                "timezone": "UTC"
            }

            # RTU transport
            if not port:
                print('Enter slave id (default to 1): ', end='')
                try:
                    slave_id = int(input())
                except ValueError:
                    slave_id = 1

                print('Enter baudrate (default to 9600): ', end='')
                try:
                    baudrate = int(input())
                except ValueError:
                    baudrate = 9600

                print('Enter bytesize (default to 8): ', end='')
                try:
                    bytesize = int(input())
                except ValueError:
                    bytesize = 8

                print('Enter bytesize (default to none): ', end='')
                parity = input()
                if parity not in ('none', 'even', 'odd', 'mark', 'space'):
                    parity = 'none'

                print('Enter stopbits (default to 1): ', end='')
                try:
                    stopbits = int(input())
                except ValueError:
                    stopbits = 1

                print('Enter xonxoff (default to 0): ', end='')
                try:
                    xonxoff = int(input())
                except ValueError:
                    xonxoff = 0

                driver_config['driver_config'].update({
                    "slave_id": slave_id,
                    "baudrate": baudrate,
                    "bytesize": bytesize,
                    "parity": parity,
                    "stopbits": stopbits,
                    "xonxoff": xonxoff
                })

            with open(
                    "{0}/{1}.config".format(self._directories['config_dir'],
                                            name), 'w') as config_file:
                jsonapi.dump(driver_config, config_file, indent=2)

        else:
            print("Device type '{0}' does not exist".format(device_type_name))
Exemplo n.º 12
0
def main():
    global agent
    global config_writer
    # parse the command line arguments
    arg_parser = argparse.ArgumentParser(description=__doc__)

    arg_parser.add_argument("device_id",
                            type=int,
                            help="Device ID of the target device")

    arg_parser.add_argument(
        "--address",
        help=
        "Address of target device, may be needed to help route initial request to device."
    )

    arg_parser.add_argument("--registry-out-file",
                            type=argparse.FileType('w'),
                            help="Output registry to CSV file",
                            default=sys.stdout)

    arg_parser.add_argument("--driver-out-file",
                            type=argparse.FileType('w'),
                            help="Output driver configuration to JSON file.",
                            default=sys.stdout)

    arg_parser.add_argument(
        "--max-range-report",
        nargs='?',
        type=float,
        help=
        'Affects how very large numbers are reported in the "Unit Details" column of the '
        'output. Does not affect driver behavior.',
        default=1.0e+20)

    arg_parser.add_argument("--proxy-id",
                            help="VIP IDENTITY of the BACnet proxy agent.",
                            default="platform.bacnet_proxy")

    args = arg_parser.parse_args()

    _log.debug("initialization")
    _log.debug("    - args: %r", args)

    config_file_name = basename(args.registry_out_file.name)

    config = {
        "driver_config": {
            "device_address": str(args.address),
            "device_id": args.device_id
        },
        "driver_type": "bacnet",
        "registry_config":
        "config://registry_configs/{}".format(config_file_name)
    }

    jsonapi.dump(config, args.driver_out_file, indent=4)

    key_store = KeyStore()
    config_writer = DictWriter(
        args.registry_out_file,
        ('Reference Point Name', 'Volttron Point Name', 'Units',
         'Unit Details', 'BACnet Object Type', 'Property', 'Writable', 'Index',
         'Write Priority', 'Notes'))

    config_writer.writeheader()

    agent = build_agent(address=get_address(),
                        volttron_home=get_home(),
                        publickey=key_store.public,
                        secretkey=key_store.secret,
                        enable_store=False)

    bn = BACnetReader(agent.vip, args.proxy_id, bacnet_response)

    async_result = AsyncResult()

    try:
        bn.get_iam(args.device_id, async_result.set, args.address)
    except errors.Unreachable as ure:
        _log.error(ure)
        _log.error(
            "No BACnet proxy Agent running on the platform with the VIP IDENTITY {}"
            .format(args.proxy_id))
        sys.exit(1)

    try:
        results = async_result.get(timeout=5.0)
    except gevent.Timeout:
        _log.error("No response from device id {}".format(args.device_id))
        sys.exit(1)

    if args.address and args.address != results["address"]:
        msg = "Inconsistent results from passed address ({}) and device address ({}) using results.".format(
            args.address, results["address"])
        _log.warning(msg)
        args.address = results["address"]
    elif results["address"]:
        args.address = results["address"]

    bn.read_device_properties(target_address=args.address,
                              device_id=args.device_id)

    agent.core.stop()