public void UpdateImage(GXDLMSImageTransfer target, byte[] data, string Identification) { //Check that image transfer ia enabled. byte[] reply = ReadDataBlock(Client.Read(target, 5)); Client.UpdateValue(reply, target, 5); if (!target.ImageTransferEnabled) { throw new Exception("Image transfer is not enabled"); } //Step 1: The client gets the ImageBlockSize. reply = ReadDataBlock(Client.Read(target, 2)); Client.UpdateValue(reply, target, 2); // Step 2: The client initiates the Image transfer process. ReadDataBlock(target.ImageTransferInitiate(Client, Identification, data.Length)); // Step 3: The client transfers ImageBlocks. int ImageBlockCount; ReadDataBlock(target.ImageBlockTransfer(Client, data, out ImageBlockCount)); //Step 4: The client checks the completeness of the Image in //each server individually and transfers any ImageBlocks not (yet) transferred; Client.UpdateValue(reply, target, 2); // Step 5: The Image is verified; ReadDataBlock(target.ImageVerify(Client)); // Step 6: Before activation, the Image is checked; //Get list to imaages to activate. reply = ReadDataBlock(Client.Read(target, 7)); Client.UpdateValue(reply, target, 7); bool bFound = false; foreach (GXDLMSImageActivateInfo it in target.ImageActivateInfo) { if (it.Identification == Identification) { bFound = true; break; } } //Read image transfer status. reply = ReadDataBlock(Client.Read(target, 6)); Client.UpdateValue(reply, target, 6); if (target.ImageTransferStatus != ImageTransferStatus.VerificationSuccessful) { throw new Exception("Image transfer status is " + target.ImageTransferStatus.ToString()); } if (!bFound) { throw new Exception("Image not found."); } //Step 7: Activate image. ReadDataBlock(target.ImageActivate(Client)); }
protected override void PreAction(ValueEventArgs[] args) { foreach (ValueEventArgs it in args) { System.Diagnostics.Debug.WriteLine("Action " + it.Target.LogicalName); if (it.Target is GXDLMSPushSetup && it.Index == 1) { SendPush(it.Target as GXDLMSPushSetup); it.Handled = true; } if (it.Target is GXDLMSImageTransfer) { GXDLMSImageTransfer i = it.Target as GXDLMSImageTransfer; //Image name and size to transfer if (it.Index == 1) { ImageUpdate = ASCIIEncoding.ASCII.GetString((byte[])(it.Parameters as object[])[0]); string file = Path.Combine(Path.GetDirectoryName(typeof(GXDLMSBase).Assembly.Location), ImageUpdate + ".exe"); System.Diagnostics.Debug.WriteLine("Updating image" + ImageUpdate + " Size:" + (it.Parameters as object[])[1]); using (var writer = File.Create(file)) { } } //Transfers one block of the Image to the server else if (it.Index == 2) { string file = Path.Combine(Path.GetDirectoryName(typeof(GXDLMSBase).Assembly.Location), ImageUpdate + ".exe"); object[] p = (object[])it.Parameters; using (BinaryWriter writer = new BinaryWriter(new FileStream(file, FileMode.Append))) { writer.Write((byte[])p[1]); } } //Verifies the integrity of the Image before activation. else if (it.Index == 3) { string file = Path.Combine(Path.GetDirectoryName(typeof(GXDLMSBase).Assembly.Location), ImageUpdate + ".exe"); i.ImageTransferStatus = ImageTransferStatus.VerificationInitiated; //Check that size match. uint size = (uint)new FileInfo(file).Length; if (size != i.ImageSize) { i.ImageTransferStatus = ImageTransferStatus.VerificationFailed; } else { i.ImageTransferStatus = ImageTransferStatus.VerificationSuccessful; } } //Activates the Image. else if (it.Index == 4) { } } } }
public void PreAction(GXActionArgs arg) { GXDLMSImageTransfer it = Target as GXDLMSImageTransfer; if (arg.Action == ActionType.Action) { if (arg.Index == 1) { if (arg.Tag is GXImageUpdateStatus status) { //Initiate the Image transfer process. arg.Value = it.ImageTransferInitiate(arg.Client, status.ImageIdentifier, status.Image.Length); } else { //Check that delay is correct and save it. GetDelay(arg); if (arg.Exception != null) { return; } GXManufacturerCollection manufacturers = new GXManufacturerCollection(); GXManufacturerCollection.ReadManufacturerSettings(manufacturers); GXManufacturer man = manufacturers.FindByIdentification(arg.Client.ManufacturerId); Properties.Settings.Default.ImageDelay = (int)arg.Value / 1000; GXImageDlg dlg = new GXImageDlg(man.ManucatureSettings, arg.Client); OnImageDialog(dlg, arg); if (arg.Action == ActionType.None) { return; } arg.Index = 5; arg.Action = ActionType.Read; return; } } else if (arg.Index == 2) { //Start image block transfer. GXImageUpdateStatus status = (GXImageUpdateStatus)arg.Tag; int imageBlockCount; arg.Value = it.ImageBlockTransfer(arg.Client, status.Image, status.ImageStartIndex, out imageBlockCount); OnDescription("Sending " + imageBlockCount + " blocks."); } else if (arg.Index == 3) { arg.Value = it.ImageVerify(arg.Client); OnDescription("Verifying image."); } else if (arg.Index == 4) { arg.Value = it.ImageActivate(arg.Client); OnDescription("Activating image."); } } }
public void PreAction(GXActionArgs arg) { GXDLMSImageTransfer it = Target as GXDLMSImageTransfer; if (arg.Action == ActionType.Action) { if (arg.Index == 1) { if (!updatingImage) { //Check that delay is correct and save it. GetDelay(arg); if (arg.Exception != null) { return; } Properties.Settings.Default.ImageDelay = (int)arg.Value / 1000; GXImageDlg dlg = new GXImageDlg(); OnImageDialog(dlg, arg); if (arg.Action == ActionType.None) { return; } arg.Index = 5; arg.Action = ActionType.Read; transformingImage = false; updatingImage = true; return; } //Initiate the Image transfer process. arg.Value = it.ImageTransferInitiate(arg.Client, imageIdentifier, image.Length); imageIdentifier = null; updatingImage = false; statusReadCount = 0; } else if (arg.Index == 2) { //Start image block transfer. int imageBlockCount; arg.Value = it.ImageBlockTransfer(arg.Client, image, out imageBlockCount); OnDescription("Sending " + imageBlockCount + " blocks."); } else if (arg.Index == 3) { arg.Value = it.ImageVerify(arg.Client); OnDescription("Verifying image."); } else if (arg.Index == 4) { arg.Value = it.ImageActivate(arg.Client); OnDescription("Activating image."); } } }
public void OnValueChanged(int index, object value, bool user) { if (index == 5) { GXDLMSImageTransfer target = Target as GXDLMSImageTransfer; this.ImageTransferEnabledCB.Checked = target.ImageTransferEnabled; } else if (index != 0) { throw new IndexOutOfRangeException("index"); } }
public void OnValueChanged(int attributeID, object value) { if (attributeID == 5) { GXDLMSImageTransfer target = Target as GXDLMSImageTransfer; this.ImageTransferEnabledCB.Checked = target.ImageTransferEnabled; } else if (attributeID != 0) { throw new IndexOutOfRangeException("attributeID"); } }
public void OnValueChanged(GXDLMSViewArguments arg) { GXDLMSImageTransfer target = Target as GXDLMSImageTransfer; if (arg.Index == 3) { ImageTransferredBlocksStatusTB.Text = (string)arg.Value; } else if (arg.Index == 5) { ImageTransferEnabledCB.CheckedChanged -= new System.EventHandler(ImageTransferEnabledCB_CheckedChanged); ImageTransferEnabledCB.Checked = target.ImageTransferEnabled; ImageTransferEnabledCB.CheckedChanged += new System.EventHandler(ImageTransferEnabledCB_CheckedChanged); } else if (arg.Index == 6) { ImageTransferStatusTb.Text = target.ImageTransferStatus.ToString(); ManualBtn_CheckedChanged(null, null); } else if (arg.Index == 7) { ImagesView.Items.Clear(); if (target.ImageActivateInfo != null) { foreach (GXDLMSImageActivateInfo it in target.ImageActivateInfo) { ListViewItem li = ImagesView.Items.Add(it.Size.ToString()); if (GXByteBuffer.IsAsciiString(it.Identification)) { li.SubItems.Add(ASCIIEncoding.ASCII.GetString(it.Identification)); } else { li.SubItems.Add(GXDLMSTranslator.ToHex(it.Identification)); } if (GXByteBuffer.IsAsciiString(it.Signature)) { li.SubItems.Add(ASCIIEncoding.ASCII.GetString(it.Signature)); } else { li.SubItems.Add(GXDLMSTranslator.ToHex(it.Signature)); } li.Tag = it; } } } else if (arg.Index != 0) { throw new IndexOutOfRangeException("index"); } }
/// <summary> /// Is image updated manually. /// </summary> private void ManualBtn_CheckedChanged(object sender, EventArgs e) { if (InvokeRequired) { BeginInvoke(new EventHandler(ManualBtn_CheckedChanged), sender, e); } else if (Target != null) { Properties.Settings.Default.ImageManualUpdate = ManualBtn.Checked; GXDLMSImageTransfer target = Target as GXDLMSImageTransfer; DelayTb.ReadOnly = Properties.Settings.Default.ImageManualUpdate || !ManualBtn.Enabled; VerifyImageBtn.Enabled = VerifyAllowed && Properties.Settings.Default.ImageManualUpdate; ActivateImageBtn.Enabled = ActivationAllowed && Properties.Settings.Default.ImageManualUpdate; } }
/// <summary> /// Is image updated manually. /// </summary> private void ManualBtn_CheckedChanged(object sender, EventArgs e) { if (InvokeRequired) { BeginInvoke(new EventHandler(ManualBtn_CheckedChanged), sender, e); } else if (Target != null) { Properties.Settings.Default.ImageManualUpdate = ManualBtn.Checked; GXDLMSImageTransfer target = Target as GXDLMSImageTransfer; DelayTb.ReadOnly = Properties.Settings.Default.ImageManualUpdate; VerifyImageBtn.Enabled = Properties.Settings.Default.ImageManualUpdate && (target.ImageTransferStatus == ImageTransferStatus.TransferInitiated || target.ImageTransferStatus == ImageTransferStatus.VerificationFailed); ActivateImageBtn.Enabled = Properties.Settings.Default.ImageManualUpdate && target.ImageTransferStatus == ImageTransferStatus.VerificationSuccessful; } }
public void OnMethodAccessRightsChange(GXDLMSViewArguments arg) { VerifyAllowed = arg.Connected && arg.Client.CanInvoke(Target, 3); ActivationAllowed = arg.Connected && arg.Client.CanInvoke(Target, 4); if (arg.Index == 2) { GXDLMSImageTransfer target = Target as GXDLMSImageTransfer; ManualBtn.Enabled = !arg.Connected; VerifyImageBtn.Enabled = VerifyAllowed && Properties.Settings.Default.ImageManualUpdate && (target.ImageTransferStatus == ImageTransferStatus.TransferInitiated || target.ImageTransferStatus == ImageTransferStatus.VerificationFailed); ActivateImageBtn.Enabled = ActivationAllowed && Properties.Settings.Default.ImageManualUpdate && target.ImageTransferStatus == ImageTransferStatus.VerificationSuccessful; if (ManualBtn.Enabled) { ManualBtn_CheckedChanged(null, null); } } }
void Init() { Media.OnReceived += new Gurux.Common.ReceivedEventHandler(OnReceived); Media.OnClientConnected += new Gurux.Common.ClientConnectedEventHandler(OnClientConnected); Media.OnClientDisconnected += new Gurux.Common.ClientDisconnectedEventHandler(OnClientDisconnected); Media.OnError += new Gurux.Common.ErrorEventHandler(OnError); Media.Open(); /////////////////////////////////////////////////////////////////////// //Add Logical Device Name. 123456 is meter serial number. GXDLMSData ldn = new GXDLMSData("0.0.42.0.0.255"); ldn.Value = "Gurux123456"; //Value is get as Octet String. ldn.SetDataType(2, DataType.OctetString); ldn.SetUIDataType(2, DataType.String); Items.Add(ldn); //Add firmware version. GXDLMSData fw = new GXDLMSData("1.0.0.2.0.255"); fw.Value = "Gurux FW 0.0.1"; Items.Add(fw); //Add Last average. GXDLMSRegister r = new GXDLMSRegister("1.1.21.25.0.255"); //Set access right. Client can't change average value. Items.Add(r); //Add default clock. Clock's Logical Name is 0.0.1.0.0.255. GXDLMSClock clock = new GXDLMSClock(); clock.Begin = new GXDateTime(-1, 9, 1, -1, -1, -1, -1); clock.End = new GXDateTime(-1, 3, 1, -1, -1, -1, -1); clock.Deviation = 0; Items.Add(clock); //Add Tcp Udp setup. Default Logical Name is 0.0.25.0.0.255. GXDLMSTcpUdpSetup tcp = new GXDLMSTcpUdpSetup(); Items.Add(tcp); /////////////////////////////////////////////////////////////////////// //Add Load profile. GXDLMSProfileGeneric pg = new GXDLMSProfileGeneric("1.0.99.1.0.255"); //Set capture period to 60 second. pg.CapturePeriod = 60; //Maximum row count. pg.ProfileEntries = 100000; pg.SortMethod = SortMethod.FiFo; pg.SortObject = clock; //Add columns. //Set saved attribute index. pg.CaptureObjects.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(clock, new GXDLMSCaptureObject(2, 0))); //Set saved attribute index. pg.CaptureObjects.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(r, new GXDLMSCaptureObject(2, 0))); Items.Add(pg); //Add initial rows. //Generate Profile Generic data file lock (FileLock) { using (var writer = File.CreateText(GetdataFile())) { //Create 10 000 rows for profile generic file. //In example profile generic we have two columns. //Date time and integer value. int rowCount = 10000; DateTime dt = DateTime.Now; //Reset minutes and seconds to Zero. dt = dt.AddSeconds(-dt.Second); dt = dt.AddMinutes(-dt.Minute); dt = dt.AddHours(-(rowCount - 1)); StringBuilder sb = new StringBuilder(); for (int pos = 0; pos != rowCount; ++pos) { sb.Append(dt.ToString(CultureInfo.InvariantCulture)); sb.Append(';'); sb.AppendLine(Convert.ToString(pos + 1)); dt = dt.AddHours(1); } sb.Length -= 2; writer.Write(sb.ToString()); } } /////////////////////////////////////////////////////////////////////// //Add Auto connect object. GXDLMSAutoConnect ac = new GXDLMSAutoConnect(); ac.Mode = AutoConnectMode.AutoDiallingAllowedAnytime; ac.Repetitions = 10; ac.RepetitionDelay = 60; //Calling is allowed between 1am to 6am. ac.CallingWindow.Add(new KeyValuePair <GXDateTime, GXDateTime>(new GXDateTime(-1, -1, -1, 1, 0, 0, -1), new GXDateTime(-1, -1, -1, 6, 0, 0, -1))); ac.Destinations = new string[] { "www.gurux.org" }; Items.Add(ac); /////////////////////////////////////////////////////////////////////// //Add Activity Calendar object. GXDLMSActivityCalendar activity = new GXDLMSActivityCalendar(); activity.CalendarNameActive = "Active"; activity.SeasonProfileActive = new GXDLMSSeasonProfile[] { new GXDLMSSeasonProfile("Summer time", new GXDateTime(-1, 3, 31, -1, -1, -1, -1), "") }; activity.WeekProfileTableActive = new GXDLMSWeekProfile[] { new GXDLMSWeekProfile("Monday", 1, 1, 1, 1, 1, 1, 1) }; activity.DayProfileTableActive = new GXDLMSDayProfile[] { new GXDLMSDayProfile(1, new GXDLMSDayProfileAction[] { new GXDLMSDayProfileAction(new GXTime(DateTime.Now), "0.1.10.1.101.255", 1) }) }; activity.CalendarNamePassive = "Passive"; activity.SeasonProfilePassive = new GXDLMSSeasonProfile[] { new GXDLMSSeasonProfile("Winter time", new GXDateTime(-1, 10, 30, -1, -1, -1, -1), "") }; activity.WeekProfileTablePassive = new GXDLMSWeekProfile[] { new GXDLMSWeekProfile("Tuesday", 1, 1, 1, 1, 1, 1, 1) }; activity.DayProfileTablePassive = new GXDLMSDayProfile[] { new GXDLMSDayProfile(1, new GXDLMSDayProfileAction[] { new GXDLMSDayProfileAction(new GXTime(DateTime.Now), "0.1.10.1.101.255", 1) }) }; activity.Time = new GXDateTime(DateTime.Now); Items.Add(activity); /////////////////////////////////////////////////////////////////////// //Add Optical Port Setup object. GXDLMSIECOpticalPortSetup optical = new GXDLMSIECOpticalPortSetup(); optical.DefaultMode = OpticalProtocolMode.Default; optical.ProposedBaudrate = BaudRate.Baudrate9600; optical.DefaultBaudrate = BaudRate.Baudrate300; optical.ResponseTime = LocalPortResponseTime.ms200; optical.DeviceAddress = "Gurux"; optical.Password1 = "Gurux1"; optical.Password2 = "Gurux2"; optical.Password5 = "Gurux5"; Items.Add(optical); /////////////////////////////////////////////////////////////////////// //Add Demand Register object. GXDLMSDemandRegister dr = new GXDLMSDemandRegister(); dr.LogicalName = "0.0.1.0.0.255"; dr.CurrentAverageValue = (uint)10; dr.LastAverageValue = (uint)20; dr.Status = (byte)1; dr.StartTimeCurrent = dr.CaptureTime = new GXDateTime(DateTime.Now); dr.Period = 10; dr.NumberOfPeriods = 1; Items.Add(dr); /////////////////////////////////////////////////////////////////////// //Add Register Monitor object. GXDLMSRegisterMonitor rm = new GXDLMSRegisterMonitor(); rm.LogicalName = "0.0.1.0.0.255"; rm.Thresholds = new object[] { (int)0x1234, (int)0x5678 }; GXDLMSActionSet set = new GXDLMSActionSet(); set.ActionDown.LogicalName = rm.LogicalName; set.ActionDown.ScriptSelector = 1; set.ActionUp.LogicalName = rm.LogicalName; set.ActionUp.ScriptSelector = 2; rm.Actions = new GXDLMSActionSet[] { set }; rm.MonitoredValue.Update(r, 2); Items.Add(rm); /////////////////////////////////////////////////////////////////////// //Add Activate test mode Script table object. GXDLMSScriptTable st = new GXDLMSScriptTable("0.1.10.1.101.255"); GXDLMSScript s = new GXDLMSScript(); s.Id = 1; GXDLMSScriptAction a = new GXDLMSScriptAction(); a.Target = null; s.Actions.Add(a); st.Scripts.Add(s); Items.Add(st); /////////////////////////////////////////////////////////////////////// //Add action schedule object. GXDLMSActionSchedule actionS = new GXDLMSActionSchedule(); actionS.Target = st; actionS.ExecutedScriptSelector = 1; actionS.Type = SingleActionScheduleType.SingleActionScheduleType1; actionS.ExecutionTime = new GXDateTime[] { new GXDateTime(DateTime.Now) }; Items.Add(actionS); /////////////////////////////////////////////////////////////////////// //Add SAP Assignment object. GXDLMSSapAssignment sap = new GXDLMSSapAssignment(); sap.SapAssignmentList.Add(new KeyValuePair <UInt16, string>(1, "Gurux")); sap.SapAssignmentList.Add(new KeyValuePair <UInt16, string>(16, "Gurux-2")); Items.Add(sap); /////////////////////////////////////////////////////////////////////// //Add Auto Answer object. GXDLMSAutoAnswer aa = new GXDLMSAutoAnswer(); aa.Mode = AutoAnswerMode.Connected; aa.ListeningWindow.Add(new KeyValuePair <GXDateTime, GXDateTime>(new GXDateTime(-1, -1, -1, 6, -1, -1, -1), new GXDateTime(-1, -1, -1, 8, -1, -1, -1))); aa.Status = AutoAnswerStatus.Inactive; aa.NumberOfCalls = 0; aa.NumberOfRingsInListeningWindow = 1; aa.NumberOfRingsOutListeningWindow = 2; Items.Add(aa); /////////////////////////////////////////////////////////////////////// //Add Modem Configuration object. GXDLMSModemConfiguration mc = new GXDLMSModemConfiguration(); mc.CommunicationSpeed = BaudRate.Baudrate57600; GXDLMSModemInitialisation init = new GXDLMSModemInitialisation(); init.Request = "AT"; init.Response = "OK"; init.Delay = 0; mc.InitialisationStrings = new GXDLMSModemInitialisation[] { init }; Items.Add(mc); /////////////////////////////////////////////////////////////////////// //Add Mac Address Setup object. GXDLMSMacAddressSetup mac = new GXDLMSMacAddressSetup(); mac.MacAddress = "00:11:22:33:44:55:66"; Items.Add(mac); /////////////////////////////////////////////////////////////////////// //Add Image transfer object. GXDLMSImageTransfer i = new GXDLMSImageTransfer(); Items.Add(i); /////////////////////////////////////////////////////////////////////// //Add IP4 Setup object. GXDLMSIp4Setup ip4 = new GXDLMSIp4Setup(); //Get local IP address. var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { ip4.IPAddress = ip.ToString(); } } Items.Add(ip4); //Add Push Setup. (On Connectivity) GXDLMSPushSetup push = new GXDLMSPushSetup("0.0.25.9.0.255"); //Send Push messages to this address as default. push.Destination = ip4.IPAddress + ":7000"; Items.Add(push); //Add push object itself. This is needed to tell structure of data to the Push listener. push.PushObjectList.Add(new KeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(push, new GXDLMSCaptureObject(2, 0))); //Add logical device name. push.PushObjectList.Add(new KeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(ldn, new GXDLMSCaptureObject(2, 0))); //Add .0.0.25.1.0.255 Ch. 0 IPv4 setup IP address. push.PushObjectList.Add(new KeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(ip4, new GXDLMSCaptureObject(3, 0))); Items.Add(new GXDLMSSpecialDaysTable()); //Add S-FSK objects Items.Add(new GXDLMSSFSKPhyMacSetUp()); Items.Add(new GXDLMSSFSKActiveInitiator()); Items.Add(new GXDLMSSFSKMacCounters()); Items.Add(new GXDLMSSFSKMacSynchronizationTimeouts()); ///Add G3-PLC objects. Items.Add(new GXDLMSG3Plc6LoWPan()); Items.Add(new GXDLMSG3PlcMacLayerCounters()); Items.Add(new GXDLMSG3PlcMacSetup()); //Add security setup object Items.Add(new GXDLMSSecuritySetup()); /////////////////////////////////////////////////////////////////////// //Server must initialize after all objects are added. Initialize(); }
/// <summary> /// This method is used to update meter firmware. /// </summary> /// <param name="target"></param> public void ImageUpdate(GXDLMSImageTransfer target, string identification, byte[] data) { //Check that image transfer ia enabled. GXReplyData reply = new GXReplyData(); ReadDataBlock(Client.Read(target, 5), reply); Client.UpdateValue(target, 5, reply.Value); if (!target.ImageTransferEnabled) { throw new Exception("Image transfer is not enabled"); } //Step 1: Read image block size. ReadDataBlock(Client.Read(target, 2), reply); Client.UpdateValue(target, 2, reply.Value); // Step 2: Initiate the Image transfer process. ReadDataBlock(target.ImageTransferInitiate(Client, identification, data.Length), reply); // Step 3: Transfers ImageBlocks. int imageBlockCount; ReadDataBlock(target.ImageBlockTransfer(Client, data, out imageBlockCount), reply); //Step 4: Check the completeness of the Image. ReadDataBlock(Client.Read(target, 3), reply); Client.UpdateValue(target, 3, reply.Value); // Step 5: The Image is verified; ReadDataBlock(target.ImageVerify(Client), reply); // Step 6: Before activation, the Image is checked; //Get list to images to activate. ReadDataBlock(Client.Read(target, 7), reply); Client.UpdateValue(target, 7, reply.Value); bool bFound = false; foreach (GXDLMSImageActivateInfo it in target.ImageActivateInfo) { if (it.Identification == identification) { bFound = true; break; } } //Read image transfer status. ReadDataBlock(Client.Read(target, 6), reply); Client.UpdateValue(target, 6, reply.Value); if (target.ImageTransferStatus != Gurux.DLMS.Objects.Enums.ImageTransferStatus.VerificationSuccessful) { throw new Exception("Image transfer status is " + target.ImageTransferStatus.ToString()); } if (!bFound) { throw new Exception("Image not found."); } //Step 7: Activate image. ReadDataBlock(target.ImageActivate(Client), reply); }
void Init() { Media.OnReceived += new Gurux.Common.ReceivedEventHandler(OnReceived); Media.OnClientConnected += new Gurux.Common.ClientConnectedEventHandler(OnClientConnected); Media.OnClientDisconnected += new Gurux.Common.ClientDisconnectedEventHandler(OnClientDisconnected); Media.OnError += new Gurux.Common.ErrorEventHandler(OnError); Media.Open(); /////////////////////////////////////////////////////////////////////// //Add Logical Device Name. 123456 is meter serial number. GXDLMSData d = new GXDLMSData("0.0.42.0.0.255"); d.Value = "Gurux123456"; //Set access right. Client can't change Device name. d.SetAccess(2, AccessMode.ReadWrite); //Value is get as Octet String. d.SetDataType(2, DataType.OctetString); d.SetUIDataType(2, DataType.String); Items.Add(d); //Add Last average. GXDLMSRegister r = new GXDLMSRegister("1.1.21.25.0.255"); //Set access right. Client can't change average value. r.SetAccess(2, AccessMode.Read); Items.Add(r); //Add default clock. Clock's Logical Name is 0.0.1.0.0.255. GXDLMSClock clock = new GXDLMSClock(); clock.Begin = new GXDateTime(-1, 9, 1, -1, -1, -1, -1); clock.End = new GXDateTime(-1, 3, 1, -1, -1, -1, -1); clock.Deviation = 0; Items.Add(clock); //Add Tcp Udp setup. Default Logical Name is 0.0.25.0.0.255. GXDLMSTcpUdpSetup tcp = new GXDLMSTcpUdpSetup(); Items.Add(tcp); /////////////////////////////////////////////////////////////////////// //Add Load profile. GXDLMSProfileGeneric pg = new GXDLMSProfileGeneric("1.0.99.1.0.255"); //Set capture period to 60 second. pg.CapturePeriod = 60; //Maximum row count. pg.ProfileEntries = 100; pg.SortMethod = SortMethod.FiFo; pg.SortObject = clock; //Add columns. //Set saved attribute index. pg.CaptureObjects.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(clock, new GXDLMSCaptureObject(2, 0))); //Set saved attribute index. pg.CaptureObjects.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(r, new GXDLMSCaptureObject(2, 0))); Items.Add(pg); //Add initial rows. pg.Buffer.Add(new object[] { DateTime.Now, (int)10 }); /////////////////////////////////////////////////////////////////////// //Add Auto connect object. GXDLMSAutoConnect ac = new GXDLMSAutoConnect(); ac.Mode = AutoConnectMode.AutoDiallingAllowedAnytime; ac.Repetitions = 10; ac.RepetitionDelay = 60; //Calling is allowed between 1am to 6am. ac.CallingWindow.Add(new KeyValuePair <GXDateTime, GXDateTime>(new GXDateTime(-1, -1, -1, 1, 0, 0, -1), new GXDateTime(-1, -1, -1, 6, 0, 0, -1))); ac.Destinations = new string[] { "www.gurux.org" }; Items.Add(ac); /////////////////////////////////////////////////////////////////////// //Add Activity Calendar object. GXDLMSActivityCalendar activity = new GXDLMSActivityCalendar(); activity.CalendarNameActive = "Active"; activity.SeasonProfileActive = new GXDLMSSeasonProfile[] { new GXDLMSSeasonProfile("Summer time", new GXDateTime(-1, 3, 31, -1, -1, -1, -1), "") }; activity.WeekProfileTableActive = new GXDLMSWeekProfile[] { new GXDLMSWeekProfile("Monday", 1, 1, 1, 1, 1, 1, 1) }; activity.DayProfileTableActive = new GXDLMSDayProfile[] { new GXDLMSDayProfile(1, new GXDLMSDayProfileAction[] { new GXDLMSDayProfileAction(new GXDateTime(DateTime.Now), "test", 1) }) }; activity.CalendarNamePassive = "Passive"; activity.SeasonProfilePassive = new GXDLMSSeasonProfile[] { new GXDLMSSeasonProfile("Winter time", new GXDateTime(-1, 10, 30, -1, -1, -1, -1), "") }; activity.WeekProfileTablePassive = new GXDLMSWeekProfile[] { new GXDLMSWeekProfile("Tuesday", 1, 1, 1, 1, 1, 1, 1) }; activity.DayProfileTablePassive = new GXDLMSDayProfile[] { new GXDLMSDayProfile(1, new GXDLMSDayProfileAction[] { new GXDLMSDayProfileAction(new GXDateTime(DateTime.Now), "0.0.1.0.0.255", 1) }) }; activity.Time = new GXDateTime(DateTime.Now); Items.Add(activity); /////////////////////////////////////////////////////////////////////// //Add Optical Port Setup object. GXDLMSIECOpticalPortSetup optical = new GXDLMSIECOpticalPortSetup(); optical.DefaultMode = OpticalProtocolMode.Default; optical.ProposedBaudrate = BaudRate.Baudrate9600; optical.DefaultBaudrate = BaudRate.Baudrate300; optical.ResponseTime = LocalPortResponseTime.ms200; optical.DeviceAddress = "Gurux"; optical.Password1 = "Gurux1"; optical.Password2 = "Gurux2"; optical.Password5 = "Gurux5"; Items.Add(optical); /////////////////////////////////////////////////////////////////////// //Add Demand Register object. GXDLMSDemandRegister dr = new GXDLMSDemandRegister(); dr.LogicalName = "0.0.1.0.0.255"; dr.CurrentAverageValue = (uint)10; dr.LastAverageValue = (uint)20; dr.Status = (byte)1; dr.StartTimeCurrent = dr.CaptureTime = new GXDateTime(DateTime.Now); dr.Period = 10; dr.NumberOfPeriods = 1; Items.Add(dr); /////////////////////////////////////////////////////////////////////// //Add Register Monitor object. GXDLMSRegisterMonitor rm = new GXDLMSRegisterMonitor(); rm.LogicalName = "0.0.1.0.0.255"; rm.Thresholds = new object[] { (int)0x1234, (int)0x5678 }; GXDLMSActionSet set = new GXDLMSActionSet(); set.ActionDown.LogicalName = rm.LogicalName; set.ActionDown.ScriptSelector = 1; set.ActionUp.LogicalName = rm.LogicalName; set.ActionUp.ScriptSelector = 2; rm.Actions = new GXDLMSActionSet[] { set }; rm.MonitoredValue.Update(r, 2); Items.Add(rm); /////////////////////////////////////////////////////////////////////// //Add action schedule object. GXDLMSActionSchedule actionS = new GXDLMSActionSchedule(); actionS.LogicalName = "0.0.1.0.0.255"; actionS.ExecutedScriptLogicalName = "1.2.3.4.5.6"; actionS.ExecutedScriptSelector = 1; actionS.Type = SingleActionScheduleType.SingleActionScheduleType1; actionS.ExecutionTime = new GXDateTime[] { new GXDateTime(DateTime.Now) }; Items.Add(actionS); /////////////////////////////////////////////////////////////////////// //Add SAP Assignment object. GXDLMSSapAssignment sap = new GXDLMSSapAssignment(); sap.SapAssignmentList.Add(new KeyValuePair <UInt16, string>(1, "Gurux")); sap.SapAssignmentList.Add(new KeyValuePair <UInt16, string>(16, "Gurux-2")); Items.Add(sap); /////////////////////////////////////////////////////////////////////// //Add Auto Answer object. GXDLMSAutoAnswer aa = new GXDLMSAutoAnswer(); aa.Mode = AutoConnectMode.EmailSending; aa.ListeningWindow.Add(new KeyValuePair <GXDateTime, GXDateTime>(new GXDateTime(-1, -1, -1, 6, -1, -1, -1), new GXDateTime(-1, -1, -1, 8, -1, -1, -1))); aa.Status = AutoAnswerStatus.Inactive; aa.NumberOfCalls = 0; aa.NumberOfRingsInListeningWindow = 1; aa.NumberOfRingsOutListeningWindow = 2; Items.Add(aa); /////////////////////////////////////////////////////////////////////// //Add Modem Configuration object. GXDLMSModemConfiguration mc = new GXDLMSModemConfiguration(); mc.CommunicationSpeed = BaudRate.Baudrate57600; GXDLMSModemInitialisation init = new GXDLMSModemInitialisation(); init.Request = "AT"; init.Response = "OK"; init.Delay = 0; mc.InitialisationStrings = new GXDLMSModemInitialisation[] { init }; Items.Add(mc); /////////////////////////////////////////////////////////////////////// //Add Mac Address Setup object. GXDLMSMacAddressSetup mac = new GXDLMSMacAddressSetup(); mac.MacAddress = "00:11:22:33:44:55:66"; Items.Add(mac); GXDLMSImageTransfer i = new GXDLMSImageTransfer(); Items.Add(i); /////////////////////////////////////////////////////////////////////// //Server must initialize after all objects are added. Initialize(); }
public void PostAction(GXActionArgs arg) { GXDLMSImageTransfer it = Target as GXDLMSImageTransfer; GXImageUpdateStatus status = (GXImageUpdateStatus)arg.Tag; if (arg.Action == ActionType.Read) { if (arg.Index == 5) { if (!it.ImageTransferEnabled) { GXHelpers.ShowMessageBox(this, Properties.Resources.ImageTransferDisabled, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); arg.Action = ActionType.None; return; } OnDescription(Properties.Resources.ImageTransferEnabled); //Get ImageBlockSize. arg.Index = 2; } else if (arg.Index == 2) { OnDescription(Properties.Resources.ImageBlockSize + it.ImageBlockSize); //Read image_transferred_blocks_status. arg.Index = 3; } else if (arg.Index == 3) { int index = 0; foreach (char b in it.ImageTransferredBlocksStatus) { if (b == '0') { DialogResult ret = GXHelpers.ShowMessageBox(this, index + " Image blocks already exists. Do you want to overwrite image? " + Environment.NewLine + "Answering No will update missing blocks.", "GXDLMSDirector", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (ret == DialogResult.Cancel) { arg.Action = ActionType.None; return; } if (ret == DialogResult.No) { status.ImageStartIndex = index; arg.Index = 2; arg.Action = ActionType.Action; return; } break; } ++index; } OnDescription(Properties.Resources.ImageBlockSize + it.ImageBlockSize); //Invoke Initiates image transfer. arg.Index = 1; arg.Action = ActionType.Action; } else if (arg.Index == 6) { OnImageTransferStatus(it.ImageTransferStatus); GetDelay(arg); if (arg.Exception != null) { return; } int delay = (int)arg.Value; arg.Value = null; switch (it.ImageTransferStatus) { case ImageTransferStatus.NotInitiated: if (++status.StatusReadCount > 10) { OnDescription("Failed to read Image transfer status after image transfer initiate."); GXHelpers.ShowMessageBox(this, "Failed to read Image transfer status after image transfer initiate.", "", MessageBoxButtons.OK, MessageBoxIcon.Information); arg.Action = ActionType.None; return; } Thread.Sleep(delay); arg.Index = 6; break; case ImageTransferStatus.TransferInitiated: if (!status.TransformingImage) { arg.Action = ActionType.Action; arg.Index = 2; status.TransformingImage = true; } else { if (!Properties.Settings.Default.ImageManualUpdate) { arg.Action = ActionType.Action; if (arg.Client.CanInvoke(arg.Target, 3)) { arg.Index = 3; } else if (arg.Client.CanInvoke(arg.Target, 4)) { arg.Index = 4; } else { GXHelpers.ShowMessageBox(this, Properties.Resources.ImageTransferedNotActivatedTxt, "", MessageBoxButtons.OK, MessageBoxIcon.Information); arg.Action = ActionType.None; } } else { arg.Action = ActionType.None; ManualBtn_CheckedChanged(null, null); } } break; case ImageTransferStatus.VerificationInitiated: Thread.Sleep(delay); OnDescription(Properties.Resources.VerificationInitiated); arg.Text = Properties.Resources.VerificationInitiated; arg.Index = 6; break; case ImageTransferStatus.VerificationSuccessful: arg.Index = 4; arg.Action = ActionType.Action; break; case ImageTransferStatus.VerificationFailed: OnDescription(Properties.Resources.VerificationFailed); arg.Text = Properties.Resources.VerificationFailed; arg.Action = ActionType.None; break; case ImageTransferStatus.ActivationInitiated: Thread.Sleep(delay); OnDescription(Properties.Resources.ActivationInitiated); arg.Text = Properties.Resources.ActivationInitiated; break; case ImageTransferStatus.ActivationSuccessful: OnDescription(Properties.Resources.ActivationSuccessful); GXHelpers.ShowMessageBox(this, Properties.Resources.ImageActivatedTxt, "", MessageBoxButtons.OK, MessageBoxIcon.Information); arg.Action = ActionType.None; break; case ImageTransferStatus.ActivationFailed: OnDescription(Properties.Resources.ActivationFailed); arg.Text = Properties.Resources.ActivationFailed; arg.Action = ActionType.None; break; } } } else if (arg.Action == ActionType.Action) { if (arg.Index == 1) { OnDescription("Image transfer initiated."); arg.Action = ActionType.Read; arg.Index = 6; } else if (arg.Index == 2) { OnDescription("Image transfered."); arg.Index = 6; arg.Action = ActionType.Read; } else if (arg.Index == 3) { if (arg.Exception is GXDLMSException) { //If meter is verifying the image. if ((arg.Exception as GXDLMSException).ErrorCode == (int)ErrorCode.TemporaryFailure) { if (!Properties.Settings.Default.ImageManualUpdate) { arg.Exception = null; GetDelay(arg); if (arg.Exception != null) { return; } int delay = (int)arg.Value; Thread.Sleep(delay); arg.Value = null; } } else { OnDescription("Image verifying failed."); } return; } if (!Properties.Settings.Default.ImageManualUpdate) { arg.Index = 6; arg.Action = ActionType.Read; } else { arg.Action = ActionType.None; } } else if (arg.Index == 4) { if (arg.Exception is GXDLMSException) { //If meter is activating image. if ((arg.Exception as GXDLMSException).ErrorCode == (int)ErrorCode.TemporaryFailure) { if (!Properties.Settings.Default.ImageManualUpdate) { arg.Exception = null; GetDelay(arg); if (arg.Exception != null) { return; } int delay = (int)arg.Value; Thread.Sleep(delay); arg.Value = null; } } else { OnDescription("Image activation failed."); } return; } OnDescription(Properties.Resources.ImageActivatedTxt); arg.Action = ActionType.None; arg.Rebooting = true; GXHelpers.ShowMessageBox(this, Properties.Resources.ActionImplemented, "", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
public void PostAction(GXActionArgs arg) { GXDLMSImageTransfer it = Target as GXDLMSImageTransfer; if (arg.Action == ActionType.Read) { if (arg.Index == 5) { if (!it.ImageTransferEnabled) { updatingImage = false; GXHelpers.ShowMessageBox(this, Properties.Resources.ImageTransferDisabled, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); arg.Action = ActionType.None; return; } OnDescription(Properties.Resources.ImageTransferEnabled); //Get ImageBlockSize. arg.Index = 2; } else if (arg.Index == 2) { OnDescription(Properties.Resources.ImageBlockSize + it.ImageBlockSize); //Invoke Initiates image transfer. arg.Index = 1; arg.Action = ActionType.Action; } else if (arg.Index == 6) { OnImageTransferStatus(it.ImageTransferStatus); GetDelay(arg); if (arg.Exception != null) { return; } int delay = (int)arg.Value; arg.Value = null; switch (it.ImageTransferStatus) { case ImageTransferStatus.NotInitiated: if (++statusReadCount > 10) { OnDescription("Failed to read Image transfer status after image transfer initiate."); GXHelpers.ShowMessageBox(this, "Failed to read Image transfer status after image transfer initiate.", "", MessageBoxButtons.OK, MessageBoxIcon.Information); arg.Action = ActionType.None; updatingImage = false; return; } Thread.Sleep(delay); arg.Index = 6; break; case ImageTransferStatus.TransferInitiated: if (!transformingImage) { arg.Action = ActionType.Action; arg.Index = 2; transformingImage = true; } else { if (!Properties.Settings.Default.ImageManualUpdate) { arg.Index = 3; arg.Action = ActionType.Action; } else { arg.Action = ActionType.None; ManualBtn_CheckedChanged(null, null); } } break; case ImageTransferStatus.VerificationInitiated: Thread.Sleep(delay); OnDescription(Properties.Resources.VerificationInitiated); arg.Text = Properties.Resources.VerificationInitiated; arg.Index = 6; break; case ImageTransferStatus.VerificationSuccessful: arg.Index = 4; arg.Action = ActionType.Action; break; case ImageTransferStatus.VerificationFailed: OnDescription(Properties.Resources.VerificationFailed); arg.Text = Properties.Resources.VerificationFailed; arg.Action = ActionType.None; break; case ImageTransferStatus.ActivationInitiated: Thread.Sleep(delay); OnDescription(Properties.Resources.ActivationInitiated); arg.Text = Properties.Resources.ActivationInitiated; break; case ImageTransferStatus.ActivationSuccessful: OnDescription(Properties.Resources.ActivationSuccessful); GXHelpers.ShowMessageBox(this, Properties.Resources.ImageActivatedTxt, "", MessageBoxButtons.OK, MessageBoxIcon.Information); arg.Action = ActionType.None; break; case ImageTransferStatus.ActivationFailed: OnDescription(Properties.Resources.ActivationFailed); arg.Text = Properties.Resources.ActivationFailed; arg.Action = ActionType.None; break; } } } else if (arg.Action == ActionType.Action) { if (arg.Index == 1) { OnDescription("Image transfer initiated."); arg.Action = ActionType.Read; arg.Index = 6; } else if (arg.Index == 2) { OnDescription("Image transfered."); arg.Index = 6; arg.Action = ActionType.Read; } else if (arg.Index == 3) { if (arg.Exception is GXDLMSException) { //If meter is activating image. if ((arg.Exception as GXDLMSException).ErrorCode == (int)ErrorCode.TemporaryFailure) { if (!Properties.Settings.Default.ImageManualUpdate) { arg.Exception = null; GetDelay(arg); if (arg.Exception != null) { return; } int delay = (int)arg.Value; Thread.Sleep(delay); arg.Value = null; } return; } } if (!Properties.Settings.Default.ImageManualUpdate) { arg.Index = 6; arg.Action = ActionType.Read; } else { arg.Action = ActionType.None; } } else if (arg.Index == 4) { if (arg.Exception is GXDLMSException) { //If meter is activating image. if ((arg.Exception as GXDLMSException).ErrorCode == (int)ErrorCode.TemporaryFailure) { if (!Properties.Settings.Default.ImageManualUpdate) { arg.Exception = null; GetDelay(arg); if (arg.Exception != null) { return; } int delay = (int)arg.Value; Thread.Sleep(delay); arg.Value = null; } return; } } OnDescription(Properties.Resources.ImageActivatedTxt); arg.Action = ActionType.None; arg.Rebooting = true; } } }