mqtt-sn-ruby 0.1.4 → 0.1.5
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-forwarder.rb +10 -34
- data/http/coffee/mqtt-sn-sub.coffee +10 -5
- data/http/haml/mqtt-sn-forwarder.haml +27 -0
- data/http/haml/mqtt-sn-sub.haml +4 -10
- data/http/json/gateways.rb +22 -6
- data/lib/mqtt-sn-http.rb +15 -6
- data/lib/mqtt-sn-ruby.rb +20 -8
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f119c9d985366ec62ccc92ca76ea7e6d09352df5
|
4
|
+
data.tar.gz: 36fe4d9e280f102cf4d3e61064e486c1a456a97e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b72dc48535dae5f64d3895dcb3a6f4f9721dcd80574f97fe60792dc010ecdb29ee7203ff9cef082e49792af90094586937129773d17b0dec17e25d4d0f910df1
|
7
|
+
data.tar.gz: ad32035222b72f7d4e4d6d6b3db53d79d5ca20253de84162b55a397efcbb878dc6d887fe5916db5291a33a169e918c1a09f6095132c103687cb7a0770c3f8da2
|
data/bin/mqtt-sn-forwarder.rb
CHANGED
@@ -14,14 +14,15 @@ else
|
|
14
14
|
require 'mqtt-sn-ruby'
|
15
15
|
end
|
16
16
|
|
17
|
-
options = {forwarder: true}
|
17
|
+
options = {forwarder: true,app_name: "mqtt-sn-forwarder"}
|
18
18
|
OptionParser.new do |opts|
|
19
19
|
opts.banner = "Usage: mqtt-sn-sub.rb [options]"
|
20
20
|
|
21
|
-
|
21
|
+
|
22
|
+
opts.on("-v", "--[no-]verbose", "Run verbosely; creates protocol log on console (false)") do |v|
|
22
23
|
options[:verbose] = v
|
23
24
|
end
|
24
|
-
opts.on("-d", "--[no-]debug", "Produce Debug dump on
|
25
|
+
opts.on("-d", "--[no-]debug", "Produce Debug dump on verbose log (false)") do |v|
|
25
26
|
options[:debug] = v
|
26
27
|
end
|
27
28
|
options[:server_uri] = "udp://localhost:1883"
|
@@ -46,38 +47,13 @@ end.parse!
|
|
46
47
|
|
47
48
|
$sn=MqttSN.new options
|
48
49
|
if options[:http_port]
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
Thread.start(server.accept) do |client|
|
55
|
-
request = client.gets.split " "
|
56
|
-
type="text/html"
|
57
|
-
case request[1]
|
58
|
-
when '/gw'
|
59
|
-
response=$sn.gateways.to_json
|
60
|
-
status="200 OK"
|
61
|
-
type="text/json"
|
62
|
-
when '/cli'
|
63
|
-
response=$sn.clients.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
|
50
|
+
if File.file? './lib/mqtt-sn-ruby.rb'
|
51
|
+
require './lib/mqtt-sn-http.rb'
|
52
|
+
puts "using local http lib"
|
53
|
+
else
|
54
|
+
require 'mqtt-sn-http'
|
80
55
|
end
|
56
|
+
http_server options
|
81
57
|
end
|
82
58
|
|
83
59
|
puts "MQTT-SN-FORWARDER: #{options.to_json}"
|
@@ -2,7 +2,9 @@
|
|
2
2
|
now=0
|
3
3
|
|
4
4
|
update_status = (data) ->
|
5
|
-
|
5
|
+
console.log data.jes
|
6
|
+
console.log data.loglines
|
7
|
+
#console.log data.logpos
|
6
8
|
now=data.now
|
7
9
|
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
10
|
for k,v of data.gateways
|
@@ -36,13 +38,16 @@ update_status = (data) ->
|
|
36
38
|
html+="<td></td>"
|
37
39
|
html+="<td>#{v.counter_recv}</td>"
|
38
40
|
html+="</tr>"
|
39
|
-
|
41
|
+
if data.jes[1]==0
|
42
|
+
$(".log").html("")
|
43
|
+
for l in data.loglines
|
44
|
+
#console.log l
|
45
|
+
$(".log").prepend(l.text+"\n")
|
40
46
|
html+="</table>"
|
41
47
|
#console.log html
|
42
48
|
$(".data").html(html)
|
43
|
-
$(".
|
44
|
-
|
45
|
-
|
49
|
+
$(".info").html("State: #{data.state}, gw: #{data.active_gw_id}, app: #{data.options.app_name}, ")
|
50
|
+
|
46
51
|
|
47
52
|
xxx = () ->
|
48
53
|
$.ajax
|
@@ -0,0 +1,27 @@
|
|
1
|
+
%html{lang: "fi"}
|
2
|
+
%head
|
3
|
+
%title MQTT-SN-RUBY Forwarder 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 Forwarder Utility
|
11
|
+
%p.info ?
|
12
|
+
%hr
|
13
|
+
%p Gateways:
|
14
|
+
%p.data
|
15
|
+
%hr
|
16
|
+
%p Clients:
|
17
|
+
%p.clients
|
18
|
+
%hr
|
19
|
+
%p Log:
|
20
|
+
%pre.log
|
21
|
+
%hr
|
22
|
+
%p
|
23
|
+
%font{size: "-1"}
|
24
|
+
Written by
|
25
|
+
%a{href: "mailto:jalopuuverstas@gmail.com"}
|
26
|
+
Ari Siitonen
|
27
|
+
2014
|
data/http/haml/mqtt-sn-sub.haml
CHANGED
@@ -8,19 +8,13 @@
|
|
8
8
|
%script{:src => "/mqtt-sn-sub.js"}
|
9
9
|
%body
|
10
10
|
%h1 MQTT-SN-SUB Subscriber Utility
|
11
|
-
%p
|
12
|
-
State:
|
13
|
-
%span.state
|
14
|
-
?
|
15
|
-
, Current GW:
|
16
|
-
%span.active_gw_id
|
17
|
-
?
|
11
|
+
%p.info ?
|
18
12
|
%hr
|
19
13
|
%p Gateways:
|
20
|
-
%p.data
|
14
|
+
%p.data
|
21
15
|
%hr
|
22
|
-
%p
|
23
|
-
%
|
16
|
+
%p Log:
|
17
|
+
%pre.log
|
24
18
|
%hr
|
25
19
|
%p
|
26
20
|
%font{size: "-1"}
|
data/http/json/gateways.rb
CHANGED
@@ -1,16 +1,32 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encode: UTF-8
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
def json_gateways request,session,event
|
7
|
+
if not session or session==0
|
8
|
+
return ["text/event-stream",{}]
|
9
|
+
end
|
10
|
+
@http_sessions[session][:log_pos]=0 if not @http_sessions[session][:log_pos]
|
11
|
+
size=$sn.http_log.size
|
12
|
+
if @http_sessions[session][:log_pos]!=size
|
13
|
+
loglines=$sn.http_log[@http_sessions[session][:log_pos],size-@http_sessions[session][:log_pos]]
|
14
|
+
@http_sessions[session][:log_pos]=size
|
15
|
+
else
|
16
|
+
loglines=[]
|
17
|
+
end
|
18
|
+
#puts ">> #{loglines.size} "
|
7
19
|
data={
|
8
20
|
now:Time.now.to_i,
|
9
|
-
jes:
|
21
|
+
jes: [session,event],
|
10
22
|
gateways: $sn.gateways,
|
11
23
|
state: $sn.state,
|
12
24
|
active_gw_id: $sn.active_gw_id,
|
25
|
+
options: $sn.options,
|
26
|
+
logpos: @http_sessions[session][:log_pos],
|
27
|
+
loglines: loglines,
|
13
28
|
}
|
14
|
-
#
|
29
|
+
#puts "#{session}/#{event}: #{@http_sessions[session].to_json}"
|
30
|
+
|
15
31
|
return ["text/event-stream",data]
|
16
32
|
end
|
data/lib/mqtt-sn-http.rb
CHANGED
@@ -6,6 +6,7 @@ require "coffee-script"
|
|
6
6
|
|
7
7
|
def http_server options
|
8
8
|
prev_t={}
|
9
|
+
#@http_log=[]
|
9
10
|
puts "Starting HTTP services at port #{options[:http_port]}"
|
10
11
|
if File.directory? './http'
|
11
12
|
$http_dir="http/"
|
@@ -15,6 +16,8 @@ def http_server options
|
|
15
16
|
|
16
17
|
Thread.new(options[:http_port],options[:app_name]) do |http_port,http_app|
|
17
18
|
server = TCPServer.new("0.0.0.0",http_port)
|
19
|
+
@http_sessions={}
|
20
|
+
http_session_counter=1
|
18
21
|
loop do
|
19
22
|
Thread.start(server.accept) do |client|
|
20
23
|
begin
|
@@ -55,7 +58,7 @@ def http_server options
|
|
55
58
|
end
|
56
59
|
if type!="text/event-stream" and status=="200 OK"
|
57
60
|
begin
|
58
|
-
type,response=eval "json_#{act} request"
|
61
|
+
type,response=eval "json_#{act} request,0,0" #event handlers get called with zero session => init :)
|
59
62
|
response=response.to_json
|
60
63
|
rescue => e
|
61
64
|
puts "**** AJAX EXEC #{fn} failed: #{e}"
|
@@ -81,13 +84,18 @@ def http_server options
|
|
81
84
|
client.print response
|
82
85
|
else
|
83
86
|
client.print "Expires: -1\r\n"
|
84
|
-
|
85
|
-
cnt=0
|
87
|
+
client.print "\r\n"
|
86
88
|
begin
|
89
|
+
my_session=client.peeraddr[1]
|
90
|
+
if not @http_sessions[my_session]
|
91
|
+
#puts "**************** new port #{my_session}"
|
92
|
+
@http_sessions[my_session]={client_port:client.peeraddr[1],client_ip:client.peeraddr[2] , log_position:0 }
|
93
|
+
end
|
94
|
+
my_event=0
|
87
95
|
loop do
|
88
96
|
begin
|
89
|
-
type,response=eval "json_#{act} request"
|
90
|
-
|
97
|
+
type,response=eval "json_#{act} request,my_session,my_event"
|
98
|
+
my_event+=1
|
91
99
|
rescue => e
|
92
100
|
puts "**** AJAX EXEC #{fn} failed: #{e}"
|
93
101
|
puts "#{e.backtrace[0..2]}"
|
@@ -100,10 +108,11 @@ def http_server options
|
|
100
108
|
client.print "data: #{response.to_json}\n\n"
|
101
109
|
end
|
102
110
|
sleep 1
|
103
|
-
break if
|
111
|
+
break if my_event>100
|
104
112
|
end
|
105
113
|
rescue => e
|
106
114
|
puts "stream #{client} died #{e}"
|
115
|
+
pp e.backtrace
|
107
116
|
end
|
108
117
|
|
109
118
|
end
|
data/lib/mqtt-sn-ruby.rb
CHANGED
@@ -54,19 +54,28 @@ class MqttSN
|
|
54
54
|
|
55
55
|
|
56
56
|
def logger str,*args
|
57
|
+
s=sprintf(str,*args)
|
58
|
+
if not @forwarder
|
59
|
+
text=sprintf("%s: (%3.3s) | %s",Time.now.iso8601,@active_gw_id,s)
|
60
|
+
else
|
61
|
+
text=sprintf("%s: [%3.3s] | %s",Time.now.iso8601,@options[:gw_id],s)
|
62
|
+
end
|
57
63
|
if @verbose or @debug
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
64
|
-
end
|
64
|
+
@log_q << text
|
65
|
+
end
|
66
|
+
if @options[:http_port] and @http_log
|
67
|
+
@http_log << {stamp: Time.now.to_i, text: text}
|
68
|
+
end
|
65
69
|
end
|
66
70
|
|
67
71
|
def note str,*args
|
68
72
|
s=sprintf(str,*args)
|
69
|
-
|
73
|
+
text=sprintf("%s: %s",Time.now.iso8601,s)
|
74
|
+
@log_q << text
|
75
|
+
if @options[:http_port] and @http_log
|
76
|
+
@http_log << {stamp: Time.now.to_i, text: text}
|
77
|
+
end
|
78
|
+
|
70
79
|
end
|
71
80
|
|
72
81
|
def log_empty?
|
@@ -131,7 +140,9 @@ class MqttSN
|
|
131
140
|
attr_accessor :clients
|
132
141
|
attr_accessor :gateways
|
133
142
|
attr_accessor :state
|
143
|
+
attr_accessor :options
|
134
144
|
attr_accessor :active_gw_id
|
145
|
+
attr_accessor :http_log
|
135
146
|
|
136
147
|
def add_gateway gw_id,hash
|
137
148
|
if not @gateways[gw_id]
|
@@ -158,6 +169,7 @@ class MqttSN
|
|
158
169
|
@will_topic=nil
|
159
170
|
@will_msg=nil
|
160
171
|
@active_gw_id=nil
|
172
|
+
@http_log=[]
|
161
173
|
|
162
174
|
@sem=Mutex.new
|
163
175
|
@gsem=Mutex.new
|
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.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Siitonen
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- http/coffee/mqtt-sn-sub.coffee
|
32
32
|
- http/coffee/mqtt-sn.coffee
|
33
33
|
- http/css/mqtt-sn.css
|
34
|
+
- http/haml/mqtt-sn-forwarder.haml
|
34
35
|
- http/haml/mqtt-sn-sub.haml
|
35
36
|
- http/json/clients.rb
|
36
37
|
- http/json/gateways.rb
|