@@ -10,13 +10,13 @@ module.exports = function (app) {
10
10
11
11
12
12
router . get ( '/' , function ( req , res , next ) {
13
- // load the single view file (angular will handle the page changes on the front-end)
14
- res . sendfile ( './public/index.html' ) ;
13
+ // load the single view file (angular will handle the page changes on the front-end)
14
+ res . sendfile ( './public/index.html' ) ;
15
15
} ) ;
16
16
17
17
router . get ( '/getCurrentTemperatur' , function ( req , res , next ) {
18
- var response = { } ;
19
- fs . readFile ( config . data . pathTow1_slave + '/' + config . device . serial + '/' + config . data . w1_slaveFilename , 'utf8' , function ( err , data ) {
18
+ var response = { } ;
19
+ fs . readFile ( config . data . pathTow1_slave + '/' + config . device . serial + '/' + config . data . w1_slaveFilename , 'utf8' , function ( err , data ) {
20
20
if ( err ) {
21
21
return console . log ( err ) ;
22
22
}
@@ -30,55 +30,70 @@ router.get('/getCurrentTemperatur', function (req, res, next) {
30
30
31
31
router . get ( '/enableLed/:gpiopin' , function ( req , res , next ) {
32
32
var gpiopin = req . params . gpiopin ;
33
- gpio . open ( gpiopin , "output" , function ( err ) {
34
- if ( err ) {
35
- console . error ( 'ERROR: Can not open gpio pin ' + gpiopin ) ;
36
- res . json ( { status : true } ) ;
37
- }
38
- else {
39
- gpio . write ( gpiopin , 1 , function ( ) {
40
- console . log ( 'LED enabled' + gpiopin ) ;
41
- res . json ( { status : true } ) ;
33
+ gpio . read ( gpiopin , function ( err , value ) {
34
+ if ( value != 1 ) {
35
+ gpio . open ( gpiopin , "output" , function ( err ) {
36
+ if ( err ) {
37
+ console . error ( 'ERROR: Can not open gpio pin ' + gpiopin ) ;
38
+ res . json ( { status : false } ) ;
39
+ }
40
+ else {
41
+ gpio . write ( gpiopin , 1 , function ( ) {
42
+ console . log ( 'LED enabled' + gpiopin ) ;
43
+ res . json ( { status : true } ) ;
44
+ } ) ;
45
+ }
42
46
} ) ;
47
+ } else {
48
+ res . json ( { status : true } ) ;
43
49
}
44
50
} ) ;
51
+
45
52
} ) ;
46
53
47
54
router . get ( '/disableLed/:gpiopin' , function ( req , res , next ) {
48
55
var gpiopin = req . params . gpiopin ;
49
- gpio . open ( gpiopin , "output" , function ( err ) { // Open pin 16 for output
50
- gpio . write ( gpiopin , 1 , function ( ) { // Set pin 16 high (1)
56
+ gpio . read ( gpiopin , function ( err , value ) {
57
+ if ( value == 1 ) {
51
58
gpio . close ( gpiopin ) ;
52
- } ) ;
59
+ }
60
+ res . json ( { status : value } ) ;
53
61
} ) ;
54
- res . json ( { status : false } ) ;
62
+
63
+
55
64
} ) ;
56
65
57
66
router . get ( '/getLedStatus' , function ( req , res , next ) {
58
- var redLedStatus = 0 ;
59
- gpio . read ( 11 , function ( err , value ) {
60
- console . log ( '11 = ' + value ) ;
61
- redLedStatus = value ; // The current state of the pin
67
+ var response = [ {
68
+ color : 'red' ,
69
+ gpiopin : 11 ,
70
+ status : false
71
+ } ,
72
+ {
73
+ color : 'yellow' ,
74
+ gpiopin : 13 ,
75
+ status : false
76
+ } ,
77
+ {
78
+ color : 'green' ,
79
+ gpiopin : 15 ,
80
+ status : false
81
+ }
82
+ ]
83
+ gpio . read ( 11 , function ( err , value ) {
84
+ response [ 0 ] . status = ( value ) ? 1 : 0 ;
85
+ gpio . read ( 13 , function ( err , value ) {
86
+ response [ 1 ] . status = ( value ) ? 1 : 0 ;
87
+ gpio . read ( 15 , function ( err , value ) {
88
+ response [ 2 ] . status = ( value ) ? 1 : 0 ;
89
+ res . json ( response ) ;
90
+ } ) ;
91
+ } ) ;
62
92
} ) ;
63
93
64
- console . log ( '11 = ' + redLedStatus ) ;
65
- var response = [ {
66
- color : 'red' ,
67
- gpiopin : 11 ,
68
- status : redLedStatus
69
- } ,
70
- {
71
- color : 'yellow' ,
72
- gpiopin : 13 ,
73
- status : false
74
- } ,
75
- {
76
- color : 'green' ,
77
- gpiopin : 15 ,
78
- status : false
79
- }
80
- ]
81
-
82
-
83
- res . json ( response ) ;
94
+
95
+
96
+
97
+
98
+
84
99
} ) ;
0 commit comments