forked from jia-seed/step-ergize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorReader.js
More file actions
71 lines (58 loc) · 1.57 KB
/
Copy pathSensorReader.js
File metadata and controls
71 lines (58 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* import React, { Component } from 'react';
import SerialPort from 'react-native-serialport';
class SensorReader extends Component {
constructor(props) {
super(props);
this.state = {
sensorValue: 'Waiting for data...',
};
}
componentDidMount() {
SerialPort.startListener('/dev/tty0', { baudRate: 9600 }, (data) => {
const parsedValue = parseFloat(data);
if (!isNaN(parsedValue)) {
this.setState({ sensorValue: parsedValue });
}
});
}
componentWillUnmount() {
SerialPort.stopListener();
}
getDistance() {
return this.state.sensorValue;
}
render() {
null;
}
}
export default SensorReader;
*/
import React, { Component } from 'react';
class SensorReader extends Component {
constructor(props) {
super(props);
this.state = {
sensorValue: this.getRandomInt(20, 100), // Initialize with a random value
};
}
componentDidMount() {
// Simulate data updates at regular intervals (e.g., every 3 seconds)
this.dataUpdateInterval = setInterval(() => {
this.setState({ sensorValue: this.getRandomInt(20, 100) });
}, 1000); // Change the interval as needed
}
componentWillUnmount() {
clearInterval(this.dataUpdateInterval); // Clear the interval when unmounting the component
}
// Generate a random integer value between min and max (inclusive)
getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
getDistance() {
return this.getRandomInt(20, 100);
}
render() {
return null;
}
}
export default SensorReader;