private void SubFunc(Perform p, SubRec sr) { if (p.art == Perform.Art.create) { return; } MqSite ms = _sites.FirstOrDefault(z => z.Owner == p.src); MqClient client; if (ms != null) { ms.Dispose(); _sites.Remove(ms); } if (p.art == Perform.Art.changedField || p.art == Perform.Art.subscribe) { var uri = p.src.GetField("MQTT.uri").Value as string; if (string.IsNullOrEmpty(uri)) { return; } Uri uUri; try { uUri = new Uri(uri, UriKind.Absolute); } catch (Exception ex) { Log.Warning("{0}.MQTT.uri = {1} - {2}", p.src.path, uri, ex.Message); return; } string uName, uPass; if (!string.IsNullOrEmpty(uUri.UserInfo)) { var uia = uUri.UserInfo.Split(':'); uName = uia[0]; uPass = uia.Length > 1 ? uia[1] : null; } else { uName = null; uPass = null; } var cid = "MQTT://" + (uName == null ? string.Empty : (uName + "@")) + uUri.DnsSafeHost + (uUri.IsDefaultPort ? string.Empty : (":" + uUri.Port.ToString())); client = _clients.FirstOrDefault(z => z.Signature == cid); if (client == null) { client = new MqClient(this, uUri.DnsSafeHost, uUri.IsDefaultPort?1883:uUri.Port, uName, uPass); _clients.Add(client); } _sites.Add(new MqSite(this, client, p.src, uUri)); } }
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); } }