gitpush
This commit is contained in:
parent
592b94c06d
commit
a961a63e1a
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "sensehat-server",
|
"name": "sensehat-server",
|
||||||
"version": "0.1.0",
|
"version": "0.0.1",
|
||||||
"description": "SenseHat Server",
|
"description": "树莓派 Sense HAT API",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "nodemon",
|
"start": "PORT=8001 pm2 start server.js --name 'RPi Sense HAT API:8001' --watch",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -23,8 +23,5 @@
|
|||||||
"request": "^2.88.2",
|
"request": "^2.88.2",
|
||||||
"sense-hat-led": "^1.2.0",
|
"sense-hat-led": "^1.2.0",
|
||||||
"socket.io": "^4.5.1"
|
"socket.io": "^4.5.1"
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"nodemon": "^2.0.19"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,170 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="zh">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<link rel="shortcut icon" href="data:" type="image/x-icon">
|
|
||||||
<title>Sense Hat</title>
|
|
||||||
<style>
|
|
||||||
* {
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
#matrix {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: 2px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.row {
|
|
||||||
display: flex;
|
|
||||||
width: 94px;
|
|
||||||
gap: 2px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.led {
|
|
||||||
width: 10px;
|
|
||||||
height: 10px;
|
|
||||||
border: 1px solid gray;
|
|
||||||
background-color: whitesmoke;
|
|
||||||
cursor: pointer;
|
|
||||||
border-radius: 2px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.led.on {
|
|
||||||
background-color: blue;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
width: 94px;
|
|
||||||
margin-top: 1rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div id="matrix">
|
|
||||||
<div class="row">
|
|
||||||
<div class="led" data-y="0" data-x="0" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="0" data-x="1" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="0" data-x="2" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="0" data-x="3" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="0" data-x="4" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="0" data-x="5" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="0" data-x="6" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="0" data-x="7" onclick="turnon(this)"></div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="led" data-y="1" data-x="0" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="1" data-x="1" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="1" data-x="2" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="1" data-x="3" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="1" data-x="4" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="1" data-x="5" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="1" data-x="6" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="1" data-x="7" onclick="turnon(this)"></div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="led" data-y="2" data-x="0" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="2" data-x="1" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="2" data-x="2" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="2" data-x="3" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="2" data-x="4" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="2" data-x="5" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="2" data-x="6" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="2" data-x="7" onclick="turnon(this)"></div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="led" data-y="3" data-x="0" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="3" data-x="1" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="3" data-x="2" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="3" data-x="3" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="3" data-x="4" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="3" data-x="5" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="3" data-x="6" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="3" data-x="7" onclick="turnon(this)"></div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="led" data-y="4" data-x="0" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="4" data-x="1" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="4" data-x="2" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="4" data-x="3" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="4" data-x="4" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="4" data-x="5" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="4" data-x="6" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="4" data-x="7" onclick="turnon(this)"></div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="led" data-y="5" data-x="0" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="5" data-x="1" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="5" data-x="2" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="5" data-x="3" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="5" data-x="4" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="5" data-x="5" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="5" data-x="6" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="5" data-x="7" onclick="turnon(this)"></div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="led" data-y="6" data-x="0" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="6" data-x="1" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="6" data-x="2" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="6" data-x="3" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="6" data-x="4" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="6" data-x="5" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="6" data-x="6" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="6" data-x="7" onclick="turnon(this)"></div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="led" data-y="7" data-x="0" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="7" data-x="1" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="7" data-x="2" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="7" data-x="3" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="7" data-x="4" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="7" data-x="5" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="7" data-x="6" onclick="turnon(this)"></div>
|
|
||||||
<div class="led" data-y="7" data-x="7" onclick="turnon(this)"></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<button onclick="client.emit('action', {action:'clear'})">clear all</button>
|
|
||||||
<br>
|
|
||||||
<button onclick="client.emit('action', {action:'hello'})">hello</button>
|
|
||||||
<br>
|
|
||||||
<button onclick="client.emit('action', {action:'flash'})">flash</button>
|
|
||||||
<br>
|
|
||||||
<button onclick="client.emit('action', {action:'temp'})">temp</button>
|
|
||||||
<div id="rpis"></div>
|
|
||||||
|
|
||||||
<script src="socket.io/socket.io.js"></script>
|
|
||||||
<script>
|
|
||||||
const client = io()
|
|
||||||
function turnon(led) {
|
|
||||||
const { x, y } = led.dataset
|
|
||||||
const action = led.classList.toggle('on') ? 'on' : 'off'
|
|
||||||
client.emit('action', { action, x, y })
|
|
||||||
}
|
|
||||||
client.on('leds', (array) => {
|
|
||||||
const leds = document.querySelectorAll('.led')
|
|
||||||
for (let y = 0; y < 8; y++) {
|
|
||||||
for (let x = 0; x < 8; x++) {
|
|
||||||
const n = y * 8 + x
|
|
||||||
if (array[y][x] == 1) leds[n].classList.add('on')
|
|
||||||
else leds[n].classList.remove('on')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
client.on('data', (data) => {
|
|
||||||
rpis.innerHTML = ''
|
|
||||||
data.forEach(rpi => {
|
|
||||||
const p = document.createElement('p')
|
|
||||||
p.innerHTML = `hostname: ${rpi.hostname}<br>cpu load: ${rpi.cpu.load}<br>cpu temperature: ${rpi.cpu.temperature}<br>memory total: ${rpi.memory.total}<br>memory available: ${rpi.memory.available}`
|
|
||||||
rpis.appendChild(p)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
206
server.js
206
server.js
@ -1,70 +1,60 @@
|
|||||||
require('dotenv').config()
|
require('dotenv').config()
|
||||||
const port = process.env.PORT || 3000
|
const port = process.env.PORT || 3000
|
||||||
|
const host = process.env.HOST || 'localhost'
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const socketio = require('socket.io')
|
const socketio = require('socket.io')
|
||||||
const CronJob = require('cron').CronJob
|
// const CronJob = require('cron').CronJob
|
||||||
const request = require('request')
|
// const request = require('request')
|
||||||
const sense = require('sense-hat-led').sync
|
const sense = require('sense-hat-led').sync
|
||||||
const app = express()
|
const app = express()
|
||||||
const server = app.listen(port, '0.0.0.0', () => console.log(`server is running on port ${port}`))
|
const server = app.listen(port, host, () => console.log(`rpi sense hat server is running at port http://${host}:${port}`))
|
||||||
const io = socketio(server)
|
const io = socketio(server)
|
||||||
app.use(express.static('public'))
|
|
||||||
sense.clear()
|
// Sense HAT 初始化
|
||||||
sense.setRotation(180)
|
|
||||||
sense.lowLight = true
|
sense.lowLight = true
|
||||||
|
sense.setRotation(180)
|
||||||
|
sense.clear()
|
||||||
|
|
||||||
const color_red = [255, 0, 0]
|
io.on('connection', (client) => {
|
||||||
const color_blue = [0, 0, 255]
|
console.log(`client conncted: ${client.id}`)
|
||||||
const leds = [
|
client.emit('message', `hello, ${client.id}`)
|
||||||
[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(
|
// const color_red = [255, 0, 0]
|
||||||
'*/3 * * * * *',
|
// const color_blue = [0, 0, 255]
|
||||||
async () => {
|
// const leds = [
|
||||||
// 测试 http://zhao 的连通性
|
// [0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
request('http://zhao', (err, res, body) => {
|
// [0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
if (res.statusCode != 200) {
|
// [0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
zhao_server_errors++
|
// [0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
}
|
// [0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
if (zhao_server_errors > 0) {
|
// [0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
for (let i = 0; i < zhao_server_errors; i++) {
|
// [0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
const x = i % 8
|
// [0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
const y = Math.floor(i / 8)
|
// ]
|
||||||
sense.setPixel(x, y, color_red)
|
// io.emit('leds', leds)
|
||||||
}
|
|
||||||
} 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(
|
// let zhao_server_errors = 0
|
||||||
// '*/5 * * * * *',
|
|
||||||
|
// const job = new CronJob(
|
||||||
|
// '*/3 * * * * *',
|
||||||
// async () => {
|
// async () => {
|
||||||
// request('http://zhao:4000', (err, res, body) => {
|
// // 测试 http://zhao 的连通性
|
||||||
// if (res.statusCode == 200) {
|
// request('http://zhao', (err, res, body) => {
|
||||||
// io.emit('data', body)
|
// if (res.statusCode != 200) {
|
||||||
|
// zhao_server_errors++
|
||||||
// }
|
// }
|
||||||
// })
|
// if (zhao_server_errors > 0) {
|
||||||
// request('http://pi:4000', (err, res, body) => {
|
// for (let i = 0; i < zhao_server_errors; i++) {
|
||||||
// if (res.statusCode == 200) {
|
// const x = i % 8
|
||||||
// io.emit('data', body)
|
// 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])
|
||||||
// }
|
// }
|
||||||
// })
|
// })
|
||||||
// },
|
// },
|
||||||
@ -72,60 +62,60 @@ const job = new CronJob(
|
|||||||
// true
|
// true
|
||||||
// )
|
// )
|
||||||
|
|
||||||
io.on('connection', (client) => {
|
// io.on('connection', (client) => {
|
||||||
client.emit('leds', leds)
|
// client.emit('leds', leds)
|
||||||
client.on('action', ({ action, x, y }) => {
|
// client.on('action', ({ action, x, y }) => {
|
||||||
console.log({ action, x, y })
|
// console.log({ action, x, y })
|
||||||
switch (action) {
|
// switch (action) {
|
||||||
case 'on':
|
// case 'on':
|
||||||
sense.setPixel(Number(x), Number(y), color_blue)
|
// sense.setPixel(Number(x), Number(y), color_blue)
|
||||||
leds[y][x] = 1
|
// leds[y][x] = 1
|
||||||
break
|
// break
|
||||||
case 'off':
|
// case 'off':
|
||||||
sense.setPixel(Number(x), Number(y), [0, 0, 0])
|
// sense.setPixel(Number(x), Number(y), [0, 0, 0])
|
||||||
leds[y][x] = 0
|
// leds[y][x] = 0
|
||||||
break
|
// break
|
||||||
case 'hello':
|
// case 'hello':
|
||||||
sense.flashMessage('HELLO', 1, color_blue)
|
// sense.flashMessage('HELLO', 1, color_blue)
|
||||||
sense.clear()
|
// sense.clear()
|
||||||
read_all_leds(0)
|
// read_all_leds(0)
|
||||||
break
|
// break
|
||||||
case 'flash':
|
// case 'flash':
|
||||||
sense.clear([255, 255, 255])
|
// sense.clear([255, 255, 255])
|
||||||
setTimeout(sense.clear, 100)
|
// setTimeout(sense.clear, 100)
|
||||||
read_all_leds(0)
|
// read_all_leds(0)
|
||||||
break
|
// break
|
||||||
case 'clear':
|
// case 'clear':
|
||||||
sense.clear()
|
// sense.clear()
|
||||||
read_all_leds(0)
|
// read_all_leds(0)
|
||||||
break
|
// break
|
||||||
case 'temp':
|
// case 'temp':
|
||||||
read_all_leds()
|
// read_all_leds()
|
||||||
get_rpis_info()
|
// get_rpis_info()
|
||||||
break
|
// break
|
||||||
default:
|
// default:
|
||||||
break
|
// break
|
||||||
}
|
// }
|
||||||
io.emit('leds', leds)
|
// io.emit('leds', leds)
|
||||||
})
|
// })
|
||||||
})
|
// })
|
||||||
|
|
||||||
function read_all_leds(value = null) {
|
// function read_all_leds(value = null) {
|
||||||
const pixels = sense.getPixels()
|
// const pixels = sense.getPixels()
|
||||||
for (let y = 0; y < 8; y++) {
|
// for (let y = 0; y < 8; y++) {
|
||||||
for (let x = 0; x < 8; x++) {
|
// for (let x = 0; x < 8; x++) {
|
||||||
leds[y][x] = value !== null ? value : sense.getPixel(x, y)[2] == 0 ? 0 : 1
|
// leds[y][x] = value !== null ? value : sense.getPixel(x, y)[2] == 0 ? 0 : 1
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
async function get_rpis_info() {
|
// async function get_rpis_info() {
|
||||||
const rpis = []
|
// const rpis = []
|
||||||
request('http://zhao:4000', (err, res, body) => {
|
// request('http://zhao:4000', (err, res, body) => {
|
||||||
if (res.statusCode == 200) rpis.push(JSON.parse(body))
|
// if (res.statusCode == 200) rpis.push(JSON.parse(body))
|
||||||
request('http://pi:4000', (err, res, body) => {
|
// request('http://pi:4000', (err, res, body) => {
|
||||||
if (res.statusCode == 200) rpis.push(JSON.parse(body))
|
// if (res.statusCode == 200) rpis.push(JSON.parse(body))
|
||||||
io.emit('data', rpis)
|
// io.emit('data', rpis)
|
||||||
})
|
// })
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
Loading…
Reference in New Issue
Block a user