Exemplo n.º 1
0
 def config(self, **params):
     super(Hmi, self).config(**params)
     hmi_mb_client_path = os.path.join(utils.get_pkg_path(),
                                       'hmi_mb_client.py')
     self.cmd('{} {} {} &'.format(sys.executable, hmi_mb_client_path,
                                  self.name))
     # TODO: Replace with parser vars
     self.vars = [{
         'name': 'Start',
         'mb_table': 'hr',
         'mb_addr': 1,
         'value': False
     }, {
         'name': 'Stop',
         'mb_table': 'hr',
         'mb_addr': 2,
         'value': False
     }, {
         'name': 'Velocity',
         'mb_table': 'hr',
         'mb_addr': 3,
         'value': 0
     }]
     self.var_link_clbk = None
     self.__var_monitor_clbk = None
Exemplo n.º 2
0
def run():
    try:
        # Get package path
        pkg_filepath = utils.get_pkg_path()
        # Get value of DSTDIR key in Makefile
        tmp_path = utils.get_dstdir_path_from_mkfile(os.environ["PLC_NAME"])
        patch_plc_runtime(pkg_filepath, tmp_path)
        patch_pous_h(tmp_path)
    except Exception as ex:
        logger.error(ex)
Exemplo n.º 3
0
 def config(self, **params):
     super(Plc, self).config(**params)
     # TODO: Error handling
     st_path = params.pop('st_path', None)
     mb_map = params.pop('mb_map', None)
     mb_map_path = self.__persist_mb_map(
         mb_map) if mb_map is not None else ''
     plc_supervisor_path = os.path.join(utils.get_pkg_path(),
                                        'plc_supervisor.py')
     cmd = '{} {} {} {} {} &'.format(sys.executable, plc_supervisor_path,
                                     self.name, st_path, mb_map_path)
     self.cmd(cmd)
Exemplo n.º 4
0
 def config(self, **params):
     super(RfidReaderMqttWiFi, self).config(**params)
     self.mqtt_host = params.pop('mqtt_host', None)
     self.mqtt_topic = params.pop('mqtt_topic', None)
     self.auth = params.pop('auth', None)
     if self.mqtt_host is None:
         logger.error(
             "No MQTT host has been provided for station [name=%s].",
             self.name)
     if self.mqtt_topic is None:
         logger.error(
             "No MQTT topic has been provided for station [name=%s].",
             self.name)
     mqtt_client_path = os.path.join(utils.get_pkg_path(), 'mqtt_client.py')
     self.cmd('{} {} {} &'.format(sys.executable, mqtt_client_path,
                                  self.name))
     self.is_connected = False
     self.read_clbks = []
Exemplo n.º 5
0
 def get_resource_path(name):
     return path.join(get_pkg_path(), WEB_BASE + sep + name)
Exemplo n.º 6
0
    def __init_viz_data(self):
        def get_links(nodes):
            def get_idx_in_nodes(name):
                for i, dic in enumerate(nodes):
                    logger.debug(dic)
                    if dic['name'] == name:
                        return i
                return -1

            def get_motor_links():
                """Returns links between PLCs and Motors (physical devices)."""
                lks = []
                # I/O links are currently limited to PLC only
                for motor in self.parser.motors:
                    idx_motor = get_idx_in_nodes(motor['name'])
                    if idx_motor == -1:
                        logger.error("Could not find motor in nodes array.")
                        continue
                    if motor['plc_name']:
                        idx_plc = get_idx_in_nodes(motor['plc_name'])
                        if idx_plc == -1:
                            logger.error("Could not find PLC in nodes array.")
                            continue
                        lks.append({
                            "source": idx_motor,
                            "target": idx_plc,
                            "type": "io"
                        })
                return lks

            def get_switch_ap_links(node_arr, link_type):
                """Returns links from Switches or Access Points based on AML parsing results."""
                # cola-format: { "source": 1, "target": 2 }
                # aml-data-struct: {'name': 'Switch1', 'links': ['HMI1', 'PLC1']}
                lks = []
                for node in node_arr:
                    idx_node = get_idx_in_nodes(node['name'])
                    if idx_node == -1:
                        logger.error(
                            "Could not find node [name={}] in nodes array.".
                            format(node['name']))
                        continue
                    for lk in node['links']:
                        idx_link = get_idx_in_nodes(lk)
                        if idx_link == -1:
                            logger.error("Could not find link in nodes array.")
                            continue
                        lks.append({
                            "source": idx_node,
                            "target": idx_link,
                            "type": link_type
                        })
                return lks

            return get_switch_ap_links(self.parser.switches, 'wired-network') + \
                   get_switch_ap_links(self.parser.aps, 'wireless-network') + \
                   get_motor_links()

        def get_nodes():
            def get_node(val, node_type=None):
                name = val['name']
                node = {
                    "id": name.lower(),
                    "name": name,
                }
                if 'network' in val:
                    node['network'] = val['network']
                if node_type is not None:
                    node['type'] = node_type
                return node

            nds = []

            type_mapping = [(self.parser.plcs, 'plc'),
                            (self.parser.hmis, 'hmi'),
                            (self.parser.switches, 'switch'),
                            (self.parser.motors, 'motor'),
                            (self.parser.aps, 'ap'),
                            (self.parser.mqttbrkrs, 'mqttbrkr'),
                            (self.parser.rfidrs, 'rfidr'),
                            (self.parser.iiotgws, 'iiotgw')]

            for (arr, n_type) in type_mapping:
                for node in arr:
                    nds.append(get_node(node, n_type))

            return nds

        def get_data():
            nodes = get_nodes()
            links = get_links(nodes)
            return {"nodes": nodes, "groups": [], "links": links}

        data_file_path = path.join(get_pkg_path(),
                                   WEB_BASE + sep + VIZ_DATA_FILENAME)
        # data = json.load(open(path.join(get_pkg_path(), WEB_BASE + sep + VIZ_DATA_FILENAME)))
        data = get_data()
        with open(data_file_path, 'w') as outfile:
            json.dump(data, outfile)
        logger.debug(data)