private bool HandleMessage(string routingKey, string msg) { string tableName = "t_" + routingKey.Replace('.', '_'); string deviceType = routingKey.Split('.')[1]; bool result = false; switch (deviceType) { case "02": Temperature_Data td = JsonConvert.DeserializeObject <Temperature_Data>(msg); result = InsertTemperatureData(tableName, td); break; case "08": Strain_Data sd = JsonConvert.DeserializeObject <Strain_Data>(msg); result = InsertStrainData(tableName, sd); break; case "09": Settlement_Data obj = JsonConvert.DeserializeObject <Settlement_Data>(msg); result = InsertSettlementData(tableName, obj); break; case "11": Inclination_Data id = JsonConvert.DeserializeObject <Inclination_Data>(msg); result = InsertInclinationData(tableName, id); break; case "13": TemperatureHumidity_Data thd = JsonConvert.DeserializeObject <TemperatureHumidity_Data>(msg); result = InsertTemperatureHumidityData(tableName, thd); break; case "19": LaserRange_Data lrd = JsonConvert.DeserializeObject <LaserRange_Data>(msg); result = InsertLaserRangeData(tableName, lrd); break; default: break; } return(result); }
private bool InsertTemperatureHumidityData(string tableName, TemperatureHumidity_Data data) { string sqlStatement = "INSERT INTO " + tableName + "(Stamp,Temperature,Humidity) VALUES(@stamp,@temperature,@humidity)"; bool result = true; try { using (SqlCommand cmd = new SqlCommand(sqlStatement, sqlConnection)) { cmd.Parameters.AddWithValue("@stamp", data.TimeStamp); cmd.Parameters.AddWithValue("@temperature", data.Temperature); cmd.Parameters.AddWithValue("@humidity", data.Humidity); cmd.ExecuteNonQuery(); } } catch (Exception ex) { result = false; form.AppendLog(ex.StackTrace.ToString() + "\r\n"); } return(result); }