Documentation
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.
# batt_draining_email.rb
require 'socket'
host = "127.0.0.1"
username = "admin"
password = "KurwGDyd+oy+ZZ3S4qMn/wjeKOQ=" #encrypted password
port = 4444
$oculussock
emailsentonce = false
#FUNCTIONS
#send text over socket
def sendString(str)
$oculussock.puts str
puts "> "+str
end
#receive text over socket, waiting for pattern
def waitForReplySearch(pattern)
while true
servermsg = $oculussock.gets.strip
puts servermsg
if servermsg =~ pattern then break end
end
return servermsg
end
#MAIN
# check battery status periodically, send email if draining
while true
#login
$oculussock = TCPSocket.new host, port
waitForReplySearch(/^<telnet> LOGIN/)
sendString(username+":"+password)
# get battery status, set batteryisdraining to TRUE if draining
sendString("battstats")
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
sendString("who")
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 bob@example.com [oculus battery draining] alert alert, battery draining "+Time.now().to_s)
emailed = true
end
sendString("quit") #logout
$oculussock.close
sleep 600 # ten minutes
end
|
|
|
|
|