Skip to content

Commit

Permalink
Push update mechanism fixed; readme completed for openhab rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
paphko committed Sep 14, 2016
1 parent 2ba8cfc commit 0caf973
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ modules: [

## Configuring Openhab

All openhab items that are configured in your module may also push their state changes directly to this magic mirror module via the [http binding](https://github.com/openhab/openhab/wiki/Http-Binding). The examples below should be self-explanatory:
The easiest and least invasive way of configuring openhab to push state changes to the magic mirror server, is via a dedicated rules-file, e.g. `mirror.rules`:

````
Switch Light_Kitchen TODO...
Switch Reed_Kitchen TODO...
String Temperature_Kitchen TODO...
// define your magic mirror URL here; make sure that there is no trailing slash
var String mirrorUrl = "https://mirror:8080/openhab"
// whenever one of your items of interest changes, send a GET request to the magic mirror server
rule "L_Living to mirror" when Item L_Living changed then sendHttpGetRequest(mirrorUrl + "?item=L_Living&state=" + L_Living.state) end
rule "Reed_Door to mirror" when Item Reed_Door changed then sendHttpGetRequest(mirrorUrl + "?item=Reed_Door&state=" + Reed_Door.state) end
rule "Temperature_Entry to mirror" when Item Temerature_Entry changed then sendHttpGetRequest(mirrorUrl + "?item=Temperature_Entry&state=" + Temperature_Entry.state) end
...
````
40 changes: 24 additions & 16 deletions mmm-openhabfloorplan.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Module.register("mmm-openhabfloorplan", {
if (!isNaN(this.config.updateInterval) && this.config.updateInterval > 0) {
var self = this;
setInterval(function() {
this.sendSocketNotification("GET_OPENHAB_ITEMS", this.config.openhab);
Log.info("requesting periodic update: " + self.config.openhab);
self.sendSocketNotification("GET_OPENHAB_ITEMS", self.config.openhab);
}, this.config.updateInterval);
}
} else {
Expand All @@ -72,25 +73,32 @@ Module.register("mmm-openhabfloorplan", {
valuesExist: function(obj) { return obj !== 'undefined' && Object.keys(obj).length > 0; },

socketNotificationReceived: function(notification, payload) {
Log.info("Notification received: " + notification);
if (notification == "OPENHAB_ITEMS") {
Log.info("Openhab items received: " + payload.item.length);
for (var key in payload.item) {
var item = payload.item[key];
if (item.name in this.config.lights) {
var visible = item.state == "ON" || (!isNaN(parseInt(item.state)) && parseInt(item.state) > 0);
this.setVisible("openhab_" + item.name, visible);
} else if (item.name in this.config.windows) {
var visible = item.state == "OFF" || item.state == "OPEN";
this.setVisible("openhab_" + item.name, visible);
if (this.config.windows[item.name].counterwindow !== 'undefined' && this.config.windows[item.name].radius !== 'undefined') {
this.setVisible("openhab_" + item.name + "_counterwindow", visible);
}
} else if (item.name in this.config.labels) {
var element = document.getElementById("openhab_" + item.name);
if (element != null) {
element.innerHTML = this.formatLabel(item.state, this.config.labels[item.name]);
}
}
this.updateDivForItem(item.name, item.state);
}
} else if (notification == "OPENHAB_ITEM") {
Log.info("Openhab item received: " + payload.item);
this.updateDivForItem(payload.item, payload.state);
}
},
updateDivForItem: function(item, state) {
if (item in this.config.lights) {
var visible = state == "ON" || (!isNaN(parseInt(state)) && parseInt(state) > 0);
this.setVisible("openhab_" + item, visible);
} else if (item in this.config.windows) {
var visible = state == "OFF" || state == "OPEN";
this.setVisible("openhab_" + item, visible);
if (this.config.windows[item].counterwindow !== 'undefined' && this.config.windows[item].radius !== 'undefined') {
this.setVisible("openhab_" + item + "_counterwindow", visible);
}
} else if (item in this.config.labels) {
var element = document.getElementById("openhab_" + item);
if (element != null) {
element.innerHTML = this.formatLabel(state, this.config.labels[item]);
}
}
},
Expand Down
20 changes: 9 additions & 11 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var request = require('request');
var NodeHelper = require("node_helper");
const request = require("request");
const NodeHelper = require("node_helper");
const url = require("url");

module.exports = NodeHelper.create({

Expand All @@ -10,16 +11,13 @@ module.exports = NodeHelper.create({
this.expressApp.get('/openhab', (req, res) => {

var query = url.parse(req.url, true).query;
var message = query.message;
var type = query.type;
var payload = { item: query.item, state: query.state, timestamp: new Date() };

if (message == null){
res.send({"status": "failed", "error": "No message given."});
}
else {
var log = {"message": message, "timestamp": new Date()};
res.send({"status": "success", "payload": log});
//TODO self.sendSocketNotification("NEW_MESSAGE", message);
if (query.item == null || query.state == null) {
res.send({ status: "failed", error: "item and/or state is missing", payload: payload});
} else {
res.send({ status: "success", payload: payload });
self.sendSocketNotification("OPENHAB_ITEM", payload);
}
});
},
Expand Down

0 comments on commit 0caf973

Please sign in to comment.