BangleApps/apps/ha/ha.app.js

100 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-06-27 14:36:26 +00:00
/**
* This app uses the ha library to send trigger to HomeAssistant.
*/
var ha = require("ha.lib.js");
2022-06-20 11:36:49 +00:00
var W = g.getWidth(), H = g.getHeight();
var position=0;
2022-06-27 14:36:26 +00:00
var triggers = ha.getTriggers();
2022-06-24 10:45:11 +00:00
2022-06-20 11:36:49 +00:00
function draw() {
g.reset().clearRect(Bangle.appRect);
2022-06-23 18:32:39 +00:00
var h = 22;
2022-06-24 06:33:14 +00:00
g.setFont("Vector", h);
var trigger = triggers[position];
var w = g.stringWidth(trigger.display);
2022-06-20 11:36:49 +00:00
2022-06-24 06:33:14 +00:00
g.setFontAlign(-1,-1);
2022-06-27 14:46:40 +00:00
var icon = trigger.getIcon();
g.setColor(g.theme.fg).drawImage(icon, 12, H/5-2-5);
g.drawString("Home", icon.width + 20, H/5-5);
g.drawString("Assistant", icon.width + 18, H/5+24-5);
2022-06-23 18:06:53 +00:00
2022-06-24 06:33:14 +00:00
g.setFontAlign(0,0);
var ypos = H/5*3+23;
2022-06-24 06:46:31 +00:00
g.drawRect(W/2-w/2-8, ypos-h/2-8, W/2+w/2+5, ypos+h/2+5);
g.fillRect(W/2-w/2-6, ypos-h/2-6, W/2+w/2+3, ypos+h/2+3);
g.setColor(g.theme.bg).drawString(trigger.display, W/2, ypos);
// draw arrows
g.setColor(g.theme.fg);
if (position > 0) {
g.drawLine(10, H/2, 20, H/2 - 10);
g.drawLine(10, H/2, 20, H/2 + 10);
}
if (position < triggers.length -1) {
g.drawLine(W - 10, H/2, W - 20, H/2 - 10);
g.drawLine(W - 10, H/2, W - 20, H/2 + 10);
}
2022-06-20 11:36:49 +00:00
}
function toLeft() {
2022-06-23 18:06:53 +00:00
Bangle.buzz(40, 0.6);
2022-06-20 11:36:49 +00:00
position -= 1;
position = position < 0 ? triggers.length-1 : position;
draw();
}
function toRight() {
Bangle.buzz(40, 0.6);
position += 1;
position = position >= triggers.length ? 0 : position;
draw();
}
function sendTrigger() {
2022-06-27 14:46:40 +00:00
ha.sendTrigger("TRIGGER");
// Now send the selected trigger
2022-06-23 18:06:53 +00:00
Bangle.buzz(80, 0.6).then(()=>{
2022-06-27 14:36:26 +00:00
ha.sendTrigger(triggers[position].trigger);
2022-06-23 18:06:53 +00:00
setTimeout(()=>{
Bangle.buzz(80, 0.6);
}, 250);
});
}
Bangle.on('touch', function(btn, e){
var left = parseInt(g.getWidth() * 0.3);
var right = g.getWidth() - left;
var isLeft = e.x < left;
var isRight = e.x > right;
if(isLeft){
toLeft();
2024-04-21 18:32:44 +00:00
}else if (isRight){
toRight();
2024-04-21 18:32:44 +00:00
}else{
sendTrigger();
2022-06-20 11:36:49 +00:00
}
});
Bangle.on("swipe", (lr,ud) => {
2024-04-21 18:32:44 +00:00
if (lr == -1) {
toLeft();
} else if (lr == 1) {
toRight();
}
});
2022-06-27 14:36:26 +00:00
2022-06-24 10:45:11 +00:00
// Send intent that the we started the app.
2022-06-27 14:36:26 +00:00
ha.sendTrigger("APP_STARTED");
2022-06-24 06:33:14 +00:00
// Next load the widgets and draw the app
2022-06-20 11:36:49 +00:00
Bangle.loadWidgets();
2022-06-24 06:33:14 +00:00
Bangle.drawWidgets();
2022-06-27 14:36:26 +00:00
// Draw app
2022-06-24 06:33:14 +00:00
draw();
setWatch(_=>load(), BTN1);