From 402540c78114ff51745e8f9ee152de6ee7a3b172 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 6 Sep 2019 08:33:39 +0200 Subject: [PATCH] System Event Handler Working --- CockpitSpace/RestartSimulation.js | 24 ++++-- Dashboard.js | 44 +++++----- Globals.js | 1 + SystemEventHandler.js | 129 ++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+), 26 deletions(-) create mode 100644 SystemEventHandler.js diff --git a/CockpitSpace/RestartSimulation.js b/CockpitSpace/RestartSimulation.js index 451c4f4ac..7d1ebab5e 100644 --- a/CockpitSpace/RestartSimulation.js +++ b/CockpitSpace/RestartSimulation.js @@ -35,6 +35,7 @@ function newRestartSimulation () { let executionFocusExists = false let idleLabel = '' + let date = '' return thisObject @@ -56,6 +57,8 @@ function newRestartSimulation () { selfMouseNotOverEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onMouseNotOver', onMouseNotOver) thisObject.status = 'Ready' + + systemEventHandler.createEventHandler('Cockpit-Restart-Simulation') } function getContainer (point, purpose) { @@ -86,6 +89,7 @@ function newRestartSimulation () { async function restart () { try { + /* Processing the restart */ thisObject.status = 'Saving' let result = await canvas.strategySpace.strategizerGateway.saveToStrategyzer(getSimulationParams()) if (result === true) { @@ -100,18 +104,20 @@ function newRestartSimulation () { } } } - callServer('', 'RestartCloneExecutor', onSaved) - function onSaved (err) { - if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) { - logger.write('[INFO] Restart Simulation -> Clone Executor Restarted') - } else { - logger.write('[ERROR] Restart Simulation -> Can not restart Clone Executor. err = ' + err.messsage) - } + + systemEventHandler.raiseEvent('Cockpit-Restart-Simulation', 'Simulation Started') + + /* We will start listening to the event that is triggered when the simulation process finishes processing one day. */ + systemEventHandler.listenToEvent('Jason-Multi-Period', 'Status Report Updated', undefined, undefined, undefined, onEvent) + + function onEvent (message) { + date = message.event.lastProcessedDay } } else { thisObject.status = 'Restarting' await graphQlRestartSimulation(simulationParams) } + thisObject.status = 'Calculating' counterTillNextState = 500 } else { @@ -326,6 +332,10 @@ function newRestartSimulation () { } labelPoint = thisObject.container.frame.frameThisPoint(labelPoint) + if (date !== '') { + label = date + } + browserCanvasContext.fillStyle = 'rgba(' + UI_COLOR.WHITE + ', ' + OPACITY + ')' browserCanvasContext.fillText(label, labelPoint.x, labelPoint.y) } diff --git a/Dashboard.js b/Dashboard.js index 4811d2b5a..ac0507ce8 100644 --- a/Dashboard.js +++ b/Dashboard.js @@ -1,6 +1,7 @@ let canvas let markets let ecosystem = newEcosystem() +let systemEventHandler let viewPort try { @@ -32,29 +33,34 @@ function newDashboard () { function start () { try { - /* If this method is executed for a second time, it should finalize the current execution structure */ + systemEventHandler = newSystemEventHandler() + systemEventHandler.initialize(startCanvas) - if (canvas !== undefined) { canvas.finalize() } + function startCanvas () { + /* If this method is executed for a second time, it should finalize the current execution structure */ - /* Here we check where we are executing. In case we are Local we need to create a Local Dummy User and Team. */ + if (canvas !== undefined) { canvas.finalize() } - if (window.canvasApp.executingAt === 'Local') { - window.localStorage.setItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY, 'Local Access Token') // Dummy Access Token - window.localStorage.setItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY, '{"authId":"x|x","alias":"user"}') // Dummy Local User + /* Here we check where we are executing. In case we are Local we need to create a Local Dummy User and Team. */ + + if (window.canvasApp.executingAt === 'Local') { + window.localStorage.setItem(LOGGED_IN_ACCESS_TOKEN_LOCAL_STORAGE_KEY, 'Local Access Token') // Dummy Access Token + window.localStorage.setItem(LOGGED_IN_USER_LOCAL_STORAGE_KEY, '{"authId":"x|x","alias":"user"}') // Dummy Local User + } + + /* Here we will setup the global eventHandler that will enable the Canvas App to react to events happening outside its execution scope. */ + + window.canvasApp.eventHandler = newEventHandler() + userProfileChangedEventSubscriptionId = window.canvasApp.eventHandler.listenToEvent('User Profile Changed', userProfileChanged) + browserResizedEventSubscriptionId = window.canvasApp.eventHandler.listenToEvent('Browser Resized', browserResized) + + /* Here we used to have a call to the Teams Module to get the profile pictures. That was removed but to keep things working, we do this: */ + + window.canvasApp.context.teamProfileImages = new Map() + window.canvasApp.context.fbProfileImages = new Map() + + setTimeout(delayedStart, DEBUG_START_UP_DELAY) } - - /* Here we will setup the global eventHandler that will enable the Canvas App to react to events happening outside its execution scope. */ - - window.canvasApp.eventHandler = newEventHandler() - userProfileChangedEventSubscriptionId = window.canvasApp.eventHandler.listenToEvent('User Profile Changed', userProfileChanged) - browserResizedEventSubscriptionId = window.canvasApp.eventHandler.listenToEvent('Browser Resized', browserResized) - - /* Here we used to have a call to the Teams Module to get the profile pictures. That was removed but to keep things working, we do this: */ - - window.canvasApp.context.teamProfileImages = new Map() - window.canvasApp.context.fbProfileImages = new Map() - - setTimeout(delayedStart, DEBUG_START_UP_DELAY) } catch (err) { if (ERROR_LOG === true) { logger.write('[ERROR] start -> err = ' + err.stack) } } diff --git a/Globals.js b/Globals.js index 9bae5463f..4e09972ee 100644 --- a/Globals.js +++ b/Globals.js @@ -1,3 +1,4 @@ + /* User Interface Colors */ const STORAGE_PROVIDER = 'AAWeb'; diff --git a/SystemEventHandler.js b/SystemEventHandler.js new file mode 100644 index 000000000..92504e60e --- /dev/null +++ b/SystemEventHandler.js @@ -0,0 +1,129 @@ + +function newSystemEventHandler () { + /* Web Sockets Connection */ + + const MODULE_NAME = 'System Event Handler' + const ERROR_LOG = true + const logger = newWebDebugLog() + logger.fileName = MODULE_NAME + + const WEB_SOCKETS_URL = 'ws://localhost:8080' + let WEB_SOCKETS_CONNECTION + + let thisObject = { + initialize: initialize, + createEventHandler: createEventHandler, + deleteEventHandler: deleteEventHandler, + listenToEvent: listenToEvent, + stopListening: stopListening, + raiseEvent: raiseEvent + } + + let eventListeners = new Map() + let responseWaiters = new Map() + + return thisObject + + function initialize (callBackFunction) { + setuptWebSockets(callBackFunction) + } + + function sendCommand (command, responseCallBack, eventsCallBack) { + if (command.action === 'listenToEvent') { + eventListeners.set(command.eventHandlerName + '-' + command.eventType, eventsCallBack) + } + if (command.callerId && responseCallBack) { + responseWaiters.set(command.callerId, responseCallBack) + } + + WEB_SOCKETS_CONNECTION.send(JSON.stringify(command)) + } + + function createEventHandler (eventHandlerName, callerId, responseCallBack) { + let eventCommand = { + action: 'createEventHandler', + eventHandlerName: eventHandlerName, + callerId: callerId + } + sendCommand(eventCommand, responseCallBack) + } + + function deleteEventHandler (eventHandlerName, callerId, responseCallBack) { + let eventCommand = { + action: 'deleteEventHandler', + eventHandlerName: eventHandlerName, + callerId: callerId + } + sendCommand(eventCommand, responseCallBack) + } + + function listenToEvent (eventHandlerName, eventType, extraData, callerId, responseCallBack, eventsCallBack) { + let eventCommand = { + action: 'listenToEvent', + eventHandlerName: eventHandlerName, + eventType: eventType, + extraData: extraData, + callerId: callerId + } + sendCommand(eventCommand, responseCallBack, eventsCallBack) + } + + function stopListening (eventHandlerName, eventSubscriptionId, callerId, responseCallBack) { + let eventCommand = { + action: 'stopListening', + eventHandlerName: eventHandlerName, + eventSubscriptionId: eventSubscriptionId, + callerId: callerId + } + sendCommand(eventCommand, responseCallBack) + } + + function raiseEvent (eventHandlerName, eventType, event, callerId, responseCallBack) { + let eventCommand = { + action: 'raiseEvent', + eventHandlerName: eventHandlerName, + eventType: eventType, + event: event, + callerId: callerId + } + sendCommand(eventCommand, responseCallBack) + } + + function setuptWebSockets (callBackFunction) { + try { + WEB_SOCKETS_CONNECTION = new WebSocket(WEB_SOCKETS_URL) + WEB_SOCKETS_CONNECTION.onopen = () => { + console.log('Websocket connection opened.') + } + WEB_SOCKETS_CONNECTION.onerror = error => { + console.log('WebSocket error:' + JSON.stringify(error)) + } + WEB_SOCKETS_CONNECTION.onopen = () => { + callBackFunction() + } + WEB_SOCKETS_CONNECTION.onmessage = e => { + console.log('Websoked Message Received: ' + e.data) + + let message = JSON.parse(e.data) + + if (message.action === 'Event Raised') { + let handler = eventListeners.get(message.eventHandlerName + '-' + message.eventType) + if (handler) { + handler(message) + } + return + } + + if (message.action === 'Event Server Response') { + let handler = responseWaiters.get(message.callerId) + if (handler) { + handler(message) + } + return + } + } + } catch (err) { + if (ERROR_LOG === true) { logger.write('[ERROR] setuptWebSockets -> err = ' + err.stack) } + } + } +}