Example #1
0
 private void CheckUpdatesBtn_Click(object sender, EventArgs e)
 {
     try
     {
         if (MediaList.SelectedItems.Count != 1)
         {
             throw new Exception("Update check failed. Selected media to check updates.");
         }
         Assembly   asm     = (Assembly)MediaList.SelectedItems[0].Tag;
         IGXUpdater updater = null;
         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)
         {
             throw new Exception("Update check failed. Update checker is not supported.");
         }
         checkUpdates = new GXAsyncWork(this, OnAsyncStateChange, CheckUpdates, OnError, null, new object[] { updater, asm });
         checkUpdates.Start();
     }
     catch (Exception Ex)
     {
         GXDLMS.Common.Error.ShowError(this, Ex);
     }
 }
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     foreach (AsyncStateChangeEventHandler e in Events)
     {
         e(sender, work, parameters, state, text);
     }
 }
 /// <summary>
 /// Change Refresh button text when read is started.
 /// </summary>
 /// <param name="work"></param>
 /// <param name="sender"></param>
 /// <param name="parameters"></param>
 /// <param name="state"></param>
 /// <param name="text"></param>
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     if (state == AsyncState.Start)
     {
         RefreshBtn.Text = Gurux.DeviceSuite.Properties.Resources.CancelTxt;
     }
     else
     {
         RefreshBtn.Text = Gurux.DeviceSuite.Properties.Resources.RefreshTxt;
         DateTime? tm = work.Result as DateTime?;
         Collector.LastRequestTimeStamp = tm;
         if (tm.HasValue)
         {
             LastConnectedTB.Text = tm.Value.ToString();
         }
         else
         {
             LastConnectedTB.Text = "";
         }                
     }
 }
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     CommandPromptTB.Visible = CommandPromptClearBtn.Visible = state != AsyncState.Start;
     CancelBtn.Visible = state == AsyncState.Start;            
 }
 /// <summary>
 /// Executes connecting of the item or the parent GXDevice of it asynchronously.
 /// </summary>
 /// <param name="item">The item to be connected.</param>
 public void Connect(System.Windows.Forms.Control sender, object item)
 {
     TransactionWork = new GXAsyncWork(sender, OnAsyncStateChange, ConnectAsync, null, Gurux.DeviceSuite.Properties.Resources.ConnectingTxt, new object[] { item });
     TransactionWork.Start();
 }
 void ApplyChanges(GXDeviceProfile dp)
 {
     if (Device)
     {
         Arguments.Target = GXDevice.Create(dp.Protocol, dp.Name, "");
     }
     else
     {
         Arguments.Target = dp;
     }
     try
     {
         GXJsonParser parser = new GXJsonParser();
         GXDeviceProfileFormSettings settings = new GXDeviceProfileFormSettings();
         settings.Custom = CustomCB.Checked;
         settings.Download = DownloadCB.Checked;
         settings.SearchText = SearchTB.Text;
         settings.Earlier = ShowEarlierVersionsCB.Checked;
         Arguments.Settings = parser.Serialize(settings);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message);
     }
     if (Arguments.OnSelect != null)
     {
         if (Arguments.UseThread)
         {
             DialogResult = DialogResult.None;
             Work = new GXAsyncWork(this, OnAsyncStateChange, OnSelect, null, "Selecting", null);
             Work.Start();
             return;
         }
         else
         {
             Arguments.OnSelect(Arguments.Target, Arguments);
         }
     }
     DialogResult = DialogResult.OK;
     Close();
 }
 /// <summary>
 /// Test DB connection.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void TestBtn_Click(object sender, EventArgs e)
 {
     try
     {
         if (string.IsNullOrEmpty(AddressTB.Text))
         {
             throw new Exception("GuruxAMI host name is invalid.");
         }
         Gurux.DeviceSuite.Properties.Settings.Default.AmiHostName = "http://" + AddressTB.Text;
         TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, CreateDBAsync, null, "", null);
         TransactionWork.Start();
     }
     catch (Exception ex)
     {
         AmiForm.Start(true, true);
         GXCommon.ShowError(this, ex);
     }                
 }
        /*
        void Download()
        {
             TODO:
            string "http://www.gurux.org/GuruxDevicePublisher";
            HttpWebRequest request = WebRequest.Create("http:/www.gurux.org/") as HttpWebRequest;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
                object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                Response jsonResponse
                = objResponse as Response;
                return jsonResponse;
            }

            
            JsonServiceClient Client = new JsonServiceClient(Gurux.DeviceSuite.Properties.Settings.Default.UpdateServer);
            GXDownloadRequest download = new GXDownloadRequest();
            download.Template = target as GXDeviceProfileVersion;
            GXDownloadResponse ret = Client.Get(download);
            GXDeviceProfile type = UpdatePublishedDeviceType(download.Template, ret.Data);
            (target as GXDeviceProfileVersion).Status = DownloadStates.Installed;
            //If restart is needed.
            if (type == null)
            {
                return Guid.Empty;
            }
             
        }
         * */

        void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new AsyncStateChangeEventHandler(this.OnAsyncStateChange), sender, work, state, text);
            }
            else
            {
                bool start = state == AsyncState.Start;
                progressBar1.Visible = start;
                panel2.Enabled = DeviceProfiles.Enabled = OKBtn.Enabled = !start;
                if (state == AsyncState.Start)
                {                    
                    progressBar1.Value = 0;
                    progressBar1.Maximum = 10;
                }
                else if (state == AsyncState.Finish)
                {
                    if (parameters != null)
                    {
                        GXDeviceProfile dp = parameters[0] as GXDeviceProfile;
                        ApplyChanges(dp);
                    }
                    else
                    {
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                }
                if (start)
                {
                    ProgresssTimer.Start();
                }
                else
                {
                    ProgresssTimer.Stop();
                }
            }
        }
 /// <summary>
 /// Accept changes.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void OKBtn_Click(object sender, EventArgs e)
 {
     try
     {                
         if (DeviceProfiles.SelectedItems.Count != 1)
         {
             throw new Exception("No device profile is selected.");
         }     
         GXDeviceProfile dp = DeviceProfiles.SelectedItems[0].Tag as GXDeviceProfile;
         //Start thread and load protocol if protocol is not loaded yet.
         if (!GXDeviceList.Protocols.ContainsKey(dp.Protocol))
         {
             Work = new GXAsyncWork(this, OnAsyncStateChange, Download, null, "Downloading", new object[]{dp});
             Work.Start();
             return;
         }
         ApplyChanges(dp);
     }
     catch (Exception ex)
     {
         DialogResult = DialogResult.None;
         Gurux.Common.GXCommon.ShowError(this, ex);
     }
 }
