Push dev work to master #2

Merged
sixteenmillimeter merged 416 commits from dev into master 2018-07-19 15:29:02 +00:00
4 changed files with 133 additions and 5 deletions
Showing only changes of commit 7d5f6c6601 - Show all commits

View File

@ -76,12 +76,15 @@
<div class="ble"> <div class="ble">
<h2>WIFI</h2> <h2>WIFI</h2>
<div> <div>
<input type="text" id="ssid" placeholder="Wifi SSID" /> <select id="available" class="" onchange="mobile.editWifi();">
<option>N/A</option>
</select>
</div> </div>
<div> <div>
<input type="password" id="password" placeholder="Wifi Password" /> <input type="password" id="password" class="" placeholder="Wifi Password" />
</div> </div>
<button id="wifi" onclick="alert('WIFI');">CONNECT</button> <button id="wifi" class="" onclick="mobile.setWifi();">CONNECT</button>
<button id="wifiRefresh" class="" onclick="mobile.getWifi();">REFRESH WIFI</button>
</div> </div>
</div> </div>
<div id="mscript" class="page"> <div id="mscript" class="page">

View File

@ -294,3 +294,13 @@ footer > div.selected{
#scan.active{ #scan.active{
display: block; display: block;
} }
#available.active{
border-color: #20ce45;;
}
#password,#wifi{
display: none;
}
#password.active,#wifi.active{
display: block;
}

View File

@ -181,6 +181,8 @@ var setState = function (res) {
STATE.sequence = true; STATE.sequence = true;
if (mobile.ble) mobile.ble.active = true; if (mobile.ble) mobile.ble.active = true;
seqState(true); seqState(true);
} else {
seqState(false);
} }
}; };

View File

@ -13,6 +13,11 @@ mobile.ble = {
active : false active : false
}; };
mobile.wifi = {
current : 'null',
available : []
};
mobile.ble.scan = function () { mobile.ble.scan = function () {
spinnerShow(); spinnerShow();
ble.scan([], 5, mobile.ble.onDiscover, mobile.ble.onError); ble.scan([], 5, mobile.ble.onDiscover, mobile.ble.onError);
@ -69,6 +74,7 @@ mobile.ble.onConnect = function (peripheral, device) {
scan.classList.remove('active'); scan.classList.remove('active');
getState(); getState();
mobile.getWifi();
}; };
mobile.ble.disconnect = function () { mobile.ble.disconnect = function () {
@ -266,18 +272,125 @@ mobile.sequence = function () {
mobile.ble.active = true; mobile.ble.active = true;
}; };
mobile.sequenceSuccess = function () { mobile.sequenceSuccess = function () {
console.log('Sequence state changed'); console.log('Sequence state changed');
mobile.getState(); mobile.getState();
setTimeout(() => { setTimeout(() => {
if (STATE.sequence) { if (STATE.sequence) {
mobile.ble.active = true; mobile.ble.active = true;
seqState(true);
} else { } else {
mobile.ble.active = false; mobile.ble.active = false;
seqState(false);
} }
}, 20); }, 20);
} };
//retreive object with list of available Wifi APs,
//and state of current connection, if available
mobile.getWifi = function () {
spinnerShow();
ble.read(mobile.ble.device.id,
mobile.ble.SERVICE_ID,
mobile.ble.WIFI_ID,
mobile.getWifiSuccess,
mobile.ble.onError);
};
mobile.getWifiSuccess = function (data) {
const elem = document.getElementById('available');
const wifi = document.getElementById('wifi');
const password = document.getElementById('password');
let option = document.createElement('option');
let str = bytesToString(data);
let res = JSON.parse(str);
spinnerHide();
elem.innerHTML = ''
if (!res.available || res.available.length === 0) {
if (elem.classList.contains('active')) {
elem.classList.remove('active');
}
option.text = 'N/A'
elem.add(option);
elem.value = '';
} else {
for (let ap of res.available) {
option = document.createElement('option');
option.text = ap;
option.value = ap;
elem.add(option);
}
if (res.current && res.available.indexOf(res.current) !== -1) {
elem.value = res.current
if (!elem.classList.contains('active')) {
elem.classList.add('active');
}
if (wifi.classList.contains('active')) {
wifi.classList.remove('active');
}
if (password.classList.contains('active')) {
password.classList.remove('active');
}
} else {
if (!wifi.classList.contains('active')) {
wifi.classList.add('active');
}
if (!password.classList.contains('active')) {
password.classList.add('active');
}
}
}
mobile.wifi.current = res.current;
mobile.wifi.available = res.available;
};
mobile.editWifi = function () {
const available = document.getElementById('available');
const wifi = document.getElementById('wifi');
const password = document.getElementById('password');
if (!wifi.classList.contains('active')) {
wifi.classList.add('active');
}
if (!password.classList.contains('active')) {
password.classList.add('active');
}
password.focus();
if (available.value !== mobile.wifi.current && available.classList.contains('active')) {
available.classList.remove('active');
}
};
mobile.setWifi = function () {
const ssid = document.getElementById('available').value;
const pwd = document.getElementById('password').value;
const opts = {
ssid : ssid,
pwd : pwd
}
spinnerShow();
if (ssid === '' || ssid === null || ssid === undefined) {
return alert('Cannot set wireless credentials with a blank SSID');
}
if (pwd === '' || pwd === null || pwd === undefined) {
return alert('Cannot set wireless credentials with a blank passphrase');
}
if (pwd.length < 8 || pwd.length > 63) {
return alert('Passphrase must be 8..63 characters');
}
ble.write(mobile.ble.device.id,
mobile.ble.SERVICE_ID,
mobile.ble.WIFI_ID,
stringToBytes(JSON.stringify(opts)),
mobile.setWifiSuccess,
mobile.ble.onError);
};
mobile.setWifiSuccess = function () {
spinnerHide();
console.log('Set new wifi credentials');
setTimeout(mobile.getWifi, 100);
};
function bytesToString (buffer) { function bytesToString (buffer) {
return String.fromCharCode.apply(null, new Uint8Array(buffer)); return String.fromCharCode.apply(null, new Uint8Array(buffer));