Skip to content

Commit

Permalink
Resolve issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
menixator committed Oct 24, 2017
1 parent 011e50f commit 2a394dc
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 167 deletions.
141 changes: 0 additions & 141 deletions lib/interface.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const request = require("request");
const formatter = require("./formatter");
const log = formatter("sunshine:interface");
const normalize = require("./normalize");
const requests = require("./requests");
const moment = require("moment");
const round = require("./round");
Expand All @@ -17,46 +16,6 @@ class APIInterface {
this.password = password;
this.token = null;
}


async getPlantPowerReading(oid, date) {
date = date || new Date();
let dayOverview = await this.dayOverview(oid, date);

if (dayOverview.readings.length === 0)
return {
latest: normalize.power({ value: 0, unit: dayOverview.unit }),
timestamp: -1,
discontinuity: 0
};

let latest = dayOverview.latest;

return {
latest: normalize.power({ value: latest.mean, unit: dayOverview.unit }),
timestamp: latest.timestamp,
discontinuity: dayOverview.discontinuity
};
}

async getPlantEnergy(oid) {
let lastDataExact = await this.lastDataExact(oid);
let unit = lastDataExact.unit;
return {
total: normalize.energy({
value: (lastDataExact.day && lastDataExact.day.absolute) || 0,
unit
}),
day: normalize.energy({
value: (lastDataExact.day && lastDataExact.day.difference) || 0,
unit
}),
hour: normalize.energy({
value: (lastDataExact.hour && lastDataExact.hour.difference) || 0,
unit
})
};
}

async lastDataExact(oid, date) {
return await new requests.LastDataExact(this.token, oid, date).perform();
Expand Down Expand Up @@ -117,106 +76,6 @@ class APIInterface {
let res = await new requests.Logout(this.token).perform();
return res;
}

async getPlantInfo(oid) {
let energy = await this.getPlantEnergy(oid);
let power = await this.getPlantPowerReading(oid);
let co2Avoided = {
total: normalize.co2Avoided({
value: CARBON_GRAM_AVOIDED_PER_KWH * energy.total.value,
unit: "g"
}),
today: normalize.co2Avoided({
value: CARBON_GRAM_AVOIDED_PER_KWH * energy.day.value,
unit: "g"
})
};

let revenue = {
total: normalize.revenue({
value: USD_PER_KWH * energy.total.value,
unit: "$"
}),
today: normalize.revenue({
value: USD_PER_KWH * energy.day.value,
unit: "$"
})
};
return { timestamp: Date.now(), energy, power, co2Avoided, revenue };
}

async aggregate() {
let now = Date.now();

let lastInterval = moment(now - now % (15 * 60 * 1000)).valueOf();

let aggregated = {
power: { value: 0, unit: "kW" },
energy: {
total: {
value: 0,
unit: "kWh"
},
today: {
value: 0,
unit: "kWh"
}
}
};
let readings = [];

let plants = this.getPlants();
for (let plant of await plants) {
let reading = await this.getPlantInfo(plant.oid);
let { energy, power } = reading;

aggregated.power.value += power.latest.value;
aggregated.energy.total.value += energy.total.value;
this.log`${plant.name} power.now = ${power.latest
.value} kW (${power.discontinuity > 0 ? -power.discontinuity : "@@"})`;
this.log`${plant.name} energy.today = ${energy.day.value} kWh`;
this.log`${plant.name} energy.total = ${energy.total.value} kWh`;
aggregated.energy.today.value += energy.day.value;

reading.oid = plant.oid;
reading.name = plant.name;

readings.push(reading);
}

aggregated.co2Avoided = {
total: normalize.co2Avoided({
value: CARBON_GRAM_AVOIDED_PER_KWH * aggregated.energy.total.value,
unit: "g"
}),
today: normalize.co2Avoided({
value: CARBON_GRAM_AVOIDED_PER_KWH * aggregated.energy.today.value,
unit: "g"
})
};

aggregated.revenue = {
total: normalize.revenue({
value: USD_PER_KWH * aggregated.energy.total.value,
unit: "$"
}),
today: normalize.revenue({
value: USD_PER_KWH * aggregated.energy.today.value,
unit: "$"
})
};

aggregated.power = normalize.power(aggregated.power);
aggregated.energy.total = normalize.energy(aggregated.energy.total);
aggregated.energy.today = normalize.energy(aggregated.energy.today);

return {
timestamp: Date.now(),
lastPowerInterval: lastInterval,
aggregated: aggregated,
plants: readings
};
}
}

APIInterface.CARBON_GRAM_AVOIDED_PER_KWH = CARBON_GRAM_AVOIDED_PER_KWH;
Expand Down
Loading

0 comments on commit 2a394dc

Please sign in to comment.