Example RUBY Telnet Program – Email if Draining Battery
The Ruby program below is similar in structure to, and defines the same functions as the previous Python example.
The main loop at the end runs every 10 minutes and logs in/out each time to check battery status (this is more reliable than connecting only once at program start, since the TCP connection could timeout after a while).
It uses the ‘
who’ command to check if there are any active RTMP users connected; that is, users connected via the web-client or Android/iOS apps (if this was the case there would be no reason to panic and send a battery-draining email). To see if the battery is draining, the program reads the STATE variable ‘batterystatus’.
Email is not configured by default — for info on how to configure it, search for the ‘
email’ command in the Oculus Command Reference, and associated settings info in the oculus_settings.txt config file.
host = "127.0.0.1"
username = "admin"
password = "KurwGDyd+oy+ZZ3S4qMn/wjeKOQ=" #encrypted password
port = 4444
emailsentonce = false
#send text over socket
puts "> "+str
#receive text over socket, waiting for pattern
servermsg = $oculussock.gets.strip
if servermsg =~ pattern then break end
# check battery status periodically, send email if draining
$oculussock = TCPSocket.new host, port
# get battery status, set batteryisdraining to TRUE if draining
batteryisdraining = waitForReplySearch(/^<state> batterystatus/) =~ /draining$/
# only send one email per battery draining instance, reset flag if charging
if not batteryisdraining then emailed = false end
# get number of connected rtmp users
rtmpusers = waitForReplySearch(/^<multiline> <messageclient> active RTMP users:/)[/\d+$/].to_i
# send warning email if battery draining, no connected users, and haven't already emailed
if batteryisdraining and rtmpusers == 0 and not emailed
sendString("email email@example.com [oculus battery draining] alert alert, battery draining "+Time.now().to_s)
emailed = true
sleep 600 # ten minutes