From 5c4bb8b515621d00ef429ccf587c96cecb07568e Mon Sep 17 00:00:00 2001 From: ztimson Date: Fri, 27 Dec 2019 01:13:31 -0500 Subject: [PATCH] Updated site to use new BMS data --- src/app/battery/battery.component.html | 37 ++++++++--------- src/app/battery/battery.component.ts | 18 +++++++-- src/app/battery/battery.service.ts | 55 +++++++++++--------------- src/app/battery/battery.ts | 30 ++++++++++++++ 4 files changed, 85 insertions(+), 55 deletions(-) create mode 100644 src/app/battery/battery.ts diff --git a/src/app/battery/battery.component.html b/src/app/battery/battery.component.html index 9a70722..1e261aa 100644 --- a/src/app/battery/battery.component.html +++ b/src/app/battery/battery.component.html @@ -1,25 +1,20 @@ -
+
-
-
+
+

Powerwall: {{batteryService.charge | number : '1.1-1'}} V

Last Updated At: {{batteryService.lastUpdate | date: 'short'}}
+
Uptime: {{batteryService.uptime}}
-
-
-
lockCooling Fans
-
-
- - Auto - On - Off - -
+
+ + 24 Hours + 12 Hours + 6 Hours +
- +
{{battery.name}}
@@ -27,13 +22,13 @@
-
@@ -50,7 +45,7 @@ [roundDomains]="true" >
-
+
{ + console.log(data); + this.batteries = Object.keys(this.batteryService.modules).map(key => { + return { + name: `Module ${key}`, + chargeHistory: this.batteryService.modules[key].map(row => ({name: new Date(row.timestamp), value: row.voltage})), + tempHistory: this.batteryService.modules[key].map(row => ({name: new Date(row.timestamp), value: row.temperature})), + temperature: this.batteryService.modules[key][this.batteryService.modules[key].length - 1].temperature, + voltage: this.batteryService.modules[key][this.batteryService.modules[key].length - 1].voltage + } + }); + }); + } ngOnInit() { } diff --git a/src/app/battery/battery.service.ts b/src/app/battery/battery.service.ts index 528eb5c..ae9d281 100644 --- a/src/app/battery/battery.service.ts +++ b/src/app/battery/battery.service.ts @@ -1,18 +1,26 @@ import {Injectable} from '@angular/core'; import {AngularFirestore} from '@angular/fire/firestore'; +import {Battery} from './battery'; +import {BehaviorSubject} from 'rxjs'; @Injectable({ providedIn: 'root' }) export class BatteryService { - batteries = []; charge: number; - lastCharge: number[] = []; + data = new BehaviorSubject([]); lastUpdate = new Date().getTime(); - relayMode: string = 'null'; + modules = []; temp: number = 0; + uptime: string; - get charging() { return this.lastCharge.reduce((acc, v) => acc + v, 0) / this.lastCharge.length < this.charge; } + get charging() { + let value = this.data.value; + if(!value.length) return null; + let last = value[value.length - 1]; + let secondLast = value[value.length - 2]; + return last.soc > secondLast.soc; + } get icon() { if(new Date().getTime() - this.lastUpdate > 300000) return 'battery_alert'; @@ -24,36 +32,21 @@ export class BatteryService { let afterDate = new Date(); afterDate.setDate(afterDate.getDate() - 1); - this.firestore.collection('Battery').doc('170614D').collection('data', ref => ref.where('timestamp', '>=', afterDate).orderBy('timestamp')).valueChanges().subscribe(data => { - this.batteries = data.reduce((acc, row) => { - row.payload.forEach((data, i) => { - if(!acc[i]) acc[i] = []; - acc[i].push(Object.assign(data, {timestamp: row.timestamp.toDate()})); + this.firestore.collection('Battery').doc('170614D').collection('data', ref => ref.where('timestamp', '>=', afterDate.getTime()).orderBy('timestamp')).valueChanges().subscribe(data => { + this.modules = data.reduce((acc: any, row) => { + Object.keys(row.modules).forEach(module => { + if(!acc[module]) acc[module] = []; + acc[module].push(Object.assign(row.modules[module], {timestamp: row.timestamp})); }); return acc; - }, []).map((module, i) => { - const last = module[module.length - 1]; - return { - charge: last.charge, - chargeHistory: module.map(row => ({name: row.timestamp, value: row.charge})), - lastUpdate: last.timestamp, - name: `Module ${i + 1}`, - temp: last.temp, - tempHistory: module.map(row => ({name: row.timestamp, value: row.temp})) - }; - }); + }, {}); - this.lastCharge.push(this.charge); - this.lastCharge.splice(0, this.lastCharge.length - 3); - this.lastUpdate = this.batteries[0].lastUpdate; - this.charge = this.batteries.reduce((acc, module) => acc + module.charge, 0) / 2; - this.temp = this.batteries.reduce((acc, module) => acc + module.temp, 0) / this.batteries.length; + let last = data[data.length - 1]; + this.lastUpdate = last.timestamp; + this.charge = last.voltage; + this.temp = last.temperature; + this.uptime = last.uptime; + this.data.next(data); }); } - - setRelayMode(mode?: string) { - if(mode == 'null') this.firestore.collection('Battery').doc('170614D').update({config: {relayMode: null}}); - else if(mode == 'true') this.firestore.collection('Battery').doc('170614D').update({config: {relayMode: true}}); - else if(mode == 'false') this.firestore.collection('Battery').doc('170614D').update({config: {relayMode: false}}); - } } diff --git a/src/app/battery/battery.ts b/src/app/battery/battery.ts new file mode 100644 index 0000000..c0d8de5 --- /dev/null +++ b/src/app/battery/battery.ts @@ -0,0 +1,30 @@ +export interface Battery { + ampHours: number; + avgCellVoltage: number; + capacity: number; + current: number; + lifetimeCharge: number; + lifetimeDischarge: number; + maxCellVoltage: number; + maxDischargeCurrent: number; + maxVoltage: number; + minCellVoltage: number; + minVoltage: number; + modules: {[key: number]: { + cells: {[key: number]: number} + negativeTemperature: number; + positiveTemperature: number; + temperature: number; + voltage: number; + }}, + negativeContactor: boolean, + positiveContactor: boolean, + power: number; + soc: number; + temperature: number; + timestamp: number; + uptime: string; + version: number; + voltage: number; + wattHours: number; +}