use of com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class EdgeConnectorForKVSService method main.
public static void main(String[] args) throws InterruptedException {
log.info("---------- EdgeConnectorForKVS Starting ----------");
boolean retry = true;
boolean isInitialSetup = true;
List<String> restartNeededConfigurationList = new ArrayList<>();
do {
hubSiteWiseAssetId = args[Constants.ARG_INDEX_SITE_WISE_ASSET_ID_FOR_HUB];
log.info("EdgeConnectorForKVS Hub Asset Id: " + hubSiteWiseAssetId);
final EdgeConnectorForKVSService edgeConnectorForKVSService = new EdgeConnectorForKVSService();
try {
// set up shared configurations first
if (isInitialSetup) {
edgeConnectorForKVSService.setUpSharedEdgeConnectorForKVSService();
restartNeededConfigurationList.addAll(edgeConnectorForKVSConfigurationList.stream().map(EdgeConnectorForKVSConfiguration::getKinesisVideoStreamName).collect(Collectors.toList()));
isInitialSetup = false;
}
if (restartNeededConfigurationList == null || restartNeededConfigurationList.isEmpty()) {
throw new EdgeConnectorForKVSUnrecoverableException("Unable to initialize component");
}
// initialize or re-init camera level configurations
edgeConnectorForKVSConfigurationList.forEach(configuration -> {
if (restartNeededConfigurationList.contains(configuration.kinesisVideoStreamName)) {
edgeConnectorForKVSService.setUpCameraLevelEdgeConnectorForKVSService(configuration);
}
});
// clear this configuration list after each restart or initial setup
restartNeededConfigurationList.clear();
// block main thread and regularly check camera level health status
while (true) {
for (EdgeConnectorForKVSConfiguration configuration : edgeConnectorForKVSConfigurationList) {
if (configuration.getFatalStatus().get()) {
log.info("fatal status found for " + configuration.getKinesisVideoStreamName());
restartNeededConfigurationList.add(configuration.kinesisVideoStreamName);
}
}
if (!restartNeededConfigurationList.isEmpty()) {
// fatal status was set, throw an exception to trigger restart
throw new EdgeConnectorForKVSException("Fatal error reported");
}
Thread.sleep(WAIT_TIME_BEFORE_POLLING_IN_MILLISECS);
}
} catch (final EdgeConnectorForKVSException ex) {
log.error("Start kicking off camera level restart: " + "Failed {}: {}", ex.getClass().getName(), ex.getMessage());
} catch (final EdgeConnectorForKVSUnrecoverableException ex) {
log.error("Unrecoverable exception caught, please re-deploy or restart the Component." + "ERROR: Failed {}: {}", ex.getClass().getName(), ex.getMessage());
retry = false;
} catch (final Exception ex) {
log.error("Uncaught exception found, please re-deploy or restart the Component." + "ERROR: Failed {}: {}", ex.getClass().getName(), ex.getMessage());
retry = false;
}
// clear fatalStatus and clean up failed cameras
clearFatalStatus();
edgeConnectorForKVSConfigurationList.forEach(configuration -> {
if (restartNeededConfigurationList.contains(configuration.kinesisVideoStreamName)) {
edgeConnectorForKVSService.cleanUpEdgeConnectorForKVSService(configuration);
}
});
if (retry) {
// If the fatalStatus is true after camera level restart, re-initializing the component
boolean isFatalStatusSet = edgeConnectorForKVSConfigurationList.stream().anyMatch(configuration -> configuration.getFatalStatus().get());
if (isFatalStatusSet) {
log.info("---------- Component Re-initializing ----------");
edgeConnectorForKVSService.cleanUpEdgeConnectorForKVSService(null);
restartNeededConfigurationList.clear();
clearFatalStatus();
isInitialSetup = true;
} else {
log.info("---------- Camera Re-starting ----------");
// wait for a bit before restarting
}
Thread.sleep(WAIT_TIME_BEFORE_RESTART_IN_MILLISECS);
}
} while (retry);
}
use of com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class EdgeConnectorForKVSService method schedulerStopTaskCallback.
@Override
public void schedulerStopTaskCallback(Constants.@NonNull JobType jobType, @NonNull String streamName) {
log.info("Stop Scheduled Task - " + jobType.name() + " Stream: " + streamName);
final EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration = edgeConnectorForKVSConfigurationMap.get(streamName);
if (jobType == Constants.JobType.LIVE_VIDEO_STREAMING) {
try {
stopLiveVideoStreaming(edgeConnectorForKVSConfiguration);
} catch (IOException ex) {
log.error("Exception on schedulerStopTaskCallback - stopLiveVideoStreaming: " + ex.getMessage());
edgeConnectorForKVSConfiguration.getFatalStatus().set(true);
}
} else if (jobType == Constants.JobType.LOCAL_VIDEO_CAPTURE) {
stopRecordingJob(edgeConnectorForKVSConfiguration);
}
}
use of com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class EdgeConnectorForKVSService method initConfiguration.
private void initConfiguration() throws IOException {
// Pull and init needed camera configurations from SiteWise
edgeConnectorForKVSConfigurationList = siteWiseManager.initEdgeConnectorForKVSServiceConfiguration(hubSiteWiseAssetId);
for (EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration : edgeConnectorForKVSConfigurationList) {
generateRecordingPath(edgeConnectorForKVSConfiguration);
edgeConnectorForKVSConfiguration.setProcessLock(new ReentrantLock());
edgeConnectorForKVSConfiguration.setRecordingRequestsCount(0);
edgeConnectorForKVSConfiguration.setLiveStreamingRequestsCount(0);
edgeConnectorForKVSConfiguration.setFatalStatus(new AtomicBoolean(false));
}
edgeConnectorForKVSConfigurationMap = edgeConnectorForKVSConfigurationList.stream().collect(Collectors.toMap(EdgeConnectorForKVSConfiguration::getKinesisVideoStreamName, Function.identity()));
}
use of com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class SiteWiseManager method initEdgeConnectorForKVSServiceConfiguration.
/**
* Init EdgeConnectorForKVS Service configuration.
* This method will call SiteWise service, query the asset property value to init the EdgeConnectorForKVS config.
* @param hubDeviceSiteWiseAssetId The SiteWise asset Id for EdgeConnectorForKVS hub device.
* @throws EdgeConnectorForKVSUnrecoverableException when there are issues querying SiteWise service
* @return List of EdgeConnectorForKVSConfiguration
*/
public List<EdgeConnectorForKVSConfiguration> initEdgeConnectorForKVSServiceConfiguration(String hubDeviceSiteWiseAssetId) {
try {
verifyConfiguration(hubDeviceSiteWiseAssetId);
List<String> cameraSiteWiseAssetIdList = siteWiseClient.listAssociatedAssets(hubDeviceSiteWiseAssetId);
if (cameraSiteWiseAssetIdList.size() > 0) {
return cameraSiteWiseAssetIdList.stream().map(cameraSiteWiseAssetId -> {
try {
return buildEdgeConnectorForKVSConfiguration(cameraSiteWiseAssetId);
} catch (Exception e) {
throw new EdgeConnectorForKVSException(e);
}
}).collect(Collectors.toList());
} else {
final String warnMessage = String.format("Could not find any camera asset under the given hub assetId." + " Please check the provided hub device SiteWise assetId: %s", hubDeviceSiteWiseAssetId);
log.warn(warnMessage);
return new ArrayList<>();
}
} catch (Exception e) {
final String errorMessage = String.format("Failed to init EdgeConnectorForKVS component. " + "Please check the provided hub device SiteWise assetId: %s", hubDeviceSiteWiseAssetId);
log.error(errorMessage, e);
throw new EdgeConnectorForKVSUnrecoverableException(errorMessage, e);
}
}
use of com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.
the class FileHandlingCallBack method handleWatchEvent.
// Thread safe method
// 1. add new added file to the recordedFilesMap in DiskManagerUtil
// 2. compute video recording period timestamp, then update SiteWise with new added file time stamp
public void handleWatchEvent(@NonNull Path directoryPath, @NonNull WatchEvent<?> event, @NonNull DiskManagerUtil diskManagerUtil) {
synchronized (diskManagementLock) {
Path videoFilePath = directoryPath.resolve((Path) event.context());
diskManagerUtil.appendRecordFileToDirPath(directoryPath, videoFilePath);
EdgeConnectorForKVSConfiguration edgeConnectorForKVSConfiguration = diskManagerUtil.getEdgeConnectorForKVSConfigurationFromPath(directoryPath);
StreamManager streamManager = edgeConnectorForKVSConfiguration.getStreamManager();
if (streamManager != null) {
streamManager.pushData(edgeConnectorForKVSConfiguration.getSiteWiseAssetId(), edgeConnectorForKVSConfiguration.getVideoRecordedTimeRangePropertyId(), (double) VideoRecordVisitor.getDateFromFilePath(videoFilePath).toInstant().getEpochSecond(), Optional.of(diskManagerUtil.getVideoRecordingPeriodStartTime(directoryPath, videoFilePath)));
}
}
}
Aggregations