mqtt-sn-ruby 0.0.17 → 0.0.18
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 +4 -4
- data/bin/mqtt-sn-pub.rb +2 -2
- data/lib/mqtt-sn-ruby.rb +29 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 355869b8237db560d527d053840b5cf3e6004f67
|
4
|
+
data.tar.gz: 7eb4e42c4d935ad629fdaf53709953d5e87c8acd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b04c285a66b26ce92e411df4f22c292c8de65b685f8ec2e11c904745fc8c6c3dfa72fbfe3be85f53e9e07330632df723da80e42555bd5814b83f7137ca67966
|
7
|
+
data.tar.gz: 86bbb6373d0790ed3e78e0123d8d4cfc0190093f2db248876650863cc5cdf39a5b9afddaaf7fba666fecb4f17b716a09291bf9debf7f63d31e8f3cabfd835111
|
data/bin/mqtt-sn-pub.rb
CHANGED
@@ -25,7 +25,7 @@ OptionParser.new do |opts|
|
|
25
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
|
+
opts.on("-q", "--qos level", "QoS level (0). When using QoS -1, you must provide either Short Topic (2-char) or Topic_Id") do |v|
|
29
29
|
options[:qos] = v.to_i
|
30
30
|
end
|
31
31
|
opts.on("-i", "--id id", "This client's id -- free choice (hostname-pid)") do |name|
|
@@ -34,7 +34,7 @@ OptionParser.new do |opts|
|
|
34
34
|
opts.on("-m", "--msg msg", "Message to send (test_value)") do |msg|
|
35
35
|
options[:msg] = msg
|
36
36
|
end
|
37
|
-
opts.on("-t", "--topic topic", "Topic to use (test/message/123)") do |topic|
|
37
|
+
opts.on("-t", "--topic topic", "Topic to use (test/message/123). For predefined Topics, use this notation '=123'") do |topic|
|
38
38
|
options[:topic] = topic
|
39
39
|
end
|
40
40
|
|
data/lib/mqtt-sn-ruby.rb
CHANGED
@@ -11,11 +11,9 @@ require 'time'
|
|
11
11
|
|
12
12
|
class MqttSN
|
13
13
|
|
14
|
-
Nretry =
|
15
|
-
Tretry =
|
14
|
+
Nretry = 3 # Max retry
|
15
|
+
Tretry = 3 # Timeout before retry
|
16
16
|
|
17
|
-
MULTICAST_ADDR = "225.4.5.6"
|
18
|
-
|
19
17
|
SEARCHGW_TYPE =0x01
|
20
18
|
GWINFO_TYPE =0x02
|
21
19
|
ADVERTISE_TYPE =0x03
|
@@ -51,6 +49,7 @@ class MqttSN
|
|
51
49
|
QOS2_FLAG =0x40
|
52
50
|
QOS1_FLAG =0x20
|
53
51
|
QOS0_FLAG =0x00
|
52
|
+
TOPIC_PREDEFINED_FLAG =0x01
|
54
53
|
TOPIC_SHORT_FLAG =0x02
|
55
54
|
|
56
55
|
|
@@ -245,6 +244,12 @@ class MqttSN
|
|
245
244
|
end
|
246
245
|
@clients[key][:stamp]=Time.now.to_i
|
247
246
|
m=MqttSN::parse_message r
|
247
|
+
case m[:type]
|
248
|
+
when :publish
|
249
|
+
if m[:qos]==-1
|
250
|
+
@clients[key][:state]=:disconnected #one shot
|
251
|
+
end
|
252
|
+
end
|
248
253
|
sbytes=@clients[key][:socket].send(r, 0, @server, @port) # to rsmb -- ok as is
|
249
254
|
_,port,_,_ = @clients[key][:socket].addr
|
250
255
|
dest="#{@server}:#{port}"
|
@@ -389,6 +394,8 @@ class MqttSN
|
|
389
394
|
end
|
390
395
|
if hash[:topic_type]==:short
|
391
396
|
flags+=TOPIC_SHORT_FLAG
|
397
|
+
elsif hash[:topic_type]==:predefined
|
398
|
+
flags+=TOPIC_PREDEFINED_FLAG
|
392
399
|
end
|
393
400
|
p=[PUBLISH_TYPE,flags,hash[:topic_id] >>8 ,hash[:topic_id] & 0xff,@msg_id >>8 ,@msg_id & 0xff]
|
394
401
|
hash[:msg].each_byte do |b|
|
@@ -495,10 +502,10 @@ class MqttSN
|
|
495
502
|
end
|
496
503
|
|
497
504
|
def send_packet_bcast m
|
498
|
-
|
505
|
+
uri = URI.parse(@broadcast_uri)
|
499
506
|
msg=MqttSN::build_packet m
|
500
|
-
MqttSN::send_raw_packet msg,
|
501
|
-
_,port,_,_ =
|
507
|
+
MqttSN::send_raw_packet msg,@bcast_s,uri.host,uri.port
|
508
|
+
_,port,_,_ = @bcast_s.addr
|
502
509
|
src="udp://0.0.0.0:#{port}"
|
503
510
|
logger "ob %-24.24s <- %-24.24s | %s",@broadcast_uri,src,MqttSN::parse_message(msg).to_json
|
504
511
|
end
|
@@ -676,15 +683,22 @@ class MqttSN
|
|
676
683
|
end
|
677
684
|
|
678
685
|
def publish topic,msg,hash={}
|
679
|
-
|
680
|
-
|
686
|
+
puts "op='#{topic}','#{topic[0]}'"
|
687
|
+
|
688
|
+
if topic[0]=="="
|
689
|
+
puts "yeah"
|
690
|
+
topic[0]=""
|
691
|
+
topic_id=topic.to_i
|
692
|
+
topic_type=:predefined
|
693
|
+
elsif topic.size==2
|
694
|
+
topic_id=((topic[0].ord&0xff)<<8)+(topic[1].ord&0xff)
|
695
|
+
topic_type=:short
|
696
|
+
else
|
697
|
+
topic_type=:long
|
681
698
|
if not @topics[topic]
|
682
699
|
register_topic topic
|
683
700
|
end
|
684
701
|
topic_id=@topics[topic]
|
685
|
-
else
|
686
|
-
topic_id=((topic[0].ord&0xff)<<8)+(topic[1].ord&0xff)
|
687
|
-
topic_type=:short
|
688
702
|
end
|
689
703
|
case hash[:qos]
|
690
704
|
when 1
|
@@ -752,6 +766,9 @@ class MqttSN
|
|
752
766
|
if flags&0x03==TOPIC_SHORT_FLAG
|
753
767
|
topic_type=:short
|
754
768
|
topic=r[3].chr+r[4].chr
|
769
|
+
elsif flags&0x03==TOPIC_PREDEFINED_FLAG
|
770
|
+
topic_type=:predefined
|
771
|
+
topic=""
|
755
772
|
end
|
756
773
|
qos=(flags>>5)&0x03
|
757
774
|
qos=-1 if qos==3
|