UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

248 lines (247 loc) 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlaneTrackerAPI = void 0; const zod_1 = require("zod"); const utils_1 = require("./internal/utils"); const PlaneTrackerAPI_1 = require("./types/PlaneTrackerAPI"); const errors_1 = require("./errors/errors"); const ProxyClient_1 = require("./internal/ProxyClient"); const GenetecAgent_1 = require("./types/GenetecAgent"); const BASE_PATH = '/local/planetracker'; 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_1.ProxyClient(this.client, proxyParams) : this.client; } async checkCameraTime(options) { const res = await this._getJson(`${BASE_PATH}/camera_time.cgi`, undefined, options); return zod_1.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 PlaneTrackerAPI_1.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 errors_1.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 errors_1.ResetCalibrationError('FOCUS', res); } } async fetchCameraSettings(options) { const res = await this._getJson(`${BASE_PATH}/package_camera_settings.cgi`, { action: 'get' }, options); return PlaneTrackerAPI_1.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 PlaneTrackerAPI_1.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 errors_1.ImportSettingsError(res); } } async fetchFlightInfo(icao, options) { const res = await this._getJson(`${BASE_PATH}/package/flightInfo.cgi`, { icao }, options); return PlaneTrackerAPI_1.flightInfoSchema.parse(res); } async getTrackingMode(options) { const res = await this._getJson(`${BASE_PATH}/package/getTrackingMode.cgi`, undefined, options); return PlaneTrackerAPI_1.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 PlaneTrackerAPI_1.getIcaoSchema.parse(res).icao; } async getPriorityList(options) { const res = await this._getJson(`${BASE_PATH}/package/getPriorityList.cgi`, undefined, options); return PlaneTrackerAPI_1.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 PlaneTrackerAPI_1.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 PlaneTrackerAPI_1.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 PlaneTrackerAPI_1.mapInfoSchema.parse(res); } async getZones(options) { const res = await this._getJson(`${BASE_PATH}/package/getZones.cgi`, undefined, options); return PlaneTrackerAPI_1.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 errors_1.CannotSetCoordsInAutoModeError(); } if (res.status === 400 && res.statusText === 'Invalid lat/lon parameters') { throw new errors_1.InvalidLatLngError(); } if (res.status === 400 && res.statusText === 'Invalid alt parameter') { throw new errors_1.InvalidAltitudeError(); } if (res.status === 400) { throw new errors_1.BadRequestError(res); } if (res.status === 500) { throw new errors_1.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 GenetecAgent_1.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 errors_1.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 errors_1.ErrorWithResponse(res); } } async parseBlobResponse(response) { try { return (await response.blob()); } catch (err) { throw new errors_1.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 errors_1.ErrorWithResponse(res); } } async _postUrlEncoded(path, params, options) { const data = (0, utils_1.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 errors_1.ErrorWithResponse(res); } } } exports.PlaneTrackerAPI = PlaneTrackerAPI;