Ejemplo n.º 1
0
    def __init__(self):

        self.queue = 'ModbusPlugin-TCP'

        self.mq_serv = MsgQueueService('test_queue')
        self.mq_serv.set_callback(self._recv)
        self.mq_serv.start(daemon=True)
Ejemplo n.º 2
0
class Unittest(object):
    def __init__(self):
        self.mq_serv = MsgQueueService('test_queue')
        self.mq_serv.set_callback(self._recv)
        self.mq_serv.start(daemon=True)

    def _recv(self, json_in):
        print('Unittest   _recv')
        print(json_in)
Ejemplo n.º 3
0
class PlugInAdapter(object):
    def __init__(self, credential=None):
        self.name = 'opcua'
        self.data_callback = None
        self.event_callback = None

        if credential is None:
            self.config = ConfigLoader()
        else:
            self.config = ConfigLoader(credential)

        self.configdict = self.config.ConfigSectionMap('Amqp')
        self.mq_serv = MsgQueueService(
            self.name,
            self.configdict['url'],
            threading.Lock(),
            tls_config=UaAmqpSecurity().get_tls_confg()
        )

    def _recv(self, json_in):
        if self.data_callback and 'data' in json_in.keys():
            plugin_name = json_in['data'][0]
            device_name = json_in['data'][1]
            var_name = json_in['data'][2]
            var_data = json_in['data'][3]
            self.data_callback(plugin_name, device_name, var_name, var_data)

        if self.event_callback and 'event' in json_in.keys():
            plugin_name = json_in['event'][0]
            device_name = json_in['event'][1]
            event_name = json_in['event'][2]
            event_data = json_in['event'][3]
            self.event_callback(
                plugin_name,
                device_name,
                event_name,
                event_data)

    def start(self):
        # TBD, use try to cather the error
        self.mq_serv.set_callback(self._recv)
        self.mq_serv.start()

    def stop(self):
        self.mq_serv.stop()

    def plugin_setvar(self, plugin_name, json_in):
        self.mq_serv.publish(plugin_name, json_in)

    def plugin_call(self, plugin_name, json_in):
        res = self.mq_serv.request(plugin_name, json_in, timeout=4)
        if res is None:
            return 0, {'code': ReturnCodes.PLUGIN_RpcError,
                       'data': 'rpc request error'}
        return 0, res

    def subscription(self, data_callback, event_callback):
        self.data_callback = data_callback
        self.event_callback = event_callback
Ejemplo n.º 4
0
class Unittest(object):
    def __init__(self):
        self.mq_serv = MsgQueueService('test_queue')
        self.mq_serv.set_callback(self._recv)
        self.mq_serv.start(daemon=True)

    def _recv(self, json_in):
        print('Unittest   _recv')
        print(json_in)

    def test_put(self, key, value):
        obj = {'method': 'put', 'data': [key, value]}
        print(self.mq_serv.request('KvPlugin', obj))

    def test_get(self, key):
        obj = {'method': 'get', 'data': [key]}
        print(self.mq_serv.request('KvPlugin', obj))

    def test_delete(self, key):
        obj = {'method': 'delete', 'data': [key]}
        print(self.mq_serv.request('KvPlugin', obj))

    def test_getstate(self, name=None):
        obj = {'method': 'getstate', 'data': [name]}
        print(self.mq_serv.request('KvPlugin', obj))
Ejemplo n.º 5
0
class Unittest(object):
    def __init__(self):
        self.mq_serv = MsgQueueService('test_queue')
        self.mq_serv.set_callback(self._recv)
        self.mq_serv.start(daemon=True)

    def _recv(self, json_in):
        print('Unittest   _recv')
        print(json_in)

    def test_open(self, *args):
        obj = {'method': 'open', 'data': args}
        print(self.mq_serv.request('ScpiPlugin', obj))

    def test_close(self, *args):
        obj = {'method': 'close', 'data': args}
        print(self.mq_serv.request('ScpiPlugin', obj))

    def test_state(self, *args):
        obj = {'method': 'state', 'data': args}
        print(self.mq_serv.request('ScpiPlugin', obj))

    def test_send(self, *args):
        obj = {'method': 'send', 'data': args}
        print(self.mq_serv.request('ScpiPlugin', obj))
