diff --git a/package-lock.json b/package-lock.json index 78f5a38..03296f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "license": "MIT", "dependencies": { "cron": "^2.1.0", + "dotenv": "^16.0.2", "easyimage": "^3.1.1", "express": "^4.18.1", "multer": "^1.4.5-lts.1", @@ -424,6 +425,14 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dotenv": { + "version": "16.0.2", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.2.tgz", + "integrity": "sha512-JvpYKUmzQhYoIFgK2MOnF3bciIZoItIIoryihy0rIA+H4Jy0FmgyKYAHCTN98P5ybGSJcIFbh6QKeJdtZd1qhA==", + "engines": { + "node": ">=12" + } + }, "node_modules/easyimage": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/easyimage/-/easyimage-3.1.1.tgz", @@ -2186,6 +2195,11 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" }, + "dotenv": { + "version": "16.0.2", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.2.tgz", + "integrity": "sha512-JvpYKUmzQhYoIFgK2MOnF3bciIZoItIIoryihy0rIA+H4Jy0FmgyKYAHCTN98P5ybGSJcIFbh6QKeJdtZd1qhA==" + }, "easyimage": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/easyimage/-/easyimage-3.1.1.tgz", diff --git a/package.json b/package.json index afb153c..2356abf 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "license": "MIT", "dependencies": { "cron": "^2.1.0", + "dotenv": "^16.0.2", "easyimage": "^3.1.1", "express": "^4.18.1", "multer": "^1.4.5-lts.1", diff --git a/public/index.html b/public/index.html index 91b28b8..60acf20 100644 --- a/public/index.html +++ b/public/index.html @@ -136,6 +136,7 @@
+
diff --git a/sensehat.js b/sensehat.js index 23627ee..32de424 100644 --- a/sensehat.js +++ b/sensehat.js @@ -1,12 +1,15 @@ +require('dotenv').config() +const port = process.env.PORT || 3000 const express = require('express') const socketio = require('socket.io') const CronJob = require('cron').CronJob const request = require('request') const sense = require('sense-hat-led').sync const app = express() -const server = app.listen(3000, '0.0.0.0', () => console.log('server is running')) +const server = app.listen(port, '0.0.0.0', () => console.log(`server is running on port ${port}`)) const io = socketio(server) app.use(express.static('public')) +sense.clear() sense.setRotation(180) sense.lowLight = true @@ -22,7 +25,6 @@ const leds = [ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] -read_all_leds(0) io.emit('leds', leds) let zhao_server_errors = 0 @@ -52,6 +54,24 @@ const job = new CronJob( true ) +// const rpistatus = new CronJob( +// '*/5 * * * * *', +// async () => { +// request('http://zhao:4000', (err, res, body) => { +// if (res.statusCode == 200) { +// io.emit('data', body) +// } +// }) +// request('http://pi:4000', (err, res, body) => { +// if (res.statusCode == 200) { +// io.emit('data', body) +// } +// }) +// }, +// null, +// true +// ) + io.on('connection', (client) => { client.emit('leds', leds) client.on('action', ({ action, x, y }) => { @@ -81,6 +101,7 @@ io.on('connection', (client) => { break case 'temp': read_all_leds() + get_rpis_info() break default: break @@ -97,3 +118,14 @@ function read_all_leds(value = null) { } } } + +async function get_rpis_info() { + const rpis = [] + request('http://zhao:4000', (err, res, body) => { + if (res.statusCode == 200) rpis.push(JSON.parse(body)) + request('http://pi:4000', (err, res, body) => { + if (res.statusCode == 200) rpis.push(JSON.parse(body)) + io.emit('data', rpis) + }) + }) +}