initial commit

This commit is contained in:
José Conde
2023-02-06 21:59:24 +01:00
parent 29069f1e37
commit e7e3b33d2f
28 changed files with 978 additions and 125 deletions

11
electron/electron-env.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
/// <reference types="vite-plugin-electron/electron-env" />
declare namespace NodeJS {
interface ProcessEnv {
VSCODE_DEBUG?: 'true'
DIST_ELECTRON: string
DIST: string
/** /dist/ or /public/ */
PUBLIC: string
}
}

127
electron/main/index.ts Normal file
View File

@ -0,0 +1,127 @@
import { app, BrowserWindow, shell, ipcMain } from 'electron'
import { release } from 'node:os'
import { join } from 'node:path'
import { SimconnectClient } from '../simconnect/Client'
// The built directory structure
//
// ├─┬ dist-electron
// │ ├─┬ main
// │ │ └── index.js > Electron-Main
// │ └─┬ preload
// │ └── index.js > Preload-Scripts
// ├─┬ dist
// │ └── index.html > Electron-Renderer
//
process.env.DIST_ELECTRON = join(__dirname, '..')
process.env.DIST = join(process.env.DIST_ELECTRON, '../dist')
process.env.PUBLIC = process.env.VITE_DEV_SERVER_URL
? join(process.env.DIST_ELECTRON, '../public')
: process.env.DIST
// Disable GPU Acceleration for Windows 7
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
// Set application name for Windows 10+ notifications
if (process.platform === 'win32') app.setAppUserModelId(app.getName())
if (!app.requestSingleInstanceLock()) {
app.quit()
process.exit(0)
}
// Remove electron security warnings
// This warning only shows in development mode
// Read more on https://www.electronjs.org/docs/latest/tutorial/security
// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
let win: BrowserWindow | null = null
// Here, you can also use other preload
const preload = join(__dirname, '../preload/index.js')
const url = process.env.VITE_DEV_SERVER_URL
const indexHtml = join(process.env.DIST, 'index.html')
const client: SimconnectClient = new SimconnectClient();
async function createWindow() {
win = new BrowserWindow({
title: 'Main window',
icon: join(process.env.PUBLIC, 'favicon.ico'),
webPreferences: {
preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
// Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
nodeIntegration: true,
contextIsolation: false,
},
})
if (process.env.VITE_DEV_SERVER_URL) { // electron-vite-vue#298
win.loadURL(url)
// Open devTool if the app is not packaged
win.webContents.openDevTools()
} else {
win.loadFile(indexHtml)
}
// Test actively push message to the Electron-Renderer
win.webContents.on('did-finish-load', () => {
win?.webContents.send('main-process-message', new Date().toLocaleString());
try {
client.open(win);
} catch(err) {
console.log(err);
}
})
// Make all links open with the browser, not with the application
win.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('https:')) shell.openExternal(url)
return { action: 'deny' }
})
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
win = null
if (process.platform !== 'darwin') app.quit()
})
app.on('second-instance', () => {
if (win) {
// Focus on the main window if the user tried to open another
if (win.isMinimized()) win.restore()
win.focus()
}
})
app.on('activate', () => {
const allWindows = BrowserWindow.getAllWindows()
if (allWindows.length) {
allWindows[0].focus()
} else {
createWindow()
}
})
// New window example arg: new windows url
ipcMain.handle('open-win', (_, arg) => {
const childWindow = new BrowserWindow({
webPreferences: {
preload,
nodeIntegration: true,
contextIsolation: false,
},
})
if (process.env.VITE_DEV_SERVER_URL) {
childWindow.loadURL(`${url}#${arg}`)
} else {
childWindow.loadFile(indexHtml, { hash: arg })
}
})

92
electron/preload/index.ts Normal file
View File

