Ejemplo n.º 1
0
        public async Task <List <DatosSensores> > ObtenerDatosSensoresAsync()
        {
            ConexionRepositorio  cnn    = new ConexionRepositorio();
            IFirebaseClient      client = cnn.ConectarseFireBase();
            List <DatosSensores> Result = new List <DatosSensores>();

            FirebaseResponse response = await client.GetTaskAsync("LECTURAS");

            string chrr = response.Body;

            dynamic dynJson = JsonConvert.DeserializeObject(chrr);

            foreach (var item in dynJson)
            {
                DatosSensores respu = new DatosSensores();
                foreach (var tm in item)
                {
                    respu.Id           = tm.Id;
                    respu.Mac          = tm.Mac;
                    respu.Propietario  = tm.Propietario;
                    respu.Referencia   = tm.Referencia;
                    respu.Consecutivo  = tm.Consecutivo;
                    respu.Fecha        = tm.Fecha;
                    respu.HumedadPow   = tm.HumedadPow;
                    respu.TemperaturaC = tm.TemperaturaC;
                    respu.TemperaturaF = tm.TemperaturaF;
                    respu.FC28         = tm.fc28;
                    respu.lumm         = tm.luz;
                }
                Result.Add(respu);
            }
            //DatosSensores resp = response.ResultAs<DatosSensores>();
            return(Result);
        }
Ejemplo n.º 2
0
        // BeginReceive()'s call back fucntion
        public void CallBack_ReceiveMsg(IAsyncResult ar)
        {
            int             length;
            String          MsgRecvStr = null;
            SensoresContext context    = new SensoresContext(Configuration.GetConnectionString("DefaultConnection"));

            //CheckHub a = new CheckHub();

            try
            {
                // If the socket close, return the 0 value
                length = clientsock.EndReceive(ar);

                if (length > 0)
                {
                    MsgRecvStr = Encoding.Default.GetString(MsgRecvBuff, 0, length);

                    Console.WriteLine("Dato recibido:" + MsgRecvStr + "\n");

                    string[] res = MsgRecvStr.Split(" ");


                    //Seteo la temperatura
                    //a.SetTemp(MsgRecvStr);

                    //Guardo en base de datos los registros
                    //
                    try
                    {
                        /*
                         * System.Globalization.CultureInfo customCulture = new System.Globalization.CultureInfo("en-US", true);
                         *
                         * customCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd h:mm tt";
                         *
                         * System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
                         * System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture;
                         *
                         * DateTime newDate = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd h:mm tt"));
                         */

                        //string f = DateTime.Now.ToString("yyyy-MM-dd H:mm:ss");
                        DatosSensores datosSensores = new DatosSensores();
                        //System.Globalization.CultureInfo customCulture = new System.Globalization.CultureInfo("en-US", true);
                        //datosSensores.Fecha = newDate;
                        datosSensores.Temperatura  = Convert.ToDecimal(res[0]);
                        datosSensores.Humedad      = Convert.ToDecimal(res[1]);
                        datosSensores.Luminosidad  = Convert.ToDecimal(res[2]);
                        datosSensores.Voltspanel   = Convert.ToDecimal(res[3]);
                        datosSensores.Voltsbateria = 0;
                        //datosSensores.Voltsbateria = Convert.ToDecimal(res[4]);

                        context.Save(datosSensores);
                    }
                    catch (Exception ex) {
                        Console.WriteLine("Error al querer grabar", ex.Message);
                    }

                    /*Conexion con MongoDb*/

                    /*
                     * try
                     * {
                     *
                     *  MongoClient dbClient = new MongoClient("mongodb://127.0.0.1:27017");
                     *
                     *  //Database List
                     *  var dbList = dbClient.ListDatabases().ToList();
                     *
                     *  Console.WriteLine("Las base de datos son:");
                     *  foreach (var item in dbList)
                     *  {
                     *      Console.WriteLine(item);
                     *  }
                     *
                     *  Console.WriteLine("\n\n");
                     *
                     *  //Get Database and Collection
                     *  IMongoDatabase db = dbClient.GetDatabase("pruebadb");
                     *  var collList = db.ListCollections().ToList();
                     *  Console.WriteLine("las listas de colecciones son :");
                     *  Console.WriteLine("\n\n");
                     *  foreach (var item in collList)
                     *  {
                     *      Console.WriteLine(item);
                     *
                     *  }
                     * }
                     * catch (Exception ex) {
                     *  Console.WriteLine("Fallo la conexión con Mongodb");
                     * }
                     */

                    //MySql context = new SensoresContext().ConnectionString(); //HttpContext.RequestServices.GetService(typeof(coreNetMysql.Models.SensoresContext)) as SensoresContext;
                    //  context.GetAll();


                    //SensoresContext context =  new SensoresContext();


                    // The socket can continuedly wait the receive message because of this function
                    clientsock.BeginReceive(MsgRecvBuff, 0, MsgRecvBuff.Length, SocketFlags.None, new AsyncCallback(CallBack_ReceiveMsg), clientsock);
                }
                else
                {
                    // If the socket close, return the 0 value
                    // Then, socket close.
                    clientsock.Close();
                }
            }
            catch (Exception er)
            {
                Console.WriteLine(er.Message);
            }
        }