OpenROV /
openrov-cockpit
| 1 | //This is the support file for the /views/index.ejs |
||
| 2 | $(function() { |
||
| 3 | |||
| 4 | //http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript |
||
| 5 | function getParameterByName(name, url) { |
||
| 6 | if (!url) |
||
| 7 | url = window.location.href; |
||
| 8 | name = name.replace(/[\[\]]/g, '\\$&'); |
||
| 9 | var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), |
||
| 10 | results = regex.exec(url); |
||
| 11 | if (!results) |
||
| 12 | return null; |
||
| 13 | if (!results[2]) |
||
| 14 | return ''; |
||
| 15 | return decodeURIComponent(results[2].replace(/\+/g, ' ')); |
||
| 16 | } |
||
| 17 | |||
| 18 | function setGetParameter(paramName, paramValue) { |
||
| 19 | var url = window.location.href; |
||
| 20 | var hash = location.hash; |
||
| 21 | url = url.replace(hash, ''); |
||
| 22 | if (url.indexOf(paramName + '=') >= 0) { |
||
| 23 | var prefix = url.substring(0, url.indexOf(paramName)); |
||
| 24 | var suffix = url.substring(url.indexOf(paramName)); |
||
| 25 | suffix = suffix.substring(suffix.indexOf('=') + 1); |
||
| 26 | suffix = suffix.indexOf('&') >= 0 ? suffix.substring(suffix.indexOf('&')) : ''; |
||
| 27 | if (paramValue == null) { |
||
|
0 ignored issues
–
show
|
|||
| 28 | url = prefix + suffix; |
||
| 29 | } else { |
||
| 30 | url = prefix + paramName + '=' + paramValue + suffix; |
||
| 31 | } |
||
| 32 | } else { |
||
| 33 | if (url.indexOf('?') < 0) |
||
| 34 | url += '?' + paramName + '=' + paramValue; |
||
| 35 | else |
||
| 36 | url += '&' + paramName + '=' + paramValue; |
||
| 37 | } |
||
| 38 | window.location.href = url + hash; |
||
| 39 | } |
||
| 40 | var force = getParameterByName('force'); |
||
| 41 | //resets the pilot position reservation |
||
| 42 | force = force == null ? false : true; |
||
|
0 ignored issues
–
show
It is recommended to use
=== to compare with null.
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator. Loading history...
|
|||
| 43 | var mc = getParameterByName('mc'); |
||
| 44 | //forces going to mission control |
||
| 45 | var replay = getParameterByName('rp'); |
||
| 46 | var e = new EventEmitter2(); |
||
| 47 | e.ThisIsTheOne = true; |
||
| 48 | var blacklist = ['CameraRegistration']; |
||
| 49 | var lvcCacheJSON = localStorage.getItem('lvc_cache'); |
||
| 50 | var cacheSeed = {}; |
||
| 51 | if (lvcCacheJSON !== null) { |
||
| 52 | var lvcCache = JSON.parse(lvcCacheJSON); |
||
| 53 | var lvcArray = Object.keys(lvcCache); |
||
| 54 | lvcArray.forEach(function(key) { |
||
| 55 | if (blacklist.includes(key)) { |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | var parms = lvcCache[key]; |
||
| 59 | // parms.unshift(key); |
||
| 60 | cacheSeed[key] = { |
||
| 61 | context: this, |
||
| 62 | args: parms |
||
| 63 | }; |
||
| 64 | // console.log('seeding event:', key); |
||
| 65 | }); |
||
| 66 | } |
||
| 67 | e = new window.EventEmiiterStoreAndForward(e, cacheSeed); |
||
| 68 | var cockpit = new Cockpit(e); |
||
| 69 | var template = $('#t')[0]; |
||
| 70 | //template.cockpitEventEmitter = cockpit; |
||
| 71 | window.cockpit = cockpit; |
||
| 72 | template.rovOnline = false; |
||
| 73 | if (mc !== null) { |
||
| 74 | loadScript('js/missioncontrol.js'); |
||
| 75 | //$.getScript('js/missioncontrol.js'); |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | if (replay !== null) { |
||
| 79 | loadScript('js/replay.js'); |
||
| 80 | //$.getScript('js/missioncontrol.js'); |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | var tokenOption = force == false ? localStorage.sessionID : 'reset'; |
||
|
0 ignored issues
–
show
It is recommended to use
=== to compare with false.
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator. Loading history...
|
|||
| 84 | |||
| 85 | var appHasStarted = false; |
||
| 86 | var handlePageLoaded = function() { |
||
| 87 | if (appHasStarted){ |
||
| 88 | return; //Guard from multiple calls |
||
| 89 | } |
||
| 90 | appHasStarted = true; |
||
| 91 | var socket = window.io.connect(window.location.protocol + '//' + window.location.hostname + ':' + window.location.port, { |
||
| 92 | path: '/cockpitsocket', |
||
| 93 | query: 'token=' + tokenOption |
||
| 94 | }); |
||
| 95 | |||
| 96 | socket = new window.SocketIOStoreAndForward(socket); |
||
| 97 | // socket=new window.SocketIOEmitter(socket); |
||
| 98 | socket.on('error', function(error) { |
||
| 99 | if (error == 'Authentication error') { |
||
| 100 | console.log('Someone else already logged in to rov Pilot slot, redirecting to mission control'); |
||
| 101 | setGetParameter('mc', true); |
||
| 102 | loadScript('js/missioncontrol.js'); |
||
| 103 | return; |
||
| 104 | } |
||
| 105 | }); |
||
| 106 | socket.on('forced-disconnect', function() { |
||
| 107 | window.location.reload(); |
||
| 108 | }); |
||
| 109 | var CacheLVC = function(lvcdumpfn, millseconds) { |
||
| 110 | var cache = lvcdumpfn(); |
||
| 111 | localStorage.setItem('lvc_cache', JSON.stringify(cache)); |
||
| 112 | setTimeout(CacheLVC.bind(this, lvcdumpfn, millseconds), millseconds); |
||
| 113 | }; |
||
| 114 | //plugin hooks |
||
| 115 | var bridge = null; |
||
| 116 | socket.on('connect', function() { |
||
| 117 | console.log('connection or reconnection to ROV established'); |
||
| 118 | if (bridge !== null) { |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | //Only do the code below once |
||
| 122 | socket.emit('request-sessionToken', function(sessionID) { |
||
| 123 | if (bridge !== null) { |
||
| 124 | alert('This should not be called'); |
||
| 125 | } |
||
| 126 | localStorage.setItem('sessionID', sessionID); |
||
| 127 | bridge = new window.SocketIOtoEmitterBridge(socket, window.cockpit.rov); |
||
| 128 | $('#t')[0].rovOnline = true; |
||
| 129 | $('#t')[0].userRole = 'Pilot'; |
||
| 130 | window.cockpit.rov.connection = 'socket.io'; |
||
| 131 | var lvcCache = {}; |
||
| 132 | if (lvcCacheJSON !== null) { |
||
| 133 | lvcCache = JSON.parse(lvcCacheJSON); |
||
| 134 | } |
||
| 135 | //if they forced the session connect, we need to remove the parameter from the URL |
||
| 136 | if (force) { |
||
| 137 | setGetParameter('force', null); |
||
| 138 | } |
||
| 139 | socket.lvcCache = lvcCache; |
||
| 140 | //TESTING: Danger, if the serialization takes to long it might look like a hitch to performance every 3 minutes |
||
| 141 | CacheLVC(function() { |
||
| 142 | return lvcCache; |
||
| 143 | }, 1000 * 10 * 3); |
||
| 144 | }); |
||
| 145 | }); |
||
| 146 | }; |
||
| 147 | |||
| 148 | window.OROV.startApp = handlePageLoaded; |
||
| 149 | |||
| 150 | }); |
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.
Read more about comparison operations.