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

Refactored node helper to allow specifiying multiple evdev readers #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
195 changes: 100 additions & 95 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,113 +12,118 @@ const Log = require("logger");
var evdev;
var udev;

module.exports = NodeHelper.create({
start: function () {
Log.log("MMM-KeyBindings helper has started…");
this.evdevMonitorCreated = false;
},
class EvDevHandler {
constructor(path, udev, onKeyPress) {
this.evdevPath = path;
this.udev = udev;
this.onKey = onKeyPress;
Log.log("Create EvDevHandler for " + path);
}

stop: function () {
if (this.evdevMonitorCreated) {
Log.log("EVDEV: Closing monitor and reader");
try {
this.udevMonitor.close();
} catch (e) {
if (
e.toString().indexOf("Cannot read property 'close' of undefined") ===
-1
) {
Log.error(e);
close() {
Log.log("EVDEV: Closing monitor and reader of " + this.evdevPath);
try {
this.udevMonitor.close();
} catch (e) {
if (e.toString().indexOf("Cannot read property 'close' of undefined") === -1) {
Log.error(e);
}
}
try {
this.evdevReader.close();
} catch (e) {
Log.error(e);
}
}
try {
this.evdevReader.close();
} catch (e) {
Log.error(e);
}
}
},

waitForDevice: function () {
this.udevMonitor = udev.monitor();
this.udevMonitor.on("add", (device) => {
if (
"DEVLINKS" in device &&
device.DEVLINKS === this.evdevConfig.eventPath
) {
Log.log("UDEV: Device connected.");
this.udevMonitor.close();
startMonitor() {
this.evdevReader = new evdev();
this.pendingKeyPress = {};

this.evdevReader
.on("EV_KEY", (data) => {
// Log.log("key : ", data.code, data.value);
if (data.value > 0) {
this.pendingKeyPress.code = data.code;
this.pendingKeyPress.value = data.value;
} else {
if ("code" in this.pendingKeyPress && this.pendingKeyPress.code === data.code) {
Log.log(`${this.pendingKeyPress.code} ${this.pendingKeyPress.value === 2 ? "long " : ""}pressed.`);
this.onKey(data.code, this.pendingKeyPress.value === 2 ? "KEY_LONGPRESSED" : "KEY_PRESSED");
}
this.pendingKeyPress = {};
}
})
.on("error", (e) => {
if (e.code === "ENODEV" || e.code === "ENOENT") {
Log.info(`EVDEV: Device not connected, nothing at path ${e.path}, waiting for device…`);
this.waitForDevice();
} else {
Log.error("EVDEV: ", e);
}
});

this.setupDevice();
}
});
},
}

setupDevice: function () {
this.device = this.evdevReader.open(this.evdevConfig.eventPath);
this.device.on("open", () => {
Log.log(`EVDEV: Connected to device: ${JSON.stringify(this.device.id)}`);
});
this.device.on("close", () => {
Log.debug(`EVDEV: Connection to device has been closed.`);
this.waitForDevice();
});
},
setupDevice() {
this.device = this.evdevReader.open(this.evdevPath);
this.device.on("open", () => {
Log.log(`EVDEV: Connected to device: ${JSON.stringify(this.device.id)}`);
});
this.device.on("close", () => {
Log.debug(`EVDEV: Connection to device has been closed.`);
this.waitForDevice();
});
}

waitForDevice() {
this.udevMonitor = this.udev.monitor();
this.udevMonitor.on("add", (device) => {
if ("DEVLINKS" in device && device.DEVLINKS === this.evdevPath) {
Log.log("UDEV: Device connected.");
this.udevMonitor.close();
this.setupDevice();
}
});
}

startEvdevMonitor: function () {
evdev = require("evdev");
udev = require("udev");
}

this.evdevMonitorCreated = true;
this.evdevReader = new evdev();
this.pendingKeyPress = {};
module.exports = NodeHelper.create({
start: function () {
Log.log("MMM-KeyBindings helper has started…");
this.evdevMonitorCreated = false;
this.handlers = [];
},

this.evdevReader
.on("EV_KEY", (data) => {
// Log.log("key : ", data.code, data.value);
if (data.value > 0) {
this.pendingKeyPress.code = data.code;
this.pendingKeyPress.value = data.value;
} else {
if (
"code" in this.pendingKeyPress &&
this.pendingKeyPress.code === data.code
) {
Log.log(
`${this.pendingKeyPress.code} ${
this.pendingKeyPress.value === 2 ? "long " : ""
}pressed.`
);
this.sendSocketNotification("KEYPRESS", {
keyName: data.code,
keyState:
this.pendingKeyPress.value === 2
? "KEY_LONGPRESSED"
: "KEY_PRESSED"
stop: function () {
if (this.evdevMonitorCreated) {
handlers.forEach(h => {
h.close();
});
}
this.pendingKeyPress = {};
}
})
.on("error", (e) => {
if (e.code === "ENODEV" || e.code === "ENOENT") {
Log.info(
`EVDEV: Device not connected, nothing at path ${e.path}, waiting for device…`
);
this.waitForDevice();
} else {
Log.error("EVDEV: ", e);
}
});
},

this.setupDevice();
},

socketNotificationReceived: function (notification, payload) {
if (notification === "ENABLE_EVDEV") {
if (!this.evdevMonitorCreated) {
this.evdevConfig = payload;
this.startEvdevMonitor();
}
socketNotificationReceived: function (notification, payload) {
var self = this;
if (notification === "ENABLE_EVDEV") {
if (!this.evdevMonitorCreated) {
evdev = require('evdev');
udev = require('udev');
var paths = payload.eventPath.split(",");
this.handlers = paths.map(p => new EvDevHandler(p, udev, (name, state) => {
self.sendSocketNotification("KEYPRESS", {
keyName: name,
keyState: state
});
}));
this.handlers.forEach(h => {
h.startMonitor();
});
this.evdevMonitorCreated = true;
}
}
}
}
});