private void Commanding_DoWork() { while (true) { try { Thread.Sleep(1000); foreach (var item in calculationEngineCommands) { if (item.MillisecondsPassedSinceLastPoll >= item.Milliseconds) { processingManager.ExecuteWriteCommand(item.RegisterType, item.Index, item.Value); item.Remove = true; } item.MillisecondsPassedSinceLastPoll += 1000; } calculationEngineCommands = new ConcurrentBag <CeCommand>(calculationEngineCommands.Where(x => x.Remove == false).ToList()); } catch (Exception e) { ScadaProxyFactory.Instance().LoggingProxy().Log(new SCADA.Common.Logging.LogEventModel() { EventType = SCADA.Common.Logging.LogEventType.ERROR, Message = $"{e.Message} - {e.StackTrace}" }); } } }
private static void OnUpdateEvent(object sender, EventArgs e) { ScadaStorageProxy proxy = ScadaProxyFactory.Instance().ScadaStorageProxy(); ScadaUpdateEvent ev = new ScadaUpdateEvent() { Points = new List <ScadaPointDto>() }; var all = proxy.GetModel().Values.ToList(); var analogs = all.Where(x => x.RegisterType == RegisterType.ANALOG_INPUT || x.RegisterType == RegisterType.ANALOG_OUTPUT).Cast <AnalogPoint>().ToList(); var binaries = all.Where(x => x.RegisterType == RegisterType.BINARY_INPUT || x.RegisterType == RegisterType.BINARY_OUTPUT).Cast <DiscretePoint>().ToList(); ev.Points.AddRange(Mapper.MapCollection <AnalogPoint, ScadaPointDto>(analogs)); ev.Points.AddRange(Mapper.MapCollection <DiscretePoint, ScadaPointDto>(binaries)); DomUpdateEvent dom = new DomUpdateEvent() { DomData = proxy.GetDomModel() }; try { instace.Publish(ev).ConfigureAwait(false).GetAwaiter().GetResult(); instace.Publish(dom).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { } }
public void Rollback() { ScadaStorageProxy proxy = ScadaProxyFactory.Instance().ScadaStorageProxy(); Console.WriteLine("Request for rollback!"); proxy.SetTransactionModel(null); }
public ProcessingManager(IFunctionExecutor functionExecutor) { this.functionExecutor = functionExecutor; applicationSequence = 0; transportSequence = 0; dom = ScadaProxyFactory.Instance().DOMProxy(); log = ScadaProxyFactory.Instance().LoggingProxy(); storage = ScadaProxyFactory.Instance().ScadaStorageProxy(); }
public bool ModelUpdate(Dictionary <DMSType, Container> model) { Console.WriteLine("New update request!"); ScadaStorageProxy proxy = ScadaProxyFactory.Instance().ScadaStorageProxy(); proxy.SetCimModel(model); //dobio si model, javi se da ucestvujes u transakciji EnList(); return(true); }
public void Update(Dictionary <string, ushort> pairs) { var storage = ScadaProxyFactory.Instance().ScadaStorageProxy(); var model = storage.GetModel(); ushort aiCount = (ushort)(model.Values.Where(x => x.RegisterType == SCADA.Common.DataModel.RegisterType.ANALOG_INPUT).Count()); ushort aoCount = (ushort)(model.Values.Where(x => x.RegisterType == SCADA.Common.DataModel.RegisterType.ANALOG_OUTPUT).Count()); ushort biCount = (ushort)(model.Values.Where(x => x.RegisterType == SCADA.Common.DataModel.RegisterType.BINARY_INPUT).Count()); ushort boCount = (ushort)(model.Values.Where(x => x.RegisterType == SCADA.Common.DataModel.RegisterType.BINARY_OUTPUT).Count()); proxy.UpdateConfig(Tuple.Create <ushort, ushort, ushort, ushort>(biCount, boCount, aiCount, aoCount), pairs); }
public Dictionary <string, BasePoint> GetData() { ScadaStorageProxy proxy = ScadaProxyFactory.Instance().ScadaStorageProxy(); Dictionary <string, BasePoint> points = new Dictionary <string, BasePoint>(); var model = proxy.GetModel(); foreach (var item in model.Values) { points.Add(item.Mrid, item); } return(points); }
/// <summary> /// Initializes a new instance of the <see cref="Acquisitor"/> class. /// </summary> /// <param name="acquisitionTrigger">The acquisition trigger.</param> /// <param name="processingManager">The processing manager.</param> /// <param name="stateUpdater">The state updater.</param> public Acquisitor(IProcessingManager processingManager) { this.processingManager = processingManager; log = ScadaProxyFactory.Instance().LoggingProxy(); if (!Int32.TryParse(ConfigurationManager.AppSettings["AcquisitionInterval"], out acquisitionInterval)) { acquisitionInterval = 1000; } seconds = 0; this.InitializeAcquisitionThread(); this.StartAcquisitionThread(); }
public bool Prepare() { ScadaStorageProxy proxy = ScadaProxyFactory.Instance().ScadaStorageProxy(); Console.WriteLine("Prepared? YES"); var converter = new ScadaModelConverter(); result = converter.Convert(proxy.GetCimModel()); proxy.SetTransactionModel(result.Points); proxy.SetDomModel(result.Equipment.Values.ToList()); return(true); }
public bool Commit() { ScadaStorageProxy proxy = ScadaProxyFactory.Instance().ScadaStorageProxy(); Console.WriteLine("Commited? YES"); proxy.SetModel(proxy.GetTransactionModel()); SCADAServer.updateEvent?.Invoke(this, null); ConfigurationChangeInvoker invoker = new ConfigurationChangeInvoker(); ScadaProxyFactory.Instance().DOMProxy().Add(proxy.GetModel().Values.ToList().ToDbModel()); invoker.Update(result.MridIndexPairs); return(true); }
private void HandleReceivedBytes(byte[] message, bool unsolicited) { Dictionary <Tuple <RegisterType, int>, BasePoint> pointsToUpdate; if (!unsolicited) { pointsToUpdate = currentCommand?.PareseResponse(message); } else { pointsToUpdate = unsolicitedCommand.PareseResponse(message); } if (pointsToUpdate != null) { var processedPoints = ScadaProxyFactory.Instance().AlarmKruncingProxy().Check(pointsToUpdate); ScadaProxyFactory.Instance().ScadaStorageProxy().UpdateModelValue(processedPoints); } }
private void DoWork() { while (executionFlag) { DomUpdateEvent dom = new DomUpdateEvent() { DomData = ScadaProxyFactory.Instance().DOMProxy().GetAll().ToSwitchingEquipment() }; HistoryUpdateEvent history = new HistoryUpdateEvent() { History = ScadaProxyFactory.Instance().HistoryProxy().GetAll() }; HistoryGraphicalEvent graph = new HistoryGraphicalEvent() { Graph = ScadaProxyFactory.Instance().HistoryProxy().GetGraph() }; ScadaUpdateEvent ev = new ScadaUpdateEvent() { Points = new List <SCADA.Common.DataModel.ScadaPointDto>() }; var all = ScadaProxyFactory.Instance().ScadaStorageProxy().GetModel().Values.ToList(); var analogs = all.Where(x => x.RegisterType == RegisterType.ANALOG_INPUT || x.RegisterType == RegisterType.ANALOG_OUTPUT).Cast <AnalogPoint>().ToList(); var binaries = all.Where(x => x.RegisterType == RegisterType.BINARY_INPUT || x.RegisterType == RegisterType.BINARY_OUTPUT).Cast <DiscretePoint>().ToList(); ev.Points.AddRange(Mapper.MapCollection <AnalogPoint, ScadaPointDto>(analogs)); ev.Points.AddRange(Mapper.MapCollection <DiscretePoint, ScadaPointDto>(binaries)); if (ev.Points.Count > 0) { endpoint.Publish(ev).ConfigureAwait(false); } if (dom.DomData.Count > 0) { endpoint.Publish(dom).ConfigureAwait(false); } if (history.History.Count > 0) { endpoint.Publish(history).ConfigureAwait(false); } endpoint.Publish(graph).ConfigureAwait(false); Thread.Sleep(GetConfigTime()); } }
private static async Task AsyncMain() { Console.Title = "NDS"; LoggingHost logHost = new LoggingHost(); logHost.Open(); var logger = ScadaProxyFactory.Instance().LoggingProxy(); log.Info("SCADA started working.."); logger.Log(new SCADA.Common.Logging.LogEventModel() { EventType = SCADA.Common.Logging.LogEventType.INFO, Message = "SCADA NDS Started!" }); endpoint = await ServiceBusStartup.StartInstance() .ConfigureAwait(false); SCADAServer.instace = endpoint; ScadaStorageService storage = new ScadaStorageService(); storage.Open(); SCADAServer scada = new SCADAServer(); scada.OpenTransaction(); scada.OpenModel(); DOMHost dom = new DOMHost(); dom.Open(); HistoryHost historyHost = new HistoryHost(); historyHost.Open(); AlarmingKruncingHost ak = new AlarmingKruncingHost(); ak.Open(); ScadaExportService scadaExportService = new ScadaExportService(); scadaExportService.Open(); Console.WriteLine("Services are working.."); logger.Log(new SCADA.Common.Logging.LogEventModel() { EventType = SCADA.Common.Logging.LogEventType.INFO, Message = "SCADA Services Started!" }); GuiDBUpdater updater = new GuiDBUpdater(endpoint); updater.Start(); IFEP fep = new FEP(); Console.WriteLine("FEP Started!"); logger.Log(new SCADA.Common.Logging.LogEventModel() { EventType = SCADA.Common.Logging.LogEventType.INFO, Message = "SCADA FEP Started!" }); Console.ReadLine(); storage.Close(); ak.Close(); dom.Close(); logHost.Close(); historyHost.Close(); scadaExportService.Close(); updater.Stop(); }
private void ConnectionProcessorThread() { while (threadCancellationSignal) { try { if (connectionState == ConnectionState.DISCONNECTED) { numberOfConnectionRetries = 0; connection.Connect(); while (numberOfConnectionRetries < 10) { if (connection.CheckState()) { this.connectionState = ConnectionState.CONNECTED; numberOfConnectionRetries = 0; break; } else { numberOfConnectionRetries++; if (numberOfConnectionRetries == 10) { connection.Disconect(); connectionState = ConnectionState.DISCONNECTED; } } } } else { processConnection.WaitOne(); while (commandQueue.TryDequeue(out currentCommand)) { lock (lockObj) { connection.Send(currentCommand.PackRequest()); byte[] message; byte[] header = connection.Recv(10); int recvLen = CalculateRecvLength(header[2]); byte[] dataChunks = connection.Recv(recvLen); message = new byte[header.Length + recvLen]; Buffer.BlockCopy(header, 0, message, 0, 10); Buffer.BlockCopy(dataChunks, 0, message, 10, recvLen); bool unsolicited = CheckIfUnsolicited(message[11]); if (unsolicited) { HandleReceivedBytes(message, unsolicited); DNP3ConfirmCommandParamters dnp3Param = new DNP3ConfirmCommandParamters(0xc0, (byte)DNP3FunctionCode.CONFIRM, 0xc0); //podesiti parametre IDNP3Function function = DNP3FunctionFactory.CreateConfirmFunction(dnp3Param); connection.Send(function.PackRequest()); } else { HandleReceivedBytes(message, unsolicited); } currentCommand = null; } } } } catch (SocketException se) { currentCommand = null; connectionState = ConnectionState.DISCONNECTED; ScadaProxyFactory.Instance().LoggingProxy().Log(new Logging.LogEventModel() { EventType = Logging.LogEventType.ERROR, Message = $"{se.Message} - {se.StackTrace}" }); } catch (Exception ex) { currentCommand = null; ScadaProxyFactory.Instance().LoggingProxy().Log(new Logging.LogEventModel() { EventType = Logging.LogEventType.ERROR, Message = $"{ex.Message} - {ex.StackTrace}" }); } } }
public bool ModelUpdate(AffectedEntities model) { Console.WriteLine("New update request!"); ScadaStorageProxy proxy = ScadaProxyFactory.Instance().ScadaStorageProxy(); NetworkModelGDAProxy nms = new NetworkModelGDAProxy("NetworkModelGDAEndpoint"); var cimModel = proxy.GetCimModel(); if (cimModel == null) { cimModel = new Dictionary <DMSType, Container>(); } model.Insert = model.Insert.Where(x => this.GetDMSType(x) == DMSType.ANALOG || this.GetDMSType(x) == DMSType.DISCRETE || this.GetDMSType(x) == DMSType.BREAKER || this.GetDMSType(x) == DMSType.DISCONNECTOR).ToList(); model.Update = model.Update.Where(x => this.GetDMSType(x) == DMSType.ANALOG || this.GetDMSType(x) == DMSType.DISCRETE || this.GetDMSType(x) == DMSType.BREAKER || this.GetDMSType(x) == DMSType.DISCONNECTOR).ToList(); model.Delete = model.Delete.Where(x => this.GetDMSType(x) == DMSType.ANALOG || this.GetDMSType(x) == DMSType.DISCRETE || this.GetDMSType(x) == DMSType.BREAKER || this.GetDMSType(x) == DMSType.DISCONNECTOR).ToList(); if (model.Insert.Count > 0) { var dataInsert = nms.GetValues(model.Insert); foreach (var item in dataInsert) { var dmsType = GetDMSType(item.GID); if (!cimModel.ContainsKey(dmsType)) { cimModel.Add(dmsType, new Container()); } cimModel[dmsType].AddEntity(item); } } if (model.Update.Count > 0) { var dataUpdate = nms.GetValues(model.Update); foreach (var item in dataUpdate) { var dmsType = GetDMSType(item.GID); if (!cimModel.ContainsKey(dmsType)) { cimModel.Add(dmsType, new Container()); } cimModel[dmsType].RemoveEntity(item.GID); cimModel[dmsType].AddEntity(item); } } if (model.Delete.Count > 0) { var dataDelete = nms.GetValues(model.Delete); foreach (var item in dataDelete) { var dmsType = GetDMSType(item.GID); if (!cimModel.ContainsKey(dmsType)) { cimModel.Add(dmsType, new Container()); } cimModel[dmsType].RemoveEntity(item.GID); } } proxy.SetCimModel(cimModel); //dobio si model, javi se da ucestvujes u transakciji EnList(); return(true); }