private static bool UpdateAccessRights(IGXDLMSView view, System.Windows.Forms.Control.ControlCollection controls, GXDLMSObject target, int index, bool method, bool connected) { foreach (Control it in controls) { if (!method && it is GXValueField) { GXValueField obj = it as GXValueField; if (obj.Index == index) { obj.Target = target; AccessMode am = target.GetAccess(index); OnUpdateAccessRights(view, obj, connected && ((am & AccessMode.Write) != 0)); return(!obj.NotifyChanges); } } else if (it is GXButton) { GXButton btn = it as GXButton; btn.Target = target; //Update custom buttons. if (method && index == 0 && btn.Index < 1) { OnUpdateAccessRights(view, btn, connected); continue; } if (method && btn.Index == index && btn.Action == ActionType.Action) { MethodAccessMode ma = target.GetMethodAccess(index); OnUpdateAccessRights(view, btn, connected && ma != MethodAccessMode.NoAccess); return(true); } if (!method && btn.Index == index && (btn.Action == ActionType.Read || btn.Action == ActionType.Write)) { AccessMode am = target.GetAccess(index); if (btn.Action == ActionType.Read) { OnUpdateAccessRights(view, btn, connected && ((am & AccessMode.Read) != 0)); } else if (btn.Action == ActionType.Write) { OnUpdateAccessRights(view, btn, connected && ((am & AccessMode.Write) != 0)); } } } else if (it.Controls.Count != 0) { bool ret = UpdateAccessRights(view, it.Controls, target, index, method, connected); //If object is updated. if (ret) { return(true); } } } return(false); }
/// <summary> /// Read attribute value. /// </summary> /// <param name="it">COSEM object to read.</param> /// <param name="attributeIndex">Attribute index.</param> /// <returns>Read value.</returns> public object Read(GXDLMSObject it, int attributeIndex) { if ((it.GetAccess(attributeIndex) & AccessMode.Read) != 0) { GXReplyData reply = new GXReplyData(); if (!ReadDataBlock(Client.Read(it, attributeIndex), reply)) { if (reply.Error != (short)ErrorCode.Rejected) { throw new GXDLMSException(reply.Error); } reply.Clear(); Thread.Sleep(1000); if (!ReadDataBlock(Client.Read(it, attributeIndex), reply)) { throw new GXDLMSException(reply.Error); } } //Update data type. if (it.GetDataType(attributeIndex) == DataType.None) { it.SetDataType(attributeIndex, reply.DataType); } return(Client.UpdateValue(it, attributeIndex, reply.Value)); } return(null); }
/// <summary> /// Called when new object is selected. This will update the access rights. /// </summary> /// <param name="view"></param> /// <param name="target"></param> /// <param name="connected"></param> public static void UpdateAccessRights(IGXDLMSView view, GXDLMSObject target, bool connected) { //Update attributes. List <int> attributeIndexes = new List <int>(); List <int> methodIndexes = new List <int>(); ControlCollection controls = (view as Form).Controls; for (int index = 1; index <= (target as IGXDLMSBase).GetAttributeCount(); ++index) { if (!UpdateAccessRights(view, controls, target, index, false, connected)) { attributeIndexes.Add(index); } } //Update methods. for (int index = 0; index <= (target as IGXDLMSBase).GetMethodCount(); ++index) { if (!UpdateAccessRights(view, controls, target, index, true, connected)) { if (index != 0) { methodIndexes.Add(index); } } } foreach (int index in attributeIndexes) { OnAccessRightsChange(view, target, index, (int)target.GetAccess(index), connected, false); } //Update methods. foreach (int index in methodIndexes) { OnAccessRightsChange(view, target, index, (int)target.GetMethodAccess(index), connected, true); } }
public void UpdateValueItems(GXDLMSObject target, int index) { if (InvokeRequired) { this.BeginInvoke(new UpdateValueItemsEventHandler(UpdateValueItems), target, index); } else { GXDLMSAttributeSettings tmp = GXDLMSClient.GetAttributeInfo(target, index); if (tmp != null) { Items = tmp.Values; } else { Items = null; } if (this.Type == GXValueFieldType.TextBox) { this.Type = Items == null || Items.Count == 0 ? GXValueFieldType.TextBox : GXValueFieldType.CompoBox; } comboBox1.Items.Clear(); if (Items != null) { foreach (GXObisValueItem it in Items) { comboBox1.Items.Add(it.UIValue); } } ReadOnly = (target.GetAccess(index) & Gurux.DLMS.AccessMode.Write) == 0; } }
public void OnAccessRightsChange(int index, AccessMode access, bool connected) { bool enabled = connected && (Target.GetAccess(index) & AccessMode.Write) != 0; if (index == 4) { AddBtn.Enabled = RemoveBtn.Enabled = enabled; } }
public void OnAccessRightsChange(int index, AccessMode access, bool connected) { bool enabled = connected && (Target.GetAccess(index) & AccessMode.Write) != 0; if (index == 2) { ScriptSelectorTB.ReadOnly = !enabled; ScriptNameTB.Enabled = enabled; } else if (index == 4) { TimeAddBtn.Enabled = TimeEditBtn.Enabled = TimeRemoveBtn.Enabled = Time.Enabled = enabled; } }
public void OnAccessRightsChange(int index, AccessMode access, bool connected) { bool enabled = connected && (Target.GetAccess(index) & AccessMode.Write) != 0; if (index == 3) { TimeAddBtn.Enabled = TimeEditBtn.Enabled = TimeRemoveBtn.Enabled = enabled; } else if (index == 5) { NumberOfCallsTB.ReadOnly = !enabled; } else if (index == 6) { RingCountInWindowTB.ReadOnly = RingCountOutOfWindowTB.ReadOnly = !enabled; } }
private static bool UpdateAccessRights(IGXDLMSView view, GXDLMSClient client, System.Windows.Forms.Control.ControlCollection controls, GXDLMSObject target, int index, bool method, bool connected) { foreach (Control it in controls) { if (!method && it is GXValueField) { GXValueField obj = it as GXValueField; if (obj.Index == index) { obj.Target = target; bool access = connected; if (connected) { if (client != null) { access = client.CanWrite(target, index); } else { AccessMode am = target.GetAccess(index); access = (am & AccessMode.Write) != 0; } } OnUpdateAccessRights(view, client, obj, connected && access); return(!obj.NotifyChanges); } } else if (it is GXButton) { bool access; GXButton btn = it as GXButton; btn.Target = target; //Update custom buttons. if (method && index == 0 && btn.Index < 1) { OnUpdateAccessRights(view, client, btn, connected); continue; } if (method && btn.Index == index && btn.Action == ActionType.Action) { if (client != null) { access = client.CanInvoke(target, index); } else { access = target.GetMethodAccess(index) != MethodAccessMode.NoAccess; if (!access) { access = target.GetMethodAccess3(index) != MethodAccessMode3.NoAccess; } } OnUpdateAccessRights(view, client, btn, connected && access); return(true); } if (!method && btn.Index == index && (btn.Action == ActionType.Read || btn.Action == ActionType.Write)) { if (btn.Action == ActionType.Read) { if (client != null) { access = client.CanRead(target, index); } else { access = (target.GetAccess(index) & AccessMode.Read) != 0; if (!access) { access = (target.GetAccess3(index) & AccessMode3.Read) != 0; } } OnUpdateAccessRights(view, client, btn, connected && access); } else if (btn.Action == ActionType.Write) { if (client != null) { access = client.CanWrite(target, index); } else { access = (target.GetAccess(index) & AccessMode.Write) != 0; if (!access) { access = (target.GetAccess3(index) & AccessMode3.Write) != 0; } } OnUpdateAccessRights(view, client, btn, connected && access); } } } else if (it.Controls.Count != 0) { bool ret = UpdateAccessRights(view, client, it.Controls, target, index, method, connected); //If object is updated. if (ret) { return(true); } } } return(false); }
/// <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)) { if (sn.Version > 1) { 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 { if ((it.GetAccess(3) & AccessMode.Read) != 0) { 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 { if ((it.GetAccess(4) & AccessMode.Read) != 0) { 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); } }
private void UpdateAttributes() { bindingSource1.Clear(); List <GXDLMSAttributeSettings> list = new List <GXDLMSAttributeSettings>(); string[] names = (Target as IGXDLMSBase).GetNames(); for (int pos = 0; pos != (Target as IGXDLMSBase).GetAttributeCount(); ++pos) { GXDLMSAttributeSettings a = new GXDLMSAttributeSettings() { Index = pos + 1, Name = names[pos] }; Target.Attributes.Add(a); //Logical name is always read-only. if (pos == 0) { if (version < 3) { a.Access = AccessMode.Read; } else { a.Access3 = AccessMode3.Read; } } else { if (version < 3) { AccessMode mode = Target.GetAccess(1 + pos); a.Access = mode; } else { AccessMode3 mode = Target.GetAccess3(1 + pos); a.Access3 = mode; } } list.Add(a); } foreach (var it in list) { bindingSource1.Add(it); } Accessrights.DataSource = bindingSource1; //Show method access levels of COSEM object. bindingSource3.Clear(); list.Clear(); //Add all methods. names = (Target as IGXDLMSBase).GetMethodNames(); for (int pos = 0; pos != (Target as IGXDLMSBase).GetMethodCount(); ++pos) { GXDLMSAttributeSettings a = new GXDLMSAttributeSettings() { Index = pos + 1, Name = names[pos] }; Target.MethodAttributes.Add(a); if (version < 3) { MethodAccessMode mode = Target.GetMethodAccess(1 + pos); a.MethodAccess = mode; } else { MethodAccessMode3 mode = Target.GetMethodAccess3(1 + pos); a.MethodAccess3 = mode; } list.Add(a); } foreach (var it in list) { bindingSource3.Add(it); } MethodAccessrights.DataSource = bindingSource3; }
public void UpdateValueItems(GXDLMSObject target, int index, object value) { if (InvokeRequired) { this.BeginInvoke(new UpdateValueItemsEventHandler(UpdateValueItems), target, index, value); } else { GXDLMSAttributeSettings tmp = GXDLMSClient.GetAttributeInfo(target, index); if (tmp != null) { Items = tmp.Values; } else { Items = null; } if (this.Type == GXValueFieldType.TextBox) { this.Type = Items == null || Items.Count == 0 ? GXValueFieldType.TextBox : GXValueFieldType.CompoBox; } else if (this.Type == GXValueFieldType.CompoBox) { comboBox1.Items.Clear(); if (Items != null && Items.Count != 0) { foreach (GXObisValueItem it in Items) { comboBox1.Items.Add(it.UIValue); } } else if (value is Enum) { foreach (var it in Enum.GetValues(value.GetType())) { comboBox1.Items.Add(it); } } } else if (this.Type == GXValueFieldType.ListBox) { listBox1.Items.Clear(); if (value is Enum) { foreach (var it in Enum.GetValues(value.GetType())) { listBox1.Items.Add(it); } } } else if (this.Type == GXValueFieldType.CheckedListBox) { checkedlistBox1.Items.Clear(); if (value is Enum) { foreach (var it in Enum.GetValues(value.GetType())) { checkedlistBox1.Items.Add(it); } } } ReadOnly = (target.GetAccess(index) & AccessMode.Write) == 0; } }
///<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]); }