コード例 #1
0
        public async Task SaveThermometerStatus(Thermometer device)
        {
            using (var cnn = SimpleDbConnection())
            {
                cnn.Open();

                bool exist = (await cnn.QueryAsync <int>(
                                  @"SELECT count(*) FROM Thermometer WHERE OpenID=@openId AND Sn=@Sn",
                                  device
                                  ))
                             .Single() > 0;

                if (!exist)
                {
                    logger.LogWarning($"来自{device.IPAddress}的设备{device.Sn}不存在。");
                    return;
                }

                var trans = cnn.BeginTransaction();

                //更新设备状态
                await cnn.ExecuteAsync(
                    @"
                        UPDATE Thermometer 
                        SET 
                            WiFiStrength = @WiFiStrength, 
                            SSID=@SSID, 
                            Firmware=@Firmware,
                            Temperature=@Temperature,
                            Power=@Power,
                            Charge=@Charge,
                            Battery=@Battery,
                            IPAddress=@IPAddress,
                            LastUpdate=@LastUpdate
                        WHERE
                            Sn=@Sn;"
                    , device);

                //添加温度历史
                await cnn.ExecuteAsync(
                    @"INSERT INTO TemperatureHistory 
                        (Sn, Temperature, Battery, IPAddress) 
                    VALUES 
                        ( @Sn, @Temperature, @Battery, @IPAddress);"
                    , device);

                trans.Commit();
            }
        }
コード例 #2
0
        public async Task <Thermometer> AddThermometer(string openId, string sn, string name)
        {
            Thermometer device = new Thermometer(openId, sn, name);

            using (var cnn = SimpleDbConnection())
            {
                cnn.Open();
                device.Id = (await cnn.QueryAsync <int>(
                                 @"INSERT INTO Thermometer 
                        (Sn, OpenId, Name) 
                    VALUES 
                        (  @Sn, @OpenId ,@Name);
                    select last_insert_rowid()", device))
                            .First();
            }

            return(device);
        }