diff --git a/IoTGateway.ViewModel/BasicData/DeviceVMs/DeviceVM.cs b/IoTGateway.ViewModel/BasicData/DeviceVMs/DeviceVM.cs index 80be40c..d8d9a3f 100644 --- a/IoTGateway.ViewModel/BasicData/DeviceVMs/DeviceVM.cs +++ b/IoTGateway.ViewModel/BasicData/DeviceVMs/DeviceVM.cs @@ -62,6 +62,8 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs List Ids = new List() { Guid.Parse(FC["id"].ToString()) }; var pluginManager = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService; + var myMqttClient = Wtm.ServiceProvider.GetService(typeof(MyMqttClient)) as MyMqttClient; + myMqttClient.DeviceDeleted(Entity); var ret = DeleteDevices.doDelete(pluginManager, DC, Ids); if (!ret.IsSuccess) { @@ -69,9 +71,6 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs return; } - var myMqttClient = Wtm.ServiceProvider.GetService(typeof(MyMqttClient)) as MyMqttClient; - myMqttClient.DeviceDeleted(Entity); - } public override DuplicatedInfo SetDuplicatedCheck() { diff --git a/Plugins/Plugin/DeviceThread.cs b/Plugins/Plugin/DeviceThread.cs index 2c99d72..23af468 100644 --- a/Plugins/Plugin/DeviceThread.cs +++ b/Plugins/Plugin/DeviceThread.cs @@ -98,7 +98,7 @@ namespace Plugin try { ret.CookedValue = _interpreter.Eval(DealMysqlStr(item.Expressions) - .Replace("raw", ret.Value.ToString())); + .Replace("raw", ret.Value?.ToString())); } catch (Exception) { @@ -114,9 +114,9 @@ namespace Plugin //变化了才推送到mqttserver,用于前端展示 if (DeviceValues[item.ID].StatusType != ret.StatusType || - DeviceValues[item.ID].Value?.ToString() != ret.Value.ToString() || - DeviceValues[item.ID].CookedValue.ToString() != - ret.CookedValue.ToString()) + DeviceValues[item.ID].Value?.ToString() != ret.Value?.ToString() || + DeviceValues[item.ID].CookedValue?.ToString() != + ret.CookedValue?.ToString()) { //这是设备变量列表要用的 mqttServer.PublishAsync( diff --git a/Plugins/Plugin/MyMqttClient.cs b/Plugins/Plugin/MyMqttClient.cs index c3b2d45..117ccd3 100644 --- a/Plugins/Plugin/MyMqttClient.cs +++ b/Plugins/Plugin/MyMqttClient.cs @@ -1,4 +1,5 @@ -using IoTGateway.DataAccess; +using System.Text; +using IoTGateway.DataAccess; using IoTGateway.Model; using Microsoft.Extensions.Logging; using MQTTnet; @@ -363,10 +364,7 @@ namespace Plugin { ServiceId = "serviceId", EventTime = DateTime.Now.ToString("yyyyMMddTHHmmssZ"), - Data = new Dictionary() - { - { "data", obj } - } + Data = obj } } } @@ -538,14 +536,16 @@ namespace Plugin case IoTPlatformType.IoTSharp: foreach (var payload in sendModel[device.DeviceName]) { - await UploadIsTelemetryDataAsync(device.DeviceName, payload.Values); + if (payload.Values != null) + await UploadIsTelemetryDataAsync(device.DeviceName, payload.Values); } break; case IoTPlatformType.ThingsCloud: foreach (var payload in sendModel[device.DeviceName]) { - await UploadTcTelemetryDataAsync(device.DeviceName, payload.Values); + if (payload.Values != null) + await UploadTcTelemetryDataAsync(device.DeviceName, payload.Values); } break; @@ -560,8 +560,8 @@ namespace Plugin { device = device.DeviceName, timestamp = payload.TS, - measurements = payload.Values.Keys.ToList(), - values = payload.Values.Values.ToList() + measurements = payload.Values?.Keys.ToList(), + values = payload.Values?.Values.ToList() }; await Client.PublishAsync(device.DeviceName, JsonConvert.SerializeObject(tsData)); } @@ -571,7 +571,8 @@ namespace Plugin case IoTPlatformType.HuaWei: foreach (var payload in sendModel[device.DeviceName]) { - await UploadHwTelemetryDataAsync(device, payload.Values); + if (payload.Values != null) + await UploadHwTelemetryDataAsync(device, payload.Values); } break; @@ -587,12 +588,13 @@ namespace Plugin foreach (var payload in sendModel[device.DeviceName]) { - foreach (var kv in payload.Values) - { - //更新到UAService - _uaNodeManager?.UpdateNode($"{device.Parent.DeviceName}.{device.DeviceName}.{kv.Key}", - kv.Value); - } + if (payload.Values != null) + foreach (var kv in payload.Values) + { + //更新到UAService + _uaNodeManager?.UpdateNode($"{device.Parent.DeviceName}.{device.DeviceName}.{kv.Key}", + kv.Value); + } } } catch (Exception ex) @@ -630,7 +632,7 @@ namespace Plugin case IoTPlatformType.HuaWei: var deviceOnLine = new HwDeviceOnOffLine() { - MId = new Random().NextInt64(), //命令ID + MId = new Random().Next(0, 1024), //命令ID DeviceStatuses = new List() { new DeviceStatus() @@ -643,7 +645,8 @@ namespace Plugin } }; await Client.PublishAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update", - JsonConvert.SerializeObject(deviceOnLine)); + JsonConvert.SerializeObject(deviceOnLine), MqttQualityOfServiceLevel.AtLeastOnce, + retain: false); break; } } @@ -682,7 +685,7 @@ namespace Plugin case IoTPlatformType.HuaWei: var deviceOnLine = new HwDeviceOnOffLine() { - MId = new Random().NextInt64(), //命令ID + MId = new Random().Next(0, 1024), //命令ID DeviceStatuses = new List() { new DeviceStatus() @@ -695,7 +698,8 @@ namespace Plugin } }; await Client.PublishAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update", - JsonConvert.SerializeObject(deviceOnLine)); + JsonConvert.SerializeObject(deviceOnLine), MqttQualityOfServiceLevel.AtLeastOnce, + retain: false); break; } } @@ -718,7 +722,7 @@ namespace Plugin var addDeviceDto = new HwAddDeviceDto { - MId = new Random().NextInt64() //命令ID + MId = new Random().Next(0, 1024), //命令ID }; addDeviceDto.DeviceInfos.Add( new DeviceInfo @@ -753,22 +757,27 @@ namespace Plugin case IoTPlatformType.HuaWei: var topic = $"/v1/devices/{_systemConfig.GatewayName}/topo/delete"; - var deleteDeviceDto = new HwDeleteDeviceDto + await using (var dc = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DbType)) { - Id = new Random().NextInt64(), //命令ID - DeviceId = device.DeviceConfigs.FirstOrDefault(x => x.DeviceConfigName == "DeviceId") - ?.Value, - RequestTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds, - Request = new() - { - ManufacturerId = "Test_n", - ManufacturerName = "Test_n", - ProductType = "A_n" - } - }; + var deviceId = dc.Set().FirstOrDefault(x => + x.DeviceId == device.ID && x.DeviceConfigName == "DeviceId")?.Value; - await Client.PublishAsync(topic, - JsonConvert.SerializeObject(deleteDeviceDto)); + var deleteDeviceDto = new HwDeleteDeviceDto + { + Id = new Random().Next(0, 1024), //命令ID + DeviceId = deviceId, + RequestTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds, + Request = new() + { + ManufacturerId = "Test_n", + ManufacturerName = "Test_n", + ProductType = "A_n" + } + }; + + await Client.PublishAsync(topic, + JsonConvert.SerializeObject(deleteDeviceDto)); + } break; } } diff --git a/Plugins/PluginInterface/SendModel.cs b/Plugins/PluginInterface/SendModel.cs index 2237f1e..7e4a2d5 100644 --- a/Plugins/PluginInterface/SendModel.cs +++ b/Plugins/PluginInterface/SendModel.cs @@ -14,7 +14,7 @@ namespace PluginInterface [JsonProperty(PropertyName = "devicestatus")] public DeviceStatusTypeEnum DeviceStatus { get; set; } = DeviceStatusTypeEnum.Good; [JsonProperty(PropertyName = "values")] - public Dictionary Values { get; set; } = new(); + public Dictionary? Values { get; set; } = new(); } public enum DeviceStatusTypeEnum {