-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
68 lines (58 loc) · 1.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const Koa = require('koa');
const Logger = require('koa-logger')
const Router = require('koa-router');
const Bodyparser = require('koa-bodyparser');
const Views = require('koa-views')
const Static = require('koa-static')
const path = require('path')
const app = new Koa();
const router = new Router();
app.use(Logger());
app.use(Static(path.join(__dirname, '/dist')));
app.use(Views(path.join(__dirname, './views'), {
extension: 'ejs'
}));
const Gpio = require('onoff').Gpio;
// 应用配置
const config = require('./config.js')
let Switch = {};
config.gpio_pin.forEach(gpio => {
Switch[gpio] = Switch[gpio] || {};
Switch[gpio].o = new Gpio(gpio, 'out');
Switch[gpio].pin = gpio;
});
// 首页
router.get('/', async (ctx) => {
ctx.type = 'html';
let list = [];
config.gpio_pin.forEach(gpio => {
list.push({
pin : gpio,
state : Switch[gpio].o.readSync()
})
});
await ctx.render('index',{
data:list
})
})
// 开
router.get('/on', async(ctx)=>{
ctx.response.set('Cache-Control','no-cache');
let pin = ctx.request.query.pin;
let o = Switch[pin].o;
o.writeSync(1);
ctx.redirect('/');
})
// 关
router.get('/off', async(ctx)=>{
ctx.response.set('Cache-Control','no-cache');
let pin = ctx.request.query.pin;
let o = Switch[pin].o;
o.writeSync(0);
ctx.redirect('/');
})
// get状态查询
// post重启服务
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(8080);