Skip to content

Commit

Permalink
Server/Bots: Added version string to log output
Browse files Browse the repository at this point in the history
Also the test/fgcom-fakepilot bot was ammended:
- now also has a version number
- now utilizes fgcom.log() and .dbg() for output (giving timestamps)
  • Loading branch information
hbeni committed Mar 20, 2024
1 parent 5dd99aa commit 04633eb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
3 changes: 2 additions & 1 deletion server/fgcom-radio-playback.bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ end


-- Connect to server, so we get the API
fgcom.log(botname..": connecting as '"..fgcom.callsign.."' to "..host.." on port "..port.." (cert: "..cert.."; key: "..key.."), joining: '"..fgcom.channel.."'")
fgcom.log(botname..": "..fgcom.getVersion())
fgcom.log("connecting as '"..fgcom.callsign.."' to "..host.." on port "..port.." (cert: "..cert.."; key: "..key.."), joining: '"..fgcom.channel.."'")
local client = assert(mumble.connect(host, port, cert, key))
client:auth(fgcom.callsign)
fgcom.dbg("connect and bind: OK")
Expand Down
3 changes: 2 additions & 1 deletion server/fgcom-radio-recorder.bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ end


-- Connect to server, so we get the API
fgcom.log(botname..": connecting as '"..fgcom.callsign.."' to "..host.." on port "..port.." (cert: "..cert.."; key: "..key.."), joining: '"..fgcom.channel.."'")
fgcom.log(botname..": "..fgcom.getVersion())
fgcom.log("connecting as '"..fgcom.callsign.."' to "..host.." on port "..port.." (cert: "..cert.."; key: "..key.."), joining: '"..fgcom.channel.."'")
local client = assert(mumble.connect(host, port, cert, key))
client:auth(botname)
fgcom.log("connect and bind: OK")
Expand Down
3 changes: 2 additions & 1 deletion server/statuspage/fgcom-status.bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ if arg[1] then
end

-- Connect to server, so we get the API
fgcom.log(botname..": connecting as '"..fgcom.callsign.."' to "..host.." on port "..port.." (cert: "..cert.."; key: "..key.."), joining: '"..fgcom.channel.."'")
fgcom.log(botname..": "..fgcom.getVersion())
fgcom.log("connecting as '"..fgcom.callsign.."' to "..host.." on port "..port.." (cert: "..cert.."; key: "..key.."), joining: '"..fgcom.channel.."'")
local client = assert(mumble.connect(host, port, cert, key))
client:auth(fgcom.callsign)
fgcom.log("connect and bind: OK")
Expand Down
80 changes: 43 additions & 37 deletions server/test/fgcom-fakepilot.bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Installation of this plugin is described in the projects readme: https://github.
]]
dofile("fgcom-sharedFunctions.inc.lua") -- include shared functions
fgcom.botversion = "1.0.1"

-- init random generator using /dev/random, if poosible (=linux)
fgcom.rng.initialize()
Expand Down Expand Up @@ -67,7 +68,7 @@ local sample = "recordings/fgcom.rec.testsample.fgcs"

if arg[1] then
if arg[1]=="-h" or arg[1]=="--help" then
print(botname)
print(botname..", "..fgcom.getVersion())
print("usage: "..arg[0].." [opt=val ...]")
print(" Options:")
print(" --id= id to join with (default=random)")
Expand All @@ -81,6 +82,8 @@ if arg[1] then
print(" --sleep= Time interval between checks (default="..sleep..")")
print(" --chancet= Chance for transmission (default="..chance_transmit..")")
print(" --chancee= Chance that transmit is echotest (default="..chance_echotest..")")
print(" --debug print debug messages (default=no)")
print(" --version print version and exit")
os.exit(0)
end

Expand All @@ -97,6 +100,8 @@ if arg[1] then
if k=="sleep" then sleep=tonumber(v) end
if k=="chancet" then chance_transmit=tonumber(v) end
if k=="chancee" then chance_echotest=tonumber(v) end
if opt == "--debug" then fgcom.debugMode = true end
if opt == "--version" then print(botname..", "..fgcom.getVersion()) os.exit(0) end
end

end
Expand Down Expand Up @@ -130,7 +135,7 @@ readFGCSSampleFile = function(file)
local endOfSamples = false
while not endOfSamples do
local nextSample = fgcom.io.readFGCSSample(sampleFH)
--print("sample: len="..nextSample.len.."; eof='"..tostring(nextSample.eof).."'; data='"..nextSample.data.."'")
--fgcom.dbg("sample: len="..nextSample.len.."; eof='"..tostring(nextSample.eof).."'; data='"..nextSample.data.."'")
if nextSample.len > 0 and not nextSample.eof then
vb:pushright(nextSample) -- write the sample structure to the buffer
end
Expand All @@ -149,36 +154,37 @@ end
-----------------------------
--[[ BOT RUNTIME ]]
-----------------------------
fgcom.log(botname..": "..fgcom.getVersion())

