This commit is contained in:
赵鑫 2022-09-04 19:20:59 +08:00
parent 28198bfcef
commit e3542e5b03
3 changed files with 23 additions and 794 deletions

727
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,12 +15,10 @@
"author": "Zhao Xin <7176466@qq.com>", "author": "Zhao Xin <7176466@qq.com>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"cron": "^2.1.0",
"dotenv": "^16.0.2", "dotenv": "^16.0.2",
"easyimage": "^3.1.1", "easyimage": "^3.1.1",
"express": "^4.18.1", "express": "^4.18.1",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"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"
} }

View File

@ -3,74 +3,43 @@ const port = process.env.PORT || 3000
const host = process.env.HOST || 'localhost' 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 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, host, () => console.log(`rpi sense hat server is running at http://${host}:${port}`))
const io = socketio(server)
// app.use((req, res, next) => {
// res.setHeader('Access-Control-Allow-Origin', '*')
// res.setHeader('Access-Control-Allow-Methods', 'HEAD,GET,POST,OPTIONS,PATCH,PUT,DELETE')
// res.setHeader('Access-Control-Allow-Headers', 'Origin,X-Requested-With,Authorization,Content-Type,Accept,Z-Key')
// next()
// })
app.get('/', (req, res) => res.sendStatus(200)) app.get('/', (req, res) => res.sendStatus(200))
const server = app.listen(port, host, () => {
console.log(`rpi sense hat server is running at http://${host}:${port}`)
})
const io = socketio(server, { cors: { origin: '*' } })
const R = [255, 0, 0]
const G = [0, 255, 0]
const B = [0, 0, 255]
const X = [0, 0, 0]
const O = [255, 255, 255]
const sensehat = {
leds: [
X, X, X, X, X, X, X, X,
O, O, O, O, O, O, O, O,
X, X, X, X, X, X, X, X,
R, R, R, R, R, R, R, R,
X, X, X, X, X, X, X, X,
G, G, G, G, G, G, G, G,
X, X, X, X, X, X, X, X,
B, B, B, B, B, B, B, B,
]
}
// Sense HAT 初始化 // Sense HAT 初始化
sense.lowLight = true sense.lowLight = true
sense.setRotation(180) sense.setRotation(180)
sense.clear([0, 0, 0]) sense.clear([0, 0, 0])
sense.setPixels(sensehat.leds)
io.on('connection', (client) => { io.on('connection', (client) => {
console.log(`client conncted: ${client.id}`) console.log(`client conncted: ${client.id}`)
client.emit('message', `hello, ${client.id}`) client.emit('message', `hello, ${client.id}`)
}) })
// 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
// )
// 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 }) => {
@ -117,14 +86,3 @@ io.on('connection', (client) => {
// } // }
// } // }
// } // }
// 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)
// })
// })
// }