Exemplo n.º 1
0
        private async Task SetScadaModel(string name, Dictionary <Tuple <RegisterType, int>, BasePoint> dictionary)
        {
            historian = new HistoryServiceProxy(ConfigurationReader.ReadValue(_context, "Settings", "History"));
            IReliableDictionary <int, BasePoint> result = null;

            using (var tx = _stateManager.CreateTransaction())
            {
                result = await _stateManager.GetOrAddAsync <IReliableDictionary <int, BasePoint> >(tx, name, TimeSpan.FromSeconds(60));

                if (dictionary == null)
                {
                    await result.ClearAsync();

                    await tx.CommitAsync();

                    return;
                }
                await tx.CommitAsync();
            }
            using (var tx = _stateManager.CreateTransaction())
            {
                var historyData = new List <HistoryDbModel>();
                foreach (var item in dictionary)
                {
                    var point = await result.TryGetValueAsync(tx, MakeKey(item.Key.Item1, item.Key.Item2), TimeSpan.FromSeconds(60), CancellationToken.None);

                    if (point.HasValue)
                    {
                        switch (item.Key.Item1)
                        {
                        case RegisterType.BINARY_INPUT:
                        case RegisterType.BINARY_OUTPUT:
                        {
                            ((DiscretePoint)point.Value).TimeStamp = (item.Value as DiscretePoint).TimeStamp;
                            if (((DiscretePoint)point.Value).Value != (item.Value as DiscretePoint).Value)
                            {
                                ((DiscretePoint)point.Value).Value = (item.Value as DiscretePoint).Value;
                                ((DiscretePoint)point.Value).Alarm = (item.Value as DiscretePoint).Alarm;
                                historyData.Add((((DiscretePoint)point.Value)).ToHistoryDbModel());
                            }
                            break;
                        }

                        case RegisterType.ANALOG_INPUT:
                        case RegisterType.ANALOG_OUTPUT:
                        {
                            ((AnalogPoint)point.Value).TimeStamp = (item.Value as AnalogPoint).TimeStamp;
                            if (((AnalogPoint)point.Value).Value != (item.Value as AnalogPoint).Value)
                            {
                                ((AnalogPoint)point.Value).Value = (item.Value as AnalogPoint).Value;
                                ((AnalogPoint)point.Value).Alarm = (item.Value as AnalogPoint).Alarm;
                                historyData.Add((((AnalogPoint)point.Value)).ToHistoryDbModel());
                            }

                            break;
                        }
                        }
                        await result.SetAsync(tx, MakeKey(item.Key.Item1, item.Key.Item2), point.Value, TimeSpan.FromSeconds(60), CancellationToken.None);
                    }
                    else
                    {
                        await result.SetAsync(tx, MakeKey(item.Key.Item1, item.Key.Item2), item.Value, TimeSpan.FromSeconds(60), CancellationToken.None);
                    }
                }
                await tx.CommitAsync();

                if (historyData.Count > 0)
                {
                    await historian.AddRange(historyData);
                }
            }
        }
Exemplo n.º 2
0
        private void Update()
        {
            var            domService     = new DomServiceProxy(ConfigurationReader.ReadValue(Context, "Settings", "Dom"));
            var            historyService = new HistoryServiceProxy(ConfigurationReader.ReadValue(Context, "Settings", "History"));
            var            storageService = new ScadaStorageProxy(ConfigurationReader.ReadValue(Context, "Settings", "Storage"));
            var            domData        = domService.GetAll().GetAwaiter().GetResult();
            DomUpdateEvent dom            = new DomUpdateEvent()
            {
                DomData = domData.ToSwitchingEquipment()
            };
            HistoryUpdateEvent history = new HistoryUpdateEvent()
            {
                History = historyService.GetAll().GetAwaiter().GetResult()
            };
            HistoryGraphicalEvent graph = new HistoryGraphicalEvent()
            {
                Graph = historyService.GetGraph().GetAwaiter().GetResult()
            };
            ScadaUpdateEvent ev = new ScadaUpdateEvent()
            {
                Points = new List <SCADA.Common.DataModel.ScadaPointDto>()
            };

            var all      = (storageService.GetModel().GetAwaiter().GetResult()).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));

            Subscription subs      = new Subscription();
            Publisher    publisher = new Publisher(subs.Topic, subs.ConnectionString);

            if (ev.Points.Count > 0)
            {
                publisher.SendMessage(new PubSubMessage()
                {
                    ContentType = ContentType.SCADA_UPDATE,
                    Sender      = Sender.SCADA,
                    Content     = JsonTool.Serialize <ScadaUpdateEvent>(ev)
                }).ConfigureAwait(false);
            }
            if (dom.DomData.Count > 0)
            {
                publisher.SendMessage(new PubSubMessage()
                {
                    ContentType = ContentType.SCADA_DOM,
                    Sender      = Sender.SCADA,
                    Content     = JsonTool.Serialize <DomUpdateEvent>(dom)
                }).ConfigureAwait(false);
            }
            if (history.History.Count > 0)
            {
                publisher.SendMessage(new PubSubMessage()
                {
                    ContentType = ContentType.SCADA_HISTORY,
                    Sender      = Sender.SCADA,
                    Content     = JsonTool.Serialize <HistoryUpdateEvent>(history)
                }).ConfigureAwait(false);
            }
            publisher.SendMessage(new PubSubMessage()
            {
                ContentType = ContentType.SCADA_HISTORY_GRAPH,
                Sender      = Sender.SCADA,
                Content     = JsonTool.Serialize <HistoryGraphicalEvent>(graph)
            }).ConfigureAwait(false);
        }