hilink_modem 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1a1df7c8728485e565ddc3f08f0ad51fde18d56
4
+ data.tar.gz: 27772b941b9788e582e3a37047262b4118e4007d
5
+ SHA512:
6
+ metadata.gz: b56e5c5cdace315e5ee47a14faa6855b22e1190f4cd58380a8ca0f3d643316aabd79426f274f9f57e33a3027356c9c5ef259dfa57a23c27374fddd8b8520c62f
7
+ data.tar.gz: fa0841ca0fe8778e0386f979c18983945df064bc057693e90467043194d3053a3fa8a49c50bd4f99ebde5b2a729e3e5b2d4b8be117456521438fab39c562cd5e
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ /RubyApplication8/nbproject/private/
3
+ /nbproject/private/
4
+ .hg*
5
+ .idea
6
+ commit
7
+ .DS_Store
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Hilink - ruby interface for Huawei Hilink web-frontend
2
+
3
+ ## Usage
4
+
5
+ The code supposes that the modem is connected and has the IP
6
+ 192.168.1.1. Then you can do:
7
+
8
+ ````
9
+ require 'hilink'
10
+ puts Hilink::Monitoring::status.inspect
11
+ ````
12
+
13
+ which will return a hash of all variables that the stick can return.
14
+
15
+ Please also see the much more complete PhP-implementation on
16
+ [BlackyPanther/Huawei-HiLink](https://github.com/BlackyPanther/Huawei-HiLink).
17
+
18
+ If you have the newer version which connects to 192.168.8.1, this library
19
+ won't work for you, sorry.
20
+
21
+ ## Commands
22
+
23
+ Each command is a module inside of Hilink. There are different modules:
24
+
25
+ - Monitoring - return different status of the modem
26
+ - Network - change 2g / 3g network
27
+ - SMS - send, receive and list
28
+ - Dialup - (dis)connect from network
29
+ - Modem - useful only when it is in modem-mode
30
+ - USSD - *Doesn't work due to Huawei restriction*
31
+
32
+ The Hilink-module itself has three methods:
33
+ - `send_request( path, request = {} )` - generates a valid XML-request and return
34
+ the result as a hash
35
+ - `switch_to_modem` - sends the command so the key behaves as a modem
36
+ - `switch_to_debug` - according to web-interface, no difference visible
37
+
38
+ ### Monitoring
39
+
40
+ You can monitor different things:
41
+
42
+ - `traffic_statistics` - per-connection and total usage
43
+ - `traffic_reset` - set all to 0
44
+ - `status` - shows whether connected or not
45
+ - `check_notifications`
46
+
47
+ ### Network
48
+
49
+ - `set_connection_type( mode = 'auto', band = '-1599903692' )` - mode
50
+ can be 'auto', '2g' or '3g'
51
+ - `current_plnm` - get actual connection
52
+
53
+ ### SMS
54
+
55
+ - `list`
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ LOAD_PATH.push %w( ../lib/ ../../HelperClasses/lib ../../QooxView/libs/activesupport-3.1.1/lib
3
+ ../../QooxView/libs/i18n-0.6.0/lib )
4
+ require 'hilink_modem'
5
+
6
+ puts HilinkModem::switch_to_modem
7
+ sleep 10
8
+ if File.exists? "/dev/ttyUSB0"
9
+ puts HilinkModem::Modem::save_modem
10
+ else
11
+ puts "Switch didn't work - sorry"
12
+ end
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'hilink_modem'
3
+ s.version = '0.3.0'
4
+ s.date = '2015-04-06'
5
+ s.summary = 'Accessing Huawei HilinkModem modems'
6
+ s.description = 'Using the web-interface of huawei hilink modems to do things'
7
+ s.authors = ['Linus Gasser']
8
+ s.email = 'ineiti@linusetviviane.ch'
9
+
10
+ s.files = `git ls-files -z`.split("\x0")
11
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
12
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
13
+ s.require_paths = ['lib']
14
+
15
+ s.add_runtime_dependency 'activesupport', '~> 4.1'
16
+ s.add_runtime_dependency 'serialport', '~> 1.3'
17
+ s.homepage = 'https://github.com/ineiti/HilinkModem'
18
+ s.license = 'GPLv3'
19
+ end
@@ -0,0 +1,172 @@
1
+ require 'net/http'
2
+ require 'active_support/core_ext'
3
+ require 'active_support'
4
+ require 'serialport'
5
+ require 'helperclasses/hashaccessor'
6
+
7
+
8
+ module HilinkModem
9
+ extend self
10
+ #using HelperClasses::HashAccessor
11
+
12
+ def send_request( path, request = {} )
13
+ url = "/api/#{path}"
14
+ http = Net::HTTP.new('192.168.1.1')
15
+
16
+ if request != {}
17
+ req = Net::HTTP::Post.new(url)
18
+ req.body = request.to_xml(root: 'request', indent: 0, skip_types: true)
19
+ req.content_type = 'text/xml'
20
+ else
21
+ req = Net::HTTP::Get.new(url)
22
+ end
23
+ begin
24
+ response = http.request(req).body
25
+ rescue Errno::ENETUNREACH => e
26
+ return nil
27
+ end
28
+
29
+ begin
30
+ Hash.from_xml( response )['response']
31
+ rescue REXML::ParseException => e
32
+ nil
33
+ end
34
+ end
35
+
36
+ def switch_to_modem
37
+ send_request( 'device/mode', :mode => 0 )
38
+ end
39
+
40
+ def switch_to_debug
41
+ send_request( 'device/mode', :mode => 1 )
42
+ end
43
+ module Modem
44
+ extend self
45
+
46
+ def send_modem( str )
47
+ sp = SerialPort.new('/dev/ttyUSB0',115200)
48
+ sp.write("#{str}\r\n")
49
+ sp.read_timeout = 100
50
+ sp.readlines
51
+ end
52
+
53
+ def switch_to_hilink
54
+ send_modem('AT^U2DIAG=119')
55
+ end
56
+
57
+ def save_modem
58
+ send_modem('AT^U2DIAG=0')
59
+ end
60
+ end
61
+ module Monitoring
62
+ extend self
63
+
64
+ def send_request( path, request = {} )
65
+ Hilink::send_request( "monitoring/#{path}", request )
66
+ end
67
+
68
+ def traffic_statistics
69
+ send_request('traffic-statistics')
70
+ end
71
+
72
+ def traffic_reset
73
+ send_request('clear-traffic')
74
+ end
75
+
76
+ def status
77
+ send_request('status')
78
+ end
79
+
80
+ def check_notifications
81
+ send_request('check-notifications')
82
+ end
83
+ end
84
+ module Network
85
+ extend self
86
+
87
+ def send_request( path, request = {} )
88
+ Hilink::send_request( "net/#{path}", request )
89
+ end
90
+
91
+ def current_plnm
92
+ send_request('current-plnm')
93
+ end
94
+
95
+ def set_connection_type( mode = 'auto', band: '-1599903692' )
96
+ nmode = case mode
97
+ when /2g/i
98
+ 1
99
+ when /3g/i
100
+ 2
101
+ else
102
+ 0
103
+ end
104
+ send_request( 'network', {
105
+ :NetworkMode => nmode,
106
+ :NetworkBand => band } )
107
+ end
108
+ end
109
+ module SMS
110
+ extend self
111
+
112
+ def send_request( path, request = {} )
113
+ Hilink::send_request( "sms/#{path}", request )
114
+ end
115
+
116
+ def list( box = 1, site: 1, pref_unread: 0, count: 20 )
117
+ ret = send_request( 'sms-list', {
118
+ :PageIndex => site,
119
+ :ReadCount => count,
120
+ :BoxType => box,
121
+ :SortType => 0,
122
+ :Ascending => 0,
123
+ :UnreadPreferred => pref_unread } )
124
+ if ret && ret['Messages']['Message'].class == Hash
125
+ ret['Messages']['Message'] = [ ret['Messages']['Message'] ]
126
+ end
127
+ ret
128
+ end
129
+
130
+ def delete( index )
131
+ send_request( 'delete-sms', { :Index => index } )
132
+ end
133
+
134
+ def send( number, message, index = -1 )
135
+ send_request( 'send-sms', {
136
+ :Index => index,
137
+ :Phones => [number].flatten,
138
+ :Sca => "",
139
+ :Content => message,
140
+ :Length => message.length,
141
+ :Reserved => 1,
142
+ :Date => Time.now.strftime('%Y-%m-%d %H:%M:%S') } )
143
+ end
144
+ end
145
+ module Dialup
146
+ extend self
147
+
148
+ def send_request( path, request = {} )
149
+ Hilink::send_request( "dialup/#{path}", request )
150
+ end
151
+
152
+ def connect
153
+ send_request( 'dial', :Action => 1 )
154
+ end
155
+
156
+ def disconnect
157
+ send_request( 'dial', :Action => 0 )
158
+ end
159
+ end
160
+ module USSD
161
+ extend self
162
+
163
+ def send_request( path, request = {} )
164
+ Hilink::send_request( "ussd/#{path}", request )
165
+ end
166
+
167
+ def send( str )
168
+ return :error => "Sorry, doesn't work!"
169
+ send_request( 'send', :content => str, :codeType => 'CodeType')
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,72 @@
1
+ require 'hilink_modem'
2
+
3
+ #puts HilinkModem::USSD::send( "*100#" )
4
+ #exit
5
+
6
+ #puts HilinkModem::switch_to_debug
7
+ #exit
8
+
9
+ #puts HilinkModem::Modem::switch_to_hilink
10
+ #puts HilinkModem::SMS::list.inspect
11
+ #exit
12
+
13
+ puts HilinkModem::switch_to_modem
14
+ exit
15
+
16
+ puts HilinkModem::Dialup::connect
17
+ puts HilinkModem::Dialup::disconnect
18
+ exit
19
+
20
+
21
+ #puts HilinkModem::SMS::send( "93999699", "hello there" )
22
+ puts HilinkModem::SMS::list.inspect
23
+ HilinkModem::SMS::list['Messages']['Message'].each{|m|
24
+ puts m.inspect
25
+ HilinkModem::SMS::delete( m['Index'] )
26
+ }
27
+ exit
28
+
29
+ #puts HilinkModem::send_request( "device/information" )
30
+ message = "Hello from Smileplug"
31
+ =begin
32
+ puts HilinkModem::send_request( "sms/send-sms",
33
+ { :Index => -1,
34
+ :Phones => ["93999699"],
35
+ :Sca => '',
36
+ :Content => message,
37
+ :Length => message.length,
38
+ :Reserved => 1,
39
+ :Date => Time.now.strftime('%Y-%m-%d %H:%M:%S') } )
40
+ =end
41
+ #puts HilinkModem::send_request( "monitoring/check-notifications" )
42
+ #puts HilinkModem::send_request( "monitoring/status" )
43
+ puts HilinkModem::send_request( "sms/sms-list",
44
+ { :PageIndex => 1,
45
+ :ReadCount => 20,
46
+ :BoxType => 1,
47
+ :SortType => 0,
48
+ :Ascending => 0,
49
+ :UnreadPreferred => 0 } )
50
+ puts HilinkModem::send_request( "sms/delete-sms", { :Index => 20067 } )
51
+
52
+ exit
53
+
54
+ a = Document.new
55
+ ( a.add_element "Index" ).text = -1
56
+ a.write $stdout
57
+
58
+ exit
59
+
60
+ url = 'http://192.168.1.1/api/monitoring/status'
61
+ url = 'http://192.168.1.1/api/device/information'
62
+
63
+ # get the XML data as a string
64
+ xml_data = Net::HTTP.get_response(URI.parse(url)).body
65
+
66
+ puts xml_data.inspect
67
+ # extract event information
68
+ doc = REXML::Document.new(xml_data)
69
+
70
+ XPath.each( doc, "//response/*" ){|a|
71
+ puts a.inspect, a.text
72
+ }
data/test/start_test ADDED
@@ -0,0 +1,8 @@
1
+ #!/bin/bash
2
+ if [ ! "$1" ]; then
3
+ echo "No argument given"
4
+ exit 1
5
+ fi
6
+
7
+ ruby -I../lib/ -I../../HelperClasses/lib -I../../QooxView/libs/activesupport-3.1.1/lib \
8
+ -I../../QooxView/libs/i18n-0.6.0/lib $1
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hilink_modem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Linus Gasser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: serialport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ description: Using the web-interface of huawei hilink modems to do things
42
+ email: ineiti@linusetviviane.ch
43
+ executables:
44
+ - hilink_to_modem.rb
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - README.md
50
+ - bin/hilink_to_modem.rb
51
+ - hilink_modem.gemspec
52
+ - lib/hilink_modem.rb
53
+ - test/hilink_test.rb
54
+ - test/start_test
55
+ homepage: https://github.com/ineiti/HilinkModem
56
+ licenses:
57
+ - GPLv3
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.2.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Accessing Huawei HilinkModem modems
79
+ test_files:
80
+ - test/hilink_test.rb
81
+ - test/start_test