UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

244 lines (243 loc) 10.2 kB
import { z } from 'zod'; import { paramToUrl } from './internal/utils'; import { blackListSchema, cameraSettingsSchema, flightInfoSchema, getIcaoSchema, mapInfoSchema, priorityListSchema, serverSettingsSchema, trackingModeSchema, whiteListSchema, wsAliasResponseSchema, zonesSchema, } from './types/PlaneTrackerAPI'; import { CannotSetCoordsInAutoModeError, ImportSettingsError, InvalidAltitudeError, InvalidLatLngError, ParsingBlobError, ResetCalibrationError, ServerError, BadRequestError, ErrorWithResponse, } from './errors/errors'; import { ProxyClient } from './internal/ProxyClient'; import { cameraListSchema } from './types/GenetecAgent'; const BASE_PATH = '/local/planetracker'; export class PlaneTrackerAPI { client; apiUser; constructor(client, apiUser) { this.client = client; this.apiUser = apiUser; } static getProxyPath = () => `${BASE_PATH}/proxy.cgi`; static getWsEventsPath = () => `${BASE_PATH}/package/ws`; 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 z.boolean().parse(res.state); } async serverRunCheck(options) { const agent = this.getClient(options?.proxyParams); return await agent.get({ path: `${BASE_PATH}/package/serverRunCheck.cgi`, timeout: options?.timeout }); } async getLiveViewAlias(rtspUrl, options) { const agent = this.getClient(options?.proxyParams); const res = await agent.get({ path: `${BASE_PATH}/getLiveViewAlias.cgi`, parameters: { rtsp_url: rtspUrl }, timeout: options?.timeout, }); return wsAliasResponseSchema.parse(await res.json()); } async resetPtzCalibration(options) { const agent = this.getClient(options?.proxyParams); const res = await agent.get({ path: `${BASE_PATH}/package/resetPtzCalibration.cgi`, parameters: this.apiUser, timeout: options?.timeout, }); if (!res.ok) { throw new ResetCalibrationError('PTZ', res); } } async resetFocusCalibration(options) { const agent = this.getClient(options?.proxyParams); const res = await agent.get({ path: `${BASE_PATH}/package/resetFocusCalibration.cgi`, parameters: this.apiUser, timeout: options?.timeout, }); if (!res.ok) { throw new ResetCalibrationError('FOCUS', res); } } async fetchCameraSettings(options) { const res = await this._getJson(`${BASE_PATH}/package_camera_settings.cgi`, { action: 'get' }, options); return cameraSettingsSchema.parse(res); } async setCameraSettings(settings, options) { return await this._postJsonEncoded(`${BASE_PATH}/package_camera_settings.cgi`, settings, { action: 'set', }, options); } async fetchServerSettings(options) { const res = await this._getJson(`${BASE_PATH}/package_server_settings.cgi`, { action: 'get' }, options); return serverSettingsSchema.parse(res); } async exportAppSettings(dataType, options) { return await this._getBlob(`${BASE_PATH}/package_data.cgi`, { action: 'EXPORT', dataType }, options); } async importAppSettings(dataType, formData, options) { const agent = this.getClient(options?.proxyParams); const res = await agent.post({ path: `${BASE_PATH}/package_data.cgi`, data: formData, parameters: { action: 'IMPORT', dataType }, timeout: options?.timeout, }); if (!res.ok) { throw new ImportSettingsError(res); } } async fetchFlightInfo(icao, options) { const res = await this._getJson(`${BASE_PATH}/package/flightInfo.cgi`, { icao }, options); return flightInfoSchema.parse(res); } async getTrackingMode(options) { const res = await this._getJson(`${BASE_PATH}/package/getTrackingMode.cgi`, undefined, options); return trackingModeSchema.parse(res); } async setTrackingMode(mode, options) { await this._postJsonEncoded(`${BASE_PATH}/package/setTrackingMode.cgi`, { mode }, this.apiUser, options); } async startTrackingPlane(icao, options) { const agent = this.getClient(options?.proxyParams); await agent.get({ path: `${BASE_PATH}/package/trackIcao.cgi`, parameters: { icao, ...this.apiUser }, timeout: options?.timeout, }); } async stopTrackingPlane(options) { const agent = this.getClient(options?.proxyParams); await agent.get({ path: `${BASE_PATH}/package/resetIcao.cgi`, parameters: this.apiUser, timeout: options?.timeout, }); } async getIcao(by, value, options) { const res = await this._getJson(`${BASE_PATH}/package/getIcao.cgi`, { [by]: value }, options); return getIcaoSchema.parse(res).icao; } async getPriorityList(options) { const res = await this._getJson(`${BASE_PATH}/package/getPriorityList.cgi`, undefined, options); return priorityListSchema.parse(res).priorityList; } async setPriorityList(priorityList, options) { return await this._postJsonEncoded(`${BASE_PATH}/package/setPriorityList.cgi`, { priorityList }, this.apiUser, options); } async getWhiteList(options) { const res = await this._getJson(`${BASE_PATH}/package/getWhiteList.cgi`, undefined, options); return whiteListSchema.parse(res).whiteList; } async setWhiteList(whiteList, options) { return await this._postJsonEncoded(`${BASE_PATH}/package/setWhiteList.cgi`, { whiteList }, this.apiUser, options); } async getBlackList(options) { const res = await this._getJson(`${BASE_PATH}/package/getBlackList.cgi`, undefined, options); return blackListSchema.parse(res).blackList; } async setBlackList(blackList, options) { return await this._postJsonEncoded(`${BASE_PATH}/package/setBlackList.cgi`, { blackList }, this.apiUser, options); } async fetchMapInfo(options) { const res = await this._getJson(`${BASE_PATH}/package/getMapInfo.cgi`, undefined, options); return mapInfoSchema.parse(res); } async getZones(options) { const res = await this._getJson(`${BASE_PATH}/package/getZones.cgi`, undefined, options); return zonesSchema.parse(res); } async setZones(zones, options) { await this._postJsonEncoded(`${BASE_PATH}/package/setZones.cgi`, zones, this.apiUser, options); } async goToCoordinates(lat, lon, alt, options) { const agent = this.getClient(options?.proxyParams); const res = await agent.get({ path: `${BASE_PATH}/package/goToCoordinates.cgi`, parameters: { lat, lon, alt, ...this.apiUser }, timeout: options?.timeout, }); if (!res.ok) { if (res.status === 400 && res.statusText === 'Cannot set coordinates in automatic mode') { throw new CannotSetCoordsInAutoModeError(); } if (res.status === 400 && res.statusText === 'Invalid lat/lon parameters') { throw new InvalidLatLngError(); } if (res.status === 400 && res.statusText === 'Invalid alt parameter') { throw new InvalidAltitudeError(); } if (res.status === 400) { throw new BadRequestError(res); } if (res.status === 500) { throw new ServerError(); } } } async checkGenetecConnection(params, options) { return await this._postUrlEncoded(`${BASE_PATH}/package/checkGenetecConnection.cgi`, params, options); } async getGenetecCameraList(params, options) { const res = await this._postUrlEncoded(`${BASE_PATH}/package/getGenetecCameraList.cgi`, params, options); return cameraListSchema.parse(await res.json()); } async _getJson(path, parameters, options, headers) { const agent = this.getClient(options?.proxyParams); const res = await agent.get({ path, parameters, timeout: options?.timeout, headers }); if (res.ok) { return await res.json(); } else { throw new ErrorWithResponse(res); } } async _getBlob(path, parameters, options) { const agent = this.getClient(options?.proxyParams); const res = await agent.get({ path, parameters, timeout: options?.timeout }); if (res.ok) { return await this.parseBlobResponse(res); } else { throw new ErrorWithResponse(res); } } async parseBlobResponse(response) { try { return (await response.blob()); } catch (err) { throw new ParsingBlobError(err); } } async _postJsonEncoded(path, data, parameters, options) { const agent = this.getClient(options?.proxyParams); const jsonData = JSON.stringify(data); const res = await agent.post({ path, data: jsonData, parameters, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, timeout: options?.timeout, }); if (res.ok) { return res; } else { throw new ErrorWithResponse(res); } } async _postUrlEncoded(path, params, options) { const data = paramToUrl(params); const agent = this.getClient(options?.proxyParams); const res = await agent.post({ path, data, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, timeout: options?.timeout, }); if (res.ok) { return res; } else { throw new ErrorWithResponse(res); } } }