public Thermostat GetThermostat() { var id = new FullID(0, 0, 2); const string HouseString = "{\"house_url\":\"http://127.0.0.1:8081\"}"; const string DeviceString = "{\"ID\": 2, \"Class\": \"Thermostat\"}"; var dev_out = ServerSideAPI.CreateDevice(id, HouseString, DeviceString, _frame); Assert.IsNotNull(dev_out); Assert.AreEqual(dev_out.Input.GetType(), typeof(HouseInput)); Assert.AreEqual(dev_out.Output.GetType(), typeof(HouseOutput)); return(dev_out as Thermostat); }
public void SetLightTest() { var id = new FullID(0, 0, 0); const string HouseString = "{\"house_url\":\"http://127.0.0.1:8081\"}"; const string DeviceString = "{\"ID\": 0, \"Class\": \"LightSwitch\"}"; var dev_out = ServerSideAPI.CreateDevice(id, HouseString, DeviceString, _frame); Assert.IsNotNull(dev_out); var ls = (LightSwitch)dev_out; Assert.IsNotNull(ls); ls.Enabled = true; Assert.IsTrue(ls.Enabled); Assert.IsTrue(ls.UpdateOk); Assert.AreEqual(ls.Value.Brightness, 1.0); }
public bool GetState(JObject model) { #if DEBUG return(true); #else // parse the JSON UInt64 houseID = (UInt64)model["houseID"]; UInt64 roomID = (UInt64)model["roomID"]; UInt64 deviceID = (UInt64)model["deviceID"]; string deviceClass = (string)model["deviceClass"]; string houseURL = (string)model["houseURL"]; // done parsing JSON TimeFrame frame = new TimeFrame(); var id = new FullID(); id.HouseID = houseID; id.RoomID = roomID; id.DeviceID = deviceID; JObject jHouseString = new JObject(); jHouseString["houseURL"] = houseURL; string HouseString = jHouseString.ToString(); JObject jDeviceString = new JObject(); jDeviceString["ID"] = Convert.ToString(deviceID); jDeviceString["Class"] = deviceClass; string DeviceString = jDeviceString.ToString(); var dev_out = ServerSideAPI.CreateDevice(id, HouseString, DeviceString, frame); if (dev_out != null) //Good to go { //state changed means some value was changed, e.g. the command was not idempotent var ok = dev_out.update(); var json = JsonConvert.SerializeObject(dev_out); //this JSON blob should be sent to whomever wants updates AppCache.AddDeviceBlob(JObject.Parse(json)); } return(true); #endif }
public bool updateSimulation(UInt64 houseID, UInt64 roomID, UInt64 deviceID, JObject sendData) { // UInt64 test = fullId.HouseID; #if DEBUG #else HouseRepository houseRepo = new HouseRepository(); JObject houseObj = new JObject(); JObject deviceObj = new JObject(); String HouseString, DeviceString, commands; var id = new FullID(); id.HouseID = houseID; id.DeviceID = deviceID; id.RoomID = roomID; WebRequest request = WebRequest.Create(storageURL + "UD/" + houseID + "/" + roomID + "/" + deviceID); request.ContentType = "application/json"; request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(sendData.ToString()); streamWriter.Flush(); streamWriter.Close(); } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception(String.Format( "Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription)); } } houseObj = houseRepo.GetHouse(houseID); // queries PS for information deviceObj = GetDevice(houseID.ToString(), roomID.ToString(), deviceID.ToString()); JToken blobVal; //String blob = (string)deviceArr["blob"]; //blobVal = JToken.Parse(blob); String house_url = (string)(houseObj["house_url"]); UInt64 ID = (UInt64)(deviceObj["ID"]); String clas = (string)(deviceObj["Class"]); JObject send = new JObject(); JObject send1 = new JObject(); send["house_url"] = house_url; send1["ID"] = ID; send1["Class"] = clas; TimeFrame frame = new TimeFrame(); HouseString = JsonConvert.SerializeObject(send); DeviceString = JsonConvert.SerializeObject(send1); commands = JsonConvert.SerializeObject(sendData); var dev_out = ServerSideAPI.CreateDevice(id, HouseString, DeviceString, frame); if (dev_out != null) //Good to go { //state changed means some value was changed, e.g. the command was not idempotent var state_changed = Interfaces.UpdateDevice(dev_out, commands); } #endif return(true); }