mqtt-sn-ruby 0.0.18 → 0.1.0

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: 355869b8237db560d527d053840b5cf3e6004f67
4
- data.tar.gz: 7eb4e42c4d935ad629fdaf53709953d5e87c8acd
3
+ metadata.gz: ccc99a508bb40d3261b690b13a97094bb257210b
4
+ data.tar.gz: 04af70b7e0a86a63044631c9e6daa41c273509c5
5
5
  SHA512:
6
- metadata.gz: 6b04c285a66b26ce92e411df4f22c292c8de65b685f8ec2e11c904745fc8c6c3dfa72fbfe3be85f53e9e07330632df723da80e42555bd5814b83f7137ca67966
7
- data.tar.gz: 86bbb6373d0790ed3e78e0123d8d4cfc0190093f2db248876650863cc5cdf39a5b9afddaaf7fba666fecb4f17b716a09291bf9debf7f63d31e8f3cabfd835111
6
+ metadata.gz: 6d1cc0c33d1becf9986eeffe185352436a4e8ebbb4b81c9ddaa0cab5a9433f92f661e890d570962bbbbf44a3f8f145bb81992078bbc38c96d06d53d2dd60e354
7
+ data.tar.gz: 5d385953fa575f7537634a5c15b3d1267abac839514191be53a00089b5f113a397ee62959e13f2f55bd30d1fba07bdf5e3859ca01ec57c0c59740e7725010566
@@ -28,6 +28,10 @@ OptionParser.new do |opts|
28
28
  opts.on("-s", "--server uri", "URI of the MQTT-SN Server to connect to (udp://localhost:1883)") do |v|
29
29
  options[:server_uri] = v
30
30
  end
31
+ options[:broadcast_uri] = "udp://225.4.5.6:5000"
32
+ opts.on("-b", "--broadcast uri", "Multicast URI for ADVERTISE, SEARCHGW and GWINFO (udp://225.4.5.6:5000)") do |v|
33
+ options[:broadcast_uri] = v
34
+ end
31
35
  opts.on("-l", "--localport port", "MQTT-SN local port to listen (1882)") do |v|
32
36
  options[:local_port] = v.to_i
33
37
  end
@@ -41,31 +41,14 @@ OptionParser.new do |opts|
41
41
  end.parse!
42
42
 
43
43
  puts "MQTT-SN-PUB: #{options.to_json}"
44
+ sn=MqttSN.new options
44
45
  begin
45
- sn=MqttSN.new options
46
- sent=false
47
- if options[:qos]==-1
48
- sn.publish options[:topic]||"XX", options[:msg]||"test_value", qos: options[:qos]
49
- puts "Sent."
50
- else
51
- while not sent
52
- sn.connect options[:id] do |s,m|
53
- if s==:ok
54
- sn.publish options[:topic]||"test/message/123", options[:msg]||"test_value", qos: options[:qos]
55
- puts "Sent ok."
56
- sent=true
57
- else
58
- sn.disconnect
59
- end
60
- end
61
- end
62
- end
63
- sn.log_flush
46
+ sn.pub options
64
47
  rescue SystemExit, Interrupt
65
48
  puts "\nExiting after Disconnect\n"
66
49
  rescue => e
67
50
  puts "\nError: '#{e}' -- Quit after Disconnect\n"
68
- pp e.backtrace
51
+ e.backtrace
69
52
  end
70
53
  sn.disconnect if sn
71
54
 
@@ -16,28 +16,33 @@ options = {}
16
16
  OptionParser.new do |opts|
17
17
  opts.banner = "Usage: mqtt-sn-sub.rb [options]"
18
18
 
19
- opts.on("-v", "--[no-]verbose", "Run verbosely (false)") do |v|
19
+ opts.on("-v", "--[no-]verbose", "Run verbosely; creates protocol log on console (false)") do |v|
20
20
  options[:verbose] = v
21
21
  end
