1
- // const Client = require('socket.io-client');
2
- const TouchPortalAPI = require ( 'touchportal-api' ) ;
1
+ const TouchPortalAPI = require ( "touchportal-api" ) ;
3
2
const TPClient = new TouchPortalAPI . Client ( ) ;
4
- const pluginId = ' keysight' ;
5
- const express = require ( ' express' ) ;
3
+ const pluginId = " keysight" ;
4
+ const express = require ( " express" ) ;
6
5
const app = express ( ) ;
7
- const server = require ( 'http' ) . createServer ( app ) ;
8
6
9
- const Port = 3000 ;
7
+ // Create a new HTTP server
8
+ let server = require ( "http" ) . createServer ( app ) ;
10
9
11
- // TPClient.on("Settings", (data) => {
12
- // const portNumber = JSON.parse(data[0]) ;
10
+ // Current Port
11
+ let Port = 3000 ;
13
12
14
- // console.log(portNumber);
15
- // });
13
+ // Create a new Socket.IO instance
14
+ let io = require ( "socket.io" ) ( Port , server , { cors : { origin : "*" } } ) ;
16
15
17
- const io = require ( 'socket.io' ) ( Port , server , { cors : { origin : '*' } } ) ;
16
+ //#####################
17
+ //Functions
18
+ //#####################
18
19
19
- // const socket = io('ws://localhost:3000');
20
+ //Restart Websocket Server
21
+ function restartWebSocketServer ( ) {
22
+ // Close the existing server
23
+ try {
24
+ server . close ( ( ) => {
25
+ console . log ( "WebSocket server closed" ) ;
20
26
21
- // socket.on('connect', () => { console.log('Client connected'); });
27
+ // Create a new server
28
+ try {
29
+ server = require ( "http" ) . createServer ( app ) ;
30
+ } catch ( error ) {
31
+ console . log ( error ) ;
32
+ }
33
+ // Create a new Socket.IO instance
34
+ try {
35
+ io = require ( "socket.io" ) ( Port , server , { cors : { origin : "*" } } ) ;
36
+ console . log ( "WebSocket server restarted" ) ;
37
+ } catch ( error ) {
38
+ console . log ( error ) ;
39
+ }
40
+ } ) ;
41
+ } catch ( error ) {
42
+ console . log ( error ) ;
43
+ }
44
+ }
22
45
23
- TPClient . on ( "Action" , ( data ) => {
24
- if ( data . actionId == "send_keysight_command" ) {
25
- let command = data . data [ 0 ] . value ;
26
- console . log ( command + " was sent to Keysight" ) ;
46
+ //Change Port
47
+ function changePort ( newPort ) {
48
+ Port = newPort ;
49
+ console . log ( `Port changed to ${ Port } ` ) ;
50
+ restartWebSocketServer ( ) ;
51
+ }
27
52
28
- // Send the command to the SocketIO server
29
- io . emit ( 'command' , command ) ;
53
+ //#####################
54
+ //Settings
55
+ //#####################
56
+ TPClient . on ( "Settings" , ( data ) => {
57
+ const portNumber = parseInt ( data [ 0 ] . Port ) ;
58
+
59
+ if ( portNumber !== NaN ) {
60
+ if ( portNumber !== Port ) {
61
+ console . log ( `Change Port from ${ Port } to ${ portNumber } ` ) ;
62
+ changePort ( portNumber ) ;
30
63
}
31
- } ) ;
64
+ } else {
65
+ console . log ( "Port Number is not a number" ) ;
66
+ }
67
+ } ) ;
32
68
33
-
69
+ //#####################
70
+ //Actions
71
+ //#####################
72
+ TPClient . on ( "Action" , ( data ) => {
73
+ if ( data . actionId == "send_keysight_command" ) {
74
+ let command = data . data [ 0 ] . value ;
75
+ console . log ( command + " was sent to Keysight" ) ;
76
+ io . emit ( "command" , command ) ;
77
+ } else if ( data . actionId == "restart_keysight_server" ) {
78
+ restartWebSocketServer ( ) ;
79
+ }
80
+ } ) ;
34
81
82
+ //#####################
83
+ //Receive Messages from Keysight
84
+ //#####################
85
+ io . on ( "connection" , ( socket ) => {
86
+ socket . on ( "message" , ( message ) => {
87
+ console . log ( "Received message:" , message ) ;
88
+ } ) ;
89
+ } ) ;
35
90
36
- TPClient . connect ( { pluginId } ) ;
91
+ TPClient . connect ( { pluginId } ) ;
0 commit comments