public bool Execute(MeterDataSet meterDataSet) { if (meterDataSet.Type != DataSetType.BENConfig) { return(false); } using (AdoDataConnection connection = new AdoDataConnection("systemSettings")) { FileInfo fi = new FileInfo(meterDataSet.FilePath); DateTime lastWriteTime = DateTime.ParseExact(string.Join(",", fi.Name.Split(',').Take(2)), "yyMMdd,HHmmssfff", CultureInfo.InvariantCulture); // see if there is already a record that matches this file ConfigFileChanges configFileChanges = new TableOperations <ConfigFileChanges>(connection).QueryRecordWhere("MeterID = {0} AND FileName = {1} AND LastWriteTime = {2}", meterDataSet.Meter.ID, $"{meterDataSet.Meter.AssetKey}.cfg", lastWriteTime); // if a record already exists for this file skip it. There was probably an error. if (configFileChanges != null) { return(false); } configFileChanges = new ConfigFileChanges(); // create new record configFileChanges.MeterID = meterDataSet.Meter.ID; configFileChanges.FileName = $"{meterDataSet.Meter.AssetKey}.cfg"; configFileChanges.LastWriteTime = lastWriteTime; configFileChanges.Text = meterDataSet.Text.Replace("\r", ""); string[] data = File.ReadAllLines(meterDataSet.FilePath); int[] channelCounts = data[1].Split(',').Select(x => int.Parse(x.Replace("A", "").Replace("D", ""))).ToArray(); int totalChannels = channelCounts[0]; // get portion of cfg file that contains channel mappings string relevantPortion = string.Join("\n", data.Take(2 + totalChannels)); // get the previous record for this file ConfigFileChanges lastChanges = new TableOperations <ConfigFileChanges>(connection).QueryRecord("LastWriteTime DESC", new RecordRestriction("MeterID = {0} AND FileName = {1} AND LastWriteTime < {2}", meterDataSet.Meter.ID, $"{meterDataSet.Meter.AssetKey}.cfg", lastWriteTime)); // if there were no previous records for this file if (lastChanges == null) { lastChanges = new ConfigFileChanges(); lastChanges.Text = configFileChanges.Text.Replace("\r", ""); // make diffs DiffMatchPatch dmp = new DiffMatchPatch(); List <Diff> diff = dmp.DiffMain(relevantPortion, relevantPortion); dmp.DiffCleanupSemantic(diff); configFileChanges.Html = dmp.DiffPrettyHtml(diff).Replace("¶", ""); configFileChanges.Changes = 0; } else { string[] data2 = lastChanges.Text.Split('\n'); int[] channelCounts2 = data2[1].Split(',').Select(x => int.Parse(x.Replace("A", "").Replace("D", ""))).ToArray(); int totalChannels2 = channelCounts2[0]; // get portion of cfg file that contains channel mappings string relevantPortion2 = string.Join("\n", data2.Take(2 + totalChannels2)); // make diffs DiffMatchPatch dmp = new DiffMatchPatch(); List <Diff> diff = dmp.DiffMain(relevantPortion2, relevantPortion); List <Patch> patch = dmp.PatchMake(relevantPortion2, relevantPortion); dmp.DiffCleanupSemantic(diff); configFileChanges.Html = dmp.DiffPrettyHtml(diff).Replace("¶", ""); configFileChanges.Changes = patch.Count; if (patch.Count == 0) { return(false); } } // write new record to db meterDataSet.ConfigChanges = configFileChanges.Changes; new TableOperations <ConfigFileChanges>(connection).AddNewRecord(configFileChanges); return(true); } }
public bool Execute(MeterDataSet meterDataSet) { if (meterDataSet.Type != DataSetType.Config) { return(false); } using (AdoDataConnection connection = new AdoDataConnection("systemSettings")) { FileInfo fi = new FileInfo(meterDataSet.FilePath); // see if there is already a record that matches this file ConfigFileChanges configFileChanges = new TableOperations <ConfigFileChanges>(connection).QueryRecordWhere("MeterID = {0} AND FileName = {1} AND LastWriteTime = {2}", meterDataSet.Meter.ID, fi.Name, fi.LastWriteTime); // if a record already exists for this file skip it. There was probably an error. if (configFileChanges != null) { return(false); } configFileChanges = new ConfigFileChanges(); // create new record configFileChanges.MeterID = meterDataSet.Meter.ID; configFileChanges.FileName = fi.Name; configFileChanges.LastWriteTime = fi.LastWriteTime; configFileChanges.Text = meterDataSet.Text; // get the previous record for this file ConfigFileChanges lastChanges = new TableOperations <ConfigFileChanges>(connection).QueryRecord("LastWriteTime DESC", new RecordRestriction("MeterID = {0} AND FileName = {1} AND LastWriteTime < {2}", meterDataSet.Meter.ID, fi.Name, fi.LastWriteTime)); // if there were no previous records for this file, just diff it against itself because we need an intial record. if (lastChanges == null) { lastChanges = new ConfigFileChanges(); lastChanges.Text = meterDataSet.Text; DiffMatchPatch dmp = new DiffMatchPatch(); List <Diff> diff = dmp.DiffMain(lastChanges.Text, configFileChanges.Text); List <Patch> patch = dmp.PatchMake(lastChanges.Text, configFileChanges.Text); dmp.DiffCleanupSemantic(diff); configFileChanges.Html = dmp.DiffPrettyHtml(diff).Replace("¶", ""); configFileChanges.Changes = patch.Count; // write new record to db meterDataSet.ConfigChanges = configFileChanges.Changes; } else { // make diffs DiffMatchPatch dmp = new DiffMatchPatch(); List <Diff> diff = dmp.DiffMain(lastChanges.Text, configFileChanges.Text); List <Patch> patch = dmp.PatchMake(lastChanges.Text, configFileChanges.Text); if (patch.Count == 0) { return(false); } dmp.DiffCleanupSemantic(diff); configFileChanges.Html = dmp.DiffPrettyHtml(diff).Replace("¶", ""); configFileChanges.Changes = patch.Count; // write new record to db meterDataSet.ConfigChanges = configFileChanges.Changes; } new TableOperations <ConfigFileChanges>(connection).AddNewRecord(configFileChanges); return(true); } }