function updateConfig()

in greengrass-opcua-adapter-nodejs/index.js [111:176]


function updateConfig()
{
    updateOPCUASubscriberSetStatus();
    ConfigAgent.configInit(ConfigAgent.ReServerConfigs, () => {
        if (ConfigAgent.ReServerConfigs.length > 0) {
            let reConfigServerMap = convertSerConfigToMap(ConfigAgent.ReServerConfigs);
            let OPCUASubscriberSetMap = convertOPCUASubscriberSetToMap(OPCUASubscriberSet);
            // 1. disconnect subscribe which not exist or need modification
            for (let serverName in OPCUASubscriberSetMap) {
                let haveSameSub = false;
                if (serverName in reConfigServerMap &&
                    OPCUASubscriberSetMap[serverName]["url"] === reConfigServerMap[serverName]["url"]) {
                    let reConfigServerSubMap = reConfigServerMap[serverName]["subscription"];
                    let OPCUASubscriberSetSubMap = OPCUASubscriberSetMap[serverName]["subscription"];
                    haveSameSub = Object.keys(reConfigServerSubMap).length == Object.keys(OPCUASubscriberSetSubMap).length;

                    if (haveSameSub) {
                        for (let subs in reConfigServerSubMap) {
                            if (! (subs in OPCUASubscriberSetSubMap)) {
                                haveSameSub = false;
                                break;
                            }
                        }
                    }
                }

                if (!haveSameSub) {
                    // subscriber disconnect and removed from OPCUASubscriberSet
                    let index = 0;
                    for (index = 0; index < OPCUASubscriberSet.length; index += 1) {
                        if (OPCUASubscriberSet[index].getServerConfig().name ===
                            serverName) {
                            OPCUASubscriberSet[index].disconnect();
                            OPCUASubscriberSet.splice(index, 1);
                            index -= 1;
                        }
                    }
                } else {
                    // set connection flag to true to prevent replicate connect in connectServer()
                    ConfigAgent.ReServerConfigs.find(function(item, index, array) {
                        if (item.server.name === serverName) {
                            item.connection = true;
                            return;
                        }
                    });
                }
            }
            // 2. connect to modified server
            connectServer(ConfigAgent.ReServerConfigs, ConfigAgent.clientOptions, ConfigAgent.customerOption);
            ConfigAgent.ServerConfigs = ConfigAgent.ReServerConfigs;
            console.log("+++++++++++++++ dump ConfigAgent.ServerConfigs +++++++++++++++");
            dumpServerInfo(ConfigAgent.ReServerConfigs);
            ConfigAgent.ReServerConfigs = [];
        }
        else
        {
            // there's no any configuration, disconnect all connected device
            // subscriber and clear the OPCUASubscriberSet
            let index = 0;
            for (index = 0; index < OPCUASubscriberSet.length; index += 1) {
                OPCUASubscriberSet[index].disconnect();
            }
            clearArray(OPCUASubscriberSet);
        }
    });
}