Beispiel #1
0
        /// <summary>
        /// Retrieve Diagnostic data for the active vehicle
        /// </summary>
        /// <returns></returns>
        public async Task <DiagnosticResponse[]> GetDiagnostics()
        {
            var cmdInfo = ActiveVehicle.GetCommand("diagnostics");

            var reqObj = new JObject()
            {
                ["diagnosticsRequest"] = new JObject()
                {
                    ["diagnosticItem"] = new JArray(cmdInfo.CommandData.SupportedDiagnostics.SupportedDiagnostic)
                }
            };

            var result = await InitiateCommandAndWait("diagnostics", reqObj);

            if (result == null)
            {
                return(null);
            }
            if ("success".Equals(result.Status, StringComparison.OrdinalIgnoreCase))
            {
                return(result.Body.DiagnosticResponse);
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Connect to vehicle. Must be called before issuing commands
        /// </summary>
        /// <param name="vin"></param>
        /// <returns></returns>
        async Task <CommandResponse> VehicleConnect()
        {
            if (ActiveVehicle == null)
            {
                throw new InvalidOperationException("ActiveVehicle must be populated");
            }
            using (var response = await PostAsync(ActiveVehicle.GetCommand("connect").Url, new StringContent("{}", Encoding.UTF8, "application/json")))
            {
                if (response.IsSuccessStatusCode)
                {
                    var respString = await response.Content.ReadAsStringAsync();

                    var respObj = JsonConvert.DeserializeObject <CommandRequestResponse>(respString);
                    if (respObj == null || respObj.CommandResponse == null)
                    {
                        return(null);
                    }
                    return(respObj.CommandResponse);
                }
                else
                {
                    var error = await response.Content.ReadAsStringAsync();

                    return(null);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Submit the initial call for a command
        /// NOTE: this will be changing to use the URLs defined in vehicle configuration
        /// </summary>
        /// <param name="vin">Vehicle VIN</param>
        /// <param name="pin">OnStar PIN</param>
        /// <param name="command">command name</param>
        /// <returns></returns>
        async Task <CommandResponse> InitiateCommand(string command, JObject requestParameters)
        {
            if (ActiveVehicle == null)
            {
                throw new InvalidOperationException("ActiveVehicle must be populated");
            }

            var cmdInfo = ActiveVehicle.GetCommand(command);

            if (cmdInfo == null)
            {
                throw new InvalidOperationException("Unsupported command");
            }

            if (cmdInfo.IsPrivSessionRequired.GetValueOrDefault())
            {
                if (!IsUpgraded)
                {
                    //TODO: need to determine how long an upgrade lasts - do we reset it when refreshing the token?
                    // Also if the android app saves the PIN, should we save the PIN?
                    throw new InvalidOperationException("Command requires upgraded login");
                }
            }

            if (!_isConnected)
            {
                await VehicleConnect();

                _isConnected = true;
            }


            JObject reqObj = requestParameters;

            if (reqObj == null)
            {
                reqObj = new JObject();
            }



            using (var response = await PostAsync(cmdInfo.Url, new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(reqObj), Encoding.UTF8, "application/json")))
            {
                if (!response.IsSuccessStatusCode)
                {
                    var error = await response.Content.ReadAsStringAsync();

                    //todo: is this needed with the fancy post?
                    return(null);
                }

                var commandResult = await response.Content.ReadAsAsync <CommandRequestResponse>();

                return(commandResult.CommandResponse);
            }
        }