public void ConfigureEngine(Dictionary <string, DataPointsListConfiguration> configuration) { foreach (KeyValuePair <string, DataPointsListConfiguration> dataPointsConfig in configuration) { DNP3Device dNP3Device = new DNP3Device(); dNP3Device.Name = dataPointsConfig.Key; dNP3Device.Protocol = IndustryProtocols.DNP3TCP; dbContext.AddRTU(dNP3Device); foreach (AnalogInputPoint analogInput in dataPointsConfig.Value.AnalogInputPoints) { Analog analog = new Analog() { Name = analogInput.Name, Type = VariableTypes.ANALOG, RelativeAddress = (ushort)analogInput.Index, ProcContrName = dataPointsConfig.Key, MinValue = analogInput.MinIntegerTransmittedValue, MaxValue = analogInput.MaxIntegerTransmittedValue, UnitSymbol = (UnitSymbol)Enum.Parse(typeof(UnitSymbol), analogInput.Units, true), IsInit = true, Scale = analogInput.ScaleFactor, Offset = analogInput.ScaleOffset }; dbContext.AddProcessVariable(analog); } } }
public bool DeserializeScadaModel(string deserializationSource = "ScadaModel.xml") { // to do Database.IsConfigurationFinished = false; string message = string.Empty; string configurationName = deserializationSource; string source = Path.Combine(basePath, configurationName); if (Database.Instance.RTUs.Count != 0) { Database.Instance.RTUs.Clear(); } if (Database.Instance.ProcessVariablesName.Count != 0) { Database.Instance.ProcessVariablesName.Clear(); } try { XElement xdocument = XElement.Load(source); // access RTUS, DIGITALS, ANALOGS, COUNTERS from ScadaModel root IEnumerable <XElement> elements = xdocument.Elements(); var rtus = xdocument.Element("RTUS").Elements("RTU").ToList(); var digitals = (from dig in xdocument.Element("Digitals").Elements("Digital") orderby(int) dig.Element("RelativeAddress") select dig).ToList(); var analogs = (from dig in xdocument.Element("Analogs").Elements("Analog") orderby(int) dig.Element("RelativeAddress") select dig).ToList(); var counters = (from dig in xdocument.Element("Counters").Elements("Counter") orderby(int) dig.Element("RelativeAddress") select dig).ToList(); // parsing RTUS if (rtus.Count != 0) { foreach (var rtu in rtus) { RTU newRtu; string uniqueName = (string)rtu.Element("Name"); // if RTU with that name does not already exist? if (!dbContext.Database.RTUs.ContainsKey(uniqueName)) { byte address = (byte)(int)rtu.Element("Address"); bool freeSpaceForDigitals = (bool)rtu.Element("FreeSpaceForDigitals"); bool freeSpaceForAnalogs = (bool)rtu.Element("FreeSpaceForAnalogs"); string stringProtocol = (string)rtu.Element("Protocol"); IndustryProtocols protocol = (IndustryProtocols)Enum.Parse(typeof(IndustryProtocols), stringProtocol); int digOutStartAddr = (int)rtu.Element("DigOutStartAddr"); int digInStartAddr = (int)rtu.Element("DigInStartAddr"); int anaInStartAddr = (int)rtu.Element("AnaInStartAddr"); int anaOutStartAddr = (int)rtu.Element("AnaOutStartAddr"); int counterStartAddr = (int)rtu.Element("CounterStartAddr"); int digOutCount = (int)rtu.Element("NoDigOut"); int digInCount = (int)rtu.Element("NoDigIn"); int anaInCount = (int)rtu.Element("NoAnaIn"); int anaOutCount = (int)rtu.Element("NoAnaOut"); int counterCount = (int)rtu.Element("NoCnt"); ushort anaInRawMin = (ushort)(int)rtu.Element("AnaInRawMin"); ushort anaInRawMax = (ushort)(int)rtu.Element("AnaInRawMax"); ushort anaOutRawMin = (ushort)(int)rtu.Element("AnaOutRawMin"); ushort anaOutRawMax = (ushort)(int)rtu.Element("AnaOutRawMax"); if (digOutCount != digInCount) { message = string.Format("Invalid config: RTU - {0}: Value of DigOutCount must be the same as Value of DigInCount", uniqueName); Console.WriteLine(message); return(false); } newRtu = new RTU() { Name = uniqueName, Address = address, FreeSpaceForDigitals = freeSpaceForDigitals, FreeSpaceForAnalogs = freeSpaceForAnalogs, Protocol = protocol, DigOutStartAddr = digOutStartAddr, DigInStartAddr = digInStartAddr, AnaInStartAddr = anaInStartAddr, AnaOutStartAddr = anaOutStartAddr, CounterStartAddr = counterStartAddr, NoDigOut = digOutCount, NoDigIn = digInCount, NoAnaIn = anaInCount, NoAnaOut = anaOutCount, NoCnt = counterCount, AnaInRawMin = anaInRawMin, AnaInRawMax = anaInRawMax, AnaOutRawMin = anaOutRawMin, AnaOutRawMax = anaOutRawMax }; dbContext.AddRTU(newRtu); } else { // to do: bacati exception mozda message = string.Format("Invalid config: There is multiple RTUs with Name={0}!", uniqueName); Console.WriteLine(message); return(false); } } } else { message = string.Format("Invalid config: file must contain at least 1 RTU!"); Console.WriteLine(message); return(false); } //parsing DIGITALS. ORDER OF RELATIVE ADDRESSES IS IMPORTANT if (digitals.Count != 0) { foreach (var d in digitals) { string procContr = (string)d.Element("ProcContrName"); // does RTU exists? RTU associatedRtu; if ((associatedRtu = dbContext.GetRTUByName(procContr)) != null) { Digital newDigital = new Digital(); // SETTING ProcContrName newDigital.ProcContrName = procContr; string uniqueName = (string)d.Element("Name"); // variable with that name does not exists in db? if (!dbContext.Database.ProcessVariablesName.ContainsKey(uniqueName)) { // SETTING Name newDigital.Name = uniqueName; // SETTING State string stringCurrentState = (string)d.Element("State"); States stateValue = (States)Enum.Parse(typeof(States), stringCurrentState); newDigital.State = stateValue; // SETTING Command parameter - for initializing Simulator with last command string lastCommandString = (string)d.Element("Command"); CommandTypes command = (CommandTypes)Enum.Parse(typeof(CommandTypes), lastCommandString); // SETTING Class string digDevClass = (string)d.Element("Class"); DigitalDeviceClasses devClass = (DigitalDeviceClasses)Enum.Parse(typeof(DigitalDeviceClasses), digDevClass); newDigital.Class = devClass; // SETTING RelativeAddress ushort relativeAddress = (ushort)(int)d.Element("RelativeAddress"); newDigital.RelativeAddress = relativeAddress; var hasCommands = d.Element("ValidCommands"); if (hasCommands.HasElements) { var validCommands = hasCommands.Elements("Command").ToList(); // SETTING ValidCommands foreach (var xElementCommand in validCommands) { string stringCommand = (string)xElementCommand; CommandTypes validCommand = (CommandTypes)Enum.Parse(typeof(CommandTypes), stringCommand); newDigital.ValidCommands.Add(validCommand); } } else { message = string.Format("Invalid config: Variable = {0} does not contain commands.", uniqueName); Console.WriteLine(message); return(false); } var hasStates = d.Element("ValidStates"); if (hasStates.HasElements) { var validStates = hasStates.Elements("State").ToList(); // SETTING ValidStates foreach (var xElementState in validStates) { string stringState = (string)xElementState; States state = (States)Enum.Parse(typeof(States), stringState); newDigital.ValidStates.Add(state); } } else { message = string.Format("Invalid config: Variable = {0} does not contain states.", uniqueName); Console.WriteLine(message); return(false); } ushort calculatedRelativeAddres; if (associatedRtu.TryMap(newDigital, out calculatedRelativeAddres)) { if (relativeAddress == calculatedRelativeAddres) { if (associatedRtu.MapProcessVariable(newDigital)) { dbContext.AddProcessVariable(newDigital); } } else { message = string.Format("Invalid config: Variable = {0} RelativeAddress = {1} is not valid.", uniqueName, relativeAddress); Console.WriteLine(message); return(false); } } } else { message = string.Format("Invalid config: Name = {0} is not unique. Variable already exists", uniqueName); Console.WriteLine(message); return(false); } } else { message = string.Format("Invalid config: Parsing Digitals, ProcContrName = {0} does not exists.", procContr); Console.WriteLine(message); return(false); } } } // parsing ANALOGS. ORDER OF RELATIVE ADDRESSES IS IMPORTANT if (analogs.Count != 0) { foreach (var a in analogs) { string procContr = (string)a.Element("ProcContrName"); // does RTU exists? RTU associatedRtu; if ((associatedRtu = dbContext.GetRTUByName(procContr)) != null) { Analog newAnalog = new Analog(); // SETTING ProcContrName newAnalog.ProcContrName = procContr; string uniqueName = (string)a.Element("Name"); // variable with that name does not exists in db? if (!dbContext.Database.ProcessVariablesName.ContainsKey(uniqueName)) { // SETTING Name newAnalog.Name = uniqueName; // SETTING NumOfRegisters ushort numOfReg = (ushort)(int)a.Element("NumOfRegisters"); newAnalog.NumOfRegisters = numOfReg; // SETTING AcqValue ushort acqValue = (ushort)(float)a.Element("AcqValue"); newAnalog.AcqValue = acqValue; // SETTING CommValue ushort commValue = (ushort)(float)a.Element("CommValue"); newAnalog.CommValue = commValue; // SETTING MinValue float minValue = (float)a.Element("MinValue"); newAnalog.MinValue = minValue; // SETTING MaxValue float maxValue = (float)a.Element("MaxValue"); newAnalog.MaxValue = maxValue; // SETTING UnitSymbol string stringUnitSymbol = (string)a.Element("UnitSymbol"); UnitSymbol unitSymbolValue = (UnitSymbol)Enum.Parse(typeof(UnitSymbol), stringUnitSymbol, true); newAnalog.UnitSymbol = unitSymbolValue; // SETTING RelativeAddress ushort relativeAddress = (ushort)(int)a.Element("RelativeAddress"); newAnalog.RelativeAddress = relativeAddress; // svejedno je uzeli AnaInRawMin ili AnaOutRawMin -> isti su trenutni, // sve dok imamo samo Analog.cs a ne AnaIn.cs + AnaOut.cs (dok je kao za digital) newAnalog.RawBandLow = associatedRtu.AnaInRawMin; newAnalog.RawBandHigh = associatedRtu.AnaInRawMax; // SETTING RawAcqValue and RawCommValue AnalogProcessor.EGUToRawValue(newAnalog); ushort calculatedRelativeAddres; if (associatedRtu.TryMap(newAnalog, out calculatedRelativeAddres)) { if (relativeAddress == calculatedRelativeAddres) { if (associatedRtu.MapProcessVariable(newAnalog)) { dbContext.AddProcessVariable(newAnalog); } } else { message = string.Format("Invalid config: Analog Variable = {0} RelativeAddress = {1} is not valid.", uniqueName, relativeAddress); Console.WriteLine(message); return(false); } } } else { message = string.Format("Invalid config: Name = {0} is not unique. Analog Variable already exists", uniqueName); Console.WriteLine(message); return(false); } } else { message = string.Format("Invalid config: Parsing Analogs, ProcContrName = {0} does not exists.", procContr); Console.WriteLine(message); return(false); } } } // to do: if (counters.Count != 0) { } Console.WriteLine("Configuration passed successfully."); } catch (FileNotFoundException e) { Console.WriteLine(e.Message); return(false); } catch (XmlException e) { Console.WriteLine(e.Message); return(false); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return(false); } Database.IsConfigurationFinished = true; return(true); }
public bool DeserializeScadaModel(string deserializationSource = "ScadaModel.xml") { // to do Database.IsConfigurationFinished = false; string message = string.Empty; string configurationName = deserializationSource; // string source = Path.Combine(basePath, configurationName); if (Database.Instance.RTUs.Count != 0) { Database.Instance.RTUs.Clear(); } if (Database.Instance.ProcessVariablesName.Count != 0) { Database.Instance.ProcessVariablesName.Clear(); } try { // XElement xdocument = XElement.Load(source); // access RTUS, DIGITALS, ANALOGS, COUNTERS from ScadaModel root // IEnumerable<XElement> elements = xdocument.Elements(); //var rtus = xdocument.Element("RTUS").Elements("RTU").ToList(); //var digitals = (from dig in xdocument.Element("Digitals").Elements("Digital") // orderby (int)dig.Element("RelativeAddress") // select dig).ToList(); //var analogs = (from dig in xdocument.Element("Analogs").Elements("Analog") // orderby (int)dig.Element("RelativeAddress") // select dig).ToList(); //var counters = (from dig in xdocument.Element("Counters").Elements("Counter") // orderby (int)dig.Element("RelativeAddress") // select dig).ToList(); List <ScadaDBClassLib.ModelData.RTU> rtus = new List <ScadaDBClassLib.ModelData.RTU>(); List <ScadaDBClassLib.ModelData.Digital> digitals = new List <ScadaDBClassLib.ModelData.Digital>(); List <ScadaDBClassLib.ModelData.Analog> analogs = new List <ScadaDBClassLib.ModelData.Analog>(); using (ScadaDBClassLib.ScadaCtxcs ctx = new ScadaDBClassLib.ScadaCtxcs()) { rtus = ctx.RTUs.ToList(); digitals = ctx.Digitals.ToList(); analogs = ctx.Analogs.ToList(); } // parsing RTUS if (rtus.Count != 0) { foreach (var rtu in rtus) { RTU newRtu; string uniqueName = (string)rtu.Name; // if RTU with that name does not already exist? if (!dbContext.Database.RTUs.ContainsKey(uniqueName)) { byte address = (byte)(int)rtu.Address; bool freeSpaceForDigitals = (bool)rtu.FreeSpaceForDigitals; bool freeSpaceForAnalogs = (bool)rtu.FreeSpaceForAnalogs; IndustryProtocols protocol = IndustryProtocols.ModbusTCP; int digOutStartAddr = (int)rtu.DigOutStartAddr; int digInStartAddr = (int)rtu.DigInStartAddr; int anaInStartAddr = (int)rtu.AnaInStartAddr; int anaOutStartAddr = (int)rtu.AnaOutStartAddr; int counterStartAddr = (int)rtu.CounterStartAddr; int digOutCount = (int)rtu.NoDigOut; int digInCount = (int)rtu.NoDigIn; int anaInCount = (int)rtu.NoAnaIn; int anaOutCount = (int)rtu.NoAnaOut; int counterCount = (int)rtu.NoCnt; ushort anaInRawMin = (ushort)(int)rtu.AnaInRawMin; ushort anaInRawMax = (ushort)(int)rtu.AnaInRawMax; ushort anaOutRawMin = (ushort)(int)rtu.AnaOutRawMin; ushort anaOutRawMax = (ushort)(int)rtu.AnaOutRawMax; if (digOutCount != digInCount) { message = string.Format("Invalid config: RTU - {0}: Value of DigOutCount must be the same as Value of DigInCount", uniqueName); Console.WriteLine(message); return(false); } newRtu = new RTU() { Name = uniqueName, Address = address, FreeSpaceForDigitals = freeSpaceForDigitals, FreeSpaceForAnalogs = freeSpaceForAnalogs, Protocol = protocol, DigOutStartAddr = digOutStartAddr, DigInStartAddr = digInStartAddr, AnaInStartAddr = anaInStartAddr, AnaOutStartAddr = anaOutStartAddr, CounterStartAddr = counterStartAddr, NoDigOut = digOutCount, NoDigIn = digInCount, NoAnaIn = anaInCount, NoAnaOut = anaOutCount, NoCnt = counterCount, AnaInRawMin = anaInRawMin, AnaInRawMax = anaInRawMax, AnaOutRawMin = anaOutRawMin, AnaOutRawMax = anaOutRawMax }; //using (ScadaContextDB ctx = new ScadaContextDB()) //{ // ctx.RTUs.Add(new ScadaCloud.Model.RTU // { // Name = uniqueName, // Address = address, // FreeSpaceForDigitals = freeSpaceForDigitals, // FreeSpaceForAnalogs = freeSpaceForAnalogs, // Protocol = protocol, // DigOutStartAddr = digOutStartAddr, // DigInStartAddr = digInStartAddr, // AnaInStartAddr = anaInStartAddr, // AnaOutStartAddr = anaOutStartAddr, // CounterStartAddr = counterStartAddr, // NoDigOut = digOutCount, // NoDigIn = digInCount, // NoAnaIn = anaInCount, // NoAnaOut = anaOutCount, // NoCnt = counterCount, // AnaInRawMin = anaInRawMin, // AnaInRawMax = anaInRawMax, // AnaOutRawMin = anaOutRawMin, // AnaOutRawMax = anaOutRawMax // }); // ctx.SaveChanges(); //} dbContext.AddRTU(newRtu); } else { // to do: bacati exception mozda message = string.Format("Invalid config: There is multiple RTUs with Name={0}!", uniqueName); Console.WriteLine(message); return(false); } } } else { message = string.Format("Invalid config: file must contain at least 1 RTU!"); Console.WriteLine(message); return(false); } //parsing DIGITALS. ORDER OF RELATIVE ADDRESSES IS IMPORTANT if (digitals.Count != 0) { foreach (var d in digitals) { string procContr = (string)d.ProcContrName; // does RTU exists? RTU associatedRtu; if ((associatedRtu = dbContext.GetRTUByName(procContr)) != null) { Digital newDigital = new Digital(); // SETTING ProcContrName newDigital.ProcContrName = procContr; string uniqueName = (string)d.Name; // variable with that name does not exists in db? if (!dbContext.Database.ProcessVariablesName.ContainsKey(uniqueName)) { // SETTING Name newDigital.Name = uniqueName; // SETTING State string stringCurrentState = (string)d.State; States stateValue = (States)Enum.Parse(typeof(States), stringCurrentState); newDigital.State = stateValue; // SETTING Command parameter - for initializing Simulator with last command string lastCommandString = (string)d.Command; CommandTypes command = (CommandTypes)Enum.Parse(typeof(CommandTypes), lastCommandString); // SETTING Class DigitalDeviceClasses devClass = DigitalDeviceClasses.SWITCH; newDigital.Class = devClass; // SETTING RelativeAddress ushort relativeAddress = (ushort)(int)d.RelativeAddress; newDigital.RelativeAddress = relativeAddress; // SETTING ValidCommands newDigital.ValidCommands.Add(CommandTypes.OPEN); newDigital.ValidCommands.Add(CommandTypes.CLOSE); newDigital.ValidStates.Add(States.CLOSED); newDigital.ValidStates.Add(States.OPENED); //using (ScadaContextDB ctx = new ScadaContextDB()) //{ // ctx.Digirals.Add(new ScadaCloud.Model.Digital // { // Name = uniqueName, // RelativeAddress = relativeAddress, // ProcContrName = procContr, // State = stringCurrentState, // Command = lastCommandString // }); // ctx.SaveChanges(); //} ushort calculatedRelativeAddres; if (associatedRtu.TryMap(newDigital, out calculatedRelativeAddres)) { if (relativeAddress == calculatedRelativeAddres) { if (associatedRtu.MapProcessVariable(newDigital)) { dbContext.AddProcessVariable(newDigital); } } else { message = string.Format("Invalid config: Variable = {0} RelativeAddress = {1} is not valid.", uniqueName, relativeAddress); Console.WriteLine(message); continue; } } } else { message = string.Format("Invalid config: Name = {0} is not unique. Variable already exists", uniqueName); Console.WriteLine(message); continue; } } else { message = string.Format("Invalid config: Parsing Digitals, ProcContrName = {0} does not exists.", procContr); Console.WriteLine(message); return(false); } } } // parsing ANALOGS. ORDER OF RELATIVE ADDRESSES IS IMPORTANT if (analogs.Count != 0) { foreach (var a in analogs) { string procContr = (string)a.ProcContrName; // does RTU exists? RTU associatedRtu; if ((associatedRtu = dbContext.GetRTUByName(procContr)) != null) { Analog newAnalog = new Analog(); // SETTING ProcContrName newAnalog.ProcContrName = procContr; string uniqueName = (string)a.Name; // variable with that name does not exists in db? if (!dbContext.Database.ProcessVariablesName.ContainsKey(uniqueName)) { // SETTING Name newAnalog.Name = uniqueName; // SETTING NumOfRegisters ushort numOfReg = (ushort)(int)a.NumOfRegisters; newAnalog.NumOfRegisters = numOfReg; // SETTING AcqValue ushort acqValue = (ushort)(float)a.AcqValue; newAnalog.AcqValue = acqValue; // SETTING CommValue ushort commValue = (ushort)(float)a.CommValue; newAnalog.CommValue = commValue; // SETTING MinValue float minValue = (float)a.MinValue; newAnalog.MinValue = minValue; // SETTING MaxValue float maxValue = (float)a.MaxValue; newAnalog.MaxValue = maxValue; // SETTING UnitSymbol string stringUnitSymbol = (string)a.UnitSymbol; UnitSymbol unitSymbolValue = (UnitSymbol)Enum.Parse(typeof(UnitSymbol), stringUnitSymbol, true); newAnalog.UnitSymbol = unitSymbolValue; // SETTING RelativeAddress ushort relativeAddress = (ushort)(int)a.RelativeAddress; newAnalog.RelativeAddress = relativeAddress; // svejedno je uzeli AnaInRawMin ili AnaOutRawMin -> isti su trenutni, // sve dok imamo samo Analog.cs a ne AnaIn.cs + AnaOut.cs (dok je kao za digital) newAnalog.RawBandLow = associatedRtu.AnaInRawMin; newAnalog.RawBandHigh = associatedRtu.AnaInRawMax; //using (ScadaContextDB ctx = new ScadaContextDB()) //{ // ctx.Analogs.Add(new ScadaCloud.Model.Analog // { // Name = uniqueName, // NumOfRegisters = numOfReg, // AcqValue = acqValue, // CommValue = commValue, // MaxValue = maxValue, // MinValue = minValue, // ProcContrName = procContr, // RelativeAddress = relativeAddress, // UnitSymbol = stringUnitSymbol // }); // ctx.SaveChanges(); //} // SETTING RawAcqValue and RawCommValue AnalogProcessor.EGUToRawValue(newAnalog); ushort calculatedRelativeAddres; if (associatedRtu.TryMap(newAnalog, out calculatedRelativeAddres)) { if (relativeAddress == calculatedRelativeAddres) { if (associatedRtu.MapProcessVariable(newAnalog)) { dbContext.AddProcessVariable(newAnalog); } } else { message = string.Format("Invalid config: Analog Variable = {0} RelativeAddress = {1} is not valid.", uniqueName, relativeAddress); Console.WriteLine(message); return(false); } } } else { message = string.Format("Invalid config: Name = {0} is not unique. Analog Variable already exists", uniqueName); Console.WriteLine(message); return(false); } } else { message = string.Format("Invalid config: Parsing Analogs, ProcContrName = {0} does not exists.", procContr); Console.WriteLine(message); return(false); } } } // to do: //if (counters.Count != 0) //{ //} Console.WriteLine("Configuration passed successfully."); } catch (FileNotFoundException e) { Console.WriteLine(e.Message); return(false); } catch (XmlException e) { Console.WriteLine(e.Message); return(false); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return(false); } Database.IsConfigurationFinished = true; return(true); }