Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:
Expand All @@ -13,15 +13,15 @@ jobs:
matrix:
node-version: [16.x, 18.x, 20.x] # Updated Node.js versions
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm' # Caches npm modules
- run: npm ci
- name: Install build tools (if needed)
run: sudo apt-get install -y build-essential python3 make
- run: npm rebuild node-gyp # Rebuild node-gyp if necessary
- run: npm run build --if-present
- run: npm test
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm" # Caches npm modules
- run: npm ci
- name: Install build tools (if needed)
run: sudo apt-get install -y build-essential python3 make
- run: npm rebuild node-gyp # Rebuild node-gyp if necessary
- run: npm run build --if-present
- run: npm test
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
- run: npm ci
- run: npm publish
env:
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx pretty-quick --staged
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This sample queries a DHT11 sensor connected to the GPIO 4 and prints out the re
```javascript
var sensor = require("node-dht-sensor");

sensor.read(11, 4, function(err, temperature, humidity) {
sensor.read(11, 4, function (err, temperature, humidity) {
if (!err) {
console.log(`temp: ${temperature}°C, humidity: ${humidity}%`);
}
Expand All @@ -86,30 +86,30 @@ var app = {
{
name: "Indoor",
type: 11,
pin: 17
pin: 17,
},
{
name: "Outdoor",
type: 22,
pin: 4
}
pin: 4,
},
],
read: function() {
read: function () {
for (var sensor in this.sensors) {
var readout = sensorLib.read(
this.sensors[sensor].type,
this.sensors[sensor].pin
this.sensors[sensor].pin,
);
console.log(
`[${this.sensors[sensor].name}] ` +
`temperature: ${readout.temperature.toFixed(1)}°C, ` +
`humidity: ${readout.humidity.toFixed(1)}%`
`humidity: ${readout.humidity.toFixed(1)}%`,
);
}
setTimeout(function() {
setTimeout(function () {
app.read();
}, 2000);
}
},
};

app.read();
Expand All @@ -130,15 +130,15 @@ sensor.initialize(22, 17);
// var readout = sensor.readSync(22, 4);

sensor.read(22, 17).then(
function(res) {
function (res) {
console.log(
`temp: ${res.temperature.toFixed(1)}°C, ` +
`humidity: ${res.humidity.toFixed(1)}%`
`humidity: ${res.humidity.toFixed(1)}%`,
);
},
function(err) {
function (err) {
console.error("Failed to read sensor data:", err);
}
},
);
```

