A Pen by Jose Estrada on CodePen.
Created
October 11, 2022 21:41
-
-
Save nadiemedicejose/285151cca4bb194e1bca5fed66a3f636 to your computer and use it in GitHub Desktop.
SimulatorMessages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const littleToBigEndian = (data) => | |
data | |
.slice(0, 8) | |
.match(/.{1,2}/g) | |
.reverse() | |
.join(""); | |
const bigEndian = (data) => | |
data.map((fourBytes) => | |
fourBytes | |
.match(/.{1,4}/g) | |
.reverse() | |
.join("") | |
); | |
const fourBytesGroups = (data) => data.match(/.{1,8}/g); | |
const hex2bin = (hex) => parseInt(hex, 16).toString(2).padStart(8, "0"); | |
const decodeData = (incomingData) => { | |
let simulatorData = incomingData.slice(24, -8); | |
const FUNCTIONAL_STATUS = littleToBigEndian(simulatorData); | |
let rotarySwitches = simulatorData.slice(8, 16); | |
// Big Endian Format | |
simulatorData = fourBytesGroups(simulatorData); | |
simulatorData = bigEndian(simulatorData); | |
// Replace FUNCTIONAL_STATUS & ROTARY_SWITCHES | |
simulatorData[0] = FUNCTIONAL_STATUS; | |
simulatorData[1] = rotarySwitches; | |
rotarySwitches = hex2bin(rotarySwitches); | |
const NAV_SELECT_CODE = rotarySwitches.slice(6, 8); | |
const APP_SELECT_CODE = rotarySwitches.slice(2, 4); | |
const VBR_SELECT_CODE = rotarySwitches.slice(4, 6); | |
const VHF_L_VOLUME = simulatorData[2].slice(0, 4); | |
const VHF_C_VOLUME = simulatorData[2].slice(4, 8); | |
const VHF_R_VOLUME = simulatorData[3].slice(0, 4); | |
const FLAT_VOLUME = simulatorData[3].slice(4, 8); | |
const CAB_VOLUME = simulatorData[4].slice(0, 4); | |
const PA_VOLUME = simulatorData[4].slice(4, 8); | |
const HF_L_VOLUME = simulatorData[5].slice(0, 4); | |
const HF_R_VOLUME = simulatorData[5].slice(4, 8); | |
const SAT_1_VOLUME = simulatorData[6].slice(0, 4); | |
const SAT_2_VOLUME = simulatorData[6].slice(4, 8); | |
const NAV_VOLUME = simulatorData[7].slice(0, 4); | |
const APPROACH_VOLUME = simulatorData[7].slice(4, 8); | |
const SPEAKER_VOLUME = simulatorData[8].slice(0, 4); | |
const decoded = { | |
NAV_SELECT_CODE, | |
VBR_SELECT_CODE, | |
APP_SELECT_CODE, | |
VHF_L_VOLUME, | |
VHF_C_VOLUME, | |
VHF_R_VOLUME, | |
FLAT_VOLUME, | |
CAB_VOLUME, | |
PA_VOLUME, | |
HF_L_VOLUME, | |
HF_R_VOLUME, | |
SAT_1_VOLUME, | |
SAT_2_VOLUME, | |
NAV_VOLUME, | |
APPROACH_VOLUME, | |
SPEAKER_VOLUME | |
}; | |
return decoded; | |
}; | |
// Test Cases | |
const testCase1 = | |
"a21000629fc213f9000000000300000000000000000032c3000003c300001a27000000df00007fff08c5000000002e2a4d94f6c0"; | |
const testCase2 = | |
"ac350062af3cc9190000000003000000000000152bad00000d6c00006af900002b5600007fff000000003191000000003837e674"; | |
const testCase3 = | |
"b69c0062bf1c2f9100000000030000000000002a14da2fcf0000000000002e06000000000000365100000c95000000006685e4a1"; | |
const testCase4 = | |
"c5be0062d6336ce900000000030000000000002b5285506b0000420408d0000000000000000000000000000000000000179265a9"; | |
const testCase5 = | |
"c8700062da50482900000000030000000000002b5285506b0000420408d0000000000000000000000000000000000000f8eca479"; | |
const testCase6 = | |
"ce980062e3b50d4100000000030000000000002b5285506b0000420408d00000000000000000000000000000000000004bdb021d"; | |
const testCase7 = | |
"d3d07a67e461695800000000030000000000003a00010001000100010001000100010001000100010001000100000001afb4fd20"; | |
console.log("Test Case 1", decodeData(testCase1)); | |
console.log("Test Case 2", decodeData(testCase2)); | |
console.log("Test Case 3", decodeData(testCase3)); | |
console.log("Test Case 4", decodeData(testCase4)); | |
console.log("Test Case 5", decodeData(testCase5)); | |
console.log("Test Case 6", decodeData(testCase6)); | |
console.log("Test Case 7", decodeData(testCase7)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment