-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataModel.js
152 lines (112 loc) · 4.85 KB
/
DataModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
module.exports = class DataModel {
static kwh = "kwh"
static kwp = "kwp"
static kw = "kw"
static percent = "%"
#currentYield = 0.0
#currentYieldUnit = DataModel.kw
#currentBatteryCharge = 0.0
#currentbatteryChargeUnit = DataModel.percent
// Negative means we're discharging from the battery
#currentBatteryUsage = 0.0
#currentBatteryUsageUnit = DataModel.kw
// Negative means we're importing from the grid
#currentGridUsage = 0.0
#currentGridUsageUnit = DataModel.kw
#currentHouseConsumption = 0.0
#currentHouseConsumptionUnit = DataModel.kw
#todayYield = 0.0
#todayYieldUnit = DataModel.kwh
#todaysCharging = 0.0
#todaysChargingUnit = DataModel.kwh
#todaysDischarging = 0.0
#todaysDischargingUnit = DataModel.kwh
#todayGridImport = 0.0
#todayGridImportUnit = DataModel.kwh
#todayGridExport = 0.0
#todayGridExportUnit = DataModel.kwh
#todayHouseConsumption = 0.0
#todayHouseConsumptionUnit = DataModel.kwh
#stationCapacity = 0.0
#stationCapacityUnit = DataModel.kwh
#scrapeStartDurationMs = 0
#scrapeEndTimeMs = 0
// Construct from the extracted strings from Solis Cloud
constructor(rawData) {
const currentYieldData = this.separateValueFromUnit(rawData.get("currentGen"))
this.currentYield = currentYieldData[0]
this.currentYieldUnit = currentYieldData[1]
const currentBatteryChargeData = this.separateValueFromUnit(rawData.get("batteryCharge"))
this.currentBatteryCharge = currentBatteryChargeData[0]
this.currentBatteryChargeUnit = currentBatteryChargeData[1]
const currentBatteryUsageData = this.separateValueFromUnit(rawData.get("drawFromBattery"))
this.currentBatteryUsage = currentBatteryUsageData[0]
this.currentBatteryUsageUnit = currentBatteryUsageData[1]
const currentGridUsageData = this.separateValueFromUnit(rawData.get("currentGridInOut"))
this.currentGridUsage = currentGridUsageData[0]
this.currentGridUsageUnit = currentGridUsageData[1]
const currentHouseConsumptionData = this.separateValueFromUnit(rawData.get("currentHouseDraw"))
this.currentHouseConsumption = currentHouseConsumptionData[0]
this.currentHouseConsumptionUnit = currentHouseConsumptionData[1]
const todayYieldData = this.separateValueFromUnit(rawData.get("totalYield"))
this.todayYield = todayYieldData[0]
this.todayYieldUnit = todayYieldData[1]
const todaysChargingData = this.separateValueFromUnit(rawData.get("todaysCharging"))
this.todaysCharging = todaysChargingData[0]
this.todaysChargingUnit = todaysChargingData[1]
const todaysDischargingData = this.separateValueFromUnit(rawData.get("todaysDischarging"))
this.todaysDischarging = todaysDischargingData[0]
this.todaysDischargingUnit = todaysDischargingData[1]
const todayGridImportData = this.separateValueFromUnit(rawData.get("todayFromGrid"))
this.todayGridImport = todayGridImportData[0]
this.todayGridImportUnit = todayGridImportData[1]
const todayGridExportData = this.separateValueFromUnit(rawData.get("todayToGrid"))
this.todayGridExport = todayGridExportData[0]
this.todayGridExportUnit = todayGridExportData[1]
const stationCapacityData = this.separateValueFromUnit(rawData.get("stationCapacity"))
this.stationCapacity = stationCapacityData[0]
this.stationCapacityUnit = stationCapacityData[1]
this.scrapeStartDurationMs = rawData.get("scrapeStartDurationMs")
this.scrapeEndTimeMs = rawData.get("scrapeEndTimeMs")
// If the house is using more than the solar is generating
if (this.currentHouseConsumption > this.currentYield) {
// Whatever the battery usage it, it must be coming out of the battery so make it negative
this.currentBatteryUsage = Math.abs(this.currentBatteryUsage) * -1
}
// If the house is using more than the solar is generating and there is power in the battery
if (this.currentHouseConsumption > this.currentYield + Math.abs(this.currentBatteryUsage)){
// Whatever the grid usage it, its probably importing from the grid, so make it negative
this.currentGridUsage = Math.abs(this.currentGridUsage) * -1
}
}
toJson() {
return JSON.stringify(this)
}
separateValueFromUnit(data) {
var value = 0.0
var unit = ""
if (data) {
const lowerData = data.toLowerCase()
// Remove all know units and spaces
var cleanLowerData = lowerData.replace(DataModel.kwh, "");
cleanLowerData = cleanLowerData.replace(DataModel.kwp, "");
cleanLowerData = cleanLowerData.replace(DataModel.kw, "");
cleanLowerData = cleanLowerData.replace(DataModel.percent, "");
cleanLowerData = cleanLowerData.replace(" ", "");
// Check its a number
if (this.isNumeric(cleanLowerData)) {
value = parseFloat(cleanLowerData)
// Remove the value from the original string to get the unit
unit = data.replace(cleanLowerData, "")
unit = unit.replace(" ", "")
}
}
return [value, unit]
}
isNumeric(str) {
if (typeof str != "string") {
return false
}
return !isNaN(str) && !isNaN(parseFloat(str))
}
}