Updated to work with new battery schema
This commit is contained in:
		@@ -42,27 +42,55 @@ export class BatteryService {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    constructor(private firestore: AngularFirestore) {
 | 
					    constructor(private firestore: AngularFirestore) {
 | 
				
			||||||
        this.firestore.collection('Battery').doc('170614D').snapshotChanges().subscribe(snap => {
 | 
					        let afterDate = new Date();
 | 
				
			||||||
            this.lastUpdate = new Date().getTime();
 | 
					        afterDate.setDate(afterDate.getDate() - 1);
 | 
				
			||||||
            let data: any = snap.payload.data();
 | 
					
 | 
				
			||||||
            this.relayMode = data.config.relayMode ? data.config.relayMode.toString() : 'null';
 | 
					        this.firestore.collection('Battery').doc('170614D').collection('data', ref => ref.where('timestamp', '>=', afterDate).orderBy('timestamp')).valueChanges().subscribe(data => {
 | 
				
			||||||
            this.batteries = Object.keys(data.modules).map(key => {
 | 
					            this.batteries = data.reduce((acc, row) => {
 | 
				
			||||||
                let last = data.modules[key].length - 1;
 | 
					                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 {
 | 
					                return {
 | 
				
			||||||
                    charge: data.modules[key][last].charge,
 | 
					                    charge: last.charge,
 | 
				
			||||||
                    chargeHistory: data.modules[key].map(val => ({name: val.timestamp.toDate(), value: val.charge})),
 | 
					                    chargeHistory: module.map(row => ({name: row.timestamp, value: row.temp})),
 | 
				
			||||||
                    lastUpdate: data.modules[key][last].timestamp.toDate(),
 | 
					                    lastUpdate: last.timestamp,
 | 
				
			||||||
                    name: key,
 | 
					                    name: `Module ${i + 1}`,
 | 
				
			||||||
                    temp: data.modules[key][last].temp,
 | 
					                    temp: last.temp,
 | 
				
			||||||
                    tempHistory: data.modules[key].map(val => ({name: val.timestamp.toDate(), value: val.temp}))
 | 
					                    tempHistory: module.map(row => ({name: row.timestamp, value: row.temp}))
 | 
				
			||||||
                }
 | 
					                };
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            this.lastCharge.push(this.charge);
 | 
					            this.lastCharge.push(this.charge);
 | 
				
			||||||
            this.lastCharge.splice(0, this.lastCharge.length - 5);
 | 
					            this.lastCharge.splice(0, this.lastCharge.length - 3);
 | 
				
			||||||
            this.lastUpdate = this.batteries.reduce((acc, battery) => acc > battery.lastUpdate ? acc : battery.lastUpdate, 0);
 | 
					            this.lastUpdate = this.batteries[0].lastUpdate;
 | 
				
			||||||
            this.charge = this.batteries.reduce((acc, battery) => acc + battery.charge, 0) / 2;
 | 
					            this.charge = this.batteries.reduce((acc, module) => acc + module.charge, 0) / 2;
 | 
				
			||||||
            this.temp = this.batteries.reduce((acc, battery) => acc + battery.temp, 0) / 4;
 | 
					            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) {
 | 
					    setRelayMode(mode?: string) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user