Exemple #1
0
 public MqClient(MQTTPl pl, string host, int port, string uName, string uPass)
 {
     _tOut     = new Timer(new TimerCallback(TimeOut));
     _pl       = pl;
     _host     = host;
     _port     = port;
     _uName    = uName;
     _uPass    = uPass;
     Signature = "MQTT://" + (_uName == null ? string.Empty : (_uName + "@")) + _host + (_port == 1883 ? string.Empty : (":" + _port.ToString()));
     Sites     = new List <MqSite>();
     status    = Status.Disconnected;
     Connect();
 }
Exemple #2
0
        public MqSite(MQTTPl pl, MqClient client, Topic owner, Uri uUri)
        {
            this.Client = client;
            this.Owner  = owner;
            this._pl    = pl;
            this._uri   = uUri;

            _subEn      = ReadFlag("MQTT.subscribe", true);
            _pubEn      = ReadFlag("MQTT.publish", true);
            _retainedEn = ReadFlag("MQTT.retained", false);

            remotePath = _uri.PathAndQuery + _uri.Fragment;
            var sl = remotePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            remotePrefix = string.Empty;
            _mask        = SubRec.SubMask.Value;
            for (int i = 0; i < sl.Length; i++)
            {
                if (sl[i] == "+")
                {
                    _mask |= SubRec.SubMask.Chldren;
                    break;
                }
                if (sl[i] == "#")
                {
                    _mask |= SubRec.SubMask.All;
                    break;
                }
                remotePrefix = remotePrefix + "/" + sl[i];
            }
            Client.Sites.Add(this);
            if (Client.status == MqClient.Status.Connected)
            {
                this.Connected();
            }
            var act = this.Owner.GetField("Action");

            JSC.JSValue txt;
            if (act == null || !act.Any(z => z.Value.ValueType == JSC.JSValueType.Object && (txt = z.Value["name"]).ValueType == JSC.JSValueType.String && (txt.Value as string) == "MQTT.Reconnect"))
            {
                int       i;
                JSL.Array act_n;
                if (act == null)
                {
                    act_n = new JSL.Array(1);
                    i     = 0;
                }
                else
                {
                    int j = act.Count();
                    act_n = new JSL.Array(j + 1);
                    for (i = 0; i < j; i++)
                    {
                        act_n[i] = act[i.ToString()];
                    }
                }
                var r_a = JSC.JSObject.CreateObject();
                r_a["name"] = "MQTT.Reconnect";
                r_a["text"] = "Reconnect MQTT connection";
                act_n[i]    = r_a;
                this.Owner.SetField("Action", act_n);
            }
        }