robotois-distance-sensor
Advanced tools
Comparing version 1.0.1 to 1.0.2
43
index.js
@@ -33,28 +33,24 @@ const DSensor = require('bindings')('DistanceSensor'); | ||
DistanceSensor.prototype.enableEvents = function enableEvents() { | ||
let dist; | ||
if (!this.eventInterval) { | ||
this.eventInterval = setInterval(() => { // Mediciones cada 100ms | ||
dist = this.getBasicValue(); | ||
// console.log('Distancia: ' + dist); | ||
this.emit('value', dist); | ||
}, 200); | ||
} | ||
DistanceSensor.prototype.publishNow = function publishNow() { | ||
this.mqttClient.publish(this.myTopic, this.getBasicValue().toString()); | ||
}; | ||
DistanceSensor.prototype.distToString = function distToString(dist) { | ||
return (` ${dist.toFixed(1).toString()}`).slice(-5); | ||
}; | ||
DistanceSensor.prototype.when = function when(value, callback) { | ||
let dist; | ||
if (!this.interval) { | ||
setInterval(() => { // Tomar mediciones cada 200ms | ||
dist = this.distance.getBasicValue(); | ||
/* eslint-disable no-console */ | ||
console.log(`Distancia: ${dist}`); | ||
if (dist === value) { | ||
callback(); | ||
DistanceSensor.prototype.enableEvents = function enableEvents(mqttConfig) { | ||
let prevValue = this.getBasicValue(); | ||
if (mqttConfig) { | ||
this.mqttClient = mqttConfig.mqttClient; | ||
this.myTopic = `sensors/distance${mqttConfig.instance}`; | ||
this.mqttClient.publish('registerTopic', this.myTopic); | ||
} | ||
if (!this.eventInterval) { | ||
this.eventInterval = setInterval(() => { | ||
const currentValue = this.getBasicValue(); | ||
if (currentValue !== prevValue) { | ||
this.emit('medicion', currentValue); | ||
if (this.mqttClient) { | ||
this.mqttClient.publish(this.myTopic, currentValue.toString()); | ||
} | ||
prevValue = currentValue; | ||
} | ||
}, 500); | ||
}, 250); | ||
} | ||
@@ -64,3 +60,2 @@ }; | ||
DistanceSensor.prototype.release = function release() { | ||
clearInterval(this.interval); | ||
clearInterval(this.eventInterval); | ||
@@ -67,0 +62,0 @@ this.distance.release(); |
{ | ||
"name": "robotois-distance-sensor", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Node library for reading the distance sensor", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,5 +6,5 @@ const DSensor = require('../index.js'); | ||
distance.on('value', (dist) => { | ||
distance.on('medicion', (dist) => { | ||
/* eslint-disable no-console */ | ||
console.log(`La distancia es: ${dist}`); | ||
console.log(`La distancia es: ${dist} cm`); | ||
}); | ||
@@ -11,0 +11,0 @@ |
@@ -7,3 +7,3 @@ const DSensor = require('../index.js'); | ||
/* eslint-disable no-console */ | ||
console.log('lol la distancia es 15cm!!'); | ||
console.log('Wow la distancia es 15cm!!'); | ||
}); | ||
@@ -10,0 +10,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62600
33
125
2