22
- opts.on("-d", "--[no-]debug", "Produce Debug dump on console (false)") do |v|
22
+ opts.on("-d", "--[no-]debug", "Produce Debug dump on verbose log (false)") do |v|
23
23
  options[:debug] = v
24
24
  end
25
- opts.on("-s", "--server uri"," URI of the MQTT-SN Server to connect to. Example udp://localhost:1883. Default: Use Autodiscovery.") do |v|
25
+ opts.on("-s", "--server uri","URI of the MQTT-SN Server to connect to. Example udp://localhost:1883. Default: Use Autodiscovery.") do |v|
26
26
  options[:server_uri] = v
27
27
  end
28
- opts.on("-q", "--qos level", "QoS level (0)") do |v|
28
+ options[:broadcast_uri] = "udp://225.4.5.6:5000"
29
+ opts.on("-b", "--[no-]broadcast uri", "Multicast URI for Autodiscovery: ADVERTISE, SEARCHGW and GWINFO (udp://225.4.5.6:5000)") do |v|
30
+ options[:broadcast_uri] = v
31
+ end
32
+ opts.on("-q", "--qos level", "QoS level -1,0,1 or 2. (0)") do |v|
29
33
  options[:qos] = v.to_i
30
34
  end
31
35
  opts.on("-i", "--id id", "This client's id -- free choice (hostname-pid)") do |name|
32
36
  options[:id] = name
33
37
  end
34
- opts.on("-t", "--topic topic", "Topic to subscribe (test/message/123)") do |topic|
38
+ options[:topic] = "#"
39
+ opts.on("-t", "--topic topic", "Topic to Subscribe (#)") do |topic|
35
40
  options[:topic] = topic
36
41
  end
37
42
  opts.on("-h", "--http port", "Http port for debug/status JSON server (false)") do |v|
38
43
  options[:http_port] = v.to_i
39
44
  end
40
- opts.on("-k", "--keepalive dur", "Keepalive timer, in seconds. Will ping with this interval. (25)") do |topic|
45
+ opts.on("-k", "--keepalive dur", "Keepalive timer, in seconds. Will ping Server with this interval. (25)") do |topic|
41
46
  options[:keepalive] = topic
42
47
  end
43
48
  end.parse!
@@ -77,24 +82,8 @@ end
77
82
 
78
83
  puts "MQTT-SN-SUB: #{options.to_json}"
79
84
  begin
80
- loop do
81
- puts "Connecting.."
82
- $sn.connect options[:id] do |cs,cm|
83
- puts "connect result: #{cs} #{cm}"
84
- if cs==:ok
85
- puts "Subscribing.."
86
- $sn.subscribe options[:topic]||"test/message/123", qos: options[:qos] do |s,m|
87
- if s==:sub_ack
88
- puts "Subscribed Ok! Waiting for Messages!"
89
- elsif s==:disconnect
90
- puts "Disconnected -- switch to new gateway"
91
- else
92
- puts "Got Message: #{s}: #{m}"
93
- end
94
- end
95
- end
96
- end
97
- puts "Disconnected..."
85
+ $sn.sub options do |status,msg|
86
+ $sn.note "Got Message '#{msg[:msg]}' with Topic '#{msg[:topic]}'"
98
87
  end
99
88
  rescue SystemExit, Interrupt
100
89
  puts "\nExiting after Disconnect\n"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mqtt-sn-ruby'
4
+
5
+ begin
6
+ sn=MqttSN.new server_uri: "udp://mqtt.fi:1882"
7
+ sn.sub do |status,msg|
8
+ sn.note "Got Message '#{msg[:msg]}' with Topic '#{msg[:topic]}'"
9
+ end
10
+ rescue SystemExit, Interrupt
11
+ puts "\nExiting after Disconnect\n"
12
+ end
13
+ sn.disconnect
14
+ puts "Exiting."
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mqtt-sn-ruby'
4
+
5
+ sn=MqttSN.new server_uri: "udp://mqtt.fi:1882"
6
+ sn.pub msg: "testing"
7
+ sn.disconnect
@@ -65,7 +65,8 @@ class MqttSN
65
65
  end
