Example #1
0
        public async Task <bool> StoreTelemetryAsync(RawMsg msg)
        {
            bool result = false;

            try
            {
                using (var _dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
                {
                    msg.MsgBody.ToList().ForEach(kp =>
                    {
                        if (kp.Value != null)
                        {
                            var tdata = new TelemetryData()
                            {
                                DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1)
                            };
                            tdata.FillKVToMe(kp);
                            _dbContext.Set <TelemetryData>().Add(tdata);
                        }
                    });
                    var result1 = await _dbContext.SaveAsync <TelemetryLatest>(msg.MsgBody, msg.DeviceId, msg.DataSide);

                    result1.exceptions?.ToList().ForEach(ex =>
                    {
                        _logger.LogError($"{ex.Key} {ex.Value} {Newtonsoft.Json.JsonConvert.SerializeObject(msg.MsgBody[ex.Key])}");
                    });
                    _logger.LogInformation($"新增({msg.DeviceId})遥测数据更新最新信息{result1.ret}");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{msg.DeviceId}数据处理失败{ex.Message} {ex.InnerException?.Message} ");
            }
            return(result);
        }
Example #2
0
        public async Task <(bool result, List <TelemetryData> telemetries)> StoreTelemetryAsync(RawMsg msg)
        {
            bool result = false;
            List <TelemetryData> telemetries = new List <TelemetryData>();

            try
            {
                using var scope = _scopeFactor.CreateScope();

                using (var db = scope.ServiceProvider.GetService <IShardingDbAccessor>())
                {
                    var lst = new List <TelemetryData>();
                    msg.MsgBody.ToList().ForEach(kp =>
                    {
                        if (kp.Value != null)
                        {
                            var tdata = new TelemetryData()
                            {
                                DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key
                            };
                            tdata.FillKVToMe(kp);
                            lst.Add(tdata);
                            telemetries.Add(tdata);
                        }
                    });
                    int ret = await db.InsertAsync(lst);

                    _logger.LogInformation($"新增({msg.DeviceId})遥测数据{ret}");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{msg.DeviceId}数据处理失败{ex.Message} {ex.InnerException?.Message} ");
            }


            try
            {
                using (var scope = _scopeFactor.CreateScope())
                {
                    using (var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
                    {
                        var result1 = await dbContext.SaveAsync <TelemetryLatest>(msg.MsgBody, msg.DeviceId, msg.DataSide);

                        result1.exceptions?.ToList().ForEach(ex =>
                        {
                            _logger.LogError(ex.Value, $"{ex.Key} {ex.Value.Message} {ex.Value.InnerException?.Message}");
                        });
                        _logger.LogInformation($"新增({msg.DeviceId})遥测数据更新最新信息{result1.ret}");
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{msg.DeviceId}数据处理失败{ex.Message} {ex.InnerException?.Message} ");
            }
            return(result, telemetries);
        }
Example #3
0
        public async Task <bool> StoreTelemetryAsync(RawMsg msg)
        {
            bool result = false;

            try
            {
                CheckDataBase();
                List <string> lst = new List <string>();
                msg.MsgBody.ToList().ForEach(kp =>
                {
                    if (kp.Value != null)
                    {
                        TelemetryData tdata = new TelemetryData()
                        {
                            DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1)
                        };
                        tdata.FillKVToMe(kp);
                        string _type  = "";
                        string _value = "";
                        // value_boolean bool, value_string binary(4096), value_long bigint,value_datetime timestamp,value_double double,value_json binary(4096) ,value_xml binary
                        switch (tdata.Type)
                        {
                        case DataType.Boolean:
                            _type  = "value_boolean";
                            _value = tdata.Value_Boolean.ToString().ToLower();
                            break;

                        case DataType.String:
                            _type  = "value_string";
                            _value = $"'{tdata.Value_String?.Replace("'", "\\'")}'";
                            break;

                        case DataType.Long:
                            _type  = "value_long";
                            _value = $"{tdata.Value_Long}";
                            break;

                        case DataType.Double:
                            _type  = "value_double";
                            _value = $"{tdata.Value_Double}";
                            break;

                        case DataType.Json:        //td 一条记录16kb , 因此为了写更多数据, 我们json  xml binary 全部使用 string
                            _type  = "value_string";
                            _value = $"'{tdata.Value_Json?.Replace("'", "\\'")}'";
                            break;

                        case DataType.XML:
                            _type  = "value_string";
                            _value = $"'{tdata.Value_XML?.Replace("'", "\\'")}'";
                            break;

                        case DataType.Binary:
                            _type  = "value_string";
                            _value = $"\"{Hex.ToHexString(tdata.Value_Binary)}\"";
                            break;

                        case DataType.DateTime:
                            _type  = "value_datetime";
                            _value = $"{tdata.Value_DateTime.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds}";
                            break;

                        default:
                            break;
                        }
                        string vals = $"device_{tdata.DeviceId:N}_{ Pinyin4Net.GetPinyin(tdata.KeyName, PinyinFormat.WITHOUT_TONE).Replace(" ", string.Empty).Replace("@", string.Empty)} USING telemetrydata TAGS('{tdata.DeviceId:N}','{tdata.KeyName}')  (ts,value_type,{_type}) values (now,{(int)tdata.Type},{_value})";
                        lst.Add(vals);
                    }
                });

                TaosConnection _taos = _taospool.Get();
                if (_taos.State != System.Data.ConnectionState.Open)
                {
                    _taos.Open();
                }
                var cmd = _taos.CreateCommand($"INSERT INTO {string.Join("\r\n", lst)}");
                _logger.LogInformation(cmd.CommandText);
                int dt = await cmd.ExecuteNonQueryAsync();

                _taospool.Return(_taos);
                _logger.LogInformation($"数据入库完成,共数据{lst.Count}条,写入{dt}条");
            }
            catch (TaosException ex)
            {
                _logger.LogError(ex, $"{msg.DeviceId}数据处理失败{ex.ErrorCode}-{ex.Message} {ex.InnerException?.Message}");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{msg.DeviceId}数据处理失败{ex.Message} {ex.InnerException?.Message} ");
            }
            return(result);
        }
Example #4
0
        public Task <bool> StoreTelemetryAsync(RawMsg msg)
        {
            bool            result = false;
            PinusConnection _pinus = _pinuspool.Get();

            try
            {
                string tablename = GetDevTableName(msg);
                var    _havedev  = _pinus.CreateCommand($"select count(*) from sys_table where tabname =  '{tablename}'").ExecuteScalar() as long?;
                if ((long)_havedev == 0)
                {
                    _pinus.CreateCommand($"CREATE TABLE {tablename} (devid bigint,tstamp datetime,value_type  tinyint,value_boolean bool, value_string string, value_long bigint,value_datetime datetime,value_double double)").ExecuteNonQuery();
                }

                msg.MsgBody.ToList().ForEach(kp =>
                {
                    if (kp.Value != null)
                    {
                        List <PinusParameter> parameters = new List <PinusParameter>();
                        TelemetryData tdata = new TelemetryData()
                        {
                            DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1)
                        };
                        tdata.FillKVToMe(kp);
                        string _type = "";
                        var cmd      = _pinus.CreateCommand();
                        switch (tdata.Type)
                        {
                        case DataType.Boolean:
                            _type = "value_boolean";
                            cmd.Parameters.Add(new PinusParameter(_type, tdata.Value_Boolean));
                            break;

                        case DataType.String:
                            _type = "value_string";
                            cmd.Parameters.Add(new PinusParameter(_type, tdata.Value_String));
                            break;

                        case DataType.Long:
                            _type = "value_long";
                            cmd.Parameters.Add(new PinusParameter(_type, tdata.Value_Long));
                            break;

                        case DataType.Double:
                            _type = "value_double";
                            cmd.Parameters.Add(new PinusParameter(_type, tdata.Value_Double));
                            break;

                        case DataType.Json:
                            _type = "value_string";
                            cmd.Parameters.Add(new PinusParameter(_type, tdata.Value_String));
                            break;

                        case DataType.XML:
                            _type = "value_string";
                            cmd.Parameters.Add(new PinusParameter(_type, tdata.Value_XML));
                            break;

                        case DataType.Binary:
                            _type = "value_string";
                            cmd.Parameters.Add(new PinusParameter(_type, Hex.ToHexString(tdata.Value_Binary)));
                            break;

                        case DataType.DateTime:
                            _type = "value_datetime";
                            cmd.Parameters.Add(new PinusParameter(_type, tdata.Value_DateTime));
                            break;

                        default:
                            break;
                        }
                        long?_keyid = GetKeyId(msg.DeviceId, kp.Key, _pinus);
                        if (!_keyid.HasValue)
                        {
                            long?maxid   = _pinus.CreateCommand($"select  max(devid)  from sys_dev  where tabname='{tablename}'").ExecuteScalar() as long?;
                            long currdev = maxid.GetValueOrDefault() + 1;
                            _pinus.CreateCommand($"INSERT INTO sys_dev(tabname, devname,devid,expand) VALUES('{tablename}','{kp.Key}',{currdev},'{(int)tdata.Type}')").ExecuteNonQuery();
                            _keyid = GetKeyId(msg.DeviceId, kp.Key, _pinus);
                        }
                        cmd.CommandText = $"INSERT INTO {tablename} (devid,tstamp,value_type,{_type}) VALUES({_keyid}, now(), {(int)tdata.Type}, @{_type})";
                        _logger.LogInformation(cmd.CommandText);
                        try
                        {
                            int dt = cmd.ExecuteNonQuery();
                            result = dt > 0;
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex, $"{msg.DeviceId}数据处理失败{ex.Message} {ex.InnerException?.Message}");
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{msg.DeviceId}数据处理失败{ex.Message} {ex.InnerException?.Message} ");
            }
            finally
            {
                _pinuspool.Return(_pinus);
            }
            return(Task.FromResult(result));
        }