private const string _password = "******"; // Password of the user static async Task Main() { Console.WriteLine("Welcome to the Customized Action Callback Sample"); Console.WriteLine(""); TokenHandler tokenHandler = new TokenHandler(_user, _password, _urlForTokenGeneration); Token token = tokenHandler.GetAccessToken(); FORCEBridgeConnector connector = new FORCEBridgeConnector(_urlToBridgeAPI, token); // Register Callback on Bridge API string MQTTTCPUrl = "mqtt://test.mosquitto.org"; //Define your MQTT-Broker and topic!!!! string PORT = "1883"; string MQTTTopicName = "external/forcecustomcallback"; string EventType = "CUSTOMIZED_ACTION_TRIGGERED"; string ForceCustomEventName = "test"; Console.WriteLine("Register callback " + EventType + " for MQTT-Broker " + MQTTTCPUrl + ", topic " + MQTTTopicName); Console.WriteLine(""); string callbackRegistrationJSON = BuildCallbackRegistrationJSON(MQTTTCPUrl + ":" + PORT + "/" + MQTTTopicName, EventType, ForceCustomEventName); connector.RegisterCallback(callbackRegistrationJSON); Console.WriteLine("Registration completed."); Console.WriteLine(""); // Inizialize a MQTT-Client and subscribe to the above defined topic Console.WriteLine("Initialize a MQTT Client and subscripe to the given topic " + MQTTTopicName + "..."); Console.WriteLine(""); var options = new MqttClientOptionsBuilder() .WithTcpServer(MQTTTCPUrl.Replace("mqtt://", String.Empty), 1883) .Build(); var factory = new MqttFactory(); var mqttClient = factory.CreateMqttClient(); await mqttClient.ConnectAsync(options, CancellationToken.None); // Subscribe to the defined topic await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(MQTTTopicName).Build()); Console.WriteLine("Wait for customited actions ..."); Console.WriteLine(""); List <string> callbackIDs = new List <string>(); mqttClient.UseApplicationMessageReceivedHandler(e => { Console.WriteLine("---------------------------------------"); var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); Console.WriteLine("--- RECEIVED " + EventType + " MESSAGE ---"); Console.WriteLine("---------------------------------------"); Console.WriteLine(Newtonsoft.Json.Linq.JObject.Parse(payload).SelectToken("properties.data").ToString()); Console.WriteLine("---------------------------------------"); Console.WriteLine(""); string callbackId = Newtonsoft.Json.Linq.JObject.Parse(payload).SelectToken("properties.callbackId").ToString(); if (!callbackIDs.Contains(callbackId)) { callbackIDs.Add(callbackId); } }); Console.ReadLine(); // DELETE THE CALLBACK Console.WriteLine("Delete the previous registered callback ..."); foreach (var id in callbackIDs) { Console.WriteLine("Delete the callback with id" + id); connector.DeleteCallback(id); } }
private const string _operating_state_code_for_alarming = "999"; // The operating state for alarming static async Task Main() { Console.WriteLine("Welcome to the alerting example (based on mqtt callbacks)!"); Console.WriteLine(""); TokenHandler tokenHandler = new TokenHandler(_user, _password, _urlForTokenGeneration); Token token = tokenHandler.GetAccessToken(); FORCEBridgeConnector connector = new FORCEBridgeConnector(_urlToBridgeAPI, token); Console.WriteLine("Connect to the Bridge API and get workplace ID of defined workplace " + _workplaceNumber); Console.WriteLine(""); string workplaceId = connector.GetWorkplaceByNumber(_workplaceNumber).Id; // Register Callback on Bridge API string MQTTTCPUrl = "mqtt://test.mosquitto.org"; //Define your MQTT-Broker and topic!!!! string PORT = "1883"; string MQTTTopicName = "external/statechange"; string EventType = "WORKPLACE_OPERATING_STATE_CHANGED"; Console.WriteLine("Register callback " + EventType + " of workplace " + workplaceId + " for MQTT-Broker " + MQTTTCPUrl + ", topic " + MQTTTopicName); Console.WriteLine(""); string callbackRegistrationJSON = BuildCallbackRegistrationJSON(MQTTTCPUrl + ":" + PORT + "/" + MQTTTopicName, workplaceId, EventType); connector.RegisterCallback(callbackRegistrationJSON); Console.WriteLine("Registration completed."); Console.WriteLine(""); // Inizialize a MQTT-Client and subscribe to the above defined topic Console.WriteLine("Initialize a MQTT Client and subscripe to the given topic " + MQTTTopicName + "..."); Console.WriteLine(""); var options = new MqttClientOptionsBuilder() .WithTcpServer(MQTTTCPUrl.Replace("mqtt://", String.Empty), 1883) .Build(); var factory = new MqttFactory(); var mqttClient = factory.CreateMqttClient(); await mqttClient.ConnectAsync(options, CancellationToken.None); // Subscribe to the defined topic await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(MQTTTopicName).Build()); Console.WriteLine("Wait for machine state changes ..."); Console.WriteLine(""); List <string> callbackIDs = new List <string>(); mqttClient.UseApplicationMessageReceivedHandler(e => { Console.WriteLine("---------------------------------------"); try { // WILL BE EXECUTED IF A MACHINE STATE OCCURES var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); var callbackResponse = Newtonsoft.Json.JsonConvert.DeserializeObject <WorkplaceOperatingStateChangedCallbackResponse>(payload); if (!callbackIDs.Contains(callbackResponse.Properties.CallbackId)) { callbackIDs.Add(callbackResponse.Properties.CallbackId); } Console.WriteLine("--- RECEIVED " + EventType + " MESSAGE ---"); Console.WriteLine("---------------------------------------"); Console.WriteLine("Current operating state: " + callbackResponse.Properties.Data.CurrentOperatingState.Description + " (" + callbackResponse.Properties.Data.CurrentOperatingState.Code + ")"); Console.WriteLine("Previous operating state: " + callbackResponse.Properties.Data.PreviousOperatingState.Description + " (" + callbackResponse.Properties.Data.PreviousOperatingState.Code + ")"); if (callbackResponse.Properties.Data.CurrentOperatingState.Code == _operating_state_code_for_alarming) { Console.WriteLine("---------------------------------------"); Console.WriteLine(" !!! WARNING WARNING WARNING WARNING !!!"); Console.WriteLine(" --- MACHINE SWITCHED TO STATE CODE " + _operating_state_code_for_alarming + " ---"); } } catch (Exception ex) { Console.WriteLine(" !!! ERROR OCCURED !!! " + ex.ToString()); } Console.WriteLine("---------------------------------------"); Console.WriteLine(""); }); Console.ReadLine(); // DELETE THE CALLBACK Console.WriteLine("Delete the previous registered callback ..."); foreach (var id in callbackIDs) { Console.WriteLine("Delete the callback with id" + id); connector.DeleteCallback(id); } }