66
66
 
67
67
  def note str,*args
68
- @log_q << sprintf(str,*args)
68
+ s=sprintf(str,*args)
69
+ @log_q << sprintf("%s: %s",Time.now.iso8601,s)
69
70
  end
70
71
 
71
72
  def log_empty?
@@ -139,7 +140,7 @@ class MqttSN
139
140
  @verbose=hash[:verbose]
140
141
  @state=:inited
141
142
  @bcast_port=5000
142
- @keepalive=hash[:keepalive]||25
143
+ @keepalive=(hash[:keepalive]||25).to_i
143
144
  @forwarder=hash[:forwarder] #flag to indicate forward mode
144
145
  @will_topic=nil
145
146
  @will_msg=nil
@@ -152,16 +153,18 @@ class MqttSN
152
153
  @clients={}
153
154
  @gateways={}
154
155
  @autodiscovery=false
155
- @broadcast_uri="udp://225.4.5.6:5000"
156
+ @broadcast_uri=hash[:broadcast_uri]
156
157
 
157
158
  if @server_uri
158
- puts "adding default gateway #{@server_uri}"
159
+ puts "Using Default Gateway: #{@server_uri}"
159
160
  @gateways[0]={stamp: Time.now.to_i,uri: @server_uri, duration: 0, source: 'default', status: :ok}
160
161
  pick_new_gateway
161
- pp @gateways
162
- else
163
- puts "Autodiscovery Active"
162
+ elsif @broadcast_uri
163
+ puts "Autodiscovery Active, using #{@broadcast_uri}"
164
164
  @autodiscovery=true
165
+ else
166
+ puts "No autodiscovery and no Default Gateway -- cannot proceed"
167
+ exit -1
165
168
  end
166
169
 
167
170
 
@@ -173,15 +176,17 @@ class MqttSN
173
176
  @iq = Queue.new
174
177
  @dataq = Queue.new
175
178
 
176
- @bcast_s=open_multicast_send_port
177
- @bcast=open_multicast_recv_port
179
+ if @broadcast_uri
180
+ @bcast_s=open_multicast_send_port
181
+ @bcast=open_multicast_recv_port
178
182
 
179
- @roam_t=Thread.new do
180
- roam_thread @bcast
183
+ @roam_t=Thread.new do
184
+ roam_thread @bcast
185
+ end
181
186
  end
182
187
  if @forwarder
183
188
  @s,@server,@port = MqttSN::open_port @server_uri
184
- puts "Open port to Gateway: #{@server_uri}: #{@server},#{@port} -- local port: #{@local_port}"
189
+ puts "Open port to Gateway: #{@server_uri}: #{@server},#{@port} -- Listening local port: #{@local_port}"
185
190
  @local_port=hash[:local_port]||1883
186
191
  @s.bind("0.0.0.0",@local_port)
187
192
  @bcast_period=60
@@ -382,7 +387,6 @@ class MqttSN
382
387
  @msg_id+=1
383
388
 
384
389
  when :publish
385
- puts "topic id: #{hash[:topic_id]}"
386
390
  raise "Need :topic_id to Publish!" if not hash[:topic_id]
387
391
  qos=hash[:qos]||0
388
392
  flags=0
@@ -517,12 +521,12 @@ class MqttSN
517
521
  debug[:debug]=MqttSN::hexdump(msg) if @debug
518
522
 
519
523
  if not @active_gw_id or not @gateways[@active_gw_id] or not @gateways[@active_gw_id][:socket]
520
- print "No active gw, wait ."
524
+ note "No active gw, wait ."
521
525
  while not @active_gw_id or not @gateways[@active_gw_id] or not @gateways[@active_gw_id][:socket]
522
526
  ret="-"
