Compare commits
No commits in common. "8d30c10f18e2dbcf92523ae6b5c6e52ceaafc203" and "a242439c4745265d62c7f5b4e14aa754f36e2f55" have entirely different histories.
8d30c10f18
...
a242439c47
19
src/bms.js
19
src/bms.js
@ -1,19 +0,0 @@
|
|||||||
import i2c from 'i2c-bus';
|
|
||||||
|
|
||||||
async function bmsStatus(address = 0x57) {
|
|
||||||
const i2cBus = await i2c.openPromisified(1);
|
|
||||||
const data = await Promise.all([
|
|
||||||
i2cBus.readByte(address, 0x02),
|
|
||||||
i2cBus.readByte(address, 0x04),
|
|
||||||
i2cBus.readByte(address, 0x22),
|
|
||||||
i2cBus.readByte(address, 0x23),
|
|
||||||
i2cBus.readByte(address, 0x2a),
|
|
||||||
]);
|
|
||||||
return {
|
|
||||||
charging: !!((data[0] >> 7) & 1),
|
|
||||||
percentage: data[4] / 100,
|
|
||||||
temperature: data[1] - 40,
|
|
||||||
voltage: ((data[2] << 8) | data[3]) / 1000,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log(await bmsStatus());
|
|
13
src/http.js
13
src/http.js
@ -9,20 +9,17 @@ export default class Http extends Controller {
|
|||||||
constructor(apollo, port = 8000) {
|
constructor(apollo, port = 8000) {
|
||||||
super(apollo);
|
super(apollo);
|
||||||
this.express = express();
|
this.express = express();
|
||||||
|
|
||||||
this.express.get('/api/*', async (req, res) => {
|
|
||||||
const cmd = req.params['0'];
|
|
||||||
console.log(cmd);
|
|
||||||
res.json(await this.run(cmd));
|
|
||||||
});
|
|
||||||
|
|
||||||
this.express.get('*', (req, res) => {
|
this.express.get('*', (req, res) => {
|
||||||
let p = req.params['0'];
|
let p = req.params['0'];
|
||||||
if(!p || p == '/') p = 'index.html';
|
if(!p || p == '/') p = 'index.html';
|
||||||
const absolute = path.join(import.meta.url, '/../../ui/', p).replace('file:', '');
|
const absolute = path.join(import.meta.url, '/../../ui/', p).replace('file:', '');
|
||||||
res.sendFile(absolute);
|
res.sendFile(absolute);
|
||||||
});
|
});
|
||||||
|
this.express.get('/api/*', async (req, res) => {
|
||||||
|
const cmd = req.params['0'];
|
||||||
|
console.log(cmd);
|
||||||
|
res.json(await this.run(cmd));
|
||||||
|
});
|
||||||
this.express.listen(port);
|
this.express.listen(port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,17 +21,16 @@ export default class SensorSuite {
|
|||||||
set data(d) { this._data = d; }
|
set data(d) { this._data = d; }
|
||||||
get data() { return {timestamp: new Date().getTime(), ...this._data}; }
|
get data() { return {timestamp: new Date().getTime(), ...this._data}; }
|
||||||
|
|
||||||
_env_sensor = false;
|
get status() { return { sensors: 'ok' }; }
|
||||||
get status() { return { env_sensor: this._env_sensor ? 'ok' : 'failed' }; }
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.bme280 = new BME280({i2cBusNo: 1, i2cAddress: 0x76});
|
this.bme280 = new BME280({i2cBusNo: 1, i2cAddress: 0x76});
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
async start() {
|
||||||
return this.bme280.init().then(() => {
|
await this.bme280.init();
|
||||||
|
|
||||||
// Poll environmental data
|
// Poll environmental data
|
||||||
this._env_sensor = true;
|
|
||||||
this.stopBme280 = poll(async () => {
|
this.stopBme280 = poll(async () => {
|
||||||
const d = await this.bme280.readSensorData();
|
const d = await this.bme280.readSensorData();
|
||||||
this._data.humidity = d.humidity / 100;
|
this._data.humidity = d.humidity / 100;
|
||||||
@ -39,9 +38,6 @@ export default class SensorSuite {
|
|||||||
this._data.pressure = d.pressure_hPa;
|
this._data.pressure = d.pressure_hPa;
|
||||||
this._data.altitude = BME280.calculateAltitudeMeters(this.data.pressure);
|
this._data.altitude = BME280.calculateAltitudeMeters(this.data.pressure);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}).catch(err => {
|
|
||||||
this._env_sensor = false;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
|
Loading…
Reference in New Issue
Block a user