@ -0,0 +1,92 @@
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
return new Promise((resolve) => {
if (condition.includes(document.readyState)) {
resolve(true)
} else {
document.addEventListener('readystatechange', () => {
if (condition.includes(document.readyState)) {
resolve(true)
}
})
}
})
}
const safeDOM = {
append(parent: HTMLElement, child: HTMLElement) {
if (!Array.from(parent.children).find(e => e === child)) {
return parent.appendChild(child)
}
},
remove(parent: HTMLElement, child: HTMLElement) {
if (Array.from(parent.children).find(e => e === child)) {
return parent.removeChild(child)
}
},
}
/**
* https://tobiasahlin.com/spinkit
* https://connoratherton.com/loaders
* https://projects.lukehaas.me/css-loaders
* https://matejkustec.github.io/SpinThatShit
*/
function useLoading() {
const className = `loaders-css__square-spin`
const styleContent = `
@keyframes square-spin {
25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
100% { transform: perspective(100px) rotateX(0) rotateY(0); }
}
.${className} > div {
animation-fill-mode: both;
width: 50px;
height: 50px;
background: #fff;
animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
}
.app-loading-wrap {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #282c34;
z-index: 9;
}
`
const oStyle = document.createElement('style')
const oDiv = document.createElement('div')
oStyle.id = 'app-loading-style'
oStyle.innerHTML = styleContent
oDiv.className = 'app-loading-wrap'
oDiv.innerHTML = `<div class="${className}"><div></div></div>`
return {
appendLoading() {
safeDOM.append(document.head, oStyle)
safeDOM.append(document.body, oDiv)
},
removeLoading() {
safeDOM.remove(document.head, oStyle)
safeDOM.remove(document.body, oDiv)
},
}
}
// ----------------------------------------------------------------------
const { appendLoading, removeLoading } = useLoading()
domReady().then(appendLoading)
window.onmessage = (ev) => {
ev.data.payload === 'removeLoading' && removeLoading()
}
setTimeout(removeLoading, 4999)

View File

