-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCS-RC.lua
74 lines (64 loc) · 2.23 KB
/
DCS-RC.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
local DCSRC = {}
local socket = require("socket")
local JSON = loadfile("Scripts\\JSON.lua")()
local tcpServer
local currentMission = ""
local data
local client
local err
local tcpPort = 50051
function DCSRC.onSimulationStart()
currentMission = DCS.getMissionName()
if tcpServer == nil then
local successful
tcpServer = socket.tcp()
successful, err = tcpServer:bind("127.0.0.1", tcpPort)
tcpServer:listen(1)
tcpServer:settimeout(0)
if not successful then
log.write("DCS-RC", log.ERROR, "Error opening tcp socket - "..tostring(err))
else
log.write("DCS-RC", log.INFO, "Opened connection")
end
end
end
function DCSRC.onSimulationFrame()
if client ~= nil then
client:settimeout(10)
data, err = client:receive()
if data ~= nil then
local keys = JSON:decode(data)
log.write("DCS-RC", log.INFO, "Received "..data)
if keys["command"] == "load_mission" then
net.load_mission(keys["mission_name"])
elseif keys["command"] == "append_mission" then
net.missionlist_append(keys["mission_name"])
elseif keys["command"] == "delete_mission" then
net.missionlist_delete(keys["index"])
elseif keys["command"] == "run_mission" then
net.missionlist_run(keys["index"])
elseif keys["command"] == "clear_missionlist" then
net.missionlist_clear()
elseif keys["command"] == "get_mission" then
client:send("{\"filename\": \""..currentMission.."\"}\n")
elseif keys["command"] == "get_missionlist" then
client:send("{\"missionlist\":" .. JSON:encode(net.missionlist_get()) .."}\n")
elseif keys["command"] == "pause_mission" then
DCS.setPause(true)
elseif keys["command"] == "unpause_mission" then
DCS.setPause(false)
elseif keys["command"] == "get_pause" then
local pauseState = DCS.getPause()
client:send("{\"pause_state\": "..tostring(pauseState).."}\n")
end
end
end
client, err = tcpServer:accept()
if client ~= nil then
log.write("DCS-RC", log.INFO, "Client Connected")
end
end
DCS.setUserCallbacks(DCSRC) -- here we set our callbacks
local webCallbacks = {}
webCallbacks.onWebServerRequest = onWebServerRequest
DCS.setUserCallbacks(webCallbacks)