def start_server():

    msgbus = None
    service = None

    try:
        print('[INFO] Initializing message bus context')

        ctx = cfg.ConfigMgr()
        if ctx.get_num_servers() is -1:
            raise "No server instances found, exiting..."
        server_ctx = ctx.get_server_by_index(0)
        msgbus_cfg = server_ctx.get_msgbus_config()
        msgbus = mb.MsgbusContext(msgbus_cfg)

        print(f'[INFO] Initializing service for PythonServer')

        # TODO: dynamically get this value using get_app_interface()
        service = msgbus.new_service("default")

        print('[INFO] Running...')
        while True:
            request, _ = service.recv()
            print(f'[INFO] Received request from client: {request}')
            service.response(request)
    except KeyboardInterrupt:
        print('[INFO] Quitting...')
    finally:
        if service is not None:
            service.close()
    def main(self):
        ''' FactoryControl app to subscribe to topics published by
            VideoAnalytics and control the red/green lights based on the
            classified metadata
        '''
        subscriber = None
        try:
            self.log.info("Modbus connecting on %s", self.ip_address)
            ret = self.modbus_client.connect()
            if not ret:
                self.log.error("Modbus Connection failed")
                sys.exit(-1)
            self.log.info("Modbus connected")
            sub_ctx = self.config_client.get_subscriber_by_index(0)
            topic = sub_ctx.get_topics()
            if len(topic) > 1:
                raise Exception("Multiple SubTopics are not supported")

            topic = topic[0]
            self.log.info("Subscribing on topic: %s", topic)
            msgbus_cfg = sub_ctx.get_msgbus_config()
            msgbus = mb.MsgbusContext(msgbus_cfg)
            subscriber = msgbus.new_subscriber(topic)
            while True:
                metadata, _ = subscriber.recv()
                if metadata is None:
                    raise Exception("Received None as metadata")
                self.light_ctrl_cb(metadata)
        except KeyboardInterrupt:
            self.log.error(' keyboard interrupt occurred Quitting...')
        except Exception as ex:
            self.log.exception(ex)
        finally:
            if subscriber is not None:
                subscriber.close()
def start_subscriber():
    subscriber = None

    try:
        ctx = cfg.ConfigMgr()
        if ctx.get_num_subscribers() is -1:
            raise "No subscriber instances found, exiting..."
        sub_ctx = ctx.get_subscriber_by_index(0)
        msgbus_cfg = sub_ctx.get_msgbus_config()

        print('[INFO] Initializing message bus context')
        msgbus_sub = mb.MsgbusContext(msgbus_cfg)

        topics = sub_ctx.get_topics()

        print(f'[INFO] Initializing subscriber for topic {topics[0]}')
        subscriber = msgbus_sub.new_subscriber(topics[0])

        print('[INFO] Running...')

        while True:
            msg, _ = subscriber.recv()
            if msg is not None:
                print(f'[INFO] RECEIVED by subscriber : {msg}')
            else:
                print('[INFO] Receive interrupted')
    except KeyboardInterrupt:
        print('[INFO] Quitting...')
    finally:
        if subscriber is not None:
            subscriber.close()
Exemple #4
0
def query_influxdb(ctx, query, img_handle_queue, condition):
    try:
        logger.info(f'Initializing message bus context')
        client_ctx = ctx.get_client_by_index(0)
        config = client_ctx.get_msgbus_config()
        interface_value = client_ctx.get_interface_value("Name")
        msgbus = mb.MsgbusContext(config)
        logger.info(
            'Initializing service for topic \'{"influxconnector_service"}\'')
        service = msgbus.get_service(interface_value)
        request = {'command': query}
        logger.info(f'Running...')
        logger.info(f'Sending request {request}')
        service.request(request)
        logger.info('Waiting for response')
        response, _ = service.recv()
        if len(response['Data']) > 0:
            loaded_json = json.loads(response['Data'])
            index = -1
            valid_input = [
                'channels', 'defects', 'encoding_level', 'encoding_type',
                'height', 'width'
            ]
            check = all(item in loaded_json['columns'] for item in valid_input)
            if check is True:
                for key in loaded_json['columns']:
                    if key == "img_handle":
                        index = (loaded_json['columns'].index(key))
            if index >= 0:
                for elm in loaded_json['values']:
                    temp_dict = dict()
                    count = 0
                    for key in loaded_json['columns']:
                        temp_dict[key] = elm[count]
                        count = count + 1

                    is_queue_empty = img_handle_queue.empty()
                    img_handle_queue.put(temp_dict)
                    if is_queue_empty:
                        with condition:
                            condition.notifyAll()

            output_dir = "/output" + "/" + "data"
            now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
            filename = str(now) + ".dat"
            common.write_to_file(filename, str.encode(response['Data']),
                                 output_dir)
        with condition:
            condition.notifyAll()

        service.close()
    except KeyboardInterrupt:
        logger.info(f' Quitting...')
        service.close()
