优化华为平台

This commit is contained in:
iioter 2022-08-11 00:07:02 +08:00
parent 858ce94bbb
commit acd7d86f5c
4 changed files with 51 additions and 43 deletions

View File

@ -62,6 +62,8 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs
List<Guid> Ids = new List<Guid>() { Guid.Parse(FC["id"].ToString()) }; List<Guid> Ids = new List<Guid>() { Guid.Parse(FC["id"].ToString()) };
var pluginManager = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService; 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); var ret = DeleteDevices.doDelete(pluginManager, DC, Ids);
if (!ret.IsSuccess) if (!ret.IsSuccess)
{ {
@ -69,9 +71,6 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs
return; return;
} }
var myMqttClient = Wtm.ServiceProvider.GetService(typeof(MyMqttClient)) as MyMqttClient;
myMqttClient.DeviceDeleted(Entity);
} }
public override DuplicatedInfo<Device> SetDuplicatedCheck() public override DuplicatedInfo<Device> SetDuplicatedCheck()
{ {

View File

@ -98,7 +98,7 @@ namespace Plugin
try try
{ {
ret.CookedValue = _interpreter.Eval(DealMysqlStr(item.Expressions) ret.CookedValue = _interpreter.Eval(DealMysqlStr(item.Expressions)
.Replace("raw", ret.Value.ToString())); .Replace("raw", ret.Value?.ToString()));
} }
catch (Exception) catch (Exception)
{ {
@ -114,9 +114,9 @@ namespace Plugin
//变化了才推送到mqttserver用于前端展示 //变化了才推送到mqttserver用于前端展示
if (DeviceValues[item.ID].StatusType != ret.StatusType || if (DeviceValues[item.ID].StatusType != ret.StatusType ||
DeviceValues[item.ID].Value?.ToString() != ret.Value.ToString() || DeviceValues[item.ID].Value?.ToString() != ret.Value?.ToString() ||
DeviceValues[item.ID].CookedValue.ToString() != DeviceValues[item.ID].CookedValue?.ToString() !=
ret.CookedValue.ToString()) ret.CookedValue?.ToString())
{ {
//这是设备变量列表要用的 //这是设备变量列表要用的
mqttServer.PublishAsync( mqttServer.PublishAsync(

View File

@ -1,4 +1,5 @@
using IoTGateway.DataAccess; using System.Text;
using IoTGateway.DataAccess;
using IoTGateway.Model; using IoTGateway.Model;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet; using MQTTnet;
@ -363,10 +364,7 @@ namespace Plugin
{ {
ServiceId = "serviceId", ServiceId = "serviceId",
EventTime = DateTime.Now.ToString("yyyyMMddTHHmmssZ"), EventTime = DateTime.Now.ToString("yyyyMMddTHHmmssZ"),
Data = new Dictionary<string, object>() Data = obj
{
{ "data", obj }
}
} }
} }
} }
@ -538,14 +536,16 @@ namespace Plugin
case IoTPlatformType.IoTSharp: case IoTPlatformType.IoTSharp:
foreach (var payload in sendModel[device.DeviceName]) foreach (var payload in sendModel[device.DeviceName])
{ {
await UploadIsTelemetryDataAsync(device.DeviceName, payload.Values); if (payload.Values != null)
await UploadIsTelemetryDataAsync(device.DeviceName, payload.Values);
} }
break; break;
case IoTPlatformType.ThingsCloud: case IoTPlatformType.ThingsCloud:
foreach (var payload in sendModel[device.DeviceName]) foreach (var payload in sendModel[device.DeviceName])
{ {
await UploadTcTelemetryDataAsync(device.DeviceName, payload.Values); if (payload.Values != null)
await UploadTcTelemetryDataAsync(device.DeviceName, payload.Values);
} }
break; break;
@ -560,8 +560,8 @@ namespace Plugin
{ {
device = device.DeviceName, device = device.DeviceName,
timestamp = payload.TS, timestamp = payload.TS,
measurements = payload.Values.Keys.ToList(), measurements = payload.Values?.Keys.ToList(),
values = payload.Values.Values.ToList() values = payload.Values?.Values.ToList()
}; };
await Client.PublishAsync(device.DeviceName, JsonConvert.SerializeObject(tsData)); await Client.PublishAsync(device.DeviceName, JsonConvert.SerializeObject(tsData));
} }
@ -571,7 +571,8 @@ namespace Plugin
case IoTPlatformType.HuaWei: case IoTPlatformType.HuaWei:
foreach (var payload in sendModel[device.DeviceName]) foreach (var payload in sendModel[device.DeviceName])
{ {
await UploadHwTelemetryDataAsync(device, payload.Values); if (payload.Values != null)
await UploadHwTelemetryDataAsync(device, payload.Values);
} }
break; break;
@ -587,12 +588,13 @@ namespace Plugin
foreach (var payload in sendModel[device.DeviceName]) foreach (var payload in sendModel[device.DeviceName])
{ {
foreach (var kv in payload.Values) if (payload.Values != null)
{ foreach (var kv in payload.Values)
//更新到UAService {
_uaNodeManager?.UpdateNode($"{device.Parent.DeviceName}.{device.DeviceName}.{kv.Key}", //更新到UAService
kv.Value); _uaNodeManager?.UpdateNode($"{device.Parent.DeviceName}.{device.DeviceName}.{kv.Key}",
} kv.Value);
}
} }
} }
catch (Exception ex) catch (Exception ex)
@ -630,7 +632,7 @@ namespace Plugin
case IoTPlatformType.HuaWei: case IoTPlatformType.HuaWei:
var deviceOnLine = new HwDeviceOnOffLine() var deviceOnLine = new HwDeviceOnOffLine()
{ {
MId = new Random().NextInt64(), //命令ID MId = new Random().Next(0, 1024), //命令ID
DeviceStatuses = new List<DeviceStatus>() DeviceStatuses = new List<DeviceStatus>()
{ {
new DeviceStatus() new DeviceStatus()
@ -643,7 +645,8 @@ namespace Plugin
} }
}; };
await Client.PublishAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update", await Client.PublishAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update",
JsonConvert.SerializeObject(deviceOnLine)); JsonConvert.SerializeObject(deviceOnLine), MqttQualityOfServiceLevel.AtLeastOnce,
retain: false);
break; break;
} }
} }
@ -682,7 +685,7 @@ namespace Plugin
case IoTPlatformType.HuaWei: case IoTPlatformType.HuaWei:
var deviceOnLine = new HwDeviceOnOffLine() var deviceOnLine = new HwDeviceOnOffLine()
{ {
MId = new Random().NextInt64(), //命令ID MId = new Random().Next(0, 1024), //命令ID
DeviceStatuses = new List<DeviceStatus>() DeviceStatuses = new List<DeviceStatus>()
{ {
new DeviceStatus() new DeviceStatus()
@ -695,7 +698,8 @@ namespace Plugin
} }
}; };
await Client.PublishAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update", await Client.PublishAsync($"/v1/devices/{_systemConfig.GatewayName}/topo/update",
JsonConvert.SerializeObject(deviceOnLine)); JsonConvert.SerializeObject(deviceOnLine), MqttQualityOfServiceLevel.AtLeastOnce,
retain: false);
break; break;
} }
} }
@ -718,7 +722,7 @@ namespace Plugin
var addDeviceDto = new HwAddDeviceDto var addDeviceDto = new HwAddDeviceDto
{ {
MId = new Random().NextInt64() //命令ID MId = new Random().Next(0, 1024), //命令ID
}; };
addDeviceDto.DeviceInfos.Add( addDeviceDto.DeviceInfos.Add(
new DeviceInfo new DeviceInfo
@ -753,22 +757,27 @@ namespace Plugin
case IoTPlatformType.HuaWei: case IoTPlatformType.HuaWei:
var topic = $"/v1/devices/{_systemConfig.GatewayName}/topo/delete"; 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 var deviceId = dc.Set<DeviceConfig>().FirstOrDefault(x =>
DeviceId = device.DeviceConfigs.FirstOrDefault(x => x.DeviceConfigName == "DeviceId") x.DeviceId == device.ID && x.DeviceConfigName == "DeviceId")?.Value;
?.Value,
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, var deleteDeviceDto = new HwDeleteDeviceDto
JsonConvert.SerializeObject(deleteDeviceDto)); {
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; break;
} }
} }

View File

@ -14,7 +14,7 @@ namespace PluginInterface
[JsonProperty(PropertyName = "devicestatus")] [JsonProperty(PropertyName = "devicestatus")]
public DeviceStatusTypeEnum DeviceStatus { get; set; } = DeviceStatusTypeEnum.Good; public DeviceStatusTypeEnum DeviceStatus { get; set; } = DeviceStatusTypeEnum.Good;
[JsonProperty(PropertyName = "values")] [JsonProperty(PropertyName = "values")]
public Dictionary<string, object> Values { get; set; } = new(); public Dictionary<string, object>? Values { get; set; } = new();
} }
public enum DeviceStatusTypeEnum public enum DeviceStatusTypeEnum
{ {