/// <summary> /// 转换获取到的值 /// </summary> /// <param name="db"></param> /// <param name="sql"></param> /// <param name="devid"></param> /// <returns></returns> private List <TelemetryDataDto> SqlToTDD(PinusConnection db, string sql, Guid devid) { List <TelemetryDataDto> dt = new List <TelemetryDataDto>(); var kts = db.CreateCommand(sql).ExecuteReader().ToList <(long keyid, string keyname, string datatype)>(); kts.ForEach(kf => { TelemetryDataDto telemetry = new TelemetryDataDto() { KeyName = kf.keyname }; try { DataType datatype = (DataType)Convert.ToInt32(kf.datatype); var queryvalue = db.CreateCommand($"SELECT {datatype.ToFieldName()},tstamp from {GetDevTableName(devid)} where devid ={kf.keyid} order by tstamp desc limit 1 ") .ExecuteReader().ToList <(object obj, DateTime dt)>(); telemetry.AttachValue(datatype, queryvalue.FirstOrDefault().obj); telemetry.DateTime = queryvalue.FirstOrDefault().dt; } catch (Exception ex) { _logger.LogError(ex, $"{telemetry.KeyName}遇到{ex.Message}, sql:{sql}"); } if (!string.IsNullOrEmpty(telemetry.KeyName)) { dt.Add(telemetry); } }); return(dt); }
private List <TelemetryDataDto> SqlToTDD(TaosConnection db, string sql, string prefix, string suffix, string keyname) { List <TelemetryDataDto> dt = new List <TelemetryDataDto>(); TaosDataReader dataReader = db.CreateCommand(sql).ExecuteReader(); while (dataReader.Read()) { TelemetryDataDto telemetry = new TelemetryDataDto(); byte datatype = (byte)dataReader[dataReader.GetOrdinal($"{prefix}value_type{suffix}")]; if (string.IsNullOrEmpty(keyname)) { telemetry.KeyName = dataReader.GetString(dataReader.GetOrdinal("keyname")); } else { telemetry.KeyName = keyname; } telemetry.DateTime = dataReader.GetDateTime(dataReader.GetOrdinal($"{prefix}ts{suffix}")); switch ((DataType)datatype) { case DataType.Boolean: telemetry.Value = dataReader.GetBoolean(dataReader.GetOrdinal($"{prefix}value_boolean{suffix}")); break; case DataType.String: telemetry.Value = dataReader.GetString(dataReader.GetOrdinal($"{prefix}value_string{suffix}")); break; case DataType.Long: telemetry.Value = dataReader.GetInt64(dataReader.GetOrdinal($"{prefix}value_long{suffix}")); break; case DataType.Double: telemetry.Value = dataReader.GetDouble(dataReader.GetOrdinal($"{prefix}value_double{suffix}")); break; case DataType.Json: case DataType.XML: case DataType.Binary: telemetry.Value = dataReader.GetString(dataReader.GetOrdinal($"{prefix}value_string{suffix}")); break; case DataType.DateTime: telemetry.Value = dataReader.GetDateTime(dataReader.GetOrdinal($"{prefix}value_datetime{suffix}")); break; default: break; } dt.Add(telemetry); } return(dt); }
public async Task <int> SendTelemetryData(TelemetryData telemetry) { var telemetryDto = new TelemetryDataDto() { CreatedUtc = telemetry.Measured, GasValue = (int)telemetry.GasValue, HeatIndex = telemetry.HeatIndex, Humidity = telemetry.Humidity, Temperature = telemetry.Temperature }; using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", oauthResponse.AccessToken); httpClient.BaseAddress = new Uri(baseUrl); var JSON = JsonConvert.SerializeObject(telemetryDto); var content = new StringContent(JSON, Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync("/api/Telemetry/SaveTelemetryData", content); var result = await response.Content.ReadAsStringAsync(); if (response.StatusCode == System.Net.HttpStatusCode.OK) { return(JsonConvert.DeserializeObject <int>(result)); } else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { var settings = ApplicationData.Current.LocalSettings; settings.Values.Remove(TokenKey); return(-1); } else { throw new Exception(result); } } }
/// <summary> /// 转换获取到的值 /// </summary> /// <param name="db"></param> /// <param name="sql"></param> /// <param name="prefix"></param> /// <param name="suffix"></param> /// <param name="keyname"></param> /// <returns></returns> /// <exception cref="https://github.com/taosdata/TDengine/issues/4269">务必注意此bug</exception> private List <TelemetryDataDto> SqlToTDD(TaosConnection db, string sql, string prefix, string suffix, string keyname) { List <TelemetryDataDto> dt = new List <TelemetryDataDto>(); TaosDataReader dataReader = db.CreateCommand(sql).ExecuteReader(); while (dataReader.Read()) { TelemetryDataDto telemetry = new TelemetryDataDto(); try { int idx = dataReader.GetOrdinal($"{prefix}value_type{suffix}"); byte datatype; if (dataReader.FieldCount > idx && idx >= 0) { datatype = dataReader.GetByte(idx); } else { throw new Exception($"字段{prefix}value_type{suffix}的Index={idx}小于0或者大于FieldCount{dataReader.FieldCount},更多信息请访问 HelpLink") { HelpLink = "https://github.com/taosdata/TDengine/issues/4269" }; } if (string.IsNullOrEmpty(keyname)) { telemetry.KeyName = dataReader.GetString(dataReader.GetOrdinal("keyname")); } else { telemetry.KeyName = keyname; } telemetry.DateTime = dataReader.GetDateTime(dataReader.GetOrdinal($"{prefix}ts{suffix}")); switch ((DataType)datatype) { case DataType.Boolean: telemetry.Value = dataReader.GetBoolean(dataReader.GetOrdinal($"{prefix}value_boolean{suffix}")); break; case DataType.String: telemetry.Value = dataReader.GetString(dataReader.GetOrdinal($"{prefix}value_string{suffix}")); break; case DataType.Long: telemetry.Value = dataReader.GetInt64(dataReader.GetOrdinal($"{prefix}value_long{suffix}")); break; case DataType.Double: telemetry.Value = dataReader.GetDouble(dataReader.GetOrdinal($"{prefix}value_double{suffix}")); break; case DataType.Json: case DataType.XML: case DataType.Binary: telemetry.Value = dataReader.GetString(dataReader.GetOrdinal($"{prefix}value_string{suffix}")); break; case DataType.DateTime: telemetry.Value = dataReader.GetDateTime(dataReader.GetOrdinal($"{prefix}value_datetime{suffix}")); break; default: break; } } catch (Exception ex) { _logger.LogError(ex, $"{telemetry.KeyName}遇到{ex.Message}, sql:{sql}"); } if (!string.IsNullOrEmpty(telemetry.KeyName)) { dt.Add(telemetry); } } return(dt); }