Esempio n. 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;
                }
            }
        }
Esempio n. 2
0
        static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            Console.WriteLine(string.Format(
                                  "------ PublishReceived\nTopic:{0} DupFlag:{1} QosLevel:{2} Retain:{3}",
                                  e.Topic,
                                  e.DupFlag,
                                  e.QosLevel,
                                  e.Retain
                                  ));

            if (!string.IsNullOrEmpty(e.Topic) && e.Message != null && e.Message.Length > 0)
            {
                string msg = Encoding.UTF8.GetString(e.Message);
                try
                {
                    if (string.IsNullOrEmpty(msg))
                    {
                        Console.WriteLine("MESSAGE: EMPTY");
                    }
                    else if (e.Topic.StartsWith("gateways/"))
                    {
                        ApiGatewayStatus status = JsonConvert.DeserializeObject <ApiGatewayStatus>(msg);
                        Console.WriteLine(string.Format("STATUS: {0}", status));
                    }
                    else if (e.Topic.StartsWith("nodes/"))
                    {
                        ApiNodePacket packet = JsonConvert.DeserializeObject <ApiNodePacket>(msg);
                        Console.WriteLine(string.Format("PACKET: {0}", packet));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("JSON ERROR: {0}", ex.Message));
                    Console.WriteLine(string.Format("MESSAGE: {0}", msg));
                }
            }
            else
            {
                Console.WriteLine("ERROR: EMPTY MESSAGE");
            }
        }