Skip to content

Instantly share code, notes, and snippets.

@sighmon
Last active March 15, 2021 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sighmon/e53cab92a1a9d16d5cf5800c8e4935be to your computer and use it in GitHub Desktop.
Save sighmon/e53cab92a1a9d16d5cf5800c8e4935be to your computer and use it in GitHub Desktop.
autorun.brs - roVideoPlayer responds with playback information via HTTP get to /hello on port 8080, and UDP packet to port 6666
Sub Main()
' MuseumOS BrightSign video player
' NOTE: This script uses an roHttpServer which doesn't do TLS
' So be sure not to let the general public on this network
' Settings
videoFileToPlay = "SD:/test.mp4"
museumosUri = "http://192.168.2.1:4444"
brightSignTimestampUri = "/hello"
timeBetweenPostsToMuseumOS = 500
brightSignId = 1
locationLatitude = "-37.8221128"
locationLongitude = "144.9676001"
' WARNING: This enables restarts of the script by HTTP get request to /restart
enableScriptRestart = True
' Set system time
systemTime = CreateObject("roSystemTime")
' Create a sync manager with default address and port.
aa1=CreateObject("roAssociativeArray")
aa1.Domain = "BS1"
s=CreateObject("roSyncManager", aa1)
p=CreateObject("roMessagePort")
s.SetPort(p)
' THIS SECTION IS ONLY DONE BY THE MASTER
' We're the master unit - send out a synchronize event saying that we're starting.
' playback 1000ms from now
s.SetMasterMode(True)
msg = s.Synchronize("Playing1", 1000)
' THIS SECTION IS ONLY DONE BY THE SLAVE
' We're a slave unit, and we're sitting waiting for a sync message.
' msg=Wait(4000, p)
v = CreateObject("roVideoPlayer")
p = CreateObject("roMessagePort")
v.SetLoopMode(True)
v.SetPort(p)
' UDP receiver
receiver = CreateObject("roDatagramReceiver", 6666)
receiver.SetPort(p)
' UDP sender
sender = CreateObject("roDatagramSender")
sender.SetDestination("192.168.2.1", 6666)
' HTTP server
httpServer = CreateObject("roHttpServer", { port: 8080 })
' To send a static response
' replyMessage = generateJSONReply(brightSignId, locationLatitude, locationLongitude, systemTime.GetZoneDateTime("AEST"), videoFileToPlay, v.GetPlaybackPosition())
' httpServer.AddGetFromString({ url_path: brightSignTimestampUri, content_type: "application/json; charset=utf-8", body: replyMessage })
' To send a dynamic response
httpServer.SetPort(p)
httpServer.AddGetFromEvent({ url_path: brightSignTimestampUri })
if enableScriptRestart=True then
httpServer.AddGetFromEvent({ url_path: "/restart" })
endif
' EVERYONE DOES THE REST
aa=CreateObject("roAssociativeArray")
aa.SyncDomain = msg.GetDomain()
aa.SyncId = msg.GetId()
aa.SyncIsoTimestamp = msg.GetIsoTimestamp()
aa.Filename = videoFileToPlay
duration = v.ProbeFile(aa.Filename).VideoDuration
print "***Video File***"
print aa.Filename
print "Duration: " + Str(duration * 1000)
' Add a playback event every timeBetweenPostsToMuseumOS milliseconds
For i=0 To (duration * 1000) Step timeBetweenPostsToMuseumOS
ok = v.AddEvent(i, i)
End For
v.PlayFile(aa)
' print "***File information***"
' print v.GetStreamInfo()
replay:
print "***START OF VIDEO***"
ok = v.PlayFile(aa.Filename)
waitloop:
msg = Wait(0,p) ' Wait for all events
currentTime = systemTime.GetZoneDateTime("AEST")
replyMessage = generateJSONReply(brightSignId, locationLatitude, locationLongitude, currentTime, aa.Filename, v.GetPlaybackPosition())
if type(msg) = "roDatagramEvent" then
' Event is a UDP event, reply with some information
print "UDP message received: " + msg + " at " + currentTime
' JSON reply via UDP
sender.Send(replyMessage)
goto waitloop
elseif type(msg) = "roVideoEvent" then
' Event is a video time event so continue on
elseif type(msg) = "roHttpEvent" then
' HTTP get request for information
print "HTTP get to " + msg.GetURL() + " at " + currentTime + ": " + formatJSON(msg.GetRequestHeaders())
' Check for restart script request
if msg.GetURL() = "/restart" then
RestartScript()
endif
' JSON reply via HTTP
msg.SetResponseBodyString(replyMessage)
msg.AddResponseHeaders({ content_type: "application/json; charset=utf-8" })
msg.SendResponse(200)
goto waitloop
else
' Some other event... so ignore it
print "Unknown event: " + type(msg)
goto waitloop
endif
' JSON data to send to MuseumOS
data = generateJSONReply(brightSignId, locationLatitude, locationLongitude, currentTime, aa.Filename, msg.GetData())
print data
' Post to MuseumOS
request = CreateObject("roUrlTransfer")
request.SetUrl(museumosUri)
request.PostFromString(data)
' print "***PROPERTIES***"
' print v.GetProperties()
' print "***END PROPERTIES***"
goto waitloop
' Turn on serial console over SSH
' reg = CreateObject("roRegistrySection", "networking")
' reg.write("ssh","22")
' n=CreateObject("roNetworkConfiguration", 0)
' n.SetLoginPassword("password")
' n.Apply()
' reg.flush()
' Turn off serial console over SSH
' reg = CreateObject("roRegistrySection", "networking")
' reg.delete("ssh")
' reg.flush()
End Sub
' JSON generator
function generateJSONReply(id, longitude, latitude, time, filename, playbackTime) As String
' formatJSON() fails to escape a date string.
' dictionaryResponse = CreateObject("roAssociativeArray")
' dictionaryResponse.AddReplace("brightSignId", Str(id))
' locationArray = CreateObject("roArray", 2, false)
' locationArray.Push(latitude)
' locationArray.Push(longitude)
' dictionaryResponse.AddReplace("location", locationArray)
' dictionaryResponse.AddReplace("timestamp", time) ' formatJSON() fails here
' dictionaryResponse.AddReplace("videoFile", filename)
' dictionaryResponse.AddReplace("videoPlaybackTime", Str(playbackTime))
' dictionaryResponse.AddReplace("uptime", Str(UpTime(0) * 1000))
' return formatJSON(dictionaryResponse)
return "{ " + Chr(34) + "brightSignId" + Chr(34) + ": " + Str(id) + ", " + Chr(34) + "location" + Chr(34) + ": [" + Chr(34) + longitude + Chr(34) + ", " + Chr(34) + latitude + Chr(34) + "], " + Chr(34) + "timestamp" + Chr(34) + ": " + Chr(34) + time + Chr(34) + ", " + Chr(34) + "videoFile" + Chr(34) + ": " + Chr(34) + filename + Chr(34) + ", " + Chr(34) + "videoPlaybackTime" + Chr(34) + ": " + Str(playbackTime) + ", " + Chr(34) + "uptime" + Chr(34) + ": " + Str(UpTime(0) * 1000) + " }"
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment