2017-11-22 17:10:33 +00:00
|
|
|
'use strict';
|
|
|
|
var mobile = {};
|
|
|
|
|
2017-11-22 19:32:13 +00:00
|
|
|
mobile.ble = {
|
|
|
|
BLENO_DEVICE_NAME : 'intval3',
|
|
|
|
DEVICE_ID : 'intval3',
|
|
|
|
SERVICE_ID : '149582bd-d49d-4b5c-acd1-1ae503d09e7a',
|
|
|
|
CHAR_ID : '47bf69fb-f62f-4ef8-9be8-eb727a54fae4', //general data
|
|
|
|
WIFI_ID : '3fe7d9cf-7bd2-4ff0-97c5-ebe87288c2cc', //wifi only
|
2017-12-13 22:52:25 +00:00
|
|
|
devices : [],
|
|
|
|
device : {},
|
|
|
|
connected : false,
|
|
|
|
active : false
|
2017-11-22 19:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mobile.ble.scan = function () {
|
2017-12-13 22:52:25 +00:00
|
|
|
spinnerShow();
|
|
|
|
ble.scan([], 5, mobile.ble.onDiscover, mobile.ble.onError);
|
|
|
|
mobile.ble.devices = [];
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!mobile.ble.connected) {
|
|
|
|
spinnerHide();
|
|
|
|
alert('No INTVAL devices found.');
|
2017-12-13 23:40:14 +00:00
|
|
|
settingsPage();
|
2017-12-13 22:52:25 +00:00
|
|
|
}
|
|
|
|
}, 5000)
|
2017-11-22 19:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mobile.ble.onDiscover = function (device) {
|
2017-12-13 23:40:14 +00:00
|
|
|
if (device && device.name && device.name.indexOf('intval3') !== -1) {
|
2017-12-13 22:52:25 +00:00
|
|
|
console.log('BLE - Discovered INTVAL');
|
|
|
|
console.dir(device);
|
|
|
|
mobile.ble.devices.push(device);
|
|
|
|
if (!mobile.ble.connected) {
|
|
|
|
mobile.ble.connect(device);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//console.log(`BLE - Discovered Other ${device.id}`);
|
|
|
|
}
|
2017-11-22 19:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:52:25 +00:00
|
|
|
mobile.ble.connect = function (device) {
|
|
|
|
console.log(`BLE - Connecting to ${device.id}`)
|
|
|
|
ble.connect(device.id, (peripheral) => {
|
|
|
|
mobile.ble.onConnect(peripheral, device);
|
2017-11-22 19:32:13 +00:00
|
|
|
}, mobile.ble.onError);
|
|
|
|
};
|
|
|
|
|
2017-12-13 22:52:25 +00:00
|
|
|
mobile.ble.onConnect = function (peripheral, device) {
|
2017-12-13 23:40:14 +00:00
|
|
|
const elem = document.getElementById('bluetooth')
|
|
|
|
const option = document.createElement('option')
|
|
|
|
const disconnect = document.getElementById('disconnect');
|
|
|
|
const scan = document.getElementById('scan');
|
|
|
|
|
2017-12-13 22:52:25 +00:00
|
|
|
spinnerHide();
|
|
|
|
console.log(`BLE - Connected to ${device.id}`);
|
2017-11-22 19:32:13 +00:00
|
|
|
console.log(peripheral);
|
2017-12-13 22:52:25 +00:00
|
|
|
console.dir(device);
|
2017-12-13 23:40:14 +00:00
|
|
|
|
2017-12-13 22:52:25 +00:00
|
|
|
mobile.ble.device = device;
|
|
|
|
mobile.ble.connected = true;
|
2017-12-13 23:40:14 +00:00
|
|
|
|
|
|
|
elem.innerHTML = '';
|
|
|
|
option.text = device.name;
|
|
|
|
option.value = device.id;
|
|
|
|
elem.add(option);
|
|
|
|
|
|
|
|
disconnect.classList.add('active');
|
|
|
|
scan.classList.remove('active');
|
|
|
|
|
2017-12-13 22:52:25 +00:00
|
|
|
getState();
|
|
|
|
};
|
|
|
|
|
|
|
|
mobile.ble.disconnect = function () {
|
2017-12-13 23:40:14 +00:00
|
|
|
const elem = document.getElementById('bluetooth');
|
|
|
|
const option = document.createElement('option');
|
|
|
|
const disconnect = document.getElementById('disconnect');
|
|
|
|
const scan = document.getElementById('scan');
|
|
|
|
let device;
|
2017-12-13 22:52:25 +00:00
|
|
|
if (!mobile.ble.connected) {
|
|
|
|
console.warn('Not connected to any device')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
device = mobile.ble.device
|
|
|
|
console.log(`BLE - Disconnecting from ${device.id}`)
|
|
|
|
ble.disconnect(device.id, mobile.ble.onDisconnect, mobile.ble.onDisconnect);
|
2017-12-13 23:40:14 +00:00
|
|
|
|
|
|
|
elem.innerHTML = '';
|
|
|
|
option.text = 'N/A';
|
|
|
|
elem.add(option);
|
|
|
|
|
|
|
|
disconnect.classList.remove('active');
|
|
|
|
scan.classList.add('active');
|
2017-12-13 22:52:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mobile.ble.onDisconnect = function (res) {
|
|
|
|
console.log(`BLE - Disconnected from ${res}`);
|
|
|
|
mobile.ble.connected = false;
|
|
|
|
mobile.ble.device = {};
|
2017-11-22 19:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mobile.ble.onError = function (err) {
|
|
|
|
alert(JSON.stringify(err));
|
|
|
|
};
|
|
|
|
|
|
|
|
mobile.init = function () {
|
2017-12-13 22:52:25 +00:00
|
|
|
const bleInputs = document.querySelectorAll('.ble')
|
|
|
|
|
|
|
|
window.frame = mobile.frame;
|
|
|
|
window.getState = mobile.getState;
|
|
|
|
window.setDir = mobile.setDir;
|
|
|
|
window.setExposure = mobile.setExposure;
|
|
|
|
window.setDelay = mobile.setDelay;
|
|
|
|
window.setCounter = mobile.setCounter;
|
2017-12-21 04:49:51 +00:00
|
|
|
window.sequence = mobile.sequence;
|
2017-12-13 22:52:25 +00:00
|
|
|
|
|
|
|
//show ble-specific fields in settings
|
|
|
|
for (let i of bleInputs) {
|
|
|
|
i.classList.add('active');
|
|
|
|
}
|
|
|
|
spinnerInit();
|
|
|
|
mobile.ble.scan();
|
|
|
|
};
|
|
|
|
|
|
|
|
mobile.getState = function () {
|
|
|
|
if (!mobile.ble.connected) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
ble.read(mobile.ble.device.id,
|
|
|
|
mobile.ble.SERVICE_ID,
|
|
|
|
mobile.ble.CHAR_ID,
|
|
|
|
mobile.stateSuccess,
|
|
|
|
mobile.ble.onError);
|
|
|
|
};
|
|
|
|
mobile.stateSuccess = function (data) {
|
|
|
|
let str = bytesToString(data);
|
|
|
|
let res = JSON.parse(str);
|
|
|
|
setState(res)
|
|
|
|
};
|
|
|
|
|
|
|
|
mobile.frame = function () {
|
|
|
|
const opts = {
|
|
|
|
type : 'frame'
|
|
|
|
};
|
|
|
|
if (!mobile.ble.connected) {
|
|
|
|
return alert('Not connected to an INTVAL device.');
|
|
|
|
}
|
|
|
|
if (mobile.ble.active) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ble.write(mobile.ble.device.id,
|
|
|
|
mobile.ble.SERVICE_ID,
|
|
|
|
mobile.ble.CHAR_ID,
|
|
|
|
stringToBytes(JSON.stringify(opts)), //check length?
|
|
|
|
mobile.frameSuccess,
|
|
|
|
mobile.ble.onError);
|
|
|
|
document.getElementById('frame').classList.add('focus');
|
|
|
|
mobile.ble.active = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
mobile.frameSuccess = function () {
|
|
|
|
console.log('Frame finished, getting state.');
|
|
|
|
mobile.ble.active = false;
|
|
|
|
document.getElementById('frame').classList.remove('focus');
|
|
|
|
mobile.getState();
|
|
|
|
}
|
|
|
|
mobile.setDir = function () {
|
|
|
|
const opts = {
|
|
|
|
type : 'dir',
|
|
|
|
dir : !document.getElementById('dir').checked
|
|
|
|
};
|
|
|
|
|
|
|
|
ble.write(mobile.ble.device.id,
|
|
|
|
mobile.ble.SERVICE_ID,
|
|
|
|
mobile.ble.CHAR_ID,
|
|
|
|
stringToBytes(JSON.stringify(opts)), //check length?
|
|
|
|
mobile.dirSuccess,
|
|
|
|
mobile.ble.onError);
|
|
|
|
};
|
|
|
|
mobile.dirSuccess = function () {
|
|
|
|
console.log('Set direction');
|
|
|
|
mobile.getState();
|
|
|
|
};
|
|
|
|
mobile.setExposure = function () {
|
|
|
|
let exposure = document.getElementById('exposure').value;
|
|
|
|
let scaledExposure;
|
|
|
|
let opts = {
|
|
|
|
type : 'exposure'
|
|
|
|
};
|
|
|
|
if (exposure === '' || exposure === null) {
|
|
|
|
exposure = 0;
|
|
|
|
}
|
|
|
|
scaledExposure = scaleTime(exposure, STATE.scale);
|
|
|
|
opts.exposure = scaledExposure;
|
|
|
|
ble.write(mobile.ble.device.id,
|
|
|
|
mobile.ble.SERVICE_ID,
|
|
|
|
mobile.ble.CHAR_ID,
|
|
|
|
stringToBytes(JSON.stringify(opts)), //check length?
|
|
|
|
mobile.exposureSuccess,
|
|
|
|
mobile.ble.onError);
|
|
|
|
};
|
|
|
|
mobile.exposureSuccess = function () {
|
|
|
|
console.log('Set exposure');
|
|
|
|
mobile.getState();
|
|
|
|
};
|
|
|
|
|
|
|
|
mobile.setDelay = function () {
|
|
|
|
const delay = document.getElementById('delay').value;
|
|
|
|
const scaledDelay = scaleTime(delay, STATE.delayScale)
|
|
|
|
let opts = {
|
|
|
|
type : 'delay',
|
|
|
|
delay : scaledDelay
|
|
|
|
};
|
|
|
|
ble.write(mobile.ble.device.id,
|
|
|
|
mobile.ble.SERVICE_ID,
|
|
|
|
mobile.ble.CHAR_ID,
|
|
|
|
stringToBytes(JSON.stringify(opts)), //check length?
|
|
|
|
mobile.delaySuccess,
|
|
|
|
mobile.ble.onError);
|
|
|
|
}
|
|
|
|
|
|
|
|
mobile.delaySuccess = function () {
|
|
|
|
console.log('Set delay')
|
|
|
|
mobile.getState();
|
|
|
|
};
|
|
|
|
|
|
|
|
mobile.setCounter = function () {
|
|
|
|
const counter = document.getElementById('counter').value;
|
|
|
|
const change = prompt(`Change counter value?`, counter);
|
|
|
|
if (change === null || !isNumeric(change)) return false;
|
|
|
|
let opts = {
|
|
|
|
type : 'counter',
|
|
|
|
counter : change
|
|
|
|
};
|
|
|
|
ble.write(mobile.ble.device.id,
|
|
|
|
mobile.ble.SERVICE_ID,
|
|
|
|
mobile.ble.CHAR_ID,
|
|
|
|
stringToBytes(JSON.stringify(opts)), //check length?
|
|
|
|
mobile.counterSuccess,
|
|
|
|
mobile.ble.onError);
|
|
|
|
};
|
|
|
|
|
|
|
|
mobile.counterSuccess = function () {
|
|
|
|
console.log('Set counter');
|
|
|
|
mobile.getState();
|
|
|
|
};
|
|
|
|
|
2017-12-21 04:47:37 +00:00
|
|
|
mobile.sequence = function () {
|
|
|
|
const opts = {
|
|
|
|
type : 'sequence'
|
|
|
|
};
|
|
|
|
if (!mobile.ble.connected) {
|
|
|
|
return alert('Not connected to an INTVAL device.');
|
|
|
|
}
|
|
|
|
ble.write(mobile.ble.device.id,
|
|
|
|
mobile.ble.SERVICE_ID,
|
|
|
|
mobile.ble.CHAR_ID,
|
|
|
|
stringToBytes(JSON.stringify(opts)), //check length?
|
|
|
|
mobile.frameSuccess,
|
|
|
|
mobile.ble.onError);
|
|
|
|
document.getElementById('seq').classList.add('focus');
|
|
|
|
mobile.ble.active = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
mobile.sequenceSuccess = function () {
|
|
|
|
console.log('Sequence state changed');
|
|
|
|
mobile.getState();
|
|
|
|
setTimeout(() => {
|
|
|
|
if (STATE.sequence) {
|
|
|
|
mobile.ble.active = true;
|
|
|
|
} else {
|
|
|
|
mobile.ble.active = false;
|
|
|
|
}
|
|
|
|
}, 20);
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:52:25 +00:00
|
|
|
function bytesToString (buffer) {
|
|
|
|
return String.fromCharCode.apply(null, new Uint8Array(buffer));
|
|
|
|
};
|
|
|
|
function stringToBytes(string) {
|
|
|
|
var array = new Uint8Array(string.length);
|
|
|
|
for (var i = 0, l = string.length; i < l; i++) {
|
|
|
|
array[i] = string.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return array.buffer;
|
|
|
|
};
|