Skip to content

Instantly share code, notes, and snippets.

@that0n3guy
Created October 28, 2021 03:06
Show Gist options
  • Save that0n3guy/ebcb574407183964195b26c9508e13a6 to your computer and use it in GitHub Desktop.
Save that0n3guy/ebcb574407183964195b26c9508e13a6 to your computer and use it in GitHub Desktop.
Electronjs R10 with GSPro - basic start and connection
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('path')
const url = require('url')
// const WebSocket = require('ws')
// const wss = new WebSocket.Server({ port: 2483 });
var net = require('net');
var client = new net.Socket();
client.connect(0921, '127.0.0.1'); // need an error for this if gspro isn't working..... not sure how to do it.
const gspro = {
DeviceID:"Garmin R10",
Units:"Yards",
ShotNumber:1,
ShotDataOptions:{
ContainsBallData:true,
ContainsClubData:false,
},
ClubData:{
Speed:1,
AngleOfAttack:1,
FaceToTarget:1,
Lie:1,
Loft:1,
Path:1,
SpeedAtImpact:1,
VerticalFaceImpact:1,
HorizontalFaceImpact:1,
ClosureRate:1,
},
BallData: {
Speed:1,
SpinAxis:1,
TotalSpin:1,
HLA:1,
VLA:1,
// CarryDistance:1,
}
};
console.log(JSON.stringify(gspro))
// https://gist.github.com/tedmiston/5935757
var server = net.createServer(function(socket) {
socket.on('error', function(err) {
console.log(err)
})
client.on('data', function(data) {
console.log('Client Received: ' + data);
});
socket.on('data', function(data) {
console.log('DATA: ' + data);
var dataArray = JSON.parse(data); //this might be an object... I don't know javascript syntax.
if(dataArray['Type'] === 'Handshake' || dataArray['Type'] === 'Handshake'){
// the challenge and response isn't needed....just say success
// var handshake = '{"Challenge":"gQW3om37uK4OOU4FXQH9GWgljxOrNcL5MvubVHAtQC0x6Z1AwJTgAIKyamJJMzm9","E6Version":"2, 0, 0, 0","ProtocolVersion":"1.0.0.5","RequiredProtocolVersion":"1.0.0.0","Type":"Handshake"}';
// socket.write(handshake);
// doing this instead of challenge code...
var success = '{"Success":"true","Type":"Authentication"}';
socket.write(success);
}
if(dataArray['Type'] === 'SetClubType'){
//ok... GSPro doens't have this on their api
// from r10: {"Type":"SetClubType","ClubType":"7Iron"}
//example response '{"Details":"Success.","SubType":"SetClubType","Type":"ACK"}';
// client.write('{"Type":"fun","doyou":"wantfun"}');
// client2.write('{"BallData":{"BallSpeed":103.35501,"LaunchAngle":17.773596,"LaunchDirection":-5.006505,"SpinAxis":353.39822,"TotalSpin":4721.594},"Type":"SetBallData"}');
}
if(dataArray['Type'] === 'fun'){
//ok... GSPro doens't have this on their api
// from r10: {"Type":"SetClubType","ClubType":"7Iron"}
//example response '{"Details":"Success.","SubType":"SetClubType","Type":"ACK"}';
socket.write('server: yes');
}
if(dataArray['Type'] === 'SetClubType'){
// '{"BallData":{"BallSpeed":103.35501,"LaunchAngle":17.773596,"LaunchDirection":-5.006505,"SpinAxis":353.39822,"TotalSpin":4721.594},"Type":"SetBallData"}'
dataArray = JSON.parse('{"BallData":{"BallSpeed":103.35501,"LaunchAngle":17.773596,"LaunchDirection":-5.006505,"SpinAxis":353.39822,"TotalSpin":4721.594},"Type":"SetBallData"}');
var gsprodata = gspro;
gsprodata.BallData.Speed = dataArray.BallData.BallSpeed;
gsprodata.BallData.SpinAxis = dataArray.BallData.SpinAxis;
gsprodata.BallData.TotalSpin = dataArray.BallData.TotalSpin;
gsprodata.BallData.HLA = dataArray.BallData.LaunchDirection;
gsprodata.BallData.VLA = dataArray.BallData.LaunchAngle;
client.write(JSON.stringify(gsprodata));
}
});
});
server.listen({
// host: '192.168.123.25', // if I set this.. localhost or 127.0.0.1 doesn't work. leave blank for 0.0.0.0 I think.
port: 2483,
exclusive: false
});
/* Or use this example tcp client written in node.js. (Originated with
example code from
http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html.) */
function sendDataToGspro(data) {
var net = require('net');
var client = new net.Socket();
client.connect(0921, 'localhost', function() {
console.log('Connected');
client.write('Hello, server! Love, Client.');
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
}
// wss.on('upgrade', function upgrade(request, socket, head) {
// const { pathname } = parse(request.url);
// console.log('received: %s', request);
// if (pathname === '/foo') {
// wss1.handleUpgrade(request, socket, head, function done(ws) {
// wss1.emit('connection', ws, request);
// });
// } else if (pathname === '/bar') {
// wss2.handleUpgrade(request, socket, head, function done(ws) {
// wss2.emit('connection', ws, request);
// });
// } else {
// socket.destroy();
// }
// });
// wss.on('open', function open() {
// // ws.send('something');
// console.log('open for here');
// });
// wss.on('connection', function connection(ws) {
// // console.log('received: %s', request);
// // console.log('received: %s', socket);
// // console.log('received: %s', head);
// ws.on('message', function incoming(message) {
// console.log('received: %s', message);
// });
// // ws.send('something');
// });
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
function setBallData(data) {
// ...
// '{"BallData":{"BallSpeed":103.35501,"LaunchAngle":17.773596,"LaunchDirection":-5.006505,"SpinAxis":353.39822,"TotalSpin":4721.594},"Type":"SetBallData"}'
var ballData = {
BallData: {
Speed:data['BallSpeed'],
SpinAxis:data['SpinAxis'],
TotalSpin:data['TotalSpin'],
HLA:data['LaunchDirection'],
VLA:data['LaunchAngle'],
}
}
}
function setClubData(data) {
// ...
// {"Type":"SetClubData","ClubData":{"ClubAngleFace":-2.421215295791626,"ClubHeadSpeed":75.21638488769531,"ClubAnglePath":-10.283570289611816}}
var clubData = {
ClubData:{
Speed:1,
AngleOfAttack:1,
FaceToTarget:1,
Lie:1,
Loft:1,
Path:data['ClubAnglePath'],
SpeedAtImpact:data['ClubHeadSpeed'],
VerticalFaceImpact:1,
HorizontalFaceImpact:1,
ClosureRate:1,
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment