@momo2555/koppeliajs 0.0.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +110 -0
  2. package/dist/components/DynamicCursor.svelte +0 -0
  3. package/dist/components/DynamicCursor.svelte.d.ts +26 -0
  4. package/dist/components/DynamicTextField.svelte +0 -0
  5. package/dist/components/DynamicTextField.svelte.d.ts +26 -0
  6. package/dist/components/GrowableElement.svelte +62 -0
  7. package/dist/components/GrowableElement.svelte.d.ts +29 -0
  8. package/dist/components/KBase.svelte +8 -0
  9. package/dist/components/KBase.svelte.d.ts +22 -0
  10. package/dist/components/KButton.svelte +0 -0
  11. package/dist/components/KButton.svelte.d.ts +26 -0
  12. package/dist/components/Koppelia.svelte +11 -0
  13. package/dist/components/Koppelia.svelte.d.ts +20 -0
  14. package/dist/components/ResizableText.svelte +0 -0
  15. package/dist/components/ResizableText.svelte.d.ts +26 -0
  16. package/dist/index.d.ts +11 -0
  17. package/dist/index.js +12 -0
  18. package/dist/scripts/console.d.ts +75 -0
  19. package/dist/scripts/console.js +180 -0
  20. package/dist/scripts/device.d.ts +49 -0
  21. package/dist/scripts/device.js +123 -0
  22. package/dist/scripts/game.d.ts +5 -0
  23. package/dist/scripts/game.js +8 -0
  24. package/dist/scripts/koppelia.d.ts +70 -0
  25. package/dist/scripts/koppelia.js +196 -0
  26. package/dist/scripts/koppeliaWebsocket.d.ts +77 -0
  27. package/dist/scripts/koppeliaWebsocket.js +159 -0
  28. package/dist/scripts/message.d.ts +116 -0
  29. package/dist/scripts/message.js +178 -0
  30. package/dist/scripts/play.d.ts +49 -0
  31. package/dist/scripts/play.js +107 -0
  32. package/dist/scripts/stage.d.ts +12 -0
  33. package/dist/scripts/stage.js +38 -0
  34. package/dist/scripts/state.d.ts +36 -0
  35. package/dist/scripts/state.js +82 -0
  36. package/dist/scripts/utils.d.ts +1 -0
  37. package/dist/scripts/utils.js +5 -0
  38. package/dist/server/index.d.ts +2 -0
  39. package/dist/server/index.js +2 -0
  40. package/dist/server/koppeliaServerApi.d.ts +7 -0
  41. package/dist/server/koppeliaServerApi.js +97 -0
  42. package/dist/stores/routeStore.d.ts +2 -0
  43. package/dist/stores/routeStore.js +21 -0
  44. package/dist/stores/stateStore.d.ts +1 -0
  45. package/dist/stores/stateStore.js +7 -0
  46. package/package.json +64 -0
