SubTopics[] ProcessSubTopicData(JSONArray arr)
 {
     SubTopics[] subTopics = new SubTopics[arr.Count];
     for (int i = 0; i < arr.Count; i++)
     {
         subTopics[i]      = new SubTopics();
         subTopics[i].id   = arr[i]["subtopic_id"].Value;
         subTopics[i].name = arr[i]["subtopic_name"].Value;
     }
     return(subTopics);
 }
Exemple #2
0
        /// <summary>
        /// Loads the configuration from the specified file.
        /// </summary>
        public bool Load(string fileName, out string errMsg)
        {
            try
            {
                SetToDefault();

                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException(string.Format(CommonPhrases.NamedFileNotFound, fileName));
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);
                XmlElement rootElem = xmlDoc.DocumentElement;

                if (rootElem.SelectSingleNode("MqttParams") is XmlElement mqttParamsElem)
                {
                    ConnectionArgs.ClientId = mqttParamsElem.GetAttribute("ClientID");
                    ConnectionArgs.Hostname = mqttParamsElem.GetAttribute("Hostname");
                    ConnectionArgs.Port     = mqttParamsElem.GetAttrAsInt("Port");
                    ConnectionArgs.Username = mqttParamsElem.GetAttribute("UserName");
                    ConnectionArgs.Password = mqttParamsElem.GetAttribute("Password");
                }

                if (xmlDoc.DocumentElement.SelectSingleNode("MqttSubTopics") is XmlNode mqttSubTopicsNode)
                {
                    foreach (XmlElement topicElem in mqttSubTopicsNode.SelectNodes("Topic"))
                    {
                        SubTopics.Add(new MqttSubTopic
                        {
                            TopicName = topicElem.GetAttribute("TopicName"),
                            QosLevel  = (MqttQos)topicElem.GetAttrAsInt("QosLevel")
                        });
                    }
                }

                if (xmlDoc.DocumentElement.SelectSingleNode("MqttSubJSs") is XmlNode mqttSubJSsNode)
                {
                    foreach (XmlElement topicElem in mqttSubJSsNode.SelectNodes("Topic"))
                    {
                        MqttSubJS subJS = new MqttSubJS
                        {
                            TopicName     = topicElem.GetAttribute("TopicName"),
                            QosLevel      = (MqttQos)topicElem.GetAttrAsInt("QosLevel"),
                            CnlCnt        = topicElem.GetAttrAsInt("CnlCnt", 1),
                            JSHandlerPath = topicElem.GetAttribute("JSHandlerPath")
                        };

                        if (subJS.LoadJSHandler())
                        {
                            SubJSs.Add(subJS);
                        }
                    }
                }

                if (xmlDoc.DocumentElement.SelectSingleNode("MqttPubTopics") is XmlNode mqttPubTopicsNode)
                {
                    foreach (XmlElement topicElem in mqttPubTopicsNode.SelectNodes("Topic"))
                    {
                        PubTopics.Add(new MqttPubTopic
                        {
                            TopicName        = topicElem.GetAttribute("TopicName"),
                            QosLevel         = (MqttQos)topicElem.GetAttrAsInt("QosLevel"),
                            Retain           = topicElem.GetAttrAsBool("Retain"),
                            NumCnl           = topicElem.GetAttrAsInt("NumCnl"),
                            PubBehavior      = topicElem.GetAttrAsEnum <PubBehavior>("PubBehavior"),
                            DecimalSeparator = topicElem.GetAttribute("NDS"),
                            Prefix           = topicElem.GetAttribute("Prefix"),
                            Suffix           = topicElem.GetAttribute("Suffix")
                        });
                    }
                }

                if (xmlDoc.DocumentElement.SelectSingleNode("MqttPubCmds") is XmlNode mqttPubCmdsNode)
                {
                    foreach (XmlElement topicElem in mqttPubCmdsNode.SelectNodes("Topic"))
                    {
                        PubCmds.Add(new MqttPubCmd
                        {
                            TopicName = topicElem.GetAttribute("TopicName"),
                            QosLevel  = (MqttQos)topicElem.GetAttrAsInt("QosLevel"),
                            Retain    = false,
                            NumCmd    = topicElem.GetAttrAsInt("NumCmd")
                        });
                    }
                }

                if (xmlDoc.DocumentElement.SelectSingleNode("MqttSubCmds") is XmlNode mqttSubCmdsNode)
                {
                    foreach (XmlElement topicElem in mqttSubCmdsNode.SelectNodes("Topic"))
                    {
                        SubCmds.Add(new MqttSubCmd
                        {
                            TopicName  = topicElem.GetAttribute("TopicName"),
                            QosLevel   = (MqttQos)topicElem.GetAttrAsInt("QosLevel"),
                            CmdType    = topicElem.GetAttrAsEnum <CmdType>("CmdType"),
                            IDUser     = topicElem.GetAttrAsInt("IDUser"),
                            NumCnlCtrl = topicElem.GetAttrAsInt("NumCnlCtrl"),
                        });
                    }
                }

                errMsg = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg = CommPhrases.LoadKpSettingsError + ": " + ex.Message;
                return(false);
            }
        }