Ejemplo n.º 6
0
    def __init__(self, credential=None):
        self.name = 'opcua'
        self.data_callback = None
        self.event_callback = None

        if credential is None:
            self.config = ConfigLoader()
        else:
            self.config = ConfigLoader(credential)

        self.configdict = self.config.ConfigSectionMap('Amqp')
        self.mq_serv = MsgQueueService(
            self.name,
            self.configdict['url'],
            threading.Lock(),
            tls_config=UaAmqpSecurity().get_tls_confg())
Ejemplo n.º 7
0
    def __init__(self, entity, config):
        self.name = entity.plugin_name
        self.thread_monitor = None
        self.monitor_wait = 1.0
        self.monitor_finish_evt = threading.Event()
        # Use threading.Event for further potential event trigger usage
        self.sigterm = threading.Event()
        self.entity = entity
        self.config = config
        self.methods = self.entity.get_methods()

        url = None
        if self.config.fetch_mq_env():
            url = self.config.fetch_mq_env()['url']

        self.mq_serv = MsgQueueService(
            self.name,
            url=url,
            req_lock=threading.Lock(),
            tls_config=self.config.fetch_security_env())
        LogService.initialize(**(self.config.fetch_logger_env() or {}))
Ejemplo n.º 8
0
class Unittest(object):
    def __init__(self):

        self.queue = 'ModbusPlugin-TCP'

        self.mq_serv = MsgQueueService('test_queue')
        self.mq_serv.set_callback(self._recv)
        self.mq_serv.start(daemon=True)

    def _recv(self, json_in):
        print('Unittest   _recv')
        print(json_in)

    def test_write_coil(self, uri, address, value):
        obj = {'method': 'write_coil', 'data': [uri, address, value]}
        print(self.mq_serv.request(self.queue, obj))

    def test_write_register(self, uri, address, value):
        obj = {'method': 'write_register', 'data': [uri, address, value]}
        print(self.mq_serv.request(self.queue, obj))

    def test_read_coils(self, uri, address, count):
        obj = {'method': 'read_coils', 'data': [uri, address, count]}
        print(self.mq_serv.request(self.queue, obj))

    def test_read_discrete_inputs(self, uri, address, count):
        obj = {'method': 'read_discrete_inputs', 'data': [uri, address, count]}
        print(self.mq_serv.request(self.queue, obj))

    def test_read_holding_registers(self, uri, address, count):
        obj = {
            'method': 'read_holding_registers',
            'data': [uri, address, count]
        }
        print(self.mq_serv.request(self.queue, obj))

    def test_read_input_registers(self, uri, address, count):
        obj = {'method': 'read_input_registers', 'data': [uri, address, count]}
        print(self.mq_serv.request(self.queue, obj))