523
527
  if not ret=pick_new_gateway
524
528
  sleep 0.5
525
- print "."
529
+ #print "."
526
530
  end
527
531
  waits+=1
528
532
  if waits>30
@@ -530,7 +534,7 @@ class MqttSN
530
534
  return
531
535
  end
532
536
  end
533
- puts " Ok!"
537
+ note "Gw Ok!"
534
538
  end
535
539
 
536
540
  if @active_gw_id and @gateways[@active_gw_id] and @gateways[@active_gw_id][:socket]
@@ -600,20 +604,16 @@ class MqttSN
600
604
  send :connect,id: id, clean: false, duration: @keepalive, expect: [:connect_ack,:will_topic_req] do |s,m| #add will here!
601
605
  if s==:ok
602
606
  if m[:type]==:will_topic_req
603
- puts "will topic!"
604
607
  send :will_topic, topic: @will_topic, expect: [:will_msg_req] do |s,m| #add will here!
605
608
  if s==:ok
606
- puts "will msg!"
607
609
  send :will_msg, msg: @will_msg, expect: [:connect_ack] do |s,m|
608
610
  end
609
611
  end
610
612
  end
611
613
  elsif m[:type]==:connect_ack
612
- puts "connected!---------------"
613
614
  block.call :ok,m if block
614
615
  end
615
616
  else
616
- puts "failed to connect"
617
617
  block.call :fail,m if block
618
618
  end
619
619
  end
@@ -683,10 +683,7 @@ class MqttSN
683
683
  end
684
684
 
685
685
  def publish topic,msg,hash={}
686
- puts "op='#{topic}','#{topic[0]}'"
687
-
688
686
  if topic[0]=="="
689
- puts "yeah"
690
687
  topic[0]=""
691
688
  topic_id=topic.to_i
692
689
  topic_type=:predefined
@@ -844,7 +841,6 @@ class MqttSN
844
841
  end
845
842
  when :publish
846
843
  if m[:topic_type]==:long
847
- puts "on pitkä #{m[:topic_id]} ->#{@topics.key(m[:topic_id])}"
848
844
  m[:topic]=@topics.key(m[:topic_id])
849
845
  end
850
846
  if not @transfer
@@ -955,7 +951,7 @@ class MqttSN
955
951
  @gsem.synchronize do #one command at a time --
956
952
 
957
953
  if @active_gw_id # if using one, mark it used, so it will be last reused
958
- note "closing gw #{@active_gw_id} cause: #{cause}"
954
+ note "Closing gw #{@active_gw_id} cause: #{cause}"
959
955
  @gateways[@active_gw_id][:last_use]=Time.now.to_i
960
956
  if @gateways[@active_gw_id][:socket]
961
957
  @gateways[@active_gw_id][:socket].close
@@ -982,7 +978,7 @@ class MqttSN
982
978
  end
983
979
  if pick
984
980
  @active_gw_id=pick
985
- note "using gateway #{@active_gw_id} #{@gateways[@active_gw_id][:uri]}"
981
+ note "Opening Gateway #{@active_gw_id}: #{@gateways[@active_gw_id][:uri]}"
986
982
  @s,@server,@port = MqttSN::open_port @gateways[@active_gw_id][:uri]
987
983
  @gateways[@active_gw_id][:socket]=@s
988
984
  @gateways[@active_gw_id][:last_use]=Time.now.to_i
@@ -1050,5 +1046,54 @@ class MqttSN
1050
1046
  end
1051
1047
 
1052
1048
 
