//Callback for MQTT connection. async void IServiceCallback.MQTTConnect(string brokerIP) { await mqttClientSemaphore.WaitAsync(); try { managedMqtt = ServiceLogic.CreateManagedClient(); await ServiceLogic.ManagedMqttConnectWebSocket(managedMqtt, brokerIP); } finally { mqttClientSemaphore.Release(); } }
protected override void OnStart(string[] args) { try { createDirectories(); //Set the static callback reference and creates interface for WinForms by hosting a WCF Service. Host.Current = this; StartBroker(); // Log an event to indicate successful start. EventLog.WriteEntry("Successful start.", EventLogEntryType.Information); //Initialize MQTT Client. managedMqtt = ServiceLogic.CreateManagedClient(); //MQTT Broker connection string brokerIP = "dev-harmony-01.southeastasia.cloudapp.azure.com:8080/mqtt"; MQTTConnectClient(managedMqtt, brokerIP); //Initialize OPC Application Instance application = new ApplicationInstance { ApplicationName = "MQTT-OPC Broker", ApplicationType = ApplicationType.ClientAndServer, ConfigSectionName = "Opc.Ua.SampleClient" }; //load the application configuration. application.LoadApplicationConfiguration(false).Wait(); // check the application certificate. application.CheckApplicationInstanceCertificate(false, 0).Wait(); m_configuration = app_configuration = application.ApplicationConfiguration; // get list of cached endpoints. m_endpoints = m_configuration.LoadCachedEndpoints(true); m_endpoints.DiscoveryUrls = app_configuration.ClientConfiguration.WellKnownDiscoveryUrls; if (!app_configuration.SecurityConfiguration.AutoAcceptUntrustedCertificates) { app_configuration.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation); } //If there was a pre-exising session before, attempt reconnection. foreach (string sessionFile in Directory.GetFiles(sessionsFolder, "*.json")) { String[] sessions = File.ReadAllLines(sessionFile); foreach (string retainedEndpoint in sessions) { if (retainedEndpoint != "") { try { OPCConnect(application, retainedEndpoint); sessionEndpoint = retainedEndpoint; break; } catch { //Ignore exception as server may not be up. } break; } } String readSession = File.ReadAllText(sessionFile); } serviceCheck = true; //Use timer callback to monitor and publish subscriptions. publishTimer = new System.Threading.Timer(x => OPCPublish(m_session), null, 5000, 1000); } catch (Exception ex) { EventLog.WriteEntry(ex.Message, EventLogEntryType.Error); } }