private async Task <modemResponse> WaitForResponseAsync(string successMatch, string failMatch) { Regex successR = new Regex(successMatch); Regex errorR = new Regex(failMatch); modemResponse r = new modemResponse(); try { r.responseString = await GetModemResponseAsync(); if (successR.Match(r.responseString).Success) { r.responseStatus = aTCommandResponse.ok; } else if (errorR.Match(r.responseString).Success) { r.responseStatus = aTCommandResponse.error; } else { r.responseStatus = aTCommandResponse.unknown; } return(r); } catch (Exception ex) { this.log.Error(ex.Message); this.ViewModel.Status = ex.Message; throw ex; } }
private async Task <bool> OnTickAsync() { bool ok = false; string atComand; string response; string ss; string jsonData; modemResponse signalStrength = new modemResponse(); int[] signal; string[] commands = { @"AT+CMEE=2", @"AT+SAPBR=3,1,""CONTYPE"",""GPRS""", @"AT+SAPBR=3,1,""APN"",""globaldata""", //giffgaff.com @"AT+SAPBR=2,1", //Query GPRS context @"AT+SAPBR=1,1", //Open GPRS context @"AT+SAPBR=2,1", //Query GPRS context @"AT+HTTPINIT", @"AT+HTTPSSL=1", @"AT+HTTPPARA=""CID"",1", @"AT+HTTPPARA=""URL"", ""https://pflfunctions.azurewebsites.net/api/LevelMeasurement1?code=1PFxb4FHlHKcRWqWM91lPXtgtIJLl/l3F/52V7LFVHyN2gCxQJRApw==&clientId=default""", @"AT+HTTPPARA=""CONTENT"",""application/json""" }; try { //Get signal strength atComand = @"AT+CSQ"; await WriteWithLogAsync(atComand); signalStrength = await WaitForResponseAsync(@".+OK\s{2,}", @".+CME ERROR:\s{2,}"); //Parse for ok: "OK\r\n" and fail: "CME ERROR: operation not allowed\r\n" //Extract signal strength value Regex r = new Regex(@"(?:.+\+CSQ:)(\s*[\d]{1,2},[\d]{1,2}).+"); Match match = r.Match(signalStrength.responseString); ss = match.Groups[1].Value.Trim(); //retrieve dta as "nn,mm" value signal = ss.Split(',').Select(x => int.Parse(x)).ToArray <int>(); jsonData = $"{{\"SignalStrength\":\"({ss}) {signalStrengthConversion[signal[0]]}\"}}"; foreach (string command in commands) { await WriteWithLogAsync(command); ok = await WaitForResponseAsync(); log.Info("Modem Response to command : " + command + " is " + ok.ToString()); this.ViewModel.Status = "Modem Response to command : " + command + " is " + ok.ToString(); if (!ok) { return(false); } } //Set up download atComand = $"AT+HTTPDATA={jsonData.Length.ToString()},60000"; //Data length is length of json data object, in this case signal strength string await WriteWithLogAsync(atComand); ok = await WaitForResponseAsync(@"DOWNLOAD"); //Write data await WriteWithLogAsync(jsonData); // + "\u001A"); log.Info("Writing data: " + jsonData); //wait for echo response = await GetModemResponseAsync(); //POST command atComand = @"AT+HTTPACTION=1"; await WriteWithLogAsync(atComand); //check for HTTP OK (200) ok = await WaitForResponseAsync(@",200,"); log.Info("Modem Response to command : " + atComand + " is " + ok.ToString()); this.ViewModel.Status = "Modem Response to command : " + atComand + " is " + ok.ToString(); //close HTTP session atComand = @"AT+HTTPTERM"; await WriteWithLogAsync(atComand); ok = await WaitForResponseAsync(); log.Info("Modem Response to command : " + atComand + " is " + ok.ToString()); this.ViewModel.Status = "Modem Response to command : " + atComand + " is " + ok.ToString(); //Close GPRS context atComand = @"AT+SAPBR=0,1"; await WriteWithLogAsync(atComand); ok = await WaitForResponseAsync(); log.Info("Modem Response to command : " + atComand + " is " + ok.ToString()); this.ViewModel.Status = "Modem Response to command : " + atComand + " is " + ok.ToString(); return(true); } catch (Exception ex) { this.log.Error(ex.Message); this.ViewModel.Status = ex.Message; throw ex; } }