Exemplo n.º 1
0
        public static void Run([IoTHubTrigger("messages/events", Connection = "IotHub", ConsumerGroup = "systemintegration"), ] EventData message, ILogger log)
        {
            log.LogInformation("I början...");
            DeviceMessage deviceMessage = JsonConvert.DeserializeObject <DeviceMessage>(Encoding.UTF8.GetString(message.Body.Array));

            using (SqlConnection sql_Connection = new SqlConnection(Environment.GetEnvironmentVariable("Sqldb_Conn")))

            {
                sql_Connection.Open();
                log.LogInformation("I mitten...");



                using (SqlCommand sql_command = new SqlCommand("INSERT INTO SensorData (Temperature, Humidity, Epochtime) VALUES(@temperature, @humidity, @epochtime)", sql_Connection)) {
                    log.LogInformation("I slutet...");
                    sql_command.Parameters.AddWithValue("@temperature", deviceMessage.temperature);
                    sql_command.Parameters.AddWithValue("@humidity", deviceMessage.humidity);
                    sql_command.Parameters.AddWithValue("@epochtime", deviceMessage.epochtime);
                    sql_command.ExecuteNonQuery();
                }
            }



            log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");
        }
Exemplo n.º 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("***Fetching data from SQL***");

            string query = "EXEC GetSQLData";

            List <DeviceMessage> deviceMessages = new List <DeviceMessage>();

            using (SqlConnection sql_Connection = new SqlConnection(Environment.GetEnvironmentVariable("Sqldb_Conn")))
            {
                sql_Connection.Open();

                using (SqlCommand sql_command = new SqlCommand(query, sql_Connection))
                {
                    try
                    {
                        using (SqlDataReader sql_DataReader = sql_command.ExecuteReader())
                        {
                            while (sql_DataReader.Read())
                            {
                                DeviceMessage deviceMessage = new DeviceMessage
                                {
                                    temperature = float.Parse(sql_DataReader["Temperature"].ToString()),
                                    humidity    = float.Parse(sql_DataReader["Humidity"].ToString()),
                                    epochtime   = long.Parse(sql_DataReader["Epochtime"].ToString()),
                                    id          = int.Parse(sql_DataReader["id"].ToString())
                                };
                                deviceMessages.Add(deviceMessage);
                            }
                        }
                    }
                    catch
                    {
                        log.LogInformation("**SQL FETCHING ERROR***");
                    }
                }
            };

            return(deviceMessages != null
                ? (ActionResult) new OkObjectResult(deviceMessages)
                : new BadRequestResult());
        }