public async Task <int> PostOperationTaskAsync(string name, int companyId, string entity, string entityId, string taskcontent) { OpsTaskModel opsTaskModel = new OpsTaskModel(name, companyId, entity, entityId, taskcontent); RestfulAPIHelper apiHelper = new RestfulAPIHelper(); string opsTaskEndPoint = Global._operationTaskEndPoint; string opsTaskPostData = opsTaskModel.GetPostData(); dynamic opsTaskObject = JObject.Parse(await apiHelper.callAPIService("post", opsTaskEndPoint, opsTaskPostData)); return(opsTaskObject.id); }
public async Task <ActionResult> ReqAction() { string jsonString = "", postData = "", endPoint = ""; int companyId; if (Request.QueryString["mAction"] != null) { try { RestfulAPIHelper apiHelper = new RestfulAPIHelper(); switch (Request.QueryString["mAction"].ToString().ToLower()) { case "iothubreceiver": { companyId = int.Parse(Request.QueryString["companyId"].ToString()); string subAction = Request.QueryString["sAction"].ToString(); string IoTHubAlias = Request.Form["iotHubAlias"].ToString(); endPoint = Global._operationTaskEndPoint; if (subAction.ToLower() == "launch iothub receiver") { 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(subAction, companyId, "IoTHubAlias", IoTHubAlias, opsInfraMessage.GetJsonContent()); postData = opsTask.GetPostData(); jsonString = await apiHelper.callAPIService("POST", endPoint, postData); dynamic jsonResult = JObject.Parse(jsonString); if (jsonResult.id != null) { opsInfraMessage.taskId = jsonResult.id; opsInfraMessage.Send(); } } else if (subAction.ToLower() == "restart iothub receiver") { IoTHubEventProcessTopic iotHubTopic = new IoTHubEventProcessTopic("Restart", IoTHubAlias, 0, Session["firstName"].ToString() + " " + Session["lastName"].ToString(), Session["email"].ToString()); OpsTaskModel opsTask = new OpsTaskModel(subAction, companyId, "IoTHubAlias", IoTHubAlias, iotHubTopic.GetJsonContent()); postData = opsTask.GetPostData(); jsonString = await apiHelper.callAPIService("POST", endPoint, postData); dynamic jsonResult = JObject.Parse(jsonString); if (jsonResult.id != null) { iotHubTopic.taskId = jsonResult.id; iotHubTopic.Send(); } } break; } case "getrunningtask": { endPoint = Global._operationTaskSearchEndPoint; string q = "?"; if (Request.QueryString["taskstatus"] != null) { endPoint = endPoint + q + "taskstatus=" + Request.QueryString["taskstatus"]; q = "&"; } if (Request.QueryString["hours"] != null) { endPoint = endPoint + q + "hours=" + Request.QueryString["hours"]; } jsonString = await apiHelper.callAPIService("GET", endPoint, postData); break; } case "getusagelogsumbyday": { endPoint = Global._usageLogSumByDayEndPoint; string q = "?"; if (Request.QueryString["day"] != null) { endPoint = endPoint + q + "days=" + Request.QueryString["day"]; q = "&"; } endPoint = endPoint + q + "order=desc"; 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() { 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")); }