import { CameraDirectionCmd, CameraSmartTrackingOpt, CameraZoomCmd, DeviceFeature, ReturnCode } from "../main"; import { initSdk, YLSdkImpl } from "../main/app" import { DeviceImpl } from "../main/device"; async function sleep(ms: number) { return new Promise((resolve) => { setTimeout(() => { resolve(''); }, ms) }); } async function waitForIdle(deviceImpl: DeviceImpl) { let isInIdle = await deviceImpl.cameraIsInIdleAsync(); while (true) { if (isInIdle) { await sleep(500); break; } } } (async () => { try { // This appId is internal demo used and may log out at any time let ylSdk = await initSdk('05d4af257a5d45c3a18c1e416b37848d'); var deviceCallback = async (deviceImpl: DeviceImpl) => { if ('UVC40' != deviceImpl.deviceName && 'UVC30' != deviceImpl.deviceName) { return; } let feaPtz = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaCameraPTZ); let feaPreset = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaCameraPreset); let feaFaceDectectioin = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaCameraPreset); let feaSmartTracking = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaCameraSmartTracking); console.log(`support camera feature, ptz:${feaPtz}, preset:${feaPreset}, faceDectection:${feaFaceDectectioin}, smartTracking:${feaSmartTracking}`); console.log(`device attached=${deviceImpl.deviceName}`); while (true) { let isReady = await deviceImpl.cameraIsReadyAsync(); if (!isReady) { console.log('device not ready, sleep 1s'); await sleep(1000); } else { break; } } console.log('device is ready'); try { await deviceImpl.cameraPTZResetAsync(); } catch (error) { console.log('cameraPTZResetAsync failed, error: ', error); if (error.code == ReturnCode.Operation_Fail_SmartTracking_Enabled) { console.log('smart tracking and ptz are mutually exclusive, set smart tracking disable'); try { console.log('set smart tracking disable'); await deviceImpl.cameraSetSmartTrackingOptAsync(CameraSmartTrackingOpt.CAMERA_SMART_TRACKING_DISABLE); } catch (error) { console.log('set smart tracking disable failed, error: ', error); } } } try { console.log('del all preset'); await deviceImpl.cameraDelAllPresetAsync(); let presetInfo = await deviceImpl.cameraGetAllPresetAsync(); if (presetInfo.length != 0) { throw new Error(`preset count incorrect, expect:0, actual:${presetInfo.length}`); } } catch (error) { console.log('del all preset failed, error:', error); } // test preset // the app needs to take a screenshot on the camera, and then save picture in local disk try { // View all results on the camera screen console.log('ptz reset'); await deviceImpl.cameraPTZResetAsync(); await waitForIdle(deviceImpl); console.log('start add preset index 0'); await deviceImpl.cameraAddPresetAsync('test path 0'); console.log('zoom in'); await deviceImpl.cameraZoomCtrlAsync(CameraZoomCmd.CAMERA_ZOOM_IN); await sleep(1500); await deviceImpl.cameraZoomCtrlAsync(CameraZoomCmd.CAMERA_ZOOM_STOP); await waitForIdle(deviceImpl); console.log('start add preset index 1'); await deviceImpl.cameraAddPresetAsync('test path 1'); console.log('direction left'); await deviceImpl.cameraDirectionCtrlAsync(CameraDirectionCmd.CAMERA_DIR_LEFT); await sleep(2000); await deviceImpl.cameraDirectionCtrlAsync(CameraDirectionCmd.CAMERA_DIR_STOP); await waitForIdle(deviceImpl); console.log('start add preset index 2'); await deviceImpl.cameraAddPresetAsync('test path 2'); console.log('direction up'); await deviceImpl.cameraDirectionCtrlAsync(CameraDirectionCmd.CAMERA_DIR_UP); await sleep(2000); await deviceImpl.cameraDirectionCtrlAsync(CameraDirectionCmd.CAMERA_DIR_STOP); await waitForIdle(deviceImpl); console.log('start add preset index 3'); await deviceImpl.cameraAddPresetAsync('test path 3'); console.log('direction right'); await deviceImpl.cameraDirectionCtrlAsync(CameraDirectionCmd.CAMERA_DIR_RIGHT); await sleep(1000); await deviceImpl.cameraDirectionCtrlAsync(CameraDirectionCmd.CAMERA_DIR_STOP); await waitForIdle(deviceImpl); console.log('start add preset index 4'); await deviceImpl.cameraAddPresetAsync('test path 4'); console.log('direction down'); await deviceImpl.cameraDirectionCtrlAsync(CameraDirectionCmd.CAMERA_DIR_DOWN); await sleep(1000); await deviceImpl.cameraDirectionCtrlAsync(CameraDirectionCmd.CAMERA_DIR_STOP); await waitForIdle(deviceImpl); console.log('start add preset index 5'); await deviceImpl.cameraAddPresetAsync('test path 5'); console.log('zoom out'); await deviceImpl.cameraZoomCtrlAsync(CameraZoomCmd.CAMERA_ZOOM_OUT); await sleep(1000); await deviceImpl.cameraZoomCtrlAsync(CameraZoomCmd.CAMERA_ZOOM_STOP); await waitForIdle(deviceImpl); console.log('start add preset index 6'); await deviceImpl.cameraAddPresetAsync('test path 6'); let presetInfo = await deviceImpl.cameraGetAllPresetAsync(); console.log('get all preset info: ', JSON.stringify(presetInfo)); if (presetInfo.length != 7) { throw new Error(`preset count incorrect, expect:6, actual:${presetInfo.length}`); } else { for (let index = 0; index < presetInfo.length; index++) { let path = 'test path ' + index.toString(); if (presetInfo[index].pathPic != path) { throw new Error(`preset picture path incorrect, expect:presetInfo[index].pathPic, actual:${path}`); } } } // apply camera preset console.log('apply preset index 0'); await deviceImpl.cameraApplyPresetAsync(0); await waitForIdle(deviceImpl); // update camera preset of index 0 (include position an picture path) console.log('zoom in'); await deviceImpl.cameraZoomCtrlAsync(CameraZoomCmd.CAMERA_ZOOM_IN); await sleep(1500); await deviceImpl.cameraZoomCtrlAsync(CameraZoomCmd.CAMERA_ZOOM_STOP); await waitForIdle(deviceImpl); // update preset of index 0 console.log('update preset of index 0'); deviceImpl.cameraUpdatePresetAsync(0, 'test path update'); presetInfo = await deviceImpl.cameraGetAllPresetAsync(); if (presetInfo.length != 7) { throw new Error(`preset count incorrect, expect:6, actual:${presetInfo.length}`); } if (presetInfo[0].pathPic != 'test path update') { throw new Error(`preset info incorrect, expect path:test path update, actual:${presetInfo[0].pathPic}`); } await sleep(3000); // change camera position console.log('apply preset index 5'); await deviceImpl.cameraApplyPresetAsync(5); await waitForIdle(deviceImpl); // verify that the update take effect() await sleep(3000); console.log('apply preset index 0'); await deviceImpl.cameraApplyPresetAsync(0); await waitForIdle(deviceImpl); try { console.log('del one preset'); await deviceImpl.cameraDelPresetAsync(0); let presetInfo = await deviceImpl.cameraGetAllPresetAsync(); if (presetInfo.length != 6) { throw new Error(`preset count incorrect, expect:6, actual:${presetInfo.length}`); } if (presetInfo[0].pathPic != 'test path 1') { throw new Error(`preset info incorrect after del one, expect path:test path 1, actual:${presetInfo[0].pathPic}`); } } catch (error) { console.log('del preset failed, error:', error); } console.log('preset test completed'); } catch (error) { console.log('ptz control failed, error: ', error); if (error.code == ReturnCode.Preset_Count_Limit) { try { console.log('count of preset has reached the upper limit, del all'); await deviceImpl.cameraDelAllPresetAsync(); let presetInfo = await deviceImpl.cameraGetAllPresetAsync(); if (presetInfo.length != 0) { throw new Error(`preset count incorrect, expect:0, actual:${presetInfo.length}`); } } catch (error) { console.log('del all preset failed, error:', error); } } } // test smart tracking if (feaSmartTracking) { try { console.log('start smart tracking test'); let optInfo = await deviceImpl.cameraGetSmartTrackingOptInfoAsync(); console.log('support opt:', JSON.stringify(optInfo)); for (let index = 0; index < optInfo.length; index++) { const opt = optInfo[index]; await deviceImpl.cameraSetSmartTrackingOptAsync(opt); let optGet = await deviceImpl.cameraGetSamrtTrackingOptAsync(); if (optGet != opt) { throw new Error(`check opt failed, expect:${opt}, actual:${optGet}`); } } console.log('smart tracking test finish'); } catch (error) { throw new Error("smart tracking failed, error: " + error); } } if (feaFaceDectectioin) { try { console.log('start face dectectioin test'); let status = await deviceImpl.cameraIsFaceDetectionEnableAsync(); console.log('face dectectioin status:' + status); console.log('set face dectectioin status:' + !status); await deviceImpl.cameraSetFaceDetectionAsync(!status); console.log('face dectectioin test finish'); } catch (error) { throw new Error("face dectectioin failed, error: " + error); } } console.log('camera test finish.'); ylSdk.disposeAsync(); }; // setting up listeners ylSdk.RegEvent('firstScanDone', () => { console.log('sdk first scan done') }) ylSdk.RegEvent('attach', deviceCallback); ylSdk.RegEvent('detach', (deviceImpl) => { console.log(`device detach, device#${deviceImpl.deviceId} ${deviceImpl.deviceName}`) }) } catch (e) { console.error("excenption: " + e); } })();