-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
executable file
·101 lines (85 loc) · 2.93 KB
/
generate.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
#!/usr/local/bin/node
/*
* Copyright 2015-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
'use strict'
var DEBUG = false
var self = this
if (!DEBUG) {
var methods = ['log', 'debug', 'warn', 'info']
for (var i = 0;i < methods.length;i++) {
console[methods[i]] = function () {}
}
}
var cli = require('cli')
var crypto = require('crypto')
var options = cli.parse({
// station: ['s', 'Station id', 'string', false],
sensor: ['s', 'Sensor Id', 'string', false],
state: ['t', 'State', 'string', 'false'],
city: ['c', 'City', 'string', false],
longtitude: ['longtitude', 'Longtitude', 'string', false],
latitude: ['latitude', 'Latitude', 'string', false]
})
if (options.sensor == undefined || !options.sensor) {
console.log('A sensor id must be supplied')
process.exit(1)
}
if (options.state == undefined || !options.state) {
console.log('A state name must be supplied')
process.exit(1)
}
if (options.city == undefined || !options.city) {
console.log('A city name must be supplied')
process.exit(1)
}
if (options.longtitude == undefined || !options.longtitude) {
console.log('A longtitude must be supplied')
process.exit(1)
}
if (options.latitude == undefined || !options.latitude) {
console.log('A latitude must be supplied')
process.exit(1)
}
var _ = require('lodash')
var log = require('./lib/modules/log')
var station = require('./lib/modules/station')
var Rainfall = require('./lib/modules/sensors/rainfall')
var Temperature = require('./lib/modules/sensors/temperature')
var Vibration = require('./lib/modules/sensors/vibration')
var WindSpeed = require('./lib/modules/sensors/windspeed')
log.init({
destination: ['local'],
defaultLevel: 'debug',
name: 'weatherGen'
})
var deviceIdHash = crypto.createHash('sha256').update(options.sensor).digest('hex').toString()
var rainfall = new Rainfall('rain', 'rainfall', deviceIdHash.substr(0, 16))
var temperature = new Temperature('temp', 'temperature', deviceIdHash.substr(16, 16))
var vibration = new Vibration('vib', 'vibration', deviceIdHash.substr(32, 16))
var windspeed = new WindSpeed('ws', 'windspeed', deviceIdHash.substr(48, 16))
var sensors = [rainfall, temperature, vibration, windspeed]
station.init(options, function () {
_.each(sensors, function (sensor, index) {
station.attachSensor(sensor)
sensor.start()
})
})
process.on('SIGINT', function () {
station.kill()
.then(function () {
console.log('finished')
process.exit()
})
})