-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from FMeneguzzo/master
Master
- Loading branch information
Showing
9 changed files
with
279 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"extends": "eslint:recommended", | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"rules": { | ||
"arrow-body-style": "off", | ||
"no-console": "off", | ||
"no-continue": "off", | ||
"brace-style": "error", | ||
"curly": "error", | ||
"default-case": "error", | ||
"eqeqeq": "error", | ||
"guard-for-in": "error", | ||
"indent": [ | ||
"error", | ||
2, { | ||
"VariableDeclarator": 2, | ||
"SwitchCase": 1 | ||
} | ||
], | ||
"linebreak-style": ["error", "unix"], | ||
"no-var": "error", | ||
"no-eval": "error", | ||
"no-undef": "error", | ||
"no-undef-init": "error", | ||
"no-undefined": "error", | ||
"no-unused-expressions": "error", | ||
"no-useless-call": "error", | ||
"no-useless-concat": "error", | ||
"no-useless-escape": "error", | ||
"no-void": "error", | ||
"no-with": "error", | ||
"one-var": ["error", "never"], | ||
"quotes": ["error", "single"], | ||
"radix": "error", | ||
"semi": ["error", "always"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Dependency directory | ||
node_modules | ||
coverage | ||
|
||
# Other | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,17 @@ | ||
var express = require('express'); | ||
var app = express(); | ||
var morgan = require('morgan'); | ||
var Gpio = require('onoff').Gpio; | ||
const express = require('express'); | ||
const morgan = require('morgan'); | ||
const sensors = require('./server/sensors'); | ||
|
||
// Set GPIO number | ||
var motionSensor = new Gpio(6, 'in', 'both'); | ||
var lightSensor = new Gpio(1,'in', 'both'); | ||
sensors.init(); | ||
|
||
var sensor = { | ||
lightSensor: 0, | ||
timeLightSensorIsOn: 0, | ||
motionSensor: 0, | ||
timeMotionSensorIsOff: 0 | ||
} | ||
const app = express(); | ||
|
||
|
||
function getLightSensorStatus() { | ||
lightSensor.read(function (err, value) { | ||
sensor['lightSensor'] = value; | ||
if(value == 0) { | ||
sensor['timeLightSensorIsOn'] += 3; | ||
} else { | ||
sensor['timeLightSensorIsOn'] = 0; | ||
} | ||
motionSensor.read(function (err, value) { | ||
sensor['motionSensor'] = value; | ||
if(value == 0) { | ||
sensor['timeMotionSensorIsOff'] += 3; | ||
} else { | ||
sensor['timeMotionSensorIsOff'] = 0; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
setInterval(getLightSensorStatus, 3000); | ||
|
||
app.use(express.static(__dirname + '/public')); | ||
app.use(express.static(__dirname + '/public')); | ||
app.use(morgan('dev')); | ||
|
||
app.get('/status', function (req, res) { | ||
res.send(sensor); | ||
}) | ||
|
||
process.on('SIGINT', function () { | ||
motionSensor.unexport(); | ||
app.get('/status', (req, res) => { | ||
res.status(200).json(sensors.getSensorsStatus()); | ||
}); | ||
|
||
app.listen(80); | ||
console.log("App listening on port 80"); | ||
console.log('App listening on port 80'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const Gpio = process.env.NODE_ENV === 'development' ? | ||
function Gpio() { | ||
this.read = (cb) => cb(null, 0); | ||
this.unexport = () => {}; | ||
} : | ||
require('onoff').Gpio; | ||
|
||
// Set GPIO number | ||
const motionSensor = new Gpio(6, 'in', 'both'); | ||
const lightSensor = new Gpio(1, 'in', 'both'); | ||
|
||
process.on('SIGINT', () => { | ||
lightSensor.unexport(); | ||
motionSensor.unexport(); | ||
process.exit(); | ||
}); | ||
|
||
const defaultStatus = { | ||
value: 0, | ||
timeOn: 0, | ||
timeOff: 0 | ||
}; | ||
|
||
const status = { | ||
lightSensor: Object.assign({}, defaultStatus), | ||
motionSensor: Object.assign({}, defaultStatus) | ||
}; | ||
|
||
const init = () => { | ||
return setInterval(() => { | ||
updateSensorStatus(lightSensor, status.lightSensor, 0); | ||
updateSensorStatus(motionSensor, status.motionSensor, 1); | ||
}, 3000); | ||
}; | ||
|
||
const updateSensorStatus = (sensor, statusObj, onStatus) => { | ||
sensor.read((err, value) => { | ||
const eq = value === onStatus; | ||
statusObj.value = value; | ||
statusObj.timeOn = 0 + ((+ eq) * (statusObj.timeOn + 3)); | ||
statusObj.timeOff = 0 + ((+!eq) * (statusObj.timeOff + 3)); | ||
}); | ||
}; | ||
|
||
const getSensorsStatus = () => { | ||
return Object.assign({}, status); | ||
}; | ||
|
||
module.exports = { | ||
init: init, | ||
updateSensorStatus: updateSensorStatus, | ||
getSensorsStatus: getSensorsStatus | ||
}; |
Oops, something went wrong.