1049
+ #toplevel funcs:
1050
+ def pub options={}
1051
+ pp options
1052
+ sent=false
1053
+ if options[:qos]==-1
1054
+ publish options[:topic]||"XX", options[:msg]||"test_value", qos: options[:qos]
1055
+ puts "Sent."
1056
+ else
1057
+ while not sent
1058
+ connect options[:id] do |s,m|
1059
+ if s==:ok
1060
+ publish options[:topic]||"test/message/123", options[:msg]||"test_value", qos: options[:qos]
1061
+ puts "Sent ok."
1062
+ sent=true
1063
+ else
1064
+ disconnect
1065
+ end
1066
+ end
1067
+ end
1068
+ end
1069
+ log_flush
1070
+ end
1071
+
1072
+ def sub options={},&block
1073
+ loop do
1074
+ note "Connecting.."
1075
+ connect options[:id] do |cs,cm|
1076
+ note "connect result: #{cs} #{cm}"
1077
+ if cs==:ok
1078
+ note "Subscribing.."
1079
+ subscribe options[:topic]||"test/message/123", qos: options[:qos] do |s,m|
1080
+ if s==:sub_ack
1081
+ note "Subscribed Ok! Waiting for Messages!"
1082
+ elsif s==:disconnect
1083
+ note "Disconnected -- switch to new gateway"
1084
+ else
1085
+ if block
1086
+ block.call s,m
1087
+ else
1088
+ note "Got Message: #{s}: #{m}"
1089
+ end
1090
+ end
1091
+ end
1092
+ end
1093
+ end
1094
+ puts "Disconnected..."
1095
+ end
1096
+ end
1097
+
1053
1098
 
1054
1099
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mqtt-sn-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Siitonen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2014-11-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby toolkit for MQTT-SN, compatible with RSMB, command line tools and
14
14
  API
@@ -23,8 +23,9 @@ files:
23
23
  - bin/mqtt-sn-forwarder.rb
24
24
  - bin/mqtt-sn-pub.rb
25
25
  - bin/mqtt-sn-sub.rb
26
+ - examples/recv.rb
27
+ - examples/send.rb
26
28
  - lib/mqtt-sn-ruby.rb
27
- - test.rb
28
29
  homepage: https://github.com/arisi/mqtt-sn-ruby
29
30
  licenses:
30
31
  - MIT
data/test.rb DELETED
@@ -1,68 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encode: UTF-8
3
-
4
- require "pp"
5
- require 'socket'
6
- require 'json'
7
-
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
- puts "using gem lib"
14
- end
15
-
16
- puts "Testing mqtt-sn.."
17
-
18
- sn=MqttSN.new debug: true
19
- sn2=MqttSN.new debug: false
20
- sn3=MqttSN.new debug: true
21
-
22
-
23
- sn.will_and_testament "top","testamentti"
24
- sn.connect "eka"
25
-
26
- sn2.connect "toka"
27
-
28
- sn3.connect "kolmas"
29
-
30
- Thread.new do
31
- sn3.subscribe "eka/2",qos:2 do |s,m|
32
- puts ">>>>>>>>>>>> subscribe got: #{s},#{m}"
33
- end
34
- end
35
- sn3.unsubscribe "eka/2"
36
-
37
- topic_id=0
38
- sn.will_and_testament "top2","testamentti2"
39
-
40
- sn2.ping
41
- sn3.subscribe "eka/3",qos:2
42
- #sn.register_topic "jeesus/perkele/toimii"
43
- sn.publish "eka/1","perkkule0",qos: 0
44
- sn.publish "eka/2","perkkule1",qos: 1
45
- sn.publish "eka/3","perkkule2",qos: 2
46
-
47
-
48
- sn2.publish "eka/4","2perkkule0",qos: 0
49
- sn2.publish "eka/5","2perkkule1rrrr",qos: 1, retain: true
50
- sn2.publish "eka/6","2perkkule2",qos: 2
51
- sn2.disconnect
52
- sn2=nil
53
-
54
- #puts "----------------- sleep"
55
- #sn2.goto_sleep 4
56
-
57
- 5.times do
58
- sn.ping
59
- sleep 0.5
60
- end
61
-
62
-
63
- sn.disconnect
64
- sn3.disconnect
65
-
66
-
67
- puts "Done testing mqtt-sn!"
68
-