Exemple #5
0
def start_publisher():
    publisher = None

    try:
        ctx = cfg.ConfigMgr()
        if ctx.get_num_publishers() == -1:
            raise "No publisher instances found, exiting..."
        pub_ctx = ctx.get_publisher_by_index(0)
        msgbus_cfg = pub_ctx.get_msgbus_config()

        print('[INFO] Initializing message bus context')
        msgbus_pub = mb.MsgbusContext(msgbus_cfg)

        topics = pub_ctx.get_topics()
        print(f'[INFO] Initializing publisher for topic \'{topics[0]}\'')
        publisher = msgbus_pub.new_publisher(topics[0])

        app_cfg = ctx.get_app_config()
        print(f'App Config is  \'{app_cfg}\'')

        print('[INFO] Running...')
        while True:
            blob = b'\x22' * 10
            meta = {
                'integer': 123,
                'floating': 55.5,
                'string': 'test',
                'boolean': True,
                'empty': None,
                'obj': {
                    'test': {
                        'test2': 'hello'
                    },
                    'test3': 'world'
                },
                'arr': ['test', 123]
            }

            publisher.publish((
                meta,
                blob,
            ))
            print(f'[INFO] Msg published by publisher :  \'{meta}\'')
            time.sleep(int(app_cfg["loop_interval"]))
    except KeyboardInterrupt:
        print('[INFO] Quitting...')
    finally:
        if publisher is not None:
            publisher.close()
    def callback(self, msgbus_cfg, topic):
        """Callback called when the databus has a new message.

        :param msgbus_cfg: config for the context creation in EIIMessagebus
        :type: str
        :param topic: Topic the message was published on
        :type: str
        """
        subscriber = None
        try:
            self.logger.info('Initializing message bus context')

            msgbus = mb.MsgbusContext(msgbus_cfg)

            self.logger.info(f'Initializing subscriber for topic \'{topic}\'')
            subscriber = msgbus.new_subscriber(topic)

            stream_label = None

            for key in self.labels:
                if key == topic:
                    stream_label = self.labels[key]
                    break

            while True:
                metadata, blob = subscriber.recv()

                if metadata is not None and blob is not None:
                    frame = self.decode_frame(metadata, blob)

                    self.logger.debug(f'Metadata is : {metadata}')

                    if (self.draw_results):
                        self.draw_defect(metadata, frame, stream_label)

                    if self.save_image:
                        self.save_images(metadata, frame)

                    self.queue_publish(topic, frame)
                else:
                    self.logger.debug(f'Non Image Data Subscription\
                                    : Classifier_results: {metadata}')
        except Exception as e:
            self.logger.exception(f'Exception occured: {e}')
        finally:
            if subscriber is not None:
                subscriber.close()
def main():
    """Main method
    """
    args = parse_args()

    # Make sure that the ./.socks directory exist just in case this is using
    # IPC communication
    if not os.path.exists('./.socks'):
        os.mkdir('./.socks')

    if os.path.isfile(args.msgbus_config):
        with open(args.msgbus_config, 'r') as f:
            config = json.load(f)

    print('[INFO] Initializing message bus context')
    ctx = msgbus.MsgbusContext(config)

    # Start the benchmarking end specified by the CLI
    args.func(ctx, args)
Exemple #8
0
def start_client():
    msgbus = None
    service = None

    try:
        print('[INFO] Initializing message bus context')

        ctx = cfg.ConfigMgr()
        if ctx.get_num_clients() is -1:
            raise "No client instances found, exiting..."
        client_ctx = ctx.get_client_by_index(0)
        msgbus_cfg = client_ctx.get_msgbus_config()

        msgbus = mb.MsgbusContext(msgbus_cfg)
        print(f'[INFO] Initializing service for {"default"}')

        # TODO: dynamically get this value using get_app_interface()
        service = msgbus.get_service("default")
        # Request used for the example
        request = {
            'int': 42,
            'float': 55.5,
            'str': 'Hello, World!',
            'bool': True
        }

        app_cfg = ctx.get_app_config()
        print(f'App Config is  \'{app_cfg}\'')

        print('[INFO] Running...')
        while True:
            print(f'[INFO] Sending request {request}')
            service.request(request)
            print('[INFO] Waiting for response')
            response, _ = service.recv()
            print(f'[INFO] Received response: {response}')
            time.sleep(int(app_cfg["loop_interval"]))
    except KeyboardInterrupt:
        print('[INFO] Quitting...')
    finally:
        if service is not None:
            service.close()
