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));
            }
        }
Exemple #2
0
        public IHttpActionResult GetIoTDeciceById(string id)
        {
            IoTDeviceModels iotDeviceModel = new IoTDeviceModels();

            try
            {
                return(Ok(iotDeviceModel.getIoTDeviceById(id)));
            }
            catch
            {
                return(NotFound());
            }
        }