public async Task <ActionResult> ReqAction() { string jsonString = "", postData = "", endPoint = ""; if (Request.QueryString["action"] != null) { try { EmployeeSession empSession = null; if (Session["empSession"] != null) { empSession = EmployeeSession.LoadByJsonString(Session["empSession"].ToString()); } RestfulAPIHelper apiHelper = new RestfulAPIHelper(); string requesterName = empSession.firstName + " " + empSession.lastName; string requesterEmail = empSession.email; int companyId = empSession.companyId; switch (Request.QueryString["action"].ToString().ToLower()) { case "getiotdevice": { endPoint = Global._iotDeviceInCompanyEndPoint; jsonString = await apiHelper.callAPIService("GET", endPoint, null); break; } case "getiotdevicebyfactory": { string factoryId = Request.QueryString["factoryId"]; if (factoryId != null) { if (factoryId.ToLower() == "all") { endPoint = Global._iotDeviceInCompanyEndPoint; } else { endPoint = Global._iotDeviceInFactoryEndPoint + "/" + factoryId; } } jsonString = await apiHelper.callAPIService("GET", endPoint, null); break; } case "addiotdevice": { //add endPoint = Global._iotDeviceEndPoint; postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("POST", endPoint, postData); System.Diagnostics.Debug.Print(endPoint); System.Diagnostics.Debug.Print(postData); /********** Send OpsInfra command message ***********/ string iotHubAlias = Request.Form["IoTHubAlias"]; string iotHubDeviceId = Request.Form["IoTHubDeviceId"]; string authType = Request.Form["AuthenticationType"]; string ceritficateId = Request.Form["DeviceCertificateId"]; IoTDeviceCmdMsg deviceCmdMsg = new IoTDeviceCmdMsg("create iothub register", iotHubDeviceId, authType, requesterName, requesterEmail, 0); await deviceCmdMsg.Init(iotHubAlias, ceritficateId); int operationTaskId = await PostOperationTaskAsync(deviceCmdMsg.task, companyId, deviceCmdMsg.entity, deviceCmdMsg.entityId, deviceCmdMsg.GetJsonInsensitiveContent()); if (operationTaskId > 0) { deviceCmdMsg.taskId = operationTaskId; deviceCmdMsg.SendToServiceBus(); } break; } case "updateiotdevice": { if (Request.QueryString["Id"] != null) { endPoint = Global._iotDeviceEndPoint + "/" + Request.QueryString["Id"]; } postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("GET", endPoint, null); dynamic oldIoTDeviceObj = JObject.Parse(jsonString); jsonString = await apiHelper.callAPIService("PUT", endPoint, postData); /********** Send OpsInfra command message ***********/ dynamic newIoTDeviceObj = new JObject(); string newIoTHubDeviceId = Request.Form["IoTHubDeviceId"]; string newAuthenticationType = Request.Form["AuthenticationType"]; string newIoTHubAlias = Request.Form["IoTHubAlias"]; string newDeviceCertificateId = Request.Form["DeviceCertificateId"]; string oldIoTHubDeviceId = oldIoTDeviceObj.IoTHubDeviceId; string oldAuthenticationType = oldIoTDeviceObj.AuthenticationType; string oldIoTHubAlias = oldIoTDeviceObj.IoTHubAlias; string oldDeviceCertificateId = oldIoTDeviceObj.DeviceCertificateId; IoTDeviceCmdMsg deviceCmdMsg = new IoTDeviceCmdMsg("update iothub register", newIoTHubDeviceId, newAuthenticationType, requesterName, requesterEmail, 0); if (oldIoTHubAlias == newIoTHubAlias) { await deviceCmdMsg.Init(newIoTHubAlias, newDeviceCertificateId); } else { await deviceCmdMsg.Init(newIoTHubAlias, newDeviceCertificateId, oldIoTHubAlias); } int operationTaskId = await PostOperationTaskAsync(deviceCmdMsg.task, companyId, deviceCmdMsg.entity, deviceCmdMsg.entityId, deviceCmdMsg.GetJsonInsensitiveContent()); if (operationTaskId > 0) { deviceCmdMsg.taskId = operationTaskId; deviceCmdMsg.SendToServiceBus(); } break; } case "resetpassword": { if (Request.QueryString["Id"] != null) { endPoint = Global._iotDeviceEndPoint + "/" + Request.QueryString["Id"] + "/" + "ResetPassword"; } postData = Request.Form.ToString(); jsonString = apiHelper.changePassword("PUT", endPoint, postData); break; } case "deleteiotdevice": { if (Request.QueryString["Id"] != null) { endPoint = Global._iotDeviceEndPoint + "/" + Request.QueryString["Id"]; } /********** Send OpsInfra command message ***********/ dynamic existingIoTDeviceObj = JObject.Parse(await apiHelper.callAPIService("GET", endPoint, null)); string iotHubAlias = existingIoTDeviceObj.IoTHubAlias; string iotHubDeviceId = existingIoTDeviceObj.IoTHubDeviceId; string authType = existingIoTDeviceObj.AuthenticationType; IoTDeviceCmdMsg deviceCmdMsg = new IoTDeviceCmdMsg("remove iothub register", iotHubDeviceId, authType, requesterName, requesterEmail, 0); await deviceCmdMsg.Init(iotHubAlias); int operationTaskId = await PostOperationTaskAsync(deviceCmdMsg.task, companyId, deviceCmdMsg.entity, deviceCmdMsg.entityId, deviceCmdMsg.GetJsonInsensitiveContent()); if (operationTaskId > 0) { deviceCmdMsg.taskId = operationTaskId; deviceCmdMsg.SendToServiceBus(); } //Delete data in DB jsonString = await apiHelper.callAPIService("DELETE", endPoint, null); break; } //attach message case "getattachedmessagebydeviceid": { if (Request.QueryString["Id"] != null) { endPoint = Global._iotDeviceEndPoint + "/" + Request.QueryString["Id"] + "/Message"; } jsonString = await apiHelper.callAPIService("GET", endPoint, null); break; } case "updateattachedmessage": { if (Request.QueryString["Id"] != null) { endPoint = Global._iotDeviceEndPoint + "/" + Request.QueryString["Id"] + "/Message"; } postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("PUT", endPoint, postData); break; } case "getdeviceconfiguration": { if (Request.QueryString["Id"] != null) { endPoint = Global._iotDeviceEndPoint + "/" + Request.QueryString["Id"] + "/Configuration"; } jsonString = await apiHelper.callAPIService("GET", endPoint, null); break; } case "updatedeviceconfiguration": { string iotHubDeviceId = Request.QueryString["Id"]; if (iotHubDeviceId != null) { endPoint = Global._iotDeviceEndPoint + "/" + iotHubDeviceId + "/DesiredProperty"; } postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("PUT", endPoint, postData); System.Diagnostics.Debug.Print(endPoint); System.Diagnostics.Debug.Print(postData); /********** Send OpsInfra command message ***********/ string deviceConfiguration = Request.Form["devicetwinsdesired"]; IoTDeviceManagementCmdMsg cmdMsg = new IoTDeviceManagementCmdMsg("update device desired property", iotHubDeviceId, requesterName, requesterEmail, 0, deviceConfiguration); await cmdMsg.Init(); int operationTaskId = await PostOperationTaskAsync(cmdMsg.task, companyId, cmdMsg.entity, cmdMsg.entityId, cmdMsg.GetJsonInsensitiveContent()); if (operationTaskId > 0) { cmdMsg.taskId = operationTaskId; cmdMsg.SendToServiceBus(); } break; } case "downloadmessagetemplate": { if (Request.QueryString["Id"] != null) { endPoint = Global._iotDeviceEndPoint + "/" + Request.QueryString["Id"] + "/MessageTemplate"; } HttpResponseMessage response = new HttpResponseMessage(); jsonString = await apiHelper.callAPIService("GET", endPoint, null); 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()); 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")); }
public async Task <ActionResult> ReqAction() { string jsonString = ""; if (Request.QueryString["action"] != null) { string endPoint = Global._superAdminEndPoint; try { RestfulAPIHelper apiHelper = new RestfulAPIHelper(); switch (Request.QueryString["action"].ToString().ToLower()) { case "getsuperadmin": if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("get", endPoint, null); break; case "deletesuperadmin": if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } jsonString = await apiHelper.callAPIService("delete", endPoint, null); break; case "addsuperadmin": { string postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("post", endPoint, postData); break; } case "updatesuperadmin": { //admin-api/SuperAdmin/{id}/ if (Request.QueryString["Id"] != null) { endPoint = endPoint + "/" + Request.QueryString["Id"]; } string postData = Request.Form.ToString(); jsonString = await apiHelper.callAPIService("put", endPoint, postData); break; } case "changepassword": { endPoint = Global._superAdminEndPoint + "/" + Request.QueryString["Id"] + "/changepassword"; string postData = Request.Form.ToString(); jsonString = apiHelper.changePassword("put", 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")); }
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")); }