Skip to content

Commit

Permalink
add own request
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucky-ESA committed Aug 30, 2024
1 parent 171427d commit 966b6e1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ exports.deTranslation = {
AUTO_DETERGENT_ON: "Automatisch Waschmittel an",
AUTO_DETERGENT_OFF: "Automatisch Waschmittel aus",
CP_OFF_EN_W: "Aus",
CP_ON_EN_W: "An"
CP_ON_EN_W: "An",
OWNREQUEST: "Eigene JSON Abfrage",
OWNANSWER: "Antwort JSON Abfrage"
};

exports.enTranslation = {
Expand Down Expand Up @@ -788,5 +790,7 @@ exports.enTranslation = {
AUTO_DETERGENT_ON: "auto detergent on",
AUTO_DETERGENT_OFF: "auto detergent off",
CP_OFF_EN_W: "OFF",
CP_ON_EN_W: "ON"
CP_ON_EN_W: "ON",
OWNREQUEST: "Own JSON request",
OWNANSWER: "Response request"
};
19 changes: 19 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,25 @@ module.exports = {
def: "",
};
await this.createDataPoint(devicedp + ".remote.Statistic.endDate", common, "state");
if (device.platformType === "thinq1" && model === 401) {
common = {
name: constants[this.lang + "Translation"]["OWNREQUEST"],
type: "string",
role: "json",
write: true,
read: true,
};
await this.createDataPoint(devicedp + ".remote.Statistic.ownrequest", common, "state");
common = {
name: constants[this.lang + "Translation"]["OWNANSWER"],
type: "string",
role: "json",
write: false,
read: true,
def: "",
};
await this.createDataPoint(devicedp + ".remote.Statistic.ownresponse", common, "state");
}
} catch (e) {
this.log.error("Error in createStatistic: " + e);
}
Expand Down
88 changes: 87 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,82 @@ class LgThinq extends utils.Adapter {
});
}

async ownRequestThinq1(data, deviceId) {
this.log.debug("ownRequestThinq1: " + data);
const headers = JSON.parse(JSON.stringify(this.defaultHeaders));
headers["x-client-id"] = constants.API1_CLIENT_ID;
let reqData = null;
try {
reqData = JSON.parse(data);
} catch(e) {
this.log.warn(`Oqn Request error: ${e}`);
return;
}
const axiosOption = {
method: reqData.method,
url: reqData.url,
baseURL: this.gateway.thinq1Uri + "/",
params: reqData.params,
data: reqData.data
};
if (axiosOption.params) {
if (axiosOption.params.deviceId === null) {
axiosOption.params.deviceId = deviceId;
}
if (axiosOption.params.workId === null) {
axiosOption.params.workId = uuid.v4();
}
}
if (axiosOption.data) {
if (axiosOption.data.deviceId === null) {
axiosOption.data.deviceId = deviceId;
}
if (axiosOption.data.workId === null) {
axiosOption.data.workId = uuid.v4();
}
}
this.log.debug(`Own request: ${JSON.stringify(axiosOption)}`);
if (reqData && reqData.method) {
const resp = await this.requestClient({
...axiosOption,
...headers
})
.then(async (res) => {
if (res.data) {
this.log.debug(`DATA: ${res.data}`);
return res.data;
} else {
this.log.debug(`STATUS: ${res.status}`);
this.log.debug(`TEXT: ${res.statusText}`);
this.log.debug(`HEADER: ${res.headers}`);
this.log.debug(`CONFIG: ${res.config}`);
return res;
}
})
.catch((error) => {
if (error.response) {
this.log.debug(`DATA: ${error.response.data}`);
this.log.debug(`STATUS: ${error.response.status}`);
this.log.debug(`HEADER: ${error.response.headers}`);
} else if (error.request) {
this.log.debug(`REQUEST: ${error.request}`);
} else {
this.log.debug(`MESSAGE: ${error.message}`);
}
console.log(error.config);
return error;
});
if (resp && resp.data) {
await this.setState(`${deviceId}.remote.Statistic.ownresponse`, {
val: JSON.stringify(resp.data),
ack: true,
});
} else {
this.log.warn(`Own request failed: ${resp}`);
}
}
}

async getDeviceEnergyThinq1(path, device) {
this.log.debug("getDeviceEnergyThinq1: " + device);
const headers = JSON.parse(JSON.stringify(this.defaultHeaders));
Expand Down Expand Up @@ -1559,7 +1635,13 @@ class LgThinq extends utils.Adapter {
if (deviceModel) {
if (!uris.data[device.modelJsonUri]) {
uris.data[device.modelJsonUri] = deviceModel;
fs.writeFileSync(`${this.adapterDir}/lib/modelJsonUri`, JSON.stringify(uris), "utf-8");
fs.writeFile(`${this.adapterDir}/lib/modelJsonUri`, JSON.stringify(uris), err => {
if (err) {
this.log.info(`Write file error: ${err}`);
} else {
this.log.info(`File written successfully`);
}
});
}
await this.setObjectNotExistsAsync(device.deviceId + ".remote", {
type: "channel",
Expand Down Expand Up @@ -2297,6 +2379,10 @@ class LgThinq extends utils.Adapter {
return;
}
let no_for = false;
if (lastsplit === "ownreqest") {
this.ownRequestThinq1(state.val, deviceId);
this.setAckFlag(id);
}
if (lastsplit === "sendJSON" || lastsplit === "sendJSONNoSync") {
let controlsync = "/control-sync";
if (lastsplit === "sendJSONNoSync") {
Expand Down

0 comments on commit 966b6e1

Please sign in to comment.