2017-06-20 11:01:30 +00:00
|
|
|
//DATABASE
|
|
|
|
"use strict";
|
|
|
|
const low = require("lowdb");
|
|
|
|
var db = low("db.json");
|
|
|
|
const sid = require("short-id");
|
|
|
|
sid.configure({ length: 12 });
|
|
|
|
db.defaults({}).write();
|
|
|
|
//SOCKETIO
|
|
|
|
const httpp = require("http");
|
|
|
|
var http = httpp.createServer();
|
|
|
|
const io = require("socket.io");
|
2017-06-20 12:17:04 +00:00
|
|
|
var ioClient = require("socket.io-client")("http://192.168.1.142:5000", { reconect: true });
|
2017-06-20 11:01:30 +00:00
|
|
|
ioClient.on("connect", () => {
|
|
|
|
console.log("connected");
|
|
|
|
});
|
|
|
|
var ios = io(http);
|
|
|
|
const artnetp = require("artnet");
|
|
|
|
const universe = 21;
|
|
|
|
var artnet = artnetp({ host: "192.168.1.255" });
|
|
|
|
class FrameTime {
|
|
|
|
constructor(millis) {
|
|
|
|
this.lframe = 0;
|
|
|
|
this.lseconds = 0;
|
|
|
|
this.lminutes = 0;
|
|
|
|
this.lhours = 0;
|
|
|
|
this.lmilliseconds = 0;
|
|
|
|
if (Number.isInteger(millis)) {
|
|
|
|
this.milliseconds = millis;
|
|
|
|
}
|
|
|
|
else if (typeof millis === "object") {
|
|
|
|
if (Number.isInteger(Number(millis.frame)) && Number.isInteger(Number(millis.seconds)) && Number.isInteger(Number(millis.minutes)) && Number.isInteger(Number(millis.hours))) {
|
|
|
|
this.lframe = millis.frame; //use the local, because its mor performant
|
|
|
|
this.lseconds = millis.seconds;
|
|
|
|
this.lminutes = millis.minutes;
|
|
|
|
this.lhours = millis.hours;
|
|
|
|
this.calcMillis();
|
|
|
|
}
|
2017-06-20 12:34:08 +00:00
|
|
|
else if (Number.isInteger(Number(millis.milliseconds))) {
|
2017-06-20 11:01:30 +00:00
|
|
|
this.milliseconds = millis.milliseconds;
|
|
|
|
}
|
2017-06-20 12:34:08 +00:00
|
|
|
else if (Number.isInteger(Number(millis.lmilliseconds))) {
|
|
|
|
this.milliseconds = millis.lmilliseconds;
|
|
|
|
}
|
2017-06-20 11:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
get milliseconds() {
|
|
|
|
return this.lmilliseconds;
|
|
|
|
}
|
|
|
|
set milliseconds(val) {
|
|
|
|
this.lmilliseconds = val;
|
|
|
|
this.millisToFrame();
|
|
|
|
}
|
|
|
|
get frame() {
|
|
|
|
return this.lframe;
|
|
|
|
}
|
|
|
|
set frame(val) {
|
|
|
|
this.lframe = val;
|
|
|
|
this.calcMillis();
|
|
|
|
}
|
|
|
|
get seconds() {
|
|
|
|
return this.lseconds;
|
|
|
|
}
|
|
|
|
set seconds(val) {
|
|
|
|
this.lseconds = val;
|
|
|
|
this.calcMillis();
|
|
|
|
}
|
|
|
|
get minutes() {
|
|
|
|
return this.lminutes;
|
|
|
|
}
|
|
|
|
set minutes(val) {
|
|
|
|
this.lminutes = val;
|
|
|
|
this.calcMillis();
|
|
|
|
}
|
|
|
|
get hours() {
|
|
|
|
return this.lhours;
|
|
|
|
}
|
|
|
|
set hours(val) {
|
|
|
|
this.lhours = val;
|
|
|
|
this.calcMillis();
|
|
|
|
}
|
|
|
|
calcMillis() {
|
|
|
|
this.lmilliseconds = (this.frame / 25 * 1000) + this.seconds * 1000 + this.minutes * 60 * 1000 + this.hours * 60 * 60 * 1000;
|
|
|
|
}
|
|
|
|
millisToFrame() {
|
|
|
|
var millis = this.lmilliseconds;
|
|
|
|
this.frame = Math.floor((millis % 1000) * 25 / 1000);
|
|
|
|
this.seconds = Math.floor((millis / 1000) % 60);
|
|
|
|
this.minutes = Math.floor((millis / (1000 * 60)) % 60);
|
|
|
|
this.hours = Math.floor((millis / (1000 * 60 * 60)) % 24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class Timecode {
|
|
|
|
vlcUpdate(vlc) {
|
|
|
|
if (!vlc.is_playing) {
|
|
|
|
this.stop();
|
2017-06-20 12:34:08 +00:00
|
|
|
return;
|
2017-06-20 11:01:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.startTime = new Date().getTime() - vlc.time;
|
|
|
|
this.filePlaying = vlc.file;
|
|
|
|
}
|
2017-06-20 12:34:08 +00:00
|
|
|
var d = vlc.time;
|
|
|
|
this.toTrigger.forEach(e => {
|
|
|
|
if (e.time.milliseconds <= d) {
|
|
|
|
console.log(e);
|
|
|
|
ios.emit("artnet", e.art);
|
2017-06-20 12:51:04 +00:00
|
|
|
artnet.set(universe, e.art.subnet, e.art.value);
|
2017-06-20 12:34:08 +00:00
|
|
|
var i = this.toTrigger.indexOf(e);
|
|
|
|
this.toTrigger.splice(i, 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var data = {};
|
|
|
|
data.time = new FrameTime(d);
|
|
|
|
data.track = this.activeTrack.id;
|
|
|
|
data.filePlaying = this.filePlaying;
|
|
|
|
ios.emit("update", data);
|
2017-06-20 11:01:30 +00:00
|
|
|
}
|
|
|
|
update(time) {
|
|
|
|
var d = new Date().getTime() - this.startTime;
|
|
|
|
this.toTrigger.forEach(e => {
|
|
|
|
if (e.time.milliseconds <= d) {
|
|
|
|
console.log(e);
|
|
|
|
ios.emit("artnet", e.art);
|
|
|
|
artnet.set(universe, e.art.subnet);
|
|
|
|
var i = this.toTrigger.indexOf(e);
|
|
|
|
this.toTrigger.splice(i, 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var data = {};
|
|
|
|
data.time = new FrameTime(d);
|
|
|
|
data.track = this.activeTrack.id;
|
|
|
|
data.filePlaying = this.filePlaying;
|
|
|
|
ios.emit("update", data);
|
|
|
|
}
|
|
|
|
loadTrack(trackid) {
|
|
|
|
this.playing = false;
|
2017-06-20 12:52:25 +00:00
|
|
|
for (var i = 0; i < 512; i++) {
|
|
|
|
artnet.set(universe, i, 0);
|
|
|
|
}
|
2017-06-20 11:01:30 +00:00
|
|
|
var track = db.get(trackid).value();
|
|
|
|
var tr = [];
|
|
|
|
track.triggers.forEach(t => {
|
|
|
|
var te = new TrackEvent();
|
|
|
|
te.art = t.art;
|
|
|
|
te.id = t.id;
|
|
|
|
te.time = new FrameTime(t.time);
|
|
|
|
tr.push(te);
|
|
|
|
});
|
|
|
|
this.activeTrack = track;
|
|
|
|
this.toTrigger = tr;
|
|
|
|
this.filePlaying = track.file;
|
|
|
|
this.startTime = 0;
|
|
|
|
}
|
|
|
|
play() {
|
|
|
|
if (!this.activeTrack)
|
|
|
|
return;
|
|
|
|
if (this.filePlaying) {
|
|
|
|
ioClient.emit("play", { file: this.filePlaying });
|
|
|
|
ioClient.on("playing", () => {
|
|
|
|
this.startTime = new Date().getTime();
|
2017-06-20 12:34:08 +00:00
|
|
|
//this.interval = setInterval(this.update.bind(this), 1000/25);
|
2017-06-20 11:58:01 +00:00
|
|
|
this.playing = true;
|
2017-06-20 11:01:30 +00:00
|
|
|
});
|
|
|
|
ioClient.on("update", (data) => {
|
|
|
|
this.vlcUpdate(data);
|
|
|
|
});
|
2017-06-20 11:56:26 +00:00
|
|
|
this.playing = false;
|
2017-06-20 11:01:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.startTime = new Date().getTime();
|
|
|
|
this.playing = true;
|
2017-06-20 11:58:01 +00:00
|
|
|
this.interval = setInterval(this.update.bind(this), 1000 / 25);
|
2017-06-20 11:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stop() {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
this.playing = false;
|
|
|
|
if (this.filePlaying) {
|
|
|
|
ioClient.emit("stop");
|
|
|
|
ios.emit("stopped");
|
|
|
|
}
|
|
|
|
this.startTime = 0;
|
2017-06-20 12:39:17 +00:00
|
|
|
ioClient.on("stopped", () => {
|
|
|
|
this.playing = false;
|
|
|
|
this.startTime = 0;
|
|
|
|
});
|
2017-06-20 11:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
class TrackEvent {
|
|
|
|
}
|
|
|
|
class Track {
|
|
|
|
}
|
|
|
|
var timecode = new Timecode();
|
|
|
|
ios.on("connection", socket => {
|
|
|
|
senddata();
|
|
|
|
function senddata() {
|
|
|
|
ios.emit("get_data", {
|
|
|
|
tracks: db.getState(),
|
|
|
|
loaded: timecode.activeTrack
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.log("Client", socket.id, "connected");
|
|
|
|
socket.on("load", (id) => {
|
|
|
|
var track = db.get(id).value();
|
|
|
|
if (track === undefined) {
|
|
|
|
return socket.emit("play_error", "Track doesn't exist");
|
|
|
|
}
|
|
|
|
timecode.loadTrack(id);
|
|
|
|
senddata();
|
|
|
|
});
|
|
|
|
socket.on("play", () => {
|
|
|
|
if (timecode.playing) {
|
|
|
|
return socket.emit("play_error", "the active track must be stoppen before re-plaing it");
|
|
|
|
}
|
|
|
|
timecode.play();
|
|
|
|
});
|
|
|
|
socket.on("stop", () => {
|
|
|
|
timecode.stop();
|
2017-06-20 12:34:08 +00:00
|
|
|
if (timecode.activeTrack)
|
|
|
|
timecode.loadTrack(timecode.activeTrack.id);
|
2017-06-20 11:01:30 +00:00
|
|
|
});
|
|
|
|
socket.on("create", (data) => {
|
|
|
|
var track = new Track();
|
|
|
|
track.displayName = data.name;
|
|
|
|
track.file = data.file;
|
|
|
|
track.id = sid.generate();
|
|
|
|
track.triggers = [];
|
|
|
|
db.set(track.id, track).write();
|
|
|
|
socket.emit("created", track);
|
|
|
|
senddata();
|
|
|
|
});
|
|
|
|
socket.on("set", (data) => {
|
|
|
|
var tid = data.track;
|
|
|
|
var time = new FrameTime(data.time);
|
|
|
|
if (data.id && data.id != "") {
|
|
|
|
var tracke = db.get(tid + ".triggers").find({ id: data.id }).assign({
|
|
|
|
art: data.art,
|
|
|
|
time: time,
|
|
|
|
}).write();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var tracke = new TrackEvent();
|
|
|
|
tracke.art = data.art;
|
|
|
|
tracke.time = time;
|
|
|
|
tracke.id = sid.generate();
|
|
|
|
db.get(tid + ".triggers").push(tracke).write();
|
|
|
|
}
|
|
|
|
senddata();
|
|
|
|
});
|
|
|
|
socket.on("get", () => {
|
|
|
|
senddata();
|
|
|
|
});
|
|
|
|
socket.on("delete", (data) => {
|
|
|
|
db.get(data.track + ".triggers").remove({ id: data.id }).write();
|
|
|
|
senddata();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
http.listen(5001);
|
2017-06-18 11:45:29 +00:00
|
|
|
//# sourceMappingURL=timec.js.map
|