-- read the file into the queue.
lastHeader, _ = readFGCSSampleFile(sample)
if not lastHeader then print("ERROR: '"..sample.."' not readable or no FGCS file") os.exit(1) end
if not lastHeader then fgcom.log("ERROR: '"..sample.."' not readable or no FGCS file") os.exit(1) end

-- AFTER THIS CODE we can be confident it was a valid FGCS file!
-- lastHeader is initialized with the header data
-- voiceBuffer is initialized with the read samples
print(sample..": successfully loaded.")
fgcom.log(sample..": successfully loaded.")



-- Connect to server, so we get the API
print(botname..": connecting as '"..fgcom.callsign.."' to "..host.." on port "..port.." (cert: "..cert.."; key: "..key..")")
fgcom.log("connecting as '"..fgcom.callsign.."' to "..host.." on port "..port.." (cert: "..cert.."; key: "..key.."), joining: '"..fgcom.channel.."'")
local client = assert(mumble.connect(host, port, cert, key))
client:auth(fgcom.callsign)
print("connect and bind: OK")
fgcom.dbg("connect and bind: OK")



-- function to get all channel users
-- this updates the global playback_target table
updateAllChannelUsersforSend = function(cl)
--print("udpate channelusers")
--fgcom.dbg("udpate channelusers")
local ch = cl:getChannel(fgcom.channel)
local users = ch:getUsers()
playback_targets = {}
--print("ok: "..ch:getName())
--fgcom.dbg("ok: "..ch:getName())
for k,v in pairs(users) do
--print(" k",k,"v=",v)
--fgcom.dbg(" k",k,"v=",v)
table.insert(playback_targets, v)
end
end
Expand All @@ -192,26 +198,26 @@ end
local playbackTimer = mumble.timer()
local freq = testfrequencies[1]
playbackTimer_func = function(t)
--print("playback timer: tick; ptt=",ptt)
--fgcom.dbg("playback timer: tick; ptt=",ptt)

-- So, a new timer tick started.
if ptt then
-- PTT is active: setup voice buffer and radio (if not done already)
if voiceBuffer:size() <= 0 then
print("fgcom.callsign.. Starting new transmission...")
fgcom.log(fgcom.callsign.." Starting new transmission...")

-- fill temporary buffer
lastHeader, voiceBuffer = readFGCSSampleFile(sample)
if not lastHeader then print("ERROR: '"..sample.."' not readable or no FGCS file") os.exit(1) else print(sample..": successfully refreshed ("..voiceBuffer:size().." samples)") end
if not lastHeader then fgcom.log("ERROR: '"..sample.."' not readable or no FGCS file") os.exit(1) else fgcom.dbg(sample..": successfully refreshed ("..voiceBuffer:size().." samples)") end

