2023-01-08 11:31:19 +00:00
//WIDGETS = {}; // <-- for development only
( ( ) => {
// persistant vals
let storedData ;
let settings ;
function loadSettings ( ) { // stolen from https://github.com/espruino/BangleApps/blob/master/apps/widpedom/widget.js
2023-02-14 20:40:16 +00:00
try {
const d = require ( 'Storage' ) . readJSON ( "widbgjs.settings.json" , 1 ) || { } ;
settings = Object . assign ( {
'unitIsMmol' : true ,
'expireThreshold' : 600000 ,
//'reloadInterval': 300000, // 2add in the future
'hide' : false
} , d || { } ) ;
return d ;
} catch ( e ) {
//console.log(e.toString());
return ;
}
2023-01-08 11:31:19 +00:00
}
function loadVals ( ) {
try {
const d = require ( 'Storage' ) . readJSON ( "widbgjs.json" , 1 ) || { } ;
storedData = Object . assign ( {
2023-02-14 20:50:08 +00:00
'bg' : null ,
2023-01-08 11:31:19 +00:00
'bgTimeStamp' : null ,
2023-02-14 20:56:33 +00:00
'bgDirection' : null
2023-01-08 11:31:19 +00:00
} , d || { } ) ;
return d ;
} catch ( e ) {
Bangle . removeFile ( "widbgjs.json" ) ;
}
2023-02-14 20:40:16 +00:00
return ;
2023-01-08 11:31:19 +00:00
}
function calculateRotation ( bgDirection ) {
var a = 90 ;
// get the arrow right (https://github.com/StephenBlackWasAlreadyTaken/NightWatch/blob/6de1d3775c6e447177c12f387f647628cc8e24ce/mobile/src/main/java/com/dexdrip/stephenblack/nightwatch/Bg.java)
switch ( bgDirection ) {
case ( "DoubleDown" ) :
g . setColor ( "#f00" ) ;
a = 180 ;
break ;
case ( "SingleDown" ) :
a = 180 ;
break ;
case ( "DoubleUp" ) :
g . setColor ( "#f00" ) ;
a = 0 ;
break ;
case ( "SingleUp" ) :
a = 0 ;
break ;
case ( "FortyFiveUp" ) :
a = 45 ;
break ;
case ( "FortyFiveDown" ) :
a = 135 ;
break ;
case ( "Flat" ) :
a = 90 ;
break ;
}
// turn the arrow thanks to (https://forum.espruino.com/conversations/344607/)
const p180 = Math . PI / 180 ;
2024-03-14 09:47:38 +00:00
/ *
2023-01-08 11:31:19 +00:00
// a is defined above
var r = 21 ;
var x = r * Math . sin ( a * p180 ) ;
var y = r * Math . cos ( a * p180 ) ;
2024-03-04 20:34:50 +00:00
* /
2023-01-08 11:31:19 +00:00
return a * p180 ;
}
function getBG ( bg ) {
var tmp = null ;
try {
if ( storedData . bg !== null ) {
tmp = bg ;
if ( settings . unitIsMmol ) {
tmp /= 18 ;
tmp = tmp . toFixed ( 1 ) ;
}
}
} catch ( e ) { }
return tmp ;
}
function isBgTooOld ( bgTimeStamp ) {
var currTimeInMilli = new Date ( ) . valueOf ( ) ;
try {
if ( bgTimeStamp === null ) {
return true ;
}
if ( currTimeInMilli - settings . expireThreshold <= bgTimeStamp ) {
return false ;
}
} catch ( e ) { }
return true ;
}
function draw ( ) {
loadSettings ( ) ;
2023-02-14 14:54:06 +00:00
if ( settings . hide ) {
2023-02-14 20:40:16 +00:00
return ;
}
console . log ( settings . unitIsMmol . toString ( ) ) ;
2023-01-08 11:31:19 +00:00
loadVals ( ) ;
2024-03-14 09:47:38 +00:00
let outpt = getBG ( storedData . bg ) ;
2023-01-08 11:31:19 +00:00
if ( outpt === null ) { // this means no value has been received yet
outpt = "BG" ;
bgTimeStamp = "0" ;
}
// prepare to write on the screen
g . reset ( ) . clearRect ( this . x , this . y , this . x + this . width , this . y + 23 ) ; // erase background
g . setFont ( 'Vector' , 22 ) ;
g . setColor ( g . theme . fg ) ;
2023-02-14 20:40:16 +00:00
// if the value is too old strikethrough it
if ( isBgTooOld ( storedData . bgTimeStamp ) ) {
g . fillRect ( this . x + 5 , this . y + 9 , g . stringWidth ( outpt ) , this . y + 10 ) ;
2023-01-08 11:31:19 +00:00
}
2023-02-14 20:40:16 +00:00
g . drawImage ( atob ( "FBQBAGAADwAB+AA/wAduAGZgAGAABgAAYAAGAABgAAYAAGAABgAAYAAGAABgAAYAAGAABgA=" ) , this . x + 60 , this . y + 9 , { rotate : calculateRotation ( storedData . bgDirection ) } ) ;
2023-01-08 11:31:19 +00:00
g . setColor ( g . theme . fg ) . drawString ( outpt , this . x + 5 , this . y ) ;
2023-02-14 20:40:16 +00:00
2023-01-08 11:31:19 +00:00
}
setInterval ( function ( ) {
WIDGETS [ "widbgjs" ] . draw ( WIDGETS [ "widbgjs" ] ) ;
2023-02-14 20:40:16 +00:00
} , 5 * 60000 ) ; // update every 5 minutes (5 * 60000)
2023-01-08 11:31:19 +00:00
// add your widget
WIDGETS [ "widbgjs" ] = {
area : "tl" ,
width : 72 ,
draw : draw
} ;
} ) ( ) ;
//Bangle.drawWidgets(); // <-- for development only