Expand All @@ -152,7 +152,7 @@ async function exec() {
const res = await sensor.read(22, 4);
console.log(
`temp: ${res.temperature.toFixed(1)}°C, ` +
`humidity: ${res.humidity.toFixed(1)}%`
`humidity: ${res.humidity.toFixed(1)}%`,
);
} catch (err) {
console.error("Failed to read sensor data:", err);
Expand All @@ -173,20 +173,20 @@ sensor.initialize({
test: {
fake: {
temperature: 21,
humidity: 60
}
}
humidity: 60,
},
},
});
```

After initialization, we can call the `read` method as usual.

```javascript
sensor.read(22, 4, function(err, temperature, humidity) {
sensor.read(22, 4, function (err, temperature, humidity) {
if (!err) {
console.log(
`temp: ${temperature.toFixed(1)}°C, ` +
`humidity: ${humidity.toFixed(1)}%`
`humidity: ${humidity.toFixed(1)}%`,
);
}
});
Expand Down
6 changes: 3 additions & 3 deletions examples/async-explicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ var count = 0;
var start = 0;
var end = 0;

var iid = setInterval(function() {
var iid = setInterval(function () {
if (++count >= repeats) {
clearInterval(iid);
}

start = new Date().getTime();

sensor.read(sensorType, gpioPin, function(err, temperature, humidity) {
sensor.read(sensorType, gpioPin, function (err, temperature, humidity) {
end = new Date().getTime();
if (err) {
console.warn("" + err);
Expand All @@ -38,7 +38,7 @@ var iid = setInterval(function() {
"temperature: %s°C, humidity: %s%%, time: %dms",
temperature.toFixed(1),
humidity.toFixed(1),
end - start
end - start,
);
}
});
Expand Down
8 changes: 4 additions & 4 deletions examples/duty.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ var count = 0;
var start = 0;
var end = 0;

var iid = setInterval(function() {
var iid = setInterval(function () {
start = new Date().getTime();
sensor.read(sensorType, gpioPin, function(err, temperature, humidity) {
sensor.read(sensorType, gpioPin, function (err, temperature, humidity) {
end = new Date().getTime();
if (err) {
console.warn("" + err);
Expand All @@ -32,7 +32,7 @@ var iid = setInterval(function() {
"temperature: %s°C, humidity: %s%%, time: %dms",
temperature.toFixed(1),
humidity.toFixed(1),
end - start
end - start,
);
fs.appendFile(
"log.csv",
Expand All @@ -44,7 +44,7 @@ var iid = setInterval(function() {
"," +
elapsed +
"\n",
function(err) {}
function (err) {},
);
}
});
Expand Down
10 changes: 5 additions & 5 deletions examples/fake-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ sensor.initialize({
test: {
fake: {
temperature: 21,
humidity: 60
}
}
humidity: 60,
},
},
});

sensor.read(22, 4, function(err, temperature, humidity) {
sensor.read(22, 4, function (err, temperature, humidity) {
if (!err) {
console.log(
"temp: " +
temperature.toFixed(1) +
"°C, " +
"humidity: " +
humidity.toFixed(1) +
"%"
"%",
);
}
});
10 changes: 5 additions & 5 deletions examples/promisse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ var sensor = require("../lib").promises;
sensor
.read(22, 17)
.then(
function(res) {
function (res) {
console.log(
`temp: ${res.temperature.toFixed(1)}°C, ` +
`humidity: ${res.humidity.toFixed(1)}%`
`humidity: ${res.humidity.toFixed(1)}%`,
);
},
function(err) {
function (err) {
console.error("Failed to read sensor data:", err);
}
},
)
.catch(e => console.error("Error: " + e));
.catch((e) => console.error("Error: " + e));
console.log("done");
4 changes: 2 additions & 2 deletions examples/sync-explicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var gpioPin = parseInt(process.argv[3], 10);
var repeats = parseInt(process.argv[4] || "10", 10);
var count = 0;

var iid = setInterval(function() {
var iid = setInterval(function () {
if (++count >= repeats) {
clearInterval(iid);
}
Expand All @@ -32,6 +32,6 @@ var iid = setInterval(function() {
`humidity: ${readout.humidity.toFixed(1)}%, ` +
`valid: ${readout.isValid}, ` +
`errors: ${readout.errors}, ` +
`time: ${end - start}ms`
`time: ${end - start}ms`,
);
}, 2500);
4 changes: 2 additions & 2 deletions examples/sync-implicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (!sensor.initialize(sensorType, gpioPin)) {
return;
}

var iid = setInterval(function() {
var iid = setInterval(function () {
if (++count >= repeats) {
clearInterval(iid);
}
Expand All @@ -39,6 +39,6 @@ var iid = setInterval(function() {
`humidity: ${readout.humidity.toFixed(1)}%, ` +
`valid: ${readout.isValid}, ` +
`errors: ${readout.errors}, ` +
`time: ${end - start}ms`
`time: ${end - start}ms`,
);
}, 2500);
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ var promises = {
return sensor.read(type, pin);
},
read(type, pin) {
return new Promise(function(resolve, reject) {
sensor.read(type, pin, function(err, temperature, humidity) {
return new Promise(function (resolve, reject) {
sensor.read(type, pin, function (err, temperature, humidity) {
if (err) {
reject(err);
} else {
resolve({ temperature, humidity });
}
});
});
}
},
};

module.exports = {
initialize: sensor.initialize,
read: sensor.read,
setMaxRetries: sensor.setMaxRetries,
promises
promises,
};
Loading
Loading