Skip to content
Snippets Groups Projects
Commit 8a62cabb authored by Denis Peters's avatar Denis Peters
Browse files

clean up javascript files (a little); uncomment filter for blacklist/whitelist;

parent 7152b4c5
No related branches found
No related tags found
No related merge requests found
function Clock(panel, config) {
this.canvas = panel[0].querySelector("canvas");
this.pane = this.canvas.getContext("2d");
this.bgCanvas = document.createElement("canvas");
this.bgPane = this.bgCanvas.getContext("2d");
this.monthNames = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"];
this.dayNames = ["so","mo","di","mi","do","fr","sa"];
this.data = {
panel: {
widht: 0,
height: 0
},
bounds: {
outer: {
x: 0,
y: 0,
widht: 0,
height: 0
},
inner: {
x: 0,
y: 0,
widht: 0,
height: 0
}
},
colors: {
background: "#000",
foreground: "#fff",
shaddow: "#333"
},
fonts: {
fat: "time-fat",
medium: "time-medium"
},
aClock: {
bgImage: "background.svg",
radius: 0,
center: {
x: 0,
y: 0
},
bounds: {
outer: {
x: 0,
y: 0,
widht: 0,
height: 0
},
inner: {
x: 0,
y: 0,
widht: 0,
height: 0
}
}
},
dClock: {
fonts: {
fat: "",
big: "",
small: ""
},
bounds: {
outer: {
x: 0,
y: 0,
widht: 0,
height: 0
},
inner: {
x: 0,
y: 0,
widht: 0,
height: 0
}
},
positions: {
hour : {
x: 0,
y: 0,
},
pulse : {
x: 0,
y: 0,
},
minute : {
x: 0,
y: 0,
},
second : {
x: 0,
y: 0,
}
}
},
dDate: {
font: "",
bounds: {
outer: {
x: 0,
y: 0,
widht: 0,
height: 0
},
inner: {
x: 0,
y: 0,
widht: 0,
height: 0
}
},
position: {
x: 0,
y: 0
}
}
};
this.resize = function(width, height) {
this.data.panel.width = width;
this.data.panel.height = height;
// canvas
this.canvas.width = this.data.panel.width;
this.canvas.height = this.data.panel.height;
this.pane = this.canvas.getContext("2d");
// bgCanvas
this.bgCanvas.width = this.data.panel.width;
this.bgCanvas.height = this.data.panel.height;
this.bgPane = this.bgCanvas.getContext("2d");
// outer bounds
this.data.bounds.outer.x = 10;
this.data.bounds.outer.y = 10;
this.data.bounds.outer.width = width - 20;
this.data.bounds.outer.height = height - 20;
var ratio = this.data.bounds.outer.width / this.data.bounds.outer.height;
if (ratio > 1.5) {
// widget
if (this.data.bounds.outer.height * 3 < this.data.bounds.outer.width) {
this.data.bounds.inner.height = this.data.bounds.outer.height;
this.data.bounds.inner.width = this.data.bounds.outer.height * 3;
} else {
this.data.bounds.inner.height = this.data.bounds.outer.width / 3;
this.data.bounds.inner.width = this.data.bounds.outer.width;
}
this.data.bounds.inner.x = this.data.bounds.outer.x + (this.data.bounds.outer.width - this.data.bounds.inner.width) / 2;
this.data.bounds.inner.y = this.data.bounds.outer.y + (this.data.bounds.outer.height - this.data.bounds.inner.height) / 2;
// dClock
this.data.dClock.bounds.outer.x = this.data.bounds.inner.x + this.data.bounds.inner.width * 0.4;
this.data.dClock.bounds.outer.y = this.data.bounds.inner.y;
this.data.dClock.bounds.outer.width = this.data.bounds.inner.width * 0.6;
this.data.dClock.bounds.outer.height = this.data.bounds.inner.height * 0.6;
// dDate
this.data.dDate.bounds.outer.x = this.data.bounds.inner.x + this.data.bounds.inner.width * 0.4;
this.data.dDate.bounds.outer.y = this.data.bounds.inner.y + this.data.bounds.inner.height * 0.6;
this.data.dDate.bounds.outer.width = this.data.bounds.inner.width * 0.6;
this.data.dDate.bounds.outer.height = this.data.bounds.inner.height * 0.4;
// aClock
this.data.aClock.bounds.outer.x = this.data.bounds.inner.x;
this.data.aClock.bounds.outer.y = this.data.bounds.inner.y;
this.data.aClock.bounds.outer.width = this.data.bounds.inner.width * 0.4;
this.data.aClock.bounds.outer.height = this.data.bounds.inner.height;
} else if (ratio < 1) {
// widget
if (this.data.bounds.outer.height * 0.7 < this.data.bounds.outer.width) {
this.data.bounds.inner.height = this.data.bounds.outer.height;
this.data.bounds.inner.width = this.data.bounds.outer.height * 0.7;
} else {
this.data.bounds.inner.height = this.data.bounds.outer.width / 0.7;
this.data.bounds.inner.width = this.data.bounds.outer.width;
}
this.data.bounds.inner.x = this.data.bounds.outer.x + (this.data.bounds.outer.width - this.data.bounds.inner.width) / 2;
this.data.bounds.inner.y = this.data.bounds.outer.y + (this.data.bounds.outer.height - this.data.bounds.inner.height) / 2;
// dClock
this.data.dClock.bounds.outer.x = this.data.bounds.inner.x;
this.data.dClock.bounds.outer.y = this.data.bounds.inner.y + this.data.bounds.inner.height * 0.7;
this.data.dClock.bounds.outer.width = this.data.bounds.inner.width;
this.data.dClock.bounds.outer.height = this.data.bounds.inner.height * 0.2;
// dDate
this.data.dDate.bounds.outer.x = this.data.bounds.inner.x;
this.data.dDate.bounds.outer.y = this.data.bounds.inner.y + this.data.bounds.inner.height * 0.9;
this.data.dDate.bounds.outer.width = this.data.bounds.inner.width;
this.data.dDate.bounds.outer.height = this.data.bounds.inner.height * 0.1;
// aClock
this.data.aClock.bounds.outer.x = this.data.bounds.inner.x;
this.data.aClock.bounds.outer.y = this.data.bounds.inner.y;
this.data.aClock.bounds.outer.width = this.data.bounds.inner.width;
this.data.aClock.bounds.outer.height = this.data.bounds.inner.height * 0.7;
} else {
// widget
if (this.data.bounds.outer.height * 2 < this.data.bounds.outer.width) {
this.data.bounds.inner.height = this.data.bounds.outer.height;
this.data.bounds.inner.width = this.data.bounds.outer.height * 2;
} else {
this.data.bounds.inner.height = this.data.bounds.outer.width / 2;
this.data.bounds.inner.width = this.data.bounds.outer.width;
}
this.data.bounds.inner.x = this.data.bounds.outer.x + (this.data.bounds.outer.width - this.data.bounds.inner.width) / 2;
this.data.bounds.inner.y = this.data.bounds.outer.y + (this.data.bounds.outer.height - this.data.bounds.inner.height) / 2;
// dClock
this.data.dClock.bounds.outer.x = this.data.bounds.inner.x + this.data.bounds.inner.width / 3;
this.data.dClock.bounds.outer.y = this.data.bounds.inner.y;
this.data.dClock.bounds.outer.width = this.data.bounds.inner.width - this.data.bounds.inner.width / 3;
this.data.dClock.bounds.outer.height = this.data.bounds.inner.height - this.data.bounds.inner.height / 3;
// dDate
this.data.dDate.bounds.outer.x = this.data.bounds.inner.x;
this.data.dDate.bounds.outer.y = this.data.bounds.inner.y + this.data.bounds.inner.height / 3 * 2;
this.data.dDate.bounds.outer.width = this.data.bounds.inner.width;
this.data.dDate.bounds.outer.height = this.data.bounds.inner.height / 3;
// aClock
this.data.aClock.bounds.outer.x = this.data.bounds.inner.x;
this.data.aClock.bounds.outer.y = this.data.bounds.inner.y;
this.data.aClock.bounds.outer.width = this.data.bounds.inner.width / 3;
this.data.aClock.bounds.outer.height = this.data.bounds.inner.height - + this.data.bounds.inner.height / 3;
}
resizeDClock();
resizeDDate();
resizeAClock();
renderBG();
}
this.render = function() {
var
date = new Date(),
n = date.getMilliseconds(),
h = date.getHours(),
i = date.getMinutes(),
s = date.getSeconds(),
t = date.getDay(),
d = date.getDate(),
m = date.getMonth(),
y = date.getFullYear();
if (d.toString().length < 2)
d = "0" + d;
if (h.toString().length < 2)
h = "0" + h;
if (i.toString().length < 2)
i = "0" + i;
if (s.toString().length < 2)
s = "0" + s;
// background
this.pane.fillStyle = this.data.colors.background;
this.pane.fillRect(0, 0, this.data.panel.width, this.data.panel.height);
this.pane.drawImage(this.bgCanvas, 0, 0);
// foreground dClock
this.pane.fillStyle = this.data.colors.foreground;
this.pane.font = this.data.dClock.fonts.fat;
if (n < 500) this.pane.fillText(":", this.data.dClock.positions.pulse.x, this.data.dClock.positions.pulse.y);
this.pane.font = this.data.dClock.fonts.big;
this.pane.fillText(h, this.data.dClock.positions.hour.x, this.data.dClock.positions.hour.y);
this.pane.fillText(i, this.data.dClock.positions.minute.x, this.data.dClock.positions.minute.y);
this.pane.font = this.data.dClock.fonts.small;
this.pane.fillText(s, this.data.dClock.positions.second.x, this.data.dClock.positions.second.y);
// foreground dDate
this.pane.font = this.data.dDate.font;
this.pane.fillText(this.dayNames[t] + " " + d + " " + this.monthNames[m] + " " + y, this.data.dDate.position.x, this.data.dDate.position.y);
// foreground aClock
this.pane.strokeStyle = this.data.colors.foreground;
//hour
var hour = ((h % 12) * Math.PI / 6) + (i * Math.PI / (6 * 60)) + (s * Math.PI / (360 * 60));
renderHand(hour, this.data.aClock.radius * 0.5, this.data.aClock.radius * 0.07);
//minute
var minute = (i * Math.PI / 30) + (s * Math.PI / (30 * 60));
renderHand(minute, this.data.aClock.radius * 0.8, this.data.aClock.radius * 0.07);
// second
var second = (s * Math.PI / 30);
renderHand(second, this.data.aClock.radius * 0.9, this.data.aClock.radius * 0.02);
// debug
//renderDebug();
}
var renderHand = function(pos, length, width) {
this.pane.beginPath();
this.pane.lineWidth = width;
this.pane.lineCap = "round";
this.pane.translate(this.data.aClock.center.x, this.data.aClock.center.y);
this.pane.moveTo(0, 0);
this.pane.rotate(pos);
this.pane.lineTo(0, -length);
this.pane.stroke();
this.pane.rotate(-pos);
this.pane.translate(-this.data.aClock.center.x, -this.data.aClock.center.y);
this.pane.lineWidth = 1;
}.bind(this);
var renderBG = function() {
// background dClock
this.bgPane.fillStyle = this.data.colors.shaddow;
this.bgPane.font = this.data.dClock.fonts.fat;
this.bgPane.fillText(":", this.data.dClock.positions.pulse.x, this.data.dClock.positions.pulse.y);
this.bgPane.font = this.data.dClock.fonts.big;
this.bgPane.fillText("88", this.data.dClock.positions.hour.x, this.data.dClock.positions.hour.y);
this.bgPane.fillText("88", this.data.dClock.positions.minute.x, this.data.dClock.positions.minute.y);
this.bgPane.font = this.data.dClock.fonts.small;
this.bgPane.fillText("88", this.data.dClock.positions.second.x, this.data.dClock.positions.second.y);
// background dDate
this.bgPane.font = this.data.dDate.font;
this.bgPane.fillText("00 00 000 0000", this.data.dDate.position.x, this.data.dDate.position.y);
this.bgPane.fillText("** ** *** ****", this.data.dDate.position.x, this.data.dDate.position.y);
if (typeof this.data.aClock.bgImage == "string") {
var img = new Image();
img.onload = function(e) {
this.data.aClock.bgImage = e.target;
this.bgPane.drawImage(
this.data.aClock.bgImage,
this.data.aClock.bounds.inner.x,
this.data.aClock.bounds.inner.y,
this.data.aClock.bounds.inner.width,
this.data.aClock.bounds.inner.height
);
}.bind(this);
img.src = "panels/clock/"+this.data.aClock.bgImage;
} else {
this.bgPane.drawImage(
this.data.aClock.bgImage,
this.data.aClock.bounds.inner.x,
this.data.aClock.bounds.inner.y,
this.data.aClock.bounds.inner.width,
this.data.aClock.bounds.inner.height
);
}
}.bind(this);
var resizeDClock = function() {
var aspect = 6 / 16;
var ratio = this.data.dClock.bounds.outer.width * aspect;
if (ratio > this.data.dClock.bounds.outer.height) {
this.data.dClock.bounds.inner.width = this.data.dClock.bounds.outer.height / aspect;
this.data.dClock.bounds.inner.height = this.data.dClock.bounds.outer.height;
} else if (ratio < this.data.dClock.bounds.outer.height) {
this.data.dClock.bounds.inner.width = this.data.dClock.bounds.outer.width;
this.data.dClock.bounds.inner.height = this.data.dClock.bounds.outer.width * aspect;
} else {
this.data.dClock.bounds.inner.width = this.data.dClock.bounds.outer.width;
this.data.dClock.bounds.inner.height = this.data.dClock.bounds.outer.height;
}
this.data.dClock.bounds.inner.x = this.data.dClock.bounds.outer.x + (this.data.dClock.bounds.outer.width - this.data.dClock.bounds.inner.width) / 2;
this.data.dClock.bounds.inner.y = this.data.dClock.bounds.outer.y + (this.data.dClock.bounds.outer.height - this.data.dClock.bounds.inner.height) / 2;
this.data.dClock.positions.pulse.x = this.data.dClock.bounds.inner.x + this.data.dClock.bounds.inner.width * 0.315;
this.data.dClock.positions.pulse.y = this.data.dClock.bounds.inner.y + this.data.dClock.bounds.inner.height * 0.72;
this.data.dClock.positions.hour.x = this.data.dClock.bounds.inner.x + this.data.dClock.bounds.inner.width * 0.02;
this.data.dClock.positions.hour.y = this.data.dClock.bounds.inner.y + this.data.dClock.bounds.inner.height * 0.88;
this.data.dClock.positions.minute.x = this.data.dClock.bounds.inner.x + this.data.dClock.bounds.inner.width * 0.43;
this.data.dClock.positions.minute.y = this.data.dClock.bounds.inner.y + this.data.dClock.bounds.inner.height * 0.88;
this.data.dClock.positions.second.x = this.data.dClock.bounds.inner.x + this.data.dClock.bounds.inner.width * 0.79;
this.data.dClock.positions.second.y = this.data.dClock.bounds.inner.y + this.data.dClock.bounds.inner.height * 0.85;
var
bigFontSize = this.data.dClock.bounds.inner.height * 1.3,
smlFontSize = this.data.dClock.bounds.inner.height * 0.7;
this.data.dClock.fonts.fat = bigFontSize + "px " + this.data.fonts.fat;
this.data.dClock.fonts.big = bigFontSize + "px " + this.data.fonts.medium;
this.data.dClock.fonts.small = smlFontSize + "px " + this.data.fonts.medium;
}.bind(this);
var resizeDDate = function() {
var aspect = 2.3 / 16;
var ratio = this.data.dDate.bounds.outer.width * aspect;
if (ratio > this.data.dDate.bounds.outer.height) {
this.data.dDate.bounds.inner.width = this.data.dDate.bounds.outer.height / aspect;
this.data.dDate.bounds.inner.height = this.data.dDate.bounds.outer.height;
} else if (ratio < this.data.dDate.bounds.outer.height) {
this.data.dDate.bounds.inner.width = this.data.dDate.bounds.outer.width;
this.data.dDate.bounds.inner.height = this.data.dDate.bounds.outer.width * aspect;
} else {
this.data.dDate.bounds.inner.width = this.data.dDate.bounds.outer.width;
this.data.dDate.bounds.inner.height = this.data.dDate.bounds.outer.height;
}
this.data.dDate.bounds.inner.x = this.data.dDate.bounds.outer.x + (this.data.dDate.bounds.outer.width - this.data.dDate.bounds.inner.width) / 2;
this.data.dDate.bounds.inner.y = this.data.dDate.bounds.outer.y + (this.data.dDate.bounds.outer.height - this.data.dDate.bounds.inner.height) / 2;
this.data.dDate.position.x = this.data.dDate.bounds.inner.x + this.data.dDate.bounds.inner.width * 0.02;
this.data.dDate.position.y = this.data.dDate.bounds.inner.y + this.data.dDate.bounds.inner.height * 0.88;
var fontSize = this.data.dDate.bounds.inner.height * 1.3;
this.data.dDate.font = fontSize + "px " + this.data.fonts.medium;
}.bind(this);
var resizeAClock = function() {
if (this.data.aClock.bounds.outer.width > this.data.aClock.bounds.outer.height) {
this.data.aClock.bounds.inner.width = this.data.aClock.bounds.outer.height;
this.data.aClock.bounds.inner.height = this.data.aClock.bounds.outer.height;
} else if (this.data.aClock.bounds.outer.width < this.data.aClock.bounds.outer.height) {
this.data.aClock.bounds.inner.width = this.data.aClock.bounds.outer.width;
this.data.aClock.bounds.inner.height = this.data.aClock.bounds.outer.width;
} else {
this.data.aClock.bounds.inner.width = this.data.aClock.bounds.outer.width;
this.data.aClock.bounds.inner.height = this.data.aClock.bounds.outer.height;
}
this.data.aClock.bounds.inner.x = this.data.aClock.bounds.outer.x + (this.data.aClock.bounds.outer.width - this.data.aClock.bounds.inner.width) / 2;
this.data.aClock.bounds.inner.y = this.data.aClock.bounds.outer.y + (this.data.aClock.bounds.outer.height - this.data.aClock.bounds.inner.height) / 2;
this.data.aClock.center.x = this.data.aClock.bounds.inner.x + this.data.aClock.bounds.inner.width / 2;
this.data.aClock.center.y = this.data.aClock.bounds.inner.y + this.data.aClock.bounds.inner.height / 2;
this.data.aClock.radius = this.data.aClock.bounds.inner.height * 0.45;
}.bind(this);
var renderDebug = function() {
// panel bounds
this.pane.strokeStyle = "#ff0";
this.pane.strokeRect(this.data.bounds.inner.x, this.data.bounds.inner.y, this.data.bounds.inner.width, this.data.bounds.inner.height);
this.pane.strokeStyle = "#0ff";
this.pane.strokeRect(this.data.bounds.outer.x, this.data.bounds.outer.y, this.data.bounds.outer.width, this.data.bounds.outer.height);
// inner bounds
this.pane.strokeStyle = "#0f0";
this.pane.strokeRect(this.data.dClock.bounds.inner.x, this.data.dClock.bounds.inner.y, this.data.dClock.bounds.inner.width, this.data.dClock.bounds.inner.height);
this.pane.strokeRect(this.data.dDate.bounds.inner.x, this.data.dDate.bounds.inner.y, this.data.dDate.bounds.inner.width, this.data.dDate.bounds.inner.height);
this.pane.strokeRect(this.data.aClock.bounds.inner.x, this.data.aClock.bounds.inner.y, this.data.aClock.bounds.inner.width, this.data.aClock.bounds.inner.height);
// outer bounds
this.pane.strokeStyle = "#f0f";
this.pane.strokeRect(this.data.dClock.bounds.outer.x, this.data.dClock.bounds.outer.y, this.data.dClock.bounds.outer.width, this.data.dClock.bounds.outer.height);
this.pane.strokeRect(this.data.dDate.bounds.outer.x, this.data.dDate.bounds.outer.y, this.data.dDate.bounds.outer.width, this.data.dDate.bounds.outer.height);
this.pane.strokeRect(this.data.aClock.bounds.outer.x, this.data.aClock.bounds.outer.y, this.data.aClock.bounds.outer.width, this.data.aClock.bounds.outer.height);
this.pane.strokeStyle = "";
}.bind(this);
}
\ No newline at end of file
AJAX = new (function AJAX() {
function newXHR() {
return !!self.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
}
function serialize(obj, prefix) {
var str = [];
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
var k = prefix ? prefix + "[" + p + "]" : p,
v = obj[p];
str.push(typeof v == "object" ? serialize(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
}
return str.join("&");
}
function decodeResponse(type, response) {
try {
switch (type) {
case "application/json":
return JSON.parse(response);
default:
return response;
}
} catch(e) {
}
return response;
}
function doReject(strict, result){
return !!strict ? Promise.reject(result) : Promise.resolve(result);
}
function doRequest(url, method, params, strict) {
return new Promise(function(resolve, reject) {
try {
if (typeof url != 'string') {
return doReject(strict, {
success: false,
response: 'url parameter must be string',
status: 0
}).then(resolve, reject);
}
if (typeof method != 'string') {
return doReject(strict, {
success: false,
response: 'method parameter must be string',
status: 0
}).then(resolve, reject);
}
method = method.toUpperCase();
var xhr = newXHR();
xhr.onload = function(e) {
if (e.target.status == 200) {
var response = decodeResponse(e.target.getResponseHeader("Content-Type"), e.target.response);
resolve({
success: true,
response: response,
status: e.target.status
});
} else {
var statusText = e.target.statusText;
if (!statusText.length) statusText = "unexpected error";
doReject(strict, {
success: false,
response: statusText,
status: e.target.status
}).then(resolve, reject);
}
}
xhr.onerror = function(e) {
var statusText = e.target.statusText;
if (!statusText.length) statusText = "unexpected error";
doReject(strict, {
success: false,
response: statusText,
status: e.target.status
}).then(resolve, reject);
}
xhr.onabort = function(e) {
doReject(strict, {
success: false,
response: 'download aborted',
status: 0
}).then(resolve, reject);
}
params = typeof params == 'object' ? serialize(params) : '';
if (method === 'GET') {
if (!!params.length) params = "?" + params;
xhr.open(method, url+params);
xhr.send();
} else {
xhr.open(method, url);
xhr.send(!!params.length ? params : undefined);
}
} catch(e) {
doReject(strict, {
success: false,
response: e.message,
status: 0
}).then(resolve, reject);
}
});
}
this.request = function(requests, strict) {
if (typeof strict != 'boolean') strict = true;
try {
if (typeof requests == 'string') {
return doRequest(requests, 'GET', [], strict);
} else {
if (typeof requests == 'object') {
if (Array.isArray(requests)) {
var proms = [];
for (var i = 0; i < requests.length; ++i)
proms.push(this.request(requests[i], strict));
return Promise.all(proms);
} else {
return doRequest(requests.url, requests.method, requests.params, strict);
}
}
throw new Error('wrong type (string/array/dict expected, '+(typeof requests)+' detected)');
}
} catch(e) {
return doReject(strict, {
success: false,
response: e.message,
status: 0
});
}
}
return Object.freeze(this);
});
\ No newline at end of file
class AbstractWorker {
constructor() {
this.config = {};
var
ival = 1000,
timer,
cmds = [],
execRun = false;
var update = function() {
if (!!timer) self.clearTimeout(timer);
this.tick(function(result) {
self.postMessage(result);
});
if (ival > 0) timer = self.setTimeout(update, ival);
}.bind(this);
var exec = function() {
if (!!cmds.length) {
execRun = true;
var cmd = cmds.shift();
if (typeof this[cmd.cmd] == "function") {
this[cmd.cmd].apply(this,cmd.params);
} else {
exec();
}
} else {
execRun = false;
}
}.bind(this);
this.start = function() {
update();
exec();
}
this.stop = function() {
if (!!timer) self.clearTimeout(timer);
exec();
}
this.loadConfig = function(name) {
AJAX.request("../../panels/departure/config/"+name+".json").then(function(data) {
this.config = this.processConfig(data.response);
exec();
}.bind(this),function() {
exec();
});
}
self.onmessage = function(e) {
cmds.push(e.data);
if (!execRun) exec();
}.bind(this);
this.setSpeed = function(val) {
if (isNaN(val = parseInt(val))) {
val = 0;
}
ival = val;
}
}
tick(callback) {
callback({});
}
processConfig(config) {
return config;
}
}
\ No newline at end of file
importScripts('AJAX.js');
importScripts('check.js');
importScripts('AbstractWorker.js');
class DepartureWorker extends AbstractWorker {
constructor() {
super();
this.setSpeed(360000);
}
processConfig(config) {
if (isObject(config) && isArrayNotEmpty(config['stops'])) {
config['stops_converted'] = [];
for (var i = 0; i < config['stops'].length; ++i) {
config['stops_converted'].push({url:'https://vrrf.finalrewind.org/'+config['stops'][i].split(':').join('/')+'.json',method:'GET',params:{frontend:'json'}});
}
}
return config;
}
tick(callback) {
var config = this.config;
if (isObject(config) && isArrayNotEmpty(config['stops_converted'])) {
AJAX.request(config['stops_converted']).then(function(results) {
if (results.length == 0) return;
var
reftime = new Date(),
result = {
vrrf_version: {
actual: '0.0.0.0',
expected: '0.0.0.0'
},
errors: {},
info: {},
lines: {},
raw: {}
},
rawLines = {};
reftime = calcDateValue(reftime.getFullYear(), reftime.getMonth()+1, reftime.getDate(), reftime.getHours(), reftime.getMinutes());
for (var i = 0; i < results.length; ++i) {
if (!results[i].success) continue;
var data = results[i].response;
if (!isObject(data)) data = JSON.parse(data);
var stopName = config['stops'][i].split('/').join(' - ');
result['vrrf_version']['actual'] = data['version'];
result['vrrf_version']['expected'] = '0.0.0.0';
result['errors'][stopName] = data['error'];
result['info'][stopName] = '';
result['raw'][stopName] = data['raw'];
for (var j = 0; j < data['raw'].length; ++j) {
var entry = data['raw'][j];
// filter
//if (isObjectNotEmpty(config['filter']) && !passFilter(config['filter'], entry)) continue;
// interprete
var
schedDate = entry['sched_date'].split('.'),
schedTime = entry['sched_time'].split(':'),
deptime = calcDateValue(schedDate[2], schedDate[1], schedDate[0], schedTime[0], schedTime[1]);
var delay = parseInt(entry['delay']);
if (isNaN(delay)) delay = 0;
if (((deptime + delay) - reftime) > 0) {
var ident = /*entry['lineref']['operator']+'|'+entry['lineref']['type']+'|'+*/entry['line']+'|'+entry['lineref']['identifier']+'|'+entry['key'];
if (!isObjectNotEmpty(result['lines'][ident])) {
result['lines'][ident] = {
timeValue: deptime,
line: entry['line'],
destination: entry['destination'],
type: entry['type'],
stops: [{
timeValue: deptime,
time: entry['sched_time'],
delay: entry['delay'],
cancel: entry['is_cancelled'],
name: stopName,
info: entry['info']
}]
};
} else {
result['lines'][ident]['stops'].push({
timeValue: deptime,
time: entry['sched_time'],
delay: entry['delay'],
cancel: entry['is_cancelled'],
name: stopName,
info: entry['info']
});
}
}
}
}
result['lines'] = sortData(Object.values(result['lines']));
if (isNumber(config.max)) {
result['lines'] = result['lines'].slice(0, config.max);
}
callback(result);
});
}
}
}
function passFilter(filter, entry) {
if (isObjectNotEmpty(filter['bl'])) {
if (isArrayNotEmpty(filter['bl']['line'])
&& arrayContains(filter['bl']['line'], entry['line'])) {
return false;
}
if (isArrayNotEmpty(filter['bl']['type'])
&& arrayContains(filter['bl']['type'], entry['type'])) {
return false;
}
if (isArrayNotEmpty(filter['bl']['platform'])
&& arrayContains(filter['bl']['platform'], entry['platform'])) {
return false;
}
if (isArrayNotEmpty(filter['bl']['destination'])
&& arrayContains(filter['bl']['destination'], entry['destination'])) {
return false;
}
}
if (isObjectNotEmpty(filter['wl'])) {
if (isArrayNotEmpty(filter['wl']['line'])
&& !arrayContains(filter['wl']['line'], entry['line'])) {
return false;
}
if (isArrayNotEmpty(filter['wl']['type'])
&& !arrayContains(filter['wl']['type'], entry['type'])) {
return false;
}
if (isArrayNotEmpty(filter['wl']['platform'])
&& !arrayContains(filter['wl']['platform'], entry['platform'])) {
return false;
}
if (isArrayNotEmpty(filter['wl']['destination'])
&& !arrayContains(filter['wl']['destination'], entry['destination'])) {
return false;
}
}
return true;
}
function sortData(data) {
for (var i = 0; i < data.length; ++i) {
data[i]['stops'] = data[i]['stops'].sort(sortFn);
data[i]['timeValue'] = data[i]['stops'][0]['timeValue'];
}
return data.sort(sortFn);
}
function sortFn(a,b) {
return a['timeValue'] - b['timeValue'];
}
function calcDateValue(year, month, day, hour, minute) {
year = parseInt(year) * 12 * 31 * 24 * 60;
month = parseInt(month) * 31 * 24 * 60;
day = parseInt(day) * 24 * 60;
hour = parseInt(hour) * 60;
minute = parseInt(minute);
return year+month+day+hour+minute;
}
new DepartureWorker;
self.interval = self.interval || 1000;
self.tick = self.tick || function() {};
self.config = self.config || {};
(new function() {
var
me = this,
timer,
cmds = [],
execRun = false;
function update() {
if (!!timer) self.clearTimeout(timer);
self.tick(function(result) {
self.postMessage(result);
});
timer = self.setTimeout(update, self.interval);
}
function exec() {
if (!!cmds.length) {
execRun = true;
var cmd = cmds.shift();
me[cmd.cmd].apply(this,cmd.params);
} else {
execRun = false;
}
}
this.start = function() {
update();
exec();
}
this.stop = function() {
if (!!timer) self.clearTimeout(timer);
exec();
}
this.loadConfig = function(name) {
AJAX.request("../../panels/departure/config/"+name+".json").then(function(data) {
config = data.response;
if (isObject(config) && isArrayNotEmpty(config['stops'])) {
config['stops_converted'] = [];
for (var i = 0; i < config['stops'].length; ++i) {
config['stops_converted'].push({url:'http://vrrf.finalrewind.org/'+config['stops'][i].split(':').join('/')+'.json',method:'GET',params:{frontend:'json'}});
}
}
exec();
},function() {
exec();
});
}
self.onmessage = function(e) {
cmds.push(e.data);
if (!execRun) exec();
}.bind(this);
}());
\ No newline at end of file
importScripts('AJAX.js');
importScripts('check.js');
self.interval = 360000;
self.tick = function(callback) {
var config = self.config;
if (isObject(config) && isArrayNotEmpty(config['stops_converted'])) {
AJAX.request(config['stops_converted']).then(function(results) {
if (results.length == 0) return;
var
result = {
vrrf_version: {
actual: '0.0.0.0',
expected: '0.0.0.0'
},
errors: {},
info: {},
lines: {},
raw: {}
},
rawLines = {};
for (var i = 0; i < results.length; ++i) {
if (!results[i].success) continue;
var data = JSON.parse(results[i].response);
var stopName = config['stops'][i].split(':').join(' - ');
result['vrrf_version']['actual'] = data['version'];
result['vrrf_version']['expected'] = '0.0.0.0';
result['errors'][stopName] = data['error'];
result['info'][stopName] = '';
result['raw'][stopName] = data['raw'];
for (var j = 0; j < data['raw'].length; ++j) {
var entry = data['raw'][j];
// filter
if (isObjectNotEmpty(config['filter']) && !passFilter(config['filter'], entry)) continue;
// interprete
var
reftime = new Date(),
schedDate = entry['sched_date'].split('.'),
schedTime = entry['sched_time'].split(':'),
deptime = calcDateValue(schedDate[0], schedTime[0], schedTime[1]);
reftime = calcDateValue(reftime.getDate(), reftime.getHours(), reftime.getMinutes());
if (deptime + parseInt(entry['delay']) - reftime) {
var ident = entry['lineref']['operator']+'|'+entry['lineref']['type']+'|'+entry['line']+'|'+entry['lineref']['identifier']+'|'+entry['key'];
if (!isObjectNotEmpty(result['lines'][ident])) {
result['lines'][ident] = {
timeValue: deptime,
line: entry['line'],
destination: entry['destination'],
type: entry['type'],
stops: [{
timeValue: deptime,
time: entry['sched_time'],
delay: entry['delay'],
cancel: entry['is_cancelled'],
name: stopName,
info: entry['info']
}]
};
} else {
result['lines'][ident]['stops'].push({
timeValue: deptime,
time: entry['sched_time'],
delay: entry['delay'],
cancel: entry['is_cancelled'],
name: stopName,
info: entry['info']
});
}
}
}
}
result['lines'] = sortData(Object.values(result['lines']));
if (isNumber(config.max)) {
result['lines'] = result['lines'].slice(0, config.max);
}
callback(result);
});
}
}
function passFilter(filter, entry) {
if (isObjectNotEmpty(filter['bl'])) {
if (isArrayNotEmpty(filter['bl']['line'])
&& arrayContains(filter['bl']['line'], entry['line'])) {
return false;
}
if (isArrayNotEmpty(filter['bl']['type'])
&& arrayContains(filter['bl']['type'], entry['type'])) {
return false;
}
if (isArrayNotEmpty(filter['bl']['platform'])
&& arrayContains(filter['bl']['platform'], entry['platform'])) {
return false;
}
if (isArrayNotEmpty(filter['bl']['destination'])
&& arrayContains(filter['bl']['destination'], entry['destination'])) {
return false;
}
}
if (isObjectNotEmpty(filter['wl'])) {
if (isArrayNotEmpty(filter['wl']['line'])
&& !arrayContains(filter['wl']['line'], entry['line'])) {
return false;
}
if (isArrayNotEmpty(filter['wl']['type'])
&& !arrayContains(filter['wl']['type'], entry['type'])) {
return false;
}
if (isArrayNotEmpty(filter['wl']['platform'])
&& !arrayContains(filter['wl']['platform'], entry['platform'])) {
return false;
}
if (isArrayNotEmpty(filter['wl']['destination'])
&& !arrayContains(filter['wl']['destination'], entry['destination'])) {
return false;
}
}
return true;
}
function sortData(data) {
for (var i = 0; i < data.length; ++i) {
data[i]['stops'] = data[i]['stops'].sort(sortFn);
data[i]['timeValue'] = data[i]['stops'][0]['timeValue'];
}
return data.sort(sortFn);
}
function sortFn(a,b) {
return a['timeValue'] > b['timeValue'];
}
function calcDateValue(day, hour, minute) {
return parseInt(day) * 24 * 60 + parseInt(hour) * 60 + parseInt(minute);
}
importScripts('Worker.js');
\ No newline at end of file
function isEmpty(value) {
switch (typeof value) {
case 'undefined':
return true;
break;
case 'object':
if (Array.isArray(value)) return value.length <= 0;
return value === null || Object.getOwnPropertyNames(value).length <= 0;
break;
case 'string':
return value == "";
break;
case 'number':
return isNaN(value);
break;
default:
return false;
break;
}
}
function isFunction(value) {
return typeof(value) == 'function';
}
function isObject(value) {
return typeof(value) == 'object' && !Array.isArray(value);
}
function isObjectNotEmpty(value) {
return typeof(value) == 'object' && !Array.isArray(value) && Object.getOwnPropertyNames(value).length > 0;
}
function isArray(value) {
return Array.isArray(value);
}
function isArrayNotEmpty(value) {
return Array.isArray(value) && value.length > 0;
}
function isString(value) {
return typeof(value) == 'string';
}
function isStringNotEmpty(value) {
return typeof(value) == 'string' && value != "";
}
function isNumber(value) {
return typeof(value) == 'number';
}
function isInteger(value) {
return Number.isSafeInteger(value);
}
function isBoolean(value) {
return typeof(value) == 'boolean';
}
function arrayContains(array, value) {
return isArray(array) && !!(array.indexOf(value)+1);
}
\ No newline at end of file
......@@ -60,7 +60,7 @@ this.loaded = function(panel, config) {
$(window).resize(r);
//var worker = new Worker('js/worker/WorkerDeparture.js');
var worker = new Worker('js/worker/DepartureWorker.js');
var worker = new Worker('/panels/departure/worker.js');
worker.addEventListener('message', function(e) {
var data = e.data;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment