-
Notifications
You must be signed in to change notification settings - Fork 13
/
Car.js
316 lines (275 loc) · 8.68 KB
/
Car.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: car;
// CONFIGURATION
const baseURL = ""
const apiKey = ""
const rangeInMiles = false //set to true to show range in miles
const showFinishTime = true //set to false to hide charge finish time
const forceImageRefresh = false //set to true to refresh the image
const exampleData = false
const timetravel = null; //set to a unix timestamp to emulate the script being run at that time (seconds!)
const socThreshold = 95 //not implemented
// WIDGET VERSION: v0.0.7-InDev
// Created by robske_110 24.01.2020
// This script is originally inspired from https://gist.github.com/mountbatt/772e4512089802a2aa2622058dd1ded7
let scriptRun = new Date()
if(timetravel !== null){
scriptRun = new Date(timetravel*1000);
}
// Translations
const translations = {
en: {
chargeStatus: {
disconnected: "Disconnected",
holdingCharge: "holding charge",
connected: "connected",
charging: "charging…"
},
soc: "SOC",
range: "Range",
targetSOC: "Target SOC",
hvac: "HVAC",
hvacStatus: {
heating: "heating",
cooling: "cooling",
ventilation: "ventilating",
off: "off"
}
},
de: {
chargeStatus: {
disconnected: "Entkoppelt",
holdingCharge: "Ladezustand halten",
connected: "Verbunden",
charging: "Lädt…"
},
soc: "Ladezustand",
range: "Reichweite",
targetSOC: "Zielladung",
hvac: "Klimaanlage",
hvacStatus: {
heating: "Heizen",
cooling: "Kühlen",
ventilation: "Lüften",
off: "Aus"
}
}
}
function getTranslatedText(key){
let lang = Device.language();
let translation = translations[lang];
if(translation == undefined){
translation = translations.en;
}
let nested = key.split(".");
key.split(".").forEach(function(element){
translation = translation[element];
});
return translation;
}
let widget = await createWidget()
// present the widget in app
if (!config.runsInWidget) {
await widget.presentMedium()
}
Script.setWidget(widget)
Script.complete()
// adds a vertical stack to widgetStack
function verticalStack(widgetStack){
let stack = widgetStack.addStack()
stack.layoutVertically()
return stack
}
// adds a value - title pair
function addFormattedData(widgetStack, dataTitle, dataValue){
let stack = widgetStack.addStack()
stack.layoutVertically()
const label = stack.addText(dataTitle)
label.font = Font.mediumSystemFont(12)
const value = stack.addText(dataValue)
value.font = Font.boldSystemFont(16)
}
// build the widget
async function createWidget() {
let widget = new ListWidget()
const data = await getData()
widget.setPadding(20, 15, 20, 15) //top, leading, bottom, trailing
widget.backgroundColor = Color.dynamic(new Color("eee"), new Color("111"))
const wrap = widget.addStack()
//wrap.centerAlignContent()
wrap.spacing = 15
const carColumn = verticalStack(wrap)
carColumn.addSpacer(5)
const carImage = await getImage(
baseURL.substr(baseURL.indexOf("://")+3).replaceAll("/", "_")+"-car.png",
baseURL+"/carPicture.php?key="+apiKey)
carColumn.addImage(carImage)
//carColumn.addSpacer(5)
let chargeStatus
switch (data.plugConnectionState){
case "disconnected":
chargeStatus = "⚫ "+getTranslatedText("chargeStatus.disconnected")
break;
case "connected":
//widget.refreshAfterDate = new Date(Date.now() + 300) //increase refresh rate?
switch (data.chargeState){
case "chargePurposeReachedAndNotConservationCharging":
case "notReadyForCharging":
case "readyForCharging":
chargeStatus = "🟠 "+getTranslatedText("chargeStatus.connected")
break;
case "chargePurposeReachedAndConservation":
chargeStatus = "🟢 "+getTranslatedText("chargeStatus.holdingCharge")
break;
case "charging":
chargeStatus = "⚡ "+getTranslatedText("chargeStatus.charging")
break;
default:
chargeStatus = "unknown cS: "+data.chargeState
}
let plugLockStatus;
switch (data.plugLockState){
case "locked":
plugLockStatus = " (🔒)"
break;
case "unlocked":
plugLockStatus = " (🔓)"
break;
case "invalid":
plugLockStatus = " (❌)"
break;
default:
plugLockStatus = "unknown pLS: "+data.plugConnectionState
break;
}
chargeStatus = chargeStatus + plugLockStatus;
break;
default:
chargeStatus = "unknown pCS: "+data.plugConnectionState+" cS: "+data.chargeState
}
//const chargeInfo = verticalStack(carColumn)
//chargeInfo.setPadding(0,10,0,10)
const chargeInfo = carColumn
let dF = new DateFormatter()
dF.useNoDateStyle()
dF.useShortTimeStyle()
chargeStatus = chargeInfo.addText(chargeStatus)
chargeStatus.font = Font.regularSystemFont(10)
chargeInfo.addSpacer(5)
let dataTimestamp = null;
if(!Number.isNaN(Date.parse(data.time))){
dataTimestamp = new Date(Date.parse(data.time));
}
if(data.chargeState === "charging" || data.chargeState === "chargePurposeReachedAndConservation"){
let realRemainChgTime = data.remainingChargingTime;
let finishTime = ""
if(dataTimestamp != null){
realRemainChgTime -= (scriptRun.getTime() - dataTimestamp.getTime()) / 60000;
realRemainChgTime = Math.max(0, realRemainChgTime);
finishTime = " ("+dF.string(new Date(dataTimestamp.getTime() + realRemainChgTime * 60000))+")";
}
let timeStr = Math.floor(realRemainChgTime / 60) + ":" + String(Math.round(realRemainChgTime % 60)).padStart(2, '0') + "h"
let chargeStateLabel = chargeInfo.addText(data.chargePower + " kW | " + timeStr + (showFinishTime ? finishTime : ""))
chargeStateLabel.font = Font.regularSystemFont(10)
}else{
chargeInfo.addSpacer(10)
}
const dataCol1 = verticalStack(wrap)
addFormattedData(dataCol1, getTranslatedText("soc"), data.batterySOC.toString()+"%")
dataCol1.addSpacer(10)
let range
if(!rangeInMiles){
range = data.remainingRange+"km";
}else{
range = Math.round(data.remainingRange/1.609344)+"mi";
}
addFormattedData(dataCol1, getTranslatedText("range"), range)
const dataCol2 = verticalStack(wrap)
addFormattedData(dataCol2, getTranslatedText("targetSOC"), data.targetSOC+"%")
dataCol2.addSpacer(10)
let hvacStatus;
switch (data.hvacState){
case "heating":
hvacStatus = getTranslatedText("hvacStatus.heating");
break;
case "cooling:":
hvacStatus = getTranslatedText("hvacStatus.cooling");
break;
case "ventilation":
hvacStatus = getTranslatedText("hvacStatus.ventilation");
break;
case "off":
hvacStatus = getTranslatedText("hvacStatus.off");
break;
default:
hvacStatus = "unknown hS: "+date.hvacState;
}
addFormattedData(dataCol2, getTranslatedText("hvac"), hvacStatus+" ("+data.hvacTargetTemp+"°C)")
timedebug = widget.addText("carUpdate "+(dataTimestamp == null ? data.time : dF.string(dataTimestamp))+" (widget "+dF.string(scriptRun)+")")
timedebug.font = Font.lightSystemFont(8)
timedebug.textColor = Color.dynamic(Color.lightGray(), Color.darkGray())
timedebug.rightAlignText()
return widget;
}
// fetch data
async function getData() {
let state
if(exampleData || baseURL == ""){
state = {};
state["batterySOC"] = "40"
state["remainingRange"] = "150"
state["remainingChargingTime"] = "61"
state["chargeState"] = "charging"
state["chargePower"] = "100"
state["targetSOC"] = "100"
state["plugConnectionState"] = "connected"
state["plugLockState"] = "locked"
state["hvacState"] = "heating"
state["hvacTargetTemp"] = "21.5"
state["time"] = "simulated"
}else{
state = getJSON()
}
/*let currentDate = ;
let newDate = new Date((new Date).getTime()+1000);
chargeReached = new Notification()
chargeReached.identifier = "SoCReached"
chargeReached.title = "ID.3 🔋 Geladen"
chargeReached.body = "Die Batterie ist zu " + socThreshold + "% geladen!"
chargeReached.sound = "complete"
chargeReached.setTriggerDate(newDate)
chargeReached.schedule()*/
return state
}
async function getJSON(){
url = baseURL+"/carStatus.php?key="+apiKey
if(timetravel !== null){
url += "&at=@"+timetravel
}
req = new Request(url)
req.method = "GET"
apiResult = await req.loadString()
console.log(apiResult)
return JSON.parse(apiResult)
}
// get images from local filestore or download them once
// credits: https://gist.github.com/marco79cgn (for example https://gist.github.com/marco79cgn/c3410c8ecc8cb0e9f87409cee7b87338#file-ffp2-masks-availability-js-L234)
async function getImage(imageName, imgUrl){
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let path = fm.joinPath(dir, imageName)
if(fm.fileExists(path) && !forceImageRefresh){
return fm.readImage(path)
}else{
// download once
let iconImage = await loadImage(imgUrl)
fm.writeImage(path, iconImage)
return iconImage
}
}
async function loadImage(imgUrl){
console.log("fetching_pic");
const req = new Request(imgUrl)
return await req.loadImage()
}