diff --git a/.gitignore b/.gitignore index b1f0a2e..d1c875a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ height +history node_modules package-lock.json diff --git a/desk-machine.js b/desk-machine.js index 97b7b7a..2343a6f 100644 --- a/desk-machine.js +++ b/desk-machine.js @@ -8,6 +8,8 @@ const gpio = require('rpi-gpio') const MAX_HEIGHT = 18 const HEIGHT_FILE = __dirname + '/height' +const HISTORY_FILE = __dirname + '/history' + const SPEED = 0.56 // how fast the desk extends/retracts (inches per second) const gpiop = gpio.promise @@ -22,6 +24,7 @@ let currentHeight = 0 function saveHeight (height) { fs.writeFileSync(HEIGHT_FILE, height, 'utf8') + fs.appendFileSync(HISTORY_FILE, `${Date.now()},${height}\n`) } diff --git a/index.js b/index.js index 70c1792..b78ffb1 100755 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const delay = require('delay') const deskMachine = require('./desk-machine.js') const express = require('express') +const fs = require('fs') const register = require('register-multicast-dns') const safeHandler = require('async-middleware').wrap const serveStatic = require('serve-static') @@ -24,6 +25,14 @@ app.use(function (req, res, next) { return next() }) +app.get('/history', function (req, res) { + if (!fs.existsSync(__dirname + '/history')) + return res.send('') + + const history = fs.readFileSync(__dirname + '/history', 'utf8') + res.send(history) +}) + app.get('/state', safeHandler(async function (req, res) { req.socket.setTimeout(0); req.socket.setNoDelay(true);