mqtt-sn-ruby 0.1.2 → 0.1.3
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 +8 -2
- data/bin/mqtt-sn-sub.rb +9 -29
- data/c/mqtt-sn-pub.c +1 -1
- data/http/coffee/mqtt-sn-sub.coffee +78 -0
- data/http/coffee/mqtt-sn.coffee +1 -0
- data/http/css/mqtt-sn.css +0 -0
- data/http/haml/mqtt-sn-sub.haml +30 -0
- data/http/json/clients.rb +7 -0
- data/http/json/gateways.rb +16 -0
- data/lib/mqtt-sn-http.rb +116 -0
- data/lib/mqtt-sn-ruby.rb +63 -43
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ae50f9d0024bbb67caaa79b891a922b7aaa321c
|
4
|
+
data.tar.gz: 504162553c549334abeed8b5049efc62c1ceeb06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af7aae3fa39905eb1a80f0eed6b925d21fa4155ab7f13bd0abd5ff208998b6381a7137dcfbda86516e0e1c9704d6434c179e15f187c1099b3d9a620b8c527e54
|
7
|
+
data.tar.gz: 0b12a0b5c46530691cdede75bd99f34df4bdc9ddf196434de031a955d98780426aead098b8c3a7eee7bce4e19934da0f957169f53edbe4816fd31e94fa89c1c4
|
data/bin/mqtt-sn-pub.rb
CHANGED
@@ -25,6 +25,10 @@ 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
|
+
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
|
28
32
|
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
33
|
options[:qos] = v.to_i
|
30
34
|
end
|
@@ -46,10 +50,12 @@ begin
|
|
46
50
|
sn.pub options
|
47
51
|
rescue SystemExit, Interrupt
|
48
52
|
puts "\nExiting after Disconnect\n"
|
49
|
-
rescue => e
|
53
|
+
rescue Exception => e
|
50
54
|
puts "\nError: '#{e}' -- Quit after Disconnect\n"
|
51
55
|
e.backtrace
|
52
56
|
end
|
53
|
-
sn.disconnect if sn
|
57
|
+
sn.disconnect if sn
|
58
|
+
|
59
|
+
sn.log_flush
|
54
60
|
|
55
61
|
puts "MQTT-SN-PUB Done."
|
data/bin/mqtt-sn-sub.rb
CHANGED
@@ -5,6 +5,7 @@ require "pp"
|
|
5
5
|
require 'socket'
|
6
6
|
require 'json'
|
7
7
|
require 'optparse'
|
8
|
+
|
8
9
|
if File.file? './lib/mqtt-sn-ruby.rb'
|
9
10
|
require './lib/mqtt-sn-ruby.rb'
|
10
11
|
puts "using local lib"
|
@@ -12,9 +13,9 @@ else
|
|
12
13
|
require 'mqtt-sn-ruby'
|
13
14
|
end
|
14
15
|
|
15
|
-
options = {}
|
16
|
+
options = {app_name: "mqtt-sn-sub"}
|
16
17
|
OptionParser.new do |opts|
|
17
|
-
opts.banner = "Usage:
|
18
|
+
opts.banner = "Usage: #{options[:app_name]}.rb [options]"
|
18
19
|
|
19
20
|
opts.on("-v", "--[no-]verbose", "Run verbosely; creates protocol log on console (false)") do |v|
|
20
21
|
options[:verbose] = v
|
@@ -50,34 +51,13 @@ end.parse!
|
|
50
51
|
$sn=MqttSN.new options
|
51
52
|
|
52
53
|
if options[:http_port]
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
Thread.start(server.accept) do |client|
|
59
|
-
request = client.gets.split " "
|
60
|
-
type="text/html"
|
61
|
-
case request[1]
|
62
|
-
when '/gw'
|
63
|
-
response=$sn.gateways.to_json
|
64
|
-
status="200 OK"
|
65
|
-
type="text/json"
|
66
|
-
else
|
67
|
-
status="404 Not Found"
|
68
|
-
response="?que"
|
69
|
-
end
|
70
|
-
client.print "HTTP/1.1 #{status}\r\n" +
|
71
|
-
"Content-Type: #{type}\r\n" +
|
72
|
-
"Content-Length: #{response.bytesize}\r\n" +
|
73
|
-
"Connection: close\r\n"
|
74
|
-
client.print "\r\n"
|
75
|
-
client.print response
|
76
|
-
client.close
|
77
|
-
puts "#{request} -> #{response}"
|
78
|
-
end
|
79
|
-
end
|
54
|
+
if File.file? './lib/mqtt-sn-ruby.rb'
|
55
|
+
require './lib/mqtt-sn-http.rb'
|
56
|
+
puts "using local http lib"
|
57
|
+
else
|
58
|
+
require 'mqtt-sn-http'
|
80
59
|
end
|
60
|
+
http_server options
|
81
61
|
end
|
82
62
|
|
83
63
|
puts "MQTT-SN-SUB: #{options.to_json}"
|
data/c/mqtt-sn-pub.c
CHANGED
@@ -0,0 +1,78 @@
|
|
1
|
+
|
2
|
+
now=0
|
3
|
+
|
4
|
+
update_status = (data) ->
|
5
|
+
#console.log data
|
6
|
+
now=data.now
|
7
|
+
html="<table><tr><th>gw_id</th><th>uri</th><th>source</th><th>stamp</th><th>last_use</th><th>last_ping</th><th>last_send</th><th>counter_send</th><th>last_recv</th><th>counter_recv</th></tr>"
|
8
|
+
for k,v of data.gateways
|
9
|
+
k=parseInt(k,10)
|
10
|
+
col="white"
|
11
|
+
if data.active_gw_id== k
|
12
|
+
col="#d0ffd0"
|
13
|
+
html+="<tr bgcolor='#{col}'>"
|
14
|
+
html+="<td>#{k}</td>"
|
15
|
+
html+="<td>#{v.uri}</td>"
|
16
|
+
html+="<td>#{v.source}</td>"
|
17
|
+
html+="<td>#{now-v.stamp}</td>"
|
18
|
+
if v.last_use
|
19
|
+
html+="<td>#{now-v.last_use}</td>"
|
20
|
+
else
|
21
|
+
html+="<td></td>"
|
22
|
+
if v.last_ping
|
23
|
+
html+="<td>#{now-v.last_ping}</td>"
|
24
|
+
else
|
25
|
+
html+="<td></td>"
|
26
|
+
|
27
|
+
if v.last_send
|
28
|
+
html+="<td>#{now-v.last_send}</td>"
|
29
|
+
else
|
30
|
+
html+="<td></td>"
|
31
|
+
html+="<td>#{v.counter_send}</td>"
|
32
|
+
|
33
|
+
if v.last_recv
|
34
|
+
html+="<td>#{now-v.last_recv}</td>"
|
35
|
+
else
|
36
|
+
html+="<td></td>"
|
37
|
+
html+="<td>#{v.counter_recv}</td>"
|
38
|
+
html+="</tr>"
|
39
|
+
|
40
|
+
html+="</table>"
|
41
|
+
#console.log html
|
42
|
+
$(".data").html(html)
|
43
|
+
$(".state").html(data.state)
|
44
|
+
$(".active_gw_id").html(data.active_gw_id)
|
45
|
+
|
46
|
+
|
47
|
+
xxx = () ->
|
48
|
+
$.ajax
|
49
|
+
url: "/gateways.json"
|
50
|
+
type: "GET"
|
51
|
+
data: "123"
|
52
|
+
processData: false
|
53
|
+
contentType: false
|
54
|
+
success: (data) ->
|
55
|
+
console.log "ajax returns: ", data
|
56
|
+
$(".data").html(html)
|
57
|
+
|
58
|
+
return
|
59
|
+
error: (xhr, ajaxOptions, thrownError) ->
|
60
|
+
alert thrownError
|
61
|
+
return
|
62
|
+
|
63
|
+
|
64
|
+
$ ->
|
65
|
+
console.log "tadaa"
|
66
|
+
#update_gw()
|
67
|
+
#setInterval(->
|
68
|
+
# update_gw()
|
69
|
+
# return
|
70
|
+
#, 200000)
|
71
|
+
stream = new EventSource("/gateways.json")
|
72
|
+
stream.addEventListener "message", (event) ->
|
73
|
+
update_status($.parseJSON(event.data))
|
74
|
+
return
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
|
File without changes
|
@@ -0,0 +1,30 @@
|
|
1
|
+
%html{lang: "fi"}
|
2
|
+
%head
|
3
|
+
%title MQTT-SN-RUBY Subscribe Utility
|
4
|
+
%meta{:charset => "utf-8"}/
|
5
|
+
%link{:href => "/mqtt-sn.css", :rel => "stylesheet", :type => "text/css"}
|
6
|
+
%script{:src => "https://code.jquery.com/jquery-2.1.1.min.js"}
|
7
|
+
%script{:src => "/mqtt-sn.js"}
|
8
|
+
%script{:src => "/mqtt-sn-sub.js"}
|
9
|
+
%body
|
10
|
+
%h1 MQTT-SN-SUB Subscriber Utility
|
11
|
+
%p
|
12
|
+
State:
|
13
|
+
%span.state
|
14
|
+
?
|
15
|
+
, Current GW:
|
16
|
+
%span.active_gw_id
|
17
|
+
?
|
18
|
+
%hr
|
19
|
+
%p Gateways:
|
20
|
+
%p.data datat tähän
|
21
|
+
%hr
|
22
|
+
%p Clients:
|
23
|
+
%p.data datat tähän
|
24
|
+
%hr
|
25
|
+
%p
|
26
|
+
%font{size: "-1"}
|
27
|
+
Written by
|
28
|
+
%a{href: "mailto:jalopuuverstas@gmail.com"}
|
29
|
+
Ari Siitonen
|
30
|
+
2014
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encode: UTF-8
|
3
|
+
$jes=123
|
4
|
+
def json_gateways request
|
5
|
+
#puts "json_gateways!"
|
6
|
+
$jes+=1
|
7
|
+
data={
|
8
|
+
now:Time.now.to_i,
|
9
|
+
jes: $jes,
|
10
|
+
gateways: $sn.gateways,
|
11
|
+
state: $sn.state,
|
12
|
+
active_gw_id: $sn.active_gw_id,
|
13
|
+
}
|
14
|
+
#pp data
|
15
|
+
return ["text/event-stream",data]
|
16
|
+
end
|
data/lib/mqtt-sn-http.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encode: UTF-8
|
3
|
+
|
4
|
+
require "haml"
|
5
|
+
require "coffee-script"
|
6
|
+
|
7
|
+
def http_server options
|
8
|
+
prev_t={}
|
9
|
+
puts "Starting HTTP services at port #{options[:http_port]}"
|
10
|
+
$http_dir="http/"
|
11
|
+
|
12
|
+
Thread.new(options[:http_port],options[:app_name]) do |http_port,http_app|
|
13
|
+
server = TCPServer.new("0.0.0.0",http_port)
|
14
|
+
loop do
|
15
|
+
Thread.start(server.accept) do |client|
|
16
|
+
begin
|
17
|
+
request = client.gets.split " "
|
18
|
+
status="200 OK"
|
19
|
+
type="text/html"
|
20
|
+
req=request[1]
|
21
|
+
req="/#{http_app}.html" if req=="/" or req=="/index.htm" or req=="/index.html"
|
22
|
+
req,args=req.split "\?"
|
23
|
+
#puts "req: #{req}"
|
24
|
+
if req[/\.html$/] and File.file?(fn="#{$http_dir}haml#{req.gsub('.html','.haml')}")
|
25
|
+
contents = File.read(fn)
|
26
|
+
response=Haml::Engine.new(contents).render
|
27
|
+
elsif req[/\.js$/] and File.file?(fn="#{$http_dir}coffee#{req.gsub('.js','.coffee')}")
|
28
|
+
type="application/javascript"
|
29
|
+
contents = File.read(fn)
|
30
|
+
begin
|
31
|
+
response=CoffeeScript.compile contents
|
32
|
+
rescue => e
|
33
|
+
response="Coffee Compile error: #{e}"
|
34
|
+
end
|
35
|
+
elsif req[/^\/(.+)\.json$/] and File.file?(fn="#{$http_dir}json#{req.gsub('.json','.rb')}")
|
36
|
+
req[/\/(.+).json$/]
|
37
|
+
act=$1
|
38
|
+
#puts "act:#{act} args:#{args}"
|
39
|
+
t=File.mtime(fn)
|
40
|
+
if not prev_t[fn] or prev_t[fn]<t
|
41
|
+
begin
|
42
|
+
load_ok=load fn
|
43
|
+
prev_t[fn]=t
|
44
|
+
rescue Exception => e
|
45
|
+
puts "**** RELOAD #{fn} failed: #{e}"
|
46
|
+
pp e.backtrace
|
47
|
+
response=[{act: :error, msg:"Error loading JSON",alert: "Load error #{e} in #{fn}"}].to_json
|
48
|
+
type="text/json"
|
49
|
+
status="404 Not Found"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
if type!="text/event-stream" and status=="200 OK"
|
53
|
+
begin
|
54
|
+
type,response=eval "json_#{act} request"
|
55
|
+
response=response.to_json
|
56
|
+
rescue => e
|
57
|
+
puts "**** AJAX EXEC #{fn} failed: #{e}"
|
58
|
+
pp e.backtrace
|
59
|
+
response=[{act: :error, msg:"Error executing JSON",alert: "Syntax error '#{e}' in '#{fn}'"}].to_json
|
60
|
+
type="text/json"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
elsif req[/\.css$/] and File.file?(fnc="#{$http_dir}css#{req}")
|
64
|
+
type="text/css"
|
65
|
+
contents = File.read(fnc)
|
66
|
+
response=contents
|
67
|
+
else
|
68
|
+
status="404 Not Found"
|
69
|
+
response="Not Found: #{request[1]}"
|
70
|
+
end
|
71
|
+
client.print "HTTP/1.1 #{status}\r\n" +
|
72
|
+
"Content-Type: #{type}\r\n"
|
73
|
+
if type!="text/event-stream"
|
74
|
+
client.print "Content-Length: #{response.bytesize}\r\n"
|
75
|
+
client.print "Connection: close\r\n"
|
76
|
+
client.print "\r\n"
|
77
|
+
client.print response
|
78
|
+
else
|
79
|
+
client.print "Expires: -1\r\n"
|
80
|
+
#client.print "\r\n"
|
81
|
+
cnt=0
|
82
|
+
begin
|
83
|
+
loop do
|
84
|
+
begin
|
85
|
+
type,response=eval "json_#{act} request"
|
86
|
+
cnt+=1
|
87
|
+
rescue => e
|
88
|
+
puts "**** AJAX EXEC #{fn} failed: #{e}"
|
89
|
+
puts "#{e.backtrace[0..2]}"
|
90
|
+
pp e.backtrace
|
91
|
+
response=[{act: :error, msg:"Error executing JSON",alert: "Syntax error '#{e}' in '#{fn}'"}].to_json
|
92
|
+
end
|
93
|
+
if not response or response==[] or response=={}
|
94
|
+
else
|
95
|
+
client.print "retry: 1000\n"
|
96
|
+
client.print "data: #{response.to_json}\n\n"
|
97
|
+
end
|
98
|
+
sleep 1
|
99
|
+
break if cnt>10
|
100
|
+
end
|
101
|
+
rescue => e
|
102
|
+
puts "stream #{client} died #{e}"
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
client.close
|
107
|
+
rescue Exception =>e
|
108
|
+
puts "http thread died #{e}"
|
109
|
+
pp e.backtrace
|
110
|
+
client.print "Error #{e}"
|
111
|
+
client.close
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/lib/mqtt-sn-ruby.rb
CHANGED
@@ -14,7 +14,6 @@ class MqttSN
|
|
14
14
|
Nretry = 3 # Max retry
|
15
15
|
Tretry = 3 # Timeout before retry
|
16
16
|
|
17
|
-
# :stopdoc:
|
18
17
|
SEARCHGW_TYPE =0x01
|
19
18
|
GWINFO_TYPE =0x02
|
20
19
|
ADVERTISE_TYPE =0x03
|
@@ -131,13 +130,28 @@ class MqttSN
|
|
131
130
|
|
132
131
|
attr_accessor :clients
|
133
132
|
attr_accessor :gateways
|
133
|
+
attr_accessor :state
|
134
|
+
attr_accessor :active_gw_id
|
135
|
+
|
136
|
+
def add_gateway gw_id,hash
|
137
|
+
if not @gateways[gw_id]
|
138
|
+
@gateways[gw_id]={stamp: Time.now.to_i, status: :ok, last_use: 0,last_ping: 0,counter_send:0, last_send: 0,counter_recv:0, last_recv: 0}.merge(hash)
|
139
|
+
else
|
140
|
+
if @gateways[gw_id][:uri]!=hash[:uri]
|
141
|
+
note "conflict -- gateway has moved? or duplicate"
|
142
|
+
else
|
143
|
+
@gateways[gw_id][:stamp]=Time.now.to_i
|
144
|
+
@gateways[gw_id]=@gateways[gw_id].merge hash
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
134
148
|
|
135
149
|
def initialize(hash={})
|
136
150
|
@options=hash #save these
|
137
151
|
@server_uri=hash[:server_uri]
|
138
152
|
@debug=hash[:debug]
|
139
153
|
@verbose=hash[:verbose]
|
140
|
-
@state=:
|
154
|
+
@state=:disconnected
|
141
155
|
@bcast_port=5000
|
142
156
|
@keepalive=(hash[:keepalive]||25).to_i
|
143
157
|
@forwarder=hash[:forwarder] #flag to indicate forward mode
|
@@ -156,13 +170,13 @@ class MqttSN
|
|
156
170
|
|
157
171
|
if @server_uri
|
158
172
|
note "Using Default Gateway: #{@server_uri}"
|
159
|
-
|
173
|
+
add_gateway(0,{uri: @server_uri,source: "default"})
|
160
174
|
pick_new_gateway
|
161
175
|
elsif @broadcast_uri
|
162
176
|
note "Autodiscovery Active, using #{@broadcast_uri}"
|
163
177
|
@autodiscovery=true
|
164
178
|
else
|
165
|
-
|
179
|
+
puts "No autodiscovery and no Default Gateway -- cannot proceed"
|
166
180
|
exit -1
|
167
181
|
end
|
168
182
|
|
@@ -299,7 +313,7 @@ class MqttSN
|
|
299
313
|
|
300
314
|
def send type,hash={},&block
|
301
315
|
#puts "" if @verbose
|
302
|
-
if @state
|
316
|
+
if @state==:disconnected and type!=:connect and type!=:will_topic and type!=:will_msg and type!=:searchgw
|
303
317
|
if type==:disconnect
|
304
318
|
return #already disconnected.. nothing to do
|
305
319
|
elsif type==:publish and hash[:qos]==-1
|
@@ -535,17 +549,22 @@ class MqttSN
|
|
535
549
|
end
|
536
550
|
end
|
537
551
|
note "Gw Ok!"
|
538
|
-
end
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
552
|
+
end
|
553
|
+
ok=false
|
554
|
+
@gsem.synchronize do
|
555
|
+
if @active_gw_id and @gateways[@active_gw_id] and @gateways[@active_gw_id][:socket]
|
556
|
+
ok=true
|
557
|
+
@gateways[@active_gw_id][:last_send]=Time.now.to_i
|
558
|
+
@gateways[@active_gw_id][:counter_send]+=1
|
559
|
+
uri=URI.parse(@gateways[@active_gw_id][:uri])
|
560
|
+
#uri.scheme
|
561
|
+
MqttSN::send_raw_packet msg,@gateways[@active_gw_id][:socket],uri.host,uri.port
|
562
|
+
_,port,_,_ = @gateways[@active_gw_id][:socket].addr
|
563
|
+
src="udp://0.0.0.0:#{port}"
|
564
|
+
logger "od %-24.24s <- %-24.24s | %s",@gateways[@active_gw_id][:uri],src,MqttSN::parse_message(msg).merge(debug).to_json
|
565
|
+
end
|
566
|
+
end
|
567
|
+
if not ok
|
549
568
|
puts "no gw to send.."
|
550
569
|
sleep 1
|
551
570
|
end
|
@@ -576,7 +595,7 @@ class MqttSN
|
|
576
595
|
end
|
577
596
|
|
578
597
|
def will_and_testament topic,msg
|
579
|
-
if @state
|
598
|
+
if @state!=:disconnected #if already connected, send changes, otherwise wait until connect does it.
|
580
599
|
if @will_topic!=topic
|
581
600
|
send :will_topic_upd, topic: topic, expect: :will_topic_resp do |status,message|
|
582
601
|
puts "will topic updated"
|
@@ -652,7 +671,7 @@ class MqttSN
|
|
652
671
|
block.call :got_data,m
|
653
672
|
end
|
654
673
|
sleep 0.1
|
655
|
-
if @state
|
674
|
+
if @state==:disconnected
|
656
675
|
block.call :disconnect,{}
|
657
676
|
#gateway_close :subscribe_disconnected
|
658
677
|
break
|
@@ -860,6 +879,14 @@ class MqttSN
|
|
860
879
|
end
|
861
880
|
when :connect_ack
|
862
881
|
@state=:connected
|
882
|
+
when :sub_ack
|
883
|
+
@state=:subscribed
|
884
|
+
when :pong
|
885
|
+
@gsem.synchronize do #one command at a time --
|
886
|
+
if @active_gw_id and @gateways[@active_gw_id]
|
887
|
+
@gateways[@active_gw_id][:last_ping]=Time.now.to_i
|
888
|
+
end
|
889
|
+
end
|
863
890
|
when :searchgw
|
864
891
|
done=true
|
865
892
|
when :advertise
|
@@ -878,7 +905,7 @@ class MqttSN
|
|
878
905
|
Thread.new do #ping thread
|
879
906
|
while true do
|
880
907
|
begin
|
881
|
-
while @state
|
908
|
+
while @state==:disconnected
|
882
909
|
sleep 1
|
883
910
|
end
|
884
911
|
sleep @keepalive
|
@@ -901,21 +928,24 @@ class MqttSN
|
|
901
928
|
Thread.new do #work thread
|
902
929
|
while true do
|
903
930
|
begin
|
904
|
-
|
905
|
-
if
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
931
|
+
do_sleep=true
|
932
|
+
if @active_gw_id and @gateways[@active_gw_id] and @gateways[@active_gw_id][:socket] #if we are connected...
|
933
|
+
if pac=MqttSN::poll_packet(@gateways[@active_gw_id][:socket]) #cannot block -- gateway may change...
|
934
|
+
r,client_ip,client_port=pac
|
935
|
+
m=MqttSN::parse_message r
|
936
|
+
if @debug and m
|
937
|
+
m[:debug]=MqttSN::hexdump r
|
938
|
+
end
|
939
|
+
_,port,_,_= @gateways[@active_gw_id][:socket].addr
|
940
|
+
src="udp://0.0.0.0:#{port}"
|
941
|
+
logger "id %-24.24s -> %-24.24s | %s",@gateways[@active_gw_id][:uri],src,m.to_json
|
942
|
+
process_message m
|
943
|
+
do_sleep=false
|
944
|
+
@gateways[@active_gw_id][:last_recv]=Time.now.to_i
|
945
|
+
@gateways[@active_gw_id][:counter_recv]+=1
|
910
946
|
end
|
911
|
-
_,port,_,_= @gateways[@active_gw_id][:socket].addr
|
912
|
-
src="udp://0.0.0.0:#{port}"
|
913
|
-
logger "id %-24.24s -> %-24.24s | %s",@gateways[@active_gw_id][:uri],src,m.to_json
|
914
|
-
process_message m
|
915
|
-
else
|
916
|
-
sleep 0.01
|
917
947
|
end
|
918
|
-
|
948
|
+
if do_sleep
|
919
949
|
sleep 0.01
|
920
950
|
end
|
921
951
|
rescue => e
|
@@ -938,17 +968,7 @@ class MqttSN
|
|
938
968
|
gw_id=m[:gw_id]
|
939
969
|
duration=m[:duration]||180
|
940
970
|
uri="udp://#{client_ip}:1882"
|
941
|
-
|
942
|
-
@gateways[gw_id]={stamp: Time.now.to_i,uri: uri, duration: duration, source: m[:type], status: :ok, last_use: 0}
|
943
|
-
else
|
944
|
-
if @gateways[gw_id][:uri]!=uri
|
945
|
-
note "conflict -- gateway has moved? or duplicate"
|
946
|
-
else
|
947
|
-
@gateways[gw_id][:stamp]=Time.now.to_i
|
948
|
-
@gateways[gw_id][:duration]=duration
|
949
|
-
@gateways[gw_id][:source]=m[:type]
|
950
|
-
end
|
951
|
-
end
|
971
|
+
add_gateway(gw_id,{uri: uri, source: m[:type], duration:duration,stamp: Time.now.to_i})
|
952
972
|
end
|
953
973
|
end
|
954
974
|
|
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.1.
|
4
|
+
version: 0.1.3
|
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-
|
11
|
+
date: 2014-11-23 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
|
@@ -28,6 +28,13 @@ files:
|
|
28
28
|
- c/mqtt-sn.h
|
29
29
|
- examples/recv.rb
|
30
30
|
- examples/send.rb
|
31
|
+
- http/coffee/mqtt-sn-sub.coffee
|
32
|
+
- http/coffee/mqtt-sn.coffee
|
33
|
+
- http/css/mqtt-sn.css
|
34
|
+
- http/haml/mqtt-sn-sub.haml
|
35
|
+
- http/json/clients.rb
|
36
|
+
- http/json/gateways.rb
|
37
|
+
- lib/mqtt-sn-http.rb
|
31
38
|
- lib/mqtt-sn-ruby.rb
|
32
39
|
homepage: https://github.com/arisi/mqtt-sn-ruby
|
33
40
|
licenses:
|