/// <summary> /// Read attribute value. /// </summary> public object Read(GXDLMSObject it, int attributeIndex) { byte[] reply = ReadDataBlock(m_Parser.Read(it.Name, 1, 1, it.ObjectType, attributeIndex)); return m_Parser.GetValue(reply, it.ObjectType, it.LogicalName, attributeIndex); }
/// <summary> /// Handle get request normal command. /// </summary> /// <param name="data">Received data.</param> private static void GetRequestNormal(GXDLMSSettings settings, byte invokeID, GXDLMSServer server, GXByteBuffer data, GXByteBuffer replyData, GXDLMSTranslatorStructure xml) { ValueEventArgs e = null; GXByteBuffer bb = new GXByteBuffer(); // Get type. ErrorCode status = ErrorCode.Ok; settings.Count = settings.Index = 0; settings.ResetBlockIndex(); // CI ObjectType ci = (ObjectType)data.GetUInt16(); byte[] ln = new byte[6]; data.Get(ln); // Attribute Id byte attributeIndex = data.GetUInt8(); // AccessSelection byte selection = data.GetUInt8(); byte selector = 0; object parameters = null; GXDataInfo info = new GXDataInfo(); if (selection != 0) { selector = data.GetUInt8(); } if (xml != null) { AppendAttributeDescriptor(xml, (int)ci, ln, attributeIndex); if (selection != 0) { info.xml = xml; xml.AppendStartTag(TranslatorTags.AccessSelection); xml.AppendLine(TranslatorTags.AccessSelector, "Value", xml.IntegerToHex(selector, 2)); xml.AppendStartTag(TranslatorTags.AccessParameters); GXCommon.GetData(settings, data, info); xml.AppendEndTag(TranslatorTags.AccessParameters); xml.AppendEndTag(TranslatorTags.AccessSelection); } return; } if (selection != 0) { parameters = GXCommon.GetData(settings, data, info); } GXDLMSObject obj = settings.Objects.FindByLN(ci, GXCommon.ToLogicalName(ln)); if (obj == null) { obj = server.NotifyFindObject(ci, 0, GXCommon.ToLogicalName(ln)); } e = new ValueEventArgs(server, obj, attributeIndex, selector, parameters); e.InvokeId = invokeID; if (obj == null) { // "Access Error : Device reports a undefined object." status = ErrorCode.UndefinedObject; } else { if (server.NotifyGetAttributeAccess(e) == AccessMode.NoAccess) { //Read Write denied. status = ErrorCode.ReadWriteDenied; } else { if (e.Target is GXDLMSProfileGeneric && attributeIndex == 2) { e.RowToPdu = GXDLMS.RowsToPdu(settings, (GXDLMSProfileGeneric)e.Target); } object value; server.NotifyRead(new ValueEventArgs[] { e }); if (e.Handled) { value = e.Value; } else { settings.Count = e.RowEndIndex - e.RowBeginIndex; value = (obj as IGXDLMSBase).GetValue(settings, e); } if (e.ByteArray) { bb.Set((byte[])value); } else { GXDLMS.AppendData(settings, obj, attributeIndex, bb, value); } server.NotifyPostRead(new ValueEventArgs[] { e }); status = e.Error; } } GXDLMSLNParameters p = new GXDLMSLNParameters(settings, e.InvokeId, Command.GetResponse, 1, null, bb, (byte)status); GXDLMS.GetLNPdu(p, replyData); if (settings.Count != settings.Index || bb.Size != bb.Position) { server.transaction = new GXDLMSLongTransaction(new ValueEventArgs[] { e }, Command.GetRequest, bb); } }
private static void HandleSetRequestNormal(GXDLMSSettings settings, GXDLMSServer server, GXByteBuffer data, byte type, GXDLMSLNParameters p, GXByteBuffer replyData, GXDLMSTranslatorStructure xml) { object value = null; GXDataInfo reply = new GXDataInfo(); // CI ObjectType ci = (ObjectType)data.GetUInt16(); byte[] ln = new byte[6]; data.Get(ln); // Attribute index. byte index = data.GetUInt8(); // Get Access Selection. data.GetUInt8(); if (type == 2) { byte lastBlock = data.GetUInt8(); p.multipleBlocks = lastBlock == 0; UInt32 blockNumber = data.GetUInt32(); if (blockNumber != settings.BlockIndex) { Debug.WriteLine("HandleSetRequest failed. Invalid block number. " + settings.BlockIndex + "/" + blockNumber); p.status = (byte)ErrorCode.DataBlockNumberInvalid; return; } settings.IncreaseBlockIndex(); int size = GXCommon.GetObjectCount(data); int realSize = data.Size - data.Position; if (size != realSize) { Debug.WriteLine("HandleSetRequest failed. Invalid block size."); p.status = (byte)ErrorCode.DataBlockUnavailable; return; } if (xml != null) { AppendAttributeDescriptor(xml, (int)ci, ln, index); xml.AppendStartTag(TranslatorTags.DataBlock); xml.AppendLine(TranslatorTags.LastBlock, "Value", xml.IntegerToHex(lastBlock, 2)); xml.AppendLine(TranslatorTags.BlockNumber, "Value", xml.IntegerToHex(blockNumber, 8)); xml.AppendLine(TranslatorTags.RawData, "Value", GXCommon.ToHex(data.Data, false, 0, data.Size)); xml.AppendEndTag(TranslatorTags.DataBlock); } return; } if (xml != null) { AppendAttributeDescriptor(xml, (int)ci, ln, index); xml.AppendStartTag(TranslatorTags.Value); GXDataInfo di = new GXDataInfo(); di.xml = xml; value = GXCommon.GetData(settings, data, di); if (!di.Complete) { value = GXCommon.ToHex(data.Data, false, data.Position, data.Size - data.Position); } else if (value is byte[]) { value = GXCommon.ToHex((byte[])value, false); } xml.AppendEndTag(TranslatorTags.Value); } if (xml != null) { return; } if (!p.multipleBlocks) { settings.ResetBlockIndex(); value = GXCommon.GetData(settings, data, reply); } GXDLMSObject obj = settings.Objects.FindByLN(ci, GXCommon.ToLogicalName(ln)); if (obj == null) { obj = server.NotifyFindObject(ci, 0, GXCommon.ToLogicalName(ln)); } // If target is unknown. if (obj == null) { // Device reports a undefined object. p.status = (byte)ErrorCode.UndefinedObject; } else { ValueEventArgs e = new ValueEventArgs(server, obj, index, 0, null); e.InvokeId = p.InvokeId; AccessMode am = server.NotifyGetAttributeAccess(e); // If write is denied. if (am != AccessMode.Write && am != AccessMode.ReadWrite) { //Read Write denied. p.status = (byte)ErrorCode.ReadWriteDenied; } else { try { if (value is byte[]) { DataType dt = (obj as IGXDLMSBase).GetDataType(index); if (dt != DataType.None && dt != DataType.OctetString && dt != DataType.Structure) { value = GXDLMSClient.ChangeType((byte[])value, dt, settings.UseUtc2NormalTime); } } e.Value = value; ValueEventArgs[] list = new ValueEventArgs[] { e }; if (p.multipleBlocks) { server.transaction = new GXDLMSLongTransaction(list, Command.GetRequest, data); } server.NotifyWrite(list); if (e.Error != 0) { p.status = (byte)e.Error; } else if (!e.Handled && !p.multipleBlocks) { (obj as IGXDLMSBase).SetValue(settings, e); server.NotifyPostWrite(list); if (e.Error != 0) { p.status = (byte)e.Error; } } p.InvokeId = e.InvokeId; } catch (Exception) { p.status = (byte)ErrorCode.HardwareFault; } } } }
/// <summary> /// Initialize server. /// </summary> /// <remarks> /// This must call after server objects are set. /// </remarks> public void Initialize() { bool association = false; Initialized = true; if (SortedItems.Count != Items.Count) { for (int pos = 0; pos != Items.Count; ++pos) { GXDLMSObject it = Items[pos]; if (this.UseLogicalNameReferencing && (string.IsNullOrEmpty(it.LogicalName) || it.LogicalName.Split('.').Length != 6)) { throw new Exception("Invalid Logical Name."); } if (it is GXDLMSProfileGeneric) { GXDLMSProfileGeneric pg = it as GXDLMSProfileGeneric; if (pg.ProfileEntries < 1) { throw new Exception("Invalid Profile Entries. Profile entries tells amount of rows in the table."); } foreach (var obj in pg.CaptureObjects) { if (obj.Value.AttributeIndex < 1) { throw new Exception("Invalid attribute index. SelectedAttributeIndex is not set for " + obj.Key.Name); } } if (pg.ProfileEntries < 1) { throw new Exception("Invalid Profile Entries. Profile entries tells amount of rows in the table."); } if (pg.CapturePeriod != 0) { GXProfileGenericUpdater p = new GXProfileGenericUpdater(this, pg); Thread thread = new Thread(new ThreadStart(p.UpdateProfileGenericData)); thread.IsBackground = true; thread.Start(); } } else if ((it is GXDLMSAssociationShortName && !this.UseLogicalNameReferencing) || (it is GXDLMSAssociationLogicalName && this.UseLogicalNameReferencing)) { association = true; } else if (!(it is IGXDLMSBase))//Remove unsupported items. { Debug.WriteLine(it.ObjectType.ToString() + " not supported."); Items.RemoveAt(pos); --pos; } } if (!association) { if (UseLogicalNameReferencing) { GXDLMSAssociationLogicalName it = new GXDLMSAssociationLogicalName(); it.ObjectList = this.Items; Items.Add(it); } else { GXDLMSAssociationShortName it = new GXDLMSAssociationShortName(); it.ObjectList = this.Items; Items.Add(it); } } //Arrange items by Short Name. ushort sn = 0xA0; if (!this.UseLogicalNameReferencing) { SortedItems.Clear(); foreach (GXDLMSObject it in Items) { if (it is GXDLMSAssociationShortName) { it.ShortName = 0xFA00; } //Generate Short Name if not given. if (it.ShortName == 0) { do { it.ShortName = sn; sn += 0xA0; }while (SortedItems.ContainsKey(it.ShortName)); } SortedItems.Add(it.ShortName, it); } } } }
private byte[][] HandleGetRequest() { ErrorCode error = ErrorCode.Ok; GXByteBuffer data = Reply.Data; GXByteBuffer bb = new GXByteBuffer(); short type; int index = 0; object parameters = null; // Get type. type = data.GetUInt8(); // Get invoke ID and priority. data.GetUInt8(); // GetRequest normal if (type == 1) { Settings.ResetBlockIndex(); ServerReply.Index = 0; parameters = null; // CI ObjectType ci = (ObjectType)data.GetUInt16(); byte[] ln = new byte[6]; data.Get(ln); // Attribute Id int attributeIndex = data.GetUInt8(); GXDLMSObject obj = Settings.Objects.FindByLN(ci, GXDLMSObject.ToLogicalName(ln)); if (obj == null) { obj = FindObject(ci, 0, GXDLMSObject.ToLogicalName(ln)); } if (obj == null) { // "Access Error : Device reports a undefined object." error = ErrorCode.UndefinedObject; } else { // AccessSelection int selection = data.GetUInt8(); int selector = 0; if (selection != 0) { selector = data.GetUInt8(); GXDataInfo info = new GXDataInfo(); parameters = GXCommon.GetData(data, info); } ValueEventArgs e = new ValueEventArgs(obj, attributeIndex, 0, parameters); Read(e); object value; if (e.Handled) { value = e.Value; } else { value = (obj as IGXDLMSBase).GetValue(Settings, attributeIndex, selector, parameters); } GXDLMS.AppedData(obj, attributeIndex, bb, value); } ServerReply.ReplyMessages = GXDLMS.SplitPdu(Settings, Command.GetResponse, 1, bb, error, DateTime.MinValue); } else if (type == 2) { // Get request for next data block // Get block index. index = (int)data.GetUInt32(); if (index != Settings.BlockIndex + 1) { Debug.WriteLine("handleGetRequest failed. Invalid block number. " + Settings.BlockIndex + "/" + index); ServerReply.ReplyMessages = GXDLMS.SplitPdu(Settings, Command.GetResponse, 1, bb, ErrorCode.DataBlockNumberInvalid, DateTime.MinValue); index = 0; ServerReply.Index = index; } else { Settings.IncreaseBlockIndex(); index = ServerReply.Index + 1; ServerReply.Index = index; } } else if (type == 3) { // Get request with a list. int cnt = GXCommon.GetObjectCount(data); GXCommon.SetObjectCount(cnt, bb); for (int pos = 0; pos != cnt; ++pos) { ObjectType ci = (ObjectType)data.GetUInt16(); byte[] ln = new byte[6]; data.Get(ln); short attributeIndex = data.GetUInt8(); GXDLMSObject obj = Settings.Objects.FindByLN(ci, GXDLMSObject.ToLogicalName(ln)); if (obj == null) { obj = FindObject(ci, 0, GXDLMSObject.ToLogicalName(ln)); } if (obj == null) { // "Access Error : Device reports a undefined object." error = ErrorCode.UndefinedObject; } else { // AccessSelection int selection = data.GetUInt8(); int selector = 0; if (selection != 0) { selector = data.GetUInt8(); GXDataInfo info = new GXDataInfo(); parameters = GXCommon.GetData(data, info); } try { ValueEventArgs e = new ValueEventArgs(obj, attributeIndex, 0, parameters); Read(e); object value; if (e.Handled) { value = e.Value; } else { value = (obj as IGXDLMSBase).GetValue(Settings, attributeIndex, selector, parameters); } bb.SetUInt8(ErrorCode.Ok); GXDLMS.AppedData(obj, attributeIndex, bb, value); } catch (Exception) { bb.SetUInt8(1); bb.SetUInt8(ErrorCode.HardwareFault); } } } ServerReply.ReplyMessages = GXDLMS.SplitPdu(Settings, Command.GetResponse, 3, bb, error, DateTime.MinValue); } else { Debug.WriteLine("handleGetRequest failed. Invalid command type."); Settings.ResetBlockIndex(); // Access Error : Device reports a hardware fault. ServerReply.ReplyMessages = GXDLMS.SplitPdu(Settings, Command.GetResponse, 1, bb, ErrorCode.HardwareFault, DateTime.MinValue); } ServerReply.Index = index; return(ServerReply.ReplyMessages[index]); }
void OnUpdateTarget(GXDLMSObject value) { if (value != null && value.Parent != null && value.Parent.Parent is GXDLMSClient && (value.Parent.Parent as GXDLMSClient).Standard == Standard.Italy) { if (value.LogicalName == "0.0.21.0.1.255") { //If non-enrolled list. RemoveBtn.Visible = false; AddWhiteBtn.Visible = AddBlackBtn.Visible = true; } else if (value.LogicalName == "0.0.21.0.2.255" || value.LogicalName == "0.0.21.0.3.255") { //If white or black list. RemoveBtn.Visible = true; AddWhiteBtn.Visible = AddBlackBtn.Visible = false; } else { RemoveBtn.Visible = AddWhiteBtn.Visible = AddBlackBtn.Visible = false; } } else { RemoveBtn.Visible = AddWhiteBtn.Visible = AddBlackBtn.Visible = false; } target = (GXDLMSProfileGeneric)value; structures = false; GXDLMSObject obj; if (target != null) { UpdateCaptureObjects(); } else { ProfileGenericView.DataSource = null; } //Set initial values... ReadFromRB.Enabled = ReadLastRB.Enabled = ReadEntryBtn.Enabled = target.CaptureObjects.Count != 0; ReadFromRB.Checked = ReadLastRB.Checked = ReadEntryBtn.Checked = false; StartEntry.Value = Properties.Settings.Default.ProfileGenericIndex; EndEntry.Value = Properties.Settings.Default.ProfileGenericCount; ReadLastTB.Value = 0; DateTime tm; if (DateTime.TryParse(Properties.Settings.Default.ProfileGenericStartTime, out tm)) { if (tm != DateTime.MinValue) { try { StartPick.Checked = true; StartPick.Value = tm; } catch (Exception) { //It's ok if this fails. StartPick.Checked = false; } } else { StartPick.Checked = false; } } else { StartPick.Value = DateTime.Now; } if (DateTime.TryParse(Properties.Settings.Default.ProfileGenericEndTime, out tm)) { if (tm != DateTime.MaxValue) { try { ToPick.Checked = true; ToPick.Value = tm; } catch (Exception) { //It's ok if this fails. ToPick.Checked = false; } } else { ToPick.Checked = false; } } else { ToPick.Value = DateTime.Now; } if (!ReadFromRB.Enabled) { return; } obj = target.CaptureObjects[0].Key; if (target.AccessSelector == AccessRange.Entry) { StartEntry.Value = Convert.ToInt32(target.From); EndEntry.Value = Convert.ToInt32(target.To); ReadEntryBtn.Checked = true; } else if (target.AccessSelector == AccessRange.Last) { TimeSpan diff = (DateTime)target.To - (DateTime)target.From; ReadLastTB.Value = diff.Days - 1; ReadLastRB.Checked = true; } else if (target.AccessSelector == AccessRange.Range) { if ((DateTime)target.From == DateTime.MinValue) { StartPick.Checked = false; } else { StartPick.Value = (DateTime)target.From; } if ((DateTime)target.To == DateTime.MaxValue) { ToPick.Checked = false; } else { ToPick.Value = (DateTime)target.To; } ReadFromRB.Checked = true; } else //All is checked. { ReadAllRB.Checked = true; } }
/// <summary> /// Method attribute value. /// </summary> public void Method(GXDLMSObject it, int attributeIndex, object value, DataType type) { GXReplyData reply = new GXReplyData(); ReadDataBlock(Client.Method(it, attributeIndex, value, type), reply); }
/// <summary> /// Write attribute value. /// </summary> public void Write(GXDLMSObject it, int attributeIndex) { ReadDataBlock(Client.Write(it, attributeIndex)); }
/// <summary> /// Method attribute value. /// </summary> public void Method(GXDLMSObject it, int attributeIndex, object value, DataType type) { ReadDataBlock(Client.Method(it, attributeIndex, value, type)[0]); }
internal static void Read(ILogger _logger, HttpClient client, GXDLMSReader reader, GXTask task, IGXMedia net, GXDLMSObject obj) { AddValue v; HttpResponseMessage response; if (_logger != null) { _logger.LogInformation("Reading: " + obj.ToString()); } Console.WriteLine("Reading: " + obj.ToString()); object val; if (obj.ObjectType == ObjectType.ProfileGeneric && task.Index == 2 && task.Object.Attributes[GetBufferIndex(task.Object)].Read != DateTime.MinValue) { //Read profile generic using range. DateTime now = DateTime.Now; now = now.AddSeconds(-now.Second); now = now.AddMinutes(-now.Minute); now = now.AddHours(1); val = reader.ReadRowsByRange((GXDLMSProfileGeneric)obj, task.Object.Attributes[GetBufferIndex(task.Object)].Read, now); } else { val = reader.Read(obj, task.Index); } if (val is Enum) { //Enum values are saved as interger. val = Convert.ToString(Convert.ToInt32(val)); } else if (val is byte[]) { DataType dt = (DataType)task.Object.Attributes[GetBufferIndex(task.Object)].UIDataType; if (dt == DataType.String) { val = ASCIIEncoding.ASCII.GetString((byte[])val); } else if (dt == DataType.StringUTF8) { val = ASCIIEncoding.UTF8.GetString((byte[])val); } else { val = GXCommon.ToHex((byte[])val); } } else if (val is GXDateTime) { val = ((GXDateTime)val).ToFormatString(); } if (obj.ObjectType == ObjectType.ProfileGeneric && task.Index == 2) { //Make own value from each row. if (val != null) { UInt64 attributeId = task.Object.Attributes[GetBufferIndex(task.Object)].Id; List <GXValue> list = new List <GXValue>(); DateTime latest = task.Object.Attributes[GetBufferIndex(task.Object)].Read; DateTime first = latest; Boolean read = false; foreach (GXStructure row in (GXArray)val) { DateTime dt = DateTime.MinValue; task.Data = GXDLMSTranslator.ValueToXml(row); for (int pos = 0; pos != row.Count; ++pos) { if (row[pos] is byte[]) { row[pos] = GXDLMSClient.ChangeType((byte[])row[pos], DataType.DateTime); if (pos == 0) { dt = ((GXDateTime)row[pos]).Value.LocalDateTime; //If we have already read this row. if (dt <= first) { read = true; break; } if (dt > latest) { latest = dt; } } } //Some meters are returning null as date time to save bytes... if (pos == 0 && row[pos] == null) { row[pos] = latest.AddHours(1); } } if (_logger != null) { _logger.LogInformation("Read: " + task.Data); } if (!read) { list.Add(new GXValue() { Read = dt, Value = task.Data, AttributeId = attributeId }); } } if (list.Count != 0) { v = new AddValue() { Items = list.ToArray() }; response = client.PostAsJsonAsync(Startup.ServerAddress + "/api/value/AddValue", v).Result; Helpers.CheckStatus(response); ListDevicesResponse r = response.Content.ReadAsAsync <ListDevicesResponse>().Result; } } } else { if (!(val is string)) { val = Convert.ToString(val); } task.Data = (string)val; if (_logger != null) { _logger.LogInformation("Read: " + (string)val); } int index = GetAttributeIndex(task.Object, task.Index); if (index != -1) { UInt64 attributeId = task.Object.Attributes[index].Id; v = new AddValue() { Items = new GXValue[] { new GXValue() { AttributeId = attributeId, Read = DateTime.Now, Value = (string)val } } }; response = client.PostAsJsonAsync(Startup.ServerAddress + "/api/value/AddValue", v).Result; Helpers.CheckStatus(response); AddValueResponse r = response.Content.ReadAsAsync <AddValueResponse>().Result; } else { if (_logger != null) { _logger.LogInformation("Invalid task index: " + task.Index); } } } }
/// <summary> /// Add value of COSEM object to byte buffer. /// </summary> /// <param name="obj">COSEM object.</param> /// <param name="index">Attribute index.</param> /// <param name="buff">Byte buffer.</param> /// <remarks> /// AddData method can be used with GetDataNotificationMessage -method. /// DLMS spesification do not specify the structure of Data-Notification body. /// So each manufacture can sent different data. /// </remarks> /// <seealso cref="GenerateDataNotificationMessages"/> public void AddData(GXDLMSObject obj, int index, GXByteBuffer buff) { AddData(Settings, obj, index, buff); }
public byte[] Read(GXDLMSObject it, int attributeOrdinal) { byte[] tmp = client.Read(it, attributeOrdinal)[0]; return(tmp); }
/// <summary> /// After UpdateObjects call objects can be read using Objects property. /// </summary> public void UpdateObjects() { try { GXDLMSObjectCollection objs = Comm.GetObjects(); objs.Tag = this; int pos = 0; foreach (GXDLMSObject it in objs) { ++pos; NotifyProgress(this, "Creating object " + it.LogicalName, pos, objs.Count); m_Objects.Add(it); } GXLogWriter.WriteLog("--- Created " + m_Objects.Count.ToString() + " objects. ---"); //Read registers units and scalers. int cnt = Objects.Count; GXLogWriter.WriteLog("--- Reading scalers and units. ---"); for (pos = 0; pos != cnt; ++pos) { GXDLMSObject it = Objects[pos]; this.OnProgress(this, "Reading scalers and units.", cnt + pos + 1, 3 * cnt); if (it is GXDLMSRegister) { //Read scaler first. try { Comm.ReadValue(it, 3); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 3, ex); if (ex is GXDLMSException) { continue; } throw ex; } } if (it is GXDLMSDemandRegister) { try { //Read scaler first. Comm.ReadValue(it, 4); //Read Period Comm.ReadValue(it, 8); //Read number of periods Comm.ReadValue(it, 9); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 3, ex); if (ex is GXDLMSException) { continue; } throw ex; } } } GXLogWriter.WriteLog("--- Reading scalers and units end. ---"); /* TODO: * if (!m.UseLogicalNameReferencing) * { * GXLogWriter.WriteLog("--- Reading Access rights. ---"); * try * { * foreach (GXDLMSAssociationShortName sn in dev.Objects.GetObjects(ObjectType.AssociationShortName)) * { * dev.Comm.Read(sn, 3); * } * } * catch (Exception ex) * { * GXLogWriter.WriteLog(ex.Message); * } * GXLogWriter.WriteLog("--- Reading Access rights end. ---"); * } * */ this.OnProgress(this, "Reading profile generic columns.", cnt, cnt); foreach (Gurux.DLMS.Objects.GXDLMSProfileGeneric it in objs.GetObjects(ObjectType.ProfileGeneric)) { ++pos; //Read Profile Generic Columns. try { NotifyProgress(this, "Get profile generic columns", (2 * cnt) + pos, 3 * objs.Count); UpdateColumns(it, Manufacturers.FindByIdentification(Manufacturer)); if (it.CaptureObjects == null || it.CaptureObjects.Count == 0) { continue; } } catch { GXLogWriter.WriteLog(string.Format("Failed to read Profile Generic {0} columns.", it.LogicalName)); continue; } } } finally { NotifyProgress(this, "", 0, 0); } }
/// <summary> /// Read object. /// </summary> /// <param name="InitialValues"></param> /// <param name="obj"></param> /// <param name="attribute">Attribute index to read.</param> /// <param name="forceRead">Force all attributes read.</param> public void Read(object sender, GXDLMSObject obj, int attribute, bool forceRead) { GXReplyData reply = new GXReplyData(); if (forceRead) { obj.ClearReadTime(); } foreach (int it in (obj as IGXDLMSBase).GetAttributeIndexToRead()) { reply.Clear(); if (obj is GXDLMSProfileGeneric && it == 2) { if (OnBeforeRead != null) { OnBeforeRead(obj, it); } try { byte[][] tmp; CurrentProfileGeneric = obj as GXDLMSProfileGeneric; OnDataReceived += new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived); if (CurrentProfileGeneric.AccessSelector == AccessRange.Range || CurrentProfileGeneric.AccessSelector == AccessRange.Last) { GXDateTime start = CurrentProfileGeneric.From as GXDateTime; if (start == null) { start = Convert.ToDateTime(CurrentProfileGeneric.From); } GXDateTime end = CurrentProfileGeneric.To as GXDateTime; if (end == null) { end = Convert.ToDateTime(CurrentProfileGeneric.To); } tmp = client.ReadRowsByRange(CurrentProfileGeneric, start, end); ReadDataBlock(tmp, "Reading profile generic data", 1, reply); } else if (CurrentProfileGeneric.AccessSelector == AccessRange.Entry) { tmp = client.ReadRowsByEntry(CurrentProfileGeneric, Convert.ToInt32(CurrentProfileGeneric.From), Convert.ToInt32(CurrentProfileGeneric.To)); ReadDataBlock(tmp, "Reading profile generic data " + CurrentProfileGeneric.Name, 1, reply); } else //Read all. { tmp = client.Read(CurrentProfileGeneric, 2); ReadDataBlock(tmp, "Reading profile generic data " + CurrentProfileGeneric.Name, 1, reply); } } finally { if (OnAfterRead != null) { OnAfterRead(obj, it); } OnDataReceived -= new GXDLMSCommunicator.DataReceivedEventHandler(OnProfileGenericDataReceived); } continue; } else { if (OnBeforeRead != null) { OnBeforeRead(obj, it); } byte[] data = client.Read(obj.Name, obj.ObjectType, it)[0]; try { ReadDataBlock(data, "Read object type " + obj.ObjectType + " index: " + it, reply); } catch (GXDLMSException ex) { if (ex.ErrorCode == (int)ErrorCode.ReadWriteDenied || ex.ErrorCode == (int)ErrorCode.UndefinedObject || ex.ErrorCode == (int)ErrorCode.UnavailableObject || //Actaris returns access violation error. ex.ErrorCode == (int)ErrorCode.AccessViolated) { obj.SetAccess(it, AccessMode.NoAccess); continue; } else { throw ex; } } if (obj is IGXDLMSBase) { object value = reply.Value; DataType type; if (value is byte[] && (type = obj.GetUIDataType(it)) != DataType.None) { value = GXDLMSClient.ChangeType((byte[])value, type); } client.UpdateValue(obj, it, value); } if (OnAfterRead != null) { OnAfterRead(obj, it); } obj.SetLastReadTime(it, DateTime.Now); //If only selected attribute is read. if (attribute != 0) { break; } } } }
/// <summary> /// Read Profile Generic Columns. /// </summary> public GXDLMSObjectCollection GetColumns(GXDLMSObject it) { byte[] reply = ReadDataBlock(m_Parser.Read(it.Name, 1, 1, it.ObjectType, 3)); return m_Parser.ParseColumns(reply); }
/// <summary> /// Read Profile Generic Columns by entry. /// </summary> public object[] ReadRowsByEntry(GXDLMSObject it, int index, int count) { byte[] reply = ReadDataBlock(Client.ReadRowsByEntry(it.Name, index, count)); return((object[])Client.UpdateValue(reply, it, 2)); }
ParsePush(Object[] data) { int index; GXDLMSObject obj; object value; DataType dt; List <KeyValuePair <GXDLMSObject, int> > items = new List <KeyValuePair <GXDLMSObject, int> >(); if (data != null) { GXDLMSConverter c = new GXDLMSConverter(); GXDLMSObjectCollection objects = new GXDLMSObjectCollection(); foreach (Object it in (Object[])data[0]) { Object[] tmp = (Object[])it; int classID = ((UInt16)(tmp[0])) & 0xFFFF; if (classID > 0) { GXDLMSObject comp; comp = this.Objects.FindByLN((ObjectType)classID, GXDLMSObject.ToLogicalName(tmp[1] as byte[])); if (comp == null) { comp = GXDLMSClient.CreateDLMSObject(classID, 0, 0, tmp[1], null); c.UpdateOBISCodeInformation(comp); objects.Add(comp); } if ((comp is IGXDLMSBase)) { items.Add(new KeyValuePair <GXDLMSObject, int>(comp, (sbyte)tmp[2])); } else { System.Diagnostics.Debug.WriteLine(string.Format("Unknown object : {0} {1}", classID, GXDLMSObject.ToLogicalName((byte[])tmp[1]))); } } } for (int pos = 0; pos < data.Length; ++pos) { obj = items[pos].Key as GXDLMSObject; value = data[pos]; index = items[pos].Value; if (value is byte[] && (dt = obj.GetUIDataType(index)) != DataType.None) { value = GXDLMSClient.ChangeType(value as byte[], dt, Settings.UseUtc2NormalTime); } ValueEventArgs e = new ValueEventArgs(Settings, obj, index, 0, null); e.Value = value; (obj as IGXDLMSBase).SetValue(Settings, e); } } return(items); }
/// <summary> /// Read data type of selected attribute index. /// </summary> public DataType GetDLMSDataType(GXDLMSObject it, int attributeIndex) { byte[] reply = ReadDataBlock(Client.Read(it.Name, it.ObjectType, attributeIndex)[0]); return(Client.GetDLMSDataType(reply)); }
/// <summary> /// Write attribute value. /// </summary> public void Write(GXDLMSObject it, int attributeIndex) { GXReplyData reply = new GXReplyData(); ReadDataBlock(Client.Write(it, attributeIndex), reply); }
/// <summary> /// Each server has own history file. /// </summary> /// <param name="target"></param> /// <returns></returns> private string GetProfileGenericName(GXDLMSObject target) { string name = (Media as GXNet).Port + "_" + target.LogicalName + ".csv"; return(name); }
private static void UpdateOBISCodeInfo(GXStandardObisCodeCollection codes, GXDLMSObject it, Standard standard) { if (!string.IsNullOrEmpty(it.Description)) { return; } GXStandardObisCode code = codes.Find(it.LogicalName, it.ObjectType, standard)[0]; it.Description = code.Description; if (standard == Standard.SaudiArabia) { it.Description = it.Description.Replace("U(", "V("); } //If string is used string datatype = code.DataType; if (datatype == null) { datatype = ""; } if (datatype.Contains("10")) { code.UIDataType = "10"; } //If date time is used. else if (datatype.Contains("25") || datatype.Contains("26")) { code.UIDataType = code.DataType = "25"; } //Time stamps of the billing periods objects (first scheme if there are two) else if (datatype.Contains("9")) { if ((GXStandardObisCodeCollection.EqualsMask("0.0-64.96.7.10-14.255", it.LogicalName) || //Time stamps of the billing periods objects (second scheme) GXStandardObisCodeCollection.EqualsMask("0.0-64.0.1.5.0-99,255", it.LogicalName) || //Time of power failure GXStandardObisCodeCollection.EqualsMask("0.0-64.0.1.2.0-99,255", it.LogicalName) || //Time stamps of the billing periods objects (first scheme if there are two) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.1.2.0-99,255", it.LogicalName) || //Time stamps of the billing periods objects (second scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.1.5.0-99,255", it.LogicalName) || //Time expired since last end of billing period GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.0.255", it.LogicalName) || //Time of last reset GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.6.255", it.LogicalName) || //Date of last reset GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.7.255", it.LogicalName) || //Time expired since last end of billing period (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.13.255", it.LogicalName) || //Time of last reset (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.14.255", it.LogicalName) || //Date of last reset (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.15.255", it.LogicalName))) { code.UIDataType = "25"; } //Local time else if (GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.1.255", it.LogicalName)) { code.UIDataType = "27"; } //Local date else if (GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.2.255", it.LogicalName)) { code.UIDataType = "26"; } //Active firmware identifier else if (GXStandardObisCodeCollection.EqualsMask("1.0.0.2.0.255", it.LogicalName)) { code.UIDataType = "10"; } } //Unix time else if (it.ObjectType == ObjectType.Data && GXStandardObisCodeCollection.EqualsMask("0.0.1.1.0.255", it.LogicalName)) { code.UIDataType = "25"; } if (code.DataType != "*" && !string.IsNullOrEmpty(code.DataType) && !code.DataType.Contains(",")) { DataType type = (DataType)int.Parse(code.DataType); switch (it.ObjectType) { case ObjectType.Data: case ObjectType.Register: case ObjectType.RegisterActivation: case ObjectType.ExtendedRegister: it.SetDataType(2, type); break; default: break; } } if (!string.IsNullOrEmpty(code.UIDataType)) { DataType uiType = (DataType)int.Parse(code.UIDataType); switch (it.ObjectType) { case ObjectType.Data: case ObjectType.Register: case ObjectType.RegisterActivation: case ObjectType.ExtendedRegister: it.SetUIDataType(2, uiType); break; default: break; } } }
/// <summary> /// Initialize server. /// </summary> /// <remarks> /// This must call after server objects are set. /// <param name="manually">If true, server handle objects and all data are updated manually.</param> public void Initialize(bool manually) { Initialized = true; if (manually) { return; } bool association = false; for (int pos = 0; pos != Items.Count; ++pos) { GXDLMSObject it = Items[pos]; if (this.UseLogicalNameReferencing && (string.IsNullOrEmpty(it.LogicalName) || it.LogicalName.Split('.').Length != 6)) { throw new Exception("Invalid Logical Name."); } it.Start(this); if (it is GXDLMSProfileGeneric) { GXDLMSProfileGeneric pg = it as GXDLMSProfileGeneric; foreach (var obj in pg.CaptureObjects) { if (obj.Value.AttributeIndex < 1) { throw new Exception("Invalid attribute index. SelectedAttributeIndex is not set for " + obj.Key.Name); } } } else if (it is GXDLMSAssociationShortName && !this.UseLogicalNameReferencing) { if ((it as GXDLMSAssociationShortName).ObjectList.Count == 0) { (it as GXDLMSAssociationShortName).ObjectList.AddRange(this.Items); } association = true; } else if (it is GXDLMSAssociationLogicalName && this.UseLogicalNameReferencing) { if ((it as GXDLMSAssociationLogicalName).ObjectList.Count == 0) { (it as GXDLMSAssociationLogicalName).ObjectList.AddRange(this.Items); } association = true; } else if (it is GXDLMSHdlcSetup) { hdlcSetup = it as GXDLMSHdlcSetup; } else if (!(it is IGXDLMSBase))//Remove unsupported items. { Debug.WriteLine(it.ObjectType.ToString() + " not supported."); Items.RemoveAt(pos); --pos; } } if (!association) { if (UseLogicalNameReferencing) { GXDLMSAssociationLogicalName it = new GXDLMSAssociationLogicalName(); it.ObjectList = this.Items; Items.Add(it); } else { GXDLMSAssociationShortName it = new GXDLMSAssociationShortName(); it.ObjectList = this.Items; Items.Add(it); } } //Arrange items by Short Name. UpdateShortNames(false); }
///<summary> ///Handle set request. ///</summary> ///<returns> ///Reply to the client. ///</returns> private byte[][] HandleSetRequest() { ErrorCode error = ErrorCode.Ok; GXByteBuffer data = Reply.Data; GXDataInfo info = new GXDataInfo(); GXByteBuffer bb = new GXByteBuffer(); // Get type. short type = data.GetUInt8(); // Get invoke ID and priority. data.GetUInt8(); // SetRequest normal if (type == 1) { Settings.ResetBlockIndex(); ServerReply.Index = 0; // CI ObjectType ci = (ObjectType)data.GetUInt16(); byte[] ln = new byte[6]; data.Get(ln); // Attribute index. int index = data.GetUInt8(); // Get Access Selection. data.GetUInt8(); object value = GXCommon.GetData(data, info); GXDLMSObject obj = Settings.Objects.FindByLN(ci, GXDLMSObject.ToLogicalName(ln)); if (obj == null) { obj = FindObject(ci, 0, GXDLMSObject.ToLogicalName(ln)); } // If target is unknown. if (obj == null) { Debug.WriteLine("Undefined object."); // Device reports a undefined object. error = ErrorCode.UndefinedObject; } else { AccessMode am = obj.GetAccess(index); // If write is denied. if (am != AccessMode.Write && am != AccessMode.ReadWrite) { Debug.WriteLine("Read Write denied."); error = ErrorCode.ReadWriteDenied; } else { try { if (value is byte[]) { DataType dt = (obj as IGXDLMSBase).GetDataType(index); if (dt != DataType.None) { value = GXDLMSClient.ChangeType((byte[])value, dt); } } ValueEventArgs e = new ValueEventArgs(obj, index, 0, null); Write(e); if (!e.Handled) { (obj as IGXDLMSBase).SetValue(Settings, index, value); } } catch (Exception e) { Debug.WriteLine(e.Message); error = ErrorCode.HardwareFault; } } } } else { Debug.WriteLine("handleSetRequest failed. Unknown command."); Settings.ResetBlockIndex(); error = ErrorCode.HardwareFault; } return(GXDLMS.SplitPdu(Settings, Command.SetResponse, 1, bb, error, DateTime.MinValue)[0]); }
private static void UpdateOBISCodeInfo(GXStandardObisCodeCollection codes, GXDLMSObject it) { if (!string.IsNullOrEmpty(it.Description)) { return; } GXStandardObisCode code = codes.Find(it.LogicalName, it.ObjectType); if (code != null) { it.Description = code.Description; //If string is used if (code.DataType.Contains("10")) { code.UIDataType = "10"; } //If date time is used. else if (code.DataType.Contains("25") || code.DataType.Contains("26")) { code.UIDataType = code.DataType = "25"; } //Time stamps of the billing periods objects (first scheme if there are two) else if (code.DataType.Contains("9")) { if ((GXStandardObisCodeCollection.EqualsMask("0.0-64.96.7.10-14.255", it.LogicalName) || //Time stamps of the billing periods objects (second scheme) GXStandardObisCodeCollection.EqualsMask("0.0-64.0.1.5.0-99,255", it.LogicalName) || //Time of power failure GXStandardObisCodeCollection.EqualsMask("0.0-64.0.1.2.0-99,255", it.LogicalName) || //Time stamps of the billing periods objects (first scheme if there are two) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.1.2.0-99,255", it.LogicalName) || //Time stamps of the billing periods objects (second scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.1.5.0-99,255", it.LogicalName) || //Time expired since last end of billing period GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.0.255", it.LogicalName) || //Time of last reset GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.6.255", it.LogicalName) || //Date of last reset GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.7.255", it.LogicalName) || //Time expired since last end of billing period (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.13.255", it.LogicalName) || //Time of last reset (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.14.255", it.LogicalName) || //Date of last reset (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.15.255", it.LogicalName))) { code.UIDataType = "25"; } //Local time else if (GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.1.255", it.LogicalName)) { code.UIDataType = "27"; } //Local date else if (GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.2.255", it.LogicalName)) { code.UIDataType = "26"; } //Active firmware identifier else if (GXStandardObisCodeCollection.EqualsMask("1.0.0.2.0.255", it.LogicalName)) { code.UIDataType = "10"; } } if (code.DataType != "*" && code.DataType != string.Empty && !code.DataType.Contains(",")) { DataType type = (DataType)int.Parse(code.DataType); switch (it.ObjectType) { case ObjectType.Data: case ObjectType.Register: case ObjectType.RegisterActivation: case ObjectType.ExtendedRegister: it.SetDataType(2, type); break; default: break; } } if (!string.IsNullOrEmpty(code.UIDataType)) { DataType uiType = (DataType)int.Parse(code.UIDataType); switch (it.ObjectType) { case ObjectType.Data: case ObjectType.Register: case ObjectType.RegisterActivation: case ObjectType.ExtendedRegister: it.SetUIDataType(2, uiType); break; default: break; } } } else { System.Diagnostics.Debug.WriteLine("Unknown OBIS Code: " + it.LogicalName + " Type: " + it.ObjectType); } }
///<summary> /// Handle action request. ///</summary> public static void HandleMethodRequest(GXDLMSSettings settings, GXDLMSServer server, GXByteBuffer data, GXDLMSConnectionEventArgs connectionInfo, GXByteBuffer replyData, GXDLMSTranslatorStructure xml) { ErrorCode error = ErrorCode.Ok; GXByteBuffer bb = new GXByteBuffer(); // Get type. ActionRequestType type = (ActionRequestType)data.GetUInt8(); // Get invoke ID and priority. byte invokeId = data.GetUInt8(); // CI ObjectType ci = (ObjectType)data.GetUInt16(); byte[] ln = new byte[6]; data.Get(ln); // Attribute Id byte id = data.GetUInt8(); // Get parameters. object parameters = null; byte selection = data.GetUInt8(); if (xml != null) { xml.AppendStartTag(Command.MethodRequest); if (type == ActionRequestType.Normal) { xml.AppendStartTag(Command.MethodRequest, ActionRequestType.Normal); xml.AppendLine(TranslatorTags.InvokeId, "Value", xml.IntegerToHex(invokeId, 2)); AppendMethodDescriptor(xml, (int)ci, ln, id); if (selection != 0) { //MethodInvocationParameters xml.AppendStartTag(TranslatorTags.MethodInvocationParameters); GXDataInfo di = new GXDataInfo(); di.xml = xml; GXCommon.GetData(settings, data, di); xml.AppendEndTag(TranslatorTags.MethodInvocationParameters); } xml.AppendEndTag(Command.MethodRequest, ActionRequestType.Normal); } xml.AppendEndTag(Command.MethodRequest); return; } if (selection != 0) { GXDataInfo info = new GXDataInfo(); parameters = GXCommon.GetData(settings, data, info); } GXDLMSObject obj = settings.Objects.FindByLN(ci, GXCommon.ToLogicalName(ln)); if (!settings.Connected && !settings.AllowAnonymousAccess && (ci != ObjectType.AssociationLogicalName || id != 1)) { replyData.Set(GXDLMSServer.GenerateConfirmedServiceError(ConfirmedServiceError.InitiateError, ServiceError.Service, (byte)Service.Unsupported)); return; } if (obj == null) { obj = server.NotifyFindObject(ci, 0, GXCommon.ToLogicalName(ln)); } if (obj == null) { // Device reports a undefined object. error = ErrorCode.UndefinedObject; } else { ValueEventArgs e = new ValueEventArgs(server, obj, id, 0, parameters); e.InvokeId = invokeId; if (server.NotifyGetMethodAccess(e) == MethodAccessMode.NoAccess) { error = ErrorCode.ReadWriteDenied; } else { server.NotifyAction(new ValueEventArgs[] { e }); byte[] actionReply; if (e.Handled) { actionReply = (byte[])e.Value; } else { actionReply = (obj as IGXDLMSBase).Invoke(settings, e); server.NotifyPostAction(new ValueEventArgs[] { e }); if (settings.Connected && (ci == ObjectType.AssociationLogicalName && id == 1)) { server.NotifyConnected(connectionInfo); } } //Set default action reply if not given. if (actionReply != null && e.Error == 0) { //Add return parameters bb.SetUInt8(1); //Add parameters error code. bb.SetUInt8(0); GXCommon.SetData(settings, bb, GXCommon.GetValueType(actionReply), actionReply); } else { error = e.Error; //Add return parameters bb.SetUInt8(0); } } invokeId = (byte)e.InvokeId; } GXDLMSLNParameters p = new GXDLMSLNParameters(settings, invokeId, Command.MethodResponse, 1, null, bb, (byte)error); GXDLMS.GetLNPdu(p, replyData); //If High level authentication fails. if (!settings.Connected && !settings.AllowAnonymousAccess && obj is GXDLMSAssociationLogicalName && id == 1) { server.NotifyInvalidConnection(connectionInfo); } }
/// <summary> /// After UpdateObjects call objects can be read using Objects property. /// </summary> public void UpdateObjects() { try { GXDLMSObjectCollection objs = Comm.GetObjects(); objs.Tag = this; int pos = 0; foreach (GXDLMSObject it in objs) { ++pos; NotifyProgress(this, "Creating object " + it.LogicalName, pos, objs.Count); Objects.Add(it); } GXLogWriter.WriteLog("--- Created " + Objects.Count.ToString() + " objects. ---"); //Read registers units and scalers. int cnt = Objects.Count; if (!UseLogicalNameReferencing) { GXLogWriter.WriteLog("--- Reading Access rights. ---"); try { foreach (GXDLMSAssociationShortName sn in Objects.GetObjects(ObjectType.AssociationShortName)) { Comm.ReadValue(sn, 3); } } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); } GXLogWriter.WriteLog("--- Reading Access rights end. ---"); } GXLogWriter.WriteLog("--- Reading scalers and units. ---"); this.OnProgress(this, "Reading scalers and units.", cnt + pos + 1, 3 * cnt); if ((Comm.client.NegotiatedConformance & Gurux.DLMS.Enums.Conformance.MultipleReferences) != 0) { List <KeyValuePair <GXDLMSObject, int> > list = new List <KeyValuePair <GXDLMSObject, int> >(); foreach (GXDLMSObject it in Objects) { if (it is GXDLMSRegister && (it.GetAccess(3) & AccessMode.Read) != 0) { list.Add(new KeyValuePair <GXDLMSObject, int>(it, 3)); } if (it is GXDLMSDemandRegister && (it.GetAccess(4) & AccessMode.Read) != 0) { list.Add(new KeyValuePair <GXDLMSObject, int>(it, 4)); } } if (list.Count != 0) { try { Comm.ReadList(list); } catch (Exception) { //Show error. } } } else { for (pos = 0; pos != cnt; ++pos) { GXDLMSObject it = Objects[pos]; if (it is GXDLMSRegister) { //Read scaler first. try { Comm.ReadValue(it, 3); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); it.SetAccess(3, AccessMode.NoAccess); if (ex is GXDLMSException) { continue; } throw ex; } } if (it is GXDLMSDemandRegister) { //Read scaler first. try { Comm.ReadValue(it, 4); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 4, ex); if (ex is GXDLMSException) { continue; } throw ex; } //Read Period try { Comm.ReadValue(it, 8); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 8, ex); if (ex is GXDLMSException) { continue; } throw ex; } //Read number of periods try { Comm.ReadValue(it, 9); } catch (Exception ex) { GXLogWriter.WriteLog(ex.Message); UpdateError(it, 9, ex); if (ex is GXDLMSException) { continue; } throw ex; } } } } GXLogWriter.WriteLog("--- Reading scalers and units end. ---"); this.OnProgress(this, "Reading profile generic columns.", cnt, cnt); foreach (Gurux.DLMS.Objects.GXDLMSProfileGeneric it in objs.GetObjects(ObjectType.ProfileGeneric)) { ++pos; //Read Profile Generic Columns. try { NotifyProgress(this, "Get profile generic columns", (2 * cnt) + pos, 3 * objs.Count); UpdateColumns(it, Manufacturers.FindByIdentification(Manufacturer)); if (it.CaptureObjects == null || it.CaptureObjects.Count == 0) { continue; } } catch { GXLogWriter.WriteLog(string.Format("Failed to read Profile Generic {0} columns.", it.LogicalName)); continue; } } } finally { NotifyProgress(this, "", 0, 0); } }
/// <summary> /// Handle get request with list command. /// </summary> /// <param name="data">Received data.</param> private static void GetRequestWithList(GXDLMSSettings settings, byte invokeID, GXDLMSServer server, GXByteBuffer data, GXByteBuffer replyData, GXDLMSTranslatorStructure xml) { ValueEventArgs e; GXByteBuffer bb = new GXByteBuffer(); int pos; int cnt = GXCommon.GetObjectCount(data); GXCommon.SetObjectCount(cnt, bb); List <ValueEventArgs> list = new List <ValueEventArgs>(); if (xml != null) { xml.AppendStartTag(TranslatorTags.AttributeDescriptorList, "Qty", xml.IntegerToHex(cnt, 2)); } try { for (pos = 0; pos != cnt; ++pos) { ObjectType ci = (ObjectType)data.GetUInt16(); byte[] ln = new byte[6]; data.Get(ln); short attributeIndex = data.GetUInt8(); // AccessSelection int selection = data.GetUInt8(); int selector = 0; object parameters = null; if (selection != 0) { selector = data.GetUInt8(); GXDataInfo info = new GXDataInfo(); parameters = GXCommon.GetData(settings, data, info); } if (xml != null) { xml.AppendStartTag(TranslatorTags.AttributeDescriptorWithSelection); xml.AppendStartTag(TranslatorTags.AttributeDescriptor); xml.AppendComment(ci.ToString()); xml.AppendLine(TranslatorTags.ClassId, "Value", xml.IntegerToHex((int)ci, 4)); xml.AppendComment(GXCommon.ToLogicalName(ln)); xml.AppendLine(TranslatorTags.InstanceId, "Value", GXCommon.ToHex(ln, false)); xml.AppendLine(TranslatorTags.AttributeId, "Value", xml.IntegerToHex(attributeIndex, 2)); xml.AppendEndTag(TranslatorTags.AttributeDescriptor); xml.AppendEndTag(TranslatorTags.AttributeDescriptorWithSelection); } else { GXDLMSObject obj = settings.Objects.FindByLN(ci, GXCommon.ToLogicalName(ln)); if (obj == null) { obj = server.NotifyFindObject(ci, 0, GXCommon.ToLogicalName(ln)); } if (obj == null) { // "Access Error : Device reports a undefined object." e = new ValueEventArgs(server, obj, attributeIndex, 0, 0); e.Error = ErrorCode.UndefinedObject; list.Add(e); } else { ValueEventArgs arg = new ValueEventArgs(server, obj, attributeIndex, selector, parameters); arg.InvokeId = invokeID; if (server.NotifyGetAttributeAccess(arg) == AccessMode.NoAccess) { //Read Write denied. arg.Error = ErrorCode.ReadWriteDenied; list.Add(arg); } else { list.Add(arg); } } } } } catch (Exception ex) { if (xml == null) { throw ex; } } if (xml != null) { xml.AppendEndTag(TranslatorTags.AttributeDescriptorList); return; } server.NotifyRead(list.ToArray()); object value; pos = 0; foreach (ValueEventArgs it in list) { try { if (it.Handled) { value = it.Value; } else { value = (it.Target as IGXDLMSBase).GetValue(settings, it); } bb.SetUInt8(it.Error); if (it.ByteArray) { bb.Set((byte[])value); } else { GXDLMS.AppendData(settings, it.Target, it.Index, bb, value); } invokeID = (byte)it.InvokeId; } catch (Exception) { bb.SetUInt8((byte)ErrorCode.HardwareFault); } if (settings.Index != settings.Count) { server.transaction = new GXDLMSLongTransaction(list.ToArray(), Command.GetRequest, null); } ++pos; } server.NotifyPostRead(list.ToArray()); GXDLMSLNParameters p = new GXDLMSLNParameters(settings, invokeID, Command.GetResponse, 3, null, bb, 0xFF); GXDLMS.GetLNPdu(p, replyData); }
/// <summary> /// Constructor. /// </summary> /// <param name="target">Target object.</param> /// <param name="attributeIndex">Attribute index.</param> public GXDLMSTarget(GXDLMSObject target, byte attributeIndex) { Target = target; AttributeIndex = attributeIndex; }
private static void HanleSetRequestWithList(GXDLMSSettings settings, byte invokeID, GXDLMSServer server, GXByteBuffer data, GXDLMSLNParameters p, GXByteBuffer replyData, GXDLMSTranslatorStructure xml) { ValueEventArgs e; int cnt = GXCommon.GetObjectCount(data); List <ValueEventArgs> list = new List <ValueEventArgs>(); if (xml != null) { xml.AppendStartTag(TranslatorTags.AttributeDescriptorList, "Qty", xml.IntegerToHex(cnt, 2)); } try { for (int pos = 0; pos != cnt; ++pos) { ObjectType ci = (ObjectType)data.GetUInt16(); byte[] ln = new byte[6]; data.Get(ln); short attributeIndex = data.GetUInt8(); // AccessSelection int selection = data.GetUInt8(); int selector = 0; object parameters = null; if (selection != 0) { selector = data.GetUInt8(); GXDataInfo info = new GXDataInfo(); parameters = GXCommon.GetData(settings, data, info); } if (xml != null) { xml.AppendStartTag(TranslatorTags.AttributeDescriptorWithSelection); xml.AppendStartTag(TranslatorTags.AttributeDescriptor); xml.AppendComment(ci.ToString()); xml.AppendLine(TranslatorTags.ClassId, "Value", xml.IntegerToHex((int)ci, 4)); xml.AppendComment(GXCommon.ToLogicalName(ln)); xml.AppendLine(TranslatorTags.InstanceId, "Value", GXCommon.ToHex(ln, false)); xml.AppendLine(TranslatorTags.AttributeId, "Value", xml.IntegerToHex(attributeIndex, 2)); xml.AppendEndTag(TranslatorTags.AttributeDescriptor); xml.AppendEndTag(TranslatorTags.AttributeDescriptorWithSelection); } else { GXDLMSObject obj = settings.Objects.FindByLN(ci, GXCommon.ToLogicalName(ln)); if (obj == null) { obj = server.NotifyFindObject(ci, 0, GXCommon.ToLogicalName(ln)); } if (obj == null) { // "Access Error : Device reports a undefined object." e = new ValueEventArgs(server, obj, attributeIndex, 0, 0); e.Error = ErrorCode.UndefinedObject; list.Add(e); } else { ValueEventArgs arg = new ValueEventArgs(server, obj, attributeIndex, selector, parameters); arg.InvokeId = invokeID; if (server.NotifyGetAttributeAccess(arg) == AccessMode.NoAccess) { //Read Write denied. arg.Error = ErrorCode.ReadWriteDenied; list.Add(arg); } else { list.Add(arg); } } } } cnt = GXCommon.GetObjectCount(data); if (xml != null) { xml.AppendEndTag(TranslatorTags.AttributeDescriptorList); xml.AppendStartTag(TranslatorTags.ValueList, "Qty", xml.IntegerToHex(cnt, 2)); } for (int pos = 0; pos != cnt; ++pos) { GXDataInfo di = new GXDataInfo(); di.xml = xml; if (xml != null && xml.OutputType == TranslatorOutputType.StandardXml) { xml.AppendStartTag(Command.WriteRequest, SingleReadResponse.Data); } object value = GXCommon.GetData(settings, data, di); if (!di.Complete) { value = GXCommon.ToHex(data.Data, false, data.Position, data.Size - data.Position); } else if (value is byte[]) { value = GXCommon.ToHex((byte[])value, false); } if (xml != null && xml .OutputType == TranslatorOutputType.StandardXml) { xml.AppendEndTag(Command.WriteRequest, SingleReadResponse.Data); } } if (xml != null) { xml.AppendEndTag(TranslatorTags.ValueList); } } catch (Exception ex) { if (xml == null) { throw ex; } } }
/// <summary> /// Read data from the meter. /// </summary> private static void ReadMeter(object parameter) { GXDLMSReader reader = null; System.Net.Http.HttpClient httpClient = Helpers.client; using (GXNet media = (GXNet)parameter) { try { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true) .Build(); ListenerOptions listener = config.GetSection("Listener").Get <ListenerOptions>(); GXDLMSObjectCollection objects = new GXDLMSObjectCollection(); GXDLMSSecureClient client = new GXDLMSSecureClient(listener.UseLogicalNameReferencing, listener.ClientAddress, listener.ServerAddress, (Authentication)listener.Authentication, listener.Password, (InterfaceType)listener.Interface); reader = new GXDLMSReader(client, media, _logger); GXDLMSData ldn = new GXDLMSData("0.0.42.0.0.255"); ldn.SetUIDataType(2, DataType.String); reader.InitializeConnection(); reader.Read(ldn, 2); Console.WriteLine("Meter connected: " + ldn.Value); //Find device. GXDevice dev = null; ListDevicesResponse devs = null; { using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/device/ListDevices", new ListDevices() { Name = (string)ldn.Value }).Result) { Helpers.CheckStatus(response); devs = response.Content.ReadAsAsync <ListDevicesResponse>().Result; } //If device is unknown. if (devs.Devices.Length == 0) { if (listener.DefaultDeviceTemplate == 0) { string str = "Unknown Meter try to connect to the Gurux.DLMS.AMI server: " + ldn.Value; Console.WriteLine(str); AddSystemError info = new AddSystemError(); info.Error = new GXSystemError() { Error = str }; using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/SystemError/AddSystemError", info).Result) { Helpers.CheckStatus(response); } return; } ListDeviceTemplates lt = new ListDeviceTemplates() { Ids = new UInt64[] { listener.DefaultDeviceTemplate } }; using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/template/ListDeviceTemplates", lt).Result) { Helpers.CheckStatus(response); ListDeviceTemplatesResponse ret = response.Content.ReadAsAsync <ListDeviceTemplatesResponse>().Result; if (ret.Devices.Length != 1) { throw new Exception("DefaultDeviceTemplate value is invalid: " + listener.DefaultDeviceTemplate); } dev = new GXDevice(); GXDevice.Copy(dev, ret.Devices[0]); dev.Name = Convert.ToString(ldn.Value); dev.TemplateId = listener.DefaultDeviceTemplate; dev.Manufacturer = ret.Devices[0].Name; } dev.Dynamic = true; UpdateDevice update = new UpdateDevice(); update.Device = dev; using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/device/UpdateDevice", update).Result) { Helpers.CheckStatus(response); UpdateDeviceResponse r = response.Content.ReadAsAsync <UpdateDeviceResponse>().Result; dev.Id = r.DeviceId; } using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/device/ListDevices", new ListDevices() { Ids = new UInt64[] { dev.Id } }).Result) { Helpers.CheckStatus(response); devs = response.Content.ReadAsAsync <ListDevicesResponse>().Result; } } else if (devs.Devices.Length != 1) { throw new Exception("There are multiple devices with same name: " + ldn.Value); } else { dev = devs.Devices[0]; if (dev.Security != Security.None) { Console.WriteLine("Reading frame counter."); GXDLMSData fc = new GXDLMSData(listener.InvocationCounter); reader.Read(fc, 2); dev.InvocationCounter = 1 + Convert.ToUInt32(fc.Value); Console.WriteLine("Device ID: " + dev.Id + " LDN: " + (string)ldn.Value); Console.WriteLine("Frame counter: " + dev.FrameCounter); } GetNextTaskResponse ret; using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/task/GetNextTask", new GetNextTask() { Listener = true, DeviceId = dev.Id }).Result) { Helpers.CheckStatus(response); ret = response.Content.ReadAsAsync <GetNextTaskResponse>().Result; } if (ret.Tasks == null || ret.Tasks.Length == 0) { Console.WriteLine("No tasks to execute"); } else { Console.WriteLine("Task count: " + ret.Tasks.Length); if (client.ClientAddress != dev.ClientAddress || dev.Security != Security.None) { reader.Release(); reader.Disconnect(); client = new GXDLMSSecureClient(dev.UseLogicalNameReferencing, dev.ClientAddress, dev.PhysicalAddress, (Authentication)dev.Authentication, dev.Password, dev.InterfaceType); client.UseUtc2NormalTime = dev.UtcTimeZone; client.Standard = (Standard)dev.Standard; if (dev.Conformance != 0) { client.ProposedConformance = (Conformance)dev.Conformance; } client.Priority = dev.Priority; client.ServiceClass = dev.ServiceClass; client.Ciphering.SystemTitle = GXCommon.HexToBytes(dev.ClientSystemTitle); client.Ciphering.BlockCipherKey = GXCommon.HexToBytes(dev.BlockCipherKey); client.Ciphering.AuthenticationKey = GXCommon.HexToBytes(dev.AuthenticationKey); client.ServerSystemTitle = GXCommon.HexToBytes(dev.DeviceSystemTitle); client.Ciphering.InvocationCounter = dev.InvocationCounter; client.Ciphering.Security = (Security)dev.Security; reader = new GXDLMSReader(client, media, _logger); reader.InitializeConnection(); } List <GXValue> values = new List <GXValue>(); foreach (GXTask task in ret.Tasks) { GXDLMSObject obj = GXDLMSClient.CreateObject((ObjectType)task.Object.ObjectType); obj.LogicalName = task.Object.LogicalName; try { if (task.TaskType == TaskType.Write) { if (obj.LogicalName == "0.0.1.1.0.255" && task.Index == 2) { client.UpdateValue(obj, task.Index, GXDateTime.ToUnixTime(DateTime.UtcNow)); } else { client.UpdateValue(obj, task.Index, GXDLMSTranslator.XmlToValue(task.Data)); } reader.Write(obj, task.Index); } else if (task.TaskType == TaskType.Action) { reader.Method(obj, task.Index, GXDLMSTranslator.XmlToValue(task.Data), DataType.None); } else if (task.TaskType == TaskType.Read) { //Reading the meter. if (task.Object.Attributes[0].DataType != 0) { obj.SetDataType(task.Index, (DataType)task.Object.Attributes[0].DataType); } if (task.Object.Attributes[0].UIDataType != 0) { obj.SetUIDataType(task.Index, (DataType)task.Object.Attributes[0].UIDataType); } Reader.Reader.Read(null, httpClient, reader, task, media, obj); if (task.Object.Attributes[0].DataType == 0) { task.Object.Attributes[0].DataType = (int)obj.GetDataType(task.Index); if (task.Object.Attributes[0].UIDataType == 0) { task.Object.Attributes[0].UIDataType = (int)obj.GetUIDataType(task.Index); } UpdateDatatype u = new UpdateDatatype() { Items = new GXAttribute[] { task.Object.Attributes[0] } }; using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/Object/UpdateDatatype", u).Result) { Helpers.CheckStatus(response); } } } } catch (Exception ex) { task.Result = ex.Message; AddError error = new AddError(); error.Error = new GXError() { DeviceId = dev.Id, Error = "Failed to " + task.TaskType + " " + task.Object.LogicalName + ":" + task.Index + ". " + ex.Message }; using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/error/AddError", error).Result) { Helpers.CheckStatus(response); response.Content.ReadAsAsync <AddErrorResponse>(); } } using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/task/TaskReady", new TaskReady() { Tasks = new GXTask[] { task } }).Result) { Helpers.CheckStatus(response); } } } } } } catch (Exception ex) { try { AddSystemError info = new AddSystemError(); info.Error = new GXSystemError() { Error = ex.Message }; using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsJsonAsync(Startup.ServerAddress + "/api/SystemError/AddSystemError", info).Result) { Helpers.CheckStatus(response); } } catch (Exception ex2) { } } finally { if (reader != null) { reader.Close(); } } } }
/// <summary> /// Read Profile Generic Columns by range. /// </summary> public object[] ReadRowsByRange(GXDLMSObject it, DateTime start, DateTime end, GXDLMSObjectCollection columns) { GXDLMSObject col = columns[0]; byte[] reply = ReadDataBlock(m_Parser.Read(m_Parser.ReadRowsByRange(it.Name, col.LogicalName, col.ObjectType, col.Version, start, end), 1, 1, ObjectType.None, 0)); object[] rows = (object[])m_Parser.GetValue(reply); if (columns != null && rows.Length != 0 && m_Parser.ObisCodes.Count != 0) { Array row = (Array)rows[0]; if (columns.Count != row.Length) { throw new Exception("Columns count do not mach."); } for (int pos = 0; pos != columns.Count; ++pos) { if (row.GetValue(pos) is byte[]) { DataType type = DataType.None; //Find Column type GXObisCode code = m_Parser.ObisCodes.FindByLN(columns[pos].ObjectType, columns[pos].LogicalName, null); if (code != null) { GXAttribute att = code.Attributes.Find(columns[pos].AttributeIndex); if (att != null) { type = att.UIType; } } foreach (object[] cell in rows) { cell[pos] = GXDLMS.ChangeType((byte[])cell[pos], type); } } } } return rows; }
void UpdateData(DataTable dt) { if (target.CaptureObjects.Count == 0) { return; } if (structures) { List <object[]> data = new List <object[]>(); foreach (object[] r in target.Buffer) { List <object> row = new List <object>(); int index = 0; foreach (var it in target.CaptureObjects) { //If COSEM object is selected. //Only few meters are supporting this. if (it.Value.AttributeIndex == 0 && r[index] is List <object> ) { //Values must be update to the list because there might be Register Scaler //and it expects that scaler is read before value is updated. GXDLMSObject obj = GXDLMSClient.CreateObject(it.Key.ObjectType); byte i2 = 1; Dictionary <byte, object> list = new Dictionary <byte, object>(); foreach (object v in (r[index] as List <object>)) { list.Add(i2, v); ++i2; } //Update values first. foreach (byte i in (obj as IGXDLMSBase).GetAttributeIndexToRead(true)) { ValueEventArgs ve = new ValueEventArgs(obj, i, 0, null); ve.Value = list[i]; (obj as IGXDLMSBase).SetValue(null, ve); } //Show values. for (byte i = 0; i != (obj as IGXDLMSBase).GetAttributeCount(); ++i) { row.Add(GetValue(obj.GetValues()[i])); } } else { row.Add(GetValue(r[index])); } ++index; } data.Add(row.ToArray()); } for (int pos = dt.Rows.Count; pos < data.Count; ++pos) { object[] row = data[pos]; dt.LoadDataRow(row, true); } } else { for (int pos = dt.Rows.Count; pos < target.Buffer.Count; ++pos) { object[] row = target.Buffer[pos]; if (row != null) { for (int col = 0; col != row.Length; ++col) { if (row[col] is byte[]) { row[col] = GXDLMSTranslator.ToHex((byte[])row[col], true); } else if (row[col] is Object[]) { row[col] = GXDLMSTranslator.ValueToXml(row[col]); } else if (row[col] is GXStructure || row[col] is GXArray) { if (target.CaptureObjects[col].Key is GXDLMSRegister && target.CaptureObjects[col].Value.AttributeIndex == 2) { GXDLMSRegister obj = new GXDLMSRegister(); ValueEventArgs ve = new ValueEventArgs(obj, target.CaptureObjects[col].Value.AttributeIndex, 0, null); ve.Value = row[col]; (obj as IGXDLMSBase).SetValue(null, ve); row[col] = "{" + obj.Scaler + ", " + obj.Unit + "}"; } else { StringBuilder sb = new StringBuilder(); GetArrayAsString(sb, row[col]); row[col] = sb.ToString(); } } else { GXDLMSAttributeSettings att = target.CaptureObjects[col].Key.Attributes.Find(target.CaptureObjects[col].Value.AttributeIndex); if (att != null && att.Values != null) { if (att.Type == DataType.BitString && row[col] is string) { string str = (string)row[col]; if (str.Length != 0 && (str[0] == '0' || str[0] == '1')) { StringBuilder sb = new StringBuilder(); int pos2 = 0; foreach (char it in str) { if (it == '1') { if (sb.Length != 0) { sb.Append(','); } sb.Append(att.Values[pos2].UIValue); } ++pos2; if (pos2 == att.Values.Count) { break; } } row[col] = sb.ToString(); } } else { foreach (GXObisValueItem it in att.Values) { if (IsNumber(row[col]) && it.Value == Convert.ToInt32(row[col])) { row[col] = it.UIValue; break; } } } } } } dt.LoadDataRow(row, true); } } } }
/// <summary> /// Read data type of selected attribute index. /// </summary> public DataType GetDLMSDataType(GXDLMSObject it, int attributeIndex) { byte[] reply = ReadDataBlock(m_Parser.Read(it.Name, 1, 1, it.ObjectType, attributeIndex)); return m_Parser.GetDLMSDataType(reply); }
public void MethodRequest(GXDLMSObject target, int methodIndex, object data, GXReplyData reply) { ReadDataBlock(client.Method(target, methodIndex, data, DataType.None), "", reply); }