Files
Trigalarm/src/pkjs/index.js
T
2026-10-21 18:31:17 -04:00

62 lines
1.9 KiB
JavaScript

var Clay = require('@rebble/clay');
var clayConfig = require('./config');
var customClay = require('./customClay');
var clay = new Clay(clayConfig, customClay, { autoHandleEvents: false });
Pebble.addEventListener('showConfiguration', function () {
Pebble.openURL(clay.generateUrl());
});
// custom listener so messages can be sent from customClay.js
Pebble.addEventListener('webviewclosed', function(e) {
if (!e || !e.response) { return; }
var data;
try {
data = JSON.parse(e.response.match(/^\{/) ? e.response : decodeURIComponent(e.response));
} catch (err) {
return;
}
if (data.resetAlarm) {
Pebble.sendAppMessage({ PKJS_ALARM_RESET: 1 });
return;
}
var dict = clay.getSettings(e.response);
Pebble.sendAppMessage(dict, function(e) {
console.log('Sent config data to Pebble');
}, function(e) {
console.log('Failed to send config data!');
console.log(JSON.stringify(e));
});
});
Pebble.addEventListener("ready", function () {
Pebble.sendAppMessage({ PKJS_READY: 1 });
});
Pebble.addEventListener("appmessage", function (dict) {
if (dict.payload["PKJS_PAVLOK_ZAP"]) {
const cfg = JSON.parse(localStorage.getItem('clay-settings'));
callAPI(cfg.PKJS_PAVLOK_API_KEY, parseInt(dict.payload["PKJS_PAVLOK_ZAP"]));
}
});
function callAPI(apiKey, zap_intensity) {
const data = JSON.stringify({
stimulus: {
stimulusType: 'zap',
stimulusValue: zap_intensity,
reason: 'Trigalarm'
}
});
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('POST', 'https://api.pavlok.com/api/v5/stimulus/send');
xhr.setRequestHeader('accept', 'application/json');
xhr.setRequestHeader('content-type', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
xhr.send(data);
}