rename
This commit is contained in:
parent
a89e416449
commit
1e7fd9e429
@ -1,15 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "my-sense-hat",
|
"name": "sensehat-server",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"description": "我的树莓派SenseHat",
|
"description": "我的树莓派SenseHat",
|
||||||
"main": "sensehat.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "nodemon",
|
"start": "nodemon",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "ssh://git@nas:2222/zhaoxin/my-sense-hat.git"
|
"url": "ssh://git@nas:2222/zhaoxin/sensehat-server.git"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "Zhao Xin <7176466@qq.com>",
|
"author": "Zhao Xin <7176466@qq.com>",
|
||||||
|
131
sensehat.js
131
sensehat.js
@ -1,131 +0,0 @@
|
|||||||
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(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
|
|
||||||
|
|
||||||
const color_red = [255, 0, 0]
|
|
||||||
const color_blue = [0, 0, 255]
|
|
||||||
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],
|
|
||||||
]
|
|
||||||
io.emit('leds', leds)
|
|
||||||
|
|
||||||
let zhao_server_errors = 0
|
|
||||||
|
|
||||||
const job = new CronJob(
|
|
||||||
'*/3 * * * * *',
|
|
||||||
async () => {
|
|
||||||
// 测试 http://zhao 的连通性
|
|
||||||
request('http://zhao', (err, res, body) => {
|
|
||||||
if (res.statusCode != 200) {
|
|
||||||
zhao_server_errors++
|
|
||||||
}
|
|
||||||
if (zhao_server_errors > 0) {
|
|
||||||
for (let i = 0; i < zhao_server_errors; i++) {
|
|
||||||
const x = i % 8
|
|
||||||
const y = Math.floor(i / 8)
|
|
||||||
sense.setPixel(x, y, color_red)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let color = sense.getPixel(0, 0)
|
|
||||||
let [r, g, b] = color
|
|
||||||
sense.setPixel(0, 0, [r, g > 0 ? 0 : 255, b])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
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 }) => {
|
|
||||||
console.log({ action, x, y })
|
|
||||||
switch (action) {
|
|
||||||
case 'on':
|
|
||||||
sense.setPixel(Number(x), Number(y), color_blue)
|
|
||||||
leds[y][x] = 1
|
|
||||||
break
|
|
||||||
case 'off':
|
|
||||||
sense.setPixel(Number(x), Number(y), [0, 0, 0])
|
|
||||||
leds[y][x] = 0
|
|
||||||
break
|
|
||||||
case 'hello':
|
|
||||||
sense.flashMessage('HELLO', 1, color_blue)
|
|
||||||
sense.clear()
|
|
||||||
read_all_leds(0)
|
|
||||||
break
|
|
||||||
case 'flash':
|
|
||||||
sense.clear([255, 255, 255])
|
|
||||||
setTimeout(sense.clear, 100)
|
|
||||||
read_all_leds(0)
|
|
||||||
break
|
|
||||||
case 'clear':
|
|
||||||
sense.clear()
|
|
||||||
read_all_leds(0)
|
|
||||||
break
|
|
||||||
case 'temp':
|
|
||||||
read_all_leds()
|
|
||||||
get_rpis_info()
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
io.emit('leds', leds)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
function read_all_leds(value = null) {
|
|
||||||
const pixels = sense.getPixels()
|
|
||||||
for (let y = 0; y < 8; y++) {
|
|
||||||
for (let x = 0; x < 8; x++) {
|
|
||||||
leds[y][x] = value !== null ? value : sense.getPixel(x, y)[2] == 0 ? 0 : 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user