public async Task <ActionResult> ReqAction() { ViewBag.Version = Global._sfAdminVersion; string jsonString = ""; if (Request.QueryString["action"] != null) { string endPoint = Global._factoryEndPoint; try { RestfulAPIHelper apiHelper = new RestfulAPIHelper(); EmployeeSession empSession = null; if (Session["empSession"] != null) { empSession = EmployeeSession.LoadByJsonString(Session["empSession"].ToString()); } switch (Request.QueryString["action"].ToString().ToLower()) { case "getfactory": endPoint = Global._factoryInCompanyEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("get", endPoint, null); break; case "deletefactory": if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("delete", endPoint, null); Global._sfAppLogger.Info("Delete Factory Completed; Factory ID:" + Request.QueryString["Id"] + ";Login Email:" + empSession.email); break; case "addfactory": { string postData = Request.Form.ToString(); postData = postData + "&CompanyId=" + empSession.companyId; jsonString = await apiHelper.callAPIService("post", endPoint, postData); if (Request.Files.Count > 0) { //Get Object ID from API result dynamic jsonResult = JObject.Parse(jsonString); if (jsonResult.id != null) { string entityID = jsonResult.id; endPoint = endPoint + "/" + entityID + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(endPoint, byteFile, Request.Files[0].FileName); } } Global._sfAppLogger.Info("Add Factory Completed; API Return:" + jsonString + ";Login Email:" + empSession.email); break; } case "updatefactory": { if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } string postData = Request.Form.ToString(); postData = postData + "&CompanyId=" + empSession.companyId; jsonString = await apiHelper.callAPIService("put", endPoint, postData); if (Request.Files.Count > 0) { //admin-api/Factory/{id}/Image endPoint = endPoint + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(endPoint, byteFile, Request.Files[0].FileName); } Global._sfAppLogger.Info("Update Factory Completed; Factory ID:" + Request.QueryString["Id"] + "; post Data:" + postData + ";Login Email:" + empSession.email); break; } default: break; } } catch (Exception ex) { if (ex.Message.ToLower() == "invalid session") { Response.StatusCode = 401; } else { StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex); logMessage.AppendLine("EndPoint:" + endPoint); logMessage.AppendLine("Action:" + Request.QueryString["action"].ToString()); logMessage.AppendLine("PostData:" + Request.Form.ToString()); Global._sfAppLogger.Error(logMessage); Response.StatusCode = 500; jsonString = ex.Message; } } } return(Content(JsonConvert.SerializeObject(jsonString), "application/json")); }
public async Task <ActionResult> ReqAction() { string jsonString = "", postData = "", endPoint = ""; if (Request.QueryString["action"] != null) { try { RestfulAPIHelper apiHelper = new RestfulAPIHelper(); switch (Request.QueryString["action"].ToString().ToLower()) { case "changepassword": endPoint = Global._employeeEndPoint + "/" + Request.QueryString["Id"] + "/changepassword"; postData = Request.Form.ToString(); jsonString = apiHelper.changePassword("put", endPoint, postData); break; case "updateemployee": { endPoint = Global._employeeEndPoint; if (Request.QueryString["Id"] != null) { postData = Request.Form.ToString(); endPoint = endPoint + "/" + Request.QueryString["Id"]; jsonString = await apiHelper.callAPIService("put", endPoint, postData); } if (Request.Files.Count > 0) { //admin-api/Company/{id}/Image var ImageEndPoint = endPoint + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(ImageEndPoint, byteFile, Request.Files[0].FileName); } break; } default: break; } } catch (Exception ex) { if (ex.Message.ToLower() == "invalid session") { Response.StatusCode = 401; } else { StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex); logMessage.AppendLine("EndPoint:" + endPoint); logMessage.AppendLine("Action:" + Request.QueryString["action"].ToString()); logMessage.AppendLine("PostData:" + Request.Form.ToString()); Global._sfAppLogger.Error(logMessage); Response.StatusCode = 500; jsonString = ex.Message; } } } return(Content(JsonConvert.SerializeObject(jsonString), "application/json")); }
public async Task <ActionResult> ReqAction() { string jsonString = "", postData = "", endPoint = ""; if (Request.QueryString["action"] != null) { try { RestfulAPIHelper apiHelper = new RestfulAPIHelper(); string requesterName = Session["firstName"].ToString() + " " + Session["lastName"].ToString(); string requesterEmail = Session["email"].ToString(); switch (Request.QueryString["action"].ToString().ToLower()) { case "genextappkey": string uniqueId = Guid.NewGuid().ToString(); jsonString = "{\"Key\":\"" + System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(uniqueId)) + "\"}"; break; case "getsacredential": var plainTextCredential = System.Text.Encoding.UTF8.GetBytes(Session["email"].ToString() + ":" + Session["password"].ToString()); jsonString = "{\"Credential\":\"" + System.Convert.ToBase64String(plainTextCredential) + "\"}"; break; case "getcompany": endPoint = Global._companyEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("get", endPoint, null); break; case "deletecompany": endPoint = Global._companyEndPoint; if (Request.QueryString["Id"] != null) { /********** Ops Infra By Andy ***********/ int companyId = Int32.Parse(Request.QueryString["Id"]); DocDBCmdMsg docDBCmdMsg = new DocDBCmdMsg("purge", companyId, requesterName, requesterEmail, 0); int operationTaskId = await PostOperationTaskAsync(docDBCmdMsg.task, companyId, docDBCmdMsg.entity, docDBCmdMsg.entityId, docDBCmdMsg.GetJsonInsensitiveContent()); if (operationTaskId > 0) { docDBCmdMsg.taskId = operationTaskId; docDBCmdMsg.SendToServiceBus(); } endPoint = endPoint + "/" + Request.QueryString["Id"]; jsonString = await apiHelper.callAPIService("delete", endPoint, null); } break; case "addcompany": { endPoint = Global._companyEndPoint; postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("post", endPoint, postData); //Get Object ID from API result dynamic jsonResult = JObject.Parse(jsonString); if (jsonResult.id != null) { //jsonResult = company if (Request.Files.Count > 0) { string entityID = jsonResult.id; endPoint = endPoint + "/" + entityID + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(endPoint, byteFile, Request.Files[0].FileName); } /********** Ops Infra By Andy ***********/ int companyId = (int)jsonResult.id; DocDBCmdMsg docDBCmdMsg = new DocDBCmdMsg("create", companyId, requesterName, requesterEmail, 0); int operationTaskId = await PostOperationTaskAsync(docDBCmdMsg.task, companyId, docDBCmdMsg.entity, docDBCmdMsg.entityId, docDBCmdMsg.GetJsonInsensitiveContent()); if (operationTaskId > 0) { docDBCmdMsg.taskId = operationTaskId; docDBCmdMsg.SendToServiceBus(); } } break; } case "updatecompany": { endPoint = Global._companyEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("put", endPoint, postData); if (Request.Files.Count > 0) { //admin-api/Company/{id}/Image endPoint = endPoint + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(endPoint, byteFile, Request.Files[0].FileName); } break; } case "getemployeebycmp": { endPoint = Global._employeeEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/Company/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("get", endPoint, null); break; } case "getuserrolebycmp": { endPoint = Global._userRoleEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/Company/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("get", endPoint, null); break; } case "deleteemployee": { endPoint = Global._employeeEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("delete", endPoint, null); break; } case "addemployee": { endPoint = Global._employeeEndPoint; postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("post", endPoint, postData); dynamic jsonResult = JObject.Parse(jsonString); if (Request.Files.Count > 0) { //Get Object ID from API result if (jsonResult.id != null) { string entityID = jsonResult.id; endPoint = endPoint + "/" + entityID + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(endPoint, byteFile, Request.Files[0].FileName); } } if (jsonResult.id != null && Request.Form["UserRoleId"] != null) { endPoint = endPoint + "/" + jsonResult.id + "/Role"; jsonString = await apiHelper.callAPIService("post", endPoint, postData); } break; } case "updateemployee": { endPoint = Global._employeeEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("put", endPoint, postData); var RoleEndPoint = endPoint + "/Role"; if (Request.Form["UserRoleId"] != null) { jsonString = await apiHelper.callAPIService("put", RoleEndPoint, postData); } else { jsonString = await apiHelper.callAPIService("put", RoleEndPoint, "UserRoleId="); } if (Request.Files.Count > 0) { //admin-api/Company/{id}/Image var ImageEndPoint = endPoint + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(ImageEndPoint, byteFile, Request.Files[0].FileName); } break; } case "resetpassword": endPoint = Global._employeeEndPoint + "/" + Request.QueryString["Id"] + "/resetpassword"; postData = Request.Form.ToString(); jsonString = apiHelper.changePassword("put", endPoint, postData); break; case "getuserrolebyemployeeid": endPoint = Global._employeeEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"] + "/Role"; } jsonString = await apiHelper.callAPIService("get", endPoint, null); break; case "getiothub": { endPoint = Global._iotHubInCompanyEndPoint; if (Request.Form["CompanyId"] != null) { endPoint = endPoint + "/" + Request.Form["CompanyId"]; } jsonString = await apiHelper.callAPIService("get", endPoint, null); break; } case "deleteiothub": { endPoint = Global._iotHubEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("get", endPoint, null); dynamic jsonResult = JObject.Parse(jsonString); if (jsonResult.IoTHubAlias != null) { string IoTHubAlias = jsonResult.IoTHubAlias; string CompanyId = Request.Form["CompanyId"]; /* Send message to OpsInfra to remove IoTHub Receiver */ OpsInfraMessage opsInfraMessage = new OpsInfraMessage("provisioning iothub alias", "IoTHubAlias", IoTHubAlias, "remove iothub alias", 0, Session["firstName"].ToString() + " " + Session["lastName"].ToString(), Session["email"].ToString()); OpsTaskModel opsTask = new OpsTaskModel("Remove IoTHub Receiver", int.Parse(CompanyId), "IoTHubAlias", IoTHubAlias, opsInfraMessage.GetJsonContent()); postData = opsTask.GetPostData(); string taskEndPoint = Global._operationTaskEndPoint; jsonString = await apiHelper.callAPIService("POST", taskEndPoint, postData); jsonResult = JObject.Parse(jsonString); if (jsonResult.id != null) { opsInfraMessage.taskId = jsonResult.id; opsInfraMessage.Send(); } /* Call Restful API to delete */ jsonString = await apiHelper.callAPIService("delete", endPoint, null); } break; } case "addiothub": { endPoint = Global._iotHubEndPoint; postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("post", endPoint, postData); /* Send Message to OpsInfra to launch IoTHub Receiver */ string IoTHubAlias = Request.Form["IoTHubAlias"]; string CompanyId = Request.Form["CompanyId"]; OpsInfraMessage opsInfraMessage = new OpsInfraMessage("provisioning iothub alias", "IoTHubAlias", IoTHubAlias, "create iothub alias", 0, Session["firstName"].ToString() + " " + Session["lastName"].ToString(), Session["email"].ToString()); OpsTaskModel opsTask = new OpsTaskModel("Launch IoTHub Receiver", int.Parse(CompanyId), "IoTHubAlias", IoTHubAlias, opsInfraMessage.GetJsonContent()); postData = opsTask.GetPostData(); string taskEndPoint = Global._operationTaskEndPoint; jsonString = await apiHelper.callAPIService("POST", taskEndPoint, postData); dynamic jsonResult = JObject.Parse(jsonString); if (jsonResult.id != null) { opsInfraMessage.taskId = jsonResult.id; opsInfraMessage.Send(); } break; } case "updateiothub": { endPoint = Global._iotHubEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("put", endPoint, postData); break; } case "addexternaldashboard": { endPoint = Global._externalDashboardEndPoint; //if (Request.QueryString["Id"] != null) // endPoint = endPoint + "/" + Request.QueryString["Id"]; postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("post", endPoint, postData); break; } case "updateexternaldashboard": { endPoint = Global._externalDashboardEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("put", endPoint, postData); break; } case "deleteexternaldashboard": { endPoint = Global._externalDashboardEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("delete", endPoint, postData); break; } case "getallexterngetaldashboard": { endPoint = Global._externalDashboardEndPoint + "/Company/" + Request.QueryString["Id"]; postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("get", endPoint, postData); break; } default: break; } } catch (Exception ex) { if (ex.Message.ToLower() == "invalid session") { Response.StatusCode = 401; } else { StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex); logMessage.AppendLine("EndPoint:" + endPoint); logMessage.AppendLine("Action:" + Request.QueryString["action"].ToString()); logMessage.AppendLine("PostData:" + Request.Form.ToString()); Global._sfAppLogger.Error(logMessage); Response.StatusCode = 500; jsonString = ex.Message; } } } return(Content(JsonConvert.SerializeObject(jsonString), "application/json")); }
public async Task <ActionResult> ReqAction() { ViewBag.Version = Global._sfAdminVersion; string jsonString = "", postData = "", endPoint = ""; try { RestfulAPIHelper apiHelper = new RestfulAPIHelper(); if (Request.QueryString["action"] != null) { switch (Request.QueryString["action"].ToLower()) { case "getequipment": { endPoint = Global._equipmentInCompanyEndPoint; jsonString = await apiHelper.callAPIService("GET", endPoint, null); break; } case "getequipbyfactory": { string factoryId = Request.QueryString["factoryId"]; if (factoryId != null) { if (factoryId.ToLower() == "all") { endPoint = Global._equipmentInCompanyEndPoint; } else { endPoint = Global._equipmentInFactoryEndPoint + "/" + factoryId; } } jsonString = await apiHelper.callAPIService("GET", endPoint, null); break; } case "addequipment": { endPoint = Global._equipmentEndPoint; postData = Request.Form.ToString(); var queryString = HttpUtility.ParseQueryString(postData); if (queryString["Location"] != null && queryString["Location"] == "Factory") { string factoryEndPoint = Global._factoryEndPoint + "/" + queryString["FactoryId"]; string factoryString = await apiHelper.callAPIService("get", factoryEndPoint, null); dynamic factoryResult = JObject.Parse(factoryString); postData = postData + "&Latitude=" + factoryResult.Latitude; postData = postData + "&Longitude=" + factoryResult.Longitude; } jsonString = await apiHelper.callAPIService("post", endPoint, postData); dynamic jsonResult = JObject.Parse(jsonString); if (Request.Files.Count > 0) { if (jsonResult.id != null) { string entityID = jsonResult.id; endPoint = endPoint + "/" + entityID + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(endPoint, byteFile, Request.Files[0].FileName); } if (Request.Files.Count > 0) { //admin-api/Company/{id}/Image var ImageEndPoint = endPoint + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(ImageEndPoint, byteFile, Request.Files[0].FileName); } } break; } case "deleteequipment": { endPoint = Global._equipmentEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("delete", endPoint, postData); break; } case "updateequipment": { endPoint = Global._equipmentEndPoint; if (Request.QueryString["Id"] != null) { postData = Request.Form.ToString(); var queryString = HttpUtility.ParseQueryString(postData); if (queryString["Location"] != null && queryString["Location"] == "Factory") { string factoryEndPoint = Global._factoryEndPoint + "/" + queryString["FactoryId"]; string factoryString = await apiHelper.callAPIService("get", factoryEndPoint, null); dynamic factoryResult = JObject.Parse(factoryString); postData = postData + "&Latitude=" + factoryResult.Latitude; postData = postData + "&Longitude=" + factoryResult.Longitude; } endPoint = endPoint + "/" + Request.QueryString["Id"]; jsonString = await apiHelper.callAPIService("put", endPoint, postData); if (Request.Files.Count > 0) { //admin-api/Company/{id}/Image var ImageEndPoint = endPoint + "/Image"; byte[] byteFile = new byte[Request.Files[0].InputStream.Length]; Request.Files[0].InputStream.Read(byteFile, 0, (int)Request.Files[0].InputStream.Length); jsonString = await apiHelper.putUploadFile(ImageEndPoint, byteFile, Request.Files[0].FileName); } // Update Meta-Data string metaDataPost = ""; string[] phrases = postData.Split('&'); foreach (var input in phrases) { if (input.StartsWith("metaDatas")) { metaDataPost = metaDataPost + input + "&"; } } if (!string.IsNullOrEmpty(metaDataPost)) { var MetaDataEndPoint = endPoint + "/MetaData"; jsonString = await apiHelper.callAPIService("put", MetaDataEndPoint, metaDataPost); } } break; } case "getmetadata": { endPoint = Global._equipmentEndPoint; if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"] + "/MetaData"; } jsonString = await apiHelper.callAPIService("GET", endPoint, null); break; } } } } catch (Exception ex) { if (ex.Message.ToLower() == "invalid session") { Response.StatusCode = 401; } else { StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex); logMessage.AppendLine("EndPoint:" + endPoint); logMessage.AppendLine("Action:" + Request.QueryString["action"].ToString()); logMessage.AppendLine("PostData:" + Request.Form.ToString()); System.Diagnostics.Debug.Print("EndPoint:" + endPoint); System.Diagnostics.Debug.Print("Action:" + Request.QueryString["action"].ToString()); System.Diagnostics.Debug.Print("PostData:" + Request.Form.ToString()); System.Diagnostics.Debug.Print("Message:" + ex.Message); Global._sfAppLogger.Error(logMessage); Response.StatusCode = 500; jsonString = ex.Message; } } return(Content(JsonConvert.SerializeObject(jsonString), "application/json")); }