/// <summary> /// Load XML commands from the file. /// </summary> /// <param name="fileName"></param> /// <returns></returns> public byte[][] PduToMessages(GXDLMSXmlPdu pdu) { List <byte[]> messages = new List <byte[]>(); if (pdu.Command == Command.Snrm) { messages.Add(pdu.Data); } else if (pdu.Command == Command.Ua) { messages.Add(pdu.Data); } else if (pdu.Command == Command.DisconnectRequest) { messages.Add(GXDLMS.GetHdlcFrame(Settings, (byte)Command.DisconnectRequest, new GXByteBuffer(pdu.Data))); } else { GXByteBuffer reply; if (Settings.InterfaceType == InterfaceType.WRAPPER) { if (Ciphering.Security != (byte)Security.None) { GXDLMSLNParameters p = new GXDLMSLNParameters(this, Settings, 0, pdu.Command, 0x0, null, null, 0xff, Command.None); reply = new GXByteBuffer(GXDLMS.Cipher0(p, pdu.Data)); } else { reply = new GXByteBuffer(pdu.Data); } } else { if (Ciphering.Security != (byte)Security.None) { GXDLMSLNParameters p = new GXDLMSLNParameters(this, Settings, 0, pdu.Command, 0x0, null, null, 0xff, Command.None); byte[] tmp = GXDLMS.Cipher0(p, pdu.Data); reply = new GXByteBuffer((UInt16)(3 + tmp.Length)); reply.Set(GXCommon.LLCSendBytes); reply.Set(tmp); } else { reply = new GXByteBuffer((UInt16)(3 + pdu.Data.Length)); reply.Set(GXCommon.LLCSendBytes); reply.Set(pdu.Data); } } byte frame = 0; while (reply.Position != reply.Size) { if (Settings.InterfaceType == Enums.InterfaceType.WRAPPER) { messages.Add(GXDLMS.GetWrapperFrame(Settings, pdu.Command, reply)); } else if (Settings.InterfaceType == Enums.InterfaceType.HDLC) { if (pdu.Command == Command.Aarq) { frame = 0x10; } else if (pdu.Command == Command.Aare) { frame = 0x30; } else if (pdu.Command == Command.EventNotification) { frame = 0x13; } messages.Add(GXDLMS.GetHdlcFrame(Settings, frame, reply)); if (reply.Position != reply.Size) { frame = Settings.NextSend(false); } } else if (Settings.InterfaceType == Enums.InterfaceType.PDU) { messages.Add(reply.Array()); break; } else { throw new ArgumentOutOfRangeException("InterfaceType"); } } } return(messages.ToArray()); }
private List <GXDLMSXmlPdu> Load(XmlDocument doc, GXXmlLoadSettings settings) { //Remove comments. List <XmlNode> comments = new List <XmlNode>(); foreach (XmlNode node in doc.SelectNodes("//comment()")) { comments.Add(node); } foreach (XmlNode node in comments) { node.ParentNode.RemoveChild(node); } List <GXDLMSXmlPdu> actions = new List <GXDLMSXmlPdu>(); string description = null, error = null, errorUrl = null, sleep = null; foreach (XmlNode m1 in doc.ChildNodes) { if (m1.NodeType == XmlNodeType.Element) { if (m1.Name == "AssociationRequest") { GXDLMSXmlSettings s = new GXDLMSXmlSettings(translator.OutputType, translator.Hex, translator.ShowStringAsHex, translator.tagsByName); s.settings.ClientAddress = Settings.ClientAddress; s.settings.ServerAddress = Settings.ServerAddress; ((GXCiphering)s.settings.Cipher).TestMode = this.Ciphering.TestMode; byte[] reply = translator.XmlToPdu(m1.OuterXml, s); GXDLMSXmlPdu p = new GXDLMSXmlPdu(s.command, m1, reply); actions.Add(p); return(actions); } foreach (XmlNode node in m1.ChildNodes) { if (node.NodeType == XmlNodeType.Element) { if (node.Name == "Description") { description = node.Value; continue; } if (node.Name == "Error") { error = node.Value; continue; } if (node.Name == "ErrorUrl") { errorUrl = node.Value; continue; } if (node.Name == "Sleep") { sleep = node.Value; continue; } if (settings != null && node.Name == "GetRequest") { if (settings.Start != DateTime.MinValue && settings.End != DateTime.MinValue) { foreach (XmlNode n1 in node.ChildNodes) { if (n1.Name == "GetRequestNormal") { foreach (XmlNode n2 in n1.ChildNodes) { if (n2.Name == "AccessSelection") { foreach (XmlNode n3 in n2.ChildNodes) { if (n3.Name == "AccessSelector") { if (n3.Attributes["Value"].Value != "1") { break; } } else if (n3.Name == "AccessParameters") { foreach (XmlNode n4 in n3.ChildNodes) { if (n4.Name == "Structure") { bool start = true; foreach (XmlNode n5 in n4.ChildNodes) { if (n5.Name == "OctetString") { if (start) { GXByteBuffer bb = new GXByteBuffer(); GXCommon.SetData(this.Settings, bb, DataType.OctetString, settings.Start); n5.Attributes["Value"].Value = bb.ToHex(false, 2); start = false; } else { GXByteBuffer bb = new GXByteBuffer(); GXCommon.SetData(this.Settings, bb, DataType.OctetString, settings.End); n5.Attributes["Value"].Value = bb.ToHex(false, 2); break; } } } } break; } break; } } break; } } break; } } } } GXDLMSXmlSettings s = new GXDLMSXmlSettings(translator.OutputType, translator.ShowStringAsHex, translator.Hex, translator.tagsByName);; s.settings.ClientAddress = Settings.ClientAddress; s.settings.ServerAddress = Settings.ServerAddress; byte[] reply = translator.XmlToPdu(node.OuterXml, s); if (s.command == Command.Snrm && !s.settings.IsServer) { Settings.Limits.MaxInfoTX = s.settings.Limits.MaxInfoTX; Settings.Limits.MaxInfoRX = s.settings.Limits.MaxInfoRX; Settings.Limits.WindowSizeRX = s.settings.Limits.WindowSizeRX; Settings.Limits.WindowSizeTX = s.settings.Limits.WindowSizeTX; } else if (s.command == Command.Ua && s.settings.IsServer) { Settings.Limits.MaxInfoTX = s.settings.Limits.MaxInfoTX; Settings.Limits.MaxInfoRX = s.settings.Limits.MaxInfoRX; Settings.Limits.WindowSizeRX = s.settings.Limits.WindowSizeRX; Settings.Limits.WindowSizeTX = s.settings.Limits.WindowSizeTX; } if (s.template) { reply = null; } GXDLMSXmlPdu p = new GXDLMSXmlPdu(s.command, node, reply); if (description != "") { p.Description = description; } if (error != "") { p.Error = error; } if (errorUrl != "") { p.ErrorUrl = errorUrl; } if (!string.IsNullOrEmpty(sleep)) { p.Sleep = int.Parse(sleep); } actions.Add(p); } } } } return(actions); }
/// <summary> /// Load XML commands from the file. /// </summary> /// <param name="fileName"></param> /// <returns></returns> public byte[][] PduToMessages(GXDLMSXmlPdu pdu) { List <byte[]> messages = new List <byte[]>(); if (pdu.Command == Command.Snrm) { messages.Add(pdu.Data); } else if (pdu.Command == Command.Ua) { messages.Add(pdu.Data); } else if (pdu.Command == Command.DisconnectRequest) { messages.Add(GXDLMS.GetHdlcFrame(Settings, (byte)Command.DisconnectRequest, new GXByteBuffer(pdu.Data))); } else { GXByteBuffer reply; if (Settings.InterfaceType == Enums.InterfaceType.WRAPPER) { reply = new GXByteBuffer(pdu.Data); } else { reply = new GXByteBuffer((UInt16)(3 + pdu.Data.Length)); reply.Set(GXCommon.LLCSendBytes); reply.Set(pdu.Data); } while (reply.Position != reply.Size) { if (Settings.InterfaceType == Enums.InterfaceType.WRAPPER) { messages.Add(GXDLMS.GetWrapperFrame(Settings, reply)); } else if (Settings.InterfaceType == Enums.InterfaceType.HDLC) { byte frame = 0; if (pdu.Command == Command.Aarq) { frame = 0x10; } else if (pdu.Command == Command.Aare) { frame = 0x30; } else if (pdu.Command == Command.EventNotification) { frame = 0x13; } messages.Add(GXDLMS.GetHdlcFrame(Settings, frame, reply)); if (reply.Position != reply.Size) { if (Settings.IsServer || pdu.Command == Command.SetRequest || pdu.Command == Command.MethodRequest) { frame = 0; } else { frame = Settings.NextSend(false); } } } else if (Settings.InterfaceType == Enums.InterfaceType.PDU) { messages.Add(reply.Array()); break; } else { throw new ArgumentOutOfRangeException("InterfaceType"); } } } return(messages.ToArray()); }
private List <GXDLMSXmlPdu> Load(XmlDocument doc) { //Remove comments. List <XmlNode> comments = new List <XmlNode>(); foreach (XmlNode node in doc.SelectNodes("//comment()")) { comments.Add(node); } foreach (XmlNode node in comments) { node.ParentNode.RemoveChild(node); } List <GXDLMSXmlPdu> actions = new List <GXDLMSXmlPdu>(); string description = null, error = null, errorUrl = null, sleep = null; foreach (XmlNode m1 in doc.ChildNodes) { if (m1.NodeType == XmlNodeType.Element) { foreach (XmlNode node in m1.ChildNodes) { if (node.NodeType == XmlNodeType.Element) { if (node.Name == "Description") { description = node.Value; continue; } if (node.Name == "Error") { error = node.Value; continue; } if (node.Name == "ErrorUrl") { errorUrl = node.Value; continue; } if (node.Name == "Sleep") { sleep = node.Value; continue; } GXDLMSXmlSettings s = new GXDLMSXmlSettings(translator.OutputType, translator.Hex, translator.ShowStringAsHex, translator.tagsByName);; s.settings.ClientAddress = Settings.ClientAddress; s.settings.ServerAddress = Settings.ServerAddress; byte[] reply = translator.XmlToPdu(node.OuterXml, s); if (s.command == Command.Snrm && !s.settings.IsServer) { Settings.Limits.MaxInfoTX = s.settings.Limits.MaxInfoTX; Settings.Limits.MaxInfoRX = s.settings.Limits.MaxInfoRX; Settings.Limits.WindowSizeRX = s.settings.Limits.WindowSizeRX; Settings.Limits.WindowSizeTX = s.settings.Limits.WindowSizeTX; } else if (s.command == Command.Ua && s.settings.IsServer) { Settings.Limits.MaxInfoTX = s.settings.Limits.MaxInfoTX; Settings.Limits.MaxInfoRX = s.settings.Limits.MaxInfoRX; Settings.Limits.WindowSizeRX = s.settings.Limits.WindowSizeRX; Settings.Limits.WindowSizeTX = s.settings.Limits.WindowSizeTX; } if (s.template) { reply = null; } GXDLMSXmlPdu p = new GXDLMSXmlPdu(s.command, node, reply); if (description != "") { p.Description = description; } if (error != "") { p.Error = error; } if (errorUrl != "") { p.ErrorUrl = errorUrl; } if (!string.IsNullOrEmpty(sleep)) { p.Sleep = int.Parse(sleep); } actions.Add(p); } } } } return(actions); }
private List <GXDLMSXmlPdu> Load(XmlDocument doc) { List <GXDLMSXmlPdu> actions = new List <GXDLMSXmlPdu>(); string description = null, error = null; foreach (XmlNode m1 in doc.ChildNodes) { if (m1.NodeType == XmlNodeType.Element) { foreach (XmlNode node in m1.ChildNodes) { if (node.NodeType == XmlNodeType.Element) { if (node.Name == "Description") { description = node.Value; continue; } if (node.Name == "Error") { error = node.Value; continue; } GXDLMSXmlSettings s = new GXDLMSXmlSettings(translator.OutputType, translator.Hex, translator.ShowStringAsHex, translator.tagsByName);; s.settings.ClientAddress = Settings.ClientAddress; s.settings.ServerAddress = Settings.ServerAddress; byte[] reply = translator.XmlToPdu(node.OuterXml, s); if (s.command == Command.Snrm && !s.settings.IsServer) { Settings.Limits.MaxInfoTX = s.settings.Limits.MaxInfoTX; Settings.Limits.MaxInfoRX = s.settings.Limits.MaxInfoRX; Settings.Limits.WindowSizeRX = s.settings.Limits.WindowSizeRX; Settings.Limits.WindowSizeTX = s.settings.Limits.WindowSizeTX; } else if (s.command == Command.Ua && s.settings.IsServer) { Settings.Limits.MaxInfoTX = s.settings.Limits.MaxInfoTX; Settings.Limits.MaxInfoRX = s.settings.Limits.MaxInfoRX; Settings.Limits.WindowSizeRX = s.settings.Limits.WindowSizeRX; Settings.Limits.WindowSizeTX = s.settings.Limits.WindowSizeTX; } if (s.template) { reply = null; } GXDLMSXmlPdu p = new GXDLMSXmlPdu(s.command, node, reply); if (description != "") { p.Description = description; } if (error != "") { p.Error = error; } actions.Add(p); } } } } return(actions); }