-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_redis.js
189 lines (152 loc) · 4.14 KB
/
app_redis.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var http = require("http"),
express = require("express"),
fs = require("fs"),
ejs = require("ejs"),
db = require("./data"),
RedisStore = require('connect-redis')(express),
sun = require('./plugins/sunInterface.js'),
scriptlib = require("./scriptlib"),
Q = require("q"),
g = require("./globals");
var redis = require('redis').createClient();
// start the http app server
var app = express();
// call this in the importing code so that we can trace in and debug
app.configure(function() {
app.use(express.static(g.htmlBase));
app.set('view options', { layout: false});
app.set('views', g.htmlBase);
// app.set('view engine','html');
app.engine('html', ejs.renderFile);
app.engine('ejs', ejs.renderFile);
// use redis for cookie/session data
app.use(express.cookieParser('some secret'));
app.use(express.session({
store: new RedisStore({host: 'localhost', port:3000, client: redis}),
secret: 'gallifrey'
}));
// add a middleware to expose the session variable to the templating engine
app.use(function(req,res,next) {
if (req.session) {
res.locals.session = req.session;
}
next();
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
console.log('express is configured');
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
function triggertext() {
var v = "on " + ev.trigger + (ev.value ? "is " + ev.value : "");
return v;
}
function actiontext(act) {
switch (act.do) {
case 'speak':
case 'device':
case 'event':
case 'play':
default:
return act.do + " blah blah";
}
}
function dodefault(res) {
res.render('default.html');
}
app.get('/', function(req, res) {
console.log("redirect / to login1.html");
req.session = null;
dodefault(res);
});
//
// driver -> event list
// typename x10,timer variable
//
function refreshMain(req,res) {
res.render('default.html');
}
// trigger is driver + value
// cron "* * 5 * * *"
// device "motiondetector5 on"
// sunrise
//
// action is
// device "light7" off
// speak "oh lordy"
// device "tempvar" 1
// event "bedtimesequence"
// curl "http://pushover...."
//
//
// add a device
app.post("/device", function(req,res) {
// device is
// name, location, group, drivername
// id
db.addDevice(req.body.name, req.body.location, req.body.group, req.body.drivername,
req.body.id).
then(function() {
refreshMain(req,res);
});
});
app.get("/cmd/loglimit", function(req,res) {});
app.get("/log", function(req,res) {
res.send(200,g.loglist);
});
//get devices
app.get("/device", function(req,res) {
if (req.name) {
db.device(req.name).then(function(dev) {
res.send(200,dev);
});
}
else db.allDevices().then(function(devs) {
res.send(200, devs);
});
});
// update a device
app.put("/device", function(req,res) {
var name = req.name;
if (g.devicemap[name]) {
g.devicemap = req;
res.send(200);
}
else res.send(404);
});
app.get("/location",function(req,res) {
res.send(200, sun.location);
});
// add an event
app.post("/event", function(req,res) {
});
app.get("/cmd/device", function(req,res) {
scriptlib.deviceAction(req.query.name, req.query.value);
res.send(200);
});
// http://localhost:82/cmd/event?name=Lights%20out%20bedtime
app.get("/cmd/event", function (req,res) {
g.runEventActions(req.query.name);
res.send(200);
// /cmd/event?name=driveway
// /cmd/device?name=doorbell&val=on
});
//get events
//TODO: should return JSON not html
app.get("/event", function(req,res) {
if (req.name) {
db.events(req.name).then(function(e) {
res.send(200, e);
});
}
else {
db.allEventsActions().then(function(evs) { res.send(200, evs);});
}
});
module.exports = app;