/// <summary>
        /// Fetch a list of devices, in JSON format, from the Gateway.
        /// </summary>
        /// <returns>A JSON document containing the available devices.</returns>
        public DevicesResponse GetDevices()
        {
            var json = "";

            SLDPAPI.SilverLink link = new SilverLink();

            // Set up the SLDP API request.
            link.ClientID  = GatewaySettings.Instance.ClientId;
            link.Secret    = GatewaySettings.Instance.ClientSecret;
            link.Solution  = GatewaySettings.Instance.Solution;
            link.BaseURL   = GatewaySettings.Instance.GatewayBaseURL;
            link.TokenPath = GatewaySettings.Instance.GatewayTokens;

            FileLogger.Write("About to send Gateway request");
            FileLogger.Write("Customer Id = " + GatewaySettings.Instance.Solution);
            FileLogger.Write("Base URL = " + GatewaySettings.Instance.GatewayBaseURL);
            FileLogger.Write("Path = " + GatewaySettings.Instance.GatewayDeviceSearch);

            try
            {
                string path = GatewaySettings.Instance.GatewayDeviceSearch.Replace("<solution>", GatewaySettings.Instance.Solution);
                FileLogger.Write("About to send Gateway request");
                FileLogger.Write("Customer Id = " + GatewaySettings.Instance.Solution);
                FileLogger.Write("Base URL = " + GatewaySettings.Instance.GatewayBaseURL);
                FileLogger.Write("Path = " + path);
                json = link.Get(path);
            }
            catch (Exception e)
            {
                FileLogger.Write("Get failed");
                FileLogger.Write(e.Message);
                FileLogger.Write(e.StackTrace);

                // Rethrow the error if the POST failed.
                throw e;
            }
            FileLogger.Write("JSON response = " + json);

            GatewaySettings.Instance.SfdpToken = link.SilverLinkToken;
            FileLogger.Write("Token acquired from SFDP");
            FileLogger.Write(GatewaySettings.Instance.SfdpToken);

            DevicesResponse devices = null;

            try
            {
                devices = JsonConvert.DeserializeObject <DevicesResponse>(json);
            }
            catch (Exception jsonDeserializeError)
            {
                FileLogger.Write("Error deserializing device search response");
                FileLogger.Write(jsonDeserializeError.Message);
                FileLogger.Write(jsonDeserializeError.Message);
            }

            return(devices);
        }
Example #2
0
 /// <summary>
 /// Given a standard DevicesResponse, reads all devices from it and caches them (or updates their values in cache)
 /// </summary>
 /// <param name="response">A DevicesResponse from server</param>
 public static void CacheDevicesResponse(DevicesResponse response)
 {
     ExceptionUtility.Try(() =>
     {
         LogUtility.LogMessage(String.Format("Caching response {0} in app DB", response?.Header?.Type));
         if (response != null && response.IsSuccessful && response?.Body?.Devices != null)
         {
             CacheDevicesResponse(response.Body.Devices?.Items);
         }
     });
 }
        /// <summary>
        /// Http GET endpoint (api/devices) to get the current state of the connected devices.
        /// This endpoint can be called at anytime after the application starts and will always
        /// provide the ConnectionManager state which can be used to determine if haptic updates can be made.
        /// </summary>
        /// <returns></returns>
        public DevicesResponse Get()
        {
            DevicesResponse response = new DevicesResponse();

            response.state = ConnectionManager.Instance.GetState().ToString();

            ConnectionManager.BrilliantSoleDevice left = ConnectionManager.Instance.GetLeftDevice();
            if (left != null)
            {
                response.leftDeviceName = left.name;
                response.leftDeviceRssi = left.rssi;
            }
            ConnectionManager.BrilliantSoleDevice right = ConnectionManager.Instance.GetRightDevice();
            if (right != null)
            {
                response.rightDeviceName = right.name;
                response.rightDeviceRssi = right.rssi;
            }

            return(response);
        }