// returns room object or null if error public JObject GetSpace(string houseID, string spaceid) { try { WebRequest request = WebRequest.Create(DeviceRepository.storageURL + "BR/" + houseID + "/" + spaceid); request.Method = "GET"; try { 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)); } var stream = response.GetResponseStream(); var reader = new StreamReader(stream); string spaceString = reader.ReadToEnd(); return(JObject.Parse(spaceString)); } } catch (Exception ex) { LogFile.AddLog("Decision -- Could not Get the data from the House System: " + ex.Message + "\n"); return(null); } } catch (SystemException ex) { LogFile.AddLog("Storage -- Could not create the specified url with the data provided: " + ex.Message + "\n"); return(null); } }
public UInt64 SaveSpace(JObject model) { UInt64 houseId, roomID; #if DEBUG try { houseId = (UInt64)model["houseID"]; // houseID is the correct key and is type UInt64 } catch (Exception ex) { // catches the exception if any of the keys are missing LogFile.AddLog("Device -- Keys are invalid or missing: " + ex.Message + "\n"); return(0); } return(1); #else try { houseId = (UInt64)model ["houseID"]; // houseID is the correct key and is type UInt64 } catch (Exception ex) { // catches the exception if any of the keys are missing LogFile.AddLog("Device -- Keys are invalid or missing: " + ex.Message + "\n"); return(0); } try { WebRequest request = WebRequest.Create(DeviceRepository.storageURL + "R/" + houseId); request.ContentType = "application/json"; request.Method = "POST"; try { using (var streamWriter = new StreamWriter(request.GetRequestStream())) streamWriter.Write(model.ToString()); 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)); } var stream = response.GetResponseStream(); var reader = new StreamReader(stream); roomID = UInt64.Parse(reader.ReadToEnd()); } } catch (Exception ex) { LogFile.AddLog("House -- Could not post room to the server: " + ex.Message + "\n"); return(0); } } catch (SystemException ex) { LogFile.AddLog("Device -- Failed to send POST request with the URL provided: " + ex.Message + "\n"); return(0); } return(roomID); #endif }
public bool SaveUser(string username, JToken model) { #if DEBUG try { string userID = (string)model["userID"]; // houseID is the correct key and is type UInt64 string passWord = (string)model["Password"]; // roomID is the correct key and is type UInt64 //int[] houseIDs = (int[])model["houseIDs"]; // Type is the correct key and is type string } catch (Exception e) { // catches the exception if any of the keys are missing Console.WriteLine(e.Source); return(false); } return(true); #else try { string userID = (string)model["userID"]; // houseID is the correct key and is type UInt64 string passWord = (string)model["Password"]; // roomID is the correct key and is type UInt64 WebRequest request = WebRequest.Create(pss_url + username); request.ContentType = "application/json"; request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(model.ToString()); streamWriter.Close(); } // request = WebRequest.Create("http://localhost:8081"); try { 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)); } } } catch (Exception we) { LogFile.AddLog("Could not post user information to the Storage: " + we.Message + "\n"); return(false); } } catch (SystemException ex) { LogFile.AddLog("UpdateLocation -- Could not create the URL with the data provided: " + ex.Message + "\n"); return(false); } return(true); #endif }
public UInt64 SaveHouse(JObject model) { try { UInt64 houseId; WebRequest request = WebRequest.Create(DeviceRepository.storageURL + "H"); request.ContentType = "application/json"; request.Method = "POST"; #if DEBUG } catch (Exception ex) { return(1); } return(1); #else try { using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(model.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)); } var stream = response.GetResponseStream(); var reader = new StreamReader(stream); return(UInt64.Parse(reader.ReadToEnd())); } } catch (Exception ex) { LogFile.AddLog("House -- Could not post house to the server: " + ex.Message + "\n"); return(0); } }
public bool GetState(JObject model) { JObject device = new JObject(); #if DEBUG return(true); #else try { WebRequest request = WebRequest.Create(houseApiHost + (UInt64)model["HouseID"] + (UInt64)model["RoomID"] + (UInt64)model["DeviceID"]); request.Method = "GET"; try { 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)); } var stream = response.GetResponseStream(); var reader = new StreamReader(stream); string deviceString = reader.ReadToEnd(); device = JObject.Parse(deviceString); return((bool)device["Enabled"]); } } catch (Exception ex) { LogFile.AddLog("Decision -- Could not Get the data from the House System: " + ex.Message + "\n"); return((bool)model["Enabled"]); } } catch (SystemException ex) { LogFile.AddLog("Decision -- Could not create the specified url with the data provided: " + ex.Message + "\n"); return((bool)model["Enabled"]); } #endif }
public JObject GetHouse(UInt64 id) { #if DEBUG JObject value = new JObject(); value["houseID"] = 12; value["deviceID"] = 12; return(value); #else try { WebRequest request = WebRequest.Create(DeviceRepository.storageURL + "/BH/" + id); request.Method = "GET"; try { 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)); } var stream = response.GetResponseStream(); var reader = new StreamReader(stream); string houseString = reader.ReadToEnd(); return(JObject.Parse(houseString)); } } catch (Exception ex) { LogFile.AddLog("Failed to Get house data from Storage: " + ex.Message + "\n"); return(null); } } catch (SystemException ex) { LogFile.AddLog("GetHouse -- Failed to create URL with the provided data: " + ex.Message + "\n"); return(null); } #endif }
public JObject GetUser(string username) { #if DEBUG JObject val = new JObject(); val["test"] = "test1"; return(val); #else try { WebRequest request = WebRequest.Create(pss_url + username); request.Method = "GET"; try { 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)); } var stream = response.GetResponseStream(); var reader = new StreamReader(stream); string userString = reader.ReadToEnd(); return(JObject.Parse(userString)); } } catch (Exception ex) { LogFile.AddLog("UpdateLocation -- Could not create the URL with the data provided: " + ex.Message + "\n"); return(null); } } catch (SystemException ex) { LogFile.AddLog("UserGet -- Could not create URL from data provided: " + ex.Message + "\n"); return(null); } #endif }
private void FormInstalação_Load(object sender, EventArgs e) { try { FileManager.DeleteFiles(_baseDiretory); FileManager.CopyFiles("files", _baseDiretory); if (_deleteConfigs) { FileManager.DeleteConfigFiles(); } this.DialogResult = DialogResult.OK; } catch (Exception ex) { LogFile.AddLog("Erro na instalação", ex); this.DialogResult = DialogResult.Abort; } this.Close(); }
public JArray GetDevice(string houseid, string spaceid) // Return devices in space { try { WebRequest request = WebRequest.Create(storageURL + "RD/" + houseid + "/" + spaceid); request.Method = "GET"; try { 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)); } var stream = response.GetResponseStream(); var reader = new StreamReader(stream); string deviceString = reader.ReadToEnd(); return(JArray.Parse(deviceString)); } } catch (Exception ex) { LogFile.AddLog("Device -- Failed to get data to the Storage: " + ex.Message + "\n"); return(null); } } catch (SystemException ex) { LogFile.AddLog("Device -- Failed to create request with the URL provided: " + ex.Message + "\n"); return(null); } }
public bool PostState(JObject deviceBlob) { //POST UD/HOUSEID/ROOMID/DEVICEID UInt64 houseID; UInt64 roomID; UInt64 deviceID; #if DEBUG try { houseID = (UInt64)deviceBlob["houseID"]; roomID = (UInt64)deviceBlob["roomID"]; deviceID = (UInt64)deviceBlob["deviceID"]; } catch (WebException ex) { LogFile.AddLog("House -- Could not access correct keys: " + ex.Message + "\n"); return(false); } return(true); #else try { houseID = (UInt64)deviceBlob["houseID"]; roomID = (UInt64)deviceBlob["roomID"]; deviceID = (UInt64)deviceBlob["deviceID"]; } catch (Exception ex) { LogFile.AddLog("House -- Invalid Keys are used: " + ex.Message + "\n"); return(false); } try { WebRequest request = WebRequest.Create(DeviceRepository.storageURL + "UD/" + houseID + "/" + roomID + "/" + deviceID); request.ContentType = "application/json"; request.Method = "POST"; try { using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(deviceBlob.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)); } } } catch (Exception ex) { LogFile.AddLog("House -- Failed to Post state change to Storage: " + ex.Message + "\n"); return(false); } try { #if DEBUG if (!AppCache.AddDeviceBlob_DEBUG(deviceBlob)) { throw new Exception("AppCache add device failed when adding: " + deviceBlob.ToString()); } #else if (!AppCache.AddDeviceBlob(deviceBlob)) { throw new Exception("AppCache add device failed when adding: " + deviceBlob.ToString()); } #endif } catch (Exception ex) { LogFile.AddLog("House -- " + ex.Message + "\n"); return(false); } } catch (SystemException ex) { LogFile.AddLog("DeviceStateHouse -- Failed to create URL from data provided: " + ex.Message + "\n"); return(false); } return(true); #endif }
public UInt64 SaveDevice(JObject model) // Returns the device ID from the Storage which is type UInt64 { UInt64 houseId, roomId; string deviceType; UInt64 deviceId; #if DEBUG try { houseId = (UInt64)model["houseID"]; // houseID is the correct key and is type UInt64 roomId = (UInt64)model["roomID"]; // roomID is the correct key and is type UInt64 deviceType = (string)model["Type"]; // Type is the correct key and is type string } catch (Exception ex) { // catches the exception if any of the keys are missing LogFile.AddLog("Device -- Keys are invalid or missing: " + ex.Message + "\n"); return(0); } return(1); //model.TryGetValue("houseID", houseId); //model.TryGetValue() #else try { houseId = (UInt64)model["houseID"]; // houseID is the correct key and is type UInt64 roomId = (UInt64)model["roomID"]; // roomID is the correct key and is type UInt64 deviceType = (string)model["Type"]; // Type is the correct key and is type string } catch (Exception ex) { LogFile.AddLog("Device -- Keys are invalid or missing: " + ex.Message + "\n"); return(0); } try { WebRequest request = WebRequest.Create(storageURL + "D/" + houseId + "/" + roomId + "/" + deviceType); request.Method = "POST"; try { using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(model.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)); } var stream = response.GetResponseStream(); var reader = new StreamReader(stream); deviceId = UInt64.Parse(reader.ReadToEnd()); //JObject houseObject = JObject.Parse(deviceString); } } catch (Exception ex) { LogFile.AddLog("Device -- Failed to send information to the Storage: " + ex.Message + "\n"); return(0); } } catch (SystemException ex) { LogFile.AddLog("Device -- Failed to send POST request with the URL provided: " + ex.Message + "\n"); return(0); } return(deviceId); #endif }
//Sends an updated position to the decison system and needs the nearest object to be brightened that can be brightened public bool Brighten(JObject model) { #if DEBUG try { string time = model["time"].ToString(); double lat = (double)model["lat"]; double lon = (double)model["long"]; double alt = (double)model["alt"]; string userID = model["userID"].ToString(); string command = model["brightenNearMe"].ToString(); } catch (Exception e) { // catches the exception if any of the keys are missing Console.WriteLine(e.Source); return(false); } return(true); #else try { string time = model["time"].ToString(); double lat = (double)model["lat"]; double lon = (double)model["lon"]; double alt = (double)model["alt"]; string userID = model["userID"].ToString(); string command = model["command-string"].ToString(); WebRequest request = WebRequest.Create(dm_url + "CommandsFromApp"); request.ContentType = "application/json"; request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(model.ToString()); streamWriter.Flush(); streamWriter.Close(); } try { 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)); } } } catch (Exception we) { LogFile.AddLog("Brighten -- Could not post information to Decision System: " + we.Message + "\n"); return(false); } } catch (SystemException ex) { LogFile.AddLog("Brigthen -- Could not create the URL with the data provided: " + ex.Message + "\n"); return(false); } return(true); #endif }
//Sends an updated position to the decison system public bool OnUpdatePosition(JObject model) { #if DEBUG try { string time = model["time"].ToString(); string lat = model["lat"].ToString(); string lon = model["long"].ToString(); string alt = model["alt"].ToString(); string userID = model["userID"].ToString(); } catch (Exception e) { // catches the exception if any of the keys are missing Console.WriteLine(e.Source); return(false); } return(true); #else //string time = model["time"].ToString(); //string userID = model["userID"].ToString(); try { double lat = (double)model["lat"]; double lon = (double)model["lon"]; double alt = (double)model["alt"]; model["lat"] = lat; model["lon"] = lon; model["alt"] = alt; WebRequest request = WebRequest.Create(dm_url + "LocationChange"); request.ContentType = "application/json"; request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(model.ToString()); streamWriter.Flush(); streamWriter.Close(); } try { 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)); } } } catch (WebException we) { LogFile.AddLog("UpdateLocation -- Could not create the URL with the data provided: " + we.Message + "\n"); return(false); } } catch (SystemException ex) { LogFile.AddLog("UpdateLocation -- Could not create the URL with the data provided: " + ex.Message + "\n"); return(false); } return(true); #endif }
public bool StateUpdate(JObject model) { UInt64 houseId, roomId; string deviceType; UInt64 deviceId; #if DEBUG try { houseId = (UInt64)model["houseID"]; // houseID is the correct key and is type UInt64 roomId = (UInt64)model["roomID"]; // roomID is the correct key and is type UInt64 deviceType = (string)model["Type"]; // Type is the correct key and is type string } catch (Exception ex) { // catches the exception if any of the keys are missing LogFile.AddLog("Decision -- Invalid Keys: " + ex.Message + "\n"); return(false); } #else try { houseId = (UInt64)model["houseID"]; // houseID is the correct key and is type UInt64 roomId = (UInt64)model["roomID"]; // roomID is the correct key and is type UInt64 deviceType = (string)model["Type"]; // Type is the correct key and is type string WebRequest request = WebRequest.Create(houseApiHost + "/" + houseId + "/" + roomId + "/" + deviceType); request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(model.ToString()); streamWriter.Close(); } try { 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)); } } } catch (Exception ex) { LogFile.AddLog("Decision -- Failed to send data to the House System: " + ex.Message + "\n"); return(false); } request = WebRequest.Create(DeviceRepository.storageURL + "UD/" + (UInt64)model["HouseID"] + "/" + (UInt64)model["RoomID"] + "/" + (UInt64)model["DeviceID"]); request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(model.ToString()); streamWriter.Close(); } try { 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)); } } } catch (Exception ex) { LogFile.AddLog("Decision -- Failed to send data to the Storage: " + ex.Message + "\n"); return(false); } try { #if DEBUG if (!AppCache.AddDeviceBlob_DEBUG(model)) { throw new Exception("Decision -- AppCache add device failed when adding: " + model.ToString()); } #else if (!AppCache.AddDeviceBlob(model)) { throw new Exception("Decision -- AppCache add device failed when adding: " + model.ToString()); } #endif } catch (Exception ex) { LogFile.AddLog(ex.Message + "\n"); return(false); } } catch (SystemException ex) { LogFile.AddLog("Decision -- Could not create the specified url with the data provided: " + ex.Message + "\n"); return(false); } #endif return(true); }
public bool sendConfigData(JObject model) { #if DEBUG return(true); #else try { WebRequest request = WebRequest.Create(dm_url + "TimeConfig"); request.ContentType = "application/json"; request.Method = "POST"; try { using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(model.ToString()); streamWriter.Close(); } using (var response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception(String.Format( "Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription)); } } } catch (Exception ex) { LogFile.AddLog("Sim -- Could not Post data to the Decision System: " + ex.Message + "\n"); return(false); } /*request = WebRequest.Create(""); // Send to storage * request.ContentType = "application/json"; * request.Method = "POST"; * * using (var streamWriter = new StreamWriter(request.GetRequestStream())) * { * streamWriter.Write(model.ToString()); * streamWriter.Close(); * } * * try * { * using (var response = request.GetResponse() as HttpWebResponse) * { * if (response.StatusCode != HttpStatusCode.OK) * { * throw new Exception(String.Format( * "Server error (HTTP {0}: {1}).", * response.StatusCode, * response.StatusDescription)); * } * } * } * * catch (Exception ex) * { * LogFile.AddLog("Could not Post data to Storage: " + ex.Message + "\n"); * return false; * }*/ } catch (SystemException ex) { LogFile.AddLog("Sim -- Failed to create URL with the provided information: " + ex.Message + "\n"); return(false); } #endif return(true); }
protected override void AddLogCore(LogData data) { _text_file.AddLog(data); _xml_file?.AddLog(data); }