Exemplo n.º 1
0
        public override void OnAddedToCommLine()
        {
            List <TagGroup> tagGroups  = new List <TagGroup> ();
            TagGroup        tagGroup   = new TagGroup("GroupMQTT");
            TagGroup        tagGroupJS = new TagGroup("GoupJS");

            XmlDocument xmlDoc   = new XmlDocument();
            string      filename = ReqParams.CmdLine.Trim();

            xmlDoc.Load(AppDirs.ConfigDir + filename);

            XmlNode MQTTSubTopics = xmlDoc.DocumentElement.SelectSingleNode("MqttSubTopics");
            XmlNode MQTTPubTopics = xmlDoc.DocumentElement.SelectSingleNode("MqttPubTopics");
            XmlNode MQTTPubCmds   = xmlDoc.DocumentElement.SelectSingleNode("MqttPubCmds");
            XmlNode MQTTSubCmds   = xmlDoc.DocumentElement.SelectSingleNode("MqttSubCmds");
            XmlNode MQTTSubJSs    = xmlDoc.DocumentElement.SelectSingleNode("MqttSubJSs");
            XmlNode RapSrvCnf     = xmlDoc.DocumentElement.SelectSingleNode("RapSrvCnf");
            XmlNode MQTTSettings  = xmlDoc.DocumentElement.SelectSingleNode("MqttParams");

            CommSettings cs = new CommSettings()
            {
                ServerHost = RapSrvCnf.Attributes.GetNamedItem("ServerHost").Value,
                ServerPort = Convert.ToInt32(RapSrvCnf.Attributes.GetNamedItem("ServerPort").Value),
                ServerUser = RapSrvCnf.Attributes.GetNamedItem("ServerUser").Value,
                ServerPwd  = RapSrvCnf.Attributes.GetNamedItem("ServerPwd").Value
            };

            RSrv = new RapSrvEx(cs);
            RSrv.Conn();
            MQTTPTs  = new List <MQTTPubTopic> ();
            MQTTCmds = new List <MQTTPubCmd> ();

            foreach (XmlElement MqttPTCnf in MQTTPubTopics)
            {
                MQTTPubTopic MqttPT = new MQTTPubTopic()
                {
                    NumCnl                 = Convert.ToInt32(MqttPTCnf.GetAttribute("NumCnl")),
                    QosLevels              = (MqttQos)Convert.ToByte(MqttPTCnf.GetAttribute("QosLevel")),
                    TopicName              = MqttPTCnf.GetAttribute("TopicName"),
                    PubBehavior            = MqttPTCnf.GetAttribute("PubBehavior"),
                    NumberDecimalSeparator = MqttPTCnf.GetAttribute("NDS"),
                    Value = 0
                };
                MQTTPTs.Add(MqttPT);
            }

            foreach (XmlElement MqttPTCnf in MQTTPubCmds)
            {
                MQTTPubCmd MqttPTCmd = new MQTTPubCmd()
                {
                    NumCmd    = MqttPTCnf.GetAttrAsInt("NumCmd"),
                    QosLevels = (MqttQos)Convert.ToByte(MqttPTCnf.GetAttribute("QosLevel")),
                    Retain    = false,
                    TopicName = MqttPTCnf.GetAttribute("TopicName")
                };
                MQTTCmds.Add(MqttPTCmd);
            }

            sp = new SubscribePacket();
            int i     = 0;
            int spCnt = MQTTSubTopics.ChildNodes.Count;

            spCnt += MQTTSubCmds.ChildNodes.Count;
            spCnt += MQTTSubJSs.ChildNodes.Count;

            sp.Topics    = new string[spCnt];
            sp.QosLevels = new MqttQos[spCnt];

            foreach (XmlElement elemGroupElem in MQTTSubTopics.ChildNodes)
            {
                sp.Topics [i]    = elemGroupElem.GetAttribute("TopicName");
                sp.QosLevels [i] = (MqttQos)Convert.ToByte(elemGroupElem.GetAttribute("QosLevel"));
                KPTag KPt = new KPTag()
                {
                    Signal = i + 1,
                    Name   = sp.Topics [i],
                    CnlNum = Convert.ToInt32(elemGroupElem.GetAttribute("NumCnl"))
                };
                tagGroup.KPTags.Add(KPt);
                i++;
            }
            tagGroups.Add(tagGroup);
            InitKPTags(tagGroups);



            SubCmds = new List <MQTTSubCmd> ();

            foreach (XmlElement elemGroupElem in MQTTSubCmds.ChildNodes)
            {
                sp.Topics [i]    = elemGroupElem.GetAttribute("TopicName");
                sp.QosLevels [i] = (MqttQos)Convert.ToByte(elemGroupElem.GetAttribute("QosLevel"));
                MQTTSubCmd cmd = new MQTTSubCmd()
                {
                    TopicName  = sp.Topics[i],
                    CmdNum     = elemGroupElem.GetAttrAsInt("NumCmd", 0),
                    CmdType    = elemGroupElem.GetAttribute("CmdType"),
                    KPNum      = elemGroupElem.GetAttrAsInt("KPNum", 0),
                    IDUser     = elemGroupElem.GetAttrAsInt("IDUser", 0),
                    NumCnlCtrl = elemGroupElem.GetAttrAsInt("NumCnlCtrl", 0)
                };
                SubCmds.Add(cmd);
                i++;
            }

            SubJSs = new List <MQTTSubJS>();

            foreach (XmlElement elemGroupElem in MQTTSubJSs.ChildNodes)
            {
                sp.Topics[i]    = elemGroupElem.GetAttribute("TopicName");
                sp.QosLevels[i] = (MqttQos)Convert.ToByte(elemGroupElem.GetAttribute("QosLevel"));
                MQTTSubJS msjs = new MQTTSubJS()
                {
                    TopicName     = sp.Topics[i],
                    CnlCnt        = elemGroupElem.GetAttrAsInt("CnlCnt", 1),
                    JSHandlerPath = elemGroupElem.GetAttrAsString("JSHandlerPath", "")
                };
                if (msjs.LoadJSHandler())
                {
                    SubJSs.Add(msjs);
                    i++;
                }
            }



            connArgs              = new MqttConnectionArgs();
            connArgs.ClientId     = MQTTSettings.Attributes.GetNamedItem("ClientID").Value;
            connArgs.Hostname     = MQTTSettings.Attributes.GetNamedItem("Hostname").Value;
            connArgs.Port         = Convert.ToInt32(MQTTSettings.Attributes.GetNamedItem("Port").Value);
            connArgs.Username     = MQTTSettings.Attributes.GetNamedItem("UserName").Value;
            connArgs.Password     = MQTTSettings.Attributes.GetNamedItem("Password").Value;
            connArgs.Keepalive    = TimeSpan.FromSeconds(60);
            connArgs.ReadTimeout  = TimeSpan.FromSeconds(10);
            connArgs.WriteTimeout = TimeSpan.FromSeconds(10);

            this.Persistence  = new InMemoryPersistence();
            Transport         = new TcpTransport(connArgs.Hostname, connArgs.Port);
            Transport.Version = connArgs.Version;
            Transport.SetTimeouts(connArgs.ReadTimeout, connArgs.WriteTimeout);

            Send(MakeConnectMessage(connArgs));
            ReceiveConnack();
            ResumeOutgoingFlows();

            if (sp.Topics.Length > 0)
            {
                Subscribe(sp);
            }

            WriteToLog(Localization.UseRussian ? "Инициализация линии связи выполнена успешно." : "Communication line initialized successfully");
        }
