Beispiel #1
0
        public async override void OnVariableHistoryChanged(HistoryChange[] changes)
        {
            var alarms = await GetActiveAlarms();

            var events = await GetEvents(lastTimeRange, alarms);

            var newOrChangedAlarms = alarms.Where(a => {
                var oldVersion = lastAlarms.FirstOrDefault(x => x.T == a.T);
                if (oldVersion == null)
                {
                    return(true);
                }
                return(!StdJson.ObjectsDeepEqual(a, oldVersion));
            }).ToArray();

            var newOrChangedEvents = events.Where(a => {
                var oldVersion = lastEvents.FirstOrDefault(x => x.T == a.T);
                if (oldVersion == null)
                {
                    return(true);
                }
                return(!StdJson.ObjectsDeepEqual(a, oldVersion));
            }).ToArray();

            if (newOrChangedAlarms.Length > 0 || newOrChangedEvents.Length > 0)
            {
                lastAlarms = alarms;
                lastEvents = events;

                Context.SendEventToUI("Event", new {
                    Alarms = newOrChangedAlarms,
                    Events = newOrChangedEvents
                });
            }
        }
Beispiel #2
0
        public async override Task OnVariableHistoryChanged(List <HistoryChange> changes)
        {
            var alarms = await GetActiveAlarms();

            var events = await GetEvents(lastTimeRange, alarms);

            var newOrChangedAlarms = alarms.Where(a => {
                var oldVersion = lastAlarms.FirstOrDefault(x => x.T == a.T);
                if (oldVersion == null)
                {
                    return(true);
                }
                return(!StdJson.ObjectsDeepEqual(a, oldVersion));
            }).ToArray();

            long[] removedAlarms = lastAlarms.Where(la => alarms.All(a => a.T != la.T)).Select(x => x.T).ToArray();

            var newOrChangedEvents = events.Where(a => {
                var oldVersion = lastEvents.FirstOrDefault(x => x.T == a.T);
                if (oldVersion == null)
                {
                    return(true);
                }
                return(!StdJson.ObjectsDeepEqual(a, oldVersion));
            }).ToArray();

            if (newOrChangedAlarms.Length > 0 || newOrChangedEvents.Length > 0 || removedAlarms.Length > 0)
            {
                lastAlarms = alarms;
                lastEvents = events;

                await Context.SendEventToUI("Event", new {
                    Alarms        = newOrChangedAlarms,
                    Events        = newOrChangedEvents,
                    RemovedAlarms = removedAlarms,
                });
            }
        }
Beispiel #3
0
        private async Task InitAdapter(CalcInstance adapter)
        {
            if (adapter.Instance == null)
            {
                throw new Exception("InitAdapter: instance is null");
            }
            Calculation info = adapter.CalcConfig.ToCalculation();

            try {
                var initParams = new InitParameter()
                {
                    Calculation  = info,
                    LastOutput   = adapter.LastOutputValues,
                    LastState    = adapter.LastStateValues,
                    ConfigFolder = Path.GetDirectoryName(base.modelFileName) ?? "",
                    DataFolder   = initInfo.DataFolder,
                    ModuleConfig = moduleConfig.ToNamedValues()
                };
                adapter.State = State.InitStarted;
                InitResult res = await adapter.Instance.Initialize(initParams, new Wrapper(this, info));

                if (adapter.State == State.InitStarted)
                {
                    Config.Input[]  newInputs  = res.Inputs.Select(ip => MakeInput(ip, adapter.CalcConfig)).ToArray();
                    Config.Output[] newOutputs = res.Outputs.Select(ip => MakeOutput(ip, adapter.CalcConfig)).ToArray();
                    Config.State[]  newStates  = res.States.Select(MakeState).ToArray();

                    bool inputsChanged  = !StdJson.ObjectsDeepEqual(adapter.CalcConfig.Inputs, newInputs);
                    bool outputsChanged = !StdJson.ObjectsDeepEqual(adapter.CalcConfig.Outputs, newOutputs);
                    bool statesChanged  = !StdJson.ObjectsDeepEqual(adapter.CalcConfig.States, newStates);

                    var changedMembers = new List <MemberValue>(2);

                    if (inputsChanged)
                    {
                        changedMembers.Add(MemberValue.Make(moduleID, adapter.CalcConfig.ID, "Inputs", DataValue.FromObject(newInputs)));
                    }

                    if (outputsChanged)
                    {
                        changedMembers.Add(MemberValue.Make(moduleID, adapter.CalcConfig.ID, "Outputs", DataValue.FromObject(newOutputs)));
                    }

                    if (statesChanged)
                    {
                        changedMembers.Add(MemberValue.Make(moduleID, adapter.CalcConfig.ID, "States", DataValue.FromObject(newStates)));
                    }

                    if (changedMembers.Count > 0)
                    {
                        await UpdateConfig(GetModuleOrigin(),
                                           updateOrDeleteObjects : new ObjectValue[0],
                                           updateOrDeleteMembers : changedMembers.ToArray(),
                                           addArrayElements : new AddArrayElement[0]);
                    }

                    adapter.State = State.InitComplete;
                }
            }
            catch (Exception e) {
                Exception exp = e.GetBaseException() ?? e;
                adapter.State     = State.InitError;
                adapter.LastError = exp.Message;
                throw new Exception($"Initialize of calculation {info.Name} failed: " + exp.Message, exp);
            }
        }