Class: MosesacsSdk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mosesacs-sdk/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 80) ⇒ Client



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mosesacs-sdk/client.rb', line 12

def initialize host, port=80
    @q = Queue.new
    @t = Thread.new do
        EM.run {
            scheme = 'ws'
            url = "ws://#{host}:#{port}/api"
            headers = {'Origin' => 'http://localhost'}
            @ws = Faye::WebSocket::Client.new(url, nil, :headers => headers)

            @ws.onopen = lambda do |event|
                # p [:open]
                # @ws.send({"Cmd" => "readMib 00507F5FC268 InternetGatewayDevice.Time."}.to_json)
                @q.push("ready")
            end

            @ws.onmessage = lambda do |event|                        
                # p [:message, event.data]                            
                d = JSON.parse(event.data)
                if d["Cmd"] =~ /soap/
                    @q.push d["Cmd"]
                else
                    # ignore logs
                    puts d['reason']                     
                    @q.push ""
                end

                # ws.close 1002, 'Going away'
            end

            @ws.onclose = lambda do |event|
                # p [:close, event.code, event.reason]
                # puts "disconnected"
                EM.stop
                exit 1
            end
        }
    end
    @q.pop
    # puts "Connected..."
end

Instance Method Details

#CancelTransfer(serial) ⇒ Object



80
81
82
83
84
# File 'lib/mosesacs-sdk/client.rb', line 80

def CancelTransfer (serial) 
    @ws.send({"MsgType" => "command", "Data" => {"command" => "canceltransfer", "serial" => serial}}.to_json)
    resp = @q.pop
    puts resp if resp != ""
end

#ChangeDuState(serial, ops) ⇒ Object



68
69
70
71
72
# File 'lib/mosesacs-sdk/client.rb', line 68

def ChangeDuState (serial, ops)
    @ws.send({"MsgType" => "command", "Data" => {"command" => "changeDuState #{serial}", "ops" => ops}}.to_json)
    resp = @q.pop
    puts resp if resp != ""
end

#Download(serial, url, username, password, filetype, filesize) ⇒ Object



74
75
76
77
78
# File 'lib/mosesacs-sdk/client.rb', line 74

def Download (serial, url, username, password, filetype, filesize)
    @ws.send({"MsgType" => "command", "Data" => {"command" => "download", "serial" => serial, "url" => url, "username" => username, "password" => password, "filetype" => filetype, "filesize" => filesize}}.to_json)
    resp = @q.pop
    puts resp if resp != ""
end

#GetParameterValues(serial, leaf) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mosesacs-sdk/client.rb', line 53

def GetParameterValues (serial, leaf)
    @ws.send({"MsgType" => "command", "Data" => {"command" => "readMib #{serial} #{leaf}"}}.to_json)            
    resp = @q.pop

    doc = Nokogiri::XML(resp)
    message_type = doc.css("soap|Body").children.map(&:name)[1]
    if message_type == "Fault"
        raise "Got Fault Message: #{doc.css("FaultString")[0].text}"
    end

    doc.css("ParameterValueStruct").each do |par|
        puts "#{par.children[1].text}: #{par.children[3].text}"
    end
end

#ScheduleDownload(serial, url, username, password, filetype, filesize, windows) ⇒ Object



86
87
88
89
90
# File 'lib/mosesacs-sdk/client.rb', line 86

def ScheduleDownload(serial, url, username, password, filetype, filesize, windows)
    @ws.send({"MsgType" => "command", "Data" => {"command" => "scheduledownload", "serial" => serial, "url" => url, "username" => username, "password" => password, "filetype" => filetype, "filesize" => filesize, "windows" => windows}}.to_json)
    resp = @q.pop
    puts resp if resp != ""
end