mqtt-sn-ruby 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2f0d1172be32ff8b6c6c5273269ff4f720557eb
4
- data.tar.gz: 8bf5058605ef6eee4878f7479dea438503f5bdfe
3
+ metadata.gz: d8d215dd5f4e11c65675f57b7b4cf605f759931f
4
+ data.tar.gz: 8f9072ae324bd014b1051dc0dc7dddfdecf0e1ac
5
5
  SHA512:
6
- metadata.gz: c44fb49055e484380ed709b8aae823ffcd5208855d28c6044d7216e876d2892f1c3540a46ea880b0e1d82be1f0f0e56fe6b8d1f2ee36c0403f64a42aba51d167
7
- data.tar.gz: ca80dc72080307d9761dfc9444a2d44957ebd8c12cfeeda1e49f4824bb41b22814d4b0d7077553d3f0bfab706eba6b6bc8e365563dc44fddc8a3b342b6002daf
6
+ metadata.gz: 6bd5a86aecc7bc02622e1e6dd7d36b6e45de68f0c5322d7ed221da3a0d129033b0a34110092ac3d4ed8614a69b658ef466afe4fe237d5a0c3814fd1d3b10b695
7
+ data.tar.gz: 0bc34db03e72c227dc7faf9e4df00ecd10f8d03cdd6405378938a54a555ff31c4cb893cbfa1da62ac87dd15f3cf82acc3d3b068dd80fdc5a63576a5f68942e4e
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+ # encode: UTF-8
3
+
4
+ require "pp"
5
+ require 'socket'
6
+ require 'json'
7
+ require 'optparse'
8
+ if File.file? './lib/mqtt-sn-ruby.rb'
9
+ require './lib/mqtt-sn-ruby.rb'
10
+ puts "using local lib"
11
+ else
12
+ require 'mqtt-sn-ruby'
13
+ end
14
+
15
+ options = {}
16
+ OptionParser.new do |opts|
17
+ opts.banner = "Usage: mqtt-sn-pub.rb [options]"
18
+
19
+ opts.on("-v", "--[no-]verbose", "Run verbosely (false)") do |v|
20
+ options[:verbose] = v
21
+ end
22
+ opts.on("-d", "--[no-]debug", "Produce Debug dump on console (false)") do |v|
23
+ options[:debug] = v
24
+ end
25
+ opts.on("-h", "--host host", "MQTT-SN Host to connect (localhost)") do |v|
26
+ options[:server] = v
27
+ end
28
+ opts.on("-p", "--port port", "MQTT-SN Port to connect (1883)") do |v|
29
+ options[:port] = v.to_i
30
+ end
31
+ opts.on("-q", "--qos level", "QoS level (0)") do |v|
32
+ options[:qos] = v.to_i
33
+ end
34
+ opts.on("-i", "--id id", "This client's id -- free choice (hostname-pid)") do |name|
35
+ options[:id] = name
36
+ end
37
+ opts.on("-m", "--msg msg", "Message to send (test_value)") do |msg|
38
+ options[:msg] = msg
39
+ end
40
+ opts.on("-t", "--topic topic", "Topic to use (test/message/123)") do |topic|
41
+ options[:topic] = topic
42
+ end
43
+ end.parse!
44
+
45
+ puts "MQTT-SN-PUB: #{options.to_json}"
46
+ begin
47
+ sn=MqttSN.new options
48
+ sn.connect options[:id]
49
+ sn.publish options[:topic]||"test/message/123", options[:msg]||"test_value", qos: options[:qos]
50
+ puts "Sent ok."
51
+ rescue SystemExit, Interrupt
52
+ puts "\nExiting after Disconnect\n"
53
+ rescue => e
54
+ puts "\nError: '#{e}' -- Quit after Disconnect\n"
55
+ end
56
+ sn.disconnect
57
+
58
+ puts "MQTT-SN-PUB Done."
59
+
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ # encode: UTF-8
3
+
4
+ require "pp"
5
+ require 'socket'
6
+ require 'json'
7
+ require 'optparse'
8
+ if File.file? './lib/mqtt-sn-ruby.rb'
9
+ require './lib/mqtt-sn-ruby.rb'
10
+ puts "using local lib"
11
+ else
12
+ require 'mqtt-sn-ruby'
13
+ end
14
+
15
+ options = {}
16
+ OptionParser.new do |opts|
17
+ opts.banner = "Usage: mqtt-sn-sub.rb [options]"
18
+
19
+ opts.on("-v", "--[no-]verbose", "Run verbosely (false)") do |v|
20
+ options[:verbose] = v
21
+ end
22
+ opts.on("-d", "--[no-]debug", "Produce Debug dump on console (false)") do |v|
23
+ options[:debug] = v
24
+ end
25
+ opts.on("-h", "--host host", "MQTT-SN Host to connect (localhost)") do |v|
26
+ options[:server] = v
27
+ end
28
+ opts.on("-p", "--port port", "MQTT-SN Port to connect (1883)") do |v|
29
+ options[:port] = v.to_i
30
+ end
31
+ opts.on("-q", "--qos level", "QoS level (0)") do |v|
32
+ options[:qos] = v.to_i
33
+ end
34
+ opts.on("-i", "--id id", "This client's id -- free choice (hostname-pid)") do |name|
35
+ options[:id] = name
36
+ end
37
+ opts.on("-t", "--topic topic", "Topic to subscribe (test/message/123)") do |topic|
38
+ options[:topic] = topic
39
+ end
40
+ end.parse!
41
+
42
+ puts "MQTT-SN-SUB: #{options.to_json}"
43
+ begin
44
+ sn=MqttSN.new options
45
+ sn.connect options[:id]
46
+ sn.subscribe options[:topic]||"test/message/123", qos: options[:qos] do |s,m|
47
+ if s==:sub_ack
48
+ puts "Subscribed Ok! Waiting for Messages!"
49
+ else
50
+ puts "Got Message: #{m}"
51
+ end
52
+ end
53
+ puts "Disconnected..."
54
+ rescue SystemExit, Interrupt
55
+ puts "\nExiting after Disconnect\n"
56
+ rescue => e
57
+ puts "\nError: '#{e}' -- Quit after Disconnect\n"
58
+ end
59
+ sn.disconnect
60
+
61
+ puts "MQTT-SN-SUB Done."
62
+
data/lib/mqtt-sn-ruby.rb CHANGED
@@ -49,6 +49,7 @@ class MqttSN
49
49
  @server=hash[:server]||"127.0.0.1"
