private async Task ExecFlowRules(ApplicationMessageNotConsumedEventArgs e, Device _dev, MountType mount) { var rules = await _caching.GetAsync($"ruleid_{_dev.Id}_raw", async() => { using (var scope = _scopeFactor.CreateScope()) using (var _dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>()) { var guids = await _dbContext.GerDeviceRulesIdList(_dev.Id, mount); return(guids); } } , TimeSpan.FromSeconds(_settings.RuleCachingExpiration)); if (rules.HasValue) { var obj = new { e.ApplicationMessage.Topic, Payload = Convert.ToBase64String(e.ApplicationMessage.Payload), e.SenderClientId }; rules.Value.ToList().ForEach(async g => { _logger.LogInformation($"{e.SenderClientId}的数据{e.ApplicationMessage.Topic}通过规则链{g}进行处理。"); await _flowRuleProcessor.RunFlowRules(g, obj, _dev.Id, EventType.Normal, null); }); } else { _logger.LogInformation($"{e.SenderClientId}的数据{e.ApplicationMessage.Topic}不符合规范, 也无相关规则链处理。"); } }
internal async Task Server_ApplicationMessageReceived(ApplicationMessageNotConsumedEventArgs e) { if (string.IsNullOrEmpty(e.SenderClientId)) { _logger.LogInformation($"ClientId为空,无法进一步获取设备信息 Topic=[{e.ApplicationMessage.Topic }]"); } else { try { _logger.LogInformation($"Server received {e.SenderClientId}'s message: Topic=[{e.ApplicationMessage.Topic }],Retain=[{e.ApplicationMessage.Retain}],QualityOfServiceLevel=[{e.ApplicationMessage.QualityOfServiceLevel}]"); string topic = e.ApplicationMessage.Topic; var tpary = topic.Split('/', StringSplitOptions.RemoveEmptyEntries); var _dev = await FoundDevice(e.SenderClientId); if (tpary.Length >= 3 && tpary[0] == "devices" && _dev != null) { var device = _dev.JudgeOrCreateNewDevice(tpary[1], _scopeFactor, _logger); if (device != null) { bool statushavevalue = false; Dictionary <string, object> keyValues = new Dictionary <string, object>(); if (tpary.Length >= 4) { string keyname = tpary.Length >= 5 ? tpary[4] : tpary[3]; if (tpary[3].ToLower() == "xml") { try { var xml = new System.Xml.XmlDocument(); xml.LoadXml(e.ApplicationMessage.ConvertPayloadToString()); keyValues.Add(keyname, xml); } catch (Exception ex) { _logger.LogWarning(ex, $"xml data error {topic},{ex.Message}"); } } else if (tpary[3].ToLower() == "binary") { keyValues.Add(keyname, e.ApplicationMessage.Payload); } } else { try { keyValues = e.ApplicationMessage.ConvertPayloadToDictionary(); } catch (Exception ex) { _logger.LogWarning(ex, $"转换为字典格式失败 {topic},{ex.Message}"); } } if (tpary[2] == "telemetry") { _queue.PublishTelemetryData(new RawMsg() { DeviceId = device.Id, MsgBody = keyValues, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.TelemetryData }); } else if (tpary[2] == "attributes") { if (tpary.Length > 3 && tpary[3] == "request") { await RequestAttributes(tpary, e.SenderClientId, e.ApplicationMessage.ConvertPayloadToDictionary(), device); } else { _queue.PublishAttributeData(new RawMsg() { DeviceId = device.Id, MsgBody = keyValues, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.AttributeData }); } } else if (tpary[2] == "status") { ResetDeviceStatus(device, tpary[3] == "online"); statushavevalue = true; } else if (tpary[2] == "rpc") { if (tpary[3] == "request") { await ExecFlowRules(e, _dev.DeviceType == DeviceType.Gateway?device : _dev, tpary[4], MountType.RPC); //完善后改成 RPC } } else { await ExecFlowRules(e, _dev.DeviceType == DeviceType.Gateway?device : _dev, MountType.RAW); //如果是网关 } if (!statushavevalue) { ResetDeviceStatus(device); } } else { _logger.LogInformation($"{e.SenderClientId}的数据{e.ApplicationMessage.Topic}未能匹配到设备"); } } else { //tpary.Length >= 3 && tpary[0] == "devices" && _dev != null _logger.LogWarning($"不支持{e.SenderClientId}的{e.ApplicationMessage.Topic}格式,Length:{tpary.Length },{tpary[0] },{ _dev != null}"); } } catch (Exception ex) { _logger.LogWarning(ex, $"ApplicationMessageReceived {ex.Message} {ex.InnerException?.Message}"); } } }