From b91fbf0f13c7834b62f29004ace9347c562106f9 Mon Sep 17 00:00:00 2001 From: Daan Wendelen Date: Sat, 6 Jul 2024 16:22:27 +0200 Subject: [PATCH 1/3] First untested version --- dcsFiles/TheWay.lua | 67 ++++++++++++++++++------------ src/App.js | 5 +++ src/components/TransferControls.js | 41 ++++++++++++------ src/store/dcsPoint.js | 1 + 4 files changed, 75 insertions(+), 39 deletions(-) diff --git a/dcsFiles/TheWay.lua b/dcsFiles/TheWay.lua index f61c17a..50b0cbc 100644 --- a/dcsFiles/TheWay.lua +++ b/dcsFiles/TheWay.lua @@ -48,7 +48,7 @@ function LuaExportStop() crosshair:setVisible(false) end -local data +local keys = nil local busy = false; local isPressed = false local currCommandIndex = 1 @@ -58,14 +58,8 @@ local lastNeedDepress = true local whenToDepress = nil local crosshairVisible = false local stringtoboolean={ ["true"]=true, ["false"]=false } -function LuaExportBeforeNextFrame() - if upstreamLuaExportBeforeNextFrame ~= nil then - successful, err = pcall(upstreamLuaExportBeforeNextFrame) - if not successful then - log.write("THEWAY", log.ERROR, "Error in upstream LuaExportBeforeNextFrame function" .. tostring(err)) - end - end +function pressKeys() if busy then if isPressed then -- check if the time has come to depress @@ -79,9 +73,6 @@ function LuaExportBeforeNextFrame() currCommandIndex = currCommandIndex + 1 end else - -- Prepare for new button push - local decodedData = JSON:decode(data) - local keys = decodedData["payload"] --check if there are buttons left to press if currCommandIndex <= #keys then lastDevice = keys[currCommandIndex]["device"] @@ -97,32 +88,53 @@ function LuaExportBeforeNextFrame() isPressed = true else --if there's nothing else to press, we are done + keys = nil busy = false currCommandIndex = 1 end end - else - local client, err = tcpServer:accept() - if client ~= nil then - client:settimeout(10) - data, err = client:receive() - if err then - log.write("THEWAY", log.ERROR, "Error at receiving: " .. err) - end + end +end - if data then - local keys = JSON:decode(data) - if keys["type"] == "waypoints" then - busy = true - elseif keys["type"] == "crosshair" then - local shouldBeVisible = stringtoboolean[keys["payload"]] - crosshair:setVisible(shouldBeVisible) - end +function checkSocket() + local client, err = tcpServer:accept() + if client ~= nil then + client:settimeout(10) + local data, err = client:receive() + if err then + log.write("THEWAY", log.ERROR, "Error at receiving: " .. err) + end + + if data then + local decodedData = JSON:decode(data) + if decodedData["type"] == "waypoints" then + keys = decodedData["payload"] + busy = true + currCommandIndex = 1 + elseif decodedData["type"] == "crosshair" then + local shouldBeVisible = stringtoboolean[decodedData["payload"]] + crosshair:setVisible(shouldBeVisible) + elseif decodedData["type"] == "abort" then + keys = nil + busy = false + currCommandIndex = 1 end end end end +function LuaExportBeforeNextFrame() + if upstreamLuaExportBeforeNextFrame ~= nil then + successful, err = pcall(upstreamLuaExportBeforeNextFrame) + if not successful then + log.write("THEWAY", log.ERROR, "Error in upstream LuaExportBeforeNextFrame function" .. tostring(err)) + end + end + + pressKeys() + checkSocket() +end + function LuaExportAfterNextFrame() if upstreamLuaExportAfterNextFrame ~= nil then successful, err = pcall(upstreamLuaExportAfterNextFrame) @@ -145,6 +157,7 @@ function LuaExportAfterNextFrame() message["coords"]["lat"] = tostring(coords.latitude) message["coords"]["long"] = tostring(coords.longitude) message["elev"] = tostring(elevation) + message["busy"] = busy local toSend = JSON:encode(message) if pcall(function() diff --git a/src/App.js b/src/App.js index 2714598..aadd8c6 100644 --- a/src/App.js +++ b/src/App.js @@ -42,6 +42,10 @@ function App() { ipcRenderer.send("messageToDcs", commands); }, [dcsWaypoints, module, userPreferences]); + const handleAbort = useCallback(async() => { + ipcRenderer.send("messageToDcs", { type: "abort" }); + }, []); + const handleFileSave = useCallback(() => { ipcRenderer.send("saveFile", JSON.stringify(dcsWaypoints)); }, [dcsWaypoints]); @@ -112,6 +116,7 @@ function App() { diff --git a/src/components/TransferControls.js b/src/components/TransferControls.js index ff8d3f7..f80f74e 100644 --- a/src/components/TransferControls.js +++ b/src/components/TransferControls.js @@ -5,10 +5,37 @@ import { useSelector } from "react-redux"; const TransferControls = (props) => { const waypointList = useSelector((state) => state.waypoints.dcsWaypoints); - const { module } = useSelector((state) => state.dcsPoint); + const { module, busy } = useSelector((state) => state.dcsPoint); const allowSaveFile = waypointList.length > 0; const allowTransfer = allowSaveFile && module && module !== "Spectator"; + function button() { + if(busy) { + return + + Abort transfer + + + ; + } else { + return + + Transfer to DCS + + + ; + } + } + return ( <> { - - - Transfer to DCS - - - + { button() } diff --git a/src/store/dcsPoint.js b/src/store/dcsPoint.js index d7be137..b2c48bd 100644 --- a/src/store/dcsPoint.js +++ b/src/store/dcsPoint.js @@ -12,6 +12,7 @@ const dcsPointSlice = createSlice({ state.lat = Number(action.payload.coords.lat); state.long = Number(action.payload.coords.long); state.elev = Number(action.payload.elev); + state.busy = Number(action.payload.busy); }, }, }); From 16efd03a1f520c1bc9ad33f7596bfd9169184f25 Mon Sep 17 00:00:00 2001 From: Daan Wendelen Date: Sat, 6 Jul 2024 17:11:19 +0200 Subject: [PATCH 2/3] Small fix: busy is a boolean, not a number --- src/store/dcsPoint.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/store/dcsPoint.js b/src/store/dcsPoint.js index b2c48bd..90be4b4 100644 --- a/src/store/dcsPoint.js +++ b/src/store/dcsPoint.js @@ -12,7 +12,7 @@ const dcsPointSlice = createSlice({ state.lat = Number(action.payload.coords.lat); state.long = Number(action.payload.coords.long); state.elev = Number(action.payload.elev); - state.busy = Number(action.payload.busy); + state.busy = action.payload.busy; }, }, }); From aeb4a830905f398c2bc7d157c5ff0697a34d97b4 Mon Sep 17 00:00:00 2001 From: Daan Wendelen Date: Mon, 15 Jul 2024 01:12:09 +0200 Subject: [PATCH 3/3] Fix initialState --- src/store/dcsPoint.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/store/dcsPoint.js b/src/store/dcsPoint.js index 90be4b4..94c23a8 100644 --- a/src/store/dcsPoint.js +++ b/src/store/dcsPoint.js @@ -1,13 +1,13 @@ import { createSlice } from "@reduxjs/toolkit"; -const initialState = { module: null, lat: null, long: null, elev: null }; +const initialState = { module: null, lat: null, long: null, elev: null, busy: null }; const dcsPointSlice = createSlice({ name: "dcsPoint", initialState, reducers: { changeCoords(state, action) { - // { "model": "F-16C_50", "coords": { "lat": "41.905925545347", "long": "43.783457188399"} , "elev": "1677.6647949219"} + // { "model": "F-16C_50", "coords": { "lat": "41.905925545347", "long": "43.783457188399"} , "elev": "1677.6647949219", "busy": false} state.module = action.payload.model; state.lat = Number(action.payload.coords.lat); state.long = Number(action.payload.coords.long);