50
50
  @port=hash[:port]||1883
51
51
  @debug=hash[:debug]
52
+ @verbose=hash[:verbose]
52
53
  @state=:inited
53
54
  @will_topic=nil
54
55
  @will_msg=nil
@@ -61,13 +62,9 @@ class MqttSN
61
62
  @t=Thread.new do
62
63
  recv_thread
63
64
  end
64
- puts "opened ok"
65
- pp @s
66
65
  end
67
66
 
68
- def close
69
- proc { puts "DESTROY OBJECT #{bar}" }
70
- end
67
+
71
68
 
72
69
  def hexdump data
73
70
  raw=""
@@ -90,15 +87,20 @@ class MqttSN
90
87
  hexdump msg
91
88
  end
92
89
 
93
-
94
90
  def send type,hash={},&block
95
- puts ""
91
+ puts "" if @verbose
96
92
  if @state!=:connected and type!=:connect and type!=:will_topic and type!=:will_msg
97
- raise "Error: Cannot #{type} while unconnected, send :connect first!"
93
+ if type==:disconnect
94
+ return #already disconnected.. nothing to do
95
+ else
96
+ raise "Error: Cannot #{type} while unconnected, send :connect first!"
97
+ end
98
98
  end
99
99
  case type
100
100
  when :connect
101
- raise "Need :id, it is required at :connect!" if not hash[:id]
101
+ if not hash[:id]
102
+ hash[:id]="xxx"
103
+ end
102
104
  flags=0
103
105
  flags+=CLEAN_FLAG if hash[:clean]
104
106
  flags+=RETAIN_FLAG if hash[:retain]
@@ -213,7 +215,7 @@ class MqttSN
213
215
  end
214
216
  raw=send_packet p
215
217
  hash[:raw]=raw if @debug
216
- puts "send:#{@id} #{type},#{hash.to_json}"
218
+ puts "send:#{@id} #{type},#{hash.to_json}" if @verbose
217
219
  timeout=hash[:timeout]||Tretry
218
220
  retries=0
219
221
  if hash[:expect]
@@ -346,7 +348,7 @@ class MqttSN
346
348
  else
347
349
  raise "Error:#{@id} Register topic #{topic} failed!"
348
350
  end
349
- pp @topics
351
+ #pp @topics
350
352
  end
351
353
  @topics[topic]
352
354
  end
@@ -435,7 +437,7 @@ class MqttSN
435
437
  topic=r[6,len-6]
436
438
  m={type: :register, topic_id: topic_id, msg_id: msg_id, topic: topic,status: :ok}
437
439
  @topics[topic]=m[:topic_id]
438
- pp @topics
440
+ #pp @topics
439
441
  send :register_ack,topic_id: topic_id, msg_id: msg_id, return_code: 0
440
442
  done=true
441
443
  when REGACK_TYPE
@@ -471,7 +473,7 @@ class MqttSN
471
473
  if @debug and m
472
474
  m[:raw]=hexdump r
473
475
  end
474
- puts "got :#{@id} #{m.to_json}"
476
+ puts "got :#{@id} #{m.to_json}" if @verbose
475
477
  if not done
476
478
  @iq<<m if m
477
479
  end
data/test.rb CHANGED
@@ -49,6 +49,7 @@ sn2.publish "eka/4","2perkkule0",qos: 0
49
49
  sn2.publish "eka/5","2perkkule1rrrr",qos: 1, retain: true
50
50
  sn2.publish "eka/6","2perkkule2",qos: 2
51
51
  sn2.disconnect
52
+ sn2=nil
52
53
 
53
54
  #puts "----------------- sleep"
54
55
  #sn2.goto_sleep 4
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mqtt-sn-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Siitonen
@@ -13,10 +13,14 @@ dependencies: []
13
13
  description: Ruby toolkit for MQTT-SN, compatible with RSMB, command line tools and
14
14
  API
15
15
  email: jalopuuverstas@gmail.com
16
- executables: []
16
+ executables:
17
+ - mqtt-sn-pub.rb
18
+ - mqtt-sn-sub.rb
17
19
  extensions: []
18
20
  extra_rdoc_files: []
19
21
  files:
22
+ - bin/mqtt-sn-pub.rb
23
+ - bin/mqtt-sn-sub.rb
20
24
  - lib/mqtt-sn-ruby.rb
21
25
  - test.rb
22
26
  homepage: https://github.com/arisi/mqtt-sn-ruby