public static void Main(string[] args) { var jsonString = @"{""date"" : ""19:21:17 28/3/2020"", ""temp"" : 5}"; Message message = Message.ToMessage(jsonString); // converte json pra Message Console.WriteLine(message); Console.WriteLine((ValueType)message.GetDate() ?? -5); // se a data nao for null entao imprime-a }
/** * Salva uma mensagem recebida na base de dados. */ public static void AddMessage(Message message) { MySqlConnection sqlConnection = new MySqlConnection(_CONNECTION); sqlConnection.Open(); using var cmd = new MySqlCommand(_INSERT_MEDIDAS, sqlConnection); cmd.Parameters.AddWithValue(_P_DATA, message.GetDate()); cmd.Parameters.AddWithValue(_P_TEMP, message.temp); cmd.Parameters.AddWithValue(_P_UMIDADE, message.humidity); cmd.Parameters.AddWithValue(_P_UID, message.uid); cmd.Prepare(); cmd.ExecuteNonQuery(); }