Exemple #9
0
    def eiiSubscriber(self):
        """ To subscribe over
        EIIMessagebus. """

        mode = self.msgbus_cfg["type"]
        if (not self.devMode and mode == "zmq_tcp"):
            for key in self.msgbus_cfg[self.topic]:
                if self.msgbus_cfg[self.topic][key] is None:
                    raise ValueError("Invalid Config")

        msgbus = mb.MsgbusContext(self.msgbus_cfg)
        subscriber = msgbus.new_subscriber(self.topic)

        logger.info('Ready to receive data from msgbus')
        while not self.done_receiving:
            meta_data, _ = subscriber.recv()
            self.handling_mode(meta_data)
            del meta_data
        subscriber.close()
        return
def retrieve_image_frames(ctx, query, img_handle_queue, condition):
    msgbus = None
    service = None
    dir_name = "/output/"
    global influxdb_query_done
    try:
        client_ctx = ctx.get_client_by_index(1)
        config = client_ctx.get_msgbus_config()
        interface_value = client_ctx.get_interface_value("Name")
        msgbus = mb.MsgbusContext(config)
        logger.info(f'Initializing service for topic \'imagestore_service\'')
        service = msgbus.get_service(interface_value)
        logger.info(f'Running...')
        while True:
            if not img_handle_queue.empty():
                metadata = img_handle_queue.get()
                img_handle = metadata['img_handle']
                request = {'command': 'read', 'img_handle': img_handle}
                logger.info(f'Sending request {request}')
                service.request(request)
                logger.info(f'Waiting for response')
                response = service.recv()
                output_dir = dir_name + "frames"
                if response[1] is not None:
                    draw_defects(metadata, response[1], output_dir)
                else:
                    logger.info("Image for {} is NULL".format(img_handle))
            elif img_handle_queue.empty() and influxdb_query_done:
                break
            elif img_handle_queue.empty() and not influxdb_query_done:
                with condition:
                    condition.wait()

    except KeyboardInterrupt:
        logger.info(f'Quitting...')
    finally:
        if service is not None:
            service.close()
 def eiiSubscriber(self):
     """ To subscribe over
     EIIMessagebus. """
     msgbus = mb.MsgbusContext(self.msgbus_config)
     subscriber = msgbus.new_subscriber(self.topic)
     if self.monitor_mode:
         while not self.done_receiving:
             md, fr = subscriber.recv()
             md['ts_vp_sub'] = int(round(time.time() * 1000))
             self.add_profile_data(md)
             for keys in md.keys():
                 for di_keys in dict_of_keys.keys():
                     if (di_keys in keys):
                         logger.critical(dict_of_keys[di_keys])
     elif self.fps_mode:
         while not self.done_receiving:
             md, fr = subscriber.recv()
             self.calculate_fps()
             # Discarding both the meta-data & frame
             del md
             del fr
     subscriber.close()
     return
