380 lines
14 KiB
C#
380 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.ComponentModel;
|
|
|
|
namespace DTS.SensorDB
|
|
{
|
|
public class SensorGroup : INotifyPropertyChanged
|
|
{
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
protected bool SetProperty<T>(ref T storage, T value, String propertyName)
|
|
{
|
|
if (object.Equals(storage, value)) return false;
|
|
|
|
storage = value;
|
|
this.OnPropertyChanged(propertyName);
|
|
return true;
|
|
}
|
|
protected void OnPropertyChanged(string propertyName)
|
|
{
|
|
var eventHandler = this.PropertyChanged;
|
|
if (eventHandler != null)
|
|
{
|
|
eventHandler(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
|
|
private string _groupName;
|
|
public string GroupName
|
|
{
|
|
get { return _groupName; }
|
|
set{ SetProperty(ref _groupName, value, "GroupName");}
|
|
}
|
|
|
|
private string _groupDescription;
|
|
public string GroupDescription
|
|
{
|
|
get { return _groupDescription; }
|
|
set { SetProperty(ref _groupDescription, value, "GroupDescription"); }
|
|
}
|
|
|
|
private string _category = "";
|
|
public string Category
|
|
{
|
|
get { return _category; }
|
|
set { SetProperty(ref _category, value, "Category"); }
|
|
}
|
|
|
|
private string _uuid = "";
|
|
public string UUID
|
|
{
|
|
get { return _uuid; }
|
|
set { SetProperty(ref _uuid, value, "UUID"); }
|
|
}
|
|
|
|
private List<SensorData> _sensors = new List<SensorData>();
|
|
public SensorData[] Sensors
|
|
{
|
|
get { return _sensors.ToArray(); }
|
|
set { SetProperty(ref _sensors, new List<SensorData>(value), "Sensors"); }
|
|
}
|
|
public SensorCalibration[] Calibrations
|
|
{
|
|
get { return _sensorCalibrations.ToArray(); }
|
|
set { SetProperty(ref _sensorCalibrations, new List<SensorCalibration>(value), "Calibrations"); }
|
|
}
|
|
public SensorCalibration GetCalibrationForSensor(int index)
|
|
{
|
|
if (index >= _sensorCalibrations.Count) { return null; }
|
|
else { return _sensorCalibrations[index]; }
|
|
}
|
|
|
|
public void SetCalibrationForSensor(int index, SensorCalibration sc)
|
|
{
|
|
_sensorCalibrations[index] = sc;
|
|
}
|
|
private List<SensorCalibration> _sensorCalibrations = new List<SensorCalibration>();
|
|
private List<SensorGroup> _groups = new List<SensorGroup>();
|
|
public SensorGroup[] Groups
|
|
{
|
|
get { return _groups.ToArray(); }
|
|
set { SetProperty(ref _groups, new List<SensorGroup>(value), "Groups"); }
|
|
}
|
|
internal void WriteToXML(System.Xml.XmlWriter writer)
|
|
{
|
|
writer.WriteStartElement("Group");
|
|
|
|
writer.WriteStartElement("GroupName");
|
|
writer.WriteValue(GroupName);
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement("GroupDescription");
|
|
writer.WriteValue(GroupDescription);
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement("Category");
|
|
writer.WriteValue(Category);
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement("UUID");
|
|
writer.WriteValue(UUID);
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement("Sensors");
|
|
foreach (var sd in _sensors)
|
|
{
|
|
var element = sd.ToXElement();
|
|
element.WriteTo(writer);
|
|
}
|
|
writer.WriteEndElement();
|
|
writer.WriteStartElement("CalibrationData");
|
|
foreach (SensorCalibration sc in _sensorCalibrations.ToArray())
|
|
{
|
|
var element = sc.ToXElement();
|
|
element.WriteTo(writer);
|
|
}
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement("Groups");
|
|
foreach (var sg in _groups)
|
|
{
|
|
sg.WriteToXML(writer);
|
|
}
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteEndElement();
|
|
}
|
|
public SensorGroup()
|
|
{
|
|
}
|
|
public void RemoveGroupAt(int index)
|
|
{
|
|
_groups.RemoveAt(index);
|
|
OnPropertyChanged("Groups");
|
|
}
|
|
public void RemoveSensorAt(int index)
|
|
{
|
|
if (index < _sensorCalibrations.Count)
|
|
{
|
|
_sensorCalibrations.RemoveAt(index);
|
|
}
|
|
_sensors.RemoveAt(index);
|
|
OnPropertyChanged("Sensors");
|
|
OnPropertyChanged("Calibrations");
|
|
}
|
|
public SensorGroup(System.Xml.XmlElement node)
|
|
{
|
|
foreach (var childNode in node.ChildNodes)
|
|
{
|
|
System.Xml.XmlElement elem = childNode as System.Xml.XmlElement;
|
|
if (null == elem) { continue; }
|
|
try
|
|
{
|
|
switch (elem.Name)
|
|
{
|
|
case "GroupName":
|
|
GroupName = elem.InnerText;
|
|
break;
|
|
case "GroupDescription":
|
|
GroupDescription = elem.InnerText;
|
|
break;
|
|
case "Category":
|
|
Category = elem.InnerText;
|
|
break;
|
|
case "UUID":
|
|
UUID = elem.InnerText;
|
|
break;
|
|
case "Sensors":
|
|
{
|
|
var nodes = elem.GetElementsByTagName("Sensor");
|
|
foreach (var node2 in nodes)
|
|
{
|
|
System.Xml.XmlElement element = node2 as System.Xml.XmlElement;
|
|
if (null == element) { continue; }
|
|
try
|
|
{
|
|
_sensors.Add(new SensorData(System.Xml.Linq.XElement.Parse(element.OuterXml)));
|
|
}
|
|
catch (System.Exception ex2)
|
|
{
|
|
DTS.Utilities.Logging.APILogger.Log("Failed to parse xml", ex2, element.Name, element.InnerXml);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case "Groups":
|
|
{
|
|
//var nodes = elem.GetElementsByTagName("Group");
|
|
var nodes = elem.ChildNodes;
|
|
foreach (var node2 in nodes)
|
|
{
|
|
System.Xml.XmlElement element = node2 as System.Xml.XmlElement;
|
|
if (null == element) { continue; }
|
|
try
|
|
{
|
|
System.Diagnostics.Trace.WriteLine("adding subgroup");
|
|
_groups.Add(new SensorGroup(element));
|
|
System.Diagnostics.Trace.WriteLine("we have " + _groups.Count.ToString() + " subgroups");
|
|
}
|
|
catch (System.Exception ex2)
|
|
{
|
|
DTS.Utilities.Logging.APILogger.Log("Failed to parse xml", ex2, element.Name, element.InnerXml);
|
|
}
|
|
}
|
|
|
|
}
|
|
break;
|
|
case "CalibrationData":
|
|
{
|
|
var nodes = elem.GetElementsByTagName("Calibration");
|
|
foreach (var node2 in nodes)
|
|
{
|
|
System.Xml.XmlElement element = node2 as System.Xml.XmlElement;
|
|
if (null == element) { continue; }
|
|
try
|
|
{
|
|
SensorCalibration sc = new SensorCalibration(System.Xml.Linq.XElement.Parse(element.OuterXml));
|
|
_sensorCalibrations.Add(sc);
|
|
}
|
|
catch (System.Exception ex2)
|
|
{
|
|
DTS.Utilities.Logging.APILogger.Log("Failed to parse xml", ex2, element.Name, element.InnerXml);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
DTS.Utilities.Logging.APILogger.Log("failed to parse xml", ex, elem.Name, elem.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public class SensorGroupList: INotifyPropertyChanged
|
|
{
|
|
private Dictionary<string, SensorGroup> _sensorGroupLookup = new Dictionary<string, SensorGroup>();
|
|
public SensorGroup[] GetMasterGroups()
|
|
{
|
|
return _sensorGroupLookup.Values.ToArray();
|
|
}
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
protected bool SetProperty<T>(ref T storage, T value, String propertyName)
|
|
{
|
|
if (object.Equals(storage, value)) return false;
|
|
|
|
storage = value;
|
|
this.OnPropertyChanged(propertyName);
|
|
return true;
|
|
}
|
|
protected void OnPropertyChanged(string propertyName)
|
|
{
|
|
var eventHandler = this.PropertyChanged;
|
|
if (eventHandler != null)
|
|
{
|
|
eventHandler(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
|
|
private static SensorGroupList _list;
|
|
private static object MyLock = new object();
|
|
public static SensorGroupList GetList()
|
|
{
|
|
lock (MyLock)
|
|
{
|
|
if (null == _list)
|
|
{
|
|
_list = new SensorGroupList();
|
|
}
|
|
return _list;
|
|
}
|
|
}
|
|
/*private List<SensorGroup> _groups = new List<SensorGroup>();
|
|
public SensorGroup[] Groups
|
|
{
|
|
get { lock(MyLock){return _groups.ToArray(); }}
|
|
set { lock(MyLock){SetProperty(ref _groups, new List<SensorGroup>(value), "Groups"); }}
|
|
}*/
|
|
|
|
private SensorGroupList()
|
|
{
|
|
LoadAllGroups();
|
|
}
|
|
private void LoadAllGroups()
|
|
{
|
|
if (System.IO.File.Exists("Groups.xml"))
|
|
{
|
|
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
|
|
string xml = System.IO.File.ReadAllText("Groups.xml");
|
|
doc.LoadXml(xml);
|
|
var nodes = doc.GetElementsByTagName("Groups");
|
|
foreach (var node in nodes)
|
|
{
|
|
System.Xml.XmlElement element = node as System.Xml.XmlElement;
|
|
if (null == element) { continue; }
|
|
foreach (var childNode in element.GetElementsByTagName("Group"))
|
|
{
|
|
System.Xml.XmlElement elem = childNode as System.Xml.XmlElement;
|
|
if (null == elem) { continue; }
|
|
LoadGroup(elem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
private void LoadGroup(System.Xml.XmlElement node)
|
|
{
|
|
try
|
|
{
|
|
SensorGroup group = new SensorGroup(node);
|
|
if (!_sensorGroupLookup.ContainsKey(group.GroupName)) { _sensorGroupLookup.Add(group.GroupName, group); }
|
|
//if (!_groups.Contains(group)) { _groups.Add(group); }
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
DTS.Utilities.Logging.APILogger.Log("failed to load node", ex);
|
|
}
|
|
}
|
|
public void AddGroup(SensorGroup sg)
|
|
{
|
|
lock (MyLock)
|
|
{
|
|
//if (!_groups.Contains(sg)) { _groups.Add(sg); }
|
|
if (!_sensorGroupLookup.ContainsKey(sg.GroupName)) { _sensorGroupLookup.Add(sg.GroupName, sg); }
|
|
else { _sensorGroupLookup[sg.GroupName] = sg; }
|
|
foreach (var subgroup in sg.Groups)
|
|
{
|
|
if (!_sensorGroupLookup.ContainsKey(subgroup.GroupName)) { _sensorGroupLookup.Add(subgroup.GroupName, subgroup); }
|
|
}
|
|
}
|
|
OnPropertyChanged("Groups");
|
|
}
|
|
public void RemoveGroup(SensorGroup sg)
|
|
{
|
|
lock (MyLock)
|
|
{
|
|
_sensorGroupLookup.Remove(sg.GroupName);
|
|
//_groups.Remove(sg);
|
|
}
|
|
OnPropertyChanged("Groups");
|
|
}
|
|
|
|
public void Commit()
|
|
{
|
|
if (System.IO.File.Exists("Groups.bak"))
|
|
{
|
|
System.IO.File.Delete("Groups.bak");
|
|
}
|
|
if (System.IO.File.Exists("Groups.xml"))
|
|
{
|
|
System.IO.File.Copy("Groups.xml", "Groups.bak");
|
|
}
|
|
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create("Groups.xml", new System.Xml.XmlWriterSettings()
|
|
{
|
|
Indent = true,
|
|
NewLineChars = "\r\n",
|
|
NewLineHandling = System.Xml.NewLineHandling.Replace,
|
|
NewLineOnAttributes = true
|
|
}))
|
|
{
|
|
writer.WriteStartDocument();
|
|
writer.WriteStartElement("Groups");
|
|
|
|
foreach (var group in GetMasterGroups())
|
|
{
|
|
group.WriteToXML(writer);
|
|
}
|
|
|
|
writer.WriteEndElement();
|
|
writer.WriteEndDocument();
|
|
writer.Flush();
|
|
}
|
|
}
|
|
}
|
|
}
|