Files
visionLabs---roku/components/MainScene.brs
2026-04-19 00:22:00 +00:00

125 lines
3.4 KiB
Plaintext

sub init()
m.top.setFocus(true)
m.backend = m.top.findNode("backend")
m.status = m.top.findNode("status")
m.cameraPosters = [
m.top.findNode("poster1"),
m.top.findNode("poster2"),
m.top.findNode("poster3"),
m.top.findNode("poster4")
]
m.cameraNodes = [
m.top.findNode("cam1"),
m.top.findNode("cam2"),
m.top.findNode("cam3"),
m.top.findNode("cam4")
]
loadConfig()
end sub
sub loadConfig()
raw = ReadAsciiFile("pkg:/config/cameras.json")
config = ParseJson(raw)
if type(config) <> "roAssociativeArray" then
m.status.text = "Unable to parse cameras.json"
return
end if
if config.doesExist("backend") and config.backend.doesExist("baseUrl") then
m.backend.text = "Frigate: " + config.backend.baseUrl
end if
if not config.doesExist("cameras") then
m.status.text = "No cameras found in config"
return
end if
cameras = config.cameras
m.status.text = cameras.count().toStr() + " cameras loaded, fetching snapshots..."
loadedCount = 0
for i = 0 to m.cameraNodes.count() - 1
poster = m.cameraPosters[i]
node = m.cameraNodes[i]
if i < cameras.count() then
camera = cameras[i]
label = camera.name
if camera.doesExist("host") then
label = label + chr(10) + camera.host
end if
if camera.doesExist("frigateCamera") then
label = label + chr(10) + "Frigate ID: " + camera.frigateCamera
end if
snapshotUrl = buildSnapshotUrl(config, camera)
targetPath = "tmp:/" + camera.id + ".jpg"
result = fetchSnapshot(snapshotUrl, targetPath)
if result.success then
poster.uri = targetPath
loadedCount = loadedCount + 1
node.text = label
else
poster.uri = ""
node.text = label + chr(10) + "Snapshot failed (" + result.code.toStr() + ")"
end if
else
poster.uri = ""
node.text = "Empty slot"
end if
end for
m.status.text = loadedCount.toStr() + " of " + cameras.count().toStr() + " snapshots loaded"
end sub
function buildSnapshotUrl(config as Object, camera as Object) as String
if not config.doesExist("backend") or not config.backend.doesExist("baseUrl") then
return ""
end if
baseUrl = config.backend.baseUrl
path = ""
if camera.doesExist("snapshotPath") then
path = camera.snapshotPath
else if camera.doesExist("frigateCamera") then
path = "/api/" + camera.frigateCamera + "/latest.jpg"
else
return ""
end if
if right(baseUrl, 1) = "/" and left(path, 1) = "/" then
return left(baseUrl, len(baseUrl) - 1) + path
else if right(baseUrl, 1) <> "/" and left(path, 1) <> "/" then
return baseUrl + "/" + path
end if
return baseUrl + path
end function
function fetchSnapshot(url as String, filePath as String) as Object
result = {
success: false
code: 0
}
if url = "" then
return result
end if
transfer = CreateObject("roUrlTransfer")
transfer.SetCertificatesFile("common:/certs/ca-bundle.crt")
transfer.InitClientCertificates()
transfer.SetUrl(url)
result.success = transfer.GetToFile(filePath)
result.code = transfer.GetResponseCode()
return result
end function