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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9c75dac8a27f93fc8756bb64868aba970a57a5e99267e749820acda6009745f
4
- data.tar.gz: 0ef832f1ca9e28be3226788205339ac92d228ee1ec013d2679d09096df63bfa7
3
+ metadata.gz: 9168c78afc2d36166c65fb36af8a62ab5f6b3b0494bc208a34666b3be3d81af9
4
+ data.tar.gz: 10f08ca4e45df407c3474299f9c5fec65b0e7415f5068d2b342fce56102a21dd
5
5
  SHA512:
6
- metadata.gz: e00b819611be0ddc22ed5d93e378b1ed4f7546d5b531ab0a57b5cca49b83ecd5832055017d27c214865e1513ef3939960558b964915c182357299d2400656044
7
- data.tar.gz: 57d53b71b510c4455c0064fce28ac11386fe724f5c2f9435d0725d6a5f43fb186bb26e4912aeab7dd2d4b42638325892140f260c97446a3fb746c61c718b80e0
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
- Layout/LineLength:
20
+ Gemspec/RequireMFA:
10
21
  Enabled: false
11
22
 
12
- Metrics/BlockLength:
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/StringLiteralsInInterpolation:
29
- Enabled: true
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 macklabsinc/mqtt_api_client
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 macklabsinc/mqtt_api_client
15
+ $ gem install mqtt_api_client
16
16
 
17
17
  ## Usage
18
18
 
19
- TODO: Write usage instructions here
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 GitHub at https://github.com/[USERNAME]/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).
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
- 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.0"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  require "amazing_print"
4
4
  require "mqtt"
5
- require_relative "mqtt_api_client/version"
6
5
  require_relative "mqtt_api_client/command"
6
+ require_relative "mqtt_api_client/version"
7
7
 
8
8
  module MqttApiClient
9
9
  class Error < StandardError; end
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.0
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-21 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
@@ -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
- - sig/mqtt_api_client.rbs
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.10
70
+ rubygems_version: 3.4.6
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: MQTT API Client
@@ -1,4 +0,0 @@
1
- module MqttApiClient
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end