using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Azure.Devices.Client; using PNC.Core.RabbleEngine.Azure; using PNC.Core.RabbleEngine.QAD; using NLog; using System.Security.Cryptography.X509Certificates; namespace QADRPCHostCMD { class Program { private static DeviceClient _client = null; private static Logger logger = LogManager.GetCurrentClassLogger(); private static RetryPolicy _retryPolicy; /// /// lazy logger... /// /// message to log /// static void _log(string msg, bool newLine = true) { logger.Info(msg); if (newLine) Console.WriteLine(msg); else Console.Write(msg); } /// /// Sets a RPC handle /// /// Name of remote method /// call back function /// outcome of operation static bool _setHandler(string methodName, MethodCallback rpcCallback) { // A lot going on in this method. We use the retry policy we instanciated in the main loop to set handles for RPC calling try { _log(string.Format("Setting handler '{0}'", methodName), false); _client.SetMethodHandlerAsync(methodName, rpcCallback, null).Wait(); Console.ForegroundColor = ConsoleColor.Green; _log(" - Done."); Console.ForegroundColor = ConsoleColor.White; return true; } catch (Exception e) { _log(string.Format("FAULT: {0}\n\tStack Trace: {1}", e.Message, e.StackTrace)); return false; } } /// /// Removes runtime handlers for RPC /// /// method to remove /// outcome of operation static bool _removeHandler(string methodName) { try { _log(string.Format("Setting handler '{0}'", methodName), false); _client?.SetMethodHandlerAsync(methodName, null, null).Wait(); Console.ForegroundColor = ConsoleColor.Green; _log(" - Done."); Console.ForegroundColor = ConsoleColor.White; return true; } catch (Exception e) { _log(string.Format("FAULT: {0}\n\tStack Trace: {1}", e.Message, e.StackTrace)); return false; } } /// /// Methods must be setup to be handled here /// static void _setupMethodHandlers() { if (!( _setHandler("IsValidSerial", QADRPCHost.QADRPCMethods.IsValidSerial) && _setHandler("GetInventroyList", QADRPCHost.QADRPCMethods.GetInventoryList) && _setHandler("MoveFGCtr", QADRPCHost.QADRPCMethods.MoveFGCtr) && _setHandler("ValidateLogin", QADRPCHost.QADRPCMethods.ValidateLogin) // add new handler here )) throw new ApplicationException("Failed to initialize one or more handlers!"); // We cannot handle this exception, it must be fatal. } /// /// Deconstruct Method handlers here /// static void _cleanUpMethodHandlers() { if((! _removeHandler("IsValidSerial") && _removeHandler("GetInventoryList") && _removeHandler("MoveFGCtr") && _removeHandler("ValidateLogin") )) throw new ApplicationException("Failed to unhook one or more handlers!"); // We cannot handle this exception, it must be fatal. } static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.White; try { //Thumbprint for this device XXXXXXXXXXXXXXXXXXXXF630E82FE014B0469498 DeviceAuthenticationWithX509Certificate certAuth = new DeviceAuthenticationWithX509Certificate("QADRPCEngine01", /* calls method to get X509 from cert store. I have confirmed the thumbprint matches whats in the device registry./*); _client = DeviceClient.Create("pc-iothub01.azure-devices.net", certAuth, TransportType.Mqtt); _setupMethodHandlers(); Console.WriteLine(); _log("Method listener Initialized Successfully..."); _log("Now listening for incoming methods..."); Console.WriteLine(); _log("Press ESC at any time to quit.\n"); while(true) { ConsoleKeyInfo input = Console.ReadKey(); if (input.Key == ConsoleKey.Escape) { _log("Cleaning up method handlers..."); _cleanUpMethodHandlers(); _log("Done. Exiting..."); Environment.Exit(0); } } } catch (Exception exc) { _log(exc.Message); } } } }