Line data Source code
1 : // @ts-check 2 : import axios from "axios"; 3 : import { get } from "lodash"; 4 : 5 : import { BaseLogger, butils } from "@cern/base-vue"; 6 : import d from "debug"; 7 : import { makeUri } from "../../utilities"; 8 : 9 1 : const debug = d("app:sources"); 10 : 11 : export default class CAMSource { 12 : /** 13 : * @param {V.Store<AppStore.State>} store 14 : */ 15 : constructor(store) { 16 : /** @type {V.Store<AppStore.State>} */ 17 1 : this.store = store; 18 : } 19 : 20 : async connect() { 21 : /** @type {string[]} */ 22 5 : const camConfig = await axios.get( 23 : makeUri(butils.currentUrl(), "/config").href()) 24 : .then( 25 5 : (ret) => ret.data, 26 : /** @param {Error} err */ 27 0 : (err) => BaseLogger.error(`failed to retrieve cameras list: ${err}`)); 28 : 29 5 : this.store.commit("cameras/update", { cameras: camConfig }); 30 : } 31 : 32 : /** 33 : * @param {string} group 34 : * @param {string|AppStore.CameraConfig} camera 35 : */ 36 : async getPTZLimits(group, camera) { 37 : /** @type {string} */ 38 0 : const name = get(camera, [ "name" ], camera); 39 0 : const limits = await axios.get(makeUri( 40 : butils.currentUrl(), 41 : `/cameras/${group}/${name}/ptz/limits`).href()) 42 : .then( 43 0 : (ret) => ret.data, 44 0 : (err) => BaseLogger.error(`failed to retrieve cameras limits: ${err}`)); 45 : 46 0 : return limits; 47 : } 48 : 49 : /** 50 : * @param {string} group 51 : * @param {string|AppStore.CameraConfig} camera 52 : */ 53 : async getPTZ(group, camera) { 54 : /** @type {string} */ 55 0 : const name = get(camera, [ "name" ], camera); 56 0 : return await axios.get(makeUri(butils.currentUrl(), 57 : `/cameras/${group}/${name}/ptz`).href()) 58 : .then( 59 0 : (ret) => ret.data, 60 0 : (err) => BaseLogger.error(`failed to retrieve cameras limits: ${err}`)); 61 : } 62 : 63 : /** 64 : * @param {string} group 65 : * @param {string} camera 66 : * @param {any} params 67 : * @param {boolean} absolute 68 : */ 69 : async movePTZ(group, camera, params, absolute = true) { 70 0 : debug("moving PTZ", params); 71 : /** @type {string} */ 72 0 : if (absolute) { 73 0 : await axios.put(makeUri( 74 : butils.currentUrl(), 75 : `/cameras/${group}/${camera}/ptz`).href(), params) 76 0 : .catch((err) => BaseLogger.error(`failed to move camera: ${err}`)); 77 : } 78 : else { 79 0 : await axios.post(makeUri( 80 : butils.currentUrl(), 81 : `/cameras/${group}/${camera}/ptz`).href(), params) 82 0 : .catch((err) => BaseLogger.error(`failed to move camera: ${err}`)); 83 : } 84 : } 85 : 86 : /** 87 : * @param {string} group 88 : * @param {string} camera 89 : * @return {Promise<Blob>} 90 : */ 91 : async getScreenshot(group, camera) { 92 0 : return axios.get( 93 : makeUri( 94 : butils.currentUrl(), 95 : `/cameras/${group}/${camera}/screenshot`).href(), 96 0 : { responseType: "blob" }).then((ret) => ret.data); 97 : } 98 : }