mqtt_api_client 0.1.2 → 0.2.1

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: 9168c78afc2d36166c65fb36af8a62ab5f6b3b0494bc208a34666b3be3d81af9
4
- data.tar.gz: 10f08ca4e45df407c3474299f9c5fec65b0e7415f5068d2b342fce56102a21dd
3
+ metadata.gz: a240564b318c55966a38d19bf82e3889f42913e95d6d0b55ca4029ef3e29a38e
4
+ data.tar.gz: a1882e0fb51e34be57b2cf9ae4daa6c2d80a09eb8a6a198cf3e384442132f043
5
5
  SHA512:
6
- metadata.gz: '080407873f5678539f5cb400afaa980eea469e22bbd827760717437e3f4bf40da45da36327a00f95dbf9dacaf1a94f5d1ca83414ecb9c287ff8af20a43255d26'
7
- data.tar.gz: cd5588a020ca71a9b29c31f843f8eab66a2be4ebcb39cb887b0f70e2069d2619faaa35f5c5a4e942205f19d25901ae468fb676c14cd8890dd56a7f55cf3da7a9
6
+ metadata.gz: 828d9958263be605c6c1ad5f4a10b7aeeac4508e5131ef6bb6399113786e8f8fcf6ed0bb1bda7ecf4f3cffe9757cdc5880d602f10a46b35efec610115c774e2a
7
+ data.tar.gz: 2ae4cd4206a80f4740b562bb80535584c97dbece54c064da133a5b0660a8f07605a576e62a13e5ada794b5d31a053193616c1b5c3fc7422b828f9eab1d245bd9
@@ -0,0 +1,8 @@
1
+ DEVICE_IDENTIFIER=
2
+ MQTT_HOST=
3
+ MQTT_PORT=8883
4
+
5
+ DOTENV_LINEBREAK_MODE=legacy
6
+
7
+ MQTT_CERT="-----BEGIN CERTIFICATE-----\nxxxx\n-----END CERTIFICATE-----\n"
8
+ MQTT_KEY="-----BEGIN RSA PRIVATE KEY-----\nxxxx\n-----END RSA PRIVATE KEY-----\n"
@@ -1,21 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "logger"
4
-
4
+ require "fileutils"
5
5
  module MqttApiClient
6
- class Command
6
+ class Device
7
7
  TIMEOUT_SECONDS = 2
8
8
 
9
- attr_reader :mqtt_client, :device_identifier
9
+ attr_reader :mqtt_client, :device_identifier, :logger
10
10
 
11
11
  #
12
12
  # @param mqtt_client [MQTT::Client]
13
13
  # @param device_identifier [String]
14
- def initialize(mqtt_client, device_identifier)
14
+ def initialize(mqtt_client:, device_identifier:, logger: default_logger(device_identifier))
15
15
  @mqtt_client = mqtt_client
16
16
  @device_identifier = device_identifier
17
+ @logger = logger
17
18
 
18
- logger.info "Command logger initialized at #{Time.now}"
19
+ logger&.info "Command logger initialized at #{Time.now}"
19
20
 
20
21
  subscribe_to_all(
21
22
  "device/#{device_identifier}/file_list",
@@ -29,10 +30,10 @@ module MqttApiClient
29
30
  begin
30
31
  process_get(
31
32
  "device/#{device_identifier}/file_list",
32
- "New file_list"
33
+ "file_list"
33
34
  )
34
35
  rescue Timeout::Error => e
35
- logger.error "file_list timeout: #{e}"
36
+ logger&.error "file_list timeout: #{e}"
36
37
 
37
38
  Timeout::Error.new(e)
38
39
  end
@@ -44,37 +45,39 @@ module MqttApiClient
44
45
  begin
45
46
  process_get(
46
47
  "device/#{device_identifier}/health_status",
47
- "New health_status"
48
+ "health_status"
48
49
  )
49
50
  rescue Timeout::Error => e
50
- logger.error "health_status timeout: #{e}"
51
+ logger&.error "health_status timeout: #{e}"
51
52
 
52
53
  Timeout::Error.new(e)
53
54
  end
54
55
  end
55
56
 
57
+ def disconnect
58
+ mqtt_client.disconnect
59
+ end
60
+
56
61
  private
57
62
 
58
63
  def process_get(subscribed_topic, logger_key)
59
64
  @_process_get ||= Timeout.timeout(TIMEOUT_SECONDS) do
60
65
  mqtt_client.get do |topic, message|
61
66
  if topic == subscribed_topic
62
- logger.info "#{logger_key}: #{message}"
67
+ logger&.info "#{logger_key}: #{message}"
63
68
 
64
69
  # do any logic with a message there, if you need to
65
70
 
66
71
  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
72
  end
72
73
  end
73
74
  end
74
75
  end
75
76
 
76
- def logger
77
- @_logger ||= Logger.new("log/command.log", "daily")
77
+ def default_logger(device_identifier)
78
+ FileUtils.mkdir_p("log")
79
+
80
+ Logger.new("log/device_#{device_identifier}.log", "daily")
78
81
  end
79
82
 
80
83
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MqttApiClient
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "amazing_print"
4
3
  require "mqtt"
5
- require_relative "mqtt_api_client/command"
4
+ require_relative "mqtt_api_client/device"
6
5
  require_relative "mqtt_api_client/version"
7
6
 
8
7
  module MqttApiClient
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.2
4
+ version: 0.2.1
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-06-06 00:00:00.000000000 Z
11
+ date: 2024-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mqtt
@@ -31,6 +31,7 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - ".env.local.example"
34
35
  - ".rspec"
35
36
  - ".rubocop.yml"
36
37
  - CHANGELOG.md
@@ -40,9 +41,8 @@ files:
40
41
  - Rakefile
41
42
  - bitbucket-pipelines.yml
42
43
  - lib/mqtt_api_client.rb
43
- - lib/mqtt_api_client/command.rb
44
+ - lib/mqtt_api_client/device.rb
44
45
  - lib/mqtt_api_client/version.rb
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.6
70
+ rubygems_version: 3.4.19
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: MQTT API Client
data/log/.keep DELETED
File without changes