@@ -22,93 +22,90 @@ const {
22
22
23
23
const gpio = require ( 'gpio' ) ;
24
24
25
- class GpioOutProperty extends Property {
25
+ class GpioInProperty extends Property {
26
26
constructor ( thing , name , value , metadata , config ) {
27
- const valueObject = new Value ( value , ( value ) => {
28
- this . handleValueChanged && this . handleValueChanged ( value ) ;
27
+ const valueObject = new Value ( Boolean ( value ) , ( ) => {
29
28
} ) ;
30
29
super ( thing , name , valueObject ,
31
30
{
32
- '@type' : 'OnOffProperty ' ,
31
+ '@type' : 'BooleanProperty ' ,
33
32
label : ( metadata && metadata . label ) || `On/Off: ${ name } ` ,
34
33
type : 'boolean' ,
35
- description : ( metadata && metadata . description ) ||
36
- ( `GPIO Actuator on pin=${ config . pin } ` ) ,
34
+ readOnly : true ,
35
+ description :
36
+ ( metadata && metadata . description ) ||
37
+ ( `GPIO Sensor on pin=${ config . pin } ` ) ,
37
38
} ) ;
38
- const _this = this ;
39
+ const self = this ;
39
40
this . config = config ;
40
-
41
+ const callback = ( ) => {
42
+ log ( `log: GPIO: ${ self . getName ( ) } : open:` ) ;
43
+ self . port . on ( 'change' , ( value ) => {
44
+ value = Boolean ( value ) ;
45
+ log ( `log: GPIO: ${ self . getName ( ) } : change: ${ value } ` ) ;
46
+ self . value . notifyOfExternalUpdate ( value ) ;
47
+ } ) ;
48
+ } ;
41
49
this . port = gpio . export ( config . pin ,
42
- { direction : 'out' ,
43
- ready : ( ) => {
44
- log ( `log: GPIO: ${ _this . getName ( ) } : open:` ) ;
45
- _this . handleValueChanged = ( value ) => {
46
- try {
47
- log ( `log: GPIO: ${ _this . getName ( ) } : \
48
- writing: ${ value } ` ) ;
49
- _this . port . set ( value ) ;
50
- } catch ( err ) {
51
- console . error ( `error: GPIO:
52
- ${ _this . getName ( ) } : Fail to write: ${ err } `) ;
53
- return err ;
54
- }
55
- } ;
56
- } } ) ;
50
+ { direction : 'in' , ready : callback } ) ;
57
51
}
58
52
59
53
close ( ) {
60
- const _this = this ;
61
54
try {
62
55
this . port && this . port . unexport ( this . config . pin ) ;
63
56
} catch ( err ) {
64
- console . error ( `error: GPIO: ${ this . getName ( ) } : Fail to close: ${ err } ` ) ;
57
+ console . error ( `error: GPIO: ${ this . getName ( ) } close:${ err } ` ) ;
65
58
return err ;
66
59
}
67
- log ( `log: GPIO: ${ _this . getName ( ) } : close:` ) ;
60
+ log ( `log: GPIO: ${ this . getName ( ) } : close:` ) ;
68
61
}
69
62
}
70
63
71
-
72
- class GpioInProperty extends Property {
64
+ class GpioOutProperty extends Property {
73
65
constructor ( thing , name , value , metadata , config ) {
74
- super ( thing , name , new Value ( Boolean ( value ) ) ,
66
+ const valueObject = new Value ( value , ( value ) => {
67
+ this . handleValueChanged && this . handleValueChanged ( value ) ;
68
+ } ) ;
69
+ super ( thing , name , valueObject ,
75
70
{
76
- '@type' : 'BooleanProperty ' ,
71
+ '@type' : 'OnOffProperty ' ,
77
72
label : ( metadata && metadata . label ) || `On/Off: ${ name } ` ,
78
73
type : 'boolean' ,
79
- readOnly : true ,
80
- description :
81
- ( metadata && metadata . description ) ||
82
- ( `GPIO Sensor on pin=${ config . pin } ` ) ,
74
+ description : ( metadata && metadata . description ) ||
75
+ ( `GPIO Actuator on pin=${ config . pin } ` ) ,
83
76
} ) ;
84
- const _this = this ;
77
+ const self = this ;
85
78
this . config = config ;
86
- const callback = ( ) => {
87
- log ( `log: GPIO: ${ _this . getName ( ) } : open:` ) ;
88
- _this . port . on ( 'change' , ( value ) => {
89
- value = Boolean ( value ) ;
90
- log ( `log: GPIO: ${ _this . getName ( ) } : change: ${ value } ` ) ;
91
- _this . value . notifyOfExternalUpdate ( value ) ;
92
- } ) ;
93
- } ;
79
+
94
80
this . port = gpio . export ( config . pin ,
95
- { direction : 'in' , ready : callback } ) ;
81
+ { direction : 'out' ,
82
+ ready : ( ) => {
83
+ log ( `log: GPIO: ${ self . getName ( ) } : open:` ) ;
84
+ self . handleValueChanged = ( value ) => {
85
+ try {
86
+ log ( `log: GPIO: ${ self . getName ( ) } : \
87
+ writing: ${ value } ` ) ;
88
+ self . port . set ( value ) ;
89
+ } catch ( err ) {
90
+ console . error ( `error: GPIO:
91
+ ${ self . getName ( ) } : Fail to write: ${ err } `) ;
92
+ return err ;
93
+ }
94
+ } ;
95
+ } } ) ;
96
96
}
97
97
98
98
close ( ) {
99
- const _this = this ;
100
99
try {
101
- this . inverval && clearInterval ( this . inverval ) ;
102
100
this . port && this . port . unexport ( this . config . pin ) ;
103
101
} catch ( err ) {
104
- console . error ( `error: GPIO: ${ this . getName ( ) } close:${ err } ` ) ;
102
+ console . error ( `error: GPIO: ${ this . getName ( ) } : Fail to close: ${ err } ` ) ;
105
103
return err ;
106
104
}
107
- log ( `log: GPIO: ${ _this . getName ( ) } : close:` ) ;
105
+ log ( `log: GPIO: ${ self . getName ( ) } : close:` ) ;
108
106
}
109
107
}
110
108
111
-
112
109
function GpioProperty ( thing , name , value , metadata , config ) {
113
110
if ( config . direction === 'out' ) {
114
111
return new GpioOutProperty ( thing , name , value , metadata , config ) ;
0 commit comments