private string ListToJson(List <GreenhouseState> states) { HardwareStatePacket packet = new HardwareStatePacket() { heater = false.ToString().ToLower(), lights = false.ToString().ToLower(), vents = false.ToString().ToLower(), pump = false.ToString().ToLower(), fans = false.ToString().ToLower(), shades = false.ToString().ToLower() }; foreach (GreenhouseState state in states) { switch (state) { case GreenhouseState.WATERING: packet.pump = true.ToString().ToLower(); break; case GreenhouseState.LIGHTING: packet.lights = true.ToString().ToLower(); break; case GreenhouseState.HEATING: packet.heater = true.ToString().ToLower(); packet.fans = true.ToString().ToLower(); break; case GreenhouseState.COOLING: packet.fans = true.ToString().ToLower(); packet.vents = true.ToString().ToLower(); break; case GreenhouseState.SHADING: packet.shades = true.ToString().ToLower(); break; default: break; } } string json = JsonConvert.SerializeObject(packet); return(json); }
/// <summary> /// Sends an HTTP POST t /// </summary> /// <param name="path"></param> /// <returns></returns> public bool PostGreenhouseState(string path) { // Get the current state of the greenhouse and put it into a form the API can handle HardwareStatePacket greenhouseState = new HardwareStatePacket(); greenhouseState.apikey = _apiKey; #region GetGreenhouseStates if (StateMachineContainer.Instance.Temperature.CurrentState == GreenhouseState.COOLING) { greenhouseState.fans = true.ToString(); greenhouseState.heater = false.ToString(); greenhouseState.vents = true.ToString(); } else if (StateMachineContainer.Instance.Temperature.CurrentState == GreenhouseState.HEATING) { greenhouseState.fans = true.ToString(); greenhouseState.heater = true.ToString(); greenhouseState.vents = false.ToString(); } else { greenhouseState.fans = false.ToString(); greenhouseState.heater = false.ToString(); greenhouseState.vents = false.ToString(); } if (StateMachineContainer.Instance.Shading.CurrentState == GreenhouseState.SHADING) { greenhouseState.shades = true.ToString(); } else { greenhouseState.shades = false.ToString(); } if (StateMachineContainer.Instance.WateringStateMachines.Where(s => s.CurrentState == GreenhouseState.WATERING).Count() != 0) { greenhouseState.pump = true.ToString(); } else { greenhouseState.pump = false.ToString(); } if (StateMachineContainer.Instance.LightStateMachines.Where(s => s.CurrentState == GreenhouseState.LIGHTING).Count() != 0) { greenhouseState.lights = true.ToString(); } else { greenhouseState.lights = false.ToString(); } #endregion string greenhouseStateText = $"heater={greenhouseState.heater}&apikey={greenhouseState.apikey}" + $"&shades={greenhouseState.shades}&fans={greenhouseState.fans}&lights={greenhouseState.lights}&pump={greenhouseState.pump}&vents={greenhouseState.vents}"; try { // Create the request and set the correct properties HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_baseUrl + path); //req.ContentType = "application/json; charset=utf-8"; req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; // Write the JSON to the stream using (StreamWriter writer = new StreamWriter(req.GetRequestStream())) { //Console.WriteLine(greenhouseStateJson); writer.Write(greenhouseStateText); writer.Flush(); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); using (var streamReader = new StreamReader(resp.GetResponseStream())) { string result = streamReader.ReadToEnd(); } return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }