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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b088d9d3aeb35ebe2ccd4859de7b6ec9a662862ff4a3967f3b4024c6a1c19d7
4
- data.tar.gz: f0715c7f69236b628be8ea7b80ffd82e91810dedca081cbb13f1cc49b0a92c47
3
+ metadata.gz: 9168c78afc2d36166c65fb36af8a62ab5f6b3b0494bc208a34666b3be3d81af9
4
+ data.tar.gz: 10f08ca4e45df407c3474299f9c5fec65b0e7415f5068d2b342fce56102a21dd
5
5
  SHA512:
6
- metadata.gz: db1bde54e70d4dd57593f0c66b0a51be1a31bebc86d2fb3771898dd97302428d7c3a7a356bafb0e0767c331990c90f61c475f9a61ded3993dc474ed79c4b8c55
7
- data.tar.gz: fade692f1b10693adf0efd4e45a2e64c4684fae77496b089eeadefa6a5b00a4f1a75ef7d900823bc4b644d1b7163359e74befbb6f89eca5a5e3f5e50995297c0
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
- command = MqttApiClient::Command.new(certificate, private_key, "localhost")
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
- attr_reader :mqtt_client, :crt, :key, :host, :logger
7
+ TIMEOUT_SECONDS = 2
8
+
9
+ attr_reader :mqtt_client, :device_identifier
9
10
 
10
11
  #
11
- # @param [File|String], crt
12
- # @param [File|String], key
13
- def initialize(crt, key, host)
14
- @crt = if File.file? crt
15
- File.read crt
16
- else
17
- crt
18
- end
19
- @key = if File.file? key
20
- File.read key
21
- else
22
- key
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
- # rubocop:disable Style/EmptyElse
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
- mqtt_client.get do |topic, message|
41
- mqtt_client.unsubscribe(subscribed_topic) if topic == subscribed_topic
42
-
43
- if topic == subscribed_topic
44
- logger.info "New file_list: #{message}"
45
-
46
- # do some logic with a message, probably update device file_list in the DB ?
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
- return message
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(device_identifier)
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
- mqtt_client.get do |topic, message|
64
- mqtt_client.unsubscribe(subscribed_topic) if topic == subscribed_topic
65
-
66
- if topic == subscribed_topic
67
- logger.info "New health_status: #{message}"
68
-
69
- # do some logic with a message, probably update device health_status in the DB ?
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
- return message
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 connect
84
- @mqtt_client = MQTT::Client.new
85
- @mqtt_client.host = host
86
- @mqtt_client.port = 8883
87
- @mqtt_client.ssl = true
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
- @mqtt_client.cert = crt
90
- @mqtt_client.key = key
76
+ def logger
77
+ @_logger ||= Logger.new("log/command.log", "daily")
78
+ end
91
79
 
92
- @mqtt_client.connect
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MqttApiClient
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "amazing_print"
4
- require "dotenv/load"
5
4
  require "mqtt"
6
5
  require_relative "mqtt_api_client/command"
7
6
  require_relative "mqtt_api_client/version"
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.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-05-22 00:00:00.000000000 Z
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.10
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
@@ -1,4 +0,0 @@
1
- AWS_CERTIFICATE=
2
- AWS_PRIVATE_KEY=
3
- LOCAL_CERTIFICATE=
4
- LOCAL_PRIVATE_KEY=