예제 #1
0
        public IHttpActionResult EditFormData(string id, [FromBody] IoTDeviceModels.Update iotDevice)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            string logForm          = "Form : " + js.Serialize(iotDevice);
            string logAPI           = "[Put] " + Request.RequestUri.ToString();

            if (!ModelState.IsValid || iotDevice == null)
            {
                Startup._sfAppLogger.Warn(logAPI + " || Input Parameter not expected || " + logForm);
                return(BadRequest("Invalid data"));
            }

            try
            {
                IoTDeviceModels iotDeviceModel = new IoTDeviceModels();
                iotDeviceModel.updateIoTDevice(id, iotDevice);
                return(Ok("Success"));
            }
            catch (Exception ex)
            {
                StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
                logMessage.AppendLine(logForm);
                Startup._sfAppLogger.Error(logAPI + logMessage);

                return(InternalServerError(ex));
            }
        }
예제 #2
0
        public IHttpActionResult UpdateDesiredProperty(string deviceId, [FromBody] IoTDeviceModels.Update_Desired desired)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            string logForm          = "Form : " + js.Serialize(desired);
            string logAPI           = "[Put] " + Request.RequestUri.ToString();

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                JObject         desiredProperty = JObject.Parse(desired.DeviceTwinsDesired);
                IoTDeviceModels iotDeviceModel  = new IoTDeviceModels();
                iotDeviceModel.updateIoTDeviceDesired(deviceId, desiredProperty);

                return(Ok());
            }
            catch (Exception ex)
            {
                StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
                Startup._sfAppLogger.Error(logAPI + logMessage);

                return(InternalServerError(ex));
            }
        }
예제 #3
0
        public IHttpActionResult AddFormData([FromBody] IoTDeviceModels.Add iotDevice)
        {
            string logForm = "Form : " + Startup._jsSerializer.Serialize(iotDevice);
            string logAPI  = "[Post] " + Request.RequestUri.ToString();

            if (!ModelState.IsValid || iotDevice == null)
            {
                Startup._sfAppLogger.Warn(logAPI + " || Input Parameter not expected || " + logForm);
                return(BadRequest("Invalid data"));
            }

            try
            {
                IoTDeviceModels iotDeviceModel = new IoTDeviceModels();
                iotDeviceModel.addIoTDevice(iotDevice);
                return(Ok());
            }
            catch (Exception ex)
            {
                StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
                logMessage.AppendLine(logForm);
                Startup._sfAppLogger.Error(logAPI + logMessage);

                return(InternalServerError(ex));
            }
        }
예제 #4
0
        public async System.Threading.Tasks.Task <IHttpActionResult> SendC2DMessageAsync(string iotDeviceId)
        {
            try
            {
                int companyId = GetCompanyIdFromToken();
                if (!General.IsIoTDeviceUnderCompany(iotDeviceId, companyId))
                {
                    return(Unauthorized());
                }

                string payload = await Request.Content.ReadAsStringAsync();

                //Retrieve IoTHub Connection String
                IoTDeviceModels iotDeviceModel         = new IoTDeviceModels();
                IoTHubModels    iotHubModel            = new IoTHubModels();
                string          IoTHubAlias            = iotDeviceModel.getIoTDeviceById(iotDeviceId).IoTHubAlias;
                string          IoTHubConnectionString = iotHubModel.getIoTHubById(IoTHubAlias).P_IoTHubConnectionString;

                //Send out Cloud to Device Message
                ServiceClient serviceClient  = ServiceClient.CreateFromConnectionString(IoTHubConnectionString);
                var           commandMessage = new Message(Encoding.ASCII.GetBytes(payload));
                await serviceClient.SendAsync(iotDeviceId, commandMessage);

                return(Ok());
            }
            catch (Exception ex)
            {
                StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
                string        logAPI     = "[Put] " + Request.RequestUri.ToString();
                Startup._sfAppLogger.Error(logAPI + logMessage);
                return(InternalServerError(ex));
            }
        }
예제 #5
0
        public IHttpActionResult GetIoTDeciceById(string id)
        {
            IoTDeviceModels iotDeviceModel = new IoTDeviceModels();

            try
            {
                return(Ok(iotDeviceModel.getIoTDeviceById(id)));
            }
            catch
            {
                return(NotFound());
            }
        }
예제 #6
0
        public IHttpActionResult GetAllMessageTemplateByDeviceId(string deviceId)
        {
            IoTDeviceModels iotDeviceModels                = new IoTDeviceModels();
            IoTDeviceMessageCatalogModels iotDMCModels     = new IoTDeviceMessageCatalogModels();
            MessageCatalogModels          msgCatalogModels = new MessageCatalogModels();

            List <object> objectList  = new List <object>();
            var           msgCatalogs = iotDMCModels.GetAllMessageCatalogByIoTDeviceId(deviceId);

            foreach (var msgCatalog in msgCatalogs)
            {
                objectList.Add(msgCatalogModels.GetMessageCatalogTemplate(msgCatalog.MessageCatalogId, deviceId));
            }
            return(Ok(objectList));
        }
예제 #7
0
 public IHttpActionResult Delete(string id)
 {
     try
     {
         IoTDeviceModels iotDeviceModel = new IoTDeviceModels();
         iotDeviceModel.deleteIoTDevice(id);
         return(Ok("Success"));
     }
     catch (Exception ex)
     {
         string        logAPI     = "[Delete] " + Request.RequestUri.ToString();
         StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
         Startup._sfAppLogger.Error(logAPI + logMessage);
         return(InternalServerError());
     }
 }
예제 #8
0
        public IHttpActionResult GetIoTDeviceById(string iotDeviceId)
        {
            try
            {
                int companyId = GetCompanyIdFromToken();
                if (!General.IsIoTDeviceUnderCompany(iotDeviceId, companyId))
                {
                    return(Unauthorized());
                }

                IoTDeviceModels iotDeviceModel = new IoTDeviceModels();
                return(Ok(iotDeviceModel.getIoTDeviceByIdReadonly(iotDeviceId)));
            }
            catch (Exception ex)
            {
                StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
                string        logAPI     = "[Get] " + Request.RequestUri.ToString();
                Startup._sfAppLogger.Error(logAPI + logMessage);

                return(InternalServerError());
            }
        }
예제 #9
0
        public IHttpActionResult GetAllIoTDeciceByFactoryId(int factoryId)
        {
            IoTDeviceModels iotDeviceModel = new IoTDeviceModels();

            return(Ok(iotDeviceModel.GetAllIoTDeviceByFactoryId(factoryId)));
        }
예제 #10
0
        public IHttpActionResult GetAllIoTDeciceByCompanyId(int companyId)
        {
            IoTDeviceModels iotDeviceModel = new IoTDeviceModels();

            return(Ok(iotDeviceModel.GetAllIoTDeviceByCompanyId(companyId)));
        }