예제 #1
0
        private void PublishSingleValues_UpdateItem(
            AasEventMsgEnvelope ev,
            AdminShell.ReferableRootInfo ri,
            AdminShell.KeyList startPath,
            AasPayloadUpdateValueItem ui)
        {
            // trivial
            if (ev == null || ui == null || startPath == null || ui.Path == null)
            {
                return;
            }

            // value of the leaf
            var valStr = "" + ui.Value;

            // build a complete path of keys
            var path    = startPath + ui.Path;
            var pathStr = path.BuildIdShortPath();

            // publish
            if (_diaData.LogDebug)
            {
                _logger?.Info("Publish single value (update value)");
            }
            var message = new MqttApplicationMessageBuilder()
                          .WithTopic(GenerateTopic(
                                         _diaData.EventTopic, defaultIfNull: "SingleValue",
                                         aasIdShort: ri?.AAS?.idShort, aasId: ri?.AAS?.identification,
                                         smIdShort: ri?.Submodel?.idShort, smId: ri?.Submodel?.identification,
                                         path: pathStr))
                          .WithPayload(valStr)
                          .WithExactlyOnceQoS()
                          .WithRetainFlag(_diaData.MqttRetain)
                          .Build();

            // publish
            _mqttClient.PublishAsync(message).GetAwaiter().GetResult();
            LogStatus(incSingleValue: 1);
        }
예제 #2
0
        public void Animate(
            AdminShell.Property prop,
            Action <AdminShell.Property, AdminShell.IAasDiaryEntry> emitEvent)
        {
            // prop needs to exists and have qualifiers
            var q    = prop.HasQualifierOfType("Animate.Args");
            var args = Parse(q.value);

            if (args == null)
            {
                return;
            }

            // get state
            var state = GetState(prop, createIfNeeded: true);

            if (state == null)
            {
                return;
            }

            // how long till last time
            var deltaSecs = (DateTime.UtcNow - state.LastTime).TotalSeconds;

            if (deltaSecs < 0.001 * Math.Max(100, args.timer))
            {
                return;
            }

            // save new lastTime, phase
            state.LastTime = DateTime.UtcNow;
            var phase = state.Phase;

            state.Phase = (state.Phase + deltaSecs * args.freq) % 2.0;

            // function?
            double?res = null;

            switch (args.type)
            {
            case AnimateArgs.TypeDef.Saw:
                res   = args.ofs + args.scale * Math.Sin(-1.0 + phase);
                phase = phase % 2.0;
                break;

            case AnimateArgs.TypeDef.Sin:
                res = args.ofs + args.scale * Math.Sin(phase * 2 * Math.PI);
                break;

            case AnimateArgs.TypeDef.Cos:
                res = args.ofs + args.scale * Math.Cos(phase * 2 * Math.PI);
                break;

            case AnimateArgs.TypeDef.Square:
                res   = args.ofs + ((phase < 1.0) ? -1.0 : 1.0);
                phase = phase % 2.0;
                break;
            }

            // use result?
            if (res.HasValue)
            {
                // set value
                prop.value = res.Value.ToString(CultureInfo.InvariantCulture);

                // send event
                if (emitEvent != null)
                {
                    // create
                    var evi = new AasPayloadUpdateValueItem(
                        path: (prop as AdminShell.IGetReference)?.GetReference()?.Keys,
                        value: prop.ValueAsText());

                    evi.ValueId = prop.valueId;

                    evi.FoundReferable = prop;

                    // add
                    AdminShell.DiaryDataDef.AddAndSetTimestamps(prop, evi, isCreate: false);

                    // kick lambda
                    emitEvent.Invoke(prop, evi);
                }
            }
        }
예제 #3
0
        //
        // Event management
        //

        /* TODO (MIHO, 2021-08-17): check if to refactor/ move to another location
         * and to extend to Submodels .. */
        public static bool UpdateSmeFromEventPayloadItem(
            AdminShell.SubmodelElement smeToModify,
            AasPayloadUpdateValueItem vl)
        {
            // access
            if (smeToModify == null || vl == null)
            {
                return(false);
            }

            var changedSomething = false;

            // adopt
            // TODO (MIHO, 2021-08-17): add more type specific conversions?
            if (smeToModify is AdminShell.Property prop)
            {
                if (vl.Value is string vls)
                {
                    prop.value = vls;
                }
                if (vl.ValueId != null)
                {
                    prop.valueId = vl.ValueId;
                }
                changedSomething = true;
            }

            if (smeToModify is AdminShell.MultiLanguageProperty mlp)
            {
                if (vl.Value is AdminShell.LangStringSet lss)
                {
                    mlp.value = lss;
                }
                if (vl.ValueId != null)
                {
                    mlp.valueId = vl.ValueId;
                }
                changedSomething = true;
            }

            if (smeToModify is AdminShell.Range rng)
            {
                if (vl.Value is string[] sarr && sarr.Length >= 2)
                {
                    rng.min = sarr[0];
                    rng.max = sarr[1];
                }
                changedSomething = true;
            }

            if (smeToModify is AdminShell.Blob blob)
            {
                if (vl.Value is string vls)
                {
                    blob.value = vls;
                }
                changedSomething = true;
            }

            // ok
            return(changedSomething);
        }