// app/service/user.js
const Service = require("egg").Service;
const parseString = require("xml2js").parseString;
const _ = require("lodash");
class HaikangweishiService extends Service {
//旋转
async continuous(params) {
let paramsTemp = {
url: `http://192.168.1.64/ISAPI/PTZCtrl/channels/${
_.get(params, ["channels"], 1) || 1
}/continuous`,
method: "PUT",
data: `${_.get(
params,
["pan"],
0
)}${_.get(params, ["tilt"], 0)}`,
};
return await this.togetherCurl(paramsTemp);
}
//到达预置点
async goto(params) {
let paramsTemp = {
url: `http://192.168.1.64/ISAPI/PTZCtrl/channels/${
_.get(params, ["channels"], 1) || 1
}/presets/${_.get(params, ["presets"], 1) || 1}/goto`,
method: "PUT",
};
return await this.togetherCurl(paramsTemp);
}
//设置预置点
async presets(params) {
let paramsTemp = {
url: `http://192.168.1.64/ISAPI/PTZCtrl/channels/${
_.get(params, ["channels"], 1) || 1
}/presets/${_.get(params, ["presets"], 1) || 1}`,
method: "PUT",
data: `${
_.get(params, ["presets"], 1) || 1
}${
_.get(
params,
["presetsName"],
"预置点 " + _.get(params, ["presets"], 1)
) || "预置点 1"
}`,
};
return await this.togetherCurl(paramsTemp);
}
//删除预置点
async delPresets(params) {
let paramsTemp = {
url: `http://192.168.1.64/ISAPI/PTZCtrl/channels/${
_.get(params, ["channels"], 1) || 1
}/presets/${_.get(params, ["presets"], 1) || 1}`,
method: "DELETE",
};
return await this.togetherCurl(paramsTemp);
}
//调焦
async zoom(params) {
let paramsTemp = {
url: `http://192.168.1.64/ISAPI/PTZCtrl/channels/${
_.get(params, ["channels"], 1) || 1
}/continuous`,
method: "PUT",
data: `${
_.get(params, ["zoom"], 0) || 0
}`,
};
return await this.togetherCurl(paramsTemp);
}
//聚焦
async focus(params) {
let paramsTemp = {
url: `http://192.168.1.64/ISAPI/System/Video/inputs/channels/${
_.get(params, ["channels"], 1) || 1
}/focus`,
method: "PUT",
data: `${
_.get(params, ["focus"], 0) || 0
}`,
};
return await this.togetherCurl(paramsTemp);
}
//曝光度
async iris(params) {
let paramsTemp = {
url: `http://192.168.1.64/ISAPI/System/Video/inputs/channels/${
_.get(params, ["channels"], 1) || 1
}/iris`,
method: "PUT",
data: `${
_.get(params, ["iris"], 0) || 0
}`,
};
return await this.togetherCurl(paramsTemp);
}
//统一请求
async togetherCurl(params) {
let { ctx } = this;
try {
const loginResult1 = await ctx.curl(params.url, {
method: params.method,
data: params.data,
headers: {
Cookie: this.config.cookie,
},
});
this.config.duplicateReq++;
let resData1 = {};
let resDataStr = "";
parseString(loginResult1.data.toString(), function (err, result) {
resData1 = result;
resDataStr = JSON.stringify(result);
});
if (resDataStr.indexOf("Unauthorized") === -1) {
this.config.duplicateReq = 0;
return {
haserror: false,
data: resData1,
};
} else {
await ctx.service.home.index();
if (this.config.duplicateReq < 10) {
return await this.togetherCurl(params);
} else {
return { haserror: true, msg: "请求次数过多,请稍后重试" };
}
}
} catch (error) {
return { haserror: true, msg: error };
}
}
}
module.exports = HaikangweishiService;