Beispiel #1
0
        /// <summary>
        /// Add gateway to database
        /// </summary>
        /// <param name="gw_db_list"></param>
        /// <param name="status"></param>
        private static void DBCheckGateway(Dictionary <string, Gateway> gw_db_list, ApiGatewayStatus status)
        {
            if (!gw_db_list.ContainsKey(status.eui))
            {
                try
                {
                    Console.WriteLine(string.Format("Add gateway {0} to database", status.eui));

                    Gateway gw = new Gateway();
                    gw.eui       = status.eui;
                    gw.latitude  = status.latitude;
                    gw.longitude = status.longitude;
                    gw.time      = status.time;
                    using (LgwDbContext ctx = new LgwDbContext())
                    {
                        gw_db_list.Add(gw.eui, gw);
                        ctx.Gateway.Add(gw);
                        ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Error: {0}", ex.Message));
                    throw ex;
                }
            }
        }
Beispiel #2
0
        private static void UseREST()
        {
            string server       = Properties.Settings.Default.Server;
            string serverFormat = Properties.Settings.Default.ServerFormat;
            string gatewayUrl   = Properties.Settings.Default.GatewayUrl;
            string nodeUrl      = Properties.Settings.Default.NodeUrl;
            bool   useDB        = Properties.Settings.Default.UseDB;
            int    limit        = Properties.Settings.Default.Limit;

            Dictionary <string, Gateway> gw_list = new Dictionary <string, Gateway>();

            try
            {
                if (useDB)
                {
                    // Load gateways list from database
                    using (LgwDbContext ctx = new LgwDbContext())
                    {
                        List <Gateway> list = ctx.Database.SqlQuery <Gateway>(@"
SET NOCOUNT ON
SELECT * FROM [Gateway]
").ToList();
                        gw_list = new Dictionary <string, Gateway>(list.Count);

                        foreach (Gateway gw in list)
                        {
                            if (gw_list.ContainsKey(gw.eui))
                            {                                   // Multiple gateway EUI found
                                Console.WriteLine(string.Format("Duplicate gateway {0} in database", gw.eui));
                                WaitKeyPress();
                                Environment.Exit(1);
                            }
                            gw_list.Add(gw.eui, gw);
                        }
                    }

                    /*
                     * // Load nodes list from database
                     * using (LgwDbContext ctx = new LgwDbContext())
                     * {
                     *      List<Gateway> list = ctx.Database.SqlQuery<Gateway>(@"
                     * SET NOCOUNT ON
                     * SELECT * FROM [Gateway]
                     * ").ToList();
                     *      gw_list = new Dictionary<string, Gateway>(list.Count);
                     *
                     *      foreach (Gateway gw in list)
                     *      {
                     *              if (gw_list.ContainsKey(gw.eui))
                     *              {	// Multiple gateway EUI found
                     *                      Console.WriteLine(string.Format("Duplicate gateway {0} in database", gw.eui));
                     *                      WaitKeyPress();
                     *                      Environment.Exit(1);
                     *              }
                     *              gw_list.Add(gw.eui, gw);
                     *      }
                     * }
                     */
                }

                RestClient client = new RestClient(server);

                foreach (string eui in Properties.Settings.Default.NodeEUI)
                {
                    Console.WriteLine(string.Format("Last {0} messages for nodes {1}", limit == 0 ? "all" : limit.ToString(), eui));

                    RestRequest request = new RestRequest(nodeUrl, Method.GET);
                    if (!string.IsNullOrEmpty(serverFormat))
                    {
                        request.AddParameter("format", serverFormat);
                    }
                    if (limit > 0)
                    {
                        request.AddParameter("limit", limit);
                    }
                    request.AddUrlSegment("eui", eui);

                    IRestResponse <List <ApiNodePacket> > response = client.Execute <List <ApiNodePacket> >(request);

                    if (response.ResponseStatus == ResponseStatus.Completed && response.Data != null)
                    {
                        foreach (ApiNodePacket data in response.Data)
                        {
                            if (useDB)
                            {
                                DBCheckGateway(gw_list, data.gateway_eui);
                                // DBCheckNode(gw_list, status);
                            }
                            Console.WriteLine(data.ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error:");
                        if (response.ErrorException != null)
                        {
                            Console.WriteLine(response.ErrorException);
                        }
                        Console.WriteLine(response.Content);
                        break;
                    }
                }

                foreach (string eui in Properties.Settings.Default.GatewayEUI)
                {
                    Console.WriteLine(string.Format("Last {0} status messages for gateway {1}", limit == 0 ? "all" : limit.ToString(), eui));

                    RestRequest request = new RestRequest(gatewayUrl, Method.GET);
                    if (!string.IsNullOrEmpty(serverFormat))
                    {
                        request.AddParameter("format", serverFormat);
                    }
                    if (limit > 0)
                    {
                        request.AddParameter("limit", limit);
                    }
                    request.AddUrlSegment("eui", eui);

                    IRestResponse <List <ApiGatewayStatus> > response = client.Execute <List <ApiGatewayStatus> >(request);

                    if (response.ResponseStatus == ResponseStatus.Completed && response.Data != null)
                    {
                        foreach (ApiGatewayStatus status in response.Data)
                        {
                            if (useDB)
                            {
                                DBCheckGateway(gw_list, status);
                            }
                            Console.WriteLine(status.ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error:");
                        if (response.ErrorException != null)
                        {
                            Console.WriteLine(response.ErrorException);
                        }
                        Console.WriteLine(response.Content);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Error: {0}", ex.Message));
                WaitKeyPress();
                Environment.Exit(2);
            }
            WaitKeyPress();
            Environment.Exit(0);
        }