forked from rjcorwin/farmos-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
push
executable file
·114 lines (98 loc) · 3.23 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
#!/usr/bin/env node
var split = require('split')
var isJson = require('is-json')
var options = parseArgv(process.argv)
var child_process = require('child_process')
var url = require('url')
var _ = require('underscore')
// If help flag is set, print help text and exit.
if (options.hasOwnProperty('help')) {
console.log('./push')
console.log(' --public_key <public_key> Example: IFEfji3f3ns')
console.log(' --private_key <private_key> Example:Xifesfisffsi')
console.log(' --url <url> Example: http://farmos.pantheon.io')
console.log(' --verbose Use for debug output')
console.log('')
console.log('Usage: echo 42 | ./push --public_key temperature_probe1 --private_key IDFJEFIEFJ --url http://farmos.pantheon.io')
console.log('Usage: echo \'{"temperature":89, "humidity":0.4, "luminosity":0.8}\' | ./push --public_key temperature_probe1 --private_key IDFJEFIEFJ --url http://farmos.pantheon.io')
console.log('')
process.exit(0)
}
// Output any errors we receive from the pipe.
process.stdin.on('error', function(e) {
console.log("error: "+e)
})
// As we get data in on the pipe, concatenate it onto data until a new line is detected at which point we push the data.
var data
process.stdin.pipe(split())
.on('data', function (line) {
data = line
if (data !== '')
console.log(data)
push(data)
})
// When we detect the process piping to this cli has exited, exitSignal is set to true and we also exit this process after sending data.
var exitSignal = false
process.stdin.on('end', function() {
exitSignal = true
})
function push(data) {
if (data == '') return
var options = parseArgv(process.argv)
console.log(options)
log('data:')
log(data)
log(options)
// Split out into many
var packets = []
if (isJson(data) == true) {
data = JSON.parse(data)
}
else {
data = {
'value': data,
}
}
log('data:')
log(data)
options.url = url.parse(options.url)
var destination = options.url.protocol + '//' + options.url.host + '/farm/sensor/listener/' + options.public_key + '?private_key=' + options.private_key
var cmd = 'curl -XPOST ' + destination + ' -d \'' + JSON.stringify(data) + '\''
log('cmd:')
log(cmd)
child_process.exec(cmd, function(err, stdout, stderr) {
if (err) console.log(err)
if (stderr) console.log(stderr)
log(stdout)
})
}
// This is the standard function for parsing process.argv. It will return
// an object that where `--argumentName` is a property name and the
// proceding argument is the value. If there is no proceeding value then
// it will be interpreted as a boolean true.
// @todo Consider argv-ee
function parseArgv(argv) {
params = {}
argv.forEach(function(arg, i) {
if (arg.substr(0, 2) == '--') {
paramName = arg.substr(2, arg.length)
nextArg = argv[i+1]
if ((typeof nextArg == 'string' && nextArg.substr(0, 2) == '--') || nextArg == undefined) {
params[paramName] = true
}
else {
params[paramName] = nextArg
}
}
})
return params
}
// A log function that will only log if the --verbose flag is set
function log(msg) {
if (typeof msg == 'object') msg = JSON.stringify(msg)
if (params.hasOwnProperty('verbose')) {
console.log(msg)
console.log('')
console.log('')
}
}