Esempio n. 1
0
 private void ExportExcelData()
 {
     try
     {
         SaveFileDialog saveFileDialog = new SaveFileDialog();
         saveFileDialog.Filter = "Excel Files | *.xlsx";;
         if (saveFileDialog.ShowDialog() == true)
         {
             IList <DeviceExcelData> list            = new List <DeviceExcelData>();
             DeviceModel[]           selectedDevices = this.Devices.Where(x => x.IsSelected).ToArray();
             foreach (var item in selectedDevices)
             {
                 DeviceExcelData data = new DeviceExcelData()
                 {
                     DeviceType        = DeviceTypeConverter.GetString(item.DeviceType),
                     DHCP              = Boolean2ChineseConverter.GetString(item.DHCP),
                     Dns1              = item.Dns1,
                     Dns2              = item.Dns2,
                     Gateway           = item.Gateway,
                     Hardware          = item.Hardware,
                     Software          = item.Software,
                     IPAddress         = item.IPAddress,
                     Username          = item.Username,
                     Password          = item.Password,
                     Model             = item.Model,
                     Name              = item.Name,
                     No                = item.No,
                     PhysicalAddress   = item.PhysicalAddress,
                     Port              = item.Port,
                     ProtocolType      = item.ProtocolType,
                     SubnetMask        = item.SubnetMask,
                     VideoChannelCount = item.VideoChannelCount,
                 };
                 list.Add(data);
             }
             ExcelPackageExporter.ExportDeviceExcelStream(list, saveFileDialog.FileName);
             this.ShowMessageAsync("导出数据成功", null);
         }
     }
     catch (Exception ex)
     {
         Helper.Logger.Error("导出数据失败", ex);
         this.ShowMessageAsync("导出数据失败", ex.Message);
     }
 }
Esempio n. 2
0
 private void ImportExcelData()
 {
     try
     {
         OpenFileDialog openFileDialog = new OpenFileDialog();
         openFileDialog.Filter = "Excel Files | *.xlsx";;
         if (openFileDialog.ShowDialog() == true)
         {
             IList <DeviceExcelData> deviceExcelDatas = ExcelPackageExporter.ImportDeviceExcelStream(openFileDialog.FileName);
             int currentCount = 0;
             int maxCount     = 100;
             foreach (var excelData in deviceExcelDatas)
             {
                 DeviceModel item = this.Devices.Where(x => x.PhysicalAddress.ToUpper() == excelData.PhysicalAddress.ToUpper()).SingleOrDefault();
                 if (item == null)
                 {
                     continue;
                 }
                 NetworkAddress networkAddress = new NetworkAddress()
                 {
                     AddressingType = Boolean2ChineseConverter.GetBoolean(excelData.DHCP) ? Howell.Industry.NetworkAddressingType.Dynamic : Howell.Industry.NetworkAddressingType.Static,
                     HttpPort       = item.NetworkDeviceInformation.HttpPort,
                     IPAddress      = item.NetworkDeviceInformation.NetworkInterface.IPAddress,
                     Port           = item.NetworkDeviceInformation.Port,
                 };
                 if (excelData.Port > 0 && excelData.Port <= 65535)
                 {
                     networkAddress.Port = excelData.Port;
                 }
                 if (String.IsNullOrEmpty(excelData.IPAddress) == false && excelData.IPAddress != "0.0.0.0")
                 {
                     networkAddress.IPAddress.IPv4Address.Address = excelData.IPAddress;
                 }
                 if (String.IsNullOrEmpty(excelData.SubnetMask) == false && excelData.SubnetMask != "0.0.0.0")
                 {
                     networkAddress.IPAddress.IPv4Address.Subnetmask = excelData.SubnetMask;
                 }
                 if (String.IsNullOrEmpty(excelData.Gateway) == false && excelData.Gateway != "0.0.0.0")
                 {
                     networkAddress.IPAddress.IPv4Address.DefaultGateway = excelData.Gateway;
                 }
                 if (String.IsNullOrEmpty(excelData.Password) == false)
                 {
                     item.Password = excelData.Password;
                 }
                 if (String.IsNullOrEmpty(excelData.Username) == false)
                 {
                     item.Username = excelData.Username;
                 }
                 if (FactoryType == "Howell8000")
                 {
                     //必须使用原始的MAC地址,区分大小写
                     DeviceSearcher.SetNetworkAddress(item.NetworkDeviceInformation.NetworkInterface.PhyscialAddress, excelData.Password, networkAddress);
                 }
                 else
                 {
                     DeviceSearcher.SetNetworkAddress(networkAddress.IPAddress.IPv4Address.Address,
                                                      networkAddress.IPAddress.IPv4Address.Subnetmask,
                                                      networkAddress.IPAddress.IPv4Address.DefaultGateway, networkAddress.Port, item.NetworkDeviceInformation.NetworkInterface.PhyscialAddress);
                 }
                 item.IPAddress  = networkAddress.IPAddress.IPv4Address.Address;
                 item.SubnetMask = networkAddress.IPAddress.IPv4Address.Subnetmask;
                 item.Gateway    = networkAddress.IPAddress.IPv4Address.DefaultGateway;
                 item.Port       = networkAddress.Port;
                 item.DHCP       = (networkAddress.AddressingType == Howell.Industry.NetworkAddressingType.Dynamic);
             }
             System.Threading.Thread.Sleep(2000);
             foreach (var excelData in deviceExcelDatas)
             {
                 DeviceModel item = this.Devices.Where(x => x.PhysicalAddress.ToUpper() == excelData.PhysicalAddress.ToUpper()).SingleOrDefault();
                 if (item == null)
                 {
                     continue;
                 }
                 do
                 {
                     if (currentCount < maxCount)
                     {
                         Interlocked.Increment(ref currentCount);
                         Task task = Task.Run(() =>
                         {
                             try
                             {
                                 NetworkDeviceHelper.SetOSDName(item, excelData.Name);
                                 item.Name           = excelData.Name;
                                 item.SettingSucceed = true;
                             }
                             catch (Exception ex)
                             {
                                 item.SettingSucceed = false;
                                 Helper.Logger.Error(String.Format("SetOSDName error, DeviceIP:{0}", item.IPAddress), ex);
                             }
                         })
                                     .ContinueWith(x =>
                         {
                             Interlocked.Decrement(ref currentCount);
                         });
                         break;
                     }
                     System.Threading.Thread.Sleep(100);
                 } while (true);
             }
             while (currentCount > 0)
             {
                 System.Threading.Thread.Sleep(100);
             }
             this.ShowMessageAsync("导入数据完成", null);
         }
     }
     catch (Exception ex)
     {
         Helper.Logger.Error("导入数据失败", ex);
         this.ShowMessageAsync("导入数据失败", ex.Message);
     }
 }