package/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # create-svelte
2
+
3
+ Everything you need to build a Svelte library, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).
4
+
5
+ Read more about creating a library [in the docs](https://svelte.dev/docs/kit/packaging).
6
+
7
+ ## Creating a project
8
+
9
+ If you're seeing this, you've probably already done this step. Congrats!
10
+
11
+ ```bash
12
+ # create a new project in the current directory
13
+ npx sv create
14
+
15
+ # create a new project in my-app
16
+ npx sv create my-app
17
+ ```
18
+
19
+ ## Developing
20
+
21
+ Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
22
+
23
+ ```bash
24
+ npm run dev
25
+
26
+ # or start the server and open the app in a new browser tab
27
+ npm run dev -- --open
28
+ ```
29
+
30
+ Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
31
+
32
+ ## Building
33
+
34
+ To build your library:
35
+
36
+ ```bash
37
+ npm run package
38
+ ```
39
+
40
+ To create a production version of your showcase app:
41
+
42
+ ```bash
43
+ npm run build
44
+ ```
45
+
46
+ You can preview the production build with `npm run preview`.
47
+
48
+ > To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
49
+
50
+ ## Publishing
51
+
52
+ Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
53
+
54
+ To publish your library to [npm](https://www.npmjs.com):
55
+
56
+ ```bash
57
+ npm publish
58
+ ```
59
+
60
+ ```bash
61
+ npm install --save-dev @types/node
62
+ ```
63
+
64
+ # Features of Koppeliajs
65
+ ## Monitor and Controller
66
+ **Monitor**: This the screen shown on the tv
67
+
68
+ **Controller**: This is the screen shown on the smartphone (Koppeli'App)
69
+
70
+ KoppeliaJS provides tools
71
+
72
+ ## State management
73
+ Manage the state of the game, change easily the state, and get all updates from the server
74
+
75
+ ## Stage management
76
+ A stage is a part of the game (it is a page), when the stage changes, it changes for the controller and monitor
77
+
78
+ ## Device management
79
+ Manage the devices connected to the console.
80
+
81
+ ## Play management
82
+ Manage the plays of the game. You can create new plays for a specific game on KOppelia'App.
83
+
84
+ ## difficulty Cursor
85
+ Change the difficulty of the game in real time. The DifficultyCurser option should be activated on KoppeliaJS.
86
+
87
+ ## Growable Element
88
+ Growable elements are elements (generally text) that are shown on the monitor screen. Those elements can be expanded from the controller. They will take all the surface of the monitor screen.
89
+
90
+ ## Resizable Text
91
+ Resizable text are text shwon on monitor screen and the font size can be changed in real time from Koppeli'App
92
+
93
+ ## ScrollSyncer
94
+
95
+ ## StageButton
96
+
97
+ # Start a new project with Koppelia js
98
+
99
+ ## Create a new project with npm
100
+
101
+ ## Install sveltekit library
102
+
103
+ ## Install koppeliajs library
104
+
105
+ ## Minimum required files (file tree)
106
+
107
+ ## Run your game
108
+
109
+ ## dockerize
110
+
File without changes
@@ -0,0 +1,26 @@
1
+ export default DynamicCursor;
2
+ type DynamicCursor = SvelteComponent<{
3
+ [x: string]: never;
4
+ }, {
5
+ [evt: string]: CustomEvent<any>;
6
+ }, {}> & {
7
+ $$bindings?: string | undefined;
8
+ };
9
+ declare const DynamicCursor: $$__sveltets_2_IsomorphicComponent<{
10
+ [x: string]: never;
11
+ }, {
12
+ [evt: string]: CustomEvent<any>;
13
+ }, {}, {}, string>;
14
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
15
+ new (options: import("svelte").ComponentConstructorOptions<Props>): import("svelte").SvelteComponent<Props, Events, Slots> & {
16
+ $$bindings?: Bindings;
17
+ } & Exports;
18
+ (internal: unknown, props: {
19
+ $$events?: Events;
20
+ $$slots?: Slots;
21
+ }): Exports & {
22
+ $set?: any;
23
+ $on?: any;
24
+ };
25
+ z_$$bindings?: Bindings;
26
+ }
File without changes
@@ -0,0 +1,26 @@
1
+ export default DynamicTextField;
2
+ type DynamicTextField = SvelteComponent<{
3
+ [x: string]: never;
4
+ }, {
5
+ [evt: string]: CustomEvent<any>;
6
+ }, {}> & {
7
+ $$bindings?: string | undefined;
8
+ };
9
+ declare const DynamicTextField: $$__sveltets_2_IsomorphicComponent<{
10
+ [x: string]: never;
11
+ }, {
12
+ [evt: string]: CustomEvent<any>;
13
+ }, {}, {}, string>;
14
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
15
+ new (options: import("svelte").ComponentConstructorOptions<Props>): import("svelte").SvelteComponent<Props, Events, Slots> & {
16
+ $$bindings?: Bindings;
17
+ } & Exports;
18
+ (internal: unknown, props: {
19
+ $$events?: Events;
20
+ $$slots?: Slots;
21
+ }): Exports & {
22
+ $set?: any;
23
+ $on?: any;
24
+ };
25
+ z_$$bindings?: Bindings;
26
+ }
@@ -0,0 +1,62 @@
1
+ <script lang="ts">
2
+ import { routeType } from '../stores/routeStore.js';
3
+ import { Koppelia } from '../scripts/koppelia.js';
4
+
5
+ export let id: string = '';
6
+
7
+ $: grownClass = '';
8
+
9
+ let koppelia = Koppelia.instance;
10
+
11
+ koppelia.onReady(async () => {
12
+ await koppelia.registerNewGrowableElement(id, (grown: boolean) => {
13
+ grownClass = grown ? 'top-growable-grown' : '';
14
+ });
15
+ });
16
+
17
+ function onElementClick() {
18
+ koppelia.updateGrowableElement(id, true);
19
+ }
20
+
21
+ function onCloseGrown() {
22
+ koppelia.updateGrowableElement(id, false);
23
+ }
24
+ </script>
25
+
26
+ <button {id} on:click={onElementClick}>
27
+ <slot />
28
+ </button>
29
+
30
+ <div class="top-growable {grownClass}" id="top-growable-{id}">
31
+ {#if $routeType == 'controller'}
32
+ <button on:click={onCloseGrown} class="top-growable-close-button"> x </button>
33
+ {/if}
34
+
35
+ <slot />
36
+ </div>
37
+
38
+ <style>
39
+ .top-growable {
40
+ width: 90%;
41
+ height: 90%;
42
+ z-index: 99999;
43
+ position: absolute;
44
+ top: 5%;
45
+ left: 5%;
46
+ border: 1px solid #362a24;
47
+ background-color: antiquewhite;
48
+ margin: auto;
49
+ display: none;
50
+ }
51
+
52
+ .top-growable-close-button {
53
+ position: absolute;
54
+ left: 5px;
55
+ top: 5px;
56
+ color: #362a24;
57
+ }
58
+
59
+ .top-growable-grown {
60
+ display: flex;
61
+ }
62
+ </style>
@@ -0,0 +1,29 @@
1
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
2
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
3
+ $$bindings?: Bindings;
4
+ } & Exports;
5
+ (internal: unknown, props: Props & {
6
+ $$events?: Events;
7
+ $$slots?: Slots;
8
+ }): Exports & {
9
+ $set?: any;
10
+ $on?: any;
11
+ };
12
+ z_$$bindings?: Bindings;
13
+ }
14
+ type $$__sveltets_2_PropsWithChildren<Props, Slots> = Props & (Slots extends {
15
+ default: any;
16
+ } ? Props extends Record<string, never> ? any : {
17
+ children?: any;
18
+ } : {});
19
+ declare const GrowableElement: $$__sveltets_2_IsomorphicComponent<$$__sveltets_2_PropsWithChildren<{
20
+ id?: string;
21
+ }, {
22
+ default: {};
23
+ }>, {
24
+ [evt: string]: CustomEvent<any>;
25
+ }, {
26
+ default: {};
27
+ }, {}, string>;
28
+ type GrowableElement = InstanceType<typeof GrowableElement>;
29
+ export default GrowableElement;
@@ -0,0 +1,8 @@
1
+ <script lang="ts">
2
+ import { routeType } from '../stores/routeStore.js'
3
+ $: route = $routeType;
4
+
5
+ </script>
6
+
7
+ <slot {route}/>
8
+
@@ -0,0 +1,22 @@
1
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
2
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
3
+ $$bindings?: Bindings;
4
+ } & Exports;
5
+ (internal: unknown, props: {
6
+ $$events?: Events;
7
+ $$slots?: Slots;
8
+ }): Exports & {
9
+ $set?: any;
10
+ $on?: any;
11
+ };
12
+ z_$$bindings?: Bindings;
13
+ }
14
+ declare const KBase: $$__sveltets_2_IsomorphicComponent<any, {
15
+ [evt: string]: CustomEvent<any>;
16
+ }, {
17
+ default: {
18
+ route: string;
19
+ };
20
+ }, {}, string>;
21
+ type KBase = InstanceType<typeof KBase>;
22
+ export default KBase;
File without changes
@@ -0,0 +1,26 @@
1
+ export default KButton;
2
+ type KButton = SvelteComponent<{
3
+ [x: string]: never;
4
+ }, {
5
+ [evt: string]: CustomEvent<any>;
6
+ }, {}> & {
7
+ $$bindings?: string | undefined;
8
+ };
9
+ declare const KButton: $$__sveltets_2_IsomorphicComponent<{
10
+ [x: string]: never;
11
+ }, {
12
+ [evt: string]: CustomEvent<any>;
13
+ }, {}, {}, string>;
14
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
15
+ new (options: import("svelte").ComponentConstructorOptions<Props>): import("svelte").SvelteComponent<Props, Events, Slots> & {
16
+ $$bindings?: Bindings;
17
+ } & Exports;
18
+ (internal: unknown, props: {
19
+ $$events?: Events;
20
+ $$slots?: Slots;
21
+ }): Exports & {
22
+ $set?: any;
23
+ $on?: any;
24
+ };
25
+ z_$$bindings?: Bindings;
26
+ }
@@ -0,0 +1,11 @@
1
+ <script lang="ts">
2
+ import { routeType } from '../stores/routeStore.js';
3
+ </script>
4
+
5
+ <div class={$routeType}>
6
+ <slot />
7
+ </div>
8
+
9
+ <style>
10
+
11
+ </style>
@@ -0,0 +1,20 @@
1
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
2
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
3
+ $$bindings?: Bindings;
4
+ } & Exports;
5
+ (internal: unknown, props: {
6
+ $$events?: Events;
7
+ $$slots?: Slots;
8
+ }): Exports & {
9
+ $set?: any;
10
+ $on?: any;
11
+ };
12
+ z_$$bindings?: Bindings;
13
+ }
14
+ declare const Koppelia: $$__sveltets_2_IsomorphicComponent<any, {
15
+ [evt: string]: CustomEvent<any>;
16
+ }, {
17
+ default: {};
18
+ }, {}, string>;
19
+ type Koppelia = InstanceType<typeof Koppelia>;
20
+ export default Koppelia;
File without changes
@@ -0,0 +1,26 @@
1
+ export default ResizableText;
2
+ type ResizableText = SvelteComponent<{
3
+ [x: string]: never;
4
+ }, {
5
+ [evt: string]: CustomEvent<any>;
6
+ }, {}> & {
7
+ $$bindings?: string | undefined;
8
+ };
9
+ declare const ResizableText: $$__sveltets_2_IsomorphicComponent<{
10
+ [x: string]: never;
11
+ }, {
12
+ [evt: string]: CustomEvent<any>;
13
+ }, {}, {}, string>;
14
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
15
+ new (options: import("svelte").ComponentConstructorOptions<Props>): import("svelte").SvelteComponent<Props, Events, Slots> & {
16
+ $$bindings?: Bindings;
17
+ } & Exports;
18
+ (internal: unknown, props: {
19
+ $$events?: Events;
20
+ $$slots?: Slots;
21
+ }): Exports & {
22
+ $set?: any;
23
+ $on?: any;
24
+ };
25
+ z_$$bindings?: Bindings;
26
+ }
@@ -0,0 +1,11 @@
1
+ import KBase from "./components/KBase.svelte";
2
+ import GrowableElement from "./components/GrowableElement.svelte";
3
+ import { Koppelia } from "./scripts/koppelia.js";
4
+ import { Console } from "./scripts/console.js";
5
+ import { Message } from "./scripts/message.js";
6
+ import { Device } from "./scripts/device.js";
7
+ import { Play } from "./scripts/play.js";
8
+ import { updateRoute, routeType } from './stores/routeStore.js';
9
+ import { gameState } from "./stores/stateStore.js";
10
+ export { KBase };
11
+ export { updateRoute, routeType, Koppelia, Console, Message, Device, gameState, Play, GrowableElement };
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ // Reexport your entry components here
2
+ import KBase from "./components/KBase.svelte";
3
+ import GrowableElement from "./components/GrowableElement.svelte";
4
+ import { Koppelia } from "./scripts/koppelia.js";
5
+ import { Console } from "./scripts/console.js";
6
+ import { Message } from "./scripts/message.js";
7
+ import { Device } from "./scripts/device.js";
8
+ import { Play } from "./scripts/play.js";
9
+ import { updateRoute, routeType } from './stores/routeStore.js';
10
+ import { gameState } from "./stores/stateStore.js";
11
+ export { KBase }; // Compoenents
12
+ export { updateRoute, routeType, Koppelia, Console, Message, Device, gameState, Play, GrowableElement }; // libraries and stores
@@ -0,0 +1,75 @@
1
+ import { KoppeliaWebsocket, type Callback } from './koppeliaWebsocket.js';
2
+ import { Message, type MessageData, PeerType } from './message.js';
3
+ import type { AnyState } from './state.js';
4
+ export type ChangeStateCallback = (from: string, state: AnyState) => void;
5
+ export type ChangeStageCallback = (from: string, stage: string) => void;
6
+ export type DataExchangeCallback = (from: string, data: any) => void;
7
+ export type DeviceEventCallback = (device: string, from_addr: string, event: string) => void;
8
+ export type DeviceDataCallback = (device: string, from_addr: string, event: string) => void;
9
+ export type AnyRequestCallback = (request: string, params: {
10
+ [key: string]: any;
11
+ }, from: string, address: string) => void;
12
+ /**
13
+ * This class define the koppelia console, send and receive requests
14
+ */
15
+ export declare class Console {
16
+ consoleHostname: string;
17
+ consoleSocket: KoppeliaWebsocket;
18
+ private _changeStateHandlers;
19
+ private _changeStageHandlers;
20
+ private _deviceEventHandlers;
21
+ private _deviceDataHandlers;
22
+ private _dataExchangeHandlers;
23
+ private _anyRequestHandlers;
24
+ private _ready;
25
+ private _onReadyCallback;
26
+ constructor();
27
+ /**
28
+ * Sdnd a message to the console, set header.to to dispatch the message
29
+ * @param message
30
+ * @param callback the callback is called when websocket receives a response
31
+ */
32
+ sendMessage(message: Message, callback?: Callback): void;
33
+ /**
34
+ * Identify the page (controller or monitor) to the console
35
+ * @param peer
36
+ */
37
+ identify(peer: PeerType): void;
38
+ getConnectedDevices(): void;
39
+ /**
40
+ * Send data to a peer
41
+ * @param receiver
42
+ * @param data
43
+ */
44
+ sendDataTo(receiver: PeerType, data: MessageData): void;
45
+ /**
46
+ * Add a callback that will be executed when receive a new state from console
47
+ * @param callback
48
+ */
49
+ onStateChange(callback: ChangeStateCallback): void;
50
+ /**
51
+ * Add a callback that will be executed when the current stage has changed
52
+ * @param callback
53
+ */
54
+ onStageChange(callback: ChangeStageCallback): void;
55
+ /**
56
+ * Add a callback that be called when console is ready, if thec onsole is already ready
57
+ * Calls directly the function
58
+ * @param callback
59
+ */
60
+ onReady(callback: () => void): void;
61
+ onDeviceEvent(callback: () => void): void;
62
+ onRequest(callback: AnyRequestCallback): void;
63
+ /**
64
+ * Get if the connection with the console is ready
65
+ */
66
+ get ready(): boolean;
67
+ private _initEvents;
68
+ private _processReceivedData;
69
+ private _execDeviceDataHandlers;
70
+ private _execDeviceEventHandlers;
71
+ private _execChangeStateHandlers;
72
+ private _execChangeStageHandlers;
73
+ private _execDataExchangeHandlers;
74
+ private _execAnyRequestHandlers;
75
+ }
@@ -0,0 +1,180 @@
1
+ import { KoppeliaWebsocket } from './koppeliaWebsocket.js';
2
+ import { Message, PeerType, MessageType } from './message.js';
3
+ import { page } from '$app/stores';
4
+ import { get } from 'svelte/store';
5
+ import { routeType } from '../stores/routeStore.js';
6
+ const PORT = 2225;
7
+ /**
8
+ * This class define the koppelia console, send and receive requests
9
+ */
10
+ export class Console {
11
+ consoleHostname = "";
12
+ consoleSocket;
13
+ _changeStateHandlers;
14
+ _changeStageHandlers;
15
+ _deviceEventHandlers;
16
+ _deviceDataHandlers;
17
+ _dataExchangeHandlers;
18
+ _anyRequestHandlers;
19
+ _ready = false;
20
+ _onReadyCallback;
21
+ constructor() {
22
+ this.consoleHostname = get(page).url.hostname;
23
+ let consoleUrl = "ws://" + this.consoleHostname + ":" + PORT;
24
+ this.consoleSocket = new KoppeliaWebsocket(consoleUrl);
25
+ this._ready = false;
26
+ this._changeStateHandlers = [];
27
+ this._changeStageHandlers = [];
28
+ this._deviceEventHandlers = [];
29
+ this._deviceDataHandlers = [];
30
+ this._dataExchangeHandlers = [];
31
+ this._onReadyCallback = [];
32
+ this._anyRequestHandlers = [];
33
+ this._initEvents();
34
+ }
35
+ /**
36
+ * Sdnd a message to the console, set header.to to dispatch the message
37
+ * @param message
38
+ * @param callback the callback is called when websocket receives a response
39
+ */
40
+ sendMessage(message, callback) {
41
+ let route = PeerType.NONE;
42
+ if (get(routeType) == "monitor")
43
+ route = PeerType.MONITOR;
44
+ else if (get(routeType) == "controller")
45
+ route = PeerType.CONTROLLER;
46
+ message.setSource(route, "");
47
+ this.consoleSocket.send(message, callback);
48
+ }
49
+ /**
50
+ * Identify the page (controller or monitor) to the console
51
+ * @param peer
52
+ */
53
+ identify(peer) {
54
+ let req = new Message();
55
+ req.setIdentification(peer);
56
+ this.consoleSocket.send(req);
57
+ }
58
+ getConnectedDevices() {
59
+ }
60
+ /**
61
+ * Send data to a peer
62
+ * @param receiver
63
+ * @param data
64
+ */
65
+ sendDataTo(receiver, data) {
66
+ let req = new Message();
67
+ req.setData(data);
68
+ req.setDestination(receiver);
69
+ }
70
+ /**
71
+ * Add a callback that will be executed when receive a new state from console
72
+ * @param callback
73
+ */
74
+ onStateChange(callback) {
75
+ this._changeStateHandlers.push(callback);
76
+ }
77
+ /**
78
+ * Add a callback that will be executed when the current stage has changed
79
+ * @param callback
80
+ */
81
+ onStageChange(callback) {
82
+ this._changeStageHandlers.push(callback);
83
+ }
84
+ /**
85
+ * Add a callback that be called when console is ready, if thec onsole is already ready
86
+ * Calls directly the function
87
+ * @param callback
88
+ */
89
+ onReady(callback) {
90
+ if (!this._ready)
91
+ this._onReadyCallback.push(callback);
92
+ else
93
+ callback();
94
+ }
95
+ onDeviceEvent(callback) {
96
+ this._deviceEventHandlers.push(callback);
97
+ }
98
+ onRequest(callback) {
99
+ this._anyRequestHandlers.push(callback);
100
+ }
101
+ /**
102
+ * Get if the connection with the console is ready
103
+ */
104
+ get ready() {
105
+ return this._ready;
106
+ }
107
+ _initEvents() {
108
+ this.consoleSocket.onOpen(() => {
109
+ this._ready = true;
110
+ for (let callback of this._onReadyCallback) {
111
+ callback();
112
+ }
113
+ });
114
+ this.consoleSocket.onReceive((request) => {
115
+ this._processReceivedData(request);
116
+ });
117
+ }
118
+ _processReceivedData(request) {
119
+ console.log("Receive new data from console", request);
120
+ if (request.header.type === undefined)
121
+ return;
122
+ let type = request.header.type;
123
+ /* Handle specific requests */
124
+ if (type == MessageType.REQUEST) {
125
+ switch (request.request.exec) {
126
+ case "changeState":
127
+ this._execChangeStateHandlers(request.header.from, request.request.params.state);
128
+ break;
129
+ case "changeStage":
130
+ this._execChangeStageHandlers(request.header.from, request.request.params.stage);
131
+ break;
132
+ default:
133
+ this._execAnyRequestHandlers(request.request.exec, request.request.params, request.header.from, request.header.from_addr);
134
+ break;
135
+ }
136
+ }
137
+ /* Handle data exchange */
138
+ else if (type == MessageType.DATA_EXCHANGE) {
139
+ this._execDataExchangeHandlers(request.header.from, request.data);
140
+ }
141
+ /* Handle device event */
142
+ else if (type == MessageType.DEVICE_EVENT) {
143
+ this._execDeviceEventHandlers(request.header.device, request.header.from_addr, request.event);
144
+ }
145
+ /* Handle device data */
146
+ else if (type == MessageType.DEVICE_DATA) {
147
+ this._execDeviceDataHandlers(request.header.from_addr, request.data);
148
+ }
149
+ }
150
+ _execDeviceDataHandlers(from_addr, data) {
151
+ for (let handler of this._deviceDataHandlers) {
152
+ handler(from_addr, data);
153
+ }
154
+ }
155
+ _execDeviceEventHandlers(device, from_addr, event) {
156
+ for (let handler of this._deviceEventHandlers) {
157
+ handler(device, from_addr, event);
158
+ }
159
+ }
160
+ _execChangeStateHandlers(from, state) {
161
+ for (let handler of this._changeStateHandlers) {
162
+ handler(from, state);
163
+ }
164
+ }
165
+ _execChangeStageHandlers(from, stage) {
166
+ for (let handler of this._changeStageHandlers) {
167
+ handler(from, stage);
168
+ }
169
+ }
170
+ _execDataExchangeHandlers(from, data) {
171
+ for (let handler of this._dataExchangeHandlers) {
172
+ handler(from, data);
173
+ }
174
+ }
175
+ _execAnyRequestHandlers(request, params, from, address) {
176
+ for (let handler of this._anyRequestHandlers) {
177
+ handler(request, params, from, address);
178
+ }
179
+ }
180
+ }