mqtt_api_client 0.1.0 → 0.1.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 +4 -4
- data/.rubocop.yml +46 -5
- data/README.md +32 -4
- data/lib/mqtt_api_client/command.rb +57 -66
- data/lib/mqtt_api_client/version.rb +1 -1
- data/lib/mqtt_api_client.rb +1 -1
- data/log/.keep +0 -0
- metadata +4 -4
- data/sig/mqtt_api_client.rbs +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9168c78afc2d36166c65fb36af8a62ab5f6b3b0494bc208a34666b3be3d81af9
|
4
|
+
data.tar.gz: 10f08ca4e45df407c3474299f9c5fec65b0e7415f5068d2b342fce56102a21dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '080407873f5678539f5cb400afaa980eea469e22bbd827760717437e3f4bf40da45da36327a00f95dbf9dacaf1a94f5d1ca83414ecb9c287ff8af20a43255d26'
|
7
|
+
data.tar.gz: cd5588a020ca71a9b29c31f843f8eab66a2be4ebcb39cb887b0f70e2069d2619faaa35f5c5a4e942205f19d25901ae468fb676c14cd8890dd56a7f55cf3da7a9
|
data/.rubocop.yml
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
# The behavior of RuboCop can be controlled via the .rubocop.yml
|
2
|
+
# configuration file. It makes it possible to enable/disable
|
3
|
+
# certain cops (checks) and to alter their behavior if they accept
|
4
|
+
# any parameters. The file can be placed either in your home
|
5
|
+
# directory or in some project directory.
|
6
|
+
#
|
7
|
+
# RuboCop will start looking for the configuration file in the directory
|
8
|
+
# where the inspected file is and continue its way up to the root directory.
|
9
|
+
#
|
10
|
+
# See https://docs.rubocop.org/rubocop/configuration
|
11
|
+
|
1
12
|
require:
|
2
13
|
- rubocop-rake
|
3
14
|
- rubocop-rspec
|
@@ -6,25 +17,55 @@ AllCops:
|
|
6
17
|
TargetRubyVersion: 3.1
|
7
18
|
NewCops: enable
|
8
19
|
|
9
|
-
|
20
|
+
Gemspec/RequireMFA:
|
10
21
|
Enabled: false
|
11
22
|
|
12
|
-
|
23
|
+
Layout/CommentIndentation:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Layout/LineLength:
|
13
27
|
Enabled: false
|
14
28
|
|
15
29
|
Metrics/MethodLength:
|
16
30
|
Enabled: false
|
17
31
|
|
32
|
+
Naming/MemoizedInstanceVariableName:
|
33
|
+
Enabled: false
|
34
|
+
|
18
35
|
RSpec/ExampleLength:
|
19
36
|
Enabled: false
|
20
37
|
|
38
|
+
RSpec/MessageSpies:
|
39
|
+
Enabled: false
|
40
|
+
|
21
41
|
RSpec/MultipleExpectations:
|
22
42
|
Enabled: false
|
23
43
|
|
44
|
+
RSpec/MultipleMemoizedHelpers:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
RSpec/ExpectInHook:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Style/Documentation:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
Style/EmptyElse:
|
54
|
+
Exclude:
|
55
|
+
- lib/mqtt_api_client/command.rb
|
56
|
+
|
57
|
+
Style/GlobalVars:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
Style/Lambda:
|
61
|
+
Enabled: false
|
62
|
+
|
63
|
+
Style/SingleLineMethods:
|
64
|
+
Enabled: false
|
65
|
+
|
24
66
|
Style/StringLiterals:
|
25
67
|
Enabled: true
|
26
68
|
EnforcedStyle: double_quotes
|
27
69
|
|
28
|
-
Style/
|
29
|
-
Enabled:
|
30
|
-
EnforcedStyle: double_quotes
|
70
|
+
Style/YodaCondition:
|
71
|
+
Enabled: false
|
data/README.md
CHANGED
@@ -8,15 +8,43 @@ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_O
|
|
8
8
|
|
9
9
|
Install the gem and add to the application's Gemfile by executing:
|
10
10
|
|
11
|
-
$ bundle add
|
11
|
+
$ bundle add mqtt_api_client
|
12
12
|
|
13
13
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
14
|
|
15
|
-
$ gem install
|
15
|
+
$ gem install mqtt_api_client
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
```ruby
|
20
|
+
#
|
21
|
+
# connect to your MQTT broker (AWS IoT, Mosquitto, and so on..)
|
22
|
+
# @param [String], crt
|
23
|
+
# @param [String], key
|
24
|
+
# @param [String], host
|
25
|
+
mqtt_client = MQTT::Client.new.tap do |client|
|
26
|
+
# connection info
|
27
|
+
client.host = "my.host"
|
28
|
+
client.port = 8883
|
29
|
+
client.ssl = true
|
30
|
+
|
31
|
+
# certificates
|
32
|
+
client.cert = my_cert
|
33
|
+
client.key = my_private_key
|
34
|
+
|
35
|
+
client.connect
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# @param [MQTT::Client], mqtt_client
|
40
|
+
# @param [String], device_identifier
|
41
|
+
command = MqttApiClient::Command.new(mqtt_client, "D0045F")
|
42
|
+
|
43
|
+
command.health_status
|
44
|
+
command.file_list
|
45
|
+
|
46
|
+
command.disconnect
|
47
|
+
```
|
20
48
|
|
21
49
|
## Development
|
22
50
|
|
@@ -26,7 +54,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
26
54
|
|
27
55
|
## Contributing
|
28
56
|
|
29
|
-
Bug reports and pull requests are welcome on
|
57
|
+
Bug reports and pull requests are welcome on Bitbucket at https://bitbucket.org/macklabsinc/mqtt_api_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://bitbucket.com/macklabsinc/mqtt_api_client/blob/master/CODE_OF_CONDUCT.md).
|
30
58
|
|
31
59
|
## License
|
32
60
|
|
@@ -3,93 +3,84 @@
|
|
3
3
|
require "logger"
|
4
4
|
|
5
5
|
module MqttApiClient
|
6
|
-
# commands implemented
|
7
6
|
class Command
|
8
|
-
|
7
|
+
TIMEOUT_SECONDS = 2
|
8
|
+
|
9
|
+
attr_reader :mqtt_client, :device_identifier
|
9
10
|
|
10
11
|
#
|
11
|
-
# @param [
|
12
|
-
# @param [
|
13
|
-
def initialize(
|
14
|
-
@
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
@host = host
|
26
|
-
|
27
|
-
@logger = Logger.new("log/command.log", "daily")
|
28
|
-
@logger.info "Command logger initialized at #{Time.now}"
|
29
|
-
|
30
|
-
connect
|
12
|
+
# @param mqtt_client [MQTT::Client]
|
13
|
+
# @param device_identifier [String]
|
14
|
+
def initialize(mqtt_client, device_identifier)
|
15
|
+
@mqtt_client = mqtt_client
|
16
|
+
@device_identifier = device_identifier
|
17
|
+
|
18
|
+
logger.info "Command logger initialized at #{Time.now}"
|
19
|
+
|
20
|
+
subscribe_to_all(
|
21
|
+
"device/#{device_identifier}/file_list",
|
22
|
+
"device/#{device_identifier}/health_status"
|
23
|
+
)
|
31
24
|
end
|
32
25
|
|
33
|
-
|
34
|
-
def file_list(device_identifier)
|
35
|
-
subscribed_topic = "device/#{device_identifier}/file_list"
|
36
|
-
|
37
|
-
mqtt_client.subscribe(subscribed_topic)
|
26
|
+
def file_list
|
38
27
|
mqtt_client.publish("device/#{device_identifier}/command", "file_list")
|
39
28
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
29
|
+
begin
|
30
|
+
process_get(
|
31
|
+
"device/#{device_identifier}/file_list",
|
32
|
+
"New file_list"
|
33
|
+
)
|
34
|
+
rescue Timeout::Error => e
|
35
|
+
logger.error "file_list timeout: #{e}"
|
47
36
|
|
48
|
-
|
49
|
-
else
|
50
|
-
# I don't want to raise exception here because we are
|
51
|
-
# getting messages from the whole mqtt queue, one by one,
|
52
|
-
# and the next message could be that what we need
|
53
|
-
end
|
37
|
+
Timeout::Error.new(e)
|
54
38
|
end
|
55
39
|
end
|
56
40
|
|
57
|
-
def health_status
|
58
|
-
subscribed_topic = "device/#{device_identifier}/health_status"
|
59
|
-
mqtt_client.subscribe(subscribed_topic)
|
60
|
-
|
41
|
+
def health_status
|
61
42
|
mqtt_client.publish("device/#{device_identifier}/command", "health_status")
|
62
43
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
44
|
+
begin
|
45
|
+
process_get(
|
46
|
+
"device/#{device_identifier}/health_status",
|
47
|
+
"New health_status"
|
48
|
+
)
|
49
|
+
rescue Timeout::Error => e
|
50
|
+
logger.error "health_status timeout: #{e}"
|
70
51
|
|
71
|
-
|
72
|
-
else
|
73
|
-
# I don't want to raise exception here because we are
|
74
|
-
# getting messages from the whole mqtt queue, one by one,
|
75
|
-
# and the next message could be that what we need
|
76
|
-
end
|
77
|
-
# rubocop:enable Style/EmptyElse
|
52
|
+
Timeout::Error.new(e)
|
78
53
|
end
|
79
54
|
end
|
80
55
|
|
81
56
|
private
|
82
57
|
|
83
|
-
def
|
84
|
-
@
|
85
|
-
|
86
|
-
|
87
|
-
|
58
|
+
def process_get(subscribed_topic, logger_key)
|
59
|
+
@_process_get ||= Timeout.timeout(TIMEOUT_SECONDS) do
|
60
|
+
mqtt_client.get do |topic, message|
|
61
|
+
if topic == subscribed_topic
|
62
|
+
logger.info "#{logger_key}: #{message}"
|
63
|
+
|
64
|
+
# do any logic with a message there, if you need to
|
65
|
+
|
66
|
+
return message
|
67
|
+
else
|
68
|
+
# I don't want to raise exception here because we are
|
69
|
+
# getting messages from the whole mqtt queue, one by one,
|
70
|
+
# and the next message could be that what we need
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
88
75
|
|
89
|
-
|
90
|
-
@
|
76
|
+
def logger
|
77
|
+
@_logger ||= Logger.new("log/command.log", "daily")
|
78
|
+
end
|
91
79
|
|
92
|
-
|
80
|
+
#
|
81
|
+
# @param topics [Array<String]
|
82
|
+
def subscribe_to_all(*topics)
|
83
|
+
topics.each { |topic| mqtt_client.subscribe(topic) }
|
93
84
|
end
|
94
85
|
end
|
95
86
|
end
|
data/lib/mqtt_api_client.rb
CHANGED
data/log/.keep
ADDED
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mqtt_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladyslav Sumskyi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mqtt
|
@@ -42,7 +42,7 @@ files:
|
|
42
42
|
- lib/mqtt_api_client.rb
|
43
43
|
- lib/mqtt_api_client/command.rb
|
44
44
|
- lib/mqtt_api_client/version.rb
|
45
|
-
-
|
45
|
+
- log/.keep
|
46
46
|
homepage: https://bitbucket.org/macklabsinc/mqtt_api_client/src/master/
|
47
47
|
licenses:
|
48
48
|
- MIT
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
|
-
rubygems_version: 3.4.
|
70
|
+
rubygems_version: 3.4.6
|
71
71
|
signing_key:
|
72
72
|
specification_version: 4
|
73
73
|
summary: MQTT API Client
|
data/sig/mqtt_api_client.rbs
DELETED