Пример #1
0
        public async Task StartAsync(
            AdminShellPackageEnv package,
            AnyUiDialogueDataMqttPublisher diaData,
            GrapevineLoggerToStoredPrints logger = null)
        {
            // first options
            _diaData = diaData;
            _logger  = logger;
            if (_diaData == null)
            {
                logger?.Error("Error: no options available.");
                return;
            }

            // broker?
            var hp = SplitBrokerUrl(_diaData.BrokerUrl);

            if (hp == null)
            {
                _logger?.Error("Error: no broker URL available.");
                return;
            }
            _logger?.Info($"Conneting broker {hp.Item1}:{hp.Item2} ..");

            // Create TCP based options using the builder.

            var options = new MqttClientOptionsBuilder()
                          .WithClientId("AASXPackageXplorer MQTT Client")
                          .WithTcpServer(hp.Item1, hp.Item2)
                          .Build();

            //create MQTT Client and Connect using options above

            _mqttClient = new MqttFactory().CreateMqttClient();
            await _mqttClient.ConnectAsync(options);

            if (_mqttClient.IsConnected)
            {
                _logger?.Info("### CONNECTED WITH SERVER ###");
            }

            //publish AAS to AAS Topic

            if (_diaData.EnableFirstPublish)
            {
                foreach (AdminShell.AdministrationShell aas in package.AasEnv.AdministrationShells)
                {
                    _logger?.Info("Publish first AAS");
                    var message = new MqttApplicationMessageBuilder()
                                  .WithTopic(GenerateTopic(
                                                 _diaData.FirstTopicAAS, defaultIfNull: "AAS",
                                                 aasIdShort: aas.idShort, aasId: aas.identification))
                                  .WithPayload(Newtonsoft.Json.JsonConvert.SerializeObject(aas))
                                  .WithExactlyOnceQoS()
                                  .WithRetainFlag(_diaData.MqttRetain)
                                  .Build();

                    await _mqttClient.PublishAsync(message);

                    LogStatus(incElement: 1);

                    //publish submodels
                    foreach (var sm in package.AasEnv.Submodels)
                    {
                        // whole structure
                        _logger?.Info("Publish first " + "Submodel_" + sm.idShort);

                        var message2 = new MqttApplicationMessageBuilder()
                                       .WithTopic(GenerateTopic(
                                                      _diaData.FirstTopicSubmodel, defaultIfNull: "Submodel_" + sm.idShort,
                                                      aasIdShort: aas.idShort, aasId: aas.identification,
                                                      smIdShort: sm.idShort, smId: sm.identification))
                                       .WithPayload(Newtonsoft.Json.JsonConvert.SerializeObject(sm))
                                       .WithExactlyOnceQoS()
                                       .WithRetainFlag(_diaData.MqttRetain)
                                       .Build();

                        await _mqttClient.PublishAsync(message2);

                        LogStatus(incElement: 1);

                        // single values as well?
                        if (_diaData.SingleValueFirstTime)
                        {
                            PublishSingleValues_FirstTimeSubmodel(aas, sm, sm.GetReference()?.Keys);
                        }
                    }
                }
            }

            _logger?.Info("Publish full events: " + _diaData.EnableEventPublish);
            _logger?.Info("Publish single values: " + _diaData.SingleValuePublish);
        }