Skip to content

Commit

Permalink
showcase nodejs client with rest server
Browse files Browse the repository at this point in the history
  • Loading branch information
AviaAv committed Jan 1, 2025
1 parent 397bd7a commit 5e25e3c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ librealsense-log.txt
*.cxx

.vscode/*
/node_modules
75 changes: 75 additions & 0 deletions app.js
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();
23 changes: 23 additions & 0 deletions gen_rest_based_nodejs_sdk.bat
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

0 comments on commit 5e25e3c

Please sign in to comment.