Example #1
0
        async Task <string> Send(ThingSpeakModel data)
        {
            var result    = "";
            var channelId = "<Your channel id>";

            using (var client = new HttpClient())
            {
                var url = $"https://api.thingspeak.com/channels/{channelId}/bulk_update.json";

                var content = JsonConvert.SerializeObject(data);
                var req     = new HttpRequestMessage(HttpMethod.Post, url)
                {
                    Content = new StringContent(content, Encoding.UTF8, "application/json")
                };
                var res = await client.SendAsync(req);


                if (res.StatusCode == HttpStatusCode.Accepted)
                {
                    result += await res.Content.ReadAsStringAsync();
                }
                else
                {
                    throw new Exception($"Fehler beim Übertragen an ThingSpeak: {res.StatusCode}");
                }
            }
            return(result);
        }
Example #2
0
        async Task <string> UploadTrip(string fileName)
        {
            string result  = "";
            var    api_key = "<Your ThingSpeak key>";

            var data = new ThingSpeakModel()
            {
                write_api_key = api_key,
                updates       = new List <ThingSpeakOBD>()
            };

            int count = 0;

            try
            {
                using (UserDialogs.Instance.Loading("CarAssist", maskType: MaskType.Black))
                {
                    using (var reader = new StreamReader(File.OpenRead(fileName)))
                    {
                        string line = null;
                        while ((line = await reader.ReadLineAsync()) != null)
                        {
                            // Datetime,Latitude,Longitude,VIN,rpm,speed,iat,maf,celv,fuel
                            var values     = line.Split(',');
                            var created_at = DateTime.Parse(values[0]).ToString("o");

                            data.updates.Add(new ThingSpeakOBD
                            {
                                created_at = created_at,
                                field1     = values[3], // VIN
                                field2     = values[4], // RPM
                                field3     = values[5], // Speed
                                field4     = values[6], // IntakeAirTemperature
                                field5     = values[7], // MAFAirFlowRate
                                field6     = values[8], // CalculatedEngineLoadValue
                                field7     = values[9]  // FuelTankLevel
                                                        //lat = values[1],
                                                        //lon = values[2]
                            });
                            count++;
                            if (count == 960)   // Free license, max. count
                            {
                                break;
                            }
                        }
                    }
                    await Send(data);

                    // hochgeladene Datei löschen
                    //File.Delete(fileName);
                    await DisplayAlert("CarAssist", $"{count} Datensätze hochgeladen", "Ok");
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("CarAssist", $"Hochgeladene Datensätze: {count}\n" + ex.Message, "Ok");
            }
            return(result);
        }