var ws = new ReconnectingWebSocket( window.location.origin.replace('http', 'ws') +'/ws', null, { debug: false, reconnectInterval: 3000 } ); var seeme = true; var tl ; var error_audio = new Audio('siren.wav'); var playSoundOnError = true; var checkbox_sound = document.getElementById('play_sound_on_error') if(checkbox_sound != null) { checkbox_sound.addEventListener('change', (event) => { if (event.target.checked) { playSoundOnError = true; } else { playSoundOnError = false; } }) } //request close before unloading window.addEventListener('beforeunload', function(){ ws.close(); }); ws.addEventListener( 'close', function( e ) { console.log( 'Closed connection' ); } ); class Timeline{ constructor(ws, el, nr) { this.ws = ws; this.el = el; this.count = nr; this.eventDataSet = new vis.DataSet([ {content: '.', start: new Date(), type: 'point', group: 1} ]); // console.log('init timeline'); this.lightMap = {}; let groups = []; for(let hid = 1; hid<=this.count; hid++) { groups.push({id: parseInt(hid), content: 'Hugvey #'+hid}); this.eventDataSet.add({content: 'initiate', start: new Date(), type: 'point', group: parseInt(hid)}) } this.dataGroups = new vis.DataSet(groups); let options = { maxHeight: '100%', height: '100%', margin : { item: { horizontal: 0, vertical: 5 } }, zoomMax: 3600000 // 1 h // 'rollingMode': {'follow': true, 'offset': .8 } }; // console.log('groups', this.dataGroups, groups, options); this.timeline = new vis.Timeline(this.el, this.eventDataSet, this.dataGroups, options); let tl = this.timeline; let startDate = new Date(); startDate.setMinutes(startDate.getMinutes()-1); let endDate = new Date(); endDate.setMinutes(endDate.getMinutes()+20); //follow the timeline or not if checkbox is checked setTimeout(function(){ tl.setWindow(startDate, endDate); }, 500); this.moveInterval = setInterval(function(){ // skip movement if not visible tl.moveTo(new Date()); }, 1000); var checkbox = document.getElementById('follow_checkbox') checkbox.addEventListener('change', (event) => { if (event.target.checked) { this.moveInterval = setInterval(function(){ // skip movement if not visible tl.moveTo(new Date()); }, 1000); } else { clearInterval(this.moveInterval) } }); ws.addEventListener( 'message', this); } handleEvent(e) { // console.log('handle it', e, this); if(e.type == 'message') { this.wsOnMessage(e) } } wsOnMessage(e) { let msg = JSON.parse( e.data ); if ( typeof msg['action'] === 'undefined' ) { console.error( "not a valid message: " + e.data ); return; } if(msg['action'] == 'status') { // reconstruct mapping of lights to hugveys this.lightMap = {} for(let hv of msg['hugveys']){ if(!this.lightMap.hasOwnProperty(hv['light_id'])) { this.lightMap[hv['light_id']] = []; } this.lightMap[hv['light_id']].push(hv['id']); } // loop over statusses of hugveys and show them at the appropriate lights for(let hv of msg['hugveys']){ // console.log(hv['language'], hv['status']); let evenOdd = parseInt(hv['light_id'])%2 ? 'odd': 'even'; let availableClass = hv['available'] ? 'is-available' : 'is-not-available'; let realHugveyId = ""; if(this.lightMap[hv['light_id']].length > 1 || this.lightMap[hv['light_id']][0] != hv['light_id']) { realHugveyId = '(#' + this.lightMap[hv['light_id']].join(' #') + ')'; } this.dataGroups.update({id: parseInt(hv['light_id']), content: `