Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read sensors #623

Draft
wants to merge 4 commits into
base: development
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions background_tasks/linker.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,8 @@ getMetaData = path => {
time: data[2],
};
};

// TODO
ipcMain.handle('SENSORS_READ', (event, args) => {
mainWindow.webContents.send('SENSORS_READ', args);
});
3 changes: 3 additions & 0 deletions scripts/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def main():
if command == 'SENSORS_SCAN':
sensors.scan()

if command == 'SENSORS_READ':
sensors.read()

# -------------------------------- Write block -----------------------------------

if command == 'START_WRITE':
Expand Down
20 changes: 19 additions & 1 deletion scripts/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
40 changes: 37 additions & 3 deletions src/screen/Sensors/Sensors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};

Expand All @@ -61,7 +72,9 @@ const SensorList = ({ sensors = [], onReadSensor = () => {} }) => (
{sensors.map((item, index) => {
return (
<SensorTab key={index} onClick={() => onReadSensor(item)}>
<SensorTitle>{item.name}</SensorTitle>
<SensorTitle>
{item.name} {item.description}
</SensorTitle>
</SensorTab>
);
})}
Expand All @@ -77,6 +90,7 @@ class Sensors extends Component {
isScanned: false,
sensorList: [],
data: null,
sensorData: null,
};
}

Expand All @@ -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(() => {
Expand All @@ -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 (
<Container>
Expand All @@ -140,12 +173,13 @@ class Sensors extends Component {
<TitleWrapper>Detected sensors</TitleWrapper>
<SensorList
sensors={sensorList}
onReadSensor={sensor => console.info({ sensor })}
onReadSensor={this.onReadSensor}
/>
</>
) : (
<TitleWrapper>No sensors detected</TitleWrapper>
))}
Sensor data: <pre>{JSON.stringify(sensorData)}</pre>
<TitleWrapper>Known sensors</TitleWrapper>
<SensorList sensors={knownSensors} />
<pre>{JSON.stringify(data)}</pre>
Expand Down
1 change: 1 addition & 0 deletions src/screen/Sensors/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down