Пример #1
0
        /// <summary>
        /// Download media updates.
        /// </summary>
        static void DownloadMediaUpdates(object sender, GXAsyncWork work, object[] parameters)
        {
            IGXUpdater updater = (IGXUpdater)parameters[0];
            Assembly   asm     = (Assembly)parameters[1];

            GXExternalMediaForm.CheckUpdates(updater, asm);
        }
Пример #2
0
        /// <summary>
        /// Download missing medias.
        /// </summary>
        static void DownloadMedias(object sender, GXAsyncWork work, object[] parameters)
        {
            string[] list    = (string[])parameters[0];
            string   initDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "GXDLMSDirector");
            string   medias  = Path.Combine(initDir, "Medias");

            foreach (string it in list)
            {
                IGXUpdater updater = null;
                string     path    = Path.Combine(medias, Path.GetFileName(it));
                Assembly   asm     = null;
                //Check is there new version from the media.
                if (File.Exists(path))
                {
                    asm = Assembly.LoadFile(path);
                    foreach (Type type in asm.GetTypes())
                    {
                        if (!type.IsAbstract && type.IsClass && typeof(IGXUpdater).IsAssignableFrom(type))
                        {
                            updater = Activator.CreateInstance(type) as IGXUpdater;
                            break;
                        }
                    }
                    if (updater != null && GXExternalMediaForm.CheckUpdates(updater, asm))
                    {
                        //TODO: Show that there are new updates.
                    }
                }
                else
                {
                    //If external media is missing.
                    GXExternalMediaForm.DownLoadMedia(it);
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Download missing medias.
 /// </summary>
 static void DownloadMedias(object sender, GXAsyncWork work, object[] parameters)
 {
     string[] list = (string[])parameters[0];
     foreach (string it in list)
     {
         GXExternalMediaForm.DownLoadMedia(it);
     }
 }
Пример #4
0
        /// <summary>
        /// Try to find client and server address from the meter.
        /// </summary>
        private void DiscoverMeters(object sender, GXAsyncWork work, object[] parameters)
        {
            GXDLMSClient cl = new GXDLMSClient(true, 16, 1, Authentication.None, null, (InterfaceType)Settings.Default.PlcInterface);

            byte[] data = cl.Plc.DiscoverRequest();
            ReceiveParameters <byte[]> p = new ReceiveParameters <byte[]>()
            {
                AllData  = false,
                Count    = 8,
                WaitTime = 10000,
            };
            DateTime     start = DateTime.Now;
            GXByteBuffer rd    = new GXByteBuffer();

            lock (media.Synchronous)
            {
                if (data != null)
                {
                    media.Send(data, null);
                    start = DateTime.Now;
                }
                GXReplyData reply = new GXReplyData();
                rd = new GXByteBuffer(p.Reply);
                try
                {
                    while (!work.IsCanceled)
                    {
                        p.Reply = null;
                        //If Eop is not set read one byte at time.
                        if (p.Eop == null)
                        {
                            p.Count = cl.GetFrameSize(rd);
                        }
                        if (!media.IsOpen)
                        {
                            throw new InvalidOperationException("Media is closed.");
                        }
                        if (!media.Receive(p))
                        {
                            //It's OK if there is no reply. Read again
                            continue;
                        }
                        rd.Position = 0;
                        rd.Set(p.Reply);
                        if (cl.GetData(rd, reply))
                        {
                            List <GXDLMSPlcMeterInfo> list = cl.Plc.ParseDiscover(reply.Data, (UInt16)reply.TargetAddress, (UInt16)reply.SourceAddress);
                            BeginInvoke(new AppendMeterEventHandler(OnAppendMeter), list);
                        }
                    }
                }
                catch (Exception)
                {
                    //Throw original exception.
                    throw;
                }
            }
        }
Пример #5
0
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new AsyncStateChangeEventHandler(OnAsyncStateChange), sender, work, parameters, state, text);
     }
     else
     {
         if (state != AsyncState.Start)
         {
             media.Close();
             BeginInvoke(new ResetProgressEventHandler(OnResetProgress), 0, true);
         }
     }
 }
