Esempio n. 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);
        }
Esempio n. 2
0
        public async Task <bool> StoreTelemetryAsync(RawMsg msg)
        {
            bool result = false;

            try
            {
                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, Value_DateTime = new DateTime(1970, 1, 1)
                            };
                            tdata.FillKVToMe(kp);
                            lst.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);
        }
Esempio n. 3
0
 static void Value_newMessage(RawMsg amsg, Server s)
 {
     try
     {
         if (clientStream != null)
         {
             string toSend = s.name + "\n" + amsg.now.Ticks.ToString() + "\n" + amsg.line;
             byte[] ts     = sendEnc.CryptData(Encoding.UTF8.GetBytes(toSend));
             clientStream.Write(ts, 0, ts.Length);
         }
     }
     catch { }
 }
Esempio n. 4
0
        public Task Execute(IJobExecutionContext context)
        {
            return(Task.Run(async() =>
            {
                RawMsg msg = null;
                int sec = 0;
                do
                {
                    msg = _queue.Dequeue();
                    if (msg == null)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(1));
                        sec++;
                    }
                } while (msg == null && sec < 60);
                if (msg != null)
                {
                    using (var _dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
                    {
                        var device = _dbContext.Device.FirstOrDefault(d => d.Id == msg.DeviceId);

                        if (device != null)
                        {
                            switch (msg.DataCatalog)
                            {
                            case DataCatalog.AttributeData:
                                var result2 = await _dbContext.SaveAsync <AttributeLatest>(msg.MsgBody, device.Id, msg.DataSide);
                                result2.exceptions?.ToList().ForEach(ex =>
                                {
                                    _logger.LogError($"{ex.Key} {ex.Value} {Newtonsoft.Json.JsonConvert.SerializeObject(msg.MsgBody[ex.Key])}");
                                });
                                _logger.LogInformation($"更新{device.Name}({device.Id})属性数据结果{result2.ret}");
                                break;

                            case DataCatalog.TelemetryData:
                                bool sta = await _storage.StoreTelemetryAsync(msg);
                                _logger.LogInformation($"新增{device.Name}({device.Id})遥测数据{sta}");
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }
            }));
        }
Esempio n. 5
0
 public override async Task <bool> StoreTelemetryAsync(RawMsg msg)
 {
     if (!_needcrtate)
     {
         var have = _context.Database.ExecuteScalar <long>("SELECT  count(0) FROM _timescaledb_catalog.hypertable where	 table_name='TelemetryData';");
         if (have == 0)
         {
             _context.Database.ExecuteNonQuery("SELECT create_hypertable('\"TelemetryData\"', 'DateTime', 'DeviceId', 2, create_default_indexes=>FALSE);");
             _context.Database.ExecuteNonQuery("CREATE INDEX ON \"TelemetryData\" (\"KeyName\", \"DateTime\" DESC);");
             _context.Database.ExecuteNonQuery("CREATE INDEX ON \"TelemetryData\" (\"DataSide\", \"DateTime\" DESC);");
             _context.Database.ExecuteNonQuery("CREATE INDEX ON \"TelemetryData\" (\"Type\", \"DateTime\" DESC);");
         }
         else
         {
             _needcrtate = true;
         }
     }
     return(await base.StoreTelemetryAsync(msg));
 }
Esempio n. 6
0
 public void StoreAttributeData(RawMsg msg)
 {
     Task.Run(async() =>
     {
         using (var _dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
         {
             var device = _dbContext.Device.FirstOrDefault(d => d.Id == msg.DeviceId);
             if (device != null)
             {
                 var result2 = await _dbContext.SaveAsync <AttributeLatest>(msg.MsgBody, device.Id, msg.DataSide);
                 result2.exceptions?.ToList().ForEach(ex =>
                 {
                     _logger.LogError($"{ex.Key} {ex.Value} {Newtonsoft.Json.JsonConvert.SerializeObject(msg.MsgBody[ex.Key])}");
                 });
                 _logger.LogInformation($"更新{device.Name}({device.Id})属性数据结果{result2.ret}");
             }
         }
     });
 }
Esempio n. 7
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (JWToken.Length != 0)
            {
                hash ^= JWToken.GetHashCode();
            }
            if (RawMsg.Length != 0)
            {
                hash ^= RawMsg.GetHashCode();
            }
            if (msgReceivedAt_ != null)
            {
                hash ^= MsgReceivedAt.GetHashCode();
            }
            if (mitelCallEnded_ != null)
            {
                hash ^= MitelCallEnded.GetHashCode();
            }
            return(hash);
        }
Esempio n. 8
0
        public override Task <TaskActionOutput> ExecuteAsync(TaskActionInput param)
        {
            var result = new TaskActionOutput()
            {
                DynamicOutput = new { code = ApiCode.Success, msg = "OK" }
            };

            try
            {
                var msg = new RawMsg()
                {
                    MsgType = MsgType.CoAP, MsgBody = JToken.Parse(param.Input)?.JsonToDictionary(), DataCatalog = DataCatalog.AttributeData, DataSide = DataSide.ClientSide, DeviceId = param.DeviceId
                };
                if (msg.DeviceId != Guid.Empty && msg.MsgBody?.Count > 0)
                {
                    _queue.PublishAttributeData(msg);
                }
            }
            catch (Exception ex)
            {
                result.DynamicOutput = new { code = ApiCode.Exception, msg = ex.Message };
            }
            return(Task.FromResult(result));
        }
Esempio n. 9
0
 public override Task <(bool result, List <TelemetryData> telemetries)> StoreTelemetryAsync(RawMsg msg)
 {
     if (!_needcrtate)
     {
         //解决单例注入问题 jy
         using var scope   = _scopeFactor.CreateScope();
         using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
         var have = context.Database.ExecuteScalar <long>("SELECT  count(0) FROM _timescaledb_catalog.hypertable where	 table_name='TelemetryData';");
         if (have == 0)
         {
             context.Database.ExecuteNonQuery("SELECT create_hypertable('\"TelemetryData\"', 'DateTime', 'DeviceId', 2, create_default_indexes=>FALSE);");
             context.Database.ExecuteNonQuery("CREATE INDEX ON \"TelemetryData\" (\"KeyName\", \"DateTime\" DESC);");
             context.Database.ExecuteNonQuery("CREATE INDEX ON \"TelemetryData\" (\"DataSide\", \"DateTime\" DESC);");
             context.Database.ExecuteNonQuery("CREATE INDEX ON \"TelemetryData\" (\"Type\", \"DateTime\" DESC);");
         }
         else
         {
             _needcrtate = true;
         }
     }
     return(base.StoreTelemetryAsync(msg));
 }
Esempio n. 10
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);
        }
