@drpollio/homebridge-reolink-camera-siren-and-light-nvr
Version:
Control alarm of Reolink cameras.
108 lines • 3.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sirenToggle = exports.setWhiteLed = exports.login = exports.apiRequest = exports.getWhiteLed = exports.Command = void 0;
const console_1 = require("console");
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
var Command;
(function (Command) {
Command["Login"] = "Login";
Command["GetWhiteLed"] = "GetWhiteLed";
Command["SetWhiteLed"] = "SetWhiteLed";
Command["AudioAlarmPlay"] = "AudioAlarmPlay";
})(Command || (Command = {}));
exports.Command = Command;
const tokens = new Map();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const apiRequest = async (config, cmd, data) => {
var _a, _b, _c;
const currentToken = tokens.get(config.ip);
const response = await fetch(`http://${config.ip}/cgi-bin/api.cgi?cmd=${cmd}${currentToken ? `&token=${currentToken}` : ''}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify([data]),
});
const jsonResponse = await response.json();
if (((_a = jsonResponse[0]) === null || _a === void 0 ? void 0 : _a.code) === 1 && ((_c = (_b = jsonResponse[0]) === null || _b === void 0 ? void 0 : _b.error) === null || _c === void 0 ? void 0 : _c.detail) === 'please login first') {
await login(config);
return apiRequest(config, cmd, data);
}
return jsonResponse;
};
exports.apiRequest = apiRequest;
const login = async (config) => {
var _a, _b, _c;
const data = {
cmd: Command.Login,
param: {
User: {
Version: '0',
userName: config.user,
password: config.password,
},
},
};
const response = await apiRequest(config, Command.Login, data);
const newToken = ((_c = (_b = (_a = response[0]) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.Token) === null || _c === void 0 ? void 0 : _c.name) || null;
if (newToken) {
tokens.set(config.ip, newToken);
}
};
exports.login = login;
const getWhiteLed = async (config) => {
const data = {
cmd: Command.GetWhiteLed,
action: 0,
param: {
channel: config.channel,
},
};
const result = await apiRequest(config, Command.GetWhiteLed, data);
return { isOn: result[0].value.WhiteLed.state !== 0, brightLevel: result[0].value.WhiteLed.bright };
};
exports.getWhiteLed = getWhiteLed;
const setWhiteLed = async (config, state, bright) => {
// There is not way to turn on without a mode like manual mode, so we have to set to timer with one minute to turn off
const data = {
cmd: Command.SetWhiteLed,
param: {
WhiteLed: {
state,
channel: config.channel,
mode: state === 1 ? 3 : 1,
bright: bright,
LightingSchedule: {
EndHour: 23,
EndMin: 59,
StartHour: 0,
StartMin: 0,
},
wlAiDetectType: {
dog_cat: state === 1 ? 1 : 0,
face: state === 1 ? 1 : 0,
people: 1,
vehicle: 1,
},
},
},
};
return apiRequest(config, Command.SetWhiteLed, data);
};
exports.setWhiteLed = setWhiteLed;
const sirenToggle = async (config, start) => {
const data = {
cmd: Command.AudioAlarmPlay,
action: 0,
param: {
alarm_mode: 'manul',
manual_switch: start ? 1 : 0,
times: 1,
channel: config.channel,
},
};
(0, console_1.log)('sirenToggle', data);
return apiRequest(config, Command.AudioAlarmPlay, data);
};
exports.sirenToggle = sirenToggle;
//# sourceMappingURL=reolink.js.map