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..94c23a8 100644 --- a/src/store/dcsPoint.js +++ b/src/store/dcsPoint.js @@ -1,17 +1,18 @@ 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); state.elev = Number(action.payload.elev); + state.busy = action.payload.busy; }, }, });