aws_iot_device 0.1.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +155 -148
- data/aws_iot_device.gemspec +11 -11
- data/codeclimate.yml +6 -0
- data/lib/aws_iot_device.rb +2 -1
- data/lib/aws_iot_device/mqtt_adapter.rb +1 -2
- data/lib/aws_iot_device/mqtt_adapter/client.rb +74 -6
- data/lib/aws_iot_device/mqtt_adapter/paho_mqtt_adapter.rb +207 -0
- data/lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb +10 -4
- data/lib/aws_iot_device/mqtt_shadow_client.rb +1 -0
- data/lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb +133 -67
- data/lib/aws_iot_device/mqtt_shadow_client/shadow_action_manager.rb +229 -146
- data/lib/aws_iot_device/mqtt_shadow_client/shadow_client.rb +78 -20
- data/lib/aws_iot_device/mqtt_shadow_client/shadow_topic_manager.rb +51 -26
- data/lib/aws_iot_device/mqtt_shadow_client/topic_builder.rb +15 -30
- data/lib/aws_iot_device/version.rb +1 -1
- data/samples/config_shadow.rb +72 -0
- data/samples/mqtt_client_samples/mqtt_client_samples.rb +7 -59
- data/samples/shadow_action_samples/sample_shadow_action_update.rb +7 -61
- data/samples/shadow_client_samples/samples_shadow_client_block.rb +25 -0
- data/samples/shadow_client_samples/samples_shadow_client_delete.rb +9 -58
- data/samples/shadow_client_samples/samples_shadow_client_description.rb +64 -0
- data/samples/shadow_client_samples/samples_shadow_client_get.rb +11 -62
- data/samples/shadow_client_samples/samples_shadow_client_getting_started.rb +22 -0
- data/samples/shadow_client_samples/samples_shadow_client_update.rb +7 -58
- data/samples/shadow_topic_samples/sample_topic_manager.rb +10 -61
- metadata +71 -16
- data/lib/aws_iot_device/mqtt_adapter/mqtt_adapter.rb +0 -139
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35edf7c0cb543d0095ff0d45af83739cbedb56e0
|
4
|
+
data.tar.gz: 639d254baa0e2cdd31cea8549ff96830992a9a58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b20075b2046770c39c864ec1a25d992d612a20aa816a124fa2d42a1845bea57dc80bd3887cb41ea5b9fb587bb513f729cb6af1a8e06020a1a810a8314ba2f73
|
7
|
+
data.tar.gz: 31f553b11f8641b2e475c869c58b78daf088d1201151770cbc1ceb1cf85300c5b23eef634c62d09623f8f55bff38fd21c9470ea3d653f43f39bf485c0096c8f9
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,30 +1,39 @@
|
|
1
1
|
<a href="https://codeclimate.com/repos/57d10b3aaee68e0a2d0016b7/feed"><img src="https://codeclimate.com/repos/57d10b3aaee68e0a2d0016b7/badges/aad862afb3ada6425b90/gpa.svg" /></a>
|
2
2
|
[![Dependency Status](https://gemnasium.com/badges/github.com/RubyDevInc/aws-iot-device-sdk-ruby.svg)](https://gemnasium.com/github.com/RubyDevInc/aws-iot-device-sdk-ruby)
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/aws_iot_device.svg)](https://badge.fury.io/rb/aws_iot_device)
|
4
4
|
|
5
5
|
# AWS IoT Device SDK for Ruby
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
## Contents
|
8
|
+
* [Dependencies](#dependencies)
|
9
|
+
* [Overview](#overview)
|
10
|
+
* [Installation](#installation)
|
11
|
+
* [Usage](#usage)
|
12
|
+
* [Getting started](#getting-started)
|
13
|
+
* [Sample files](#sample-files)
|
14
|
+
* [API Description](#api-description)
|
15
|
+
* [Shadow Client](#shadow-client)
|
16
|
+
* [Connection Mode](#connection-mode)
|
17
|
+
* [MQTT Adapter](#mqtt-adapter)
|
18
|
+
* [License](#license)
|
19
|
+
* [Contact](#contact)
|
20
|
+
|
21
|
+
## Dependencies
|
9
22
|
Ruby gems:
|
10
|
-
- ruby
|
11
|
-
- mqtt
|
12
|
-
- json
|
13
|
-
- facets
|
14
|
-
- timers
|
15
|
-
|
16
|
-
## Introduction
|
17
|
-
The AWS IoT SDK for Ruby is a gems which enables to manage device registered as shadow/things on the AWS IoT platform. A shadow is a JSON document that describes the state of a associated thing(app, device, sensor,...). The JSON document is divided in two part, the desired and the reported state of the thing. Three operations could be done on the Shadow:
|
18
|
-
- Get: read the current state of the shadow
|
19
|
-
- Update: add, change or remove the attribute value of the shadow
|
20
|
-
- Delete: clear all the attribute value of the shadow
|
23
|
+
- ruby >= 2.2
|
24
|
+
- mqtt >= 0.0.2
|
25
|
+
- json >= 2.0
|
26
|
+
- facets >= 3.1
|
27
|
+
- timers >= 4.1
|
28
|
+
- paho-mqtt >= 0.0.2
|
21
29
|
|
22
|
-
|
30
|
+
## Overview
|
31
|
+
`aws_iot_device` is a gem that enables a remote client to communicate with the AWS IoT platform. The AWS IoT platform allows to register a device as a `thing`. The status of each `thing` is stored in a json format, referred as the `shadow` of a `thing`. The SDK uses the MQTT protocol to control the `thing` registered on the AWS IoT platform. The MQTT protocol is a lightweight protocol used to exchange short messages between a client and a message broker. The message broker is located on the AWS IoT platform, and the client is included in the `aws_iot_device` gem, the default client is the `paho-mqtt`. The `paho-mqtt` client has a MQTT API and a callback system to handle the events trigger by the mqtt packages.
|
23
32
|
|
24
33
|
## Installation
|
25
|
-
The gem is
|
26
|
-
|
27
|
-
|
34
|
+
The gem is currentely in its first version, key features are available but any improvements is welcomed.
|
35
|
+
There are two ways to install the gem, from [rubygems](https://rubygems.org/gems/aws_iot_device) or directly from [sources](https://github.com/RubyDevInc/aws-iot-device-sdk-ruby).
|
36
|
+
- From RubyGems:
|
28
37
|
The gem may be find on [RubyGems](https://rubygems.org/gems/aws_iot_device) and installed with the following command:
|
29
38
|
```
|
30
39
|
gem install aws_iot_device
|
@@ -33,168 +42,166 @@ gem install aws_iot_device
|
|
33
42
|
- From sources:
|
34
43
|
|
35
44
|
The gem could be download and installed manually:
|
45
|
+
|
36
46
|
```
|
37
47
|
git clone https://github.com/RubyDevInc/aws-iot-device-sdk-ruby.git
|
38
48
|
cd aws-iot-device-sdk-ruby
|
39
49
|
bundle install
|
40
50
|
```
|
51
|
+
## Usage
|
52
|
+
### Getting started
|
53
|
+
The following example is a strait-forward way for using shadows. Check at the samples files for more detailed usage.
|
54
|
+
```ruby
|
55
|
+
require "aws_iot_device"
|
56
|
+
|
57
|
+
host = "AWS IoT endpoint"
|
58
|
+
port = 8883
|
59
|
+
thing = "Thing Name"
|
60
|
+
|
61
|
+
root_ca_path = "Path to your CA certificate"
|
62
|
+
private_key_path = "Path to your private key"
|
63
|
+
certificate_path = "Path to your certificate"
|
64
|
+
|
65
|
+
shadow_client = AwsIotDevice::MqttShadowClient::ShadowClient.new
|
66
|
+
shadow_client.configure_endpoint(host, port)
|
67
|
+
shadow_client.configure_credentials(root_ca_path, private_key_path, certificate_path)
|
68
|
+
shadow_client.create_shadow_handler_with_name(thing, true)
|
69
|
+
|
70
|
+
shadow_client.connect
|
71
|
+
shadow_client.get_shadow do |message|
|
72
|
+
# Do what you want with the get_shadow's answer
|
73
|
+
# ...
|
74
|
+
p ":)"
|
75
|
+
end
|
76
|
+
sleep 2 #Timer to ensure that the answer is received
|
41
77
|
|
42
|
-
|
43
|
-
|
44
|
-
```bash
|
45
|
-
### If the gem have been install with the `gem` command
|
46
|
-
ruby "sample_file".rb -c "path to certificate" -a "path to authority certificate" -k "path to key" -H "aws endpoint URI" -t "thing name"
|
78
|
+
shadow_client.disconnect
|
79
|
+
```
|
47
80
|
|
81
|
+
### Sample files
|
82
|
+
Once you have cloned the current repository the several samples files provide test on the API a multiple levels.
|
83
|
+
The shadow examples could be run with the following command :
|
84
|
+
```
|
85
|
+
ruby samples/shadow_client_samples/samples_shadow_client_xxx.rb -c "CERTIFICATE PATH" -k "PRIVATE KEY PATH" -a "CA CERTIFICATE PATH" -H "AWS IOT ENDPOINT" -t "THING NAME"
|
86
|
+
```
|
48
87
|
|
49
|
-
|
50
|
-
|
51
|
-
ruby -I lib "sample_file".rb -c "path to certificate" -a "path to authority certificate" -k "path to key" -H "aws endpoint URI" -t "thing name"
|
88
|
+
## API Description
|
89
|
+
Thank you very much for your interst in the `aws_iot_device` gem. The following part details all the features available with the SDK.
|
52
90
|
|
53
|
-
|
91
|
+
### Shadow Client
|
92
|
+
The shadow client API provide the key functions to control a thing/shadow on the Aws IoT platform. The methods contain in the API could be seperate in three differents roles, the configuration roles, the communication roles, and the treatements roles.
|
54
93
|
|
55
|
-
|
56
|
-
|
94
|
+
#### Configuration role
|
95
|
+
The Shadow client initializer would create the mqtt client used by the shadow client to communicate with the remote host. The parameters available on the initialization depend on the type of the chosen mqtt client. The default mqtt client type is the paho_client, the availables parameters are detailed in `paho-mqtt` gem [page](https://github.com/RubyDevInc/paho.mqtt.ruby#initialization).
|
96
|
+
|
97
|
+
The remote host(endpoint) and port could be configured with the following method:
|
98
|
+
```
|
99
|
+
shaadow_client.configure_endpoint(host, port)
|
57
100
|
```
|
58
101
|
|
59
|
-
|
60
|
-
|
102
|
+
The encryption configuration could be done with the following method:
|
103
|
+
```
|
104
|
+
shadow_client.configure_credentials(root_ca_path, private_key_path, certificate_path)
|
105
|
+
```
|
61
106
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
private_key_path = "PATH_TO_YOUR_PRIVATE_KEY"
|
66
|
-
certificate_path = "PATH_TO_YOUR_CERTIFICATE_FILE"
|
107
|
+
The thing where the client would connect is done by the following method. The persitent_subscribe attribute is a boolean that would prevent the client to unsubscribe to the mqtt topic after every action. As the susbcription require a short time, for performace issues it might usefull to keep subscribed (default is `false`).
|
108
|
+
```
|
109
|
+
shadow_client.create_shadow_handler_with_name(thing, persistent_subscribe)
|
67
110
|
|
68
|
-
|
69
|
-
host = "ENDPOINT_URI_ON_AWS"
|
111
|
+
```
|
70
112
|
|
71
|
-
|
72
|
-
|
113
|
+
Finally, the connect method enable to send a connect request and (re)define some attributes threw a `Hash`. The available parameter are `:host`, `:port`, `:keep_alive`, `:persitent` and `:blocking`. `:persistent` and `:blocking` are detailed in the connection mode [section](#connection-mode)
|
114
|
+
```
|
115
|
+
shadow_client.connect
|
116
|
+
```
|
73
117
|
|
74
|
-
|
118
|
+
#### Communication role
|
119
|
+
In the API there are three methods that directely act on the remote shadow. A `timeout` define the time until which the request should be considered as a failure. The default timeout is five second. The `get` and `delete` do not accept a payload where a json format payload is mandatory for the `update` action.
|
120
|
+
```
|
121
|
+
timeout = 2
|
75
122
|
|
76
|
-
|
77
|
-
my_shadow_client.configure_endpoint(host, port)
|
78
|
-
my_shadow_client.configure_credentials(root_ca_path, private_key_path, certificate_path)
|
123
|
+
shadow_client.get_shadow(timeout)
|
79
124
|
|
80
|
-
|
81
|
-
|
82
|
-
my_shadow_client.create_shadow_handler_with_name("YOUR_THING_NAME", false)
|
125
|
+
payload = "{\"state: \": \"desired\": ..... }"
|
126
|
+
shadow_client.update_shadow(payload)
|
83
127
|
|
84
|
-
|
85
|
-
|
128
|
+
shadow_client.delete_shadow
|
129
|
+
```
|
130
|
+
|
131
|
+
#### Callbacks
|
132
|
+
Callbacks are small piece of code that would be executed when the answer of one action is received by the client. The Shadow client API enables to register two kind of callbacks, a generic callback and single usage callback.
|
133
|
+
For generic action (get, update, delete), the callback would be executed on every answer of the dedicated generic action. Futhermore, another callback may be executed for a single answer of a dedicated action. The generic callback could be seen as re-usable callback functions when the single usage could be seen as disposable treatment.
|
134
|
+
Both kind of callbacks must be registered as a `block`, a `proc` or a `lambda`. The followings lines provide an examples of the different callbacks usage.
|
135
|
+
```ruby
|
136
|
+
# Register a callback for the get action as a block
|
137
|
+
shadow_client.register_get_callback do
|
138
|
+
puts "generic callback for get action"
|
86
139
|
end
|
87
|
-
### The three basic AWS Iot operations:
|
88
|
-
### time_out is a integer reprensenting the time to keep request alive in second
|
89
|
-
my_shadow_client.get_shadow(callback, time_out)
|
90
|
-
# or without special callback
|
91
|
-
my_shadow_client.get_shadow(nil, time_out)
|
92
140
|
|
93
|
-
|
141
|
+
# Register a callback for the delete action as a proc
|
142
|
+
shadow_client.register_delete_callback(proc { puts "generic callback for delete action"})
|
94
143
|
|
95
|
-
|
96
|
-
|
97
|
-
my_shadow_client.update_shadow(payload, callback, time_out)
|
98
|
-
```
|
144
|
+
# Register a callback for the update action as a lambda
|
145
|
+
shadow_client.register_update_callback(lambda {|message| puts "genereic callback for update action"})
|
99
146
|
|
147
|
+
# Register a callback for the delta events
|
148
|
+
shadow_client.register_delta_callback do
|
149
|
+
puts "delta have been catch in a callback"
|
150
|
+
end
|
100
151
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
152
|
+
# Send a get request with an associated callback in a block
|
153
|
+
shadow_client.get_shadow do
|
154
|
+
puts "get thing"
|
155
|
+
end
|
156
|
+
|
157
|
+
# Send a delete request with an associated callback in a proc
|
158
|
+
shadow_client.delete_shadow(timeout, proc {puts "delete thing"})
|
159
|
+
|
160
|
+
# Send a udpate request with an associated callback in a lambda
|
161
|
+
shadow_client.update_shadow("{\"state\":{\"desired\":{\"message\":\"Hello\"}}}", timeout, lambda {|message| puts "update thing"})
|
162
|
+
|
163
|
+
# Clear out the previously registered callback for each action
|
164
|
+
shadow_client.remove_get_callback
|
165
|
+
shadow_client.remove_update_callback
|
166
|
+
shadow_client.remove_delete_callback
|
167
|
+
shadow_client.remove_delta_callback
|
112
168
|
```
|
113
|
-
|
169
|
+
|
170
|
+
### Client persitences
|
171
|
+
For performance issues, sometimes subscriptions and (re)connection time would better to saved. This could be done with two different persistences, the subscription persistence and the connection persistence. The subscription persistence keeps the shadow_client subscribed to action topics (get/accepted, get/rejected, update/accepted, update/rejected, delete/accepted and delete/rejected). The subscription process require a short time, the persistent subscription avoid to susbscribe before each and so saved the subscription time. The subscription persistence could be set at the initialization of the shadow handler or directely at the shadow client initialization.
|
172
|
+
```ruby
|
173
|
+
shadow_client = AwsIotDevice::MqttShadowClient::ShadowClient.new({:shadow_name => "Thing name", :persistent_subscribe => true})
|
174
|
+
# Or
|
175
|
+
shadow_client.create_shadow_handler_with_name(shadow_name, persistent_subscribe)
|
114
176
|
```
|
115
|
-
The
|
177
|
+
The default subscription persistence if desactivated, so the client might subscribe/unsubscribe to reserved topics before/after every actions.
|
178
|
+
|
179
|
+
The connection persistence enables the client to keep the mqtt connection alive until the client explicitly requests to disconnect. The basic client would disconnect from the remote host if no activity has been detected for a preset keep_alive timer. There is two ways to configure the connection persistence, at the initialization of the shadow client or at the connection time.
|
116
180
|
```ruby
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
# For exemple for Tokyo area: host = "xxx.iot.ap-northeast-1.amazonaws.com"
|
123
|
-
host = "ENDPOINT_URI_ON_AWS"
|
124
|
-
port = 8883 #default port of MQTT protocol
|
181
|
+
shadow_client = AwsIotDevice::MqttShadowClient::ShadowClient.new({:persistent => true})
|
182
|
+
# Or
|
183
|
+
shadow_client.connect({:persitent => true})
|
184
|
+
```
|
125
185
|
|
126
|
-
mqtt_manager = AwsIotDevice::MqttShadowClient::MqttManager.new(host,
|
127
|
-
port,
|
128
|
-
ssl,
|
129
|
-
certificate_path,
|
130
|
-
private_key_path,
|
131
|
-
root_ca_path)
|
132
186
|
|
133
|
-
|
187
|
+
### MQTT Adapter
|
188
|
+
The `aws-iot-device` gem is based on a MQTT client (`paho-mqtt`) that enables the usage of basic MQTT operations.
|
189
|
+
```ruby
|
190
|
+
mqtt_client = AwsIoTDevice::MqttShadowClient::MqttManager.new
|
134
191
|
|
135
|
-
|
192
|
+
mqtt_client.config_endpoint(host, port)
|
193
|
+
mqtt_client.config_ssl_context(host, port)
|
194
|
+
mqtt_client.connect
|
136
195
|
|
137
|
-
|
138
|
-
|
196
|
+
mqtt_client.subscribe(topic, qos, callback)
|
197
|
+
mqtt_client.publish(topic, "Hello world!", qos, retain)
|
139
198
|
|
140
|
-
|
141
|
-
|
142
|
-
manager.shadow_topic_subscribe("SHADOW_NAME", "SHADOW_ACTION", callback=nil)
|
143
|
-
manager.shadow_topic_unsubscribe(shadow_name, shadow_action)
|
199
|
+
mqtt_client.unsubscribe(topic)
|
200
|
+
mqtt_client.disconnect
|
144
201
|
```
|
202
|
+
For the default paho mqtt_client, some callbacks are available for each event related with the MQTT protocol. We recommend to read the `paho-mqtt` [gem page](https://github.com/RubyDevInc/paho.mqtt.ruby#handlers-and-callbacks) for more details about the mqtt callbacks usage.
|
145
203
|
|
146
|
-
|
147
|
-
|
148
|
-
```ruby
|
149
|
-
### Directly create the ShadowManagerAction
|
150
|
-
### SUBSRIBE_MODE is a boolean, 'true' for persistent mode and (default)'false' for not-persistent
|
151
|
-
client = AwsIotDevice::MqttShadowClient::ShadowActionManager.new("THING_NAME", "TOPIC_MANGER", "SUBSCRIBE_MODE")
|
152
|
-
|
153
|
-
### Or through a ShadowClient object
|
154
|
-
my_shadow_client = AwsIotDevice::MqttShadowClient::ShadowClient.new
|
155
|
-
client = my_shadow_client.create_shadow_handler_with_name("THING_NAME", "SUBSCRIBE_MODE")
|
204
|
+
## License
|
205
|
+
This SDK is distributed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
|
156
206
|
|
157
|
-
|
158
|
-
client.shadow_get("YOUR_CALLBACK_OR_NIL", "TIME_OUT")#TIME_OUT is a integer reprensenting the time to keep request alive in second
|
159
|
-
client.shadow_update("UPDATE_PAYLOAD", "YOUR_CALLBACK_OR_NIL", "TIME_OUT")
|
160
|
-
client.shadow_delete("YOUR_CALLBACK_OR_NIL", "TIME_OUT")
|
161
|
-
```
|
162
|
-
## The MQTT Manager
|
163
|
-
The MqttManager class supports the operations related with the MQTT protocol, it is a customized MQTT client. According to the MQTT protocol, the MqttManager may connect, publish, subscribe and disconnect. It holds a callbacks system which are triggered by MQTT events, for exemple when a message is received on a subscribed topic. Currently (September 2016), the callback system only support the message(PUBLISH) event, other events (CONNACK, SUBACK, ...) should be supported in the future version. It is possible to perform the previous AWS Iot operation through the MqttManager, by simply typing the desired topics in the publish request. The following example details how to sent a AWS Iot get request at the MQTT level.
|
164
|
-
```ruby
|
165
|
-
### There two way to initiate the object :
|
166
|
-
# 1) Send parameter when creating the object and connect
|
167
|
-
client = AwsIotDevice::MqttShadowClient::MqttManager.new(host: "YOUR_AWS_ENDPOINT",
|
168
|
-
port: 8883,
|
169
|
-
ssl: true,
|
170
|
-
cert_file: "YOUR_CERT_FILE_PATH",
|
171
|
-
key_file: "YOUR_KEY_FILE_PATH",
|
172
|
-
ca_file: "YOUR_ROOT_CA_FILE_PATH")
|
173
|
-
|
174
|
-
# 2) A step by step initialization
|
175
|
-
client = AwsIotDevice::MqttShadowClient::MqttManager.new()
|
176
|
-
client.host = "YOUR_AWS_ENDPOINT"
|
177
|
-
client.ssl = true
|
178
|
-
client.port = 8883
|
179
|
-
client.cert_file = "YOUR_CERT_FILE_PATH"
|
180
|
-
client.key_file = "YOUR_KEY_FILE_PATH"
|
181
|
-
client.ca_file = "YOUR_ROOT_CA_FILE_PATH"
|
182
|
-
|
183
|
-
### Then send a MQTT connect request
|
184
|
-
client.connect()
|
185
|
-
|
186
|
-
client.subscribe("THING_TOPIC_GET_ACCEPTED")
|
187
|
-
sleep 2 # Assert the subscription is completed
|
188
|
-
### An example of AWS Iot get operation
|
189
|
-
client.publish("THING_TOPIC_GET", "")
|
190
|
-
sleep 2 # Assert the answer is received to execute the callback
|
191
|
-
client.unsubscribe("THING_TOPIC_GET_ACCEPTED")
|
192
|
-
|
193
|
-
client.disconnect()
|
194
|
-
```
|
195
|
-
|
196
|
-
## MQTT Adapters modules
|
197
|
-
The previously detailed MqttManager class is said to be based on a MQTT client, in this project the MQTT client is implemented as an adapters design pattern named the MqttAdapter. The adapter design pattern enables the client implementation to be independent from the back-end MQTT library. Thanks to this design pattern, the MqttAdapter can work over several implementations of the MQTT protocol. The default implementation used in the project is the [ruby-mqtt](https://github.com/njh/ruby-mqtt) module, where some new features have been added. The adapters defined the method that should be accessible to higher level classes (ex. MqttManager).
|
198
|
-
|
199
|
-
### Ruby MQTT Adapter
|
200
|
-
The [ruby-mqtt](https://github.com/njh/ruby-mqtt) gem provides a client which does the basic MQTT operation(connect, subscribe, publish ....) by reading the packets directly from the sockets. It adapts the method of the [ruby-mqtt](https://github.com/njh/ruby-mqtt) gem in order to match with the definition in the MqttAdapter. Inspired by the [Paho](http://www.eclipse.org/paho/) library, a system of (infinite)loop in background is added to this class. This loop system enables a not blocking and automated message reading. Also, a callback system is enabled to make some treatment when message are received on a subscribed MQTT topic. If no specific callback is registered for the topic a default callback is executed.
|
207
|
+
## Contact
|
data/aws_iot_device.gemspec
CHANGED
@@ -9,10 +9,10 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Pierre Goudet"]
|
10
10
|
spec.email = ["p-goudet@ruby-dev.jp"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.description = %q{
|
12
|
+
spec.summary = %q{The ruby version of the AWS IoT Device SDK. It enables to connect to AWS IoT platform and manage Things.}
|
13
|
+
spec.description = %q{The gem is using the MQTT protocol to execute the command defined by the AWS IoT platform. A default MQTT client is included in the gem, however an adapter system enables to plug another MQTT client.}
|
14
14
|
spec.homepage = "https://github.com/RubyDevInc/aws-iot-device-sdk-ruby"
|
15
|
-
spec.license = "Apache
|
15
|
+
spec.license = "Apache-2.0"
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
18
|
# delete this section to allow pushing this gem to any host.
|
@@ -27,13 +27,13 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
-
spec.add_development_dependency "bundler", "~> 1.10"
|
31
|
-
spec.add_development_dependency "pry"
|
32
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
-
spec.add_development_dependency "rspec"
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.10", ">= 1.10"
|
31
|
+
spec.add_development_dependency "pry", "~> 0.10.4", ">= 0.10.4"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0", ">= 1.10"
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.5.0", ">= 3.5.0"
|
34
34
|
|
35
|
-
spec.add_runtime_dependency "facets", "~> 3.1.0"
|
36
|
-
spec.add_runtime_dependency "json", "~>
|
37
|
-
spec.add_runtime_dependency "mqtt", "~> 0.
|
38
|
-
spec.add_runtime_dependency "timers", "~> 4.1.1"
|
35
|
+
spec.add_runtime_dependency "facets", "~> 3.1.0", ">= 3.1.0"
|
36
|
+
spec.add_runtime_dependency "json", "~> 2.0.2", ">= 2.0.2"
|
37
|
+
spec.add_runtime_dependency "paho-mqtt", "~> 1.0.0", ">= 1.0.0"
|
38
|
+
spec.add_runtime_dependency "timers", "~> 4.1.1", ">= 4.1.1"
|
39
39
|
end
|
data/codeclimate.yml
ADDED
data/lib/aws_iot_device.rb
CHANGED
@@ -9,7 +9,7 @@ module AwsIotDevice
|
|
9
9
|
def adapter
|
10
10
|
return @adapter if @adapter
|
11
11
|
### Calling the setter method with the default symbol 'RubyMqttAdapter' and return it.
|
12
|
-
self.adapter = :
|
12
|
+
self.adapter = :paho_mqtt_adapter
|
13
13
|
@adapter
|
14
14
|
end
|
15
15
|
|
@@ -21,7 +21,6 @@ module AwsIotDevice
|
|
21
21
|
require "aws_iot_device/mqtt_adapter/#{adapter_lib}"
|
22
22
|
rescue LoadError
|
23
23
|
raise "LoadError: Could find adapters for the lib #{adapter_lib}"
|
24
|
-
exit
|
25
24
|
end
|
26
25
|
@adapter = MqttAdapter.const_get("#{adapter_lib.to_s.camelcase(:upper)}")
|
27
26
|
else
|
@@ -48,9 +48,9 @@ module AwsIotDevice
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def mqtt_loop
|
51
|
-
@adapter.
|
51
|
+
@adapter.mqtt_loop
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
def loop_read
|
55
55
|
@adapter.loop_read
|
56
56
|
end
|
@@ -83,30 +83,98 @@ module AwsIotDevice
|
|
83
83
|
@adapter.connected?
|
84
84
|
end
|
85
85
|
|
86
|
-
def subscribe(topic)
|
87
|
-
@adapter.subscribe(topic)
|
86
|
+
def subscribe(topic, qos)
|
87
|
+
@adapter.subscribe(topic, qos)
|
88
88
|
end
|
89
89
|
|
90
|
+
def subscribe_bunch(*topics)
|
91
|
+
@adapter.subscribe_bunch(topics)
|
92
|
+
end
|
93
|
+
|
90
94
|
def unsubscribe(topic)
|
91
95
|
@adapter.unsubscribe(topic)
|
92
96
|
end
|
93
97
|
|
98
|
+
def unsubscribe_bunch(*topics)
|
99
|
+
@adapter.unsubscribe_bunch(topics)
|
100
|
+
end
|
101
|
+
|
94
102
|
def set_tls_ssl_context(ca_cert, cert=nil, key=nil)
|
95
103
|
@adapter.set_tls_ssl_context(ca_cert, cert, key)
|
96
104
|
end
|
97
105
|
|
98
|
-
def add_callback_filter_topic(topic, callback)
|
99
|
-
@adapter.add_callback_filter_topic(topic, callback)
|
106
|
+
def add_callback_filter_topic(topic, callback=nil, &block)
|
107
|
+
@adapter.add_callback_filter_topic(topic, callback, &block)
|
100
108
|
end
|
101
109
|
|
102
110
|
def remove_callback_filter_topic(topic)
|
103
111
|
@adapter.remove_callback_filter_topic(topic)
|
104
112
|
end
|
105
113
|
|
114
|
+
def on_connack=(callback)
|
115
|
+
@adapter.on_connack = callback
|
116
|
+
end
|
117
|
+
|
118
|
+
def on_suback=(callback)
|
119
|
+
@adapter.on_suback = callback
|
120
|
+
end
|
121
|
+
|
122
|
+
def on_unsuback=(callback)
|
123
|
+
@adapter.on_unsuback = callback
|
124
|
+
end
|
125
|
+
|
126
|
+
def on_puback=(callback)
|
127
|
+
@adapter.on_puback = callback
|
128
|
+
end
|
129
|
+
|
130
|
+
def on_pubrec=(callback)
|
131
|
+
@adapter.on_pubrec = callback
|
132
|
+
end
|
133
|
+
|
134
|
+
def on_pubrel=(callback)
|
135
|
+
@adapter.on_pubrel = callback
|
136
|
+
end
|
137
|
+
|
138
|
+
def on_pubcomp=(callback)
|
139
|
+
@adapter.on_pubcomp = callback
|
140
|
+
end
|
141
|
+
|
106
142
|
def on_message=(callback)
|
107
143
|
@adapter.on_message = callback
|
108
144
|
end
|
145
|
+
|
146
|
+
def on_connack(&block)
|
147
|
+
@adapter.on_connack(&block)
|
148
|
+
end
|
149
|
+
|
150
|
+
def on_suback(&block)
|
151
|
+
@adapter.on_suback(&block)
|
152
|
+
end
|
153
|
+
|
154
|
+
def on_unsuback(&block)
|
155
|
+
@adapter.on_unsuback(&block)
|
156
|
+
end
|
157
|
+
|
158
|
+
def on_puback(&block)
|
159
|
+
@adapter.on_puback(&block)
|
160
|
+
end
|
161
|
+
|
162
|
+
def on_pubrec(&block)
|
163
|
+
@adapter.on_pubrec(&block)
|
164
|
+
end
|
109
165
|
|
166
|
+
def on_pubrel(&block)
|
167
|
+
@adapter.on_pubrel(&block)
|
168
|
+
end
|
169
|
+
|
170
|
+
def on_pubcomp(&block)
|
171
|
+
@adapter.on_pubcomp(&block)
|
172
|
+
end
|
173
|
+
|
174
|
+
def on_message(&block)
|
175
|
+
@adapter.on_message(&block)
|
176
|
+
end
|
177
|
+
|
110
178
|
### The following attributes should exists in every MQTT third party librairy.
|
111
179
|
### They are necessary (or really usefull and common) for the establishement of the connection and/or the basic Mqtt actions.
|
112
180
|
### The setter directely change the third party client value when the getter remote the actual SharedClient instance's attribute value
|