Skip to content

Commit fe764ce

Browse files
authored
Refactor boot.js for improved location handling
1 parent 99ed1f4 commit fe764ce

File tree

1 file changed

+124
-85
lines changed

1 file changed

+124
-85
lines changed

apps/phoneremind/boot.js

Lines changed: 124 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,130 @@
11
{
2-
let locationsFile="phoneremind.json";
3-
let buzzInterval;
4-
let addLocation=function(location){
5-
6-
let locs = require("Storage").readJSON(locationsFile, 1) || [];
7-
locs.push(location);
8-
require("Storage").writeJSON(locationsFile, locs);
9-
10-
}
11-
let getAllLocations = function() {
12-
return require("Storage").readJSON(locationsFile, 1) || [];
13-
};
14-
15-
let getMyLocation = function() {
16-
if (!require("Storage").read("mylocation.json")) {
17-
require("Storage").writeJSON("mylocation.json", {
18-
location: "Unknown",
19-
lat: 0,
20-
lon: 0
21-
});
2+
let locationsFile = "phoneremind.json";
3+
let buzzInterval;
4+
let addLocation = function (location) {
5+
let connected;
6+
let promptShown = false;
7+
let locs = require("Storage").readJSON(locationsFile, 1) || [];
8+
locs.push(location);
9+
require("Storage").writeJSON(locationsFile, locs);
10+
11+
}
12+
let getSettings=function(){
13+
return Object.assign({
14+
precision: 30,
15+
timeDelay:30000
16+
}, require('Storage').readJSON("phoneremind.settings.json", true) || {});
17+
}
18+
let getAllLocations = function () {
19+
return require("Storage").readJSON(locationsFile, 1) || [];
20+
};
21+
22+
let getMyLocation = function () {
23+
if (!require("Storage").read("mylocation.json")) {
24+
require("Storage").writeJSON("mylocation.json", {
25+
location: "Unknown",
26+
lat: 0,
27+
lon: 0
28+
});
29+
}
30+
return require("Storage").readJSON("mylocation.json", 1);
31+
};
32+
33+
let buzz = function () {
34+
Bangle.buzz(230);
35+
}
36+
37+
let convertCoordsToMeters = function (lat, lon) {
38+
return {
39+
lat: 110574 * lat,
40+
lon: 111320 * Math.cos(lat * Math.PI / 180) * lon
41+
42+
}
2243
}
23-
return require("Storage").readJSON("mylocation.json", 1);
24-
};
25-
26-
let buzz=function(){
27-
Bangle.buzz(230);
28-
}
29-
30-
let disconnected=function(){
31-
myLocation=getMyLocation();
32-
locs=getAllLocations();
33-
print(myLocation);
34-
print(locs);
35-
var useGPS=false;
36-
if(useGPS){
37-
38-
}else{
39-
40-
myLocation=getMyLocation();
41-
locs=getAllLocations();
42-
print(myLocation);
43-
44-
for (let location of locs) {
45-
if(Math.abs(myLocation.lat - location.lat) < 0.0001 && Math.abs(myLocation.lon -location.lon) < 0.0001){
46-
//at a familiar location, no action needed.
47-
return;
44+
45+
46+
47+
48+
let disconnected = function () {
49+
connected = false;
50+
var myLocation = getMyLocation();
51+
var locs = getAllLocations();
52+
print(locs);
53+
var useGPS = false;
54+
if (useGPS) {
55+
56+
} else {
57+
58+
myLocation = getMyLocation();
59+
var convLoc = convertCoordsToMeters(myLocation.lat, myLocation.lon)
60+
myLocation.lat = convLoc.lat;
61+
myLocation.lon = convLoc.lon;
62+
63+
locs = getAllLocations();
64+
65+
print(myLocation);
66+
67+
for (let location of locs) {
68+
if (Math.abs(myLocation.lat - location.lat) < getSettings().precision && Math.abs(myLocation.lon - location.lon) < getSettings().precision) {
69+
//at a familiar location, no action needed.
70+
return;
71+
}
72+
}
73+
//no location matched the current one...
74+
75+
76+
77+
setTimeout(function(){
78+
if(connected==true)return;
79+
buzz();
80+
if (require("Storage").readJSON("setting.json", true).quiet != 2) {
81+
82+
setTimeout(function () {
83+
buzzInterval = setInterval(buzz, 850);
84+
}, 750);
85+
}
86+
promptShown = true;
87+
E.showPrompt("Phone was disconnected. It may have been left in " + myLocation.location + ".", {
88+
89+
title: "Phone Left Behind",
90+
buttons: { "Ok": true, "Save Location": false }
91+
92+
}).then(function (answer) {
93+
promptShown = false;
94+
clearInterval(buzzInterval);
95+
if (answer) {
96+
Bangle.load();
97+
} else {
98+
addLocation({
99+
city: myLocation.location,
100+
lat: myLocation.lat,
101+
lon: myLocation.lon
102+
});
103+
E.showPrompt("Added location in " + myLocation.location + " to familiar locations.", {
104+
title: "Location Saved",
105+
buttons: { "Ok": true }
106+
})
107+
.then(function () {
108+
Bangle.load();
109+
});
110+
}
111+
});
112+
},getSettings().timeDelay)
113+
48114
}
49-
}
50-
//no location matched the current one...
51-
buzz()
52-
if(require("Storage").readJSON("setting.json",true).quiet!=2){
53-
54-
setTimeout(function(){
55-
buzzInterval=setInterval(buzz,850);
56-
},750);
57-
}
58-
E.showPrompt("Phone was disconnected. It may have been left in "+myLocation.location+".",{
59-
60-
title: "Phone Left Behind",
61-
buttons : {"Ok":true,"Save Location":false}
62-
63-
}).then(function(answer) {
64-
clearInterval(buzzInterval);
65-
if(answer){
66-
Bangle.load();
67-
}else{
68-
addLocation({
69-
city:myLocation.location,
70-
lat:myLocation.lat,
71-
lon:myLocation.lon
72-
});
73-
E.showPrompt("Added location in "+myLocation.location+" to familiar locations.",{
74-
title:"Location Saved",
75-
buttons:{"Ok":true}
76-
})
77-
.then(function() {
78-
Bangle.load();
79-
});
115+
}
116+
117+
let connectHandler = function () {
118+
connected = true;
119+
if (promptShown) {
120+
//prompt is shown, dismiss
121+
if (buzzInterval) clearInterval(buzzInterval);
122+
load();
123+
80124
}
81-
});
82-
125+
83126
}
84-
}
85-
86-
87-
88-
NRF.on('disconnect', disconnected);
89-
myLocation=getMyLocation();
90-
locs=getAllLocations();
127+
128+
NRF.on('disconnect', disconnected);
129+
NRF.on('connect', connectHandler);
91130
}

0 commit comments

Comments
 (0)