-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
executable file
·127 lines (97 loc) · 3.43 KB
/
controller.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
var express = require('express');
var gpio = require('wpi-gpio');
var sleep = require('sleep');
var Influx = require('influx');
var Promise = require("bluebird");
var PythonShell = require('python-shell');
var math = require('mathjs');
var config = require('./config');
var moment = require('moment');
//Configuration de la base de donnes
var db = new Influx.InfluxDB({
host: config.host,
database: config.database,
tags: config.tags
});
console.log("Controller PI46 connecté");
var flag = 'free';
var arrosage = function(req, res) {
res.send('temps :' + req.params.time);
if (flag == 'free') {
flag = 'blocking';
moment.locale('fr');
gpio.BCM_GPIO = true;
Promise.try(() => {
return gpio.output(5, 1)
}).then(() => {
console.log("*** Arrosage en cours de préparation ... ***");
return gpio.write(5, 0)
}).then(() => {
console.log("*** Arrosage activé ***");
db.writePoints([{
"measurement": "arrosage",
"fields": {
"open": moment().format('MMMM Do, h:mm a')
}
}]);
return Promise.delay(req.params.time * 1000);
}).then(() => {
return gpio.write(5, 1)
}).then(() => {
console.log("*** Arrosage terminé ***");
db.writePoints([{
"measurement": "arrosage",
"fields": {
"close": moment().format('MMMM Do, h:mm a')
}
}]);
flag = 'free';
})
} else {
console.log('Arrosage déjà en cours...');
}
}
const niveauCuve = function(req, res) {
new Promise((resolve, reject) => {
console.log("[Mesure Niveau Eau]");
var options = {
mode: 'text',
pythonPath: '/usr/bin/python',
pythonOptions: ['-u'],
scriptPath: '/home/pi/garage/'
}
PythonShell
.run('niveauCuve.py', options, function(err, results) {
if (err) reject(err);
if (results) {
var mediane = [math.median(results)];
console.log('Impulsions :' + results)
console.log('Médiane :' + mediane);
res.send(mediane)
} else {
console.log('niveauCuve - Aucune donnée')
res.send(0)
}
});
})
}
/**
var niveauCuve = function(req, res) {
console.log("Mesure du niveau d'eau en cours...");
var options = {
mode: 'text',
pythonPath: '/usr/bin/python',
pythonOptions: ['-u'],
scriptPath: '/home/pi/garage/'
// make sure you use an absolute path for scriptPath
}
PythonShell.run('niveauCuve.py', options, function (err, results) {
var mediane = [math.median(results)];
console.log('Niveau Cuve :' + results)
console.log('Médiane :' + mediane);
res.send(mediane);
if (err) throw err;
});
}*/
exports.Arrosage = arrosage;
exports.NiveauCuve = niveauCuve;