public override void DataReported(MqttContent Content) { string s = Encoding.UTF8.GetString(Content.Data); if (CommonTypes.TryParse(s, out bool b)) { this.value = b; this.timestamp = DateTime.Now; this.qos = Content.Header.QualityOfService; this.retain = Content.Header.Retain; } else { throw new Exception("Invalid boolean value."); } }
private void MqttClient_OnContentReceived(object Sender, MqttContent Content) { lock (this.queue) { if (this.processing) { this.queue.AddLast(Content); return; } else { this.processing = true; } } this.Process(Content); }
public override void DataReported(MqttContent Content) { string s = Encoding.UTF8.GetString(Content.Data); Match M = RegEx.Match(s); if (M.Success && CommonTypes.TryParse(M.Groups["Magnitude"].Value, out this.value, out this.nrDecimals)) { this.unit = M.Groups["Unit"].Value; this.timestamp = DateTime.Now; this.qos = Content.Header.QualityOfService; this.retain = Content.Header.Retain; } else { throw new Exception("Invalid physical quantity value."); } }
private async void MqttConnection_OnContentReceived(object Sender, MqttContent Content) { try { BinaryInput Input = Content.DataInput; byte Command = Input.ReadByte(); switch (Command) { case 0: // Hello string ApplicationName = Input.ReadString(); if (ApplicationName != this.applicationName) { break; } Player Player = this.Deserialize(Input); if (Player is null) { break; } #if LineListener Console.Out.WriteLine("Rx: HELLO(" + Player.ToString() + ")"); #endif IPEndPoint ExpectedEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork); lock (this.remotePlayersByEndpoint) { this.remotePlayersByEndpoint[ExpectedEndpoint] = Player; this.remotePlayerIPs[ExpectedEndpoint.Address] = true; this.playersById[Player.PlayerId] = Player; this.UpdateRemotePlayersLocked(); } MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerAvailable; if (h != null) { try { h(this, Player); } catch (Exception ex) { Events.Log.Critical(ex); } } break; case 1: // Interconnect ApplicationName = Input.ReadString(); if (ApplicationName != this.applicationName) { break; } Player = this.Deserialize(Input); if (Player is null) { break; } #if LineListener Console.Out.Write("Rx: INTERCONNECT(" + Player.ToString()); #endif int Index = 0; int i, c; LinkedList <Player> Players = new LinkedList <Player>(); bool LocalPlayerIncluded = false; Player.Index = Index++; Players.AddLast(Player); c = (int)Input.ReadUInt(); for (i = 0; i < c; i++) { Player = this.Deserialize(Input); if (Player is null) { #if LineListener Console.Out.Write("," + this.localPlayer.ToString()); #endif this.localPlayer.Index = Index++; LocalPlayerIncluded = true; } else { #if LineListener Console.Out.Write("," + Player.ToString()); #endif Player.Index = Index++; Players.AddLast(Player); } } #if LineListener Console.Out.WriteLine(")"); #endif if (!LocalPlayerIncluded) { break; } this.mqttConnection.Dispose(); this.mqttConnection = null; lock (this.remotePlayersByEndpoint) { this.remotePlayersByEndpoint.Clear(); this.remotePlayerIPs.Clear(); this.remotePlayersByIndex.Clear(); this.playersById.Clear(); this.remotePlayersByIndex[this.localPlayer.Index] = this.localPlayer; this.playersById[this.localPlayer.PlayerId] = this.localPlayer; foreach (Player Player2 in Players) { ExpectedEndpoint = Player2.GetExpectedEndpoint(this.p2pNetwork); this.remotePlayersByIndex[Player2.Index] = Player2; this.remotePlayersByEndpoint[ExpectedEndpoint] = Player2; this.remotePlayerIPs[ExpectedEndpoint.Address] = true; this.playersById[Player2.PlayerId] = Player2; } this.UpdateRemotePlayersLocked(); } this.State = MultiPlayerState.ConnectingPlayers; await this.StartConnecting(); break; case 2: // Bye ApplicationName = Input.ReadString(); if (ApplicationName != this.applicationName) { break; } Guid PlayerId = Input.ReadGuid(); lock (this.remotePlayersByEndpoint) { if (!this.playersById.TryGetValue(PlayerId, out Player)) { break; } #if LineListener Console.Out.WriteLine("Rx: BYE(" + Player.ToString() + ")"); #endif ExpectedEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork); this.playersById.Remove(PlayerId); this.remotePlayersByEndpoint.Remove(ExpectedEndpoint); this.remotePlayersByIndex.Remove(Player.Index); IPAddress ExpectedAddress = ExpectedEndpoint.Address; bool AddressFound = false; foreach (IPEndPoint EP in this.remotePlayersByEndpoint.Keys) { if (IPAddress.Equals(EP.Address, ExpectedAddress)) { AddressFound = true; break; } } if (!AddressFound) { this.remotePlayerIPs.Remove(ExpectedAddress); } this.UpdateRemotePlayersLocked(); } break; } } catch (Exception ex) { Log.Critical(ex); } }
private void Client_OnContentReceived(object Sender, MqttContent Content) { string Xml = System.Text.Encoding.UTF8.GetString(Content.Data); XmlDocument Doc = new XmlDocument(); try { Doc.LoadXml(Xml); } catch (Exception) { return; } XmlElement E = Doc.DocumentElement; if (E.LocalName != "log" || E.NamespaceURI != MqttEventSink.NamespaceEventLogging) { return; } XmlElement E2; List <KeyValuePair <string, object> > Tags = new List <KeyValuePair <string, object> >(); DateTime Timestamp = XML.Attribute(E, "timestamp", DateTime.MinValue); string EventId = XML.Attribute(E, "id"); EventType Type = (EventType)XML.Attribute(E, "type", EventType.Informational); EventLevel Level = (EventLevel)XML.Attribute(E, "level", EventLevel.Minor); string Object = XML.Attribute(E, "object"); string Actor = XML.Attribute(E, "subject"); string Facility = XML.Attribute(E, "facility"); string Module = XML.Attribute(E, "module"); string Message = string.Empty; string StackTrace = string.Empty; foreach (XmlNode N in E.ChildNodes) { switch (N.LocalName) { case "message": Message = N.InnerText; break; case "tag": E2 = (XmlElement)N; string TagName = XML.Attribute(E2, "name"); string TagValue = XML.Attribute(E2, "value"); string TagType = XML.Attribute(E2, "type"); object TagValueParsed = TagValue; switch (TagType) { case "xs:anyURI": Uri URI; if (Uri.TryCreate(TagValue, UriKind.Absolute, out URI)) { TagValueParsed = URI; } break; case "xs:boolean": bool b; if (CommonTypes.TryParse(TagValue, out b)) { TagValueParsed = b; } break; case "xs:unsignedByte": byte ui8; if (byte.TryParse(TagValue, out ui8)) { TagValueParsed = ui8; } break; case "xs:short": short i16; if (short.TryParse(TagValue, out i16)) { TagValueParsed = i16; } break; case "xs:int": int i32; if (int.TryParse(TagValue, out i32)) { TagValueParsed = i32; } break; case "xs:long": long i64; if (long.TryParse(TagValue, out i64)) { TagValueParsed = i64; } break; case "xs:byte": sbyte i8; if (sbyte.TryParse(TagValue, out i8)) { TagValueParsed = i8; } break; case "xs:unsignedShort": ushort ui16; if (ushort.TryParse(TagValue, out ui16)) { TagValueParsed = ui16; } break; case "xs:unsignedInt": uint ui32; if (uint.TryParse(TagValue, out ui32)) { TagValueParsed = ui32; } break; case "xs:unsignedLong": ulong ui64; if (ulong.TryParse(TagValue, out ui64)) { TagValueParsed = ui64; } break; case "xs:decimal": decimal d; if (CommonTypes.TryParse(TagValue, out d)) { TagValueParsed = d; } break; case "xs:double": double d2; if (CommonTypes.TryParse(TagValue, out d2)) { TagValueParsed = d2; } break; case "xs:float": float f; if (CommonTypes.TryParse(TagValue, out f)) { TagValueParsed = f; } break; case "xs:time": TimeSpan TS; if (TimeSpan.TryParse(TagValue, out TS)) { TagValueParsed = TS; } break; case "xs:date": case "xs:dateTime": DateTime DT; if (XML.TryParse(TagValue, out DT)) { TagValueParsed = DT; } break; case "xs:string": case "xs:language": default: break; } Tags.Add(new KeyValuePair <string, object>(TagName, TagValueParsed)); break; case "stackTrace": StackTrace = N.InnerText; break; } } if (string.IsNullOrEmpty(Facility)) { Facility = Content.Topic; } Event Event = new Event(Timestamp, Type, Message, Object, Actor, EventId, Level, Facility, Module, StackTrace, Tags.ToArray()); EventEventHandler h = this.OnEvent; if (h == null) { Log.Event(Event); } else { try { h(this, new EventEventArgs(Content, Event)); } catch (Exception ex) { Log.Critical(ex); } } }
private Data FindDataType(MqttContent Content) { try { string s = Encoding.UTF8.GetString(Content.Data); if (long.TryParse(s, out long i)) { this.RemoveWarning(); return(new IntegerData(this, i)); } if (CommonTypes.TryParse(s, out double x, out byte NrDecimals)) { this.RemoveWarning(); return(new FloatingPointData(this, x, NrDecimals)); } if (CommonTypes.TryParse(s, out bool b)) { this.RemoveWarning(); return(new BooleanData(this, b)); } if (System.Guid.TryParse(s, out Guid Guid)) { this.RemoveWarning(); return(new GuidData(this, Guid)); } if (System.TimeSpan.TryParse(s, out TimeSpan TimeSpan)) { this.RemoveWarning(); return(new TimeSpanData(this, TimeSpan)); } if (System.DateTime.TryParse(s, out DateTime DateTime)) { this.RemoveWarning(); return(new DateTimeData(this, DateTime)); } if (CommonTypes.TryParseRfc822(s, out DateTimeOffset DateTimeOffset)) { this.RemoveWarning(); return(new DateTimeOffsetData(this, DateTimeOffset)); } if (Waher.Content.Duration.TryParse(s, out Duration Duration)) { this.RemoveWarning(); return(new DurationData(this, Duration)); } if (s.StartsWith("<") && s.EndsWith(">")) { try { XmlDocument Doc = new XmlDocument(); Doc.LoadXml(s); this.RemoveWarning(); return(new XmlData(this, s, Doc)); } catch (Exception) { // Not XML } } if ((s.StartsWith("{") && s.EndsWith("}")) || (s.StartsWith("[") && s.EndsWith("]"))) { try { object Obj = JSON.Parse(s); this.RemoveWarning(); return(new JsonData(this, s, Obj)); } catch (Exception) { // Not JSON } } if (s.IndexOfAny(controlCharacters) >= 0) { this.RemoveWarning(); return(new BinaryData(this, Content.Data)); } if (Base64Data.RegEx.IsMatch(s)) { this.RemoveWarning(); byte[] Data = Convert.FromBase64String(s.Trim()); if (Data.Length > 0) { return(new Base64Data(this, Data)); } } if (HexStringData.RegEx.IsMatch(s)) { this.RemoveWarning(); byte[] Data = Security.Hashes.StringToBinary(s.Trim()); if (Data.Length > 0) { return(new HexStringData(this, Data)); } } Match M = QuantityData.RegEx.Match(s); if (M.Success && CommonTypes.TryParse(M.Groups["Magnitude"].Value, out x, out NrDecimals)) { this.RemoveWarning(); return(new QuantityData(this, x, NrDecimals, M.Groups["Unit"].Value)); } if (!this.hasWarning.HasValue || !this.hasWarning.Value) { this.node?.LogWarningAsync("Format", "Unrecognized string format: " + s); this.hasWarning = true; } return(new StringData(this, s)); } catch (Exception) { return(new BinaryData(this, Content.Data)); } }
public void DataReported(MqttContent Content) { int Len = Content.Data.Length; if (Len == 0) { this.data = null; return; } this.dataCount += Len; try { if (this.data == null) { this.data = this.FindDataType(Content); } else { this.data.DataReported(Content); } this.SetOk(); } catch (Exception) { try { this.data = this.FindDataType(Content); } catch (Exception ex2) { this.Exception(ex2); return; } } if (this.broker.Client?.HasSniffers ?? false) { this.data.SnifferOutput(this.broker.Client); } if (Types.TryGetModuleParameter("Sensor", out object Obj) && Obj is SensorServer SensorServer) { try { InternalReadoutRequest Request = new InternalReadoutRequest(string.Empty, new ThingReference[] { this.node }, FieldType.All, null, DateTime.MinValue, DateTime.MaxValue, (sender, e) => { SensorServer.NewMomentaryValues(this.node, e.Fields); MqttTopic Current = this; MqttTopic Parent = this.parent; while (Parent != null) { foreach (Field F in e.Fields) { if (F.Name == "Value") { F.Name = Current.localTopic; } else { F.Name = Current.localTopic + ", " + F.Name; } SensorServer.NewMomentaryValues(Parent.node, F); } Current = Parent; Parent = Parent.parent; } return(Task.CompletedTask); }, (sender, e) => { return(Task.CompletedTask); }, null); this.StartReadout(Request); } catch (Exception ex) { this.Exception(ex); } } }
public async Task DataReceived(MqttContent Content) { MqttTopic Topic = await this.GetTopic(Content.Topic, true); Topic?.DataReported(Content); }
internal EventEventArgs(MqttContent e, Event Event) : base(e.Header, e.Topic, e.Data) { this.ev = Event; }
public abstract void DataReported(MqttContent Content);