/// <summary> /// 输出多個 XML files,每個 XML file 最多只包含 2 萬笔资料 /// </summary> /// <param name="xmlFilePath">要输出的 XML file 路径</param> /// <returns>成功输出的 XML file path list</returns> private List <string> ExportMultiple4BytesAlignXmls( string xmlOutputFolderPath, AddrList.DevDataTable Devices) { const int SEGMENT_SIZE = 20000; List <string> files = new List <string>(); if (Devices.Count() > 0) { int fileCount = (Devices.Count() - 1) / SEGMENT_SIZE + 1; int devIndex = 0; for (int i = 0; i < fileCount; ++i) { string xmlFilePath = (i == 0) ? (xmlOutputFolderPath + @"\addressbook.xml") : (xmlOutputFolderPath + @"\addressbook" + i + ".xml"); AddrList ds = new AddrList(); int devCount = Math.Min(SEGMENT_SIZE, Devices.Count() - i * SEGMENT_SIZE); for (int j = 0; j < devCount; ++j) { ds.dev.ImportRow(Devices[devIndex++]); } if (ExportXml(xmlFilePath, ds.dev) && Export4BytesAlignXml(xmlFilePath, xmlFilePath)) { files.Add(xmlFilePath); } } } return(files); }
private AddrList DeviceToAddrList() { AddrList addrList = new AddrList(); using (var db = new ICMDBContext()) { var Devices = db.Devices.ToList(); AddLog("总共有 {0} 笔资料需要输出", Devices.Count); foreach (var d in Devices) { AddrList.devRow dev = addrList.dev.NewdevRow(); dev.ip = d.ip; dev.ro = d.roomid; dev.alias = d.Alias; dev.group = d.group; dev.mc = d.mac; dev.ty = (byte)d.type; dev.sm = d.sm; dev.gw = d.gw; dev.id = d.cameraid; dev.pw = d.camerapw; addrList.dev.AdddevRow(dev); } addrList.AcceptChanges(); } return(addrList); }
private string ExportAddressBookUclPkg() { AddrList addrList = DeviceToAddrList(); string pkgFilePath = Path.GetAddressBookPkgFilePath(); ExportAddressBookUclPkg(pkgFilePath, addrList.dev); return(pkgFilePath); }
private void ExportXmlAddressBook() { using (var db = new ICMDBContext()) { AddrList addrList = DeviceToAddrList(); string filePath = Path.GetAddressBookTempXmlFilePath(); ExportXml(filePath, addrList.dev); AddLog("已输出至 {0}", Path.GetAddressBookTempXmlFilePath()); } }
private void ExportAddressBook(IProgress <int> progress = null) { int processCount = 0; int reportedProgressPercentage = -1; using (var db = new ICMDBContext()) { AddrList addrList = new AddrList(); var Devices = db.Devices.ToList(); AddLog("总共有 {0} 笔资料需要输出", Devices.Count); foreach (var d in Devices) { AddrList.devRow dev = addrList.dev.NewdevRow(); dev.ip = d.ip; dev.ro = d.roomid; dev.alias = d.Alias; dev.group = d.group; dev.mc = d.mac; dev.ty = (byte)d.type; dev.sm = d.sm; dev.gw = d.gw; dev.id = d.cameraid; dev.pw = d.camerapw; addrList.dev.AdddevRow(dev); if (progress != null) { int currentProgressPercentage = (++processCount * 99 / Devices.Count); if (reportedProgressPercentage != currentProgressPercentage) { reportedProgressPercentage = currentProgressPercentage; progress.Report(reportedProgressPercentage); } } } addrList.AcceptChanges(); string filePath = Path.GetAddressBookTempXmlFilePath(); ExportXml(addrList, filePath); AddLog("已输出至 {0}", Path.GetAddressBookTempXmlFilePath()); } Thread.Sleep(500); if (progress != null) { progress.Report(100); } }
public static void AddDevices(AddrList addrList, IProgress <int> progress = null) { Stopwatch stopwatch = Stopwatch.StartNew(); using (var db = new ICMDBContext()) { db.Database.ExecuteSqlCommand("DELETE FROM Device"); db.Database.ExecuteSqlCommand("ALTER TABLE Device AUTO_INCREMENT = 1"); int processCount = 0; int reportedProgressPercentage = -1; foreach (var d in addrList.dev) { Device dev = new Device { ip = d.ip, roomid = d.ro, Alias = (d.IsaliasNull()) ? null : d.alias, group = (d.IsgroupNull()) ? null : d.group, mac = (d.IsmcNull()) ? null : d.mc, type = d.ty, sm = d.sm, gw = d.gw, cameraid = (d.IsidNull()) ? null : d.id, camerapw = (d.IspwNull()) ? null : d.pw }; db.Devices.Add(dev); processCount++; if (progress != null) { int currentProgressPercentage = (processCount * 99 / addrList.dev.Count); if (reportedProgressPercentage != currentProgressPercentage) { reportedProgressPercentage = currentProgressPercentage; progress.Report(reportedProgressPercentage); } } } db.SaveChanges(); if (progress != null) { progress.Report(100); } } stopwatch.Stop(); //DebugLog.TraceMessage(string.Format("done: {0}", stopwatch.Elapsed)); }
private bool ExportXml(AddrList addrList, string xmlFilePath) { bool result = false; XmlWriterSettings settings = new XmlWriterSettings { Indent = true }; using (MemoryStream stream = new MemoryStream()) { using (XmlWriter writer = XmlWriter.Create(stream, settings)) { try { addrList.dev.WriteXml(writer); // Reset stream to origin stream.Seek(0, SeekOrigin.Begin); // Load stream as XDocument XDocument xdoc = XDocument.Load(stream); // TODO 版本号应该要可以修改 xdoc.Element("AddrList").SetAttributeValue("ver", "1.0"); // Save to file as XML xdoc.Save(xmlFilePath); result = true; } catch (Exception) { //MessageBox.Show(ex.Message, "操作错误", // MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { writer.Close(); } } } return(result); }
public static Task AddDevicesAsync(AddrList addrList, IProgress <int> progress = null) { return(Task.Run(() => { AddDevices(addrList, progress); })); }