-
Notifications
You must be signed in to change notification settings - Fork 2
/
omega_garage.js
175 lines (145 loc) · 4.32 KB
/
omega_garage.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
var method = omegaGarage.prototype;
var GPIOHelper = require('./gpiohelper');
var emailClient = require('./emailClient');
var temphum = require('./temphum');
var camera = require('./camera');
relaysStates = [0, 0];
config = {};
garageDoorsLength = 0;
myGPIO = new GPIOHelper();
function omegaGarage() {
}
omegaGarage.prototype.init = function()
{
try
{
loadConfigFile();
garageDoorsLength = config.garageDoors.length;
for(var i = 0; i < garageDoorsLength; i++)
{
console.log("Creating relay for " + config.garageDoors[i].garageName + " garage on pin: " + config.garageDoors[i].relayPin);
myGPIO.setPinSync(config.garageDoors[i].relayPin, 0);
console.log("Creating sensor input for " + config.garageDoors[i].garageName + " garage on pin: " + config.garageDoors[i].sensorPin);
myGPIO.setPinSync(config.garageDoors[i].sensorPin);
}
emailClient.init(config.UserEmail, config.UserPassword, config.RecipientEmail, config.EmailHost);
temphum.init(config.TempSensorEnabled, config.TempSensorOffset);
camera.init(config.CameraEnabled);
setInterval(beginStateUpdates, 5000);
}
catch(e)
{
console.log("Error initializing: " + e);
}
};
omegaGarage.prototype.getGarageState = function(garageDoorIndex)
{
try
{
var strResult = "";
if(relaysStates[garageDoorIndex] == 0)
strResult = "OPEN";
else
strResult = "CLOSED";
console.log("The " + config.garageDoors[garageDoorIndex].garageName + " garage is " + strResult);
return strResult;
}
catch(e)
{
console.log("Error getting garage state: " + e);
return "OPEN";
}
}
omegaGarage.prototype.changeGarageState = function(garageDoorIndex)
{
try
{
console.log("Changing the state of the " + config.garageDoors[garageDoorIndex].garageName + " garage.");
this.setRelayState(garageDoorIndex, 1);
var obj = this;
setTimeout(function()
{
obj.setRelayState(garageDoorIndex, 0);
}, 1000);
}
catch(e)
{
console.log("Error changing the garage state: " + e);
}
};
omegaGarage.prototype.closePins = function()
{
for(var i = 0; i < this.garageDoorsLength; i++)
{
console.log("Closing relay pin: " + config.garageDoors[i].relayPin);
myGPIO.closepin(config.garageDoors[i].relayPin);
console.log("Closing sensor pin: " + config.garageDoors[i].sensorPin);
myGPIO.closepin(config.garageDoors[i].sensorPin);
}
};
omegaGarage.prototype.setRelayState = function(garageDoorIndex, value)
{
myGPIO.setPinSync(config.garageDoors[garageDoorIndex].relayPin, value);
}
omegaGarage.prototype.getAllGarageStates = function()
{
var obj = [];
for(var i = 0; i < garageDoorsLength; i++)
obj.push(relaysStates[i]);
return obj;
}
////////////////////////////////PRIVATE FUNCTIONS///////////////////////////////
function loadConfigFile()
{
try
{
console.log("Loading configuration file...");
var home = process.env.HOME;
config = require('/root/config.json');
console.log("Configuration file loaded..." + JSON.stringify(config));
}
catch (e)
{
console.log('Error loading the configuration file:', e);
process.exit();
}
};
function beginStateUpdates()
{
for(var i = 0; i < garageDoorsLength; i++)
{
updateGarageState(i);
}
}
function updateGarageState(garageDoorIndex)
{
try
{
console.log("Updating garage door states");
var result = myGPIO.getPinSync(config.garageDoors[garageDoorIndex].sensorPin);
if(result != relaysStates[garageDoorIndex])//If the state of the garage has changed, then notify the user.
{
var state = "";
if(result == 0)
state = " garage is now open";
else
state = " garage is now closed";
camera.takePicture(doneTakingPicture);
function doneTakingPicture(didCapture)
{
var subject = config.garageDoors[garageDoorIndex].garageName + state;
var message = "The " + config.garageDoors[garageDoorIndex].garageName + state;
if(didCapture)
emailClient.sendEmail(subject, message, "/root/garageImage.jpg");
else
emailClient.sendEmail(subject, message, null);
}
}
relaysStates[garageDoorIndex] = result;
}
catch(e)
{
console.log("Error getting garage state: " + e);
}
}
module.exports = new omegaGarage();