@ -0,0 +1,141 @@
import { BrowserWindow } from 'electron';
import {
open,
Protocol,
SimConnectDataType,
SimConnectPeriod,
SimConnectConstants,
readLatLonAlt,
RecvSimObjectData
} from 'node-simconnect';
import { DataDefiniion } from './DataDefinition';
import { DefinitionID, EventID, RequestID } from './enums';
export class SimconnectClient {
private requestsMap: Map<number, Function>;
private isPaused: Boolean;
private onGround: Boolean;
private onRunway: Boolean;
private window: BrowserWindow;
constructor() {
this.requestsMap = new Map();
this.isPaused = true;
this.onGround = true;
this.onRunway = false;
}
async open(win: BrowserWindow) {
const { recvOpen, handle } = await open('My SimConnect client', Protocol.KittyHawk);
console.log('Connected to', recvOpen);
this.subscribeToSystemEvent(handle);
this.addDataDefinitions(handle);
this.window = win;
handle.on('simObjectData', this.objectDataHandler.bind(this));
}
objectDataHandler(objectData: RecvSimObjectData) {
const { requestID, data } = objectData;
if (this.requestsMap.has(requestID)) {
const fn: Function = this.requestsMap.get(requestID);
fn(data, this.window);
}
}
subscribeToSystemEvent(handle) {
handle.subscribeToSystemEvent(EventID.PAUSE, 'Pause');
handle.on('event', function(recvEvent) {
switch (recvEvent.clientEventId) {
case EventID.PAUSE:
console.log(recvEvent.data === 1 ? 'Sim paused' : 'Sim unpaused');
this.isPaused = recvEvent.data === 1;
break;
}
}.bind(this));
}
addDataDefinitions(handle) {
const onGroundDef = new DataDefiniion(
DefinitionID.ON_GROUND,
RequestID.ON_GROUND,
SimConnectPeriod.SECOND,
data => {
const onGround = data.readInt32() === 1;
console.log('On Groud :>> ', onGround);
this.onGround = onGround;
}
);
const onRunwayDef = new DataDefiniion(
DefinitionID.ON_RUNWAY,
RequestID.ON_RUNWAY,
SimConnectPeriod.SECOND,
(data, window) => {
const isOnRunway = data.readInt32() === 1;
console.log('On Runway :>> ', isOnRunway);
this.onRunway = isOnRunway;
window.send('data_on_runway', this.onRunway);
}
);
const liveDef = new DataDefiniion(
DefinitionID.LIVE_DATA,
RequestID.LIVE_DATA,
SimConnectPeriod.SECOND,
data => {
console.log('this.isPaused :>> ', this.isPaused);
if (!this.isPaused) {
const timestamp = new Date().getTime();
const { latitude, longitude, altitude } = readLatLonAlt(data);
const airspeed = data.readInt32();
const verticalSpeed = data.readInt32();
const heading = data.readInt32();
console.log("position", `${latitude} ${longitude} ${altitude}`,
"airspeed", airspeed,
"vertical speed", verticalSpeed,
"heading", heading,
// "landing lights", recvSimObjectData.data.readInt32(),
// "logo lights", recvSimObjectData.data.readInt32(),
// "taxi lights", recvSimObjectData.data.readInt32(),
// "wing lights", recvSimObjectData.data.readInt32(),
// "nav lights", recvSimObjectData.data.readInt32(),
// "beacon lights", recvSimObjectData.data.readInt32()
);
}
}
);
const aircraftDef = new DataDefiniion(
DefinitionID.AIRCRAFT_DATA,
RequestID.AIRCRAFT_DATA,
SimConnectPeriod.ONCE,
data => {
console.log('===================================');
console.log(`Type: "${data.readStringV()}"`);
console.log(`Title: "${data.readStringV()}"`);
console.log(`ATC ID: "${data.readString32()}"`);
console.log('===================================');
}
);
onGroundDef.add(['SIM ON GROUND', 'bool', SimConnectDataType.INT32, 0, SimConnectConstants.UNUSED]);
onRunwayDef.add(['ON ANY RUNWAY', 'bool', SimConnectDataType.INT32, 0, SimConnectConstants.UNUSED])
liveDef.add(['STRUCT LATLONALT', null, SimConnectDataType.LATLONALT]);
liveDef.add(['AIRSPEED INDICATED', 'knots', SimConnectDataType.INT32]);
liveDef.add(['VERTICAL SPEED', 'Feet per second', SimConnectDataType.INT32]);
liveDef.add(['PLANE HEADING DEGREES TRUE', 'Degrees', SimConnectDataType.INT32]);
liveDef.add(['LIGHT LANDING', 'bool', SimConnectDataType.INT32]);
liveDef.add(['LIGHT LOGO', 'bool', SimConnectDataType.INT32]);
liveDef.add(['LIGHT TAXI', 'bool', SimConnectDataType.INT32]);
liveDef.add(['LIGHT WING', 'bool', SimConnectDataType.INT32]);
liveDef.add(['LIGHT NAV', 'bool', SimConnectDataType.INT32]);
liveDef.add(['LIGHT BEACON', 'bool', SimConnectDataType.INT32]);
aircraftDef.add(['CATEGORY', null, SimConnectDataType.STRINGV, 0, SimConnectConstants.UNUSED]);
aircraftDef.add(['TITLE', null, SimConnectDataType.STRINGV, 0, SimConnectConstants.UNUSED]);
aircraftDef.add(['ATC ID', null, SimConnectDataType.STRING32, 0, SimConnectConstants.UNUSED]);
onGroundDef.build(handle, this.requestsMap);
onRunwayDef.build(handle, this.requestsMap);
liveDef.build(handle, this.requestsMap);
// aircraftDef.build(handle, this.requestsMap);
}
}

View File

@ -0,0 +1,29 @@
import { SimConnectConstants } from 'node-simconnect';
export class DataDefiniion {
private definitions: Array<any> = [];
private id: number;
private requestId: number;
private period: number;
private handler: Function;
constructor(id, requestId, period, handler) {
this.id = id;
this.requestId = requestId;
this.handler = handler;
this.period = period;
}
add(definition :Array<any>): void {
this.definitions.push(definition);
}
build(handle, requestsMap: Map<number, Function>) {
this.definitions.forEach(definition => {
handle.addToDataDefinition(this.id, ...definition);
});
requestsMap.set(this.requestId, this.handler);
handle.requestDataOnSimObject(this.requestId, this.id, SimConnectConstants.OBJECT_ID_USER, this.period);
}
}

View File

@ -0,0 +1,18 @@
export enum EventID {
PAUSE,
};
export enum DefinitionID {
LIVE_DATA,
AIRCRAFT_DATA,
ON_GROUND,
ON_RUNWAY,
};
export enum RequestID {
LIVE_DATA,
AIRCRAFT_DATA,
ON_GROUND,
ON_RUNWAY,
};