void CreateSensorOnClick() { foreach (InputField e in required) { function.RequiredInputOnEndEdit(e); } foreach (Text e in warning) { if (e.IsActive()) { return; } } if (function.InputFieldRequired(required)) { string requestType; if (temperature.isOn) { requestType = "/saveTemperatureSensor"; } else { requestType = "/saveWetnessSensor"; } HTTPRequest request = new HTTPRequest(new Uri(data.IP + requestType), HTTPMethods.Post, (req, res) => { switch (req.State) { case HTTPRequestStates.Finished: Debug.Log(res.DataAsText); GameObject.Find("Canvas/cover").SetActive(false); GameObject.Find("Canvas/sensor_box").SetActive(false); function.Clear(required, warning, pass); function.FreshGarden(selected); break; default: Debug.Log("Error!Status code:" + res.StatusCode); break; } }); request.AddHeader("Content-Type", "application/json"); JsonData newSensor = new JsonData(); newSensor["gardenId"] = selected.getId(); newSensor["x"] = location_x.text; newSensor["y"] = location_y.text; newSensor["name"] = sensor_name.text; newSensor["valid"] = true; request.RawData = System.Text.Encoding.UTF8.GetBytes(newSensor.ToJson()); request.Send(); } }
void SaveOnClick() { foreach (InputField e in required) { function.RequiredInputOnEndEdit(e); } foreach (Text e in warning) { if (e.IsActive()) { return; } } if (function.InputFieldRequired(required)) { if (garden_name.text != selected.getName() || temperature.text != selected.getIdealTemperature().ToString() || humidity.text != selected.getIdealHumidity().ToString() || length.text != selected.getLength().ToString() || width.text != selected.getWidth().ToString()) { HTTPRequest request = new HTTPRequest(new Uri(data.IP + "/updateGarden"), HTTPMethods.Post, (req, res) => { switch (req.State) { case HTTPRequestStates.Finished: Debug.Log(res.DataAsText); function.FreshGarden(selected); break; default: Debug.Log("Error!Status code:" + res.StatusCode); break; } }); request.AddHeader("Content-Type", "application/json"); JsonData newGarden = new JsonData(); newGarden["id"] = selected.getId().ToString(); newGarden["userId"] = data.m_user.getId().ToString(); newGarden["width"] = width.text; newGarden["length"] = length.text; newGarden["name"] = garden_name.text; newGarden["idealTemperature"] = temperature.text; newGarden["idealWetness"] = humidity.text; request.RawData = System.Text.Encoding.UTF8.GetBytes(newGarden.ToJson()); request.Send(); } GameObject.Find("Canvas/cover").SetActive(false); GameObject.Find("Canvas/garden_info").SetActive(false); } }
public void test() { HTTPRequest request_getSensorData1 = new HTTPRequest(new Uri(data.IP + "/getLatestTemperatureByGardenId?gardenId=" + selected.getId()), HTTPMethods.Get, (req_data1, res_data1) => { Debug.Log(res_data1.DataAsText); JArray array = JArray.Parse(res_data1.DataAsText); foreach (var e in array) { foreach (sensor n in selected.getSensors()) { if (n.getId() == (long)e["id"] && n.getType()) { n.setData((float)e["temperature"]); updateOnePoint(n.getX(), n.getY(), n.getData()); } } } }).Send(); webSocket.Open(); }
public static void GetControllers(m_garden garden) { List <controller> controllers = new List <controller>(); HTTPRequest request_getController = new HTTPRequest(new Uri(data.IP + "/getControllerByGardenId?gardenId=" + garden.getId()), HTTPMethods.Get, (req_controller, res_controller) => { Debug.Log(res_controller.DataAsText); JArray array = JArray.Parse(res_controller.DataAsText); foreach (var e in array) { controller temp = new controller(); temp.setId((long)e["id"]); temp.setName((string)e["name"]); temp.setX((int)e["x"] * data.width / garden.getLength()); temp.setY((int)e["y"] * data.length / garden.getWidth()); temp.setState((bool)e["valid"]); controllers.Add(temp); MapBG.drawOne(temp.getId(), temp.getName(), temp.getX(), temp.getY(), MapBG.SensorControllerType.Irrigation, temp.getState(), 0, 0, 0); } garden.addController(controllers); }).Send(); return; }
public static void GetSensors(m_garden garden) { List <sensor> sensors = new List <sensor>(); HTTPRequest request_getSensor1 = new HTTPRequest(new Uri(data.IP + "/getTemperatureSensorByGardenId?gardenId=" + garden.getId()), HTTPMethods.Get, (req_sensor1, res_sensor1) => { Debug.Log(res_sensor1.DataAsText); JArray array = JArray.Parse(res_sensor1.DataAsText); foreach (var e in array) { sensor temp = new sensor(); temp.setId((long)e["id"]); temp.setName((string)e["name"]); temp.setX((int)e["x"] * data.width / garden.getLength()); temp.setY((int)e["y"] * data.length / garden.getWidth()); temp.setType(true); sensors.Add(temp); } garden.addSensor(sensors); }).Send(); HTTPRequest request_getSensor2 = new HTTPRequest(new Uri(data.IP + "/getWetnessSensorByGardenId?gardenId=" + garden.getId()), HTTPMethods.Get, (req_sensor2, res_sensor2) => { Debug.Log(res_sensor2.DataAsText); JArray array = JArray.Parse(res_sensor2.DataAsText); foreach (var e in array) { sensor temp = new sensor(); temp.setId((long)e["id"]); temp.setName((string)e["name"]); temp.setX((int)e["x"] * data.width / garden.getLength()); temp.setY((int)e["y"] * data.length / garden.getWidth()); temp.setType(false); sensors.Add(temp); } garden.addSensor(sensors); }).Send(); HTTPRequest request_getSensorData1 = new HTTPRequest(new Uri(data.IP + "/getLatestTemperatureByGardenId?gardenId=" + garden.getId()), HTTPMethods.Get, (req_data1, res_data1) => { Debug.Log(res_data1.DataAsText); JArray array = JArray.Parse(res_data1.DataAsText); foreach (var e in array) { foreach (sensor n in garden.getSensors()) { if (n.getId() == (long)e["id"] && n.getType()) { n.setData((float)e["temperature"]); } } } foreach (sensor e in garden.getSensors()) { if (e.getType() && e.getData() != 0) { MapBG.drawOne(e.getId(), e.getName(), e.getX(), e.getY(), MapBG.SensorControllerType.Temperature, true, e.getData(), garden.getIdealTemperature() * (float)1.2, garden.getIdealTemperature() * (float)0.8); } else if (e.getType() && e.getData() == 0) { MapBG.drawOne(e.getId(), e.getName(), e.getX(), e.getY(), MapBG.SensorControllerType.Temperature, false, garden.getIdealTemperature(), garden.getIdealTemperature() * (float)1.2, garden.getIdealTemperature() * (float)0.8); } } }).Send(); HTTPRequest request_getSensorData2 = new HTTPRequest(new Uri(data.IP + "/getLatestWetnessByGardenId?gardenId=" + garden.getId()), HTTPMethods.Get, (req_data2, res_data2) => { Debug.Log(res_data2.DataAsText); JArray array = JArray.Parse(res_data2.DataAsText); foreach (var e in array) { foreach (sensor n in garden.getSensors()) { if (n.getId() == (long)e["id"] && !n.getType()) { n.setData((float)e["wetness"]); } } } foreach (sensor e in garden.getSensors()) { if (!e.getType() && e.getData() != 0) { MapBG.drawOne(e.getId(), e.getName(), e.getX(), e.getY(), MapBG.SensorControllerType.Humidity, true, e.getData(), garden.getIdealHumidity() * (float)1.2, garden.getIdealHumidity() * (float)0.8); } else if (!e.getType() && e.getData() == 0) { MapBG.drawOne(e.getId(), e.getName(), e.getX(), e.getY(), MapBG.SensorControllerType.Humidity, false, garden.getIdealHumidity(), garden.getIdealHumidity() * (float)1.2, garden.getIdealHumidity() * (float)0.8); } } }).Send(); return; }
public static void GetGardenInfo(m_garden garden) { HTTPRequest request_getGarden = new HTTPRequest(new Uri(data.IP + "/getGardenById?id=" + garden.getId()), HTTPMethods.Get, (req_garden, res_garden) => { JsonData json = JsonMapper.ToObject(res_garden.DataAsText); Debug.Log(res_garden.DataAsText); garden.setName((string)json["name"]); garden.setLength((int)json["length"]); garden.setWidth((int)json["width"]); garden.setIdealTemperature(float.Parse(json["idealTemperature"].ToString())); garden.setIdealHumidity(float.Parse(json["idealWetness"].ToString())); }).Send(); }