-
Notifications
You must be signed in to change notification settings - Fork 2
/
aois.ts
51 lines (42 loc) · 1.27 KB
/
aois.ts
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
import { getLogger } from "~/app/core/utils/logger";
import { AreaOfInterest, areasOfInterest } from "@awarns/geofencing";
export async function setupAreasOfInterest() {
const logger = getLogger("AreasOfInterestManager");
const currentAoIs = await areasOfInterest.getAll();
const newAoIs: Array<AreaOfInterest> = [
// Add your areas of interest here
];
if (!aoisDidChange(currentAoIs, newAoIs)) {
return;
}
await areasOfInterest.deleteAll();
await areasOfInterest.insert(newAoIs);
logger.info("Areas of interest updated");
}
function aoisDidChange(
currentAoIs: Array<AreaOfInterest>,
newAoIs: Array<AreaOfInterest>
): boolean {
if (currentAoIs.length !== newAoIs.length) {
return true;
}
const currentSorted = sort(currentAoIs);
const newSorted = sort(newAoIs);
for (let i = 0; i < currentSorted.length; i++) {
if (currentSorted[i].id !== newSorted[i].id) {
return true;
}
}
return false;
}
function sort(aois: Array<AreaOfInterest>): Array<AreaOfInterest> {
return [...aois].sort((aoi1, aoi2) => {
if (aoi1.id < aoi2.id) {
return -1;
}
if (aoi1.id > aoi2.id) {
return 1;
}
return 0;
});
}