public static string DoGetVariable(ParticleVariables particleVariable) { Stopwatch logSw = new Stopwatch(); logSw.Start(); try { if (HttpContext.Current.Request.Headers["Content-Type"] != null && HttpContext.Current.Request.Headers["Content-Type"].IndexOf(JsonContentType, StringComparison.OrdinalIgnoreCase) == -1) { throw new ApplicationException($"Must use Contenty-Type of '{JsonContentType}'"); } var url = $"{EnvironmentVariables.ParticleUrl}/{particleVariable}?access_token={EnvironmentVariables.AccessToken}"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; // when running on 1and1 host you can't make a request except through their proxy if (!EnvironmentVariables.IsLocal) { request.Proxy = new WebProxy("ntproxyus.lxa.perfora.net", 3128); } string respRaw; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream strm = response.GetResponseStream()) { StreamReader strmRdr = new StreamReader(strm); respRaw = strmRdr.ReadToEnd(); } var particleVariableResponse = JsonConvert.DeserializeObject <ParticleVariableResponse>(respRaw); logSw.Stop(); return($"{particleVariableResponse.result}"); } catch (Exception ex) { logSw.Stop(); Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex, HttpContext.Current)); // send junk back to the client var logMsg = $"GetVariable Exception: {ex.Message} {ex.StackTrace}"; return(logMsg); } #region Pass Cookies experiment //HttpCookie x = Context.Request.Cookies[".ASPXFORMSAUTH"]; //CookieContainer cc = new CookieContainer(); //Uri uri = new Uri("http://local.garagedoor/"); //cc.Add(new Cookie("xxxxxxx", x.Value) { Domain = uri.Host }); //request.CookieContainer = cc; #endregion }
//public int PublishDataReady(ParticleVariables data) public int PublishDataReady(ParticleVariables data) { try { // In this case the retrieved particle variable is being logged to DB if (data == ParticleVariables.moveReadings) { var jStr = WebService1.DoGetVariable(data); var jObj = JsonConvert.DeserializeObject <SensorReadingsForOperation>(jStr); var readings = jObj.Readings.Select(r => $"t:{r.LogTime} r:{r.Rpm}"); Debug.WriteLine($"{string.Join("\n", readings.ToArray())}"); return(Log.LogSensor(jObj)); } return(-1); } catch (Exception ex) { Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex, HttpContext.Current)); return(-1); } }
public string GetVariable(ParticleVariables particleVariable) { return(DoGetVariable(particleVariable)); }