Example #10
0
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new AsyncStateChangeEventHandler(this.OnAsyncStateChange), sender, work, state, text);
     }
     else
     {
         ParentComponent.Progress.Visible = ParentComponent.CancelOperationMenu.Enabled = state == AsyncState.Start;
         if (state == AsyncState.Start)
         {                    
             ParentComponent.StatusLbl.Text = text;
             GXAmiDevice[] devices = parameters[1] as GXAmiDevice[];
             ParentComponent.Progress.Value = 0;
             ParentComponent.Progress.Maximum = devices.Length;                    
         }
         else
         {
             ParentComponent.StatusLbl.Text = Gurux.DeviceSuite.Properties.Resources.ReadyTxt;                
         }
     }
 }
Example #11
0
 void ReadAsync(object sender, GXAsyncWork work, object[] parameters)
 {
     GXAmiClient client = (parameters[0] as GXAmiClient);
     GXAmiDevice[] devices = parameters[1] as GXAmiDevice[];
     int pos = 0;
     foreach (GXAmiDevice it in devices)
     {
         if (work.IsCanceled)
         {
             break;
         }
         BeginInvoke(new UpdateProgressEventHandler(this.OnUpdateProgress), ++pos);
         client.Read(it);
         Application.DoEvents();
     }            
 }
 public GXAmiLoginForm(GXAmiClientLoginInfo info)
 {
     InitializeComponent();
     Info = info;
     work = new GXAsyncWork(this, AsyncStateChangeEventHandler, Connect, OnError, null, null);
 }
 void Connect(object sender, GXAsyncWork work, object[] parameters)
 {
     GXAmiClient cl = new GXAmiClient(Info.Address, this.NameTB.Text, this.PasswordTB.Text);
     cl.GetUserInfo();
     if (!work.IsCanceled)
     {
         Client = cl;
     }
 }
 /// <summary>
 /// Status of work is changed.
 /// </summary>
 void AsyncStateChangeEventHandler(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     bool isTrying = state == AsyncState.Start;
     this.NameTB.ReadOnly = this.PasswordTB.ReadOnly = isTrying;
     RemembeMeCB.Enabled = this.EditHostBtn.Enabled = this.LoginBtn.Enabled = !isTrying;
     //Close Wnd if connection succeeded.
     if (Client != null)
     {
         this.DialogResult = System.Windows.Forms.DialogResult.OK;
         Info.RememberMe = RemembeMeCB.Checked;
         Info.UserName = this.NameTB.Text;
         Info.Password = PasswordTB.Text;
         Close();
     }
 }
 /// <summary>
 /// Create DB asynchronously.
 /// </summary>
 void CreateDBAsync(object sender, GXAsyncWork work, object[] parameters)
 {
     GXAmiClient cl;
     if (IsDBCreated(out cl))
     {
         this.Invoke(new ShowQuestionEventHandler(OnMessage), "GuruxAMI database is created and working.");
     }            
 }
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     tabControl1.Enabled = CancelBtn.Enabled = OkBtn.Enabled = state != AsyncState.Start;
 }
 void GetLastRequestTimeStampAsync(object sender, GXAsyncWork work, object[] parameters)
 {
     GXAmiClient cl = parameters[0] as GXAmiClient;
     GXAmiDataCollector dc = parameters[1] as GXAmiDataCollector;
     work.Result = cl.GetDataCollectorByGuid(dc.Guid).LastRequestTimeStamp;            
 }
