forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
showcase nodejs client with rest server
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,3 +89,4 @@ librealsense-log.txt | |
*.cxx | ||
|
||
.vscode/* | ||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const { ApiClient, CameraControlsApi, StatusApi } = require('real_sense_api'); | ||
|
||
// helper class, helps us avoid using promises and callbacks | ||
class CameraApiWrapper { | ||
constructor(basePath = 'http://127.0.0.1:8000') { | ||
const apiClient = new ApiClient(); | ||
apiClient.basePath = basePath; | ||
|
||
// Initialize APIs - wraps every function call | ||
this.status = this._wrapApi(new StatusApi(apiClient)); | ||
this.camera_controls = this._wrapApi(new CameraControlsApi(apiClient)); | ||
} | ||
|
||
_wrapApi(api) { | ||
// Create a proxy to automatically wrap all methods | ||
return new Proxy(api, { | ||
get: (target, prop) => { | ||
const original = target[prop]; | ||
|
||
// all functions are wrapped as 'Promises', so we can await them in our code | ||
if (typeof original === 'function') { | ||
return async (...args) => { | ||
return new Promise((resolve, reject) => { | ||
original.call(target, ...args, (error, data) => { | ||
if (error) reject(error); | ||
else resolve(data); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
// non functions are returned as-is | ||
return original; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
// Usage example | ||
async function manageCamera() { | ||
try { | ||
const camera = new CameraApiWrapper(); | ||
let exposure = await camera.camera_controls.getExposureRGBCameraExposureGetGet(); | ||
console.log("current exposure is", exposure.val); | ||
|
||
const status = await camera.status.getCameraStatusCameraStatusGet(); | ||
console.log(status); | ||
|
||
if (!status.is_on) { | ||
await camera.camera_controls.toggleCameraToggleCameraPost(); | ||
console.log('Camera toggled on.'); | ||
} | ||
if (!status.depth_on) { | ||
await camera.camera_controls.toggleDepthToggleDepthPost(); | ||
console.log('Depth toggled on.'); | ||
} | ||
if (!status.color_on) { | ||
await camera.camera_controls.toggleColorToggleColorPost(); | ||
console.log('Color toggled on.'); | ||
} | ||
|
||
new_exposure = exposure.val === 39 ? 312 : 39; | ||
await camera.camera_controls.setExposureRGBCameraExposurePost(new_exposure); | ||
console.log(`Exposure set to: ${new_exposure}`); | ||
|
||
} catch (error) { | ||
console.error('Error managing camera:', error); | ||
} | ||
} | ||
|
||
// to use this wrapper on other files too | ||
module.exports = CameraApiWrapper; | ||
|
||
// call the example | ||
manageCamera(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@echo off | ||
REM Generate Python SDK from OpenAPI spec | ||
echo.Note: triggering openapi-generator-cli requires installing it using npm | ||
call openapi-generator-cli generate -i http://127.0.0.1:8000/openapi.json -g javascript -o ./nodejs-client | ||
|
||
echo.client generated, installing NodeJS requirements using npm... | ||
|
||
set /p answer="Do you want to delete the folder used for generation? (y/N): " | ||
if /i "%answer%"=="y" ( | ||
echo Folder will be deleted, packing and installing | ||
call npm install .\nodejs-client\ | ||
cd nodejs-client | ||
npm pack | ||
cd .. | ||
REM this might need to be modified for future versions | ||
npm install ./nodejs-client/real_sense_api-1.0.0.tgz | ||
rmdir /s /q .\nodejs-client\ | ||
) else ( | ||
echo Folder won't be deleted, installing from it | ||
call npm install .\nodejs-client\ | ||
) | ||
|
||
echo.setup completed |