-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush
executable file
·141 lines (124 loc) · 4.55 KB
/
push
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
#!/usr/bin/env node
/*
* This cli places a file in your home directory named `~/.fido-<name>`, where `<name>` is the value you give to the `name` option.
*
* This file is crucial for maintaining state of the CLI between start and stops.
* Understanding the file:
* File with a value means the program has started and the last reading was out of bounds.
* Empty file means the program has started and the last reading was in bounds.
* File with a value means the program has started and the last reading was out of bounds.
*/
var log = console.log
var fs = require('fs')
var program = require('commander')
var exec = require('child_process').exec;
var push = require('./lib/SendEmail.js')
var home = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
var isJson = require('is-json')
var _ = require('underscore')
program
.version('0.0.1')
.option('--name <name>', 'A unique name for this Fido')
.option('--minimum <repository>')
.option('--maximum <destination>')
.option('--emailTo <address>', 'A JSON object to feed into the templates. You must provide all variables.')
.option('--emailUserName <user name>')
.option('--emailPassword <password>')
.option('--emailServer <smtp server address>', 'For example, Gmail is smpt.gmail.com:587')
.option('--emailFrom <an email from address>')
.option('--sensorId <sensor id>')
.option('--verbose')
.parse(process.argv);
pushOptions = program
// convert string to int
program.minimum = parseFloat(program.minimum)
program.maximum = parseFloat(program.maximum)
if(program.verbose) log('minimum: ' + program.minimum)
if(program.verbose) log('maximum: ' + program.maximum)
var receivedValue = ''
process.stdin.on('error', function(e) {
console.log("error: "+e)
})
process.stdin.on('data', function(chunk) {
if (chunk)
receivedValue += chunk
})
process.stdin.on('end', function() {
if(program.verbose) log('receivedValue: ' + receivedValue)
sniff()
})
/*
* Sniff STDIN to see if it is in or out of bounds, then check the process file to see what to do.
*/
function sniff() {
// If our receivedValue is JSON, get our actual sensor value and assign it to recievedValue.
if (isJson(receivedValue) === true) {
var packet = JSON.parse(receivedValue)
if (program.hasOwnProperty('sensorId')) {
receivedValue = packet[program.sensorId]
}
else {
// If we haven't been told a sensor_id, then use the first value in the array.
receivedValue = packet[(_.keys(packet))[0]]
}
}
// Make sure recievedValue is a float
receivedValue = parseFloat(receivedValue)
if (receivedValue === NaN) console.error('Error: Received value is not a number')
fs.readFile(home + '/.fido-' + program.name, {encoding: 'utf8'}, function(err, value) {
// No file means the program has not yet started.
if (err) {
salute()
createProcessFile()
}
// Empty file means the program has started and the last reading was in bounds.
else if (value == '') {
if (receivedValue > program.maximum) {
bark(program.name + ' has detected a value of ' + receivedValue + ' which is more than your maximum of ' + program.maximum)
}
else if (receivedValue < program.minimum) {
bark(program.name + ' has detected a value of ' + receivedValue + ' which is less than your minimum of ' + program.minimum)
}
}
// File with a value means the program has started and the last reading was out of bounds.
else {
// Do nothing unless the receivedValue is back in bounds, in which case blank the file
if (receivedValue < program.maximum && receivedValue > program.minimum) {
fs.writeFile(home + '/.fido-' + program.name, '')
}
}
})
}
/*
* Warn that receivedValue has gone out of bounds.
*/
function bark(message) {
if (program.verbose) log('Triggered...')
pushOptions.message = message
pushOptions.subject = 'Fido Alert for ' + program.name
fs.open(home + '/.fido-' + program.name, 'w', function(err, fd) {
var buffer = new Buffer(receivedValue.toString())
fs.write(fd, buffer, 0, buffer.length, null, function(err) {
push(pushOptions, function(err) {
if (err) log(err)
})
})
})
}
/*
* Announce the program is now on.
*/
function salute() {
pushOptions.subject = 'Hello from ' + program.name
push(pushOptions, function(err) {
if (err) log(err)
})
}
function createProcessFile() {
fs.open(home + '/.fido-' + program.name, 'w', function(err, fd) {
var buffer = new Buffer('')
fs.write(fd, buffer, 0, buffer.length, null, function(err) {
if (err) log(err)
})
})
}