mosca 0.0.2
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 +7 -0
- data/lib/command_builder.rb +38 -0
- data/lib/mosca/version.rb +3 -0
- data/lib/mosca.rb +109 -0
- data/mosca.gemspec +24 -0
- data/spec/mosca_spec.rb +31 -0
- data/spec/spec_helper.rb +1 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8790d6fe2852f8a4486cbc47d9ed48c43e0c2e1b
|
4
|
+
data.tar.gz: e166ef25f408e8d7532eed8ee6690c705d98a5d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 92d433566337cb9cc73eb153accd127d300857ad89967b3c0bdadc8670de0299bfc18471e6a0bd2763b6f2c8ed0d720792c4f95b03f4cd0f171d719ed3c94275
|
7
|
+
data.tar.gz: 530ba900c1cd6e483cbc4fd70fa27efd2cf3d3097eecaec2f351824417174a3f2f40d2652653032145cb7e7f923b34ecd484e600d6b0309a9dac716a1dd732fe
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module CommandBuilder
|
4
|
+
public
|
5
|
+
|
6
|
+
def method_missing(method, *args)
|
7
|
+
json = full_hash(method, args.first).to_json
|
8
|
+
client.publish json
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
def default_hash
|
14
|
+
{}
|
15
|
+
end
|
16
|
+
|
17
|
+
def process_command command, args
|
18
|
+
{command: command}
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def specific_hash method, args
|
24
|
+
hash = {}
|
25
|
+
if respond_to? "#{method}_hash"
|
26
|
+
hash.merge! send("#{method}_hash", args)
|
27
|
+
end
|
28
|
+
hash
|
29
|
+
end
|
30
|
+
|
31
|
+
def full_hash method, args
|
32
|
+
command_default = process_command(method, args)
|
33
|
+
command_specific = specific_hash(method, args)
|
34
|
+
default_hash.merge(command_default).merge(command_specific)
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
end
|
data/lib/mosca.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'mqtt'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class Mosca
|
5
|
+
@@default_broker = "test.mosquitto.org"
|
6
|
+
@@default_timeout = 5
|
7
|
+
@@debug = false
|
8
|
+
|
9
|
+
attr_accessor :user, :pass, :topic_in, :topic_out, :broker, :topic_base
|
10
|
+
|
11
|
+
def initialize params = {}
|
12
|
+
@user = params[:user]
|
13
|
+
@pass = params[:pass]
|
14
|
+
@topic_in = params[:topic_in]
|
15
|
+
@topic_out = params[:topic_out]
|
16
|
+
@topic_base = params[:topic_base] || ""
|
17
|
+
@broker = params[:broker] || @@default_broker
|
18
|
+
@client = params[:client] || MQTT::Client
|
19
|
+
end
|
20
|
+
|
21
|
+
def publish json, params = {}
|
22
|
+
connection do |c|
|
23
|
+
topic = params[:topic] || @topic_out
|
24
|
+
debug "[start publish] " + timestamp
|
25
|
+
c.subscribe(topic_base + topic_in) if params[:response]
|
26
|
+
c.publish(topic_base + topic,json)
|
27
|
+
debug "[end publish] " + timestamp
|
28
|
+
if params[:response]
|
29
|
+
return get(params.merge({connection: c}))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def get params = {}
|
35
|
+
response = {}
|
36
|
+
connection(params) do |c|
|
37
|
+
topic = params[:topic] || @topic_in
|
38
|
+
timeout = params[:timeout] || @@default_timeout
|
39
|
+
begin
|
40
|
+
Timeout.timeout(timeout) do
|
41
|
+
debug "[start get] " + timestamp
|
42
|
+
c.get(topic_base + topic) do |topic, message|
|
43
|
+
response = parse_response message
|
44
|
+
break
|
45
|
+
end
|
46
|
+
debug "[end get] " + timestamp
|
47
|
+
end
|
48
|
+
rescue
|
49
|
+
end
|
50
|
+
end
|
51
|
+
response
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_with_connection
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.default_broker= param
|
59
|
+
@@default_broker = param
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.default_timeout= param
|
63
|
+
@@default_timeout = param
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.debug= param
|
67
|
+
@@debug = param
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def opts
|
73
|
+
{remote_host: @broker, username: @user, password: @pass}
|
74
|
+
end
|
75
|
+
|
76
|
+
def connection params = {}
|
77
|
+
if params[:connection]
|
78
|
+
yield params[:connection]
|
79
|
+
else
|
80
|
+
@client.connect(opts) do |c|
|
81
|
+
yield c
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def parse_response response
|
87
|
+
if valid_json? response
|
88
|
+
response = JSON.parse response
|
89
|
+
end
|
90
|
+
response
|
91
|
+
end
|
92
|
+
|
93
|
+
def valid_json? json_
|
94
|
+
begin
|
95
|
+
JSON.parse(json_)
|
96
|
+
return true
|
97
|
+
rescue
|
98
|
+
return false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def debug message
|
103
|
+
puts message if @@debug
|
104
|
+
end
|
105
|
+
|
106
|
+
def timestamp
|
107
|
+
Time.new.to_f.to_s
|
108
|
+
end
|
109
|
+
end
|
data/mosca.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'mosca/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = 'mosca'
|
10
|
+
s.version = Mosca::VERSION
|
11
|
+
s.summary = "MQTT messaging made easy"
|
12
|
+
s.description = "A simple client for mqtt communication"
|
13
|
+
s.authors = ["Armando Andini"]
|
14
|
+
s.email = 'armando.andini@hotmail.com'
|
15
|
+
s.license = 'MIT'
|
16
|
+
s.homepage = 'http://github.com/antico5/mosca'
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split($/)
|
19
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency 'mqtt', '~> 0.2.0'
|
23
|
+
s.add_dependency 'json', '~> 1.8.1'
|
24
|
+
end
|
data/spec/mosca_spec.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'mosca'
|
2
|
+
|
3
|
+
describe Mosca do
|
4
|
+
before do
|
5
|
+
@client = Mosca.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "has a default broker" do
|
9
|
+
@client.broker.should eq("test.mosquitto.org")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "uses topic_out to publish if it was specified" do
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
it "uses topic_in to get messages if it was specified" do
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
it "publishes messages" do
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
it "gets messages" do
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
it "gets a hash if the message was JSON" do
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'json'
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mosca
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Armando Andini
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mqtt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.8.1
|
41
|
+
description: A simple client for mqtt communication
|
42
|
+
email: armando.andini@hotmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/command_builder.rb
|
48
|
+
- lib/mosca.rb
|
49
|
+
- lib/mosca/version.rb
|
50
|
+
- mosca.gemspec
|
51
|
+
- spec/mosca_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
homepage: http://github.com/antico5/mosca
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.0.3
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: MQTT messaging made easy
|
77
|
+
test_files:
|
78
|
+
- spec/mosca_spec.rb
|
79
|
+
- spec/spec_helper.rb
|