Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit fcd4b3a8 authored by Harald Martini's avatar Harald Martini
Browse files

write overridden files to override.txt

parent 61f294e5
No related branches found
No related tags found
No related merge requests found
...@@ -84,6 +84,7 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -84,6 +84,7 @@ namespace TUGraz.VectoCore.Models.Declaration
public override double Lookup(MeterPerSecond targetVelocity) public override double Lookup(MeterPerSecond targetVelocity)
{ {
WarnReadFromFile();
var section = Data.GetSection(kv => kv.Key < targetVelocity); var section = Data.GetSection(kv => kv.Key < targetVelocity);
return VectoMath.Interpolate(section.Item1.Key, section.Item2.Key, section.Item1.Value, section.Item2.Value, return VectoMath.Interpolate(section.Item1.Key, section.Item2.Key, section.Item1.Value, section.Item2.Value,
targetVelocity); targetVelocity);
...@@ -132,6 +133,7 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -132,6 +133,7 @@ namespace TUGraz.VectoCore.Models.Declaration
public override double Lookup(MeterPerSecond targetVelocity) public override double Lookup(MeterPerSecond targetVelocity)
{ {
WarnReadFromFile();
var section = Data.GetSection(kv => kv.Key < targetVelocity); var section = Data.GetSection(kv => kv.Key < targetVelocity);
return VectoMath.Interpolate(section.Item1.Key, section.Item2.Key, section.Item1.Value, section.Item2.Value, return VectoMath.Interpolate(section.Item1.Key, section.Item2.Key, section.Item1.Value, section.Item2.Value,
targetVelocity); targetVelocity);
......
...@@ -35,8 +35,10 @@ using System; ...@@ -35,8 +35,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using TUGraz.VectoCommon.Exceptions; using TUGraz.VectoCommon.Exceptions;
using TUGraz.VectoCommon.Models; using TUGraz.VectoCommon.Models;
using TUGraz.VectoCommon.Utils; using TUGraz.VectoCommon.Utils;
...@@ -50,6 +52,22 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -50,6 +52,22 @@ namespace TUGraz.VectoCore.Models.Declaration
protected abstract string ErrorMessage { get; } protected abstract string ErrorMessage { get; }
protected abstract void ParseData(DataTable table); protected abstract void ParseData(DataTable table);
#if USE_EXTERNAL_DECLARATION_DATA
protected bool _readFromFile = false;
private static bool _overrideFileDeleted = false;
#endif
[Conditional("USE_EXTERNAL_DECLARATION_DATA")]
protected void WarnReadFromFile()
{
if (_readFromFile) {
//Not displayed in simulation window (don't know why? Just write to file for now)
//var tmp = ResourceId?.Replace(DeclarationData.DeclarationDataResourcePrefix + ".", "") ?? "unknown resource";
//Log.Warn(string.Format("{0} overridden", tmp));
}
}
protected LookupData() protected LookupData()
{ {
ReadData(); ReadData();
...@@ -58,24 +76,44 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -58,24 +76,44 @@ namespace TUGraz.VectoCore.Models.Declaration
protected void ReadData() protected void ReadData()
{ {
if (!string.IsNullOrWhiteSpace(ResourceId)) { if (!string.IsNullOrWhiteSpace(ResourceId)) {
var table = ReadCsvResource(ResourceId, (s) => System.Diagnostics.Debug.WriteLine(s)); var table = ReadCsvResource(ResourceId, (s) => {
System.Diagnostics.Debug.WriteLine(s);
Log.Warn(s);
});
NormalizeTable(table); NormalizeTable(table);
ParseData(table); ParseData(table);
} }
} }
#if USE_EXTERNAL_DECLARATION_DATA
protected static DataTable ReadCsvResource(string resourceId, Action<string> overrideWarning = null) [MethodImpl(MethodImplOptions.Synchronized)]
#endif
protected DataTable ReadCsvResource(string resourceId, Action<string> overrideWarning = null)
{ {
// TODO: MQ 2020-07 Remove in official bus version! // TODO: MQ 2020-07 Remove in official bus version!
#if USE_EXTERNAL_DECLARATION_DATA #if USE_EXTERNAL_DECLARATION_DATA
var tmp = resourceId.Replace(DeclarationData.DeclarationDataResourcePrefix + ".", ""); var tmp = resourceId.Replace(DeclarationData.DeclarationDataResourcePrefix + ".", "");
var parts = tmp.Split('.'); var parts = tmp.Split('.');
//one dir up
var fileName = Path.GetFullPath(Path.Combine(@"Declaration\Override", string.Join(".", parts[parts.Length-2], parts[parts.Length-1]))); var fileName = Path.GetFullPath(Path.Combine(@"Declaration\Override", string.Join(".", parts[parts.Length-2], parts[parts.Length-1])));
Console.WriteLine(fileName);
if (File.Exists(fileName)) { if (File.Exists(fileName)) {
if (overrideWarning != null) { if (overrideWarning != null) {
overrideWarning($"{resourceId} overridden by {fileName}"); overrideWarning($"{resourceId} overridden by {fileName}");
} }
return VectoCSVFile.Read(fileName);
_readFromFile = true;
var overrideFileName = "override.txt";
if (File.Exists(overrideFileName) && !_overrideFileDeleted) {
File.Delete(overrideFileName);
_overrideFileDeleted = true;
}
using (StreamWriter w = File.AppendText(overrideFileName))
{
w.WriteLine(string.Format("{0}: {1}", DateTime.Now, fileName));
w.Flush();
}
return VectoCSVFile.Read(fileName);
} }
#endif #endif
return VectoCSVFile.ReadStream(RessourceHelper.ReadStream(resourceId), source: resourceId); return VectoCSVFile.ReadStream(RessourceHelper.ReadStream(resourceId), source: resourceId);
...@@ -98,6 +136,7 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -98,6 +136,7 @@ namespace TUGraz.VectoCore.Models.Declaration
public virtual TValue Lookup(TKey key) public virtual TValue Lookup(TKey key)
{ {
WarnReadFromFile();
try { try {
return Data[key]; return Data[key];
} catch (KeyNotFoundException) { } catch (KeyNotFoundException) {
...@@ -114,7 +153,8 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -114,7 +153,8 @@ namespace TUGraz.VectoCore.Models.Declaration
public virtual TValue Lookup(TKey1 key1, TKey2 key2) public virtual TValue Lookup(TKey1 key1, TKey2 key2)
{ {
try { WarnReadFromFile();
try {
return Data[Tuple.Create(key1, key2)]; return Data[Tuple.Create(key1, key2)];
} catch (KeyNotFoundException) { } catch (KeyNotFoundException) {
throw new VectoException(string.Format(ErrorMessage, key1, key2)); throw new VectoException(string.Format(ErrorMessage, key1, key2));
...@@ -129,7 +169,8 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -129,7 +169,8 @@ namespace TUGraz.VectoCore.Models.Declaration
public virtual TValue Lookup(TKey1 key1, TKey2 key2, TKey3 key3) public virtual TValue Lookup(TKey1 key1, TKey2 key2, TKey3 key3)
{ {
try { WarnReadFromFile();
try {
return Data[Tuple.Create(key1, key2, key3)]; return Data[Tuple.Create(key1, key2, key3)];
} catch (KeyNotFoundException) { } catch (KeyNotFoundException) {
throw new VectoException(string.Format(ErrorMessage, key1, key2, key3)); throw new VectoException(string.Format(ErrorMessage, key1, key2, key3));
......
...@@ -91,6 +91,7 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -91,6 +91,7 @@ namespace TUGraz.VectoCore.Models.Declaration
public override PT1Result Lookup(PerSecond key) public override PT1Result Lookup(PerSecond key)
{ {
WarnReadFromFile();
var extrapolated = key.IsSmaller(_entries[0].Key) || key.IsGreater(_entries.Last().Key); var extrapolated = key.IsSmaller(_entries[0].Key) || key.IsGreater(_entries.Last().Key);
var index = _entries.FindIndex(x => x.Key.IsGreater(key)); var index = _entries.FindIndex(x => x.Key.IsGreater(key));
......
...@@ -95,6 +95,7 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -95,6 +95,7 @@ namespace TUGraz.VectoCore.Models.Declaration
public override StandardBody Lookup(string id) public override StandardBody Lookup(string id)
{ {
WarnReadFromFile();
return string.IsNullOrWhiteSpace(id) ? Empty : base.Lookup(id); return string.IsNullOrWhiteSpace(id) ? Empty : base.Lookup(id);
} }
......
...@@ -48,6 +48,7 @@ namespace TUGraz.VectoCore.Models.Declaration ...@@ -48,6 +48,7 @@ namespace TUGraz.VectoCore.Models.Declaration
public override Entry Lookup(string key) public override Entry Lookup(string key)
{ {
WarnReadFromFile();
return base.Lookup(key.RemoveWhitespace()); return base.Lookup(key.RemoveWhitespace());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment