diff --git a/background_tasks/linker.html b/background_tasks/linker.html index 219e4e69..d6fe24e9 100644 --- a/background_tasks/linker.html +++ b/background_tasks/linker.html @@ -58,6 +58,9 @@ case 'SENSORS_SCAN': ipcRenderer.send('SENSORS_SCAN', { data: parsedJSON.data }); break; + case 'SENSORS_READ': + ipcRenderer.send('SENSORS_READ', { data: parsedJSON.data }); + break; case 'START_MUL_MET': ipcRenderer.send('MUL_MET_DATA', { data: parsedJSON.data, prefix: parsedJSON.prefix }); break; diff --git a/public/electron.js b/public/electron.js index e22f42cb..8048c63c 100644 --- a/public/electron.js +++ b/public/electron.js @@ -245,3 +245,8 @@ getMetaData = path => { time: data[2], }; }; + +// TODO +ipcMain.handle('SENSORS_READ', (event, args) => { + mainWindow.webContents.send('SENSORS_READ', args); +}); diff --git a/scripts/bridge.py b/scripts/bridge.py index ea6169e8..76b57f4a 100755 --- a/scripts/bridge.py +++ b/scripts/bridge.py @@ -187,6 +187,9 @@ def main(): if command == 'SENSORS_SCAN': sensors.scan() + if command == 'SENSORS_READ': + sensors.read() + # -------------------------------- Write block ----------------------------------- if command == 'START_WRITE': diff --git a/scripts/sensors.py b/scripts/sensors.py index 0caf9d4b..4ac61a71 100644 --- a/scripts/sensors.py +++ b/scripts/sensors.py @@ -3,7 +3,8 @@ import sys import threading import time - +from pslab.external import SHT21 +from pslab.bus import i2c class Sensors: def __init__(self, I, file_write): @@ -39,6 +40,23 @@ def capture_loop(self): if self.active_category == 'SCAN': self.scan() + def read(self): + datetime_data = datetime.datetime.now() + timestamp = time.time() + + # FIXME! + sensor = SHT21(self.device.i2c) + data = sensor.getRaw() + + self.file_write.update_buffer( + "SENSOR_DATA", timestamp=timestamp, datetime=datetime_data, data='sensor_data', value=data) + time.sleep(0.25) + + output = {'type': 'SENSORS_READ', 'data': data} + print(json.dumps(output)) + sys.stdout.flush() + + def scan(self): datetime_data = datetime.datetime.now() timestamp = time.time() diff --git a/src/screen/Sensors/Sensors.js b/src/screen/Sensors/Sensors.js index 33ca1331..7873cf3e 100644 --- a/src/screen/Sensors/Sensors.js +++ b/src/screen/Sensors/Sensors.js @@ -40,19 +40,30 @@ const knownSensors = [ id: 0x68, name: 'MPU6050', }, + { + id: 0x29, + name: 'VL53LXX', // TODO: implement in Python lib + description: 'Time-of-flight', + }, { id: 0x40, name: 'SHT21', + description: 'Temperature and humidity', }, { id: 0x49, name: 'TSL2561', }, + { + id: 0x22, + name: 'GY-906', + }, ]; const filterKnownSensors = (detectedSensors = []) => { // PSL always lists 0 and 96, regardless of whether a sensor is present const filtered = detectedSensors.filter(s => s !== 96 && s !== 0); + console.info({ detected: filtered }); return knownSensors.filter(k => filtered.includes(k.id)); }; @@ -61,7 +72,9 @@ const SensorList = ({ sensors = [], onReadSensor = () => {} }) => ( {sensors.map((item, index) => { return ( onReadSensor(item)}> - {item.name} + + {item.name} {item.description} + ); })} @@ -77,6 +90,7 @@ class Sensors extends Component { isScanned: false, sensorList: [], data: null, + sensorData: null, }; } @@ -96,11 +110,18 @@ class Sensors extends Component { sensorList: filterKnownSensors(args.data), }); }); + ipcRenderer.on('SENSORS_READ', (event, args) => { + this.setState({ + isScanned: true, + sensorData: args.data, + }); + }); // this.getConfigFromDevice(); } componentWillUnmount() { ipcRenderer.removeAllListeners('SENSORS_SCAN'); + ipcRenderer.removeAllListeners('SENSORS_READ'); } getConfigFromDevice = debounce(() => { @@ -118,8 +139,20 @@ class Sensors extends Component { command: 'SENSORS_SCAN', }); }, 500); + + onReadSensor = debounce(sensor => { + // TODO: use sensor and implement switch over it + // TODO: can we find a generic interface? + console.info({ sensor }); + const { isConnected } = this.props; + isConnected && + loadBalancer.sendData(ipcRenderer, 'linker', { + command: 'SENSORS_READ', + }); + }, 500); + render() { - const { data, isScanned, sensorList } = this.state; + const { data, isScanned, sensorList, sensorData } = this.state; return ( @@ -140,12 +173,13 @@ class Sensors extends Component { Detected sensors console.info({ sensor })} + onReadSensor={this.onReadSensor} /> ) : ( No sensors detected ))} + Sensor data:
{JSON.stringify(sensorData)}
Known sensors
{JSON.stringify(data)}
diff --git a/src/screen/Sensors/styles.js b/src/screen/Sensors/styles.js index 3a61b4db..cd0d0d0f 100644 --- a/src/screen/Sensors/styles.js +++ b/src/screen/Sensors/styles.js @@ -5,6 +5,7 @@ export const Container = styled.div` display: flex; flex-direction: column; align-items: center; + background-color: ${props => props.theme.common.white}; `; export const Wrapper = styled.div`