Skip to content

Commit 0cd2849

Browse files
authored
Refactor Parser interface names. (#18)
* Renamed ILogFile to IParser and ILogFileParser to IParserFactory PluginFramework loads factory instances at startup which improve performance. Fixed spelling of Factories * Cleaning of TestPlugin
1 parent d5c37b6 commit 0cd2849

17 files changed

+292
-219
lines changed

PluginFramework/IAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace PluginFramework
44
{
55
public interface IAnalyzer
66
{
7-
Dictionary<string, object> Analyze(ILogFile logfile);
7+
Dictionary<string, object> Analyze(IParser logfile);
88
}
99
}

PluginFramework/ILogFile.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

PluginFramework/ILogFileParser.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

PluginFramework/IParserFactory.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
3+
namespace PluginFramework
4+
{
5+
/// <summary>
6+
/// Interface for providing a log file parser.
7+
/// </summary>
8+
public interface IParserFactory
9+
{
10+
/// <summary>
11+
/// Returns true if the specified file can be opened.
12+
/// </summary>
13+
/// Allows for further checks that just the filename, for example reading a header.
14+
/// <param name="filename">the filename to check</param>
15+
/// <returns>True if file can be loaded. False if loading is not possible.</returns>
16+
bool CanOpen(string filename);
17+
18+
/// <summary>
19+
/// Open the specified log file using the specified settings.
20+
/// </summary>
21+
/// <param name="filename">The filename to open</param>
22+
/// <param name="settings">The setting to use when loading. Setting should be collected from user with the ShowSettings method.</param>
23+
/// <returns>The loaded log file object.</returns>
24+
IParser Open(string filename, ParserSettings settings = null);
25+
26+
/// <summary>
27+
/// Show file load settings dialog to user
28+
/// </summary>
29+
/// <param name="filename">The filename to open</param>
30+
/// <param name="settings">Key/value collection of settings. </param>
31+
/// <returns>True if file should be loaded. False if loading should be aborted.</returns>
32+
bool ShowSettingsDialog(string filename, ref ParserSettings settings);
33+
}
34+
35+
/// <summary>
36+
/// Parser for a log format
37+
/// </summary>
38+
public interface IParser
39+
{
40+
/// <summary>
41+
/// List of signals provided by this parser
42+
/// </summary>
43+
SignalList Signals { get; }
44+
45+
/// <summary>
46+
/// Read and return the provided signal
47+
/// </summary>
48+
/// <param name="signal">The signal to read</param>
49+
/// <returns>The sample data points of the signal</returns>
50+
Sample[] ReadSignal(Signal signal);
51+
52+
/// <summary>
53+
/// The file origo time.
54+
/// </summary>
55+
DateTime Origo { get; }
56+
57+
/// <summary>
58+
/// The duration of the data in the file
59+
/// </summary>
60+
TimeSpan Length { get; }
61+
}
62+
63+
public struct Sample
64+
{
65+
public DateTime Time;
66+
public double Value;
67+
}
68+
69+
public struct Signal
70+
{
71+
public int UID;
72+
public string Name;
73+
public string FriendlyName;
74+
public string Unit;
75+
public string Description;
76+
public string Tag;
77+
}
78+
}

PluginFramework/IProjectViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface IProjectViewModel: INotifyPropertyChanged
2828
event EventHandler FilesRealigned;
2929

3030
void AddLogFile(string filename);
31-
void AddLogFile(string filename, ILogFileParser parser);
31+
void AddLogFile(string filename, IParserFactory parser);
3232
List<ISignalViewModel> FindSignalByTag(string tag);
3333
ISignalViewModel GetSignal(string path);
3434
IMarkerViewModel GetMarkerViewModel(IMarkerModel markerModel);

PluginFramework/PluginFactory.cs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public static class PluginFactory
1616
static PluginFactory()
1717
{
1818
Info = new List<PluginInfo>();
19-
Parsers = new List<Type>();
19+
ParserFactories = new List<IParserFactory>();
2020
Analyzers = new List<Type>();
21-
PanelFactorys = new List<IPanelFactory>();
21+
PanelFactories = new List<IPanelFactory>();
2222
}
2323

2424
/// <summary>
@@ -48,9 +48,10 @@ public static List<Exception> LoadPlugins(string pluginPath)
4848
{
4949
foreach (var item in type.GetInterfaces())
5050
{
51-
if (item.Name == nameof(ILogFileParser) && !type.IsGenericType && !type.IsInterface)
51+
if (item.Name == nameof(IParserFactory) && !type.IsGenericType && !type.IsInterface)
5252
{
53-
Parsers.Add(type);
53+
IParserFactory factory = PluginFactory.CreateParserFactory(type);
54+
ParserFactories.Add(factory);
5455

5556
ParserPluginAttribute attr = type.GetCustomAttribute<ParserPluginAttribute>(false);
5657
if (attr != null)
@@ -67,7 +68,7 @@ public static List<Exception> LoadPlugins(string pluginPath)
6768
else if (item.Name == nameof(IPanelFactory) && !type.IsGenericType && !type.IsInterface)
6869
{
6970
IPanelFactory factory = PluginFactory.CreatePanelFactory(type);
70-
PanelFactorys.Add(factory);
71+
PanelFactories.Add(factory);
7172
info.AddItem(new PluginInfo.ItemInfo() { Name = factory.Title, Type = PluginInfo.ItemType.Display });
7273
}
7374
}
@@ -81,7 +82,7 @@ public static List<Exception> LoadPlugins(string pluginPath)
8182
Exception exL = ex.LoaderExceptions[0];
8283
if (exL is TypeLoadException)
8384
{
84-
exList.Add(new PluginLoadException("Plugin " + name + " could not load! Please contact plugin author.", exL));
85+
exList.Add(new PluginLoadException("Plugin " + name + " could not load from " + pluginPath + "! Please contact plugin author.", exL));
8586
}
8687
else
8788
{
@@ -107,15 +108,15 @@ public static List<Exception> LoadPlugins(string pluginPath)
107108
public static void Reset()
108109
{
109110
Info.Clear();
110-
Parsers.Clear();
111+
ParserFactories.Clear();
111112
Analyzers.Clear();
112-
PanelFactorys.Clear();
113+
PanelFactories.Clear();
113114
}
114115

115116
private static List<Type> modelTypes = new List<Type>();
116117
public static Type[] GetModelTypes()
117118
{
118-
var types = from item in PluginFactory.PanelFactorys select item.ModelType;
119+
var types = from item in PluginFactory.PanelFactories select item.ModelType;
119120
var list = types.ToList<Type>();
120121
list.AddRange(modelTypes);
121122
return list.ToArray<Type>();
@@ -127,18 +128,18 @@ public static void RegisterModelType(Type modelType)
127128
}
128129

129130
public static List<PluginInfo> Info {get; private set;}
130-
public static List<Type> Parsers { get; private set; }
131+
public static List<IParserFactory> ParserFactories { get; private set; }
131132
public static List<Type> Analyzers { get; private set; }
132-
public static List<IPanelFactory> PanelFactorys { get; private set; }
133+
public static List<IPanelFactory> PanelFactories { get; private set; }
133134

134135
public static string FileFilterString
135136
{
136137
get
137138
{
138139
List<string> fileFilter = new List<string>();
139-
foreach (Type type in Parsers)
140+
foreach (IParserFactory type in ParserFactories)
140141
{
141-
ParserPluginAttribute attr = type.GetCustomAttribute<ParserPluginAttribute>(false);
142+
ParserPluginAttribute attr = type.GetType().GetCustomAttribute<ParserPluginAttribute>(false);
142143
if (attr != null)
143144
{
144145
fileFilter.Add(attr.FileType);
@@ -151,9 +152,9 @@ public static string FileFilterString
151152
}
152153
}
153154

154-
public static ILogFileParser CreateLogFileParser(Type type)
155+
public static IParserFactory CreateParserFactory(Type type)
155156
{
156-
ILogFileParser plugin = (ILogFileParser)Activator.CreateInstance(type);
157+
IParserFactory plugin = (IParserFactory)Activator.CreateInstance(type);
157158
return plugin;
158159
}
159160

@@ -169,23 +170,18 @@ public static IPanelFactory CreatePanelFactory(Type type)
169170
return factory;
170171
}
171172

172-
public static Type FindParser(string typename)
173+
public static IParserFactory FindParser(string typename)
173174
{
174-
foreach (var item in Parsers)
175-
{
176-
if (item.FullName == typename)
177-
return item;
178-
}
175+
var ret = from item in ParserFactories where item.GetType().FullName == typename select item;
179176

180-
return null;
177+
return ret.FirstOrDefault();
181178
}
182179

183-
public static ILogFileParser FindLogFileParser(string filename)
180+
public static IParserFactory FindLogFileParser(string filename)
184181
{
185-
ILogFileParser suggestedLogFileParser = null;
186-
foreach (Type pluginType in PluginFactory.Parsers)
182+
IParserFactory suggestedLogFileParser = null;
183+
foreach (IParserFactory tmpParser in PluginFactory.ParserFactories)
187184
{
188-
ILogFileParser tmpParser = PluginFactory.CreateLogFileParser(pluginType);
189185
if (tmpParser.CanOpen(filename))
190186
{
191187
suggestedLogFileParser = tmpParser;
@@ -221,17 +217,11 @@ public static List<Type> FindAnalyzers(Type logfileParser)
221217

222218
public static IPanelFactory FindPanelFactory(IPanelModel model)
223219
{
224-
var ret = from item in PanelFactorys where item.GetType().FullName == model.FactoryReference select item;
220+
var ret = from item in PanelFactories where item.GetType().FullName == model.FactoryReference select item;
225221

226222
return ret.FirstOrDefault();
227223
}
228224

229-
public static ParserPluginAttribute GetPluginAttribute(string typename)
230-
{
231-
Type pluginType = FindParser(typename);
232-
return pluginType.GetCustomAttribute<ParserPluginAttribute>(false);
233-
}
234-
235225
private static Assembly MyInterceptMethod(object sender, ResolveEventArgs e)
236226
{
237227
string path = AppDomain.CurrentDomain.BaseDirectory;

PluginFramework/PluginFramework.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
<Compile Include="IDialogServiceExtcs.cs" />
7878
<Compile Include="IFileModel.cs" />
7979
<Compile Include="IFileViewModel.cs" />
80-
<Compile Include="ILogFile.cs" />
81-
<Compile Include="ILogFileParser.cs" />
80+
<Compile Include="SignalList.cs" />
81+
<Compile Include="IParserFactory.cs" />
8282
<Compile Include="IMarkerModel.cs" />
8383
<Compile Include="IMarkerViewModel.cs" />
8484
<Compile Include="IPanel.cs" />

PluginFramework/SignalList.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace PluginFramework
2+
{
3+
using System.Collections.Generic;
4+
5+
public class SignalList:List<Signal>
6+
{
7+
public Signal GetSignalByName(string name)
8+
{
9+
return this.Find(p => p.Name == name);
10+
}
11+
12+
public Signal GetSignalByUID(int uid)
13+
{
14+
return this.Find(p => p.UID == uid);
15+
}
16+
}
17+
}

Sample Crunch/MainWindow.xaml.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
7474
}
7575

7676
// Add local factories which will not be found because they are not in dll's.
77-
PluginFactory.PanelFactorys.Add(PluginFactory.CreatePanelFactory(typeof(Factory.MarkerPanelFactory)));
78-
PluginFactory.PanelFactorys.Add(PluginFactory.CreatePanelFactory(typeof(Factory.ProjectPanelFactory)));
77+
PluginFactory.PanelFactories.Add(PluginFactory.CreatePanelFactory(typeof(Factory.MarkerPanelFactory)));
78+
PluginFactory.PanelFactories.Add(PluginFactory.CreatePanelFactory(typeof(Factory.ProjectPanelFactory)));
7979

8080
MainViewModel.PropertyChanged += Main_PropertyChanged;
8181

82-
foreach (var item in PluginFactory.PanelFactorys)
82+
foreach (var item in PluginFactory.PanelFactories)
8383
{
8484
// Add if it has no attribute set or if the attribute is set check the visible flag
8585
PanelPluginAttribute attr = item.GetType().GetCustomAttribute<PanelPluginAttribute>(false);
@@ -139,9 +139,9 @@ private void SendConfigurationTelemetry()
139139
StringBuilder sb = new StringBuilder();
140140

141141
// Log Parsers
142-
foreach (Type parserType in PluginFactory.Parsers)
142+
foreach (IParserFactory parser in PluginFactory.ParserFactories)
143143
{
144-
ParserPluginAttribute attr = parserType.GetCustomAttribute<ParserPluginAttribute>(false);
144+
ParserPluginAttribute attr = parser.GetType().GetCustomAttribute<ParserPluginAttribute>(false);
145145
if (attr != null)
146146
{
147147
sb.AppendFormat("{0} ({1}), ", attr.Title, attr.FileType);
@@ -151,7 +151,7 @@ private void SendConfigurationTelemetry()
151151
sb.Clear();
152152

153153
// Log Panels
154-
PluginFactory.PanelFactorys.ForEach((str) => { sb.AppendFormat("{0} ({1}), ", str.Title, str.ToString()); });
154+
PluginFactory.PanelFactories.ForEach((str) => { sb.AppendFormat("{0} ({1}), ", str.Title, str.ToString()); });
155155
props.Add("Panels", sb.ToString());
156156
sb.Clear();
157157

Sample Crunch/ProjectPane.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private void treeView_DragOver(object sender, DragEventArgs e)
151151
{
152152
try
153153
{
154-
ILogFileParser lfp = PluginFactory.FindLogFileParser(filename);
154+
IParserFactory lfp = PluginFactory.FindLogFileParser(filename);
155155
if (lfp != null)
156156
{
157157
e.Effects = DragDropEffects.Copy;

0 commit comments

Comments
 (0)