From 247877805fa9facf9b92247b06d96f0732eb1b7f Mon Sep 17 00:00:00 2001 From: Parnic Date: Thu, 22 Apr 2021 21:26:21 -0500 Subject: [PATCH] Replace localstorage with node_helper write to file Seeing a bunch of weird issues with this data not saving through the browser/electron, so let's just use a known-good method instead. --- .gitignore | 1 + MMM-chores.js | 20 ++++++++++++++++---- node_helper.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 node_helper.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f7eb8c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +saved.json diff --git a/MMM-chores.js b/MMM-chores.js index 64eead3..89c4f9b 100644 --- a/MMM-chores.js +++ b/MMM-chores.js @@ -17,6 +17,7 @@ Module.register("MMM-chores", { start() { this.doUpdate() + this.sendSocketNotification("CHORES_READY") }, doUpdate() { @@ -49,6 +50,17 @@ Module.register("MMM-chores", { return wrapper }, + socketNotificationReceived(notification, payload) { + if (notification === "CHORES_DATA_READ") { + for (const v in payload) { + const elem = document.getElementById(v) + if (elem) { + elem.checked = payload[v] === 1 + } + } + } + }, + getDateString(date) { var td = String(date.getDate()).padStart(2, '0') var tm = String(date.getMonth() + 1).padStart(2, '0') @@ -71,11 +83,11 @@ Module.register("MMM-chores", { if (id !== undefined) { cb.id = id } - if (parseInt(localStorage.getItem(cb.id), 10)) { - cb.checked = true - } cb.onclick = (ev) => { - localStorage.setItem(ev.target.id, cb.checked ? 1 : 0) + let targetId = ev.target.id + let val = cb.checked ? 1 : 0 + this.sendSocketNotification("CHORES_ADD_DATA", { "key": targetId, "val": val }) + const numItems = 2 const randVal = Math.floor(Math.random() * 1000) if (this.config.showConfetti && cb.checked) { diff --git a/node_helper.js b/node_helper.js new file mode 100644 index 0000000..5a5dde1 --- /dev/null +++ b/node_helper.js @@ -0,0 +1,43 @@ +const NodeHelper = require("node_helper"); +const path = require("path"); +const fs = require("fs"); +const filename = "/saved.json"; +var configFilename = path.resolve(__dirname + filename); + +module.exports = NodeHelper.create({ + start() { + console.log("##### Starting node helper for: " + this.name); + + fs.readFile(configFilename, (err, data) => { + if (err) { + if (err.code === 'ENOENT') { + return; + } + + throw err; + } + + this.saved_data = JSON.parse(data); + }); + }, + + writeToFile(data) { + var json = JSON.stringify(data); + fs.writeFileSync(configFilename, json, "utf8"); + }, + + socketNotificationReceived(notification, payload) { + if (notification === "CHORES_ADD_DATA") { + if (!this.saved_data) { + this.saved_data = {} + } + + this.saved_data[payload.key] = payload.val + this.writeToFile(this.saved_data) + } else if (notification === "CHORES_READY") { + if (this.saved_data) { + this.sendSocketNotification("CHORES_DATA_READ", this.saved_data) + } + } + } +}); \ No newline at end of file