Updated to work with new battery schema

This commit is contained in:
Zakary Timson 2019-08-03 13:18:23 -04:00
parent c52d38f49b
commit f9f3f7029f

View File

@ -42,27 +42,55 @@ export class BatteryService {
}
constructor(private firestore: AngularFirestore) {
this.firestore.collection('Battery').doc('170614D').snapshotChanges().subscribe(snap => {
this.lastUpdate = new Date().getTime();
let data: any = snap.payload.data();
this.relayMode = data.config.relayMode ? data.config.relayMode.toString() : 'null';
this.batteries = Object.keys(data.modules).map(key => {
let last = data.modules[key].length - 1;
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()}));
});
return acc;
}, []).map((module, i) => {
const last = module[module.length - 1];
return {
charge: data.modules[key][last].charge,
chargeHistory: data.modules[key].map(val => ({name: val.timestamp.toDate(), value: val.charge})),
lastUpdate: data.modules[key][last].timestamp.toDate(),
name: key,
temp: data.modules[key][last].temp,
tempHistory: data.modules[key].map(val => ({name: val.timestamp.toDate(), value: val.temp}))
}
charge: last.charge,
chargeHistory: module.map(row => ({name: row.timestamp, value: row.temp})),
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 - 5);
this.lastUpdate = this.batteries.reduce((acc, battery) => acc > battery.lastUpdate ? acc : battery.lastUpdate, 0);
this.charge = this.batteries.reduce((acc, battery) => acc + battery.charge, 0) / 2;
this.temp = this.batteries.reduce((acc, battery) => acc + battery.temp, 0) / 4;
});
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;
})
// .snapshotChanges().subscribe(snap => {
// this.lastUpdate = new Date().getTime();
// let data: any = snap.payload.data();
// this.relayMode = data.config.relayMode ? data.config.relayMode.toString() : 'null';
// this.batteries = Object.keys(data.modules).map(key => {
// let last = data.modules[key].length - 1;
// return {
// charge: data.modules[key][last].charge,
// chargeHistory: data.modules[key].map(val => ({name: val.timestamp.toDate(), value: val.charge})),
// lastUpdate: data.modules[key][last].timestamp.toDate(),
// name: key,
// temp: data.modules[key][last].temp,
// tempHistory: data.modules[key].map(val => ({name: val.timestamp.toDate(), value: val.temp}))
// }
// });
// this.lastCharge.push(this.charge);
// this.lastCharge.splice(0, this.lastCharge.length - 5);
// this.lastUpdate = this.batteries.reduce((acc, battery) => acc > battery.lastUpdate ? acc : battery.lastUpdate, 0);
// this.charge = this.batteries.reduce((acc, battery) => acc + battery.charge, 0) / 2;
// this.temp = this.batteries.reduce((acc, battery) => acc + battery.temp, 0) / 4;
// });
}
setRelayMode(mode?: string) {