public async void ThreadProc()
        {
            try
            {
                string exceptionString              = "";
                DBHelper._IoTDevice dbhelper        = new DBHelper._IoTDevice();
                DBHelper._IoTHub    dbhelper_iotHub = new DBHelper._IoTHub();
                IoTDevice           iotDevice       = dbhelper.GetByid(_IoTHubDeviceId);
                int deviceConfigurationStatus       = iotDevice.DeviceConfigurationStatus;
                switch (_Action)
                {
                case "update deired":
                    exceptionString += await UpdateDeviceConfigurationToTwins(_PrimaryIothubConnectionString, "primary");

                    //exceptionString += await UpdateDeviceConfigurationToTwins(_SecondaryIothubConnectionString, "secondary");
                    if (_Status != TaskStatus.FAILED)
                    {
                        dbhelper.UpdateDeviceConfigurationStatusAndProperty(_IoTHubDeviceId, IoTDeviceConfigurationStatus.WAITING_DEVICE_ACK);
                    }
                    break;

                case "update db device reported":
                {
                    Thread.Sleep(30000);
                    string iotHubConnectionString = "";

                    //Get IoT Hub Device Twins Reported Value
                    IoTHub iotHub = dbhelper_iotHub.GetByid(_IoTHubAlias);
                    if (_IoTHubIsPrimary)
                    {
                        iotHubConnectionString = iotHub.P_IoTHubConnectionString;
                    }
                    else
                    {
                        iotHubConnectionString = iotHub.S_IoTHubConnectionString;
                    }

                    string reportedObjJsonString = await GetDeviceTwinsReportedValue(iotHubConnectionString, _IoTHubDeviceId);

                    Console.WriteLine(reportedObjJsonString);

                    //update db
                    dbhelper.UpdateDeviceConfigurationStatusAndProperty(_IoTHubDeviceId, IoTDeviceConfigurationStatus.RECEIVE_DEVICE_ACK, null, reportedObjJsonString);

                    return;
                    //recode to operationTask
                }
                break;
                }

                if (exceptionString == "")
                {
                    Program.UpdateTaskBySuccess(_TaskId);
                    Console.WriteLine("[DeviceManagement] Apply device configuration to IoTHub desired property success: IoTHubDeviceId-" + _IoTHubDeviceId);
                }
                else
                {
                    throw new Exception(exceptionString);
                }
            }
            catch (Exception ex)
            {
                StringBuilder logMessage = new StringBuilder();
                logMessage.AppendLine("[DeviceManagement] Apply device configuration to IoTHub desired property faild: IoTHubDeviceId-" + _IoTHubDeviceId);
                logMessage.AppendLine("\tMessage:" + JsonConvert.SerializeObject(this));
                logMessage.AppendLine("\tException:" + ex.Message);
                Program._sfAppLogger.Error(logMessage);
                Program.UpdateTaskByFail(_TaskId, Program.FilterErrorMessage(ex.Message), _Status);
                Console.WriteLine(logMessage);
                _Status = 0;
            }
        }
예제 #2
0
 public void updateIoTDeviceDesired(string iotHubDeviceId, JObject desiredProperty)
 {
     DBHelper._IoTDevice dbhelp = new DBHelper._IoTDevice();
     dbhelp.UpdateDeviceConfigurationStatusAndProperty(iotHubDeviceId, 0, JsonConvert.SerializeObject(desiredProperty));
 }