Ejemplo n.º 9
0
class BasePluginClient(object):
    def __init__(self, entity, config):
        self.name = entity.plugin_name
        self.thread_monitor = None
        self.monitor_wait = 1.0
        self.monitor_finish_evt = threading.Event()
        # Use threading.Event for further potential event trigger usage
        self.sigterm = threading.Event()
        self.entity = entity
        self.config = config
        self.methods = self.entity.get_methods()

        url = None
        if self.config.fetch_mq_env():
            url = self.config.fetch_mq_env()['url']

        self.mq_serv = MsgQueueService(
            self.name,
            url=url,
            req_lock=threading.Lock(),
            tls_config=self.config.fetch_security_env())
        LogService.initialize(**(self.config.fetch_logger_env() or {}))

    def sys_handler(self, signum, frame):
        logger.info('Exiting plugin process ... name:%s', self.name)
        self.stop()

    def start(self, daemon=False):
        logger.debug('Loading plugin process ... name:%s', self.name)

        self.thread_monitor = threading.Thread(target=self._thread_monitor,
                                               args=(None, ))
        self.thread_monitor.setDaemon(True)
        self.thread_monitor.start()

        self.mq_serv.set_callback(self._recv, self._notify)
        logger.info('Loading plugin process finish ... name:%s', self.name)

        self.mq_serv.start()
        signal.signal(signal.SIGTERM, self.sys_handler)
        signal.signal(signal.SIGINT, self.sys_handler)

        logger.info('Loading plugin process start ... name:%s', self.name)
        signal.pause()

    def stop(self):
        self.sigterm.set()
        self.monitor_finish_evt.set()
        self.thread_monitor.join()
        self.pub_event('', 'exit', '')
        self.mq_serv.stop()
        self.sigterm.clear()
        logger.info('Exited plugin process ... name:%s', self.name)

    def is_alive(self):
        return not self.sigterm.is_set()

    def pub_data(self, dev_name, var_name, var_data):
        json_out = {'data': [self.name, dev_name, var_name, var_data]}
        try:
            if self.mq_serv:
                self.mq_serv.publish('opcua', json_out)
        except BaseException:
            pass

    def pub_event(self, dev_name, evt_name, evt_data):
        json_out = {'event': [self.name, dev_name, evt_name, evt_data]}
        try:
            if self.mq_serv:
                self.mq_serv.publish('opcua', json_out)
        except BaseException:
            pass

    def getrd(self):
        return {'code': ReturnCodes.Good, 'data': json.dumps(self.entity.data)}

    def ping(self):
        return {'code': ReturnCodes.Good, 'data': 'pong'}

    def getstate(self, name=None):
        return {
            'code': ReturnCodes.PLUGIN_ParamError,
            'data': 'No yet implemented in this Plugin'
        }

    def refresh(self):
        return {
            'code': ReturnCodes.PLUGIN_ParamError,
            'data': 'No yet implemented in this Plugin'
        }

    def download(self, file_name, file_str):
        file_path = os.path.split(os.path.realpath(
            self.entity.file_name))[0] + '/' + file_name
        file = open(file_path, 'wb')
        file.write(b64decode(file_str))
        file.close()
        json_out = {'code': ReturnCodes.Good, 'data': "Success"}
        return json_out

    def _prepare_methods(self, method, input_list):
        m = self.methods.get(method, None)
        args = []
        if not m:
            return False, args, 'the method is not defined'

        flag, args, msg = m.check_inputs(input_list)
        if not flag:
            return False, [], msg

        return True, args, 'OK'

    def plugin_call(self, method, msg):
        flag, args, msg = self._prepare_methods(method, msg)
        if not flag:
            return {'code': ReturnCodes.PLUGIN_ParamError, 'data': msg}
        if hasattr(self, method):
            func = getattr(self, method)
            if callable(func):
                try:
                    return func(*args)
                except BaseException:
                    logger.exception('method execute error')
                    return {
                        'code': 500,
                        'data': 'unknown method execute error'
                    }
        return {
            'code': ReturnCodes.PLUGIN_ParamError,
            'data': 'method not implemented'
        }

    def plugin_poll(self):
        custom_objs = self.entity.get_custom_nodes()
        if len(custom_objs) == 0:
            try:
                self.pub_event('', 'online', '')
            except BaseException:
                pass
        for obj in custom_objs:
            try:
                self.pub_event(obj.name, 'online', '')
            except BaseException:
                pass

    def _do_write_variable(self, data):
        variable_name = data['variable_name']
        value = data['data']

    def _recv(self, json_in):
        if 'method' in json_in:
            method = json_in['method'] if 'method' in json_in else None
            data = json_in['data'] if 'data' in json_in else None
            json_out = self.plugin_call(method, data)
            return json_out
        else:
            logger.info('incorrect client request. %s' % json.dumps(json_in))

    def _notify(self, json_in):
        if 'variable' in json_in:
            self._do_write_variable(json_in)
        else:
            logger.info('incorrect client request. %s' % json.dumps(json_in))

    def _thread_monitor(self, *args, **kwargs):
        while self.is_alive():
            self.monitor_finish_evt.wait(self.monitor_wait)
            try:
                self.plugin_poll()
                if self.monitor_wait < 5:
                    self.monitor_wait += 1
            except BaseException:
                logger.exception('plugin_poll error')