-
Notifications
You must be signed in to change notification settings - Fork 1
/
cowboy-widget.js
346 lines (295 loc) · 10.1 KB
/
cowboy-widget.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: pink; icon-glyph: bicycle;
// parsing widget parameters for username and password
let email
let password
const param = args.widgetParameter;
if (param != null && param.length > 0) {
if (param.indexOf(";") > 0) {
email = param.substring(0, param.indexOf(';')).trim();
password = param.substring(param.indexOf(';') + 1).trim();
} else {
console.log("Error reading user credentials.")
}
}
// widget colors
const labelColor = new Color('#fb3464') // widget label color
const textColor = new Color('#34443c') // widget text color
// battery circle graphics
const canvSize = 200;
const canvTextSize = 80;
const canvas = new DrawContext();
canvas.opaque = false;
const battCircleRemainColor = new Color("#fb3464"); // battery remaining color
const battCircleDepletedColor = new Color("#E3e3dc"); // battery depleted color
const battCircleBGColor = new Color('#fff'); // battery circle background color
const canvWidth = 16; // battery circle thickness
const canvRadius = 80; // battery circle radius
canvas.size = new Size(canvSize, canvSize);
canvas.respectScreenScale = true;
let widget = new ListWidget()
widget.url = "shortcuts://run-shortcut?name=unlock-cowboy"
let clientId
let uid
let accessToken
let bikeId
let success = false
// check existence of username and password
if (isNotEmpty(email) && isNotEmpty(password)) {
let cowboyCredentials = await getCowboyCredentials(false)
if (cowboyCredentials) {
success = true
clientId = cowboyCredentials.client
uid = cowboyCredentials.uid
accessToken = cowboyCredentials.accessToken
bikeId = cowboyCredentials.bikeId
widget = await createWidget()
}
}
if (!success) {
// show error
let cowboyIcon = await getImage("cowboy.png")
let cowboyIconImage = widget.addImage(cowboyIcon)
cowboyIconImage.imageSize = new Size(70, 14)
cowboyIconImage.centerAlignImage()
widget.addSpacer()
let warningTxt = widget.addText("Couldn't read username and password. Please set it as widget parameter, separated by semicolon!")
warningTxt.font = Font.semiboldSystemFont(12)
warningTxt.textColor = textColor
warningTxt.centerAlignText()
}
Script.setWidget(widget)
Script.complete()
widget.presentSmall()
async function createWidget() {
let cowboyIcon = await getImage("cowboy.png")
// widget.backgroundColor = new Color("#768178")
let bike = await getBikeInfo()
if (bike) {
const batteryLevel = bike.battery_state_of_charge;
widget.setPadding(10, 10, 8, 8)
let batteryStack = widget.addStack()
drawArc(
Math.floor(batteryLevel * 3.6),
battCircleRemainColor,
battCircleDepletedColor,
"🔋"
)
batteryStack.addImage(canvas.getImage())
batteryStack.addSpacer(6)
let infoStack = batteryStack.addStack()
infoStack.layoutVertically()
infoStack.addSpacer(12)
let cowboyIconImage = infoStack.addImage(cowboyIcon)
cowboyIconImage.imageSize = new Size(70, 14)
cowboyIconImage.centerAlignImage()
infoStack.addSpacer(4)
let percentageText = infoStack.addText(batteryLevel.toString() + "%")
percentageText.font = Font.mediumRoundedSystemFont(20)
if (batteryLevel < 21) {
console.log("battery level is low")
percentageText.textColor = new Color("#fb3464")
} else {
percentageText.textColor = new Color("#fb3464")
}
widget.addSpacer(3)
let detailsStack = widget.addStack()
detailsStack.layoutHorizontally()
let distanceStack = detailsStack.addStack()
distanceStack.layoutVertically()
let totalDistance = distanceStack.addText("DISTANCE")
totalDistance.font = Font.boldSystemFont(10)
totalDistance.textColor = labelColor
totalDistance.textOpacity = 0.8
let totalDistaneNo = distanceStack.addText(Math.round(bike.total_distance).toLocaleString() + ' KM')
totalDistaneNo.font = Font.boldSystemFont(11)
totalDistaneNo.textColor = textColor
detailsStack.addSpacer()
let co2Stack = detailsStack.addStack()
co2Stack.layoutVertically()
let totalCo2Saved = co2Stack.addText("CO₂ SAVED")
totalCo2Saved.font = Font.boldSystemFont(10)
totalCo2Saved.textColor = labelColor
totalCo2Saved.textOpacity = 0.8
let totalCo2SavedNo = co2Stack.addText((bike.total_co2_saved / 1000).toFixed(1).toLocaleString() + ' KG')
totalCo2SavedNo.font = Font.semiboldSystemFont(11)
totalCo2SavedNo.textColor = textColor
widget.addSpacer(4)
let locationText = widget.addText("LOCATION")
locationText.font = Font.boldSystemFont(10)
locationText.textColor = labelColor
locationText.textOpacity = 0.8
const geoPosition = bike.position.address.split(", ")
let geoPositionStreetTxt = widget.addText(geoPosition[0].replace("straße", "str."))
geoPositionStreetTxt.font = Font.semiboldSystemFont(12)
geoPositionStreetTxt.textColor = textColor
geoPositionStreetTxt.lineLimit = 1
geoPositionStreetTxt.minimumScaleFactor = 0.8
let geoPositionCityTxt = widget.addText(geoPosition[1])
geoPositionCityTxt.font = Font.semiboldSystemFont(12)
geoPositionCityTxt.textColor = textColor
geoPositionCityTxt.lineLimit = 1
} else {
let cowboyIconImage = widget.addImage(cowboyIcon)
cowboyIconImage.imageSize = new Size(70, 14)
cowboyIconImage.centerAlignImage()
widget.addSpacer()
let warningTxt = widget.addText("Error getting data, please double check your credentials.")
warningTxt.font = Font.semiboldSystemFont(12)
warningTxt.textColor = textColor
warningTxt.centerAlignText()
}
return widget
}
// get images from local filestore or download them once
async function getImage(image) {
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let path = fm.joinPath(dir, image)
if (fm.fileExists(path)) {
return fm.readImage(path)
} else {
// download once
let imageUrl
switch (image) {
case 'cowboy.png':
imageUrl = "https://i.imgur.com/6lFsyTZ.png"
break
default:
console.log(`Sorry, couldn't find ${image}.`);
}
let iconImage = await loadImage(imageUrl)
fm.writeImage(path, iconImage)
return iconImage
}
}
// helper function to download an image from a given url
async function loadImage(imgUrl) {
const req = new Request(imgUrl)
return await req.loadImage()
}
// query bike data from cowboy api
async function getBikeInfo() {
let bikeApiUrl = "https://app-api.cowboy.bike/bikes/" + bikeId
const appToken = atob("ZXlKaGJHY2lPaUpJVXpJMU5pSXNJblI1Y0NJNklrcFhWQ0o5")
req = new Request(bikeApiUrl)
req.headers = {
"X-Cowboy-App-Token": appToken,
"Content-Type": "application/json",
"Client": clientId,
"Uid": uid,
"Access-Token": accessToken
}
let result = await req.loadJSON()
// check if api call has been successful
if (req.response.statusCode == 200) {
console.log("Successfully retrieved bike data! Result: " + JSON.stringify(result))
return result
} else if (req.response.statusCode == 401) {
console.log("Unauthorized - access token seems to be expired. Trying to reauthorize.")
// reauthorize and store new access token
let refreshedCredentials = await getCowboyCredentials(true)
req = new Request(bikeApiUrl)
req.headers = {
"X-Cowboy-App-Token": appToken,
"Content-Type": "application/json",
"Client": refreshedCredentials.client,
"Uid": refreshedCredentials.uid,
"Access-Token": refreshedCredentials.accessToken
}
result = await req.loadJSON()
if (req.response.statusCode == 200) {
return result
}
}
return null
}
function sinDeg(deg) {
return Math.sin((deg * Math.PI) / 180);
}
function cosDeg(deg) {
return Math.cos((deg * Math.PI) / 180);
}
function drawArc(deg, fillColor, strokeColor, label) {
let ctr = new Point(canvSize / 2, canvSize / 2),
bgx = ctr.x - canvRadius;
bgy = ctr.y - canvRadius;
bgd = 2 * canvRadius;
bgr = new Rect(bgx, bgy, bgd, bgd);
canvas.opaque = false;
canvas.setFillColor(fillColor);
canvas.setStrokeColor(strokeColor);
canvas.setLineWidth(canvWidth);
canvas.strokeEllipse(bgr);
for (t = 0; t < deg; t++) {
rect_x = ctr.x + canvRadius * sinDeg(t) - canvWidth / 2;
rect_y = ctr.y - canvRadius * cosDeg(t) - canvWidth / 2;
rect_r = new Rect(rect_x, rect_y, canvWidth, canvWidth);
canvas.fillEllipse(rect_r);
}
// attempt to draw label/icon
const canvLabelRect = new Rect(
0,
(100 - canvTextSize / 2) - 8,
canvSize,
200
);
canvas.setTextAlignedCenter();
canvas.setFont(Font.boldSystemFont(canvTextSize));
canvas.drawTextInRect(label, canvLabelRect);
// return canvas.getImage()
}
// obtain cowboy api credentials - either cached or new
async function getCowboyCredentials(forceRefresh) {
// load credentials from local storage
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let path = fm.joinPath(dir, "cowboy-credentials.json")
let cowboyCredentials
if (fm.fileExists(path) && !forceRefresh) {
let cowboyCredentialsFile = Data.fromFile(path)
cowboyCredentials = JSON.parse(cowboyCredentialsFile.toRawString())
if (isNotEmpty(cowboyCredentials.accessToken)
&& isNotEmpty(cowboyCredentials.client)
&& isNotEmpty(cowboyCredentials.uid)) {
return cowboyCredentials
}
}
console.log("Cowboy credentials either don't exist or refresh was forced. Obtaining new credentials.")
cowboyCredentials = await authenticateUser()
return cowboyCredentials
}
// authenticate user and cache result locally
async function authenticateUser() {
let authUrl = "https://app-api.cowboy.bike/auth/sign_in"
req = new Request(authUrl)
req.headers = {
"Content-Type": "application/json"
}
req.method = "POST"
req.body = JSON.stringify({ email, password })
let result = await req.loadJSON()
if (req.response.statusCode = 200) {
// SUCCESS
console.log(req.response.headers)
const headers = req.response.headers
const cowboyCredentials = { "accessToken": headers["Access-Token"], "expiry": headers["Expiry"], "client": headers["Client"], "uid": headers["Uid"], "bikeId": result.data.bike.id }
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let path = fm.joinPath(dir, "cowboy-credentials.json")
fm.write(path, Data.fromString(JSON.stringify(cowboyCredentials)))
return cowboyCredentials
} else {
// FAILED
return null
}
}
// helper function to check not empty strings
function isNotEmpty(stringToCheck) {
if (stringToCheck != null && stringToCheck.length > 0) {
return true
} else {
return false
}
}