// hiFiCalendarServer.js // // Created by Mark Brosche on 4/3/2019 // Copyright 2019 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html (function() { var FIVE_MINUTES_MS = 300000; var that; var intervalSchedule = false; this.remotelyCallable = [ "enteredMeetingZone", "leftMeetingZone", "updateSignColor", "addEvent" ]; this.preload = function(entityID) { that = this; that.entityID = entityID; that.entityProperties = Entities.getEntityProperties(that.entityID, ['id', 'name', 'type', 'parentID']); that.room = {}; if (that.entityProperties.name.indexOf("_SCHEDULE") === -1 && that.entityProperties.name.indexOf("_OCCUPANTS") === -1 && that.entityProperties.type === "Text") { that.room = { "id": that.entityProperties.id, "name": that.entityProperties.name, "eventList": [], "scheduleEntityID": that.entityProperties.parentID }; } else { that.room = { "id": that.entityProperties.id, "occupants": [] } return; } intervalSchedule = Script.setInterval(function(){ Messages.sendMessage("HiFi.GoogleCalendar", JSON.stringify({ type: "schedule request" })); }, FIVE_MINUTES_MS); }; this.addEvent = function(id, params) { if (id === that.entityID) { if (!params) { startTime = undefined; endTime = undefined; Entities.editEntity(that.room.scheduleEntityID, {text: "No events scheduled for this room"}); return; } else { var startTime = that.googleDateToUTCDate(params[1]); var endTime = that.googleDateToUTCDate(params[2]); var tempEvent = {summary: params[0], start: startTime, end: endTime}; var index = 0; that.room.eventList.forEach(function(event) { if ((new Date().getDate() - startTime.getDate() > 0) || (startTime - event.start === 0 && that.room.eventList.length > 0)) { index = -1; return; } else if (startTime - event.start > 0) { index++; } }); if (index > -1) { that.room.eventList.splice(index, 0, tempEvent); } } var printedSchedule = ''; if (that.room.eventList[0].start === "Invalid Date") { Entities.editEntity(that.room.scheduleEntityID, {text: "No events scheduled for this room"}); } else { that.room.eventList.forEach(function(event) { printedSchedule = printedSchedule + event.summary + ': (' + event.start.toLocaleDateString('en-US', {"timezone": "America/Los_Angeles", "hour12": true}) + ' ' + event.start.toLocaleTimeString('en-US', {"timezone": "America/Los_Angeles", "hour12": true}) + ' - ' + event.end.toLocaleTimeString('en-US', {"timezone": "America/Los_Angeles", "hour12": true}) + ') \n\n'; }); Entities.editEntity(that.room.scheduleEntityID, {text: printedSchedule}); } that.getBusy(); } }; this.removeEvent = function(event) { if (event > -1) { that.room.eventList.splice(event, 1)[0]; } }; // Taken from https://stackoverflow.com/a/11318669 var googleDate = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})([+-]\d{2}):(\d{2})$/; var MINS_PER_HOUR = 60; this.googleDateToUTCDate = function(d) { var m = googleDate.exec(d); var year = +m[1]; var month = +m[2]; var day = +m[3]; var hour = +m[4]; var minute = +m[5]; var second = +m[6]; var msec = +m[7]; var tzHour = +m[8]; var tzMin = +m[9]; var tzOffset = new Date().getTimezoneOffset() + tzHour * MINS_PER_HOUR + tzMin; return new Date(year, month - 1, day, hour, minute - tzOffset, second, msec); }; this.getBusy = function() { var now = Date.now(); if (typeof that.room.eventList[0] === "object") { console.log(that.room.name, " end: ",now,new Date(that.room.eventList[0].end), now - new Date(that.room.eventList[0].end)," start: ", now - new Date(that.room.eventList[0].start)); if (now - new Date(that.room.eventList[0].end) > 0 && now - new Date(that.room.eventList[0].start) <= 0) { that.updateSignColor(that.room.id, [that.room.name, "RED"]); } else { that.updateSignColor(that.room.id, [that.room.name, "GREEN"]); } } else { Entities.editEntity(that.room.scheduleEntityID, {text: "No events scheduled for this room"}); that.updateSignColor(that.room.id, [that.room.name, "GREEN"]); } }; this.enteredMeetingZone = function(id, params) { if (that.entityID === id) { var displayName = params[0]; console.log("User with display name " + displayName + " entered the meeting zone for " + that.room.name); if (that.room.occupants.indexOf(displayName) === -1) { that.room.occupants.push(displayName); } Entities.editEntity(id, { "text": (that.room.occupants).join("\n"), "textColor": [255, 255, 255] }); } }; this.leftMeetingZone = function(id, params) { if (that.entityID === id) { var displayName = params[0]; console.log("User with display name " + displayName + " left the meeting zone for " + that.room.name); var index = that.room.occupants.indexOf(displayName); if (index > -1) { that.room.occupants.splice(index, 1); } Entities.editEntity(id, { "text": (that.room.occupants).join("\n"), "textColor": [255, 255, 255] }); } }; this.updateSignColor = function(id, params) { if (that.entityID === id) { if (params[1] === "GREEN") { Entities.editEntity(id, { "text": params[0], "textColor": [0,0,0], "textAlpha": 1, "backgroundColor": [125, 255, 125] }); } else { Entities.editEntity(id, { "text": params[0], "textColor": [0,0,0], "textAlpha": 1, "backgroundColor": [255, 125, 125] }); } } }; this.unload = function() { if (intervalSchedule) { Script.clearInterval(intervalSchedule); intervalSchedule = false; } }; });