using Microsoft.EntityFrameworkCore; using Plugin; using System; using System.Collections.Generic; using System.Linq; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Extensions; using IoTGateway.Model; using System.ComponentModel.DataAnnotations; using Microsoft.Extensions.DependencyInjection; using WalkingTec.Mvvm.Core.Support.FileHandlers; using NPOI.XSSF.UserModel; using NPOI.SS.UserModel; using PluginInterface; using IoTGateway.ViewModel.BasicData.DeviceVMs; using Microsoft.Extensions.Logging; namespace IoTGateway.ViewModel.BasicData { public class ImportExcelVM : BaseVM { [Display(Name = "全部覆盖")] public bool Cover { get; set; } public string 导入结果 { get; set; } [Display(Name = "Excel模板文件")] public Guid ExcelFileId { get; set; } public FileAttachment ExcelFile { get; set; } private List _drivers; private List _devices; private List _deviceVariables; private Dictionary dateTypeNameMapping; private Dictionary endianTypeNameMapping; public void Import() { using var transaction = DC.BeginTransaction(); _drivers = DC.Set().AsNoTracking().ToList(); var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService; try { var oldDevices = DC.Set().ToList(); if (true)//Cover { foreach (var device in oldDevices) { var dapConfigs = DC.Set().Where(x => x.DeviceId == device.ID).ToList(); var dapVariables = DC.Set().Where(x => x.DeviceId == device.ID).ToList(); var rpcs = DC.Set().Where(x => x.DeviceId == device.ID).ToList(); //var dapGroups = DC.Set().Where(x => x.DeviceId == dap.ID).ToList(); DC.Set().RemoveRange(dapConfigs); DC.Set().RemoveRange(dapVariables); DC.Set().RemoveRange(rpcs); deviceService.RemoveDeviceThread(device); } DC.Set().RemoveRange(oldDevices); DC.SaveChanges(); DeleteDevices.doDelete(deviceService, DC, oldDevices.Select(x => x.ID).ToList()); } var fp = Wtm.ServiceProvider.GetRequiredService(); var file = fp.GetFile(ExcelFileId.ToString(), true, DC); var xssfworkbook = new XSSFWorkbook(file.DataStream); file.DataStream.Dispose(); //获取数据的Sheet页信息 var sheetDevice = xssfworkbook.GetSheet("设备维护"); var devices = GetDevices(sheetDevice); DC.Set().AddRange(devices); var sheetVariables = xssfworkbook.GetSheet("变量配置"); var deviceVariables = GetVariables(sheetVariables); DC.Set().AddRange(deviceVariables); var sheetDeviceConfigs = xssfworkbook.GetSheet("通讯配置"); var deviceConfigs = GetDeviceConfigs(sheetDeviceConfigs); DC.Set().AddRange(deviceConfigs); var sheetSystemConfig = xssfworkbook.GetSheet("传输配置"); var newSystemConfig = GetSystemConfig(sheetSystemConfig); var systemConfig = DC.Set().First(); systemConfig.GatewayName = newSystemConfig.GatewayName; systemConfig.ClientId = newSystemConfig.ClientId; systemConfig.IoTPlatformType = newSystemConfig.IoTPlatformType; systemConfig.MqttIp = newSystemConfig.MqttIp; systemConfig.MqttPort = newSystemConfig.MqttPort; systemConfig.MqttUName = newSystemConfig.MqttUName; systemConfig.MqttUPwd = newSystemConfig.MqttUPwd; DC.SaveChanges(); transaction.Commit(); var messageService = Wtm.ServiceProvider.GetService(typeof(MessageService)) as MessageService; messageService.StartClientAsync().Wait(); //重新启动采集 deviceService.CreateDeviceThreads(); 导入结果 = $"成功导入{devices.Count(x => x.DeviceTypeEnum == DeviceTypeEnum.Device)}个设备,{devices.Count(x => x.DeviceTypeEnum == DeviceTypeEnum.Group)}个组"; } catch (Exception ex) { transaction.Rollback(); 导入结果 = $"导入失败,已经回滚。{ex}"; Console.WriteLine($"{导入结果},{ex.Message}"); } } private List GetDevices(ISheet sheetDevice) { var devices = new List(); for (int i = 1; i <= sheetDevice.LastRowNum; i++) { try { var row = sheetDevice.GetRow(i); Device device = new Device(); device.ID = Guid.NewGuid(); device.DeviceName = row.GetCell(0)?.ToString(); device.Index = uint.Parse(row.GetCell(1)?.ToString()); device.ParentId = devices.FirstOrDefault(x => x.DeviceName == row.GetCell(8)?.ToString() && x.DeviceTypeEnum == DeviceTypeEnum.Group)?.ID; device.DeviceTypeEnum = device.ParentId == null ? DeviceTypeEnum.Group : DeviceTypeEnum.Device; if (device.DeviceTypeEnum == DeviceTypeEnum.Device) { device.DriverId = _drivers.FirstOrDefault(x => x.DriverName == row.GetCell(2)?.ToString())?.ID; device.AutoStart = row.GetCell(3).BooleanCellValue; device.CgUpload = row.GetCell(4).BooleanCellValue; device.EnforcePeriod = uint.Parse(row.GetCell(5)?.ToString()); device.CmdPeriod = uint.Parse(row.GetCell(6)?.ToString()); } devices.Add(device); } catch (Exception e) { Console.WriteLine(e); } } _devices = devices; return devices; } private List GetVariables(ISheet sheet) { DateTypeNameMapping(); EndianNameMapping(); var deviceVariables = new List(); for (int i = 1; i <= sheet.LastRowNum; i++) { try { var row = sheet.GetRow(i); var variable = new DeviceVariable(); variable.ID = Guid.NewGuid(); variable.DeviceId = _devices.FirstOrDefault(x => x.DeviceName == row.GetCell(0)?.ToString())?.ID; variable.Name = row.GetCell(1)?.ToString(); variable.Method = row.GetCell(2)?.ToString(); variable.DeviceAddress = row.GetCell(3)?.ToString(); variable.DataType = dateTypeNameMapping.ContainsKey(row.GetCell(4)?.ToString()) ? dateTypeNameMapping[row.GetCell(4)?.ToString()] : DataTypeEnum.Any; variable.EndianType = endianTypeNameMapping.ContainsKey(row.GetCell(5)?.ToString()) ? endianTypeNameMapping[row.GetCell(5)?.ToString()] : EndianEnum.None; variable.Expressions = row.GetCell(6)?.ToString(); variable.Alias = row.GetCell(7)?.ToString(); variable.IsUpload = row.GetCell(8)?.ToString().ToLower() == "false" ? false : true; variable.Index = string.IsNullOrWhiteSpace(row.GetCell(9)?.ToString()) ? 999 : uint.Parse(row.GetCell(9).ToString()); variable.IsTrigger = row.GetCell(10)?.ToString().ToLower() == "false" ? false : true; deviceVariables.Add(variable); } catch (Exception e) { Console.WriteLine(e); } } _deviceVariables = deviceVariables; return deviceVariables; } private List GetDeviceConfigs(ISheet sheet) { var deviceConfigs = new List(); for (int i = 1; i <= sheet.LastRowNum; i++) { try { var row = sheet.GetRow(i); var config = new DeviceConfig(); config.ID = Guid.NewGuid(); config.DeviceId = _devices.FirstOrDefault(x => x.DeviceName == row.GetCell(0)?.ToString())?.ID; config.DeviceConfigName = row.GetCell(1)?.ToString(); config.DataSide = row.GetCell(2)?.ToString() == "共享属性" ? DataSide.AnySide : DataSide.ClientSide; config.Description = row.GetCell(3)?.ToString(); config.Value = row.GetCell(4)?.ToString(); config.EnumInfo = row.GetCell(5)?.ToString(); deviceConfigs.Add(config); } catch (Exception e) { Console.WriteLine(e); } } return deviceConfigs; } private SystemConfig GetSystemConfig(ISheet sheet) { var systemConfig = new SystemConfig(); for (int i = 1; i <= 1; i++) { try { var row = sheet.GetRow(i); systemConfig.GatewayName = row.GetCell(0)?.ToString(); systemConfig.ClientId = row.GetCell(1)?.ToString(); systemConfig.IoTPlatformType = IoTPlatformType.IoTSharp; systemConfig.MqttIp = row.GetCell(3)?.ToString(); systemConfig.MqttPort = int.Parse(row.GetCell(4)?.ToString()); systemConfig.MqttUName = row.GetCell(5)?.ToString(); systemConfig.MqttUPwd = row.GetCell(6)?.ToString(); } catch (Exception e) { Console.WriteLine(e); } } return systemConfig; } protected void DateTypeNameMapping() { dateTypeNameMapping = new Dictionary(); var enumType = typeof(DataTypeEnum); var displayAttributeType = typeof(DisplayAttribute); DataTypeEnum? found = null; foreach (var name in Enum.GetNames(enumType)) { var member = enumType.GetMember(name).First(); var displayAttrib = (DisplayAttribute)member.GetCustomAttributes(displayAttributeType, false).First(); dateTypeNameMapping.Add(displayAttrib.Name, (DataTypeEnum)Enum.Parse(enumType, name)); } } protected void EndianNameMapping() { endianTypeNameMapping = new Dictionary(); var enumType = typeof(EndianEnum); var displayAttributeType = typeof(DisplayAttribute); EndianEnum? found = null; Enum.GetNames(enumType).ToList().ForEach(name => { var member = enumType.GetMember(name).First(); var displayAttrib = (DisplayAttribute)member.GetCustomAttributes(displayAttributeType, false).First(); endianTypeNameMapping.Add(displayAttrib.Name, (EndianEnum)Enum.Parse(enumType, name)); }); } } }