@@ -6,14 +6,10 @@ const FUDGE = Symbol();
66// ds18b20Device object
77class ds18b20Device {
88 constructor ( val ) {
9- //this.name = val;
10- //this.id = val;
119 this [ DSNAME ] = val ;
12- this [ FUDGE ] = parseFloat ( 0.7 ) ;
10+ this [ FUDGE ] = parseFloat ( 0.0 ) ;
1311
14- console . log ( "ds18b20Device constructor() name = " + this . name ) ;
15-
16- //console.log('New ds18b20Device with id = ' + this.id + ', fudge = ' + this.fudge);
12+ console . log ( 'New ds18b20Device with id = ' + this . id + ', fudge = ' + this . fudge ) ;
1713 }
1814
1915 // Return a list of sensor devices
@@ -36,22 +32,22 @@ class ds18b20Device {
3632 get id ( ) { return this [ DSNAME ] ; }
3733
3834 set fudge ( fudgeFactor ) {
39- this [ FUDGE ] = fudgeFactor ;
35+ this [ FUDGE ] = parseFloat ( fudgeFactor ) ;
4036 }
4137 get fudge ( ) {
4238 return this [ FUDGE ] ;
4339 }
4440
45- get temp ( ) {
41+ getTemp ( ) {
4642 var dpath = '/sys/bus/w1/devices/' + this . id + '/w1_slave' ;
4743 var data = fs . readFileSync ( dpath , 'utf8' ) ;
48- console . log ( '(ds18b20Device) ' + this . id + ' data = ' + data ) ;
49- console . log ( '(ds18b20Device) fudge ' + this . fudge ) ;
50- console . log ( '(ds18b20Device) ' + parseFloat ( data . split ( ' ' ) [ 20 ] . split ( '=' ) [ 1 ] ) / 1000 ) ;
5144 return parseFloat ( this . fudge ) + parseFloat ( data . split ( ' ' ) [ 20 ] . split ( '=' ) [ 1 ] ) / 1000 ;
5245 }
5346
5447 getTempAsync ( callback ) {
48+ if ( ! arguments . length || arguments . length && typeof arguments [ 0 ] !== "function" ) {
49+ return this . getTemp ( ) ;
50+ }
5551 var dpath = '/sys/bus/w1/devices/' + this . id + '/w1_slave' ;
5652 var id = this . id ;
5753 var fudge = this . fudge ;
@@ -65,31 +61,12 @@ class ds18b20Device {
6561 } ) ;
6662 }
6763
64+ get temp ( ) {
65+ return this . getTemp ( ) ;
66+ }
67+
6868}
6969export default ds18b20Device ;
7070
7171
72- /*
73- ds18b20Device.prototype.getTempAsync = function (callback) {
74- var dpath = '/sys/bus/w1/devices/' + this.id + '/w1_slave';
75- var id = this.id;
76- var fudge = parseFloat(this.fudge);
77- fs.readFile(dpath, 'utf8', function (err, data) {
78- if (err) {
79- console.log('Error reading device data: ' + dpath);
80- } else {
81- var result = parseFloat(fudge) + parseFloat(data.split(' ')[20].split('=')[1]) / 1000;
82- callback(result, id);
83- }
84- });
85- };
86-
87- ds18b20Device.prototype.getTemp = function () {
88- var dpath = '/sys/bus/w1/devices/' + this.id + '/w1_slave';
89- var data = fs.readFileSync(dpath, 'utf8');
90- console.log("getTemp(): " + parseFloat(data.split(' ')[20].split('=')[1]) / 1000);
91- return (parseFloat(this.fudge) +parseFloat(data.split(' ')[20].split('=')[1]) / 1000);
92- };
93- */
94-
9572/* ex:set ai shiftwidth=2 inputtab=spaces smarttab noautotab: */
0 commit comments