BangleApps/apps/solarclock/solar_location.js

105 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-07-17 20:04:04 +00:00
const storage = require("Storage");
class LocationManager {
constructor(locations) {
this.idx=0;
this.locations = locations;
this.listeners = [];
this.in_use = true;
this.gpsPower = 0;
this.location_info = null;
this.gpsRequested = false;
2021-07-17 20:04:04 +00:00
}
init(){
try {
this.location_info = storage.readJSON("solar_loc." + this.getName() + ".json");
} catch(e){
console.log("failed to load location:" + this.getName())
}
if(this.location_info == null){
this.location_info = {};
}
if (this.isGPSLocation() && this.getCoordinates() == null) {
this.requestGpsUpdate();
}
}
initCallback(){
2021-07-17 20:04:04 +00:00
Bangle.on('GPS', (g) => {
if (!this.in_use)
return;
if (g.fix) {
var loc_info = {
coordinates: [g.lon, g.lat]
};
console.log("Received gps fixing:" + JSON.stringify(loc_info));
storage.writeJSON("solar_loc.local.json", loc_info);
this.setGPSPower(0);
2021-07-17 20:04:04 +00:00
if(this.isGPSLocation()){
this.location_info = loc_info;
this.notifyUpdate();
}
}
});
}
setGPSPower(power){
if(power && !this.gpsRequested){
this.initCallback();
}
this.gpsPower = power;
this.gpsRequested = true;
Bangle.setGPSPower(this.gpsPower);
}
getGPSPower(){return this.gpsPower;}
requestGpsUpdate(){
if (this.getGPSPower() == 0) {
console.log("updating gps location update");
this.setGPSPower(1);
} else {
console.log("gps already updating");
}
2021-07-17 20:04:04 +00:00
}
isGPSLocation(){return this.getName() == 'local';}
addUpdateListener(listener){this.listeners.push(listener);}
nextLocation() {
if(this.locations.length > 1) {
this.idx += 1;
this.idx = this.idx % this.locations.length;
console.log("location now:" + this.getName());
this.init();
this.notifyUpdate();
} else {
console.log("no extra locations found");
}
}
notifyUpdate(){
for(var i=0; i<this.listeners.length; i++){
this.listeners[i](this);
}
}
getUTCOffset(){return this.location_info.utc_offset;}
getName(){return this.locations[this.idx];}
getCoordinates(){return this.location_info.coordinates;}
shutdown(){
this.in_use=false;
this.setGPSPower(0);
}
}
const LOCATIONS_FILE = "solar_locations.json";
const LocationUtils = {
load_locations : ()=>{
2021-07-25 19:57:17 +00:00
var locations;
try {
locations = storage.readJSON(LOCATIONS_FILE);
} catch(e){
console.log("failed to load locations file:" + e);
}
if(locations == null)
locations = ['local'];
2021-07-17 20:04:04 +00:00
console.log("loaded locations:" + locations);
var mgr = new LocationManager(locations);
mgr.init();
return mgr;
}
}
module.exports = LocationUtils;