Skip to content

Commit ea32b2b

Browse files
author
Excodibur
committed
Fixed an issue where a event receiving updates was not found internally
1 parent 492a093 commit ea32b2b

File tree

2 files changed

+102
-87
lines changed

2 files changed

+102
-87
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
Placeholder for the next version (add instead of version-number-headline below):
44
## **WORK IN PROGRESS**
55
-->
6+
## **WORK IN PROGRESS**
7+
- Fixed: Some crashed caused by event-updates were fixed with a workaround.
8+
69
## 0.10.2 (2023-03-25)
710
- Fixed: Improved core:MovingState. Should reflect moving blinds correctly now.
811

lib/tahoma.js

Lines changed: 99 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Tahoma.prototype.sendInternalRequest = function (method, requestPath, payload, c
325325
let jsonPayload = null;
326326

327327
const requestParams = {
328-
"url": url
328+
url
329329
};
330330

331331
if (requestPath === "login") { // only on POST
@@ -730,7 +730,7 @@ Tahoma.prototype.updateDevice = function (name, deviceData) {
730730
// Prepare sub-states
731731
const channelStates = [];
732732
for (const [key, value] of Object.entries(state.value))
733-
channelStates.push({ "name": key, "value": value });
733+
channelStates.push({ "name": key, value });
734734

735735
// Create a channel with state-sublelements
736736
controller.createOrUpdateChannel(name + ".states." + state.name, channelStates);
@@ -1113,100 +1113,112 @@ Tahoma.prototype.updateEventState = function (event) {
11131113

11141114
const currentState = event.state || event.newState;
11151115

1116-
if (event.name === "ExecutionRegisteredEvent") {
1117-
controller.events[event.execId] = {
1118-
"deviceURL": event.actions[0].deviceURL,
1119-
"command": event.actions[0].command || event.actions[0].commands[0].name,
1120-
"parameters": event.actions[0].parameters,
1121-
"state": event.actions[0].state
1122-
};
1123-
}
1116+
try {
1117+
if (event.name === "ExecutionRegisteredEvent") {
1118+
controller.events[event.execId] = {
1119+
"deviceURL": event.actions[0].deviceURL,
1120+
"command": event.actions[0].command || event.actions[0].commands[0].name,
1121+
"parameters": event.actions[0].parameters,
1122+
"state": event.actions[0].state
1123+
};
1124+
}
11241125

1125-
if (currentState !== "IN_PROGRESS" && currentState !== "COMPLETED" && currentState !== "FAILED")
1126-
return;
1126+
if (currentState !== "IN_PROGRESS" && currentState !== "COMPLETED" && currentState !== "FAILED")
1127+
return;
11271128

1128-
let url = "exec/current/";
1129-
if (currentState === "COMPLETED" || currentState === "FAILED") {
1130-
const storedEvent = controller.events[event.execId];
1131-
controller.context.log.debug(`Skipping calling history. Using stored event (${JSON.stringify(storedEvent)}) instead`);
1132-
storedEvent.state = currentState;
1133-
controller.updateDeviceActionState(storedEvent, execId);
1134-
delete controller.events[event.execId];
1135-
return;
1136-
}
1129+
let url = "exec/current/";
1130+
if (currentState === "COMPLETED" || currentState === "FAILED") {
1131+
const storedEvent = controller.events[event.execId];
1132+
controller.context.log.debug(`Skipping calling history. Using stored event (${JSON.stringify(storedEvent)}) instead`);
11371133

1138-
url += execId;
1139-
if (!controller.use_local_api)
1140-
url += "?_=" + (new Date()).getTime();
1134+
// Workaround: If the event cant be found in stored events, just ignore it for now
1135+
// This will probably cause some command states not to be updated in some cases, but is better than log-spaming warn-messages.
1136+
if (storedEvent === undefined) {
1137+
controller.context.log.debug(`Could not find event for execId ${event.execId} locally. Ignoring it for now.`);
1138+
return;
1139+
}
11411140

1142-
controller.sendGET(url, {}, function (err, data) {
1143-
if (err) {
1144-
controller.context.log.warn("Failed getting execution state for " + execId);
1145-
return;
1146-
} else if (!data || (!data.execution && !data.state))
1141+
storedEvent.state = currentState;
1142+
controller.updateDeviceActionState(storedEvent, execId);
1143+
delete controller.events[event.execId];
11471144
return;
1145+
}
11481146

1149-
controller.context.log.debug(url + " - Fetched Data " + JSON.stringify(data));
1147+
url += execId;
1148+
if (!controller.use_local_api)
1149+
url += "?_=" + (new Date()).getTime();
11501150

1151-
let actions;
1152-
if (data.execution) {
1153-
// unify contents
1154-
data = data.execution;
1155-
actions = data.commands;
1156-
} else {
1157-
if (data.actionGroup)
1158-
actions = data.actionGroup.actions;
1159-
else if (data.commands)
1160-
actions = data.commands;
1161-
}
1151+
controller.sendGET(url, {}, function (err, data) {
1152+
if (err) {
1153+
controller.context.log.warn("Failed getting execution state for " + execId);
1154+
return;
1155+
} else if (!data || (!data.execution && !data.state))
1156+
return;
11621157

1163-
let action;
1164-
for (let i = 0; i < actions.length; i++) {
1165-
action = actions[i];
1166-
if (action.commands) {
1167-
const cmd = action.commands[0];
1168-
action.type = cmd.type;
1169-
action.name = cmd.name;
1170-
action.parameters = cmd.parameters;
1171-
}
1158+
controller.context.log.debug(url + " - Fetched Data " + JSON.stringify(data));
11721159

1173-
if (currentState === "IN_PROGRESS" || currentState === "FAILED") {
1174-
// Acknowledge that the command state was set, so the user can see something is happening
1175-
const devicePath = controller.Map_DeviceURL2StateName[action.deviceURL];
1176-
let command = (action.command ? action.command : action.name);
1177-
if (command === "setClosureAndLinearSpeed")
1178-
command = ((action.parameters[0] === 100) ? "down:slow" : "up:slow");
1179-
const commandStateId = devicePath + ".commands." + command;
1180-
controller.context.getState(commandStateId, (err, tmpState) => {
1181-
if (err || tmpState === null) {
1182-
controller.context.log.debug("Command " + commandStateId + " does not seem to be a changable state. Skipping it.");
1183-
return; // state for command probably does not exist, so do nothing
1184-
}
1185-
switch (currentState) {
1186-
case "IN_PROGRESS":
1187-
controller.context.log.debug("Command \"" + commandStateId + "\" " + currentState + ". ACKnowledging it.");
1188-
controller.context.setState(commandStateId, true, true);
1189-
break;
1190-
case "FAILED":
1191-
controller.context.log.debug("Command \"" + commandStateId + "\" " + currentState + ". Setting it to false.");
1192-
controller.context.setState(commandStateId, false, true);
1193-
break;
1194-
}
1195-
});
1160+
let actions;
1161+
if (data.execution) {
1162+
// unify contents
1163+
data = data.execution;
1164+
actions = data.commands;
1165+
} else {
1166+
if (data.actionGroup)
1167+
actions = data.actionGroup.actions;
1168+
else if (data.commands)
1169+
actions = data.commands;
11961170
}
11971171

1198-
// store event for future use
1199-
/* controller.events[execId] = {
1200-
"deviceURL": action.deviceURL,
1201-
"command": action.command,
1202-
"name": action.name,
1203-
"parameters": action.parameters,
1204-
"state": action.state
1205-
}; */
1172+
let action;
1173+
for (let i = 0; i < actions.length; i++) {
1174+
action = actions[i];
1175+
if (action.commands) {
1176+
const cmd = action.commands[0];
1177+
action.type = cmd.type;
1178+
action.name = cmd.name;
1179+
action.parameters = cmd.parameters;
1180+
}
12061181

1207-
controller.updateDeviceActionState(action, execId);
1208-
}
1209-
}, controller.use_local_api);
1182+
if (currentState === "IN_PROGRESS" || currentState === "FAILED") {
1183+
// Acknowledge that the command state was set, so the user can see something is happening
1184+
const devicePath = controller.Map_DeviceURL2StateName[action.deviceURL];
1185+
let command = (action.command ? action.command : action.name);
1186+
if (command === "setClosureAndLinearSpeed")
1187+
command = ((action.parameters[0] === 100) ? "down:slow" : "up:slow");
1188+
const commandStateId = devicePath + ".commands." + command;
1189+
controller.context.getState(commandStateId, (err, tmpState) => {
1190+
if (err || tmpState === null) {
1191+
controller.context.log.debug("Command " + commandStateId + " does not seem to be a changable state. Skipping it.");
1192+
return; // state for command probably does not exist, so do nothing
1193+
}
1194+
switch (currentState) {
1195+
case "IN_PROGRESS":
1196+
controller.context.log.debug("Command \"" + commandStateId + "\" " + currentState + ". ACKnowledging it.");
1197+
controller.context.setState(commandStateId, true, true);
1198+
break;
1199+
case "FAILED":
1200+
controller.context.log.debug("Command \"" + commandStateId + "\" " + currentState + ". Setting it to false.");
1201+
controller.context.setState(commandStateId, false, true);
1202+
break;
1203+
}
1204+
});
1205+
}
1206+
1207+
// store event for future use
1208+
/* controller.events[execId] = {
1209+
"deviceURL": action.deviceURL,
1210+
"command": action.command,
1211+
"name": action.name,
1212+
"parameters": action.parameters,
1213+
"state": action.state
1214+
}; */
1215+
1216+
controller.updateDeviceActionState(action, execId);
1217+
}
1218+
}, controller.use_local_api);
1219+
} catch (exception) {
1220+
controller.context.log.warn(`Failure updating state for execId ${execId}. Probably no event was found. Root cause: ${exception}`);
1221+
}
12101222
};
12111223

12121224
Tahoma.prototype.updateDeviceActionState = function (event, execId) {
@@ -1491,7 +1503,7 @@ Tahoma.prototype.onApplyChange = function (attribute, id, value, slow) {
14911503

14921504
const action = {
14931505
"label": roomName + " - " + (attribute === "orientation" ? "Ausrichtung" : "Positioniere") + " auf " + stateValue + " % - ioBroker",
1494-
"deviceURL": deviceURL,
1506+
deviceURL,
14951507
"commands": [{
14961508
"name": commandName,
14971509
"parameters": params
@@ -1667,7 +1679,7 @@ Tahoma.prototype.onExecuteDeviceCommand = function (id, slow) {
16671679

16681680
const action = {
16691681
"label": "command " + commandName + " from ioBroker",
1670-
"deviceURL": deviceURL,
1682+
deviceURL,
16711683
"commands": [{
16721684
"name": commandName,
16731685
"parameters": params
@@ -1687,5 +1699,5 @@ Tahoma.prototype.onExecuteDeviceCommand = function (id, slow) {
16871699
};
16881700

16891701
module.exports = {
1690-
"Tahoma": Tahoma
1702+
Tahoma
16911703
};

0 commit comments

Comments
 (0)