Example #18
0
 /// <summary>
 /// Read selected DC or device.
 /// </summary>
 public void Read()
 {
     try
     {
         if (DCList.Focused)
         {
             foreach (ListViewItem it in DCList.SelectedItems)
             {
                 Client.Read(it.Tag as GXAmiDataCollector);
             }
         }
         else if (DevicesList.Focused)
         {
             if (TransactionWork != null && TransactionWork.IsRunning)
             {
                 throw new Exception("Read Failed. Transcaction already in progress.");
             }
             List<GXAmiDevice> devices = new List<GXAmiDevice>();
             foreach (ListViewItem it in DevicesList.SelectedItems)
             {
                 devices.Add(it.Tag as GXAmiDevice);                        
             }
             TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, ReadAsync, null, Gurux.DeviceSuite.Properties.Resources.ReadingTxt, new object[] { Client, devices.ToArray() });
             TransactionWork.Start();
         }                
         else
         {
             throw new Exception("Unknown target.");
         }
     }
     catch (Exception ex)
     {
         GXCommon.ShowError(this.ParentComponent, Gurux.DeviceSuite.Properties.Resources.GuruxDeviceSuiteTxt, ex);
     }
 }
 /// <summary>
 /// Refresh last access time of DC.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void RefreshBtn_Click(object sender, EventArgs e)
 {
     try
     {
         if (TransactionWork == null)
         {
             TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, GetLastRequestTimeStampAsync, null, "", new object[] { Client, Collector });
         }
         //Is work running.
         if (!TransactionWork.IsRunning)
         {
             TransactionWork.Start();
         }
         else //Wait until work ends.
         {
             TransactionWork.Wait(0);                    
         }
     }             
     catch (Exception ex)
     {
         GXCommon.ShowError(this, ex);             
     }
 }
 /// <summary>
 /// Disconnects the specified item or its parent device.
 /// </summary>
 /// <param name="item">An item to be connected.</param>
 void DisconnectAsync(object sender, GXAsyncWork work, object[] parameters)
 {
     object item = parameters[0];
     //If device list is selected.
     if (item is GXDeviceList)
     {
         ((GXDeviceList)item).Disconnect();
     }
     //If devicegroup is selected.
     else if (item is GXDeviceGroup)
     {
         ((GXDeviceGroup)item).Disconnect();
     }
     //If device is selected.
     else if (item is GXDevice)
     {
         ((GXDevice)item).Disconnect();
     }
     //If category is selected.
     else if (item is GXCategory)
     {
         ((GXCategory)item).Device.Disconnect();
     }
     //If table is selected.
     else if (item is GXTable)
     {
         ((GXTable)item).Device.Disconnect();
     }
     //If property is selected.
     else if (item is GXProperty)
     {
         ((GXProperty)item).Device.Disconnect();
     }            
 }
 void Download(object sender, GXAsyncWork work, object[] parameters)
 {
     //TODO:
     GXDeviceProfile dp = parameters[0] as GXDeviceProfile;
     GXJsonClient parser = new GXJsonClient(Arguments.DownloadServer, Arguments.UserName, Arguments.Password);
     GXDownloadRequest req = new GXDownloadRequest(dp);
     GXDownloadResponse res = parser.Post(req);
 }
