All files / src/ptz AxisPTZ.js

87.5% Statements 133/152
71.87% Branches 23/32
100% Functions 6/6
87.5% Lines 133/152

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 22x 22x 22x 22x 22x 2x 2x 1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 14x 14x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 8x 2x 2x 6x 6x 6x 8x 1x 1x 1x 1x 1x 1x 1x 1x 2x 40x 40x 2x 2x 2x       2x 2x 1x 1x 1x 1x 1x 1x     1x 1x 6x 1x 1x 6x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 6x 1x 1x 6x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x  
// @ts-check
// SPDX-License-Identifier: Zlib
// SPDX-FileCopyrightText: 2024 CERN (home.cern)
// SPDX-FileContributor: Author: Sylvain Fargier <sylvain.fargier@cern.ch>
 
import { isNil } from "@cern/nodash";
import d from "debug";
import axios from "axios";
import PTZDevice from "./PTZDevice.js";
 
const debug = d("cam:ptz:axis");
 
/**
 * @typedef {import('express').Request} Request
 * @typedef {import('express').IRouter} expressIRouter
 * @typedef {import('express-ws').WithWebsocketMethod & expressIRouter} IRouter
 */
 
const ControlParams = [ "pan", "tilt", "zoom", "iris", "focus", "brightness" ];
const ExtraControlParams = [
  "backlight", "ircutfilter", "imagerotation", "speed", "gotodevicepreset",
  "gotopresetname", "auxiliary", "continuousbrightnessmove",
  "continuousirismove", "continuousfocusmove", "continuouszoommove",
  "continuouspantiltmove", "autoiris", "autofocus", "imageheight", "imagewidth",
  "areazoom", "center", "whoami", "camera"
];
 
class AxisPTZ extends PTZDevice {
  /**
   *
   * @param {any} data
   */
  _parseResponse(data) {
    return ((typeof data === "string") ? data : "").split("\n").reduce((ret, line) => {
      const pos = line.indexOf("=");
      if (pos >= 0) {
        ret[line.slice(0, pos).trim()] = line.slice(pos + 1).trim();
      }
      return ret;
    }, {});
  }
 
  async getLimits() {
    const opts = { timeout: 5000 };
    if (this.user) {
      opts.auth = { username: this.user, password: this.pass };
    }
 
    const ret = await axios.get(
      `${this.src}/axis-cgi/com/ptz.cgi?query=limits`, opts)
    .catch((ret) => {
      debug("get limits error: %s", ret?.response?.data ?? "?");
      throw new Error("request failed with status: " + ret?.response?.status);
    });
    return Object.entries(this._parseResponse(ret.data)).reduce(
      (ret, [ key, value ]) => {
        ret[key] = Number(value);
        return ret;
      }, {});
  }
 
  async getPosition() {
    const opts = { timeout: 5000 };
    if (this.user) {
      opts.auth = { username: this.user, password: this.pass };
    }
 
    const ret = await axios.get(
      `${this.src}/axis-cgi/com/ptz.cgi?query=position`, opts)
    .catch((ret) => {
      debug("get position error: %s", ret?.response?.data ?? "?");
      throw new Error("request failed with status: " + ret?.response?.status);
    });
    return Object.entries(this._parseResponse(ret.data)).reduce(
      (ret, [ key, v ]) => {
        if (key === "autofocus" || key === "autoiris") {
          ret[key] = (v === "on");
        }
        else {
          ret[key] = Number(v);
        }
        return ret;
      }, {});
  }
 
  /**
   * @param {any} params
   * @param {any} query
   */
  _prepareQuery(params, query) {
    (ExtraControlParams ?? []).forEach((p) => {
      const value = params[p];
      if (isNil(value)) { return; }
      else if (value === true || value === false) {
        query[p] = value ? "on" : "off";
      }
      else {
        query[p] = value;
      }
    });
  }
 
  async putPosition(params) {
    if (!(params instanceof Object)) { throw new Error("invalid arguments: " + params); }
 
    const opts = { timeout: 5000 };
    if (this.user) {
      opts.auth = { username: this.user, password: this.pass };
    }
 
    opts.params = ControlParams.reduce((ret, name) => {
      if (!isNil(params[name])) {
        ret[name] = params[name];
      }
      return ret;
    }, {});
 
    this._prepareQuery(params, opts.params);
 
    await axios.get(`${this.src}/axis-cgi/com/ptz.cgi`, opts)
    .catch((ret) => {
      debug("put position error: %s", ret?.response?.data ?? "?");
      throw new Error("request failed with status: " + ret?.response?.status);
    });
    return true;
  }
 
  async postPosition(params) {
    if (!(params instanceof Object)) { throw new Error("invalid arguments: " + params); }
 
    const opts = { timeout: 5000 };
    if (this.user) {
      opts.auth = { username: this.user, password: this.pass };
    }
 
    opts.params = ControlParams.reduce((ret, name) => {
      if (!isNil(params[name])) {
        ret["r" + name] = params[name];
      }
      return ret;
    }, {});
    this._prepareQuery(params, opts.params);
    await axios.get(`${this.src}/axis-cgi/com/ptz.cgi`, opts)
    .catch((ret) => {
      debug("post position error: %s", ret?.response?.data ?? "?");
      throw new Error("request failed with status: " + ret?.response?.status);
    });
    return true;
  }
}
 
export default AxisPTZ;