Class: HuaweiE3131

Inherits:
Object
  • Object
show all
Defined in:
lib/huawei_e3131.rb

Overview

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)

Instance Method Summary collapse

Constructor Details

#initialize(host = '192.168.1.1', callback: nil, autostart: false) ⇒ HuaweiE3131

Returns a new instance of HuaweiE3131.



28
29
30
31
32
33
34
# 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

Instance Method Details

#check_notificationsObject Also known as: notifications



36
37
38
39
40
# File 'lib/huawei_e3131.rb', line 36

def check_notifications()

  query 'monitoring/check-notifications'

end

#connection_statusObject Also known as: status



44
45
46
47
48
# File 'lib/huawei_e3131.rb', line 44

def connection_status()

  query 'monitoring/status'

end

#messagesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

#read(idx = 1) ⇒ Object

read an SMS message



76
77
78
# File 'lib/huawei_e3131.rb', line 76

def read(idx=1)  
  messages[idx.to_i-1]
end

#sms_countObject Also known as: count



80
81
82
83
84
# File 'lib/huawei_e3131.rb', line 80

def sms_count()

  query 'sms/sms-count'

end

#startObject

continuously check for new messages



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

#stopObject



115
116
117
118
119
120
121
# File 'lib/huawei_e3131.rb', line 115

def stop()

  @thread.stop
  @thread.kill

  'message checker stopped'
end