Exemplo n.º 2
0
        public override void OnAddedToCommLine()
        {
            List <TagGroup> tagGroups  = new List <TagGroup>();
            TagGroup        tagGroup   = new TagGroup("GroupMQTT");
            TagGroup        tagGroupJS = new TagGroup("GoupJS");

            XmlDocument xmlDoc   = new XmlDocument();
            string      filename = ReqParams.CmdLine.Trim();

            xmlDoc.Load(AppDirs.ConfigDir + filename);

            XmlNode MQTTSubTopics = xmlDoc.DocumentElement.SelectSingleNode("MqttSubTopics");
            XmlNode MQTTPubTopics = xmlDoc.DocumentElement.SelectSingleNode("MqttPubTopics");
            XmlNode MQTTPubCmds   = xmlDoc.DocumentElement.SelectSingleNode("MqttPubCmds");
            XmlNode MQTTSubCmds   = xmlDoc.DocumentElement.SelectSingleNode("MqttSubCmds");
            XmlNode MQTTSubJSs    = xmlDoc.DocumentElement.SelectSingleNode("MqttSubJSs");
            XmlNode RapSrvCnf     = xmlDoc.DocumentElement.SelectSingleNode("RapSrvCnf");

            MQTTSettings = xmlDoc.DocumentElement.SelectSingleNode("MqttParams");

            CommSettings cs = new CommSettings()
            {
                ServerHost = RapSrvCnf.Attributes.GetNamedItem("ServerHost").Value,
                ServerPort = Convert.ToInt32(RapSrvCnf.Attributes.GetNamedItem("ServerPort").Value),
                ServerUser = RapSrvCnf.Attributes.GetNamedItem("ServerUser").Value,
                ServerPwd  = RapSrvCnf.Attributes.GetNamedItem("ServerPwd").Value
            };

            RSrv = new RapSrvEx(cs);
            RSrv.Conn();
            MQTTPTs  = new List <MQTTPubTopic>();
            MQTTCmds = new List <MQTTPubCmd>();

            foreach (XmlElement MqttPTCnf in MQTTPubTopics)
            {
                MQTTPubTopic MqttPT = new MQTTPubTopic()
                {
                    NumCnl                 = Convert.ToInt32(MqttPTCnf.GetAttribute("NumCnl")),
                    QosLevels              = (MqttQos)Convert.ToByte(MqttPTCnf.GetAttribute("QosLevel")),
                    TopicName              = MqttPTCnf.GetAttribute("TopicName"),
                    PubBehavior            = MqttPTCnf.GetAttribute("PubBehavior"),
                    NumberDecimalSeparator = MqttPTCnf.GetAttribute("NDS"),
                    Value = 0
                };
                MQTTPTs.Add(MqttPT);
            }

            foreach (XmlElement MqttPTCnf in MQTTPubCmds)
            {
                MQTTPubCmd MqttPTCmd = new MQTTPubCmd()
                {
                    NumCmd    = MqttPTCnf.GetAttrAsInt("NumCmd"),
                    QosLevels = (MqttQos)Convert.ToByte(MqttPTCnf.GetAttribute("QosLevel")),
                    Retain    = false,
                    TopicName = MqttPTCnf.GetAttribute("TopicName")
                };
                MQTTCmds.Add(MqttPTCmd);
            }

            sp = new SubscribePacket();
            int i     = 0;
            int spCnt = MQTTSubTopics.ChildNodes.Count;

            spCnt += MQTTSubCmds.ChildNodes.Count;
            spCnt += MQTTSubJSs.ChildNodes.Count;

            sp.Topics    = new string[spCnt];
            sp.QosLevels = new MqttQos[spCnt];

            foreach (XmlElement elemGroupElem in MQTTSubTopics.ChildNodes)
            {
                sp.Topics[i]    = elemGroupElem.GetAttribute("TopicName");
                sp.QosLevels[i] = (MqttQos)Convert.ToByte(elemGroupElem.GetAttribute("QosLevel"));
                KPTag KPt = new KPTag()
                {
                    Signal = i + 1,
                    Name   = sp.Topics[i],
                    CnlNum = Convert.ToInt32(elemGroupElem.GetAttribute("NumCnl"))
                };
                tagGroup.KPTags.Add(KPt);
                i++;
            }
            tagGroups.Add(tagGroup);
            InitKPTags(tagGroups);

            SubCmds = new List <MQTTSubCmd>();

            foreach (XmlElement elemGroupElem in MQTTSubCmds.ChildNodes)
            {
                sp.Topics[i]    = elemGroupElem.GetAttribute("TopicName");
                sp.QosLevels[i] = (MqttQos)Convert.ToByte(elemGroupElem.GetAttribute("QosLevel"));
                MQTTSubCmd cmd = new MQTTSubCmd()
                {
                    TopicName  = sp.Topics[i],
                    CmdNum     = elemGroupElem.GetAttrAsInt("NumCmd", 0),
                    CmdType    = elemGroupElem.GetAttribute("CmdType"),
                    KPNum      = elemGroupElem.GetAttrAsInt("KPNum", 0),
                    IDUser     = elemGroupElem.GetAttrAsInt("IDUser", 0),
                    NumCnlCtrl = elemGroupElem.GetAttrAsInt("NumCnlCtrl", 0)
                };
                SubCmds.Add(cmd);
                i++;
            }

            SubJSs = new List <MQTTSubJS>();

            foreach (XmlElement elemGroupElem in MQTTSubJSs.ChildNodes)
            {
                sp.Topics[i]    = elemGroupElem.GetAttribute("TopicName");
                sp.QosLevels[i] = (MqttQos)Convert.ToByte(elemGroupElem.GetAttribute("QosLevel"));
                MQTTSubJS msjs = new MQTTSubJS()
                {
                    TopicName     = sp.Topics[i],
                    CnlCnt        = elemGroupElem.GetAttrAsInt("CnlCnt", 1),
                    JSHandlerPath = elemGroupElem.GetAttrAsString("JSHandlerPath", "")
                };
                if (msjs.LoadJSHandler())
                {
                    SubJSs.Add(msjs);
                    i++;
                }
            }

            Connect(MQTTSettings);
        }