Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include code to provide options for Web-Clients to request feedback f… #295

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions MMM-Remote-Control.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Module.register("MMM-Remote-Control", {
if (notification === "USER_PRESENCE") {
this.sendSocketNotification(notification, payload);
}
if (notification === "MMMRC_RESPONSE") {
this.sendSocketNotification(notification, payload);
}
},

// Override socket notification handler.
Expand Down
84 changes: 81 additions & 3 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = NodeHelper.create(Object.assign({
this.configData = {};

this.waiting = [];
this.waitforresponses = {};

this.template = "";
this.modulesAvailable = [];
Expand Down Expand Up @@ -772,7 +773,8 @@ module.exports = NodeHelper.create(Object.assign({
self.updateModule(decodeURI(query.module), res);
return true;
}
if (query.action === 'NOTIFICATION') {
if (query.action === 'NOTIFICATION' || query.action === 'MMMRC_RESPONSE') {
//Log.log(["MMMRC executeQuery "+query.action,query]);
try {
var payload = {}; // Assume empty JSON-object if no payload is provided
if (typeof query.payload === 'undefined') {
Expand All @@ -786,8 +788,80 @@ module.exports = NodeHelper.create(Object.assign({
payload = query.payload;
}
}
this.sendSocketNotification(query.action, { 'notification': query.notification, 'payload': payload });
this.sendResponse(res);
var mmmrc_options={};
if (typeof payload.mmmrc_options !== 'undefined') {
mmmrc_options=payload.mmmrc_options;
if (typeof payload.mmmrc_options === 'string') {
if (payload.mmmrc_options.startsWith("{")) {
payload = JSON.parse(query.payload);
}
}
if (typeof mmmrc_options.timeout === 'undefined') {
mmmrc_options.timeout=1000;
}
if ((typeof mmmrc_options.wait !== 'undefined') && (mmmrc_options.wait===true)) {
mmmrc_options.type="request";
delete mmmrc_options.wait;
}

}
var immediateresponse=true;
var immediatenotification=true;

if (mmmrc_options.type) {
//Log.log(["MMMRC executeQuery "+query.action+" mmmrc_options ",mmmrc_options]);
if (mmmrc_options.type=="request") {
if (typeof mmmrc_options.id === 'undefined') {
mmmrc_options.id="ID"+Date.now().toString(36) + Math.floor(Math.pow(10, 12) + Math.random() * 9*Math.pow(10, 12)).toString(36);
}
if (typeof mmmrc_options.responsecount === 'undefined') {
mmmrc_options.responsecount=1;
}
this.waitforresponses[mmmrc_options.id]={ mmmrc_options: mmmrc_options, res: res, responses: [] }
var that=this;
setTimeout(function(id=mmmrc_options.id) {
if (typeof(that.waitforresponses[id]) !== 'undefined') {
// else it's already gone
var waitforresponse=that.waitforresponses[id];
//Log.log(["MMMRC "+mmmrc_options.id+" timeout",waitforresponse]);
var data=waitforresponse.responses;
var error=false;
if (waitforresponse.mmmrc_options.responsecount > waitforresponse.responses.length) {
error="timeout"
}
that.sendResponse(waitforresponse.res,error,data);
delete that.waitforresponses[id];
}
},mmmrc_options.timeout);
//Log.log(["MMMRC "+mmmrc_options.id+" new request",this.waitforresponses[mmmrc_options.id]]);
immediateresponse=false;
}
if (mmmrc_options.type=="response") {
if (typeof mmmrc_options.id === 'undefined') {
throw "invalid response, no ID";
} else {
var id=mmmrc_options.id;
if (typeof this.waitforresponses[id] === 'undefined') {
throw "invalid ID:"+id;
} else {
this.waitforresponses[id].responses.push(payload.response);
if (this.waitforresponses[id].responses.length >= this.waitforresponses[id].mmmrc_options.responsecount) {
//Log.log(["MMMRC "+id+" response",this.waitforresponses[id]]);
this.sendResponse(this.waitforresponses[id].res,false,{ "responses" : this.waitforresponses[id].responses,"mmmrc_options" : mmmrc_options});
delete this.waitforresponses[id];
}
immediatenotification=false;
}
}
}
}

if (immediatenotification) {
this.sendSocketNotification(query.action, { 'notification': query.notification, 'payload': payload });
}
if (immediateresponse) {
this.sendResponse(res);
}
return true;
} catch (err) {
Log.error("ERROR: ", err);
Expand Down Expand Up @@ -1114,6 +1188,10 @@ module.exports = NodeHelper.create(Object.assign({
this.answerGet(payload, { isSocket: true });
}
}
if (notification === "MMMRC_RESPONSE") {
var query=payload; query.action=notification;
this.executeQuery(query, { isSocket: true });
}
if (notification === "UNDO_CONFIG") {
var backupHistorySize = 5;
var iteration = -1
Expand Down