Exemplo n.º 1
0
        public override async Task <ReqResult> OnUiRequestAsync(string command, DataValue parameters)
        {
            switch (command)
            {
            case "Load": {
                TimeRange?time = parameters.Object <TimeRange>();
                if (time == null)
                {
                    throw new Exception("TimeRange is null");
                }

                var alarms = await GetActiveAlarms();

                var events = await GetEvents(time, alarms);

                lastAlarms    = alarms;
                lastEvents    = events;
                lastTimeRange = time;

                return(ReqResult.OK(new {
                        Alarms = alarms,
                        Events = events
                    }));
            }

            case "AckReset": {
                AckResetParams?para = parameters.Object <AckResetParams>();
                if (para == null)
                {
                    throw new Exception("AckResetParams is null");
                }
                string method  = para.Ack ? "Ack" : "Reset";
                var    comment = new NamedValue("Comment", para.Comment);
                var    keys    = new NamedValue("Keys", string.Join(';', para.Timestamps.Select(x => x.ToString(CultureInfo.InvariantCulture))));
                await Connection.CallMethod(Module, method, comment, keys);

                var alarms = await GetActiveAlarms();

                var events = await GetEvents(para.TimeRange, alarms);

                lastAlarms    = alarms;
                lastEvents    = events;
                lastTimeRange = para.TimeRange;

                return(ReqResult.OK(new {
                        Alarms = alarms,
                        Events = events
                    }));
            }

            default:
                return(ReqResult.Bad("Unknown command: " + command));
            }
        }
Exemplo n.º 2
0
        public virtual async Task <ReqResult> OnUiRequestAsync(string command, JObject parameters)
        {
            if (mapUiReqMethods.ContainsKey(command))
            {
                UiReqMethod method = mapUiReqMethods[command];
                method.ResetValues();

                foreach (JProperty p in parameters.Properties())
                {
                    if (method.ParameterMap.ContainsKey(p.Name))
                    {
                        UiReqPara para = method.ParameterMap[p.Name];
                        para.Value = p.Value.ToObject(para.Type);
                    }
                }

                object?[] paramValues = method.Parameters.Select(p => p.Value).ToArray();
                return(await method.TheDelegate(paramValues));
            }

            return(ReqResult.Bad("Unknown command: " + command));
        }
Exemplo n.º 3
0
        public override async Task <ReqResult> OnUiRequestAsync(string command, DataValue parameters)
        {
            bool hasModuleID = !string.IsNullOrEmpty(configuration.ModuleID);

            moduleID = hasModuleID ? configuration.ModuleID : "CALC";

            switch (command)
            {
            case "GetModel": {
                ObjectInfo root = await Connection.GetRootObject(moduleID);

                RootID = root.ID;

                await Connection.EnableVariableValueChangedEvents(SubOptions.AllUpdates(sendValueWithEvent: true), root.ID);

                return(await GetModelResult());
            }

            case "Save": {
                SaveParams saveParams = parameters.Object <SaveParams>() ?? throw new Exception("SaveParams is null");
                string     objID      = saveParams.ID;
                IDictionary <string, JToken?> dict = saveParams.Obj;
                MemberValue[] members = dict
                                        .Where(kv => kv.Key != "ID")
                                        .Select(entry => MakeMemberValue(objID, entry))
                                        .ToArray();
                await Connection.UpdateConfig(members);

                return(await GetModelResult());
            }

            case "Delete": {
                ObjectRef obj = ObjectRef.Make(moduleID, parameters.GetString() ?? "");
                await Connection.UpdateConfig(ObjectValue.Make(obj, DataValue.Empty));

                return(await GetModelResult());
            }

            case "AddObject": {
                AddObjectParams addParams = parameters.Object <AddObjectParams>() ?? throw new Exception("AddObjectParams is null");
                ObjectRef       objParent = ObjectRef.Make(moduleID, addParams.ParentObjID);
                DataValue       dataValue = DataValue.FromObject(new {
                        ID   = addParams.NewObjID,
                        Name = addParams.NewObjName
                    });
                var element = AddArrayElement.Make(objParent, addParams.ParentMember, dataValue);
                await Connection.UpdateConfig(element);

                return(await GetModelResult());
            }

            case "MoveObject": {
                var  move = parameters.Object <MoveObject_Params>() ?? throw new Exception("MoveObject_Params is null");
                bool up   = move.Up;

                ObjectRef  obj     = ObjectRef.Make(moduleID, move.ObjID);
                ObjectInfo objInfo = await Connection.GetObjectByID(obj);

                MemberRefIdx?parentMember = objInfo.Parent;

                if (parentMember.HasValue)
                {
                    MemberValue value = await Connection.GetMemberValue(parentMember.Value.ToMemberRef());

                    DataValue v = value.Value;
                    if (v.IsArray)
                    {
                        JArray array = (JArray)StdJson.JTokenFromString(v.JSON);
                        int    index = parentMember.Value.Index;
                        if (up && index > 0)
                        {
                            JToken tmp = array[index - 1];
                            array[index - 1] = array[index];
                            array[index]     = tmp;

                            MemberValue mv = MemberValue.Make(parentMember.Value.ToMemberRef(), DataValue.FromObject(array));
                            await Connection.UpdateConfig(mv);
                        }
                        else if (!up && index < array.Count - 1)
                        {
                            JToken tmp = array[index + 1];
                            array[index + 1] = array[index];
                            array[index]     = tmp;

                            MemberValue mv = MemberValue.Make(parentMember.Value.ToMemberRef(), DataValue.FromObject(array));
                            await Connection.UpdateConfig(mv);
                        }
                    }
                }

                return(await GetModelResult());
            }

            case "ReadModuleObjects": {
                var pars = parameters.Object <ReadModuleObjectsParams>() ?? throw new Exception("ReadModuleObjectsParams is null");

                ObjectInfos objects;

                try {
                    objects = await Connection.GetAllObjects(pars.ModuleID);
                }
                catch (Exception) {
                    objects = new ObjectInfos();
                }

                Func <Variable, bool> isMatch = GetMatchPredicate(pars.ForType);

                return(ReqResult.OK(new {
                        Items = objects.Where(o => o.Variables.Any(isMatch)).Select(o => new {
                            Type = o.ClassName,
                            ID = o.ID.ToEncodedString(),
                            Name = o.Name,
                            Variables = o.Variables.Where(isMatch).Select(v => v.Name).ToArray()
                        }).ToArray()
                    }));
            }

            default:
                return(ReqResult.Bad("Unknown command: " + command));
            }
        }