Esempio n. 11
0
 public void StoreTelemetryData(RawMsg msg) => Task.Run(() => _storage.StoreTelemetryAsync(msg));
Esempio n. 12
0
 public static void PublishTelemetryData(this ICapPublisher cap, RawMsg msg)
 {
     cap.Publish("iotsharp.services.datastream.telemetrydata", msg);
 }
Esempio n. 13
0
 void irc_OnArchMsg(RawMsg amsg)
 {
     backlog.Enqueue(amsg);
     if (newMessage != null)
         newMessage(amsg, server);
 }
Esempio n. 14
0
 public static void PublishAttributeData(this ICapPublisher cap, RawMsg msg)
 {
     cap.Publish("iotsharp.services.datastream.attributedata", msg);
 }
Esempio n. 15
0
        public void StoreAttributeData(RawMsg msg)
        {
            Task.Run(async() =>
            {
                using (var _scope = _scopeFactor.CreateScope())
                {
                    using (var _dbContext = _scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
                    {
                        var device = _dbContext.Device.FirstOrDefault(d => d.Id == msg.DeviceId);
                        if (device != null)
                        {
                            device.CheckOrUpdateDevStatus();
                            var mb = msg.MsgBody;
                            Dictionary <string, object> dc = new Dictionary <string, object>();
                            mb.ToList().ForEach(kp =>
                            {
                                if (kp.Value.GetType() == typeof(System.Text.Json.JsonElement))
                                {
                                    var je = (System.Text.Json.JsonElement)kp.Value;
                                    switch (je.ValueKind)
                                    {
                                    case System.Text.Json.JsonValueKind.Undefined:
                                    case System.Text.Json.JsonValueKind.Object:
                                    case System.Text.Json.JsonValueKind.Array:
                                        dc.Add(kp.Key, je.GetRawText());
                                        break;

                                    case System.Text.Json.JsonValueKind.String:
                                        dc.Add(kp.Key, je.GetString());
                                        break;

                                    case System.Text.Json.JsonValueKind.Number:
                                        dc.Add(kp.Key, je.GetDouble());
                                        break;

                                    case System.Text.Json.JsonValueKind.True:
                                    case System.Text.Json.JsonValueKind.False:
                                        dc.Add(kp.Key, je.GetBoolean());
                                        break;

                                    case System.Text.Json.JsonValueKind.Null:
                                        break;

                                    default:
                                        break;
                                    }
                                }
                                else
                                {
                                    dc.Add(kp.Key, kp.Value);
                                }
                            });
                            var result2 = await _dbContext.SaveAsync <AttributeLatest>(dc, device.Id, msg.DataSide);
                            result2.exceptions?.ToList().ForEach(ex =>
                            {
                                _logger.LogError($"{ex.Key} {ex.Value} {Newtonsoft.Json.JsonConvert.SerializeObject(msg.MsgBody[ex.Key])}");
                            });
                            _logger.LogInformation($"更新{device.Name}({device.Id})属性数据结果{result2.ret}");
                        }
                    }
                }
            });
        }
Esempio n. 16
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));
        }
Esempio n. 17
0
 private static string GetDevTableName(RawMsg msg) => $"telemetrydata_{msg.DeviceId:N}";
Esempio n. 18
0
 static void Value_newMessage(RawMsg amsg, Server s)
 {
     try
     {
         if (clientStream != null)
         {
             string toSend = s.name + "\n" + amsg.now.Ticks.ToString() + "\n" + amsg.line;
             byte[] ts = sendEnc.CryptData(Encoding.UTF8.GetBytes(toSend));
             clientStream.Write(ts, 0, ts.Length);
         }
     }
     catch { }
 }