public GXAmiEventListener(GXAmiDataCollectorServer server, GXProxyClass pc, GXAmiClient dc) { Server = server; DC = dc; pc.OnUpdated += new PropertyUpdateEventHandler(OnUpdated); pc.OnError += new ErrorEventHandler(pc_OnError); pc.OnStateChange += new DeviceStateChangedEventHandler(pc_OnStateChange); pc.OnUpdateParameters += new UpdateparametersEventHandler(pc_OnUpdateParameters); pc.OnTrace += new TraceAddEventHandler(pc_OnTrace); }
/// <summary> /// Start GuruxAMI Data collector as console. /// </summary> /// <param name="args"></param> static void Main(string[] args) { //Update previous installed settings. if (Properties.Settings.Default.UpdateSettings) { Properties.Settings.Default.Upgrade(); Properties.Settings.Default.UpdateSettings = false; Properties.Settings.Default.Save(); } bool trace = false; GXAmiDataCollectorServer collector = null; try { for (int pos = 0; pos != args.Length; ++pos) { string tag = args[pos]; if (tag[0] == '/' || tag[0] == '-') { tag = tag.Substring(1).ToLower(); if (tag == "h") { GuruxAMI.DataCollector.Properties.Settings.Default.AmiHostName = args[++pos]; } if (tag == "p") { GuruxAMI.DataCollector.Properties.Settings.Default.AmiHostPort = args[++pos]; } //Register Data Collector again. if (tag == "r") { GuruxAMI.DataCollector.Properties.Settings.Default.AmiDCGuid = Guid.Empty; } //Trace messages if (tag == "t") { trace = true; } } } string host = GuruxAMI.DataCollector.Properties.Settings.Default.AmiHostName; if (string.IsNullOrEmpty(host)) { ShowHelp(); return; } Guid guid = GuruxAMI.DataCollector.Properties.Settings.Default.AmiDCGuid; Console.WriteLine("Starting Data Collector..."); if (!host.StartsWith("http://")) { host = "http://" + host + ":" + GuruxAMI.DataCollector.Properties.Settings.Default.AmiHostPort + "/"; } Console.WriteLine("Connecting " + host); GXAmiUser user = null; string r, dcName = null; GuruxAMI.Client.GXAmiClient cl = null; if (guid == Guid.Empty) { Console.WriteLine("Registering Data Collector to GuruxAMI Service: "); int pos = 0; do { Console.WriteLine("Enter user name:"); string username = Console.ReadLine(); if (username == "") { return; } Console.WriteLine("Enter password:"******"") { return; } cl = new GXAmiClient(host, username, password); //Get info from registered user. try { user = cl.GetUserInfo(); break; } catch(UnauthorizedAccessException) { continue; } }while(++pos != 3); //If authorisation failed. if (user == null) { return; } Console.WriteLine("Finding data collectors."); GXAmiDataCollector[] dcs = cl.GetDataCollectors(); //If there are existing DCs... if (dcs.Length != 0) { Console.WriteLine("Do you want to register new data collector or bind old? (n/b)"); do { r = Console.ReadLine().Trim().ToLower(); if (r == "n" || r == "b") { break; } } while (r == ""); } else { r = "n"; } //Old DC replaced. if (r == "b") { Console.WriteLine("Select data collector number that you want to bind:"); pos = 0; foreach (GXAmiDataCollector it in dcs) { ++pos; Console.WriteLine(pos.ToString() + ". " + it.Name); } do { r = Console.ReadLine().Trim(); int sel = 0; if (int.TryParse(r, out sel) && sel > 0 && sel <= pos) { guid = dcs[sel - 1].Guid; break; } } while (true); } else { do { Console.WriteLine("Enter name of the data collector:"); dcName = Console.ReadLine().Trim(); if (dcName == "") { return; } if (cl.Search(new string[] { dcName }, ActionTargets.DataCollector, SearchType.Name).Length == 0) { GXAmiDataCollector tmp = new GXAmiDataCollector(dcName, "", ""); cl.AddDataCollector(tmp, cl.GetUserGroups(false)); guid = tmp.Guid; break; } Console.WriteLine("Name exists. Give new one."); } while (true); } } collector = new GXAmiDataCollectorServer(host, guid); if (trace) { collector.OnTasksAdded += new TasksAddedEventHandler(OnTasksAdded); collector.OnTasksClaimed += new TasksClaimedEventHandler(OnTasksClaimed); collector.OnTasksRemoved += new TasksRemovedEventHandler(OnTasksRemoved); collector.OnError += new ErrorEventHandler(OnError); } collector.OnAvailableSerialPorts += new AvailableSerialPortsEventHandler(OnAvailableSerialPorts); GXAmiDataCollector dc = collector.Init(dcName); //If new Data collector is added bind it to the user groups. if (guid == Guid.Empty && cl != null) { cl.AddDataCollector(dc, cl.GetUserGroups(false)); } if (dc != null) { GuruxAMI.DataCollector.Properties.Settings.Default.AmiDCGuid = dc.Guid; } Console.WriteLine(string.Format("Data Collector '{0}' started.", dc.Name)); GuruxAMI.DataCollector.Properties.Settings.Default.Save(); } catch (Exception ex) { if (ex is UnauthorizedAccessException) { Console.WriteLine("Unknown data collector."); GuruxAMI.DataCollector.Properties.Settings.Default.AmiDCGuid = Guid.Empty; GuruxAMI.DataCollector.Properties.Settings.Default.Save(); } else { Console.WriteLine(ex.Message); } } //Wait until user press enter. ConsoleKeyInfo key; while ((key = System.Console.ReadKey()).Key != ConsoleKey.Enter) { System.Console.Write("\b \b"); } if (collector != null) { collector.Dispose(); } }
GXAmiDataCollectorServer StartDataCollector(Guid guid) { string baseUr = Gurux.DeviceSuite.Properties.Settings.Default.AmiHostName; if (baseUr.Contains("*")) { baseUr = baseUr.Replace("*", "localhost"); } GXAmiDataCollectorServer collector = new GXAmiDataCollectorServer(baseUr, guid); DataCollectors.Add(collector); collector.OnTasksAdded += new TasksAddedEventHandler(DC_OnTasksAdded); collector.OnTasksClaimed += new TasksClaimedEventHandler(DC_OnTasksClaimed); collector.OnTasksRemoved += new TasksRemovedEventHandler(DC_OnTasksRemoved); collector.OnError += new Gurux.Common.ErrorEventHandler(DC_OnError); collector.OnAvailableSerialPorts += new AvailableSerialPortsEventHandler(DC_OnAvailableSerialPorts); collector.Init(null); if (!Gurux.DeviceSuite.Properties.Settings.Default.AmiDataCollectors.Contains(guid.ToString())) { Gurux.DeviceSuite.Properties.Settings.Default.AmiDataCollectors.Add(guid.ToString()); } InternalDCs.Add(collector); return collector; }