mqtt-sn-ruby 0.1.5 → 0.1.6
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/http/coffee/mqtt-sn-sub.coffee +8 -5
- data/http/haml/mqtt-sn-sub.haml +12 -0
- data/http/json/action.rb +17 -0
- data/http/json/gateways.rb +4 -1
- data/lib/mqtt-sn-http.rb +19 -7
- data/lib/mqtt-sn-ruby.rb +25 -16
- 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: 1ec6573a2dc9ed720a687b3219498d0846a6d2c4
|
4
|
+
data.tar.gz: 2e847e9b73f0066b6dca9b7cd9ca63316232a8f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afb381b554159602a31c03931b187aee6395a72e8e73c36f47501af7a1e6147e24ac0527e2cf242c758bab6f028766ba72f4cb36ea87d01fbd1fae2435ab294b
|
7
|
+
data.tar.gz: c067c54a08074a8d11ee6c506c4b73d14e65443868fa07683f0fa6dbd6af946b36ee1035e92be643801e9388ff0a9fd940e62e15e5a849cbf853e12cde1e5f05
|
@@ -49,17 +49,20 @@ update_status = (data) ->
|
|
49
49
|
$(".info").html("State: #{data.state}, gw: #{data.active_gw_id}, app: #{data.options.app_name}, ")
|
50
50
|
|
51
51
|
|
52
|
-
|
52
|
+
@ajax = (obj) ->
|
53
|
+
console.log "doin ajax"
|
54
|
+
form=$(obj).closest("form")
|
55
|
+
key=form.attr('id')
|
56
|
+
q=$( form ).serialize()
|
57
|
+
console.log q
|
53
58
|
$.ajax
|
54
|
-
url: "/
|
59
|
+
url: "/action.json?#{q}"
|
55
60
|
type: "GET"
|
56
|
-
data: "123"
|
57
61
|
processData: false
|
58
62
|
contentType: false
|
59
63
|
success: (data) ->
|
60
64
|
console.log "ajax returns: ", data
|
61
|
-
|
62
|
-
|
65
|
+
|
63
66
|
return
|
64
67
|
error: (xhr, ajaxOptions, thrownError) ->
|
65
68
|
alert thrownError
|
data/http/haml/mqtt-sn-sub.haml
CHANGED
@@ -9,6 +9,18 @@
|
|
9
9
|
%body
|
10
10
|
%h1 MQTT-SN-SUB Subscriber Utility
|
11
11
|
%p.info ?
|
12
|
+
%form{ onsubmit: "ajax(this); return(false);"}
|
13
|
+
Msg:
|
14
|
+
%input{name: "msg", size: 20}
|
15
|
+
Topic:
|
16
|
+
%input{name: "topic", size: 10}
|
17
|
+
QoS:
|
18
|
+
%select.qos{name: "qos"}
|
19
|
+
%option{value: "0"} 0
|
20
|
+
%option{value: "1"} 1
|
21
|
+
%option{value: "2"} 2
|
22
|
+
%button{value: "Publish", onclick: "ajax(this); return(false);"}
|
23
|
+
Publish
|
12
24
|
%hr
|
13
25
|
%p Gateways:
|
14
26
|
%p.data
|
data/http/json/action.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encode: UTF-8
|
3
|
+
|
4
|
+
|
5
|
+
def json_action request,args,session,event
|
6
|
+
#$sn.pub msg: "jees123", server_uri: "udp://20.20.20.21:1882"
|
7
|
+
puts "agrs:#{args}"
|
8
|
+
topic="top"
|
9
|
+
msg=args['msg']||"test_message"
|
10
|
+
topic=args['topic']||"XX"
|
11
|
+
qos=(args['qos']||0).to_i
|
12
|
+
puts "1"
|
13
|
+
$sn.publish topic, msg, qos: qos
|
14
|
+
puts "2"
|
15
|
+
data={jee: 123}
|
16
|
+
return ["text/json",data]
|
17
|
+
end
|
data/http/json/gateways.rb
CHANGED
@@ -3,13 +3,16 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
|
6
|
-
def json_gateways request,session,event
|
6
|
+
def json_gateways request,args,session,event
|
7
7
|
if not session or session==0
|
8
8
|
return ["text/event-stream",{}]
|
9
9
|
end
|
10
10
|
@http_sessions[session][:log_pos]=0 if not @http_sessions[session][:log_pos]
|
11
11
|
size=$sn.http_log.size
|
12
12
|
if @http_sessions[session][:log_pos]!=size
|
13
|
+
if size-@http_sessions[session][:log_pos]>30
|
14
|
+
@http_sessions[session][:log_pos]=size-30
|
15
|
+
end
|
13
16
|
loglines=$sn.http_log[@http_sessions[session][:log_pos],size-@http_sessions[session][:log_pos]]
|
14
17
|
@http_sessions[session][:log_pos]=size
|
15
18
|
else
|
data/lib/mqtt-sn-http.rb
CHANGED
@@ -7,7 +7,8 @@ require "coffee-script"
|
|
7
7
|
def http_server options
|
8
8
|
prev_t={}
|
9
9
|
#@http_log=[]
|
10
|
-
|
10
|
+
ports=Socket.getifaddrs.map { |i| i.addr.ip_address if i.addr.ipv4? }.compact
|
11
|
+
puts "Starting HTTP services at port #{options[:http_port]}, server IPs: #{ports}"
|
11
12
|
if File.directory? './http'
|
12
13
|
$http_dir="http/"
|
13
14
|
else
|
@@ -26,8 +27,18 @@ def http_server options
|
|
26
27
|
type="text/html"
|
27
28
|
req=request[1]
|
28
29
|
req="/#{http_app}.html" if req=="/" or req=="/index.htm" or req=="/index.html"
|
29
|
-
req,
|
30
|
-
|
30
|
+
req,argss=req.split "\?"
|
31
|
+
puts "req: #{req}, agss:#{argss}"
|
32
|
+
args={}
|
33
|
+
if argss
|
34
|
+
argss.split("&").each do |a|
|
35
|
+
if a
|
36
|
+
k,v=URI.decode(a).force_encoding("UTF-8").split "="
|
37
|
+
args[k]=v
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
puts "req: #{req}, args:#{args}"
|
31
42
|
if req[/\.html$/] and File.file?(fn="#{$http_dir}haml#{req.gsub('.html','.haml')}")
|
32
43
|
contents = File.read(fn)
|
33
44
|
response=Haml::Engine.new(contents).render
|
@@ -57,16 +68,17 @@ def http_server options
|
|
57
68
|
end
|
58
69
|
end
|
59
70
|
if type!="text/event-stream" and status=="200 OK"
|
71
|
+
puts "json_#{act} request,args,0,0 '#{req},#{args}'"
|
60
72
|
begin
|
61
|
-
type,response=eval "json_#{act}
|
62
|
-
response=response.to_json
|
73
|
+
type,response=eval "json_#{act} req,args,0,0" #event handlers get called with zero session => init :)
|
63
74
|
rescue => e
|
64
75
|
puts "**** AJAX EXEC #{fn} failed: #{e}"
|
65
76
|
pp e.backtrace
|
66
77
|
response=[{act: :error, msg:"Error executing JSON",alert: "Syntax error '#{e}' in '#{fn}'"}].to_json
|
67
78
|
type="text/json"
|
68
79
|
end
|
69
|
-
|
80
|
+
response=response.to_json
|
81
|
+
end
|
70
82
|
elsif req[/\.css$/] and File.file?(fnc="#{$http_dir}css#{req}")
|
71
83
|
type="text/css"
|
72
84
|
contents = File.read(fnc)
|
@@ -94,7 +106,7 @@ def http_server options
|
|
94
106
|
my_event=0
|
95
107
|
loop do
|
96
108
|
begin
|
97
|
-
type,response=eval "json_#{act} request,my_session,my_event"
|
109
|
+
type,response=eval "json_#{act} request,args,my_session,my_event"
|
98
110
|
my_event+=1
|
99
111
|
rescue => e
|
100
112
|
puts "**** AJAX EXEC #{fn} failed: #{e}"
|
data/lib/mqtt-sn-ruby.rb
CHANGED
@@ -69,13 +69,17 @@ class MqttSN
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def note str,*args
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
@
|
72
|
+
begin
|
73
|
+
s=sprintf(str,*args)
|
74
|
+
text=sprintf("%s: %s",Time.now.iso8601,s)
|
75
|
+
@log_q << text
|
76
|
+
if @options[:http_port] and @http_log
|
77
|
+
@http_log << {stamp: Time.now.to_i, text: text}
|
78
|
+
end
|
79
|
+
rescue => e
|
80
|
+
pp e.backtrace
|
81
|
+
puts "note dies: #{e} '#{str}'"
|
77
82
|
end
|
78
|
-
|
79
83
|
end
|
80
84
|
|
81
85
|
def log_empty?
|
@@ -170,6 +174,8 @@ class MqttSN
|
|
170
174
|
@will_msg=nil
|
171
175
|
@active_gw_id=nil
|
172
176
|
@http_log=[]
|
177
|
+
@local_ifs=Socket.getifaddrs.map { |i| i.addr.ip_address if i.addr.ipv4? }.compact
|
178
|
+
|
173
179
|
|
174
180
|
@sem=Mutex.new
|
175
181
|
@gsem=Mutex.new
|
@@ -211,8 +217,8 @@ class MqttSN
|
|
211
217
|
end
|
212
218
|
if @forwarder
|
213
219
|
@s,@server,@port = MqttSN::open_port @server_uri
|
214
|
-
|
215
|
-
@local_port
|
220
|
+
@local_port=hash[:local_port]||1882
|
221
|
+
note "Open port to Gateway: #{@server_uri}: #{@server},#{@port} -- Listening local port: #{@local_port} @ IPs: #{@local_ifs}"
|
216
222
|
@s.bind("0.0.0.0",@local_port)
|
217
223
|
@bcast_period=60
|
218
224
|
else
|
@@ -283,10 +289,14 @@ class MqttSN
|
|
283
289
|
sbytes=@clients[key][:socket].send(r, 0, @server, @port) # to rsmb -- ok as is
|
284
290
|
_,port,_,_ = @clients[key][:socket].addr
|
285
291
|
dest="#{@server}:#{port}"
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
292
|
+
begin
|
293
|
+
if @active_gw_id
|
294
|
+
logger "cs %-24.24s -> %-24.24s | %s", @clients[key][:uri],@gateways[@active_gw_id][:uri],m.to_json
|
295
|
+
else
|
296
|
+
logger "cs %-24.24s -> %-24.24s | %s", @clients[key][:uri],"??",m.to_json
|
297
|
+
end
|
298
|
+
rescue Exception =>e
|
299
|
+
puts "logging fails #{e}"
|
290
300
|
end
|
291
301
|
end
|
292
302
|
end
|
@@ -324,7 +334,6 @@ class MqttSN
|
|
324
334
|
end
|
325
335
|
|
326
336
|
def send type,hash={},&block
|
327
|
-
#puts "" if @verbose
|
328
337
|
if @state==:disconnected and type!=:connect and type!=:will_topic and type!=:will_msg and type!=:searchgw
|
329
338
|
if type==:disconnect
|
330
339
|
return #already disconnected.. nothing to do
|
@@ -429,7 +438,7 @@ class MqttSN
|
|
429
438
|
end
|
430
439
|
p=[PUBLISH_TYPE,flags,hash[:topic_id] >>8 ,hash[:topic_id] & 0xff,@msg_id >>8 ,@msg_id & 0xff]
|
431
440
|
hash[:msg].each_byte do |b|
|
432
|
-
p<<b
|
441
|
+
p<<b.ord
|
433
442
|
end
|
434
443
|
@msg_id+=1
|
435
444
|
when :pubrel
|
@@ -573,7 +582,7 @@ class MqttSN
|
|
573
582
|
MqttSN::send_raw_packet msg,@gateways[@active_gw_id][:socket],uri.host,uri.port
|
574
583
|
_,port,_,_ = @gateways[@active_gw_id][:socket].addr
|
575
584
|
src="udp://0.0.0.0:#{port}"
|
576
|
-
logger "od %-24.24s <- %-24.24s | %s",@gateways[@active_gw_id][:uri],src,MqttSN::parse_message(msg).merge(debug).to_json
|
585
|
+
logger "od %-24.24s <- %-24.24s | %s",@gateways[@active_gw_id][:uri],src,MqttSN::parse_message(msg).merge(debug).to_json ###utf-8
|
577
586
|
end
|
578
587
|
end
|
579
588
|
if not ok
|
@@ -793,7 +802,7 @@ class MqttSN
|
|
793
802
|
when PUBLISH_TYPE
|
794
803
|
topic_id=(r[3].ord<<8)+r[4].ord
|
795
804
|
msg_id=(r[5].ord<<8)+r[6].ord
|
796
|
-
msg=r[7,len-7]
|
805
|
+
msg=r[7,len-7].force_encoding("UTF-8")
|
797
806
|
flags=r[2].ord
|
798
807
|
topic_type=:long
|
799
808
|
topic=""
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Siitonen
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- http/css/mqtt-sn.css
|
34
34
|
- http/haml/mqtt-sn-forwarder.haml
|
35
35
|
- http/haml/mqtt-sn-sub.haml
|
36
|
+
- http/json/action.rb
|
36
37
|
- http/json/clients.rb
|
37
38
|
- http/json/gateways.rb
|
38
39
|
- lib/mqtt-sn-http.rb
|