-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
148 lines (137 loc) · 4.26 KB
/
worker.js
File metadata and controls
148 lines (137 loc) · 4.26 KB
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
const {spawn} = require('child_process');
const {datastore} = require('nedb-promise');
const DateDiff = require('date-diff');
const ard_1 = spawn("python",['/home/pi/Documents/CSI_final/pro_one.py']);
const ard_2 = spawn("python",['/home/pi/Documents/CSI_final/pro_two.py']);
const room1_status =[0]
const room2_status =[0,0,0,0]
switch_act = new datastore({ filename: '/home/pi/Documents/CSI_final/data/switch_act.db', autoload: true });
async function rooms(req,res,next) {
let switches =await switch_act.find({});
let ib = [];
let room1 = {room:"1",switches:[]};
let room2 = {room:"2",switches:[]};
for(i of switches){
if(i.room=="1"){
var ob={};
ob[i.switch]=i.status;
room1.switches.push(ob);
}
else{
var ob={};
ob[i.switch]=i.status;
room2.switches.push(ob);
}
}
res.send([room1,room2]);
}
async function consumption(req,res,next){
console.log('log');
let switches =await switch_act.find({});
let ib = [];
let room1 = {switches:[]};
let room2 = {switches:[]};
for(i of switches){
if(i.room=="1"){
var ob={};
ob[i.switch]=i.total_consumption;
room1.switches.push(ob);
}
else{
var ob={};
ob[i.switch]=i.total_consumption;
room2.switches.push(ob);
}
}
console.log('log');
res.send([room1,room2]);
}
async function toggle(req,res,next){
let room = parseInt(String(req.body.room));
let switcha = parseInt(String(req.body.switch));
let val = -1;
let status ="";
if(room==1){
let doc = await switch_act.findOne({"room":room,"switch":switcha});
console.log(room1_status[switcha-1])
if(room1_status[switcha-1]==0){
val = 1;
try{
ard_1.stdin.write('1\n');
status="active"
}
catch(e){
console.log(e)
}
}
else{
val = 0;
try{
let now = new Date();
let prev = new Date(doc.since);
let Dif = new DateDiff(now,prev);
let secs = Dif.seconds();
let updated_consume = ((secs/3600)*doc.power_rating)+doc.total_consumption;
switch_act.update({"room":room,"switch":switcha},{'$set':{"total_consumption":updated_consume}})
ard_1.stdin.write('2\n');
status="inactive"
}
catch(e){
console.log(e)
}
}
room1_status[switcha-1]=val;
}
else{
let doc = await switch_act.findOne({"room":room,"switch":switcha});
if(room2_status[switcha-1]==0){
val = 1;
ard_2.stdin.write(String(switcha+4)+"\n");
status="active"
}
else{
let now = new Date();
let prev = new Date(doc.since);
let Dif = new DateDiff(now,prev);
let secs = Dif.seconds();
let updated_consume = ((secs/3600)*doc.power_rating)+doc.total_consumption;
switch_act.update({"room":room,"switch":switcha},{'$set':{"total_consumption":updated_consume}})
val = 0;
ard_2.stdin.write(String(switcha)+"\n");
status="inactive"
}
room2_status[switcha-1]=val;
}
switch_act.update({"room":room,"switch":switcha},{'$set':{"status":status,"since":new Date().toUTCString()}})
res.send("done")
}
async function init(req,res,next){
let r = [[1],[1,2,3,4]];
let index = 1;
for(i of r){
console.log(i);
for(j of i){
switch_act.insert({"room":index,"switch":j,"status":"inactive","since":new Date().toUTCString(),"total_uptime":0,"total_consumption":0,"power_rating":50 })
}
index += 1;
}
res.send('done');
}
async function initdb(){
let switches =await switch_act.find();
for(i of switches){
if(i.room==1){
room1_status[i.switch-1]=0
}
else{
room2_status[i.switch-1]=0
}
}
console.log(room1_status);
}
//init();
initdb();
exports.rooms = rooms;
exports.consumption = consumption;
exports.toggle = toggle;
exports.init = init;