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 = ""
username = "admin"
password = "KurwGDyd+oy+ZZ3S4qMn/wjeKOQ=" #encrypted password
port = 4444
emailsentonce = false


#send text over socket
def sendString(str)
    $oculussock.puts str
    puts "> "+str    

#receive text over socket, waiting for pattern
def waitForReplySearch(pattern)
    while true
        servermsg = $oculussock.gets.strip
        puts servermsg
        if servermsg =~ pattern then break end
    return servermsg


# check battery status periodically, send email if draining
while true
    $oculussock = host, port
    waitForReplySearch(/^<telnet> LOGIN/)
    # 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 [oculus battery draining] alert alert, battery draining "
        emailed = true
    sendString("quit")   #logout

    sleep 600 # ten minutes

CART 0 items | US$
    Check Order Status
Follow →
instagram twitter facebook youtube
@XaxxonTech Twitter
© 2020 Xaxxon
Products Documentation Downloads Source Code
Warranty & Returns About Us Contact/Support News
instagram twitter facebook youtube