UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

77 lines (76 loc) 3.22 kB
import { ProxyClient } from './internal/ProxyClient'; import { responseStringify } from './internal/utils'; import { cameraTimeResponseSchema, nodeStateSchema, packageInfoListSchema, cameraStorageSchema, } from './types/CamScripterAPI'; import { networkCameraListSchema } from './types/common'; const BASE_PATH = '/local/camscripter'; export class CamScripterAPI { client; constructor(client) { this.client = client; } static getProxyPath = () => `${BASE_PATH}/proxy.cgi`; getClient(proxyParams) { return proxyParams ? new ProxyClient(this.client, proxyParams) : this.client; } async checkCameraTime(options) { const res = await this._getJson(`${BASE_PATH}/camera_time.cgi`, undefined, options); return cameraTimeResponseSchema.parse(res).state; } async getNetworkCameraList(options) { const res = await this._getJson(`${BASE_PATH}/network_camera_list.cgi`, undefined, options); return networkCameraListSchema.parse(res.camera_list); } async getStorageInfo(options) { const res = await this._getJson(`${BASE_PATH}/package/get_storage.cgi`, undefined, options); return cameraStorageSchema.parse(res); } async getPackageList(options) { const res = await this._getJson(`${BASE_PATH}/package/list.cgi`, undefined, options); return packageInfoListSchema.parse(res); } async installPackages(formData, storage, options) { await this._post(`${BASE_PATH}/package/install.cgi`, formData, { storage: storage }, options); } async uninstallPackage(packageId, options) { await this._getJson(`${BASE_PATH}/package/remove.cgi`, { package_name: packageId }, options); } async importSettings(packageId, formData, options) { await this._post(`${BASE_PATH}/package/data.cgi`, formData, { action: 'IMPORT', package_name: packageId, }, options); } async exportSettings(packageId, formData, options) { await this._post(`${BASE_PATH}/package/data.cgi`, formData, { action: 'EXPORT', package_name: packageId, }, options); } async getNodejsStatus(options) { const res = await this._getJson(`${BASE_PATH}/diagnostics.cgi`, undefined, options); return nodeStateSchema.parse(res); } async installNodejs(storage, options) { await this._getJson(`${BASE_PATH}/node_update.cgi`, { storage: storage }, options); } async _getJson(path, parameters, options) { const agent = this.getClient(options?.proxyParams); const res = await agent.get({ path, parameters, timeout: options?.timeout }); if (res.ok) { return await res.json(); } else { throw new Error(await responseStringify(res)); } } async _post(path, data, parameters, options, headers) { const agent = this.getClient(options?.proxyParams); const res = await agent.post({ path, data, parameters, headers, timeout: options?.timeout }); if (res.ok) { return await res.json(); } else { throw new Error(await responseStringify(res)); } } }