Files
Aaru.Server/Aaru.Server.Task/Program.cs

538 lines
23 KiB
C#
Raw Normal View History

2019-11-02 01:40:41 +00:00
// /***************************************************************************
2020-02-29 19:22:23 +00:00
// Aaru Data Preservation Suite
2019-11-02 01:40:41 +00:00
// ----------------------------------------------------------------------------
//
// Filename : Program.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
2020-02-29 19:24:13 +00:00
// Component : Aaru Server Task.
2019-11-02 01:40:41 +00:00
//
// --[ Description ] ----------------------------------------------------------
//
// Runs time consuming server tasks.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:59:18 +00:00
// Copyright © 2011-2020 Natalia Portillo
2019-11-02 01:40:41 +00:00
// ****************************************************************************/
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json;
2020-02-29 20:00:34 +00:00
using Aaru.Server.Models;
2019-11-02 01:40:41 +00:00
using HtmlAgilityPack;
2019-11-02 21:01:25 +00:00
using Microsoft.EntityFrameworkCore;
2019-11-02 01:40:41 +00:00
2020-02-29 20:00:34 +00:00
namespace Aaru.Server.Task
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:39:19 +00:00
internal class Program
2019-11-02 01:40:41 +00:00
{
public static void Main(string[] args)
{
DateTime start, end;
2019-11-02 21:01:25 +00:00
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Connecting to database...", DateTime.UtcNow);
2019-11-02 21:39:19 +00:00
var ctx = new DicServerContext();
2019-11-02 01:40:41 +00:00
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Migrating database to latest version...", DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
ctx.Database.Migrate();
2019-11-02 01:40:41 +00:00
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", DateTime.UtcNow, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
WebClient client;
try
{
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Retrieving USB IDs from Linux USB...", DateTime.UtcNow);
2019-11-02 23:52:33 +00:00
start = DateTime.UtcNow;
2019-11-02 01:40:41 +00:00
client = new WebClient();
2019-11-02 21:39:19 +00:00
var sr = new StringReader(client.DownloadString("http://www.linux-usb.org/usb.ids"));
2019-11-02 01:40:41 +00:00
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
UsbVendor vendor = null;
int newVendors = 0;
int newProducts = 0;
int modifiedVendors = 0;
int modifiedProducts = 0;
int counter = 0;
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Adding and updating database entries...", DateTime.UtcNow);
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
do
{
2019-11-02 23:52:33 +00:00
if(counter == 1000)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
DateTime start2 = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Saving changes", start2);
2019-11-02 01:40:41 +00:00
ctx.SaveChanges();
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start2).TotalSeconds);
2019-11-02 01:40:41 +00:00
counter = 0;
}
2019-11-02 23:52:33 +00:00
string line = sr.ReadLine();
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(line is null)
break;
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(line.Length == 0 ||
line[0] == '#')
continue;
2019-11-02 01:40:41 +00:00
ushort number;
string name;
2019-11-02 23:52:33 +00:00
if(line[0] == '\t')
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:39:19 +00:00
try
{
number = Convert.ToUInt16(line.Substring(1, 4), 16);
}
2019-11-02 23:52:33 +00:00
catch(FormatException)
2019-11-02 21:39:19 +00:00
{
continue;
}
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(number == 0)
continue;
2019-11-02 01:40:41 +00:00
name = line.Substring(7);
2019-11-02 23:52:33 +00:00
UsbProduct product =
ctx.UsbProducts.FirstOrDefault(p => p.ProductId == number && p.Vendor != null &&
2019-11-02 01:40:41 +00:00
p.Vendor.VendorId == vendor.VendorId);
2019-11-02 23:52:33 +00:00
if(product is null)
2019-11-02 01:40:41 +00:00
{
product = new UsbProduct(vendor, number, name);
ctx.UsbProducts.Add(product);
2019-11-02 23:52:33 +00:00
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Will add product {1} with ID {2:X4} and vendor {3} ({4:X4})",
2019-11-02 23:52:33 +00:00
DateTime.UtcNow, product.Product, product.ProductId,
product.Vendor?.Vendor ?? "null", product.Vendor?.VendorId ?? 0);
2019-11-02 01:40:41 +00:00
newProducts++;
counter++;
}
2019-11-02 23:52:33 +00:00
else if(name != product.Product)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
System.Console.
WriteLine("{0}: Will modify product with ID {1:X4} and vendor {2} ({3:X4}) from \"{4}\" to \"{5}\"",
DateTime.UtcNow, product.ProductId, product.Vendor?.Vendor ?? "null",
product.Vendor?.VendorId ?? 0,
product.Product, name);
product.Product = name;
2019-11-02 01:40:41 +00:00
product.ModifiedWhen = DateTime.UtcNow;
modifiedProducts++;
counter++;
}
continue;
}
2019-11-02 21:39:19 +00:00
try
{
number = Convert.ToUInt16(line.Substring(0, 4), 16);
}
2019-11-02 23:52:33 +00:00
catch(FormatException)
2019-11-02 21:39:19 +00:00
{
continue;
}
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(number == 0)
continue;
2019-11-02 01:40:41 +00:00
name = line.Substring(6);
vendor = ctx.UsbVendors.FirstOrDefault(v => v.VendorId == number);
2019-11-02 23:52:33 +00:00
if(vendor is null)
2019-11-02 01:40:41 +00:00
{
vendor = new UsbVendor(number, name);
ctx.UsbVendors.Add(vendor);
2019-11-02 23:52:33 +00:00
2019-11-02 21:39:19 +00:00
System.Console.WriteLine("{0}: Will add vendor {1} with ID {2:X4}", DateTime.UtcNow,
2019-11-02 23:52:33 +00:00
vendor.Vendor, vendor.VendorId);
2019-11-02 01:40:41 +00:00
newVendors++;
counter++;
}
2019-11-02 23:52:33 +00:00
else if(name != vendor.Vendor)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Will modify vendor with ID {1:X4} from \"{2}\" to \"{3}\"",
2019-11-02 23:52:33 +00:00
DateTime.UtcNow, vendor.VendorId, vendor.Vendor, name);
vendor.Vendor = name;
2019-11-02 01:40:41 +00:00
vendor.ModifiedWhen = DateTime.UtcNow;
modifiedVendors++;
counter++;
}
2019-11-02 23:52:33 +00:00
} while(true);
2019-11-02 01:40:41 +00:00
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Saving database changes...", DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
ctx.SaveChanges();
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
2019-11-02 21:39:19 +00:00
System.Console.WriteLine("{0}: {1} vendors added.", DateTime.UtcNow, newVendors);
System.Console.WriteLine("{0}: {1} products added.", DateTime.UtcNow, newProducts);
System.Console.WriteLine("{0}: {1} vendors modified.", DateTime.UtcNow, modifiedVendors);
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: {1} products modified.", DateTime.UtcNow, modifiedProducts);
2019-11-02 01:40:41 +00:00
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Looking up a vendor", DateTime.UtcNow);
2019-11-02 23:52:33 +00:00
start = DateTime.UtcNow;
2019-11-02 01:40:41 +00:00
vendor = ctx.UsbVendors.FirstOrDefault(v => v.VendorId == 0x8086);
2019-11-02 23:52:33 +00:00
if(vendor is null)
System.Console.WriteLine("{0}: Error, could not find vendor.", DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
else
2019-11-02 23:52:33 +00:00
System.Console.WriteLine("{0}: Found {1}.", DateTime.UtcNow, vendor.Vendor);
2019-11-02 01:40:41 +00:00
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Looking up a product", DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
2019-11-02 23:52:33 +00:00
UsbProduct prd =
2019-11-02 01:40:41 +00:00
ctx.UsbProducts.FirstOrDefault(p => p.ProductId == 0x0001 && p.Vendor.VendorId == 0x8086);
2019-11-02 23:52:33 +00:00
if(prd is null)
System.Console.WriteLine("{0}: Error, could not find product.", DateTime.UtcNow);
else
System.Console.WriteLine("{0}: Found {1}.", DateTime.UtcNow, prd.Product);
2019-11-02 01:40:41 +00:00
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
catch(Exception ex)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
#if DEBUG
if(Debugger.IsAttached)
throw;
#endif
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Exception {1} filling USB IDs...", DateTime.UtcNow, ex);
2019-11-02 01:40:41 +00:00
}
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Fixing all devices without modification time...", DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
2019-11-02 23:52:33 +00:00
foreach(Device device in ctx.Devices.Where(d => d.ModifiedWhen == null))
2019-11-02 01:40:41 +00:00
device.ModifiedWhen = device.AddedWhen;
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Committing changes...", DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
ctx.SaveChanges();
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
try
{
2019-11-02 21:39:19 +00:00
System.Console.WriteLine("{0}: Retrieving CompactDisc read offsets from AccurateRip...",
2019-11-02 23:52:33 +00:00
DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
client = new WebClient();
2019-11-02 23:52:33 +00:00
string html = client.DownloadString("http://www.accuraterip.com/driveoffsets.htm");
2019-11-02 01:40:41 +00:00
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
// The HTML is too malformed to process easily, so find start of table
html = "<html><body><table><tr>" +
html.Substring(html.IndexOf("<td bgcolor=\"#000000\">", StringComparison.Ordinal));
2019-11-02 21:39:19 +00:00
var doc = new HtmlDocument();
2019-11-02 01:40:41 +00:00
doc.LoadHtml(html);
2019-11-02 23:52:33 +00:00
HtmlNode firstTable = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/table[1]");
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
bool firstRow = true;
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
int addedOffsets = 0;
int modifiedOffsets = 0;
2019-11-02 01:40:41 +00:00
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Processing offsets...", DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
2019-11-02 23:52:33 +00:00
foreach(HtmlNode row in firstTable.Descendants("tr"))
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
HtmlNode[] columns = row.Descendants("td").ToArray();
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(columns.Length != 4)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:39:19 +00:00
System.Console.WriteLine("{0}: Row does not have correct number of columns...",
2019-11-02 23:52:33 +00:00
DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
continue;
}
2019-11-02 23:52:33 +00:00
string column0 = columns[0].InnerText;
string column1 = columns[1].InnerText;
string column2 = columns[2].InnerText;
string column3 = columns[3].InnerText;
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(firstRow)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
if(column0.ToLowerInvariant() != "cd drive")
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Unexpected header \"{1}\" found...", DateTime.UtcNow,
2019-11-02 23:52:33 +00:00
columns[0].InnerText);
2019-11-02 01:40:41 +00:00
break;
}
2019-11-02 23:52:33 +00:00
if(column1.ToLowerInvariant() != "correction offset")
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Unexpected header \"{1}\" found...", DateTime.UtcNow,
2019-11-02 23:52:33 +00:00
columns[1].InnerText);
2019-11-02 01:40:41 +00:00
break;
}
2019-11-02 23:52:33 +00:00
if(column2.ToLowerInvariant() != "submitted by")
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Unexpected header \"{1}\" found...", DateTime.UtcNow,
2019-11-02 23:52:33 +00:00
columns[2].InnerText);
2019-11-02 01:40:41 +00:00
break;
}
2019-11-02 23:52:33 +00:00
if(column3.ToLowerInvariant() != "percentage agree")
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Unexpected header \"{1}\" found...", DateTime.UtcNow,
2019-11-02 23:52:33 +00:00
columns[3].InnerText);
2019-11-02 01:40:41 +00:00
break;
}
firstRow = false;
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
continue;
}
string manufacturer;
string model;
2019-11-02 23:52:33 +00:00
if(column0[0] == '-' &&
column0[1] == ' ')
2019-11-02 01:40:41 +00:00
{
manufacturer = null;
2019-11-02 23:52:33 +00:00
model = column0.Substring(2).Trim();
2019-11-02 01:40:41 +00:00
}
else
{
2019-11-02 23:52:33 +00:00
int cutOffset = column0.IndexOf(" - ", StringComparison.Ordinal);
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(cutOffset == -1)
2019-11-02 01:40:41 +00:00
{
manufacturer = null;
2019-11-02 23:52:33 +00:00
model = column0;
2019-11-02 01:40:41 +00:00
}
else
{
manufacturer = column0.Substring(0, cutOffset).Trim();
2019-11-02 23:52:33 +00:00
model = column0.Substring(cutOffset + 3).Trim();
2019-11-02 01:40:41 +00:00
}
}
2019-11-02 23:52:33 +00:00
switch(manufacturer)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
case"Lite-ON":
2019-11-02 01:40:41 +00:00
manufacturer = "JLMS";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
break;
2019-11-02 23:52:33 +00:00
case"LG Electronics":
2019-11-02 01:40:41 +00:00
manufacturer = "HL-DT-ST";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
break;
2019-11-02 23:52:33 +00:00
case"Panasonic":
2019-11-02 01:40:41 +00:00
manufacturer = "MATSHITA";
2019-11-02 23:52:33 +00:00
2019-11-02 01:40:41 +00:00
break;
}
2019-11-02 23:52:33 +00:00
CompactDiscOffset cdOffset =
2019-11-02 01:40:41 +00:00
ctx.CdOffsets.FirstOrDefault(o => o.Manufacturer == manufacturer && o.Model == model);
2019-11-02 23:52:33 +00:00
if(column1.ToLowerInvariant() == "[purged]")
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
if(cdOffset != null)
ctx.CdOffsets.Remove(cdOffset);
2019-11-02 01:40:41 +00:00
continue;
}
2019-11-02 23:52:33 +00:00
if(!short.TryParse(column1, out short offset))
continue;
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
if(!int.TryParse(column2, out int submissions))
continue;
if(column3[column3.Length - 1] != '%')
continue;
2019-11-02 01:40:41 +00:00
column3 = column3.Substring(0, column3.Length - 1);
2019-11-02 23:52:33 +00:00
if(!float.TryParse(column3, out float percentage))
continue;
2019-11-02 01:40:41 +00:00
percentage /= 100;
2019-11-02 23:52:33 +00:00
if(cdOffset is null)
2019-11-02 01:40:41 +00:00
{
cdOffset = new CompactDiscOffset
{
2019-11-02 23:52:33 +00:00
AddedWhen = DateTime.UtcNow, ModifiedWhen = DateTime.UtcNow, Agreement = percentage,
Manufacturer = manufacturer, Model = model, Offset = offset,
Submissions = submissions
2019-11-02 01:40:41 +00:00
};
ctx.CdOffsets.Add(cdOffset);
addedOffsets++;
}
else
{
2019-11-02 23:52:33 +00:00
if(Math.Abs(cdOffset.Agreement - percentage) > 0)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
cdOffset.Agreement = percentage;
2019-11-02 01:40:41 +00:00
cdOffset.ModifiedWhen = DateTime.UtcNow;
}
2019-11-02 23:52:33 +00:00
if(cdOffset.Offset != offset)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
cdOffset.Offset = offset;
2019-11-02 01:40:41 +00:00
cdOffset.ModifiedWhen = DateTime.UtcNow;
}
2019-11-02 23:52:33 +00:00
if(cdOffset.Submissions != submissions)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
cdOffset.Submissions = submissions;
2019-11-02 01:40:41 +00:00
cdOffset.ModifiedWhen = DateTime.UtcNow;
}
2019-11-02 23:52:33 +00:00
if(Math.Abs(cdOffset.Agreement - percentage) > 0 ||
cdOffset.Offset != offset ||
cdOffset.Submissions != submissions)
modifiedOffsets++;
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
foreach(Device device in ctx.
Devices.
Where(d => d.Manufacturer == null && d.Model != null &&
d.Model.Trim() == model).
Union(ctx.Devices.Where(d => d.Manufacturer != null &&
d.Manufacturer.Trim() == manufacturer &&
d.Model != null &&
d.Model == model)))
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
if(device.CdOffset == cdOffset &&
device.ModifiedWhen == cdOffset.ModifiedWhen)
continue;
2019-11-02 01:40:41 +00:00
2019-11-02 23:52:33 +00:00
device.CdOffset = cdOffset;
2019-11-02 01:40:41 +00:00
device.ModifiedWhen = cdOffset.ModifiedWhen;
}
}
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
if(File.Exists("drive_offsets.json"))
{
var sr = new StreamReader("drive_offsets.json");
var offsets = JsonSerializer.Deserialize<CompactDiscOffset[]>(sr.ReadToEnd());
if(offsets != null)
{
foreach(var offset in offsets)
{
CompactDiscOffset cdOffset =
ctx.CdOffsets.FirstOrDefault(o => o.Manufacturer == offset.Manufacturer && o.Model == offset.Model);
if(cdOffset is null)
{
offset.ModifiedWhen = DateTime.UtcNow;
ctx.CdOffsets.Add(offset);
addedOffsets++;
}
else
{
if(Math.Abs(cdOffset.Agreement - offset.Agreement) > 0 || offset.Agreement < 0)
{
cdOffset.Agreement = offset.Agreement;
cdOffset.ModifiedWhen = DateTime.UtcNow;
}
if(cdOffset.Offset != offset.Offset)
{
cdOffset.Offset = offset.Offset;
cdOffset.ModifiedWhen = DateTime.UtcNow;
}
if(cdOffset.Submissions != offset.Submissions)
{
cdOffset.Submissions = offset.Submissions;
cdOffset.ModifiedWhen = DateTime.UtcNow;
}
if(Math.Abs(cdOffset.Agreement - offset.Agreement) > 0 ||
cdOffset.Offset != offset.Offset ||
cdOffset.Submissions != offset.Submissions)
modifiedOffsets++;
}
}
}
}
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Committing changes...", DateTime.UtcNow);
2019-11-02 01:40:41 +00:00
start = DateTime.UtcNow;
ctx.SaveChanges();
end = DateTime.UtcNow;
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Took {1:F2} seconds", end, (end - start).TotalSeconds);
2019-11-02 01:40:41 +00:00
2019-11-02 21:39:19 +00:00
System.Console.WriteLine("{0}: Added {1} offsets", end, addedOffsets);
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Modified {1} offsets", end, modifiedOffsets);
2019-11-02 01:40:41 +00:00
}
2019-11-02 23:52:33 +00:00
catch(Exception ex)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
#if DEBUG
if(Debugger.IsAttached)
throw;
#endif
2019-11-02 21:01:25 +00:00
System.Console.WriteLine("{0}: Exception {1} filling CompactDisc read offsets...", DateTime.UtcNow, ex);
2019-11-02 01:40:41 +00:00
}
}
}
}