Exemple #12
0
    def configure(self, config):
        """Configure the Azure Bridge using the given Azure digital
        twin for the module.

        .. warning:: This will clear out any previous state that existed (i.e.
            all subscribers will be stopped)

        :param dict config: Azure IoT Hub digital twin for the Azure Bridge
        """
        self.log.info('Configuring the Azure Bridge')

        # Verify the configuration
        self.log.debug('Validating JSON schema of new configuration')
        validate(instance=config, schema=self.schema)

        # Reset message bus state if needed
        if self.ipc_msgbus_ctxs or self.tcp_msgbus_ctxs:
            # Stop all subscribers
            self.log.debug('Stopping previous subscribers')

            # Clean up the message bus contexts and subscribers
            self._cleanup_msgbus_ctxs()

        # Configure logging
        if 'log_level' in config:
            log_level = config['log_level'].upper()
        else:
            log_level = 'INFO'

        self.log = configure_logging(log_level, __name__, False)

        self.log.info('Getting EII Message Bus configuration')
        ipc_msgbus_config, tcp_msgbus_config = get_msgbus_config(
            self.app_name, self.config_mgr, self.dev_mode)

        self.log.debug(f'Topic msgbus config dict: \nIPC: '
                       f'{ipc_msgbus_config}, \nTCP:{tcp_msgbus_config}')

        # Initialize message bus context
        self.log.info('Initializing EII Message Bus')
        if ipc_msgbus_config:
            for topic, msgbus_config in ipc_msgbus_config.items():
                ipc_msgbus_ctx = emb.MsgbusContext(msgbus_config)
                self.ipc_msgbus_ctxs[topic] = ipc_msgbus_ctx
        if tcp_msgbus_config:
            for topic, msgbus_config in tcp_msgbus_config.items():
                tcp_msgbus_ctx = emb.MsgbusContext(msgbus_config)
                self.tcp_msgbus_ctxs[topic] = tcp_msgbus_ctx

        # Initialize subscribers
        listener_coroutines = []

        try:
            for (in_topic, topic_conf) in config['topics'].items():
                self.log.info(f'Creating subscriber {in_topic}')
                self.log.debug(f'{in_topic} config: {topic_conf}')
                if 'az_output_topic' not in topic_conf:
                    raise AssertionError('Missing az_output_topic')

                if self.bsc is not None and \
                        'az_blob_container_name' in topic_conf:
                    cn = topic_conf['az_blob_container_name']
                else:
                    cn = None

                msgbus_ctx = None
                if in_topic in self.ipc_msgbus_ctxs:
                    msgbus_ctx = self.ipc_msgbus_ctxs[in_topic]
                elif in_topic in self.tcp_msgbus_ctxs:
                    msgbus_ctx = self.tcp_msgbus_ctxs[in_topic]
                else:
                    raise RuntimeError(
                        f'Cannot find {in_topic} msgbus context')

                # Initialize the subcsriber
                subscriber = msgbus_ctx.new_subscriber(in_topic)

                self.subscribers.append(subscriber)
                listener_coroutines.append(
                    emb_subscriber_listener(self, subscriber,
                                            topic_conf['az_output_topic'], cn))
        except Exception as ex:
            # Clean up the message bus contexts
            self._cleanup_msgbus_ctxs()
            self.log.exception(f'Exception: {ex}')

            # Re-raise whatever exception just occurred
            raise

        # Schedule task for C2D Listener
        self.subscriber_listeners = asyncio.gather(*listener_coroutines)

        # Configure EII
        self.log.info('Getting ETCD configuration')

        # NOTE: THIS IS A HACK, AND NEEDS TO BE FIXED IN THE FUTURE
        hostname = 'localhost'

        # This change will be moved to an argument to the function in 2.3
        # This is done now for backward compatibility
        etcd_host = os.getenv('ETCD_HOST')
        if etcd_host is not None and etcd_host != '':
            hostname = etcd_host

        port = os.getenv('ETCD_CLIENT_PORT', '2379')
        if not Util.check_port_availability(hostname, port):
            raise RuntimeError(f'etcd service port: {port} is not up!')

        try:
            if self.dev_mode:
                etcd = etcd3.client(host=hostname, port=port)
            else:
                etcd = etcd3.client(
                    host=hostname,
                    port=port,
                    ca_cert='/run/secrets/rootca/cacert.pem',
                    cert_key='/run/secrets/root/root_client_key.pem',
                    cert_cert='/run/secrets/root/root_client_certificate.pem')
        except Exception as e:
            self.log.exception(f'Exception raised when creating etcd'
                               f'client instance with error: {e}')
            raise e
        resp = etcd.get_all()
        eii_config = {}
        for value, meta in resp:
            try:
                value = json.loads(value.decode('utf-8'))
                key = meta.key.decode('utf-8')
                eii_config[key] = value
            except Exception as e:
                # NOTE: Errors may happen if security is enabled, because the
                # first part is the request's key
                self.log.error(f'{e}')

        self.log.debug('Finding changes in EII configuration')
        new_eii_config = json.loads(config['eii_config'])
        changed_keys, removed_keys = find_root_changes(eii_config,
                                                       new_eii_config)

        self.log.debug(f'Changed service configs: {changed_keys}')
        self.log.debug(f'Removed service configs: {removed_keys}')

        self.log.info('Applying EII configuration')

        self.log.debug('Applying changes to the changed configs')
        for key in changed_keys:
            self.log.debug(f'Pushing config for {key}')
            etcd.put(key, json.dumps(new_eii_config[key], indent=4))
            self.log.debug(f'Successfully pushed config for {key}')

        self.log.debug('Deleting removed service configs')
        for key in removed_keys:
            # NOTE: THIS IS A HACK, AND NEEDS TO BE FIXED IN THE FUTURE
            self.log.debug(f'Removing config for {key}')
            etcd.delete(key)
            self.log.debug(f'Successfully removed config for {key}')

        self.log.info('EII configuration update applied')

        # Save configuration for future comparisons
        self.config = config
Exemple #13
0
ap.add_argument('-s',
                '--service-name',
                dest='service_name',
                default='echo_service',
                help='Service name')
args = ap.parse_args()

msgbus = None
service = None

with open(args.config, 'r') as f:
    config = json.load(f)

try:
    print('[INFO] Initializing message bus context')
    msgbus = mb.MsgbusContext(config)

    print(f'[INFO] Initializing service for topic \'{args.service_name}\'')
    service = msgbus.get_service(args.service_name)

    # Request used for the example
    request = {'int': 42, 'float': 55.5, 'str': 'Hello, World!', 'bool': True}

    print('[INFO] Running...')
    while True:
        print(f'[INFO] Sending request {request}')
        service.request(request)
        print('[INFO] Waiting for response')
        response = service.recv()
        print(f'[INFO] Received response: {response.get_meta_data()}')
        time.sleep(1)