Exemplo n.º 1
0
        /// <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();
            }
        }
Exemplo n.º 2
0
        bool IsDBCreated(out GuruxAMI.Client.GXAmiClient cl)
        {
            if (!Gurux.DeviceSuite.Properties.Settings.Default.AmiEnabled)
            {
                Gurux.DeviceSuite.Properties.Settings.Default.AmiHostName = "http://" + AddressTB.Text;
                //Start AMI Server.
                AmiForm.Start(true, true);
            }
            string baseUr = "http://" + AddressTB.Text;
            if (baseUr.Contains('*'))
            {
                baseUr = baseUr.Replace("*", "localhost");
            }
            cl = new GuruxAMI.Client.GXAmiClient(baseUr, 
                Gurux.DeviceSuite.Properties.Settings.Default.AmiUserName, 
                Gurux.DeviceSuite.Properties.Settings.Default.AmiPassword);
            bool created = false;
            try
            {
                created = cl.IsDatabaseCreated();
            }
            catch (System.Net.WebException ex)
            {
                if (ex.Status == System.Net.WebExceptionStatus.ConnectFailure ||
                    ex.Status == System.Net.WebExceptionStatus.ConnectionClosed)
                {
                    created = false;
                }
                else
                {
                    throw ex;
                }
            }
            if (!created)
            {
                if ((DialogResult)this.Invoke(new ShowQuestionEventHandler(OnShowQuestion), "GuruxAMI Database is not created. Do you want to create it?") != DialogResult.Yes)
                {
                    return false;
                }
                if ((DialogResult)this.Invoke(new ShowQuestionEventHandler(OnShowQuestion2), "") == DialogResult.OK)
                {
                    //Close AMI Server and start again.
                    AmiForm.ClosingApplication.Set();
                    AmiForm.ServerThreadClosed.WaitOne();

                    string connStr = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}",
                        Gurux.DeviceSuite.Properties.Settings.Default.AmiDatabaseHostName, Gurux.DeviceSuite.Properties.Settings.Default.AmiDatabaseName,
                        Gurux.DeviceSuite.Properties.Settings.Default.AmiDBUserName, Gurux.DeviceSuite.Properties.Settings.Default.AmiDBPassword);
                    GXAmiDataCollector dc;
                    using (IDbConnection Db = new OrmLiteConnectionFactory(connStr, true, ServiceStack.OrmLite.MySql.MySqlDialectProvider.Instance).OpenDbConnection())
                    {
                        GXDBService.CreateTables(Db, OnProgress, "gurux", "gurux");
                        dc = GXDBService.AddDataCollector(Db);
                    }
                    dc.Internal = true;
                    AmiForm.Start(true, false);
                    AmiForm.AddDataCollector(new GXAmiDataCollector[] {dc});
                    if (cl.IsDatabaseCreated())
                    {
                        this.Invoke(new ShowQuestionEventHandler(OnMessage), "Database created successfully.");
                    }
                }
            }
            return created;
        }