-
Notifications
You must be signed in to change notification settings - Fork 0
/
bg96_gps.device.lib.nut
665 lines (597 loc) · 26 KB
/
bg96_gps.device.lib.nut
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
* BG96_GPS library
* Copyright 2023 KORE Wireless
*
* MIT License
* SPDX-License-Identifier: MIT
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Enums
*/
enum BG96_AT_ERROR_CODE {
FILE_INVALID_INPUT = "400",
FILE_SIZE_MISMATCH = "401",
FILE_READ_ZERO_BYTE = "402",
FILE_DRIVE_FULL = "403",
FILE_NOT_FOUND = "405",
FILE_INVALID_NAME = "406",
FILE_ALREADY_EXISTS = "407",
FILE_WRITE_FAIL = "409",
FILE_OPEN_FAIL = "410",
FILE_READ_FAIL = "411",
FILE_MAX_OPEN_FILES = "413",
FILE_READ_ONLY = "414",
FILE_INVALID_DESCRIPTOR = "416",
FILE_LIST_FAIL = "417",
FILE_DELETE_FAIL = "418",
FILE_GET_DISK_INFO_FAIL = "419",
FILE_NO_SPACE = "420",
FILE_TIMEOUT = "421",
FILE_TOO_LARGE = "423",
FILE_INVALID_PARAM = "425",
FILE_ALREADY_OPEN = "426",
GPS_NOT_READY = "500",
GPS_INVALID_PARAM = "501",
GPS_OPERATION_NOT_SUPPORTED = "502",
GPS_GNSS_SUBSYSTEM_BUSY = "503",
GPS_SESSION_IS_ONGOING = "504",
GPS_SESSION_NOT_ACTIVE = "505",
GPS_OPERATION_TIMEOUT = "506",
GPS_FUNCTION_NOT_ENABLED = "507",
GPS_TIME_INFO_ERROR = "508",
GPS_XTRA_NOT_ENABLED = "509",
GPS_VALIDITY_TIME_OUT_OF_RANGE = "512",
GPS_INTERNAL_RESOURCE_ERROR = "513",
GPS_GNSS_LOCKED = "514",
GPS_END_BY_E911 = "515",
GPS_NO_FIX_NOW = "516",
GPS_GEO_FENCE_ID_DOES_NOT_EXIST = "517",
GPS_UNKNOWN_ERROR = "549"
}
enum BG96_IMPOS_ERROR_CODE {
TIMEOUT = -1,
MODEM_NOT_READY = -2,
CMD_IN_FLIGHT = -3,
GPS_INVALID_PARAM = -4,
TX_FAILURE = -5
}
enum BG96_GNSS_ON_DEFAULT {
MODE = 1, // Stand Alone is the only mode supported (1)
MAX_POS_TIME_SEC = 30, // Sec max pos time (30)
FIX_ACCURACY_METERS = 50, // Fix accuracy in meters (50)
NUM_FIX_CHECKS = 0, // Num of checks after fix before powering down GPS (0 - continuous)
GET_LOC_FREQ_SEC = 1, // Check every x sec (1)
RETRY_TIME_SEC = 1 // Time to wait for modem to power up
}
enum BG95_GNSS_ON_DEFAULT {
MODE = 1, // Stand Alone is the only mode supported (1)
ACCURACY = 3,
NUM_FIX_CHECKS = 10, // Num of checks after fix before powering down GPS (0 - continuous)
GET_LOC_FREQ_SEC = 1, // Check every x sec (1)
RETRY_TIME_SEC = 1, // Time to wait for modem to power up
}
enum BG96_GNSS_LOCATION_MODE {
ZERO, // <latitude>,<longitude> format: ddmm.mmmm N/S,dddmm.mmmm E/W
ONE, // <latitude>,<longitude> format: ddmm.mmmmmm,N/S,dddmm.mmmmmm,E/W
TWO // <latitude>,<longitude> format: (-)dd.ddddd,(-)ddd.ddddd
}
enum BG96_RESET_MODE {
COLD_START = 0, // Delete all assistance data except gpsOneXTRA data. Enforce a cold start after starting GNSS
HOT_START = 1, // Do not delete any data. Perform hot start if the conditions are permitted after starting GNSS
WARM_START = 2, // Delete some related data. Perform warm start if the conditions are permitted after starting GNSS
DELETE_DATA = 3 // Delete the gpsOneXTRA assistance data injected into GNSS engine (Not supported by BG95)
}
// Stale location data is often returned immediately after power up
const BG96_GPS_EN_POLLING_TIMEOUT = 3;
/*
* Library -- set as a GLOBAL table so its properties and functions
* should always be in scope
*/
BG96_GPS <- {
VERSION = "1.0.1",
/*
* PUBLIC PROPERTIES
*/
onEvent = null,
onLocation = null,
debug = true,
/*
* PRIVATE PROPERTIES
*/
_session = null,
_locTimer = null,
_pollTimer = null,
_impOSVersion = null,
_minSuppportedImpOS = 43.0,
_modemModel = null,
/*
* PUBLIC FUNCTIONS
*/
/**
* Check whether GNSS is enabled or not.
*
* @returns {bool} Whether GNSS is enabled (true) or not (false).
*/
isGNSSEnabled = function() {
_checkOS();
if (_session == null) return false;
try {
local resp = _session.getstate();
return (resp.state == 1);
} catch(err) {
// In case _session.getstate() fails
return false;
}
},
/**
* Enable GNSS if it is not yet enabled and configure the library.
*
* @param {table} opts - Configuration options. All are optional; defaults will be used in their place.
* Key options include:
* * onEvent - callback for library and GNSS event notifications, including errors.
* * onLocation - callback for location data, including location-specific errors.
* NOTE Responds via the onEvent callback.
*
*/
enableGNSS = function(opts = {}) {
_checkOS();
local gnssMode = ("gnssMode" in opts) ? opts.gnssMode : BG96_GNSS_ON_DEFAULT.MODE;
local posTime = ("maxPosTime" in opts) ? opts.maxPosTime : BG96_GNSS_ON_DEFAULT.MAX_POS_TIME_SEC;
local accuracy = ("accuracy" in opts) ? opts.accuracy : BG96_GNSS_ON_DEFAULT.FIX_ACCURACY_METERS;
local numFixes = ("numFixes" in opts) ? opts.numFixes : BG96_GNSS_ON_DEFAULT.NUM_FIX_CHECKS;
local checkFreq = ("checkFreq" in opts) ? opts.checkFreq : BG96_GNSS_ON_DEFAULT.GET_LOC_FREQ_SEC;
local retryTime = ("retryTime" in opts) ? opts.retryTime : BG96_GNSS_ON_DEFAULT.RETRY_TIME_SEC;
local locMode = ("locMode" in opts) ? opts.locMode : BG96_GNSS_LOCATION_MODE.TWO;
local assistData = ("assistData" in opts) ? opts.assistData : null;
local useAssist = ("useAssist" in opts) ? opts.useAssist : false;
// FROM 0.2.0
// Make callbacks accessible outside function
onEvent = ("onEnabled" in opts && typeof opts.onEnabled == "function") ? opts.onEnabled : onEvent;
onEvent = ("onNotify" in opts && typeof opts.onNotify == "function") ? opts.onNotify : onEvent;
onEvent = ("onEvent" in opts && typeof opts.onEvent == "function") ? opts.onEvent : onEvent;
onLocation = ("onLocation" in opts && typeof opts.onLocation == "function") ? opts.onLocation : onLocation;
if (!isGNSSEnabled()) {
if (_session == null) {
try {
// Open the session to the modem
_session = hardware.gnss.open(function(t) {
if (t.ready == 1) {
enableGNSS(opts);
} else {
_notify("Session could not be opened", null, 0);
}
}.bindenv(this));
} catch(err) {
// Throw caused by modem not yet ready when we open,
// so pause a couple of seconds and re-try
_notify("Modem not yet ready -- backing off for " + retryTime + "s", null, 0);
imp.wakeup(retryTime, function() {
enableGNSS(opts);
}.bindenv(this));
}
return;
}
if (assistData) {
_session.assist.load(function(t) {
if (t.status != 0) {
_notify("Assist data not loaded. ", null, t.status);
} else {
// Prep for re-entry into function
opts.assistData = null;
if (!("useAssist" in opts)) opts.useAssist <- true;
enableGNSS(opts);
}
}.bindenv(this), assistData);
return;
}
if (useAssist) {
// FROM 0.1.5 -- check we have assist data before proceeding
// This will be the case if 'enableGNSS()' called with 'useAssist' set true,
// but 'assistData' is null or passed bad data
local t = _session.assist.read();
if (t.status == 0) {
// There is assist data present, so proceed to enable
// NOTE Enabling assist data on a device that previously lacked assist data
// can cause session.enable() call below to fail with a 509 (GPS_XTRA_NOT_ENABLED).
// So we check for this in the session.enable() call below
try {
t = _session.assist.enable();
if (t.status != 0) {
_notify("Could not enable assist", null, t.status);
}
} catch(err) {
throw "[BG96_GPS] Assist cannot be enabled without hardware RTC";
}
} else {
_notify("Assist data not present or invalid", null, t.status);
}
}
local netData = imp.net.info();
foreach (i in netData.interface) {
if (i.type == "cell") {
_modemModel = i.model;
}
}
_notify("Modem is " + _modemModel, null);
local resp;
if (_modemModel == "BG95-M3") {
gnssMode = ("gnssMode" in opts) ? opts.gnssMode : BG95_GNSS_ON_DEFAULT.MODE;
accuracy = ("accuracy" in opts) ? opts.accuracy : BG95_GNSS_ON_DEFAULT.ACCURACY;
numFixes = ("numFixes" in opts) ? opts.numFixes : BG95_GNSS_ON_DEFAULT.NUM_FIX_CHECKS;
checkFreq = ("checkFreq" in opts) ? opts.checkFreq : BG95_GNSS_ON_DEFAULT.GET_LOC_FREQ_SEC;
resp = _session.enable(gnssMode, accuracy, numFixes, checkFreq);
} else {
resp = _session.enable(gnssMode, posTime, accuracy, numFixes, checkFreq);
}
if (resp.status != 0) {
// Error enabling GNSS
local status = resp.status.tostring();
if (status == BG96_AT_ERROR_CODE.GPS_SESSION_IS_ONGOING ||
status == BG96_AT_ERROR_CODE.GPS_XTRA_NOT_ENABLED ||
status == BG96_AT_ERROR_CODE.GPS_NOT_READY) {
// Retry after 'retryTime'
imp.wakeup(retryTime, function() {
enableGNSS(opts);
}.bindenv(this));
return;
}
_notify("Error enabling GNSS", null, resp.status);
return;
}
}
_notify("GNSS enabled", null);
if (onLocation != null) {
// If there is no delay returns stale loc on first 2 (1sec) requests
_cancelPollTimer();
_pollTimer = imp.wakeup(BG96_GPS_EN_POLLING_TIMEOUT, function() {
_pollLoc(locMode, checkFreq, onLocation);
}.bindenv(this));
}
},
/**
* Disable GNSS and cancel all location polling timers.
*
* @return (bool) Whether GNSS was disabled (true) or not (false).
*/
disableGNSS = function() {
_checkOS();
// Always cancel location timers
_cancelLocTimer();
_cancelPollTimer();
if (isGNSSEnabled()) {
local resp = _session.disable();
if (resp.status != 0) {
_notify("Could not disable GNSS", null, resp.status);
return false;
}
}
// Zap the session
_session = null;
return true;
},
/**
* Cancel all location polling without disabling GNSS.
*
*/
cancelPoll = function() {
// Always cancel location timers
_cancelLocTimer();
_cancelPollTimer();
},
/**
* Get the device's locatiom using GNSS.
*
* @param {table} opts - Configuration options. All are optional; defaults will be used in their place.
* Key options include:
* * onLocation {function} - callback for location data, including location-specific errors.
* This replaces a onLocation callback set using enableGNSS(),
* You MUST set this value via either of these calls
* * checkFreq {integer} - How often the modem is polled for location data. Default: 1 second.
* * poll {bool} - Pass in true to trigger regular location sensing. Default: false.
* * mode {integer} - The location data string format. See the enum section above.
*
* This function responds via the onEvent callback.
*/
getLocation = function(opts = {}) {
_checkOS();
local poll = ("poll" in opts) ? opts.poll : false;
local mode = ("mode" in opts) ? opts.mode : BG96_GNSS_LOCATION_MODE.ZERO;
local checkFreq = ("checkFreq" in opts) ? opts.checkFreq : BG96_GNSS_ON_DEFAULT.GET_LOC_FREQ_SEC;
local waitFix = ("waitFix" in opts) ? opts.waitFix : false;
// FROM 0.2.0
// Make callbacks accessible outside function
onLocation = ("onLocation" in opts && typeof opts.onLocation == "function") ? opts.onLocation : onLocation;
// Make sure we have a way to report the location
if (onLocation == null) {
_notify("No location report callback", null, 1);
return;
}
// Make sure we're enabled
if (isGNSSEnabled()) {
if (poll) {
_pollLoc(mode, checkFreq, onLocation);
} else {
_getLoc(mode, function(loc) {
if (loc == null) {
loc = waitFix ? {"fix" : "GPS fix not yet available"} : {"error" : "GPS fix not available"};
}
if (onLocation) onLocation(loc);
});
}
} else {
if (onLocation) onLocation({"error" : "GNSS not enabled"});
}
},
/**
* Check whether the modem has assist data, and whether it is valid.
*
* @returns {bool} Whether the assist data is valid (true), invalid (false),
* absent (false), or validity could not be determined (false).
* NOTE Function also responds via the onEvent callback.
*/
isAssistDataValid = function() {
_checkOS();
if (_session != null) {
local t = _session.assist.read();
local valid = (t.status == 0);
if ("xtradatadurtime" in t && t.xtradatadurtime == 0) valid = false;
local msg = valid ? "GNSS assist data valid" : "GNSS assist data invalid";
local data = {};
data.valid <- valid;
if (valid) data.time <- _getValidTime(t.injecteddatatime);
_notify(msg, data);
return valid;
} else {
_notify("GNSS not enabled", null, 1);
return false;
}
},
/**
* Delete any existing assist data.
*
* @param {integer} mode - BG96 deletion mode. For possible values and their effects,
* see the enum BG96_RESET_MODE, above.
* Responds via the onEvent callback.
*/
deleteAssistData = function(mode = BG96_RESET_MODE.DELETE_DATA) {
_checkOS();
if (_session == null) {
// We have to make a session in order to delete the assist data
try {
_session = hardware.gnss.open(function(t) {
if (t.ready == 1) _deleteAssist(mode);
}.bindenv(this));
} catch(err) {
_notify("Modem not yet ready -- backing off for " + retryTime + "s", null, 0);
imp.wakeup(retryTime, function() {
deleteAssistData(mode);
}.bindenv(this));
}
} else {
// We have a session, but GNSS may be enabled
if (isGNSSEnabled()) {
// GNSS enabled, so disable before deleting
local resp = _session.disable();
if (resp.status != 0) {
_notify("Could not delete assist data", null, resp.status);
} else {
// GNSS now disabled, so we can proceed with deletion
_deleteAssist(mode);
}
} else {
// GNSS disabled, but we have a session, so delete assist
_deleteAssist(mode);
}
}
},
/*
* PRIVATE FUNCTIONS -- DO NOT CALL DIRECTLY
*/
// Loop that polls for location, if location data or error (excluding no fix available) is received it is
// passed to the onLoc callback
_pollLoc = function(mode, checkFreq, onLoc) {
// Only allow one schedule timer at a time
_cancelLocTimer();
// Schedule next location check
_locTimer = imp.wakeup(checkFreq, function() {
_pollLoc(mode, checkFreq, onLoc);
}.bindenv(this));
// Fetch and process location
// Returns `null` if GPS error is no fix now, otherwise returns table with keys fix or error
_getLoc(mode, function(loc) {
if (loc != null) {
// Pass error or location fix to main application
imp.wakeup(0, function() { onLoc(loc); }.bindenv(this));
}
});
},
// Sends AT command to get location, mode parameter sets the data lat/lng data format
// Calls back with null if no fix is available or the response as a table that may contain slots:
// error (string): The error encountered
// fix (table/string): response data string if location parsing failed otherwise a table with
// slots: cog, alt, fixType, time, numSats, lat, lon, spkm, spkn, utc, data, hdop
_getLoc = function(mode, cb) {
try {
_session.readposition(function(resp) {
local data = {};
if (resp.status != 0) {
// Look for expected errors
local errorCode = resp.status.tostring();
switch (errorCode) {
case BG96_AT_ERROR_CODE.GPS_NO_FIX_NOW:
// 'No fix yet' Reported on callback when supplied value is null
return cb(null);
case BG96_AT_ERROR_CODE.GPS_SESSION_NOT_ACTIVE:
return cb(data.error <- "GPS not enabled");
default:
return cb(data.error <- "AT error code: " + errorCode);
}
}
if (resp.status == 0 && "quectel" in resp) {
data.fix <- _parseLocData(resp.quectel, mode);
}
cb(data);
}.bindenv(this), mode);
} catch(err) {
cb({"error":err});
}
},
// Cancels location polling timer
_cancelLocTimer = function() {
if (_locTimer != null) {
imp.cancelwakeup(_locTimer);
_locTimer = null;
}
},
_cancelPollTimer = function() {
if (_pollTimer != null) {
imp.cancelwakeup(_pollTimer);
_pollTimer = null;
}
},
// Format GPS timestamp
_formatTimeStamp = function(d, utc) {
// Input d: DDMMYY, utc HHMMSS.S
// Formatted result: YYYY-MM-DD HH:MM:SS.SZ
return format("20%s-%s-%s %s:%s:%sZ", d.slice(4),
d.slice(2, 4),
d.slice(0, 2),
utc.slice(0, 2),
utc.slice(2, 4),
utc.slice(4));
},
// Parses location data into table based on mode
_parseLocData = function(parsed, mode) {
try {
switch(mode) {
case BG96_GNSS_LOCATION_MODE.ZERO:
// 190629.0,3723.7238N,12206.1395W,1.0,16.0,2,188.18,0.0,0.0,031219,09
case BG96_GNSS_LOCATION_MODE.TWO:
// 190629.0,37.39540,-122.10232,1.0,16.0,2,188.18,0.0,0.0,031219,09
case BG96_GNSS_LOCATION_MODE.ONE:
// 190629.0,3723.723831,N,12206.139526,W,1.0,16.0,2,188.18,0.0,0.0,031219,09
return {
"utc" : parsed.utc,
"lat" : parsed.latitude,
"lon" : parsed.longitude,
"hdop" : parsed.hdop,
"alt" : parsed.altitude,
"fixType" : parsed.fix,
"cog" : parsed.cog,
"spkm" : parsed.spkm,
"spkn" : parsed.spkn,
"date" : parsed.date,
"numSats" : parsed.nsat,
"time" : _formatTimeStamp(parsed.date, parsed.utc),
};
default:
throw "Unknown mode";
}
} catch(ex) {
return parsed;
}
},
// Creates a log if device is online and debug flag is set to true
_log = function(msg, isError = false) {
if (debug && server.isconnected()) {
if (isError) {
server.error(msg);
} else {
server.log(msg);
}
}
},
// General notification poster
_notify = function(msg, data = null, code = 999) {
if (onEvent != null) {
local info = {};
if (data != null) info.data <- data;
if (code == 999) {
info.event <- msg;
} else {
info.error <- msg;
info.errcode <- code;
}
onEvent(info);
}
},
// Check we're running on a system that support GNSS
_checkOS = function() {
if (!("gnss" in hardware)) {
throw "The BG96_GPS library requires impOS 43 or above";
}
},
// FROM 0.1.5
// Get assist data remaining validity period in mins
// 'uploadDate' is <= now, format: YYYY/MM/DD,hh:mm:ss
// Returns -99 on error
_getValidTime = function(uploadDate, now = null) {
local invalid = -1;
// Convert date string to date table
local date_parts = split(uploadDate, ","); // YYYY/MM/DD,hh:mm:ss
if (date_parts.len() != 2) return invalid;
local date_string = split(date_parts[0], "/"); // YYYY/MM/DD
local time_string = split(date_parts[1], ":"); // hh:mm:ss
local old = {"year" : date_string[0].tointeger(),
"month": date_string[1].tointeger() - 1,
"day": date_string[2].tointeger(),
"hour": time_string[0].tointeger(),
"min": time_string[1].tointeger()};
if (now == null) now = date();
if (now.year - old.year > 1) return invalid;
if (now.year < old.year || (now.year == old.year && old.month > now.month)) return invalid;
local remaining = _getTime(now) - _getTime(old);
return (remaining < 10080 ? remaining : invalid);
},
// FROM 0.2.1
_getTime = function(adate, baseYear = 2010) {
// Base month lengths
local mls = [31,28,31,30,31,30,31,31,30,31,30,31];
// Start with today's minutes
local mins = adate.min + (60 * adate.hour);
local days = 0;
// Add minutes for each day of each year from base to last year
for (local i = baseYear ; i < adate.year ; i++) {
days += (_isLeap(i) ? 366 : 365);
}
// Add minutes for days this year
for (local i = 0 ; i < adate.month ; i++) {
local month_days = mls[i];
// Add an extra day to Feb for leap years
if (i == 1 && _isLeap(adate.year)) month_days += 1;
days += month_days;
}
// Add minutes for days this month
days += (adate.day - 1);
// Return the minutes
return (days * 1440) + mins;
},
//FROM 0.2.1
_isLeap = function(year) {
return ((year % 4 == 0) && ((year % 100 > 0) || (year % 400 == 0)));
},
// FROM 0.1.5
_deleteAssist = function(mode) {
local t = _session.assist.reset(mode);
if (t.status == 0) {
_notify("Assist data deleted");
} else {
_notify("Could not delete assist data", null, t.status);
}
}
}