SODashServer/app/js/appv3.js
2016-07-28 13:57:16 +01:00

369 lines
8.6 KiB
JavaScript

/*
SO App v2
*/
var skycons = new Skycons({color: '#e5f7fd'});
var SOController = (function() {
'use strict';
var path;
var wsUrl;
var local = true;
var bus = {};
var prevDate;
var prevTime;
var weatherTimer = 0;
var weatherStore = null;
var lastWeatherRequest = 0;
var soWebSocket;
var iosConfig = {
badge: true, sound: true, alert: true
};
var calendarData;
var extendTimer;
var lightArray = [];
var lightsList = {
off: ['frontLightOff', 'backLightOff'], on: ['frontLightOn', 'backLightOn'], aux: ['auxFront','auxBack']
};
console.log('Localmode',local);
if (local) {
console.log('Using local urls');
path = 'http://localhost:3000/';
wsUrl = 'ws://localhost:3001';
} else {
console.log('Using remote urls');
/*wsUrl = 'ws://ec2-52-50-147-81.eu-west-1.compute.amazonaws.com:8080';
path = 'http://ec2-52-50-147-81.eu-west-1.compute.amazonaws.com/';*/
/*wsUrl = 'ws://13.80.9.55:8080';
path = 'http://13.80.9.55/';*/
wsUrl = 'ws://smartofficehost.cloudapp.net:8080';
path = 'http://smartofficehost.cloudapp.net/';
}
MicroEvent.mixin(bus);
var iOS = ['iPad', 'iPhone', 'iPod'].indexOf(navigator.platform) >= 0;
if (iOS) {
$('#iosTaskbar').show();
// path = 'http://localhost:3000/';
}
var module = {lights: lightsList};
var storage = {};
storage.supportsLocalStorage = function() {
try {
return !localStorage ? false : true;
}
catch (e) {
return false;
}
};
storage._localStorage = storage.supportsLocalStorage();
if (storage._localStorage) {
storage.LocalStorage = {
save: function(i, v) {
localStorage[i] = v;
}, load: function(i) {
return localStorage[i];
}
};
} else {
storage.LocalStorage = {
save: function(i, v) {
document.cookie = (i) + '=' + encodeURIComponent(v);
}, load: function(i) {
var s;
var p;
s = '; ' + document.cookie + ';';
p = s.indexOf('; ' + i + '=');
if (p < 0) {
return '';
}
p = p + i.length + 3;
var p2 = s.indexOf(';', p + 1);
return decodeURIComponent(s.substring(p, p2));
}
};
}
function projectorCommand(id) {
console.log('projectorCommand',path + 'api/v1/projector/cmd');
$.post(path + 'api/v1/projector/cmd', {id: id}, function() {});
}
/*
function lightCommand(id) {
console.log('lightCommand',path + 'api/v1/lighting/cmd');
$.post(path + 'api/v1/lighting/cmd', {id: id}, function() {});
}
function turnOnLights(id) {
console.log('turnonlights',path + 'api/v1/lighting/on');
$.post(path + 'api/v1/lighting/on', {light: id}, function() {});
}
function turnOffLights(id) {
$.post(path + 'api/v1/lighting/off', {light: id}, function() {});
}
*/
function turnOnHeating() {
$.post(path + 'api/v1/heating/on', {}, function() {});
}
function turnOffHeating() {
$.post(path + 'api/v1/heating/off', {}, function() {});
}
function turnOnProjector() {
$.post(path + 'api/v1/projector/on', {}, function() {});
}
function turnOffProjector() {
$.post(path + 'api/v1/projector/off', {}, function() {});
}
function extendMeetingBy(extendBy) {
console.log('+ extendMeetingBy', extendBy);
var payload = {extendBy: extendBy};
if (Object.has(calendarData.current, 'actualEnd')) {
payload.uid = calendarData.current.uid;
}
$.post(path + 'api/v1/extend', payload, function() {});
console.log('+ extendMeetingBy', extendBy);
}
function attachClicks() {
/*
$('#projectorOn').on('click', function() {
turnOnProjector();
});
$('#projectorOff').on('click', function() {
turnOffProjector();
});
*/
$('#heatingOn').on('click', function() {
turnOnHeating();
});
$('#heatingOff').on('click', function() {
turnOffHeating();
});
$('#extend05').on('click', function() {
extendMeetingBy(5);
});
$('#extend10').on('click', function() {
extendMeetingBy(10);
});
$('#extend15').on('click', function() {
extendMeetingBy(15);
});
$('#extend30').on('click', function() {
extendMeetingBy(30);
});
$('#projectorHDMI').on('click', function(event) {
var target = $(event.target);
projectorCommand('HDMI');
delayButton(target);
});
$('#projectorVGA').on('click', function(event) {
var target = $(event.target);
projectorCommand('VGA');
delayButton(target);
});
}
function delayButton($button) {
$button.attr('disabled', true);
setTimeout(function() {
$button.prop('disabled', false);
},3000);
}
var browserNotify = function(d) {
if (Notification.permission !== 'granted') {
Notification.requestPermission();
} else {
var notification = new Notification('SmartOffice Console', {
icon: '/fav/favicon-96x96.png',
body: d.msg
});
notification.onclick = function() {
console.log('click');
};
}
};
var notifyConfirm = function(r) {
console.log('notifyConfirm', r);
};
var cordovaNotify = function(d) {
console.log('+++ CORDOVA NOTIFY +++');
try {
navigator.notification.beep(1);
} catch (e) {
console.log(e);
}
try {
navigator.notification.confirm(
d.msg, // Message
notifyConfirm, // Callback to invoke with index of button pressed
'Smartoffice Console', // Title
['Extend','Ignore'] // ButtonLabels
);
} catch (e) {
console.log(e);
}
};
var setupPushNotifications = function() {
console.log('Setting up iOS push notifications..');
var push = PushNotification.init({android: {},ios: iosConfig,windows: {}});
push.on('registration', function(d) {
$.post(path + 'api/v1/register/ios', d, function() {});
});
push.on('notification', function(d) {
console.log(d);
});
push.on('error', function(e) {
Console.error('*PUSH* Push error:', e);
});
};
//
module.init = function() {
var self = this;
// AttachClicks();
this.clock = new Clock({model: new ClockModel()});
this.meetingModel = new MeetingModel();
this.meetings = new Meetings({model: this.meetingModel});
this.frontLightData = new LightModel({device: 'front',on: 'o',off: 'f', up: '9', down: '1'});
this.frontLight = new Light({el: $('#front-light'),model: this.frontLightData});
this.backLightData = new LightModel({device: 'back',on: 'n',off: 'g', up: '8', down: '2'});
this.backLight = new Light({el: $('#back-light'),model: this.backLightData});
this.projectorData = new ProjectorModel({device: 'projector', up: '8', down: '2'});
this.projector = new Projector({el: $('#projector-panel'),model: this.projectorData});
this.addLight(this.frontLightData);
this.addLight(this.backLightData);
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
self.weather = new Weather({model: new WeatherModel({lat: latitude, long: longitude})});
},
function(e) {console.error(e.code + ' / ' + e.message);});
soWebSocket = new SOWEBSOCKET(this);
// Setup iOS notifications
if (window.cordova && !(window.cordova instanceof HTMLElement)) {
if (device.platform === 'iOS' || device.platform === 'ios') {
setupPushNotifications();
}
}
};
module.updateCalendar = function(data) {
console.log('module.updateCalendar','Updating data');
this.meetingModel.set('data',data);
//UpdateCalendar(data);
};
module.webSocketURL = function() {
return wsUrl;
};
module.notify = function(d) {
if (window.cordova && !(window.cordova instanceof HTMLElement)) {
if (device.platform === 'android' || device.platform === 'Android') {
cordovaNotify(d);
}
} else {
browserNotify(d);
}
};
module.addLight = function(obj) {
lightArray.push(obj);
};
module.getLight = function(n) {
return lightArray[n];
};
return module;
})();
function onDeviceReady() {
SOController.init();
}
/*
If (window.cordova && !(window.cordova instanceof HTMLElement)) {
document.addEventListener('deviceready', onDeviceReady, false);
} else {
}
*/
;(function() {
if (window.cordova && !(window.cordova instanceof HTMLElement)) {
document.addEventListener('deviceready', onDeviceReady, false);
} else {
onDeviceReady();
Notification.requestPermission();
}
})();