Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Camera adapter clean up + fixing warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dipannita-shaw committed Sep 11, 2020
1 parent cad669b commit 65f0eea
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions pnpbridge/src/adapters/src/Camera/CameraIotPnpDeviceAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ CameraIotPnpDeviceAdapter::CameraIotPnpDeviceAdapter(
const std::string& cameraId,
const std::string& componentName) :
m_cameraId(cameraId),
m_componentName(componentName)
m_componentName(componentName),
m_deviceClient(nullptr)
{
}

Expand All @@ -49,7 +50,7 @@ void CameraIotPnpDeviceAdapter::OnCameraArrivalRemoval()
std::lock_guard<std::mutex> lock(m_cameraDeviceLock);
if (m_cameraDiscovery->GetUniqueIdByCameraId(m_cameraId, cameraName) == S_OK && !m_cameraDevice)
{
LogError("Camera device has arrived, starting telemetry");
LogInfo("Camera device has arrived");

// For now, assume:
// - IP camera IDs begin with "uuid"
Expand All @@ -66,35 +67,33 @@ void CameraIotPnpDeviceAdapter::OnCameraArrivalRemoval()

if (m_cameraDevice->Initialize() == S_OK)
{
if (m_hasStartedReporting)
{
m_cameraDevice->StartTelemetryWorker();
}
LogInfo("Camera device with camera name %s initialized successfully", (const char*) cameraName.c_str());
}
else
{
m_cameraDevice = nullptr;
LogInfo("Camera device with camera name %s has already been initialized", (const char*) cameraName.c_str());
}
}
else if (m_cameraDiscovery->GetUniqueIdByCameraId(m_cameraId, cameraName) != S_OK && m_cameraDevice)
{
LogError("Camera device has been removed, stopping telemetry");
LogInfo("Camera device has been removed, stopping telemetry");

m_cameraDevice->StopTelemetryWorker();
m_cameraDevice->Shutdown();
m_cameraDevice.reset();
}
else if (m_cameraDiscovery->GetUniqueIdByCameraId(m_cameraId, cameraName) == S_OK && m_cameraDevice && m_hasStartedReporting)
{
LogInfo("Camera device has started, starting telemetry");
m_cameraDevice->SetIotHubDeviceClientHandle(m_deviceClient);
m_cameraDevice->StartTelemetryWorker();
}
}

HRESULT CameraIotPnpDeviceAdapter::Start()
{
m_hasStartedReporting = true;

std::lock_guard<std::mutex> lock(m_cameraDeviceLock);
if (m_cameraDevice)
{
return m_cameraDevice->StartTelemetryWorker();
}
OnCameraArrivalRemoval();
return S_OK;
}

Expand Down Expand Up @@ -170,15 +169,19 @@ int CameraIotPnpDeviceAdapter::TakePhoto(
int result = PNP_STATUS_SUCCESS;
std::string strResponse;

if (m_hasStartedReporting)
std::lock_guard<std::mutex> lock(m_cameraDeviceLock);
if (m_cameraDevice)
{
m_cameraDevice->StartTelemetryWorker();
}
if (m_hasStartedReporting)
{
m_cameraDevice->StartTelemetryWorker();
}

if (S_OK == m_cameraDevice->TakePhotoOp(strResponse))
{
mallocAndStrcpy_s((char**)CommandResponse, strResponse.c_str());
*CommandResponseSize = strlen(strResponse.c_str());
if (S_OK == m_cameraDevice->TakePhotoOp(strResponse))
{
mallocAndStrcpy_s((char**)CommandResponse, strResponse.c_str());
*CommandResponseSize = strlen(strResponse.c_str());
}
}
else
{
Expand All @@ -197,15 +200,19 @@ int CameraIotPnpDeviceAdapter::TakeVideo(
int result = PNP_STATUS_SUCCESS;
std::string strResponse;

if (m_hasStartedReporting)
std::lock_guard<std::mutex> lock(m_cameraDeviceLock);
if (m_cameraDevice)
{
m_cameraDevice->StartTelemetryWorker();
}
if (m_hasStartedReporting)
{
m_cameraDevice->StartTelemetryWorker();
}

if (S_OK == m_cameraDevice->TakeVideoOp(10000, strResponse))
{
mallocAndStrcpy_s((char**)CommandResponse, strResponse.c_str());
*CommandResponseSize = strlen(strResponse.c_str());
if (S_OK == m_cameraDevice->TakeVideoOp(10000, strResponse))
{
mallocAndStrcpy_s((char**)CommandResponse, strResponse.c_str());
*CommandResponseSize = strlen(strResponse.c_str());
}
}
else
{
Expand All @@ -224,7 +231,9 @@ int CameraIotPnpDeviceAdapter::GetURI(

int result = PNP_STATUS_SUCCESS;
std::string strResponse;
if (S_OK == m_cameraDevice->GetURIOp(strResponse))

std::lock_guard<std::mutex> lock(m_cameraDeviceLock);
if (m_cameraDevice && (S_OK == m_cameraDevice->GetURIOp(strResponse)))
{
mallocAndStrcpy_s((char**)CommandResponse, strResponse.c_str());
*CommandResponseSize = strlen(strResponse.c_str());
Expand All @@ -243,5 +252,4 @@ void CameraIotPnpDeviceAdapter::SetIotHubDeviceClientHandle(
IOTHUB_DEVICE_CLIENT_HANDLE DeviceClientHandle)
{
m_deviceClient = DeviceClientHandle;
m_cameraDevice->SetIotHubDeviceClientHandle(DeviceClientHandle);
}

0 comments on commit 65f0eea

Please sign in to comment.