22parent : OpenGolfSim Desktop
33title : Plugins
44nav_order : 8
5- published : false
65---
76
87# Plugins
@@ -13,6 +12,10 @@ You can extend OpenGolfSim by writing your own custom plugins, which run within
1312{: toc }
1413
1514
15+ ## SDK Documentation
16+
17+ You can browse our full [ Plugin SDK Documentation] ( /apis/plugins/ ) here.
18+
1619## Install Plugins
1720
1821To install an new plugin, copy the provided plugin to your OpenGolfSim ` plugins/ ` folder.
@@ -45,14 +48,73 @@ To create a new custom plugin, create a new folder in the OpenGolfSim `plugins/`
4548
4649 ``` json
4750 {
48- "name" : " OpenConnect API" ,
51+ "name" : " Connector API" ,
4952 "version" : " 1.0.0" ,
50- "description" : " Creates an OpenConnect v1 API server to allow connectors to send shot data over TCP" ,
51- "main" : " index.js" ,
53+ "description" : " Creates a simple TCP server to allow connectors to send and receive shot data" ,
5254 "plugin" : {
5355 "type" : " launch"
5456 }
5557 }
5658 ```
5759
58- 1 . `index.js` - This file will contain your javascript plugin code. When your plugin is started we'll execute the code in this file.
60+ 1 . `index.js` - This file will contain all your javascript plugin code. When your plugin is started we'll execute the code in this file.
61+
62+
63+ ```javascript
64+ const PORT = 3921;
65+
66+ const device = { isConnected: false, isReady: false };
67+
68+
69+ const serverCallback = (socket) => {
70+ // A TCP client has connected
71+ device.isConnected = true;
72+ launchMonitor.updateDeviceStatus(device);
73+
74+ socket.on('data', (data) => {
75+ // A message has been received
76+ console.log(`Socket data received`);
77+ try {
78+ // Parse the JSON payload
79+ const obj = JSON.parse(data);
80+ if (obj.type === 'device') {
81+ // Set the status to ready
82+ launchMonitor.setReady(obj.status === 'ready');
83+ } else if (obj.type === 'shot') {
84+ // Send the shot data in OpenGolfSim format
85+ launchMonitor.sendShot({
86+ ballSpeed: obj.shot.speed,
87+ verticalLaunchAngle: obj.shot.vla,
88+ horizontalLaunchAngle: obj.shot.hla,
89+ spinSpeed: obj.shot.totalspin,
90+ spinAxis: obj.shot.spinaxis
91+ });
92+ }
93+
94+ } catch (error) {
95+ console.error(error);
96+ }
97+ });
98+
99+ socket.on('end', () => {
100+ console.log(`Socket ended`);
101+ device.isConnected = false;
102+ launchMonitor.updateDeviceStatus(device);
103+ });
104+
105+ socket.on('error', (err) => {
106+ console.error(`Socket error: ${err}`);
107+ });
108+ };
109+
110+ const server = network.createServer(serverCallback);
111+
112+ server.on('close', () => {
113+ console.log('TCP server closed');
114+ });
115+
116+ console.log(`Starting TCP server...`);
117+ server.listen(PORT, () => {
118+ console.log(`TCP server listening at 127.0.0.1:${PORT}`);
119+ });
120+ ```
0 commit comments