public async Task <IActionResult> Add([FromBody] InstrumentIdVM newInstrument)
        {
            try
            {
                // Log that we were called.
                _logger.LogInformation("Add");
                if (!IsAdmin())
                {
                    return(new UnauthorizedResult());
                }

                if (newInstrument == null)
                {
                    return(new StatusCodeResult(500));
                }

                InstrumentInfo instrumentInfo;
                if (Configuration["DataCenter:Location:Simulated"] == "true")
                {
                    instrumentInfo = m_simulatedInstruments.AddInstrument(newInstrument.vm_address, newInstrument.vm_description).InstrumentInfo;
                }
                else
                {
                    InstrumentManagementAdminClientAsync m_Client;

                    string serverUriString = string.Format("{0}{1}", GetServerURI(), ProductHelper.UrlInstrumentManagementAdmin);
                    m_Client = new InstrumentManagementAdminClientAsync(ServicesHelper.GetDefaultBinding(),
                                                                        new EndpointAddress(serverUriString));

                    instrumentInfo = await m_Client.AddInstrumentAsync(newInstrument.vm_address, newInstrument.vm_description);
                }

                // Return the result in JSON format
                return(new JsonResult(
                           instrumentInfo.HostAddress,
                           new JsonSerializerSettings()
                {
                    Formatting = Formatting.Indented
                }));
            }
            catch (Exception exc)
            {
                // Log the exception and return it to the caller
                _logger.LogError(exc.ToString());
                throw;
            }
        }
        public async Task <IActionResult> Count()
        {
            try
            {
                // Log that we were called.
                _logger.LogInformation("Count");

                LiveInstrumentInfo[] array;
                if (Configuration["DataCenter:Location:Simulated"] == "true")
                {
                    array = m_simulatedInstruments.Instruments.Cast <LiveInstrumentInfo>().ToArray();
                }
                else
                {
                    IEnumerable <LiveInstrumentInfo> enumerable;
                    InstrumentManagementClientAsync  m_Client;

                    string serverUriString = string.Format("{0}{1}", GetServerURI(), ProductHelper.UrlInstrumentManagement);
                    m_Client = new InstrumentManagementClientAsync(ServicesHelper.GetDefaultBinding(),
                                                                   new EndpointAddress(serverUriString));

                    enumerable = await m_Client.GetRegisteredInstrumentsAsync();

                    array = enumerable.Cast <LiveInstrumentInfo>().ToArray();
                }

                int count = array.Length;
                // Return the result in JSON format
                return(new JsonResult(
                           count,
                           new JsonSerializerSettings()
                {
                    Formatting = Formatting.Indented
                }));
            }
            catch (Exception exc)
            {
                // Log the exception and return it to the caller
                _logger.LogError(exc.ToString());
                throw;
            }
        }
        public async Task <IActionResult> RegisteredInstruments()
        {
            try
            {
                // Log that we were called.
                _logger.LogInformation("RegisteredInstruments");

                // Caller must be a logged on user in order to request the list of registered instruments
                if (!IsUser())
                {
                    return(new UnauthorizedResult());
                }

                LiveInstrumentInfo[] array;
                if (Configuration["DataCenter:Location:Simulated"] == "true")
                {
                    array = m_simulatedInstruments.Instruments.Cast <LiveInstrumentInfo>().ToArray();
                }
                else
                {
                    IEnumerable <LiveInstrumentInfo> enumerable;
                    InstrumentManagementClientAsync  m_Client;

                    string serverUriString = string.Format("{0}{1}", GetServerURI(), ProductHelper.UrlInstrumentManagement);
                    m_Client = new InstrumentManagementClientAsync(ServicesHelper.GetDefaultBinding(),
                                                                   new EndpointAddress(serverUriString));

                    enumerable = await m_Client.GetRegisteredInstrumentsAsync();

                    array = enumerable.Cast <LiveInstrumentInfo>().ToArray();
                }

                List <IInstrumentVM> list = new List <IInstrumentVM>();
                foreach (var liveInstrumentInfo in array)
                {
                    var instrumentViewModel = new InstrumentVM();
                    instrumentViewModel.vm_address         = liveInstrumentInfo.HostAddress;
                    instrumentViewModel.vm_description     = liveInstrumentInfo.HostAddress;
                    instrumentViewModel.vm_instrument      = liveInstrumentInfo.HostAddress;
                    instrumentViewModel.vm_status          = liveInstrumentInfo.Status.ToString();
                    instrumentViewModel.vm_reactor_1       = liveInstrumentInfo.Reactor1Value;
                    instrumentViewModel.vm_reactor_2       = liveInstrumentInfo.Reactor2Value;
                    instrumentViewModel.vm_version         = liveInstrumentInfo.InstrumentInfo.Version;
                    instrumentViewModel.vm_serial_number   = liveInstrumentInfo.InstrumentInfo.SerialNumber;
                    instrumentViewModel.vm_time_difference = liveInstrumentInfo.TimeDifference.HasValue ? liveInstrumentInfo.TimeDifference.Value.ToString() : "";
                    instrumentViewModel.vm_last_update     = liveInstrumentInfo.LastSuccessfulCommunication.HasValue ? liveInstrumentInfo.LastSuccessfulCommunication.Value : DateTime.MinValue;
                    list.Add(instrumentViewModel);
                }

                // Return the result in JSON format
                return(new JsonResult(
                           list.ToArray(),
                           new JsonSerializerSettings()
                {
                    Formatting = Formatting.Indented
                }));
            }
            catch (Exception exc)
            {
                // Log the exception and return it to the caller
                _logger.LogError(exc.ToString());
                throw;
            }
        }