2022-08-31 23:41:47 +00:00
|
|
|
const express = require('express')
|
|
|
|
const socketio = require('socket.io')
|
2022-09-01 07:34:21 +00:00
|
|
|
const CronJob = require('cron').CronJob
|
|
|
|
const request = require('request')
|
2022-08-31 23:41:47 +00:00
|
|
|
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 io = socketio(server)
|
2022-09-01 00:37:42 +00:00
|
|
|
app.use(express.static('public'))
|
2022-08-31 23:41:47 +00:00
|
|
|
sense.setRotation(180)
|
|
|
|
sense.lowLight = true
|
2022-09-01 05:03:01 +00:00
|
|
|
|
2022-09-01 07:34:21 +00:00
|
|
|
const job = new CronJob(
|
|
|
|
'*/5 * * * * *',
|
|
|
|
async () => {
|
|
|
|
// 测试 http://zhao 的连通性
|
|
|
|
request('http://zhao', (err, res, body) => {
|
2022-09-01 07:35:19 +00:00
|
|
|
console.log(err, res.statusCode)
|
2022-09-01 07:34:21 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
)
|
2022-09-01 07:25:26 +00:00
|
|
|
|
2022-09-01 07:01:36 +00:00
|
|
|
const blue = [0, 0, 255]
|
2022-09-01 05:37:32 +00:00
|
|
|
const leds = [
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
]
|
2022-09-01 06:08:01 +00:00
|
|
|
read_all_leds()
|
2022-09-01 05:40:43 +00:00
|
|
|
|
2022-08-31 23:41:47 +00:00
|
|
|
io.on('connection', (client) => {
|
2022-09-01 07:12:40 +00:00
|
|
|
client.emit('leds', leds)
|
2022-08-31 23:41:47 +00:00
|
|
|
client.on('action', ({ action, x, y }) => {
|
|
|
|
console.log({ action, x, y })
|
|
|
|
switch (action) {
|
|
|
|
case 'on':
|
2022-09-01 05:03:01 +00:00
|
|
|
sense.setPixel(Number(x), Number(y), blue)
|
2022-09-01 05:37:32 +00:00
|
|
|
leds[y][x] = 1
|
2022-08-31 23:41:47 +00:00
|
|
|
break
|
|
|
|
case 'off':
|
|
|
|
sense.setPixel(Number(x), Number(y), [0, 0, 0])
|
2022-09-01 05:37:32 +00:00
|
|
|
leds[y][x] = 0
|
2022-08-31 23:41:47 +00:00
|
|
|
break
|
|
|
|
case 'hello':
|
2022-09-01 05:03:01 +00:00
|
|
|
sense.flashMessage('HELLO', 1, blue)
|
2022-08-31 23:41:47 +00:00
|
|
|
sense.clear()
|
2022-09-01 07:15:00 +00:00
|
|
|
read_all_leds(0)
|
2022-08-31 23:41:47 +00:00
|
|
|
break
|
|
|
|
case 'flash':
|
2022-09-01 05:37:32 +00:00
|
|
|
sense.clear([255, 255, 255])
|
2022-09-01 06:12:36 +00:00
|
|
|
setTimeout(sense.clear, 100)
|
2022-09-01 07:15:00 +00:00
|
|
|
read_all_leds(0)
|
2022-08-31 23:41:47 +00:00
|
|
|
break
|
|
|
|
case 'clear':
|
|
|
|
sense.clear()
|
2022-09-01 07:15:00 +00:00
|
|
|
read_all_leds(0)
|
2022-08-31 23:41:47 +00:00
|
|
|
break
|
2022-09-01 05:47:00 +00:00
|
|
|
case 'temp':
|
|
|
|
read_all_leds()
|
|
|
|
break
|
2022-08-31 23:41:47 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2022-09-01 07:12:40 +00:00
|
|
|
io.emit('leds', leds)
|
2022-08-31 23:41:47 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-09-01 07:15:00 +00:00
|
|
|
function read_all_leds(value = null) {
|
2022-09-01 05:56:33 +00:00
|
|
|
const pixels = sense.getPixels()
|
|
|
|
for (let y = 0; y < 8; y++) {
|
|
|
|
for (let x = 0; x < 8; x++) {
|
2022-09-01 07:15:00 +00:00
|
|
|
leds[y][x] = value !== null ? value : sense.getPixel(x, y)[2] == 0 ? 0 : 1
|
2022-09-01 05:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-31 23:41:47 +00:00
|
|
|
}
|