UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

191 lines (190 loc) 8.7 kB
import { z } from 'zod'; import { blackListSchema, cameraSettingsSchema, flightInfoSchema, getIcaoSchema, mapInfoSchema, priorityListSchema, serverSettingsSchema, trackingModeSchema, typePriorityListSchema, whiteListSchema, wsAliasResponseSchema, zonesSchema, } from './types/PlaneTrackerAPI'; import { CannotSetCoordsInAutoModeError, ImportSettingsError, InvalidAltitudeError, InvalidLatLngError, ResetCalibrationError, ServerError, BadRequestError, } from './errors/errors'; import { cameraListSchema } from './types/GenetecAgent'; import { BasicAPI } from './internal/BasicAPI'; const BASE_PATH = '/local/planetracker'; export class PlaneTrackerAPI extends BasicAPI { apiUser; constructor(client, apiUser) { super(client); this.apiUser = apiUser; } static getProxyPath = () => `${BASE_PATH}/proxy.cgi`; static getWsEventsPath = () => `${BASE_PATH}/package/ws`; async checkAPIAvailable(options) { await this._getJson(`${BASE_PATH}/api_check.cgi`, undefined, options); } 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 getTypePriorityList(options) { const res = await this._getJson(`${BASE_PATH}/package/getTypePriorityList.cgi`, undefined, options); return typePriorityListSchema.parse(res).typePriorityList; } async setTypePriorityList(typePriorityList, options) { return await this._postJsonEncoded(`${BASE_PATH}/package/setTypePriorityList.cgi`, { typePriorityList }, 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(); } } } downloadReport(options) { return this._getText(`${BASE_PATH}/report.cgi`, undefined, options); } 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()); } }