mqtt_api_client 0.1.1 → 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 +71 -0
- data/README.md +24 -1
- data/lib/mqtt_api_client/command.rb +57 -66
- data/lib/mqtt_api_client/version.rb +1 -1
- data/lib/mqtt_api_client.rb +0 -1
- data/log/.keep +0 -0
- metadata +5 -4
- data/.env.example +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
ADDED
@@ -0,0 +1,71 @@
|
|
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
|
+
|
12
|
+
require:
|
13
|
+
- rubocop-rake
|
14
|
+
- rubocop-rspec
|
15
|
+
|
16
|
+
AllCops:
|
17
|
+
TargetRubyVersion: 3.1
|
18
|
+
NewCops: enable
|
19
|
+
|
20
|
+
Gemspec/RequireMFA:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Layout/CommentIndentation:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Layout/LineLength:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Metrics/MethodLength:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Naming/MemoizedInstanceVariableName:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
RSpec/ExampleLength:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
RSpec/MessageSpies:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
RSpec/MultipleExpectations:
|
42
|
+
Enabled: false
|
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
|
+
|
66
|
+
Style/StringLiterals:
|
67
|
+
Enabled: true
|
68
|
+
EnforcedStyle: double_quotes
|
69
|
+
|
70
|
+
Style/YodaCondition:
|
71
|
+
Enabled: false
|
data/README.md
CHANGED
@@ -17,10 +17,33 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
17
17
|
## Usage
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
|
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")
|
21
42
|
|
22
43
|
command.health_status
|
23
44
|
command.file_list
|
45
|
+
|
46
|
+
command.disconnect
|
24
47
|
```
|
25
48
|
|
26
49
|
## Development
|
@@ -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
|
@@ -31,8 +31,8 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
-
- ".env.example"
|
35
34
|
- ".rspec"
|
35
|
+
- ".rubocop.yml"
|
36
36
|
- CHANGELOG.md
|
37
37
|
- CODE_OF_CONDUCT.md
|
38
38
|
- LICENSE.txt
|
@@ -42,6 +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
|
+
- log/.keep
|
45
46
|
homepage: https://bitbucket.org/macklabsinc/mqtt_api_client/src/master/
|
46
47
|
licenses:
|
47
48
|
- MIT
|
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
67
|
- !ruby/object:Gem::Version
|
67
68
|
version: '0'
|
68
69
|
requirements: []
|
69
|
-
rubygems_version: 3.4.
|
70
|
+
rubygems_version: 3.4.6
|
70
71
|
signing_key:
|
71
72
|
specification_version: 4
|
72
73
|
summary: MQTT API Client
|
data/.env.example
DELETED