Пример #6
0
        void CheckUpdates(object sender, GXAsyncWork work, object[] parameters)
        {
            IGXUpdater updater = (IGXUpdater)parameters[0];
            Assembly   asm     = (Assembly)parameters[1];

            if (!GXExternalMediaForm.CheckUpdates(updater, asm))
            {
                // There's nothing to update
                MessageBox.Show(Properties.Resources.ExternalMediaNoUpdates, Properties.Resources.GXDLMSDirectorTxt, MessageBoxButtons.OK);
            }
            else
            {
                if (MessageBox.Show("You need to restart application. Do you want to do it now?", Properties.Resources.GXDLMSDirectorTxt, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Application.Restart();
                }
            }
        }
Пример #7
0
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new AsyncStateChangeEventHandler(OnAsyncStateChange), sender, work, parameters, state, text);
     }
     else
     {
         if (state != AsyncState.Start)
         {
             RegisterMnu.Enabled = true;
             media.Close();
             BeginInvoke(new ResetProgressEventHandler(OnResetProgress), 0, true);
         }
         else
         {
             RegisterMnu.Enabled = false;
         }
         MetersView_SelectedIndexChanged(null, null);
     }
 }
Пример #8
0
 private void DiscoverMnu_Click(object sender, EventArgs e)
 {
     try
     {
         if (media.IsOpen)
         {
             ScanWork.Cancel();
             media.Close();
         }
         else
         {
             ClearMnu_Click(null, null);
             media.Open();
             ScanWork = new GXAsyncWork(this, OnAsyncStateChange, DiscoverMeters, OnError, null, new object[] { sender });
             ScanWork.Start();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Пример #9
0
 private void OnMediaStateChange(object sender, MediaStateEventArgs e)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new MediaStateChangeEventHandler(OnMediaStateChange), sender, e);
     }
     else
     {
         try
         {
             if (e.State == Gurux.Common.MediaState.Open)
             {
                 ScanBtn.Checked          = true;
                 MediaSettingsBtn.Enabled = MediaMnu.Enabled = settingsToolStripMenuItem.Enabled = false;
                 ProgressTb.Text          = "";
                 TraceView.Text           = "";
                 ScanMnu.Text             = "Stop";
                 UpdateStatus("Scanning");
                 ScanWork = new GXAsyncWork(this, OnAsyncStateChange, FindSettings, OnError, null, new object[] { sender });
                 ScanWork.Start();
             }
             else if (e.State == Gurux.Common.MediaState.Closed)
             {
                 ScanBtn.Checked          = false;
                 MediaSettingsBtn.Enabled = MediaMnu.Enabled = settingsToolStripMenuItem.Enabled = true;
                 ScanMnu.Text             = "Scan";
                 UpdateStatus("Ready.");
                 if (ScanWork != null)
                 {
                     ScanWork.Cancel();
                 }
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
Пример #10
0
 static void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
 }
Пример #11
0
        static void Main()
        {
            //Allow only one instance.
            bool first;

            try
            {
                first = mutex.WaitOne(TimeSpan.Zero, true);
            }
            catch (AbandonedMutexException)
            {
                first = true;
            }
            if (first)
            {
                try
                {
                    //Update previous installed settings.
                    //If file is corrupted it's found from:
                    //%USERPROFILE%\AppData\Local\Gurux_Ltd\
                    //This might happen if Windows is not closed correctly.
                    if (Properties.Settings.Default.UpdateSettings)
                    {
                        Properties.Settings.Default.Upgrade();
                        Properties.Settings.Default.UpdateSettings = false;
                        Properties.Settings.Default.Save();
                        Gurux.DLMS.UI.GXDlmsUi.Upgrade();
                    }
#if (NET46)
                    //This is needed to make Gurux.MQTT visible.
                    try
                    {
                        new Gurux.MQTT.GXMqtt();
                        //Make AMI visible.
                        new Gurux.DLMS.AMI.UI.GXDlmsAmi();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }
#endif

                    string initDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "GXDLMSDirector");
                    if (!Directory.Exists(initDir))
                    {
                        Directory.CreateDirectory(initDir);
                    }
                    string updates = Path.Combine(initDir, "Updates");
                    string medias  = Path.Combine(initDir, "Medias");
                    if (Directory.Exists(updates))
                    {
                        if (!Directory.Exists(medias))
                        {
                            Directory.CreateDirectory(medias);
                        }
                        DirectoryInfo di = new DirectoryInfo(updates);
                        foreach (string it in Properties.Settings.Default.ExternalMedias.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            FileInfo fi = new FileInfo(Path.Combine(updates, Path.GetFileName(it)));
                            if (fi.Exists)
                            {
                                try
                                {
                                    File.Copy(fi.FullName, Path.Combine(medias, fi.Name), true);
                                    File.Delete(fi.FullName);
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine(ex.Message);
                                }
                            }
                        }
                    }

                    try
                    {
                        SetAddRemoveProgramsIcon();
                        Directory.SetCurrentDirectory(initDir);
                        //Load external medias.
                        List <string> missingMedias    = new List <string>();
                        List <string> downloadedMedias = new List <string>();
                        foreach (string it in Properties.Settings.Default.ExternalMedias.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            if (GXExternalMediaForm.IsDownloaded(it))
                            {
                                FileInfo fi = new FileInfo(Path.Combine(medias, Path.GetFileName(it)));
                                if (!fi.Exists)
                                {
                                    missingMedias.Add(it);
                                }
                                else
                                {
                                    downloadedMedias.Add(it);
                                }
                            }
                            else if (File.Exists(it))
                            {
                                Assembly assembly = Assembly.LoadFile(it);
                            }
                        }
                        if (missingMedias.Count != 0)
                        {
                            //Download media again if not found.
                            GXAsyncWork checkUpdates = new GXAsyncWork(null, OnAsyncStateChange, DownloadMedias, OnError, null, new object[] { missingMedias.ToArray() });
                            checkUpdates.Start();
                        }
                        if (downloadedMedias.Count != 0)
                        {
                            //Download media again if not found.
                            GXAsyncWork checkUpdates = new GXAsyncWork(null, OnAsyncStateChange, DownloadMedias, OnError, null, new object[] { downloadedMedias.ToArray() });
                            checkUpdates.Start();
                        }
                    }
                    catch (Exception)
                    {
                    }
                    MainForm.InitMain();
                }
                catch (Exception Ex)
                {
                    GXDLMS.Common.Error.ShowError(null, Ex);
                }
                mutex.ReleaseMutex();
            }
            else
            {
                foreach (Process p in Process.GetProcessesByName("GXDLMSDirector"))
                {
                    Gurux.Win32.SetForegroundWindow(p.MainWindowHandle);
                }
            }
        }
Пример #12
0
        static void Main()
        {
            //Allow only one instance.
            bool first;

            try
            {
                first = mutex.WaitOne(TimeSpan.Zero, true);
            }
            catch (AbandonedMutexException)
            {
                first = true;
            }
            if (first)
            {
                try
                {
                    //Update previous installed settings.
                    if (Properties.Settings.Default.UpdateSettings)
                    {
                        Properties.Settings.Default.Upgrade();
                        Properties.Settings.Default.UpdateSettings = false;
                        Properties.Settings.Default.Save();
                    }

                    Assembly asm     = Assembly.GetExecutingAssembly();
                    string   updates = Path.Combine(Path.GetDirectoryName(asm.Location), "Updates");
                    string   medias  = Path.Combine(Path.GetDirectoryName(asm.Location), "Medias");
                    if (Directory.Exists(updates))
                    {
                        if (!Directory.Exists(medias))
                        {
                            Directory.CreateDirectory(medias);
                        }
                        DirectoryInfo di = new DirectoryInfo(updates);
                        foreach (FileInfo it in di.GetFiles("*.dll"))
                        {
                            try
                            {
                                File.Copy(it.FullName, Path.Combine(medias, it.Name), true);
                                File.Delete(it.FullName);
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Debug.WriteLine(ex.Message);
                            }
                        }
                    }

                    string initDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "GXDLMSDirector");
                    try
                    {
                        if (!Directory.Exists(initDir))
                        {
                            Directory.CreateDirectory(initDir);
                        }
                        SetAddRemoveProgramsIcon();
                        Directory.SetCurrentDirectory(initDir);
                        LoadMedias(new DirectoryInfo(medias));
                        //Load external medias.
                        List <string> missingMedias    = new List <string>();
                        List <string> downloadedMedias = new List <string>();
                        foreach (string it in Properties.Settings.Default.ExternalMedias.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            if (GXExternalMediaForm.IsDownloaded(it))
                            {
                                FileInfo fi = new FileInfo(Path.Combine(medias, Path.GetFileName(it)));
                                if (!fi.Exists)
                                {
                                    missingMedias.Add(it);
                                }
                                else
                                {
                                    downloadedMedias.Add(it);
                                }
                            }
                            else if (File.Exists(it))
                            {
                                Assembly assembly = Assembly.LoadFile(it);
                            }
                        }
                        if (missingMedias.Count != 0)
                        {
                            //Download media again if not found.
                            GXAsyncWork checkUpdates = new GXAsyncWork(null, OnAsyncStateChange, DownloadMedias, OnError, null, new object[] { missingMedias.ToArray() });
                            checkUpdates.Start();
                        }
                        if (downloadedMedias.Count != 0)
                        {
                            //Download media again if not found.
                            GXAsyncWork checkUpdates = new GXAsyncWork(null, OnAsyncStateChange, DownloadMedias, OnError, null, new object[] { downloadedMedias.ToArray() });
                            checkUpdates.Start();
                        }
                    }
                    catch (Exception)
                    {
                    }
                    MainForm.InitMain();
                }
                catch (Exception Ex)
                {
                    GXDLMS.Common.Error.ShowError(null, Ex);
                }
                mutex.ReleaseMutex();
            }
            else
            {
                foreach (Process p in Process.GetProcessesByName("GXDLMSDirector"))
                {
                    Gurux.Win32.SetForegroundWindow(p.MainWindowHandle);
                }
            }
        }
Пример #13
0
        /// <summary>
        /// Try to find client and server address from the meter.
        /// </summary>
        private void FindSettings(object sender, GXAsyncWork work, object[] parameters)
        {
            GXDLMSDevice dev = new GXDLMSDevice(media);

            dev.Comm.client.UseLogicalNameReferencing = dev.UseLogicalNameReferencing = true;
            dev.Conformance = (int)new GXDLMSClient(true).ProposedConformance;
            dev.WaitTime    = -Properties.Settings.Default.HdlcSearchWaitTime;
            if ((media is GXSerial serial) && Settings.Default.HdlcAddressUseOpticalProbe)
            {
                try
                {
                    int    baudRate = serial.BaudRate;
                    string manufactureID;
                    dev.StartProtocol         = StartProtocolType.IEC;
                    dev.InterfaceType         = InterfaceType.HdlcWithModeE;
                    media.OnMediaStateChange -= OnMediaStateChange;
                    try
                    {
                        dev.Manufacturers = Manufacturers;
                        manufactureID     = dev.Comm.InitializeIEC();
                        media.Trace       = TraceLevel.Verbose;
                    }
                    finally
                    {
                        media.OnMediaStateChange += OnMediaStateChange;
                    }
                    BeginInvoke(new AppendTextEventHandler(OnAppendText),
                                "++++++++++++++++++++++++++++++++++++" + Environment.NewLine +
                                "Connect to the meter using IEC handshaking." + Environment.NewLine +
                                "Manufacturer is " + manufactureID + Environment.NewLine +
                                "Baud rate is " + baudRate + Environment.NewLine);
                }
                catch (Exception ex)
                {
                    BeginInvoke(new AppendTextEventHandler(OnAppendText),
                                "Failed to receive reply from the optical probe." + Environment.NewLine +
                                "++++++++++++++++++++++++++++++++++++" + Environment.NewLine +
                                "Checking device with baud rate " + ex.Message + Environment.NewLine);
                    return;
                }
            }
            if (media is GXSerial && Settings.Default.HdlcAddressScanBaudRates && !Settings.Default.HdlcAddressUseOpticalProbe)
            {
                int           original = (media as GXSerial).BaudRate;
                List <string> rates    = new List <string>(Settings.Default.HdlcAddressBaudRates.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
                int           count    = rates.Count;
                bool          first    = true;
                foreach (string it in rates)
                {
                    int rate = int.Parse(it);
                    if (rate != 0)
                    {
                        (media as GXSerial).BaudRate = rate;
                        BeginInvoke(new AppendTextEventHandler(OnAppendText),
                                    "++++++++++++++++++++++++++++++++++++" + Environment.NewLine +
                                    "Checking device with baud rate " + rate + Environment.NewLine);
                        if (CheckSnrm(dev, rates.Count, first))
                        {
                            return;
                        }
                        first = false;
                        if (Settings.Default.HdlcConnectionDelay != 0)
                        {
                            BeginInvoke(new AppendTextEventHandler(OnAppendText),
                                        string.Format("Waiting {0} seconds before the new try.", Settings.Default.HdlcConnectionDelay)
                                        + Environment.NewLine);
                            Thread.Sleep(Settings.Default.HdlcConnectionDelay * 1000);
                        }
                    }
                    --count;
                }
                (media as GXSerial).BaudRate = original;
            }
            else
            {
                if (!CheckSnrm(dev, 1, true))
                {
                    BeginInvoke(new ChangeAddressEventHandler(OnChangeAddress), 0, true);
                }
            }
        }
Пример #14
0
        /// <summary>
        /// Try to find client and server address from the meter.
        /// </summary>
        private void FindSettings(object sender, GXAsyncWork work, object[] parameters)
        {
            GXDLMSDevice dev = new GXDLMSDevice(media);

            dev.WaitTime = 1;
            if ((media is GXSerial serial) && Settings.Default.HdlcAddressUseOpticalProbe)
            {
                try
                {
                    string manufactureID;
                    dev.StartProtocol         = StartProtocolType.IEC;
                    media.OnMediaStateChange -= OnMediaStateChange;
                    try
                    {
                        dev.Manufacturers = Manufacturers;
                        manufactureID     = dev.Comm.InitializeIEC();
                    }
                    finally
                    {
                        media.OnMediaStateChange += OnMediaStateChange;
                    }
                    BeginInvoke(new AppendTextEventHandler(OnAppendText),
                                "++++++++++++++++++++++++++++++++++++" + Environment.NewLine +
                                "Connect to the meter using IEC handshaking." + Environment.NewLine +
                                "Manufacturer is " + manufactureID + Environment.NewLine +
                                "Baud rate is " + serial.BaudRate + Environment.NewLine);
                }
                catch (Exception ex)
                {
                    BeginInvoke(new AppendTextEventHandler(OnAppendText),
                                "Failed to receive reply from the optical probe." + Environment.NewLine +
                                "++++++++++++++++++++++++++++++++++++" + Environment.NewLine +
                                "Checking device with baud rate " + ex.Message + Environment.NewLine);
                    return;
                }
            }
            if (media is GXSerial && Settings.Default.HdlcAddressScanBaudRates && !Settings.Default.HdlcAddressUseOpticalProbe)
            {
                int           original = (media as GXSerial).BaudRate;
                List <string> rates    = new List <string>(Settings.Default.HdlcAddressBaudRates.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
                int           count    = rates.Count;
                bool          first    = true;
                foreach (string it in rates)
                {
                    int rate = int.Parse(it);
                    if (rate != 0)
                    {
                        (media as GXSerial).BaudRate = rate;
                        Thread.Sleep(1000);
                        BeginInvoke(new AppendTextEventHandler(OnAppendText),
                                    "++++++++++++++++++++++++++++++++++++" + Environment.NewLine +
                                    "Checking device with baud rate " + rate + Environment.NewLine);
                        if (CheckSnrm(dev, rates.Count, first))
                        {
                            break;
                        }
                        first = false;
                    }
                    --count;
                }
                (media as GXSerial).BaudRate = original;
            }
            else
            {
                CheckSnrm(dev, 1, true);
            }
        }