Skip to content

Commit eb7b98a

Browse files
Merge pull request #13 from marcus-universe/dev
Update 3.0
2 parents 03c6f72 + 0817f95 commit eb7b98a

File tree

5 files changed

+93
-31
lines changed

5 files changed

+93
-31
lines changed
1.32 KB
Binary file not shown.

TouchPortalKeysight/entry.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"sdk": 1,
3-
"version": 2,
4-
"name": "keysight",
3+
"version": 3,
4+
"name": "Keysight",
55
"id": "keysight",
66
"configuration": {
77
"colorDark": "#029e84",
@@ -12,9 +12,7 @@
1212
"id": "keysightmessages",
1313
"name": "Keysight",
1414
"imagepath": "%TP_PLUGIN_FOLDER%TouchPortalKeysight\\img\\keysight_logo.png",
15-
"actions": [
16-
17-
{
15+
"actions": [{
1816
"id": "send_keysight_command",
1917
"prefix": "Send a command to Keysight",
2018
"name": "Send command to Keysight",
@@ -30,14 +28,23 @@
3028
"description": "The message to send to Keysight",
3129
"required": true
3230
}]
31+
},
32+
{
33+
"id": "restart_keysight_server",
34+
"prefix": "restarts the Keysight server",
35+
"name": "Restart_Keysight_Server",
36+
"type": "communicate",
37+
"tryInline": true,
38+
"format": "Keysight server restarted"
3339
}
40+
3441
]
3542
}],
3643
"settings": [{
3744
"name": "Port",
3845
"default": "3000",
3946
"type": "text",
40-
"maxLenght": 16,
47+
"maxLenght": 5,
4148
"isPassword": false,
4249
"minValue": 0,
4350
"maxValue": 120,

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"touchportal-api": "^3.2.1"
77
},
88
"name": "touchportal_keysightplugin",
9-
"version": "2.2.2",
9+
"version": "3.0.0",
1010
"description": "A plugin for touchportal to control websockets in keysight",
1111
"main": "src/index.js",
1212
"bin": "src/index.js",

src/index.js

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,91 @@
1-
// const Client = require('socket.io-client');
2-
const TouchPortalAPI = require('touchportal-api');
1+
const TouchPortalAPI = require("touchportal-api");
32
const TPClient = new TouchPortalAPI.Client();
4-
const pluginId = 'keysight';
5-
const express = require('express');
3+
const pluginId = "keysight";
4+
const express = require("express");
65
const app = express();
7-
const server = require('http').createServer(app);
86

9-
const Port = 3000;
7+
// Create a new HTTP server
8+
let server = require("http").createServer(app);
109

11-
// TPClient.on("Settings", (data) => {
12-
// const portNumber = JSON.parse(data[0]);
10+
// Current Port
11+
let Port = 3000;
1312

14-
// console.log(portNumber);
15-
// });
13+
// Create a new Socket.IO instance
14+
let io = require("socket.io")(Port, server, { cors: { origin: "*" } });
1615

17-
const io = require('socket.io')(Port, server, {cors: {origin: '*'}});
16+
//#####################
17+
//Functions
18+
//#####################
1819

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");
2026

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+
}
2245

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+
}
2752

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);
3063
}
31-
});
64+
} else {
65+
console.log("Port Number is not a number");
66+
}
67+
});
3268

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+
});
3481

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+
});
3590

36-
TPClient.connect({ pluginId });
91+
TPClient.connect({ pluginId });

0 commit comments

Comments
 (0)