-- choose a random frequency
freq = testfrequencies[math.random(1,#testfrequencies)]
local ce = tonumber(math.random(0, 100)/100)
if ce < chance_echotest then
freq = "910.00"
print(" (Echotest transmission @"..freq..")")
fgcom.log(" (Echotest transmission @"..freq..")")
else
print(" (Normal transmission @"..freq..")")
fgcom.log(" (Normal transmission @"..freq..")")
end

-- Broadcast radio
Expand All @@ -221,31 +227,31 @@ playbackTimer_func = function(t)
..",CHN="..freq
..",PWR=10"
..",PTT=1"
print(fgcom.callsign.." Bot sets radio: "..msg)
fgcom.dbg(fgcom.callsign.." Bot sets radio: "..msg)
client:sendPluginData("FGCOM:UPD_COM:0:0", msg, playback_targets)
end
end

-- Play the samples, then stop transmission.
if voiceBuffer:size() > 0 then
--print("voiceBuffer is still filled, samples: "..voiceBuffer:size())
--fgcom.dbg("voiceBuffer is still filled, samples: "..voiceBuffer:size())

-- get the next sample from the buffer and play it
local nextSample = voiceBuffer:popleft()
local endofStream = false
if voiceBuffer:size() == 0 then endofStream = true end

print("transmit next sample @"..freq)
--print(" tgt="..playback_target:getSession())
print(" eos="..tostring(endofStream))
print(" cdc="..lastHeader.voicecodec)
print(" dta="..#nextSample.data)
--print(" dta="..nextSample.data)
fgcom.dbg("transmit next sample @"..freq)
--fgcom.dbg(" tgt="..playback_target:getSession())
fgcom.dbg(" eos="..tostring(endofStream))
fgcom.dbg(" cdc="..lastHeader.voicecodec)
fgcom.dbg(" dta="..#nextSample.data)
--fgcom.dbg(" dta="..nextSample.data)
client:transmit(lastHeader.voicecodec, nextSample.data, not endofStream) -- Transmit the single frame as an audio packet (the bot "speaks")
print(" transmit ok")
fgcom.dbg(" transmit ok")
if endofStream then
-- no samples left? Just loop around to trigger all the checks
print(fgcom.callsign.." no samples left, playback complete")
fgcom.dbg(fgcom.callsign.." no samples left, playback complete")

ptt = false;

Expand All @@ -256,19 +262,19 @@ playbackTimer_func = function(t)
..",CHN="..freq
..",PWR=10"
..",PTT=0"
print(" Bot sets radio: "..msg)
fgcom.dbg(" Bot sets radio: "..msg)
client:sendPluginData("FGCOM:UPD_COM:0:0", msg, playback_targets)
end

t:stop() -- Stop the timer
print(fgcom.callsign.." Transmission complete.")
fgcom.log(fgcom.callsign.." Transmission complete.")
end
end

else
-- PTT is false.
-- (This should never be reached, because the only place ptt is reset to false is, if the voicebuffer is empty. Somehow the timer was not stopped...)
print("ERROR: PTT=0 invalid state reached.")
fgcom.log("ERROR: PTT=0 invalid state reached.")
t:stop()
voiceBuffer = Queue.new()
--os.exit(1)
Expand All @@ -286,20 +292,20 @@ local alt = math.random(15, 8000)
local latmv = math.random(-100, 100)/100000
local lonmv = math.random(-100, 100)/100000
client:hook("OnServerSync", function(client, event)
print("Sync done; server greeted with: ", event.welcome_text)
fgcom.log("Sync done; server greeted with: ", event.welcome_text)

-- try to join fgcom-mumble channel
local ch = client:getChannel(fgcom.channel)
event.user:move(ch)
print(fgcom.callsign.." joined channel "..fgcom.channel)
fgcom.log(fgcom.callsign.." joined channel "..fgcom.channel)

updateAllChannelUsersforSend(client)
local msg = "CALLSIGN="..fgcom.callsign
client:sendPluginData("FGCOM:UPD_USR:0", msg, playback_targets)

-- update location
locUpd:start(function(t)
--print("locUpd: tick")
--fgcom.dbg("locUpd: tick")
-- update current users of channel
updateAllChannelUsersforSend(client)
if #playback_targets > 0 then
Expand All @@ -311,24 +317,24 @@ client:hook("OnServerSync", function(client, event)
local msg = "LON="..lat
..",LAT="..lon
..",ALT="..alt
--print("Bot sets location: "..msg)
--fgcom.dbg("Bot sets location: "..msg)
client:sendPluginData("FGCOM:UPD_LOC:0", msg, playback_targets)
end

end, 0.00, locs)

-- let the pilot check every n seconds if he wants to do a transmission
checkTimer:start(function(t)
--print("checkTimer: tick")
--fgcom.dbg("checkTimer: tick")
local ct = math.random(0, 100)/100
if chance_transmit < ct then
-- triggerTransmit, if not already transmitting
if not ptt then
--print("activating PTT")
--fgcom.dbg("activating PTT")
ptt = true
playbackTimer:start(playbackTimer_func, 0.0, lastHeader.samplespeed)
else
-- print("(not activating PTT, its still active)")
-- fgcom.dbg("(not activating PTT, its still active)")
end
end
end, 0.00, sleep)
Expand All @@ -342,11 +348,11 @@ client:hook("OnPluginData", function(client, event)
--["receivers"] = { -- A table of who is receiving this data
-- [1] = mumble.user,
--},
--print("OnPluginData(): DATA INCOMING FROM="..tostring(event.id)..", "..tostring(event.sender))
--fgcom.dbg("OnPluginData(): DATA INCOMING FROM="..tostring(event.id)..", "..tostring(event.sender))

-- Answer data requests
if event.id:len() > 0 and event.id:find("FGCOM:ICANHAZDATAPLZ") then
print("OnPluginData(): client asks for data: "..tostring(event.sender))
fgcom.dbg("OnPluginData(): client asks for data: "..tostring(event.sender))

local msg = "CALLSIGN="..fgcom.callsign
client:sendPluginData("FGCOM:UPD_USR:0", msg, {event.sender})
Expand All @@ -370,4 +376,4 @@ end)


mumble.loop()
print(botname.." with callsign "..fgcom.callsign.." completed.")
fgcom.log(botname.." with callsign "..fgcom.callsign.." completed.")

0 comments on commit 04633eb

Please sign in to comment.