Example #22
0
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new AsyncStateChangeEventHandler(this.OnAsyncStateChange), sender, work, state, text);
     }
     else
     {
         CancelOperationMenu.Enabled = state == AsyncState.Start;
         if (state == AsyncState.Start)
         {
             StatusLbl.Text = text;
         }
         else if (state == AsyncState.Cancel)
         {
             object target = parameters[0];
             GXDevice device = GXTransactionManager.GetDevice(target);
             if (device != null)
             {
                 device.Cancel();
             }
             //ToolsDisconnectMenu_Click(this, null);
         }
         else if (state == AsyncState.Finish)
         {
             StatusLbl.Text = Gurux.DeviceSuite.Properties.Resources.ReadyTxt;
         }
     }
 }
 void OnSelect(object sender, GXAsyncWork work, object[] parameters)
 {
     Arguments.OnSelect(Arguments.Target, Arguments);
 }
 /// <summary>
 /// Thread to wait media connection.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 void ConnectAsync(object sender, GXAsyncWork work, object[] parameters)
 {
     Handled.Reset();
     Client.MediaOpen((Guid)parameters[0], (string)parameters[1], (string)parameters[2], (string)parameters[3], Handled);
 }
 /// <summary>
 /// Stops monitoring the specified item or its parent device.
 /// </summary>
 /// <param name="item">An item to halt monitoring on.</param>
 void StopMonitoringAsync(object sender, GXAsyncWork work, object[] parameters)
 {
     object item = parameters[0];
     if (item is GXDeviceList)
     {
         ((GXDeviceList)item).StopMonitoring();
     }
     else if (item is GXDeviceGroup)
     {
         ((GXDeviceGroup)item).StopMonitoring();
     }
     else if (item is GXDevice)
     {
         ((GXDevice)item).StopMonitoring();
     }
     else if (item is GXCategory)
     {
         ((GXCategory)item).Device.StopMonitoring();
     }
     else if (item is GXTable)
     {
         ((GXTable)item).Device.StopMonitoring();
     }
     else if (item is GXProperty)
     {
         ((GXProperty)item).Device.StopMonitoring();
     }            
 }
 /// <summary>
 /// Clear text if connected. Otherwice reconnect.
 /// </summary>
 private void CommandPromptClearBtn_Click(object sender, EventArgs e)
 {
     try
     {
         if (CommandPromptTB.Enabled)
         {
             CommandPromptTB.Text = string.Empty;
         }
         else
         {
             TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, ConnectAsync, null, Gurux.DeviceSuite.Properties.Resources.ConnectingTxt, new object[] { Collector.Guid, Media, MediaName, Settings });
             TransactionWork.Start();
         }
     }
     catch (Exception ex)
     {
         GXCommon.ShowError(ParentDlg, Gurux.DeviceSuite.Properties.Resources.GuruxDeviceSuiteTxt, ex);
     }
 }
 void OnAsyncStateChange(object sender, GXAsyncWork work, object[] parameters, AsyncState state, string text)
 {
     panel1.Visible = panel2.Visible = MediaFrame.Visible = state != AsyncState.Start;
     ConnectingPanel.Visible = state == AsyncState.Start;
     if (state != AsyncState.Start)
     {
         if (state == AsyncState.Cancel)
         {
             Handled.Set();
             this.DialogResult = DialogResult.None;
         }
         //Close dlg if user has not cancel the connection and no error are occured.
         else if (state == AsyncState.Finish)
         {
             this.DialogResult = DialogResult.OK;
         }
     }
 }
 /// <summary>
 /// Writes the specified item.
 /// </summary>
 /// <param name="item">An item to be written</param>
 void WriteAsync(object sender, GXAsyncWork work, object[] parameters)
 {
     object item = parameters[0];
     //If device list is selected.
     if (item is GXDeviceList)
     {
         ((GXDeviceList)item).Write();
     }
     //If device group is selected.
     else if (item is GXDeviceGroup)
     {
         ((GXDeviceGroup)item).Write();
     }
     //If device is selected.
     else if (item is GXDevice)
     {
         ((GXDevice)item).Write();
     }
     //If category is selected.
     else if (item is GXCategory)
     {
         ((GXCategory)item).Write();
     }
     //If table is selected.
     else if (item is GXTable)
     {
         ((GXTable)item).Write();
     }
     //If property is selected.
     else if (item is GXProperty)
     {
         ((GXProperty)item).Write();
     }
     else
     {
         throw new Exception("GXTransactionManager.Write failed: Unknown item type: " + item.GetType().ToString());
     }            
 }
 /// <summary>
 /// Start connection to the selected media.
 /// </summary>
 private void OkBtn_Click(object sender, EventArgs e)
 {
     try
     {                
         //Apply media dialog settings.
         if (PropertiesForm != null)
         {
             ((IGXPropertyPage)PropertiesForm).Apply();
         }
         SelectedMedia.Validate();
         TransactionWork = new GXAsyncWork(this, OnAsyncStateChange, ConnectAsync, null, "", new object[] { DataCollector.Guid, SelectedMedia.MediaType, SelectedMedia.Name, SelectedMedia.Settings });
         TransactionWork.Start();
     }            
     catch (Exception ex)
     {                
         GXCommon.ShowError(this, ex);
     }
 }
 void ConnectAsync(object sender, GXAsyncWork work, object[] parameters)
 {
     ExecutedTask = Client.MediaOpen((Guid)parameters[0], (string)parameters[1], (string)parameters[2], (string)parameters[3], Connected);
     Connected.WaitOne();            
 }