class HuaweiE3131

HOWTO: Switching the Huawei E3131 to network controller mode

  1. Plug in the Huawei E3131 dongle

  2. `sudo apt-get install sg3-utils`

  3. `sudo /usr/bin/sg_raw /dev/sr0 11 06 20 00 00 00 00 00 01 00`

  4. edit /etc/network/interfaces and add the following:

    allow-hotplug eth1
    iface eth1 inet dhcp
  5. observe IP address 192.168.1.1 is now reachable from eth1

credit: [Huawei E3131 on Wheezy](www.raspberrypi.org/forums/viewtopic.php?p=210958)

Public Class Methods

new(host='192.168.1.1', callback: nil, autostart: false) click to toggle source
# File lib/huawei_e3131.rb, line 28
def initialize(host='192.168.1.1', callback: nil, autostart: false)
  
  @host, @callback = host, callback
  
  start if autostart
  
end

Public Instance Methods

check_notifications() click to toggle source
# File lib/huawei_e3131.rb, line 36
def check_notifications()

  query 'monitoring/check-notifications'

end
Also aliased as: notifications
connection_status() click to toggle source
# File lib/huawei_e3131.rb, line 44
def connection_status()

  query 'monitoring/status'

end
Also aliased as: status
count()
Alias for: sms_count
messages() click to toggle source
# File lib/huawei_e3131.rb, line 52
def messages()

  options = {PageIndex: 1, ReadCount: 20, BoxType: 1, SortType: 0, 
             Ascending: 0, UnreadPreferred: 0}

  a = RexleBuilder.new(options, debug: false).to_a
  a[0] = 'request'
  xml = Rexle.new(a).xml(declaration: false)    
  
  response = RestClient.post "http://#{@host}/api/sms/sms-list", xml.to_s, 
      :content_type => "text/xml"        

  Rexle.new(response.body).root.xpath('Messages/Message').map do |message|

    message.xpath('./*').inject({}) do |r,x|
      x.text ? r.merge(x.name => x.text.unescape) : r
    end

  end

end
notifications()
Alias for: check_notifications
read(idx=1) click to toggle source

read an SMS message

# File lib/huawei_e3131.rb, line 76
def read(idx=1)  
  messages[idx.to_i-1]
end
sms_count() click to toggle source
# File lib/huawei_e3131.rb, line 80
def sms_count()

  query 'sms/sms-count'

end
Also aliased as: count
start() click to toggle source

continuously check for new messages

# File lib/huawei_e3131.rb, line 90
def start()
  
  unread = notifications()['UnreadMessage'].to_i

  @thread = Thread.new do
    loop do

      unread_messages = notifications()['UnreadMessage'].to_i

      if unread_messages > unread then

        @callback.call if @callback
        unread = unread_messages

      end

      sleep 3

    end
  end

  'checking for new message every 3 seconds ...'

end
status()
Alias for: connection_status
stop() click to toggle source
# File lib/huawei_e3131.rb, line 115
def stop()

  @thread.stop
  @thread.kill

  'message checker stopped'
end

Private Instance Methods

query(s) click to toggle source

returns a Hash object from the flat XML records returned from the response

# File lib/huawei_e3131.rb, line 127
def query(s)

  response = RestClient.get "http://#{@host}/api/" + s

  Rexle.new(response.body).root.xpath('./*').inject({}) do |r,x|
    x.text ? r.merge(x.name => x.text.unescape) : r
  end

end