/**
* ReachView code is placed under the GPL license.
* Written by Egor Fedorov (egor.fedorov@emlid.com) and Danil Kramorov (danil.kramorov@emlid.com)
* Copyright (c) 2015-2018, Emlid Limited
* All rights reserved.
*
* If you are interested in using ReachView code as a part of a
* closed source project, please contact Emlid Limited (info@emlid.com).
*
* This file is part of ReachView.
*
* ReachView is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ReachView is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReachView. If not, see .
*/
$(document).ready(function () {
requirejs(['socket', 'bootstrap_select'], function (io, bootstrap_select) {
var lostConnectionNoty;
var addConnectionNoty;
var removeConnetctionNoty;
var emptyRequiredModal = false;
var incorrectSymbolsNumber = false;
var incorrectSymbols = false;
var syncInterval;
var new_network = {};
var to_append = '';
var testPass = false;
var wifiPass = false;
var timePass = false;
var receiverPass = false;
var updatePass = false;
var currentVersion = false;
var availableReceiverVersion = false;
var availableVersion = false;
var opkgResult = false;
var receiverLocked = false;
var device = '';
var preventLostConnectionNoty = false;
var disconnect_msg = 'Lost connection with Reach. Please check your network, then try refreshing the page.';
$('.bootstrap-select').selectpicker();
$('.styled, .multiselect-container input').uniform({ radioClass: 'choice' });
$(document).on('click', '#create_new_network', function () {
$(this).parents('.modal-content').find('.required_field:visible').each(function () {
if ($.trim($(this).val()) === '') {
emptyRequiredModal = true;
return false;
}
});
if ($('#new_network_pass:visible').length !== 0) {
if ($('#new_network_pass').val().length < 8) {
incorrectSymbolsNumber = true;
} else {
incorrectSymbolsNumber = false;
}
} else {
incorrectSymbolsNumber = false;
}
if (emptyRequiredModal) {
$(this).parents('.modal-content').find('.required_field:visible')
.filter(function () { return $.trim($(this).val()) === ''; }).css('border', '1px solid red');
$(this).parents('.modal-content').find('.required_field')
.filter(function () { return $.trim($(this).val()) !== ''; }).css('border', '1px solid #ddd');
emptyRequiredModal = false;
} else {
$(this).parents('.modal-content').find('.required_field').css('border', '1px solid #ddd');
if (incorrectSymbols) {
$('.modal_add_warning').text('Incorrect symbols. Use only a-z, 1-9 characters.');
} else if (incorrectSymbolsNumber) {
$('.modal_add_warning').text('Password should contain at least 8 characters');
} else {
new_network['ssid'] = $('#new_network_name').val();
new_network['password'] = $('#new_network_pass').val();
new_network['security'] = $('#security_select').val();
new_network['identity'] = $('#new_network_identity').val();
$('#modal_network').modal('hide');
socket.emit('add new network', new_network);
}
}
});
$(document).on('change', '#security_select', function () {
$('#new_network_identity').removeClass('required_field');
$('#new_network_identity').parents('.form-group').css('display', 'none');
$('#new_network_pass').addClass('required_field');
$('#new_network_pass').parents('.form-group').css('display', 'block');
$('#uniform-show_pass').parents('.form-group').css('display', 'block');
if ($(this).val() === 'open') {
$('#new_network_pass').removeClass('required_field');
$('#new_network_pass').parents('.form-group').css('display', 'none');
$('#uniform-show_pass').parents('.form-group').css('display', 'none');
} else if ($(this).val() == 'wpaeap') {
$('#new_network_identity').addClass('required_field');
$('#new_network_identity').parents('.form-group').css('display', 'block');
}
});
$('#modal_network').on('show.bs.modal', function () {
$('#new_network_name').val('');
$('#new_network_pass').val('');
$('#new_network_pass').attr('type', 'password');
$('#new_network_identity').val('');
$('#security_select').val('wpa-psk');
$('#security_select').selectpicker('refresh');
$('#show_pass').attr('checked', false);
$('#show_pass').parent().removeClass('checked');
$('.modal_add_warning').text('');
$('#security_select').change();
$('#modal_network input').css('border', '1px solid #ddd');
});
$(document).on('change', '#show_pass', function () {
if ($(this).is(':checked')) {
$('#new_network_pass').attr('type', 'text');
} else {
$('#new_network_pass').attr('type', 'password');
}
});
$(document).on('click', '#added_wi-fi li', function () {
if (!$(this).hasClass('connected_wi-fi_network')) {
$('#modal_saved_connect .network_title').text($(this).find('.wi-fi_title').text());
$('#modal_saved_connect .network_mac').text('Security: ' + $(this).find('.wi-fi_security').val());
$('#modal_saved_connect').modal('show');
}
return false;
});
$(document).on('click', '#connected_wi-fi li', function () {
return false;
});
$(document).on('click', '#connect_network', function () {
var ssid_to_connect = $('#modal_saved_connect .network_title').text();
disconnect_msg = 'Reach is connecting to another network. Switch to ' + ssid_to_connect + ' to continue.';
socket.emit('connect to network', ssid_to_connect);
$('#modal_saved_connect').modal('hide');
return false;
});
$(document).on('click', '#forget_network', function () {
var ssid_to_remove = $('#modal_saved_connect .network_title').text();
socket.emit('remove network', ssid_to_remove);
$('#modal_saved_connect').modal('hide');
return false;
});
$('.update_reachview').on('click', function () {
if (!$(this).hasClass('disabled')) {
receiverPass = true;
socket.emit('upgrade reachview');
$('.current_version').text('Updating...');
$('.update_status').html('');
$('.row_try_skip').css('display', 'none');
$('.skip_update_btn').css('display', 'none');
$('.update_reachview').addClass('disabled');
}
return false;
});
$('.skip_update, .skip_update_btn').on('click', function () {
receiverPass = true;
socket.emit('skip update');
$('.update_status').html('');
$('.update_anchor:not(.collapsed)').click();
$('.row_try_skip').css('display', 'none');
$('.skip_update_btn').css('display', 'none');
updatePass = true;
if (updatePass && receiverPass && timePass && wifiPass && testPass) {
$('.to_app').removeClass('disabled');
} else {
$('.to_app').addClass('disabled');
}
return false;
});
$('.try_again').on('click', function () {
receiverPass = true;
$('.row_try_skip').css('display', 'none');
currentVersion = false;
socket.emit('get reachview version');
$('.current_version').text('Getting current version...');
$('.update_status').html('');
return false;
});
$('.to_app').on('click', function () {
if (!$(this).hasClass('disabled')) {
disconnect_msg = 'Reach is rebooting and has been disconnected from this network.';
noty({
width: 200,
text: 'Reboot will start in 3...',
type: 'information',
dismissQueue: true,
timeout: 3000,
closeWith: false,
layout: 'topRight',
callback: {
onClose: function () {
if (device === 'ReachM+' || device === 'ReachRS+') {
preventLostConnectionNoty = true;
$('#modal_mender_guide').modal({ backdrop: 'static', keyboard: false });
}
socket.emit('reboot now');
}
}
});
setTimeout(function () { $('.noty_text').text('Reboot will start in 2...'); }, 1000);
setTimeout(function () { $('.noty_text').text('Reboot will start in 1...'); }, 2000);
}
return false;
});
// SocketIO namespace:
socket = io();
socket.on('add network results', function (msg) {
if (msg) {
socket.emit('get saved wifi networks');
} else {
addConnectionNoty = noty({
width: 200,
text: 'Failed to add a new Wi-Fi connection',
type: 'error',
dismissQueue: true,
timeout: 4000,
closeWith: ['click'],
layout: 'topRight'
});
}
});
socket.on('remove network results', function (msg) {
if (msg) {
socket.emit('get saved wifi networks');
} else {
removeConnetctionNoty = noty({
width: 200,
text: 'Failed to remove network',
type: 'error',
dismissQueue: true,
timeout: 4000,
closeWith: ['click'],
layout: 'topRight'
});
}
});
// say hello on connect
socket.on('connect', function () {
socket.emit('browser connected', {data: 'I\'m connected'});
if (typeof lostConnectionNoty !== 'undefined') {
lostConnectionNoty.close();
}
});
socket.on('reconnect', function () {
$('.disconnect_overlay').fadeOut();
$('body').css('position', 'relative');
$(window).resize();
});
socket.on('disconnect', function () {
if (preventLostConnectionNoty) {
return;
}
$('.disconnect_overlay').fadeIn();
$('body').css('position', 'fixed');
lostConnectionNoty = noty({
width: 200,
text: disconnect_msg,
type: 'error',
dismissQueue: true,
timeout: false,
closeWith: false,
layout: 'topRight',
callback: {
onClose: function () {
$('.disconnect_overlay').fadeOut();
$('body').css('position', 'relative');
noty({
width: 200,
text: 'Reach reconnected!',
type: 'success',
dismissQueue: true,
closeWith: false,
timeout: 3000,
layout: 'topRight'
});
}
}
});
});
socket.on('connect_error', function () {
console.clear();
console.warn('Lost connection with sockets');
});
socket.emit('get test results');
socket.on('reachview upgrade status', function (msg) {
if (!receiverPass) {
return;
}
$('.row_try_skip').css('display', 'none');
$('.skip_update_btn').css('display', 'none');
if (!$('.update_status i').hasClass('icon-spinner2')) {
$('.update_status').html('');
}
$('.available_version').css('display', 'none');
$('.update_reachview').addClass('disabled');
$('.coords_progress').css('display', 'block');
if (msg['active']) {
$('.current_version').text(msg['state'] + ' ' + msg['package'] + ' (' + msg['version'] + ')');
}
if (msg['state'] === 'Downloading') {
$('.coords_progress .progress-bar').removeClass('progress-bar-striped active');
$('.coords_progress .progress-bar').css('width', msg['percentage'] + '%');
if (parseInt(msg['percentage']) > 30) {
$('.coords_progress .progress-bar span').text(msg['percentage'] + '%');
} else {
$('.coords_progress .progress-bar span').text('');
}
} else if (msg['state'] === 'Installing') {
$('.coords_progress .progress-bar').css('width', '100%');
$('.coords_progress .progress-bar span').text('');
$('.coords_progress .progress-bar').addClass('progress-bar-striped active');
} else if (msg['state'] === 'Finished') {
$('.coords_progress').css('display', 'none');
currentVersion = false;
socket.emit('get reachview version');
$('.update_status').html('');
$('.update_reachview').css('display', 'none');
$('.available_version').css('display', 'none');
updatePass = true;
if (updatePass && receiverPass && timePass && wifiPass && testPass) {
$('.to_app').removeClass('disabled');
} else {
$('.to_app').addClass('disabled');
}
} else if (msg['state'] === 'Failed') {
$('.coords_progress').css('display', 'none');
$('.update_status').html('');
$('.coords_progress .progress-bar').css('width', '0%');
$('.coords_progress .progress-bar span').text('');
if (msg['locked']) {
$('.current_version').html('Update system is used by another process. Please try again later.');
} else {
$('.current_version').html('Failed to perform update.');
}
$('.skip_update_btn').css('display', 'inline-block');
$('.update_reachview').css('display', 'block');
$('.update_reachview').removeClass('disabled');
$('.update_anchor.collapsed').click();
}
});
socket.on('wifi saved networks results', function (msg) {
var connectedNetwork = false;
var to_append = '';
msg.forEach(function (key, value) {
var ssid = (key['ssid'] !== '') ? key['ssid'] : 'Unknown';
if (key['is_connected']) {
to_append += '
';
$('#current_network').text(ssid);
} else {
to_append += '';
}
to_append += '';
to_append += '
' + ssid + '
';
if (key['is_connected']) {
to_append += '
Connected (' + key['ip'] + ')';
connectedNetwork = true;
} else {
to_append += '
Saved';
}
to_append += '
';
if (key['is_connected']) {
to_append += '
';
}
to_append += '';
});
$('#added_wi-fi').html(to_append);
$('#connected_wi-fi').html('');
$('#connected_wi-fi').append($('.connected_wi-fi_network'));
if (connectedNetwork) {
$('.wifi_status').html('');
socket.emit('get time sync status');
$('.overlay.sync_overlay').fadeOut();
$('.sync_status').html('');
syncInterval = setInterval(function () { socket.emit('get time sync status'); }, 1000);
wifiPass = true;
} else {
$('.wifi_status').html('');
$('.wi-fi_anchor.collapsed').click();
wifiPass = false;
}
});
socket.on('time sync status', function (msg) {
if (timePass) {
return;
}
if (msg['status']) {
$('.sync_status').html('');
$('.time_sync_warning').text('Time was synchronized!');
// Receiver update
availableReceiverVersion = false;
socket.emit('is receiver upgrade available');
$('.receiver_status').html('');
$('#receiver_upgrade_msg').text('Checking for receiver updates...');
$('.receiver_overlay').fadeOut();
// Reach update
// currentVersion = false;
// socket.emit('get reachview version');
// $('.current_version').text('Getting current version...');
// $('.overlay.update_overlay').fadeOut();
// $('.update_status').html('');
clearInterval(syncInterval);
timePass = true;
} else {
$('.time_sync_warning').text('Check your internet connection or connect antenna.');
timePass = false;
}
});
socket.on('receiver upgrade available', function (msg) {
if (availableReceiverVersion || receiverLocked) {
return;
}
if (msg['running']) {
$('#receiver_upgrade_msg').text('Updating receiver...');
$('.receiver_status').html('');
$('.update_receiver').css('display', 'inline-block');
$('.update_receiver').addClass('disabled');
$('.skip_receiver_update').css('display', 'none');
$('.receiver_progress').css('display', 'block');
$('.receiver_anchor.collapsed').click();
return;
}
if (msg['available']) {
$('.receiver_status').html('');
$('#receiver_upgrade_msg').text('Receiver upgrade available');
$('.update_receiver').css('display', 'inline-block');
$('.skip_receiver_update').css('display', 'inline-block');
$('.receiver_anchor.collapsed').click();
} else {
$('.receiver_status').html('');
$('#receiver_upgrade_msg').text('No upgrades available for receiver');
$('.skip_receiver_update').trigger('click', [true]);
}
availableReceiverVersion = true;
});
$('.update_receiver').on('click', function () {
if (!$(this).hasClass('disabled')) {
receiverPass = false;
socket.emit('upgrade receiver');
$('#receiver_upgrade_msg').text('Updating receiver...');
$('.receiver_status').html('');
$('.update_receiver').addClass('disabled');
$('.skip_receiver_update').css('display', 'none');
$('.receiver_progress').css('display', 'block');
}
return false;
});
$('.skip_receiver_update, .receiver_skip_locked').on('click', function (event, fakeClick) {
if (!$(this).hasClass('disabled')) {
$(this).css('display', 'none');
$('.receiver_status').html('');
if (!fakeClick) {
$('.receiver_anchor:not(.collapsed)').click();
}
if (!availableVersion && !updatePass) {
currentVersion = false;
socket.emit('get reachview version');
$('.current_version').text('Getting current version...');
$('.overlay.update_overlay').fadeOut();
$('.update_status').html('');
}
receiverLocked = false;
receiverPass = true;
}
return false;
});
$('.receiver_try_again').on('click', function () {
receiverPass = false;
availableReceiverVersion = false;
socket.emit('is receiver upgrade available');
$('.receiver_status').html('');
$('#receiver_upgrade_msg').text('Checking for receiver updates...');
$('.receiver_try_skip').css('display', 'none');
receiverLocked = false;
return false;
});
socket.on('receiver upgrade result', function (msg) {
if (receiverPass || receiverLocked) {
return;
}
if (msg) {
$('.update_receiver').css('display', 'none');
$('#receiver_upgrade_msg').text('Receiver updated successfully');
$('.receiver_status').html('');
$('.skip_receiver_update').trigger('click', [true]);
} else {
$('#receiver_upgrade_msg').text('Failed to perform receiver update');
$('.receiver_status').html('');
$('.update_receiver').css('display', 'inline-block');
$('.skip_receiver_update').css('display', 'inline-block');
$('.receiver_anchor.collapsed').click();
$('.update_receiver').removeClass('disabled');
}
$('.receiver_progress').css('display', 'none');
});
socket.on('opkg update result', function (msg) {
if (opkgResult) {
return;
}
if (msg['state'] === 'Failed') {
$('.update_status').html('');
$('.update_anchor.collapsed').click();
if (msg['locked']) {
$('.current_version').html('Update system is used by another process. Please try again later.');
} else {
$('.current_version').html('Update server unreachable. Check your Internet connection or try again later.');
}
$('.available_version').css('display', 'none');
$('.row_try_skip').css('display', 'block');
$('.update_reachview').css('display', 'none');
$('.update_anchor.collapsed').click();
}
else if (msg['state'] === 'Finished') {
opkgResult = true;
availableVersion = false;
socket.emit('is reachview upgrade available');
}
});
socket.on('update system locked', function () {
if (receiverPass) {
$('.update_anchor.collapsed').click();
$('.update_status').html('');
$('.current_version').text('Update system is used by another process. Please try again later.');
$('.available_version').css('display', 'none');
$('.row_try_skip').css('display', 'block');
} else {
$('.receiver_anchor.collapsed').click();
$('.receiver_status').html('');
$('#receiver_upgrade_msg').text('Update system is used by another process. Please try again later.');
$('.update_receiver').css('display', 'none');
$('.skip_receiver_update').css('display', 'none');
$('.receiver_try_skip').css('display', 'block');
receiverLocked = true;
}
});
socket.on('current reachview version', function (msg) {
if (currentVersion || !receiverPass) {
return;
}
var version = (msg['version'] != null)
? 'Current ReachView version: ' + msg['version']
: 'Could not retrieve ReachView version.';
$('.current_version').text(version);
currentVersion = true;
opkgResult = false;
socket.emit('update');
$('.available_version').css('display', 'block');
$('.available_version').text('Checking for updates...');
});
socket.on('reachview upgrade version', function (msg) {
if (availableVersion || !receiverPass) {
return;
}
if (!msg['upgrade available']) {
$('.update_status').html('');
$('.update_reachview').css('display', 'none');
$('.available_version').css('display', 'none');
updatePass = true;
if (updatePass && receiverPass && timePass && wifiPass && testPass) {
$('.to_app').removeClass('disabled');
} else {
$('.to_app').addClass('disabled');
}
} else {
$('.available_version').css('display', 'block');
$('.available_version').text('Available version: ' + msg['available version']);
$('.update_status').html('');
$('.update_reachview').css('display', 'inline-block');
$('.update_anchor.collapsed').click();
}
availableVersion = true;
});
socket.on('test results', function (msg) {
$('.tests_status').css('display', 'none');
device = msg['device'];
if (msg['device'] === 'Reach' || msg['device'] === 'ReachM+') {
$('.ltc_test, .stc_test, .lora_test').parent().css('display', 'none');
}
if (!msg['ltc']) {
$('.tests_status').html('');
$('.test_warning').text('Test 3 failed');
$('.test_warning').slideDown();
testPass = true;
}
if (!msg['stc']) {
$('.tests_status').html('');
$('.test_warning').text('Test 4 failed');
$('.test_warning').slideDown();
testPass = true;
}
if (!msg['lora']) {
$('.tests_status').html('');
$('.test_warning').text('Test 5 failed');
$('.test_warning').slideDown();
testPass = true;
}
if (!msg['mpu']) {
$('.tests_status').html('');
$('.test_warning').text('Test 1 failed');
$('.test_warning').slideDown();
testPass = true;
}
if (!msg['u-blox']) {
$('.tests_status').html('');
$('.test_warning').text('Test 2 failed');
$('.test_warning').slideDown();
testPass = false;
}
if (msg['device'] === 'Reach' || msg['device'] === 'ReachM+') {
if (msg['u-blox'] && msg['mpu']) {
$('.tests_status').html('');
testPass = true;
}
} else {
if (msg['u-blox'] && msg['mpu'] && msg['lora'] && msg['ltc'] && msg['stc']) {
$('.tests_status').html('');
testPass = true;
}
}
$('.tests_status').fadeIn();
if (msg['mpu']) {
$('.mpu_test').html('');
} else {
$('.mpu_test').html('');
}
if (msg['u-blox']) {
$('.u-blox_test').html('');
} else {
$('.u-blox_test').html('');
}
if (msg['lora']) {
$('.lora_test').html('');
} else {
$('.lora_test').html('');
}
if (msg['stc']) {
$('.stc_test').html('');
} else {
$('.stc_test').html('');
}
if (msg['ltc']) {
$('.ltc_test').html('');
} else {
$('.ltc_test').html('');
}
socket.emit('get saved wifi networks');
});
});
});