mirror of
https://github.com/hawkeye-stan/msfs-popout-panel-manager.git
synced 2024-11-25 07:10:10 +00:00
43caff1ca9
commit 2bbda3c4e4969fdf05566908fde01f1c9e4e23f7 Author: Stanley <hawkeyesk@outlook.com> Date: Mon Sep 5 23:54:39 2022 -0400 Added in-game panel support commit ec29b0ec2612b10e45ab9a73c10b88a98fcf6eaf Author: Stanley <hawkeyesk@outlook.com> Date: Sun Sep 4 21:21:37 2022 -0400 Added in-game panel support commit 97edc184f349e1fde74a15a643fb90fb9bd90395 Author: Stanley <hawkeyesk@outlook.com> Date: Thu Sep 1 18:08:44 2022 -0400 Update touch panel commit da48ca0a272290466952c5c1bd1ca035d56f930c Author: Stanley <hawkeyesk@outlook.com> Date: Mon Aug 29 22:19:38 2022 -0400 Added pop out panel temporary display commit 701346193804f93616b0e6e2222d9d55223f516f Author: Stanley <hawkeyesk@outlook.com> Date: Wed Aug 24 10:33:59 2022 -0400 Added auto resize window display mode commit 98cbcd949f1555b44db22267ce5c54858bef47cd Author: Stanley <hawkeyesk@outlook.com> Date: Wed Aug 24 09:39:38 2022 -0400 Added auto resize window display mode
155 lines
No EOL
4.1 KiB
JavaScript
155 lines
No EOL
4.1 KiB
JavaScript
const SIMCONNECT_DATA_REQUEST_INTERVAL_SLOW = 5000;
|
|
|
|
var apiUrl = undefined;
|
|
var networkStatus = false;
|
|
var arduinoStatus = false;
|
|
var simConnectSystemEvent = undefined;
|
|
var simConnectData = undefined;
|
|
|
|
onconnect = (ev) => {
|
|
const [port] = ev.ports;
|
|
|
|
port.onmessage = e => {
|
|
apiUrl = e.data.apiUrl;
|
|
updateInterval = e.data.updateInterval;
|
|
|
|
setInterval(() => {
|
|
port.postMessage({
|
|
networkStatus: networkStatus,
|
|
arduinoStatus: arduinoStatus,
|
|
simConnectSystemEvent: simConnectSystemEvent,
|
|
simConnectData: simConnectData
|
|
});
|
|
}, updateInterval);
|
|
|
|
requestData(updateInterval);
|
|
};
|
|
};
|
|
|
|
requestData = async (updateInterval) => {
|
|
try {
|
|
let response = await fetch(`${apiUrl}/getdata`).catch(() => {
|
|
throw('MSFS Touch Panel Server is unavailable.')
|
|
});
|
|
|
|
if (response !== undefined) {
|
|
let result = await response.json();
|
|
|
|
if (result === undefined)
|
|
throw new Error('MSFS Touch Panel Server error');
|
|
|
|
|
|
networkStatus = Boolean(result.msfsStatus ?? false);
|
|
arduinoStatus = Boolean(result.arduinoStatus ?? false);
|
|
|
|
if (result.systemEvent !== null && result.systemEvent !== undefined)
|
|
simConnectSystemEvent = result.systemEvent.split('-')[0];
|
|
else
|
|
simConnectSystemEvent = null;
|
|
|
|
if (!result.msfsStatus)
|
|
throw('MSFS SimConnect is unavailable.')
|
|
|
|
var simData = JSON.parse(result.data ?? []);
|
|
|
|
if ((simData !== null && simData !== []))
|
|
simConnectData = parseRequestData(simData);
|
|
|
|
setTimeout(() => requestData(updateInterval), updateInterval);
|
|
}
|
|
else {
|
|
throw('Empty MSFS Touch Panel Server response.')
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.log(error);
|
|
networkStatus = false;
|
|
setTimeout(() => requestData(SIMCONNECT_DATA_REQUEST_INTERVAL_SLOW), SIMCONNECT_DATA_REQUEST_INTERVAL_SLOW);
|
|
}
|
|
}
|
|
|
|
parseRequestData = (resultData) => {
|
|
if (resultData === []) return [];
|
|
|
|
let newData = [];
|
|
|
|
// Format value as specified by the data key as needed and apply defaults
|
|
resultData.forEach(item => {
|
|
if (item.javaScriptFormatting !== null) {
|
|
item.value = formattingMethod[item.javaScriptFormatting](item.value);
|
|
}
|
|
|
|
if (item.value === null || item.value === undefined) {
|
|
item.value = item.defaultValue;
|
|
}
|
|
|
|
newData[item.propName] = item.value;
|
|
})
|
|
|
|
return newData;
|
|
}
|
|
|
|
formattingMethod = {
|
|
toFixed0: (value) => {
|
|
return value.toFixed(0);
|
|
},
|
|
toFixed1: (value) => {
|
|
return value.toFixed(1);
|
|
},
|
|
toFixed2: (value) => {
|
|
return value.toFixed(2);
|
|
},
|
|
toFixed3: (value) => {
|
|
return value.toFixed(3);
|
|
},
|
|
toFixed4: (value) => {
|
|
return value.toFixed(4);
|
|
},
|
|
padStartZero4: (value) => {
|
|
return String(value).padStart(4, '0');
|
|
},
|
|
decToHex: (value) => {
|
|
let str = value.toString(16);
|
|
return str.substring(0, str.length - 4).padStart(4, '0');
|
|
},
|
|
toBlankIfNegative: (value) => {
|
|
if (value < 0)
|
|
return ''
|
|
return value;
|
|
},
|
|
toBlankIfZeroOrNegative: (value) => {
|
|
if (value <= 0)
|
|
return ''
|
|
|
|
return value;
|
|
},
|
|
toBoeingFlapsValue: (value) => {
|
|
value = value.toFixed(0)
|
|
|
|
switch (value) {
|
|
case '0':
|
|
return '0';
|
|
case '1':
|
|
return '1'
|
|
case '2':
|
|
return '2'
|
|
case '3':
|
|
return '5'
|
|
case '4':
|
|
return '10'
|
|
case '5':
|
|
return '15'
|
|
case '6':
|
|
return '25'
|
|
case '7':
|
|
return '30'
|
|
case '8':
|
|
return '40'
|
|
default:
|
|
return '5'
|
|
}
|
|
},
|
|
toBoeingElevatorTrimValue: (value) => {
|
|
return (value / 10).toFixed(2);
|
|
}
|
|
} |