mqttopia 0.1.20 → 0.1.25
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 +1 -1
- data/MQTT_SUBSCRIPTIONS.md +7 -13
- data/lib/mqttopia/client.rb +16 -11
- data/lib/mqttopia/logger.rb +18 -6
- data/lib/mqttopia/serializers/trip_metric.rb +12 -12
- data/lib/mqttopia/version.rb +1 -1
- data/lib/mqttopia.rb +25 -1
- data/lib/tasks/initialize.rake +2 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eaaf92a832470b3cfb5169cabdeda2af5538d9abd95a1bcbb15f46cde4a1432d
|
4
|
+
data.tar.gz: aee124311821053eaf5589e28fb6d0f630b48bec035f85d34be842237f7d5db3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61356c8227cb3704a37d7d597f7df890e37b84318254d68726c19918b31da213edb0a37a905c4b58031b05c3272d99cd1242c8af537ae1612ece6f331fd6e552
|
7
|
+
data.tar.gz: de265791bfcc71254701dc1af96a916d3ba950ac7a6954f8dd0911a6a84d71d2c38c961f4924129c12014c48a75fc6743d5fe93c02db6412760c382078ee277f
|
data/.rubocop.yml
CHANGED
data/MQTT_SUBSCRIPTIONS.md
CHANGED
@@ -17,7 +17,7 @@ This subscription handles trip metrics and serializes the data related to trips,
|
|
17
17
|
"l": 100,
|
18
18
|
"cs": "2"
|
19
19
|
},
|
20
|
-
"connt":
|
20
|
+
"connS":{"type":"Connected","connt":"MOBILE","subType":"LTE"},
|
21
21
|
"pws": -1,
|
22
22
|
"gps": -1,
|
23
23
|
"nlog": 585,
|
@@ -40,7 +40,11 @@ This subscription handles trip metrics and serializes the data related to trips,
|
|
40
40
|
trip_id: 123,
|
41
41
|
metrics: {
|
42
42
|
location_permission: "BACKGROUND_FINE",
|
43
|
-
|
43
|
+
connection: {
|
44
|
+
connection_type: "MOBILE",
|
45
|
+
connection_sub_type: "LTE",
|
46
|
+
connection_status: "Connected"
|
47
|
+
},
|
44
48
|
battery: {
|
45
49
|
battery_level: 100,
|
46
50
|
charging_status: "UNKNOWN",
|
@@ -84,16 +88,6 @@ This subscription handles trip metrics and serializes the data related to trips,
|
|
84
88
|
"-4": "DISABLED"
|
85
89
|
}
|
86
90
|
```
|
87
|
-
- **connection_type**
|
88
|
-
```json
|
89
|
-
{
|
90
|
-
"wf": "WIFI",
|
91
|
-
"cel": "CELLULAR",
|
92
|
-
"et": "ETHERNET",
|
93
|
-
"nc": "NO_CONNECTION",
|
94
|
-
"uk": "UNKNOWN"
|
95
|
-
}
|
96
|
-
```
|
97
91
|
- **charging_status**
|
98
92
|
```json
|
99
93
|
{
|
@@ -118,7 +112,7 @@ payload = {
|
|
118
112
|
"l": 100,
|
119
113
|
"cs": "2"
|
120
114
|
},
|
121
|
-
"connt":
|
115
|
+
"connS":{"type":"Connected","connt":"MOBILE","subType":"LTE"},
|
122
116
|
"pws": -1,
|
123
117
|
"gps": -1,
|
124
118
|
"nlog": 585,
|
data/lib/mqttopia/client.rb
CHANGED
@@ -23,15 +23,20 @@ module Mqttopia
|
|
23
23
|
ssl: Mqttopia.ssl
|
24
24
|
)
|
25
25
|
@mqtt_client = MQTT::Client.new(
|
26
|
+
version: Mqttopia.version,
|
26
27
|
port: port,
|
27
28
|
username: username,
|
28
29
|
password: password,
|
29
30
|
ssl: ssl,
|
30
|
-
keep_alive:
|
31
|
-
|
31
|
+
keep_alive: Mqttopia.keep_alive,
|
32
|
+
will_topic: Mqttopia.will_topic,
|
33
|
+
will_retain: Mqttopia.will_retain,
|
34
|
+
will_payload: Mqttopia.will_payload,
|
35
|
+
will_qos: Mqttopia.will_qos,
|
36
|
+
ack_timeout: Mqttopia.ack_timeout
|
32
37
|
)
|
33
38
|
@debugging = Mqttopia.debugging
|
34
|
-
@debugging_topic =
|
39
|
+
@debugging_topic = Mqttopia.debugging_topic
|
35
40
|
rescue Exception => e
|
36
41
|
log_error("initialize", e)
|
37
42
|
end
|
@@ -46,6 +51,10 @@ module Mqttopia
|
|
46
51
|
disconnect_and_log("connect", e)
|
47
52
|
end
|
48
53
|
|
54
|
+
def publish(topic, message = nil, qos = 0)
|
55
|
+
self.class.publish(topic, message, qos)
|
56
|
+
end
|
57
|
+
|
49
58
|
def self.publish(topic, message = nil, qos = 0)
|
50
59
|
client = new
|
51
60
|
client.connect
|
@@ -82,6 +91,8 @@ module Mqttopia
|
|
82
91
|
private
|
83
92
|
|
84
93
|
def active_host(hosts = Mqttopia.hosts, port = Mqttopia.port, timeout = 10)
|
94
|
+
return if hosts.nil? || hosts.empty?
|
95
|
+
|
85
96
|
hosts.each do |host|
|
86
97
|
Socket.tcp(host, port, connect_timeout: timeout) do |sock|
|
87
98
|
sock.close
|
@@ -93,17 +104,15 @@ module Mqttopia
|
|
93
104
|
nil # Return nil if no reachable host is found
|
94
105
|
end
|
95
106
|
|
96
|
-
def create_subscription_thread(
|
107
|
+
def create_subscription_thread(_qos)
|
97
108
|
Thread.new do
|
98
109
|
mqtt_client.get do |topic, message|
|
99
|
-
log_debug("Received RAW message on topic #{topic}: #{message} with QOS #{qos}")
|
100
|
-
|
101
110
|
response = safe_mqtt_response(topic, message)
|
102
111
|
|
103
112
|
if debugging && topic.exclude?(debugging_topic)
|
104
113
|
Mqttopia::Client.publish(debugging_topic,
|
105
114
|
{ 'topic_name': topic,
|
106
|
-
'mqttopia_response': response },
|
115
|
+
'mqttopia_response': response }, 0)
|
107
116
|
end
|
108
117
|
|
109
118
|
yield response if response
|
@@ -139,10 +148,6 @@ module Mqttopia
|
|
139
148
|
Mqttopia::Logger.error("\nMqttopia::Client -> #{method}: #{error_details(error)}\n")
|
140
149
|
end
|
141
150
|
|
142
|
-
def log_debug(message)
|
143
|
-
Mqttopia::Logger.debug("\n#{message}\n") if debugging
|
144
|
-
end
|
145
|
-
|
146
151
|
def disconnect_and_log(method, error)
|
147
152
|
disconnect
|
148
153
|
log_error(method, error)
|
data/lib/mqttopia/logger.rb
CHANGED
@@ -7,23 +7,35 @@ module Mqttopia
|
|
7
7
|
class << self
|
8
8
|
# Initialize the logger
|
9
9
|
def logger
|
10
|
-
@logger ||= Mqttopia.logger
|
10
|
+
@logger ||= Mqttopia.logger
|
11
|
+
end
|
12
|
+
|
13
|
+
def logger_level
|
14
|
+
@logger_level ||= Mqttopia.logger_level
|
11
15
|
end
|
12
16
|
|
13
17
|
# Allow users to set a custom logger (e.g., logging to a file)
|
14
|
-
attr_writer :logger
|
18
|
+
attr_writer :logger, :logger_level
|
15
19
|
|
16
20
|
# Simple wrapper for logging debug messages
|
17
|
-
|
21
|
+
def debug(msg)
|
22
|
+
logger.debug(msg) if %i[debug].include?(logger_level)
|
23
|
+
end
|
18
24
|
|
19
25
|
# Wrapper for logging info messages
|
20
|
-
|
26
|
+
def info(msg)
|
27
|
+
logger.info(msg) if %i[debug info].include?(logger_level)
|
28
|
+
end
|
21
29
|
|
22
30
|
# Wrapper for logging warn messages
|
23
|
-
|
31
|
+
def warn(msg)
|
32
|
+
logger.warn(msg) if %i[debug info warn].include?(logger_level)
|
33
|
+
end
|
24
34
|
|
25
35
|
# Wrapper for logging error messages
|
26
|
-
|
36
|
+
def error(msg)
|
37
|
+
logger.error(msg) if %i[debug info warn error].include?(logger_level)
|
38
|
+
end
|
27
39
|
end
|
28
40
|
end
|
29
41
|
end
|
@@ -5,7 +5,7 @@ module Mqttopia
|
|
5
5
|
module TripMetric
|
6
6
|
module_function
|
7
7
|
|
8
|
-
KEYS = %i[ts locPerms
|
8
|
+
KEYS = %i[ts locPerms connS batt battPwOp pws notf gps nlog loc].freeze
|
9
9
|
|
10
10
|
def serialize(body)
|
11
11
|
return unless valid_metric?(body[:metrics])
|
@@ -21,7 +21,7 @@ module Mqttopia
|
|
21
21
|
def serialize_metrics(metrics)
|
22
22
|
{
|
23
23
|
location_permission: location_permission_mapper(metrics[:locPerms]),
|
24
|
-
|
24
|
+
connection: serialize_connection(metrics[:connS]),
|
25
25
|
battery: serialize_battery(metrics[:batt], metrics[:battPwOp], metrics[:pws]),
|
26
26
|
notification_permission: metrics[:notf]&.zero?,
|
27
27
|
gps: metrics[:gps]&.zero?,
|
@@ -30,6 +30,16 @@ module Mqttopia
|
|
30
30
|
}
|
31
31
|
end
|
32
32
|
|
33
|
+
def serialize_connection(connt_s)
|
34
|
+
return unless connt_s
|
35
|
+
|
36
|
+
{
|
37
|
+
connection_type: connt_s[:type],
|
38
|
+
connection_sub_type: connt_s[:subType],
|
39
|
+
connection_status: connt_s[:connt]
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
33
43
|
def serialize_battery(battery, optimization_status, power_saving_mode)
|
34
44
|
return unless battery
|
35
45
|
|
@@ -63,16 +73,6 @@ module Mqttopia
|
|
63
73
|
}.fetch(permission&.to_s) { "UNKNOWN" }
|
64
74
|
end
|
65
75
|
|
66
|
-
def connection_type_mapper(connection_type)
|
67
|
-
{
|
68
|
-
"wf" => "WIFI",
|
69
|
-
"cel" => "CELLULAR",
|
70
|
-
"et" => "ETHERNET",
|
71
|
-
"nc" => "NO_CONNECTION",
|
72
|
-
"uk" => "UNKNOWN"
|
73
|
-
}.fetch(connection_type) { "UNKNOWN" }
|
74
|
-
end
|
75
|
-
|
76
76
|
def charging_status_mapper(charging_status)
|
77
77
|
{
|
78
78
|
"0" => "UNKNOWN",
|
data/lib/mqttopia/version.rb
CHANGED
data/lib/mqttopia.rb
CHANGED
@@ -13,12 +13,36 @@ Dir[File.expand_path("tasks/**/*.rake", __dir__)].each { |task| load task }
|
|
13
13
|
module Mqttopia
|
14
14
|
class Error < StandardError; end
|
15
15
|
|
16
|
-
mattr_accessor :
|
16
|
+
mattr_accessor :version,
|
17
|
+
:hosts,
|
18
|
+
:port,
|
19
|
+
:username,
|
20
|
+
:password,
|
21
|
+
:ssl,
|
22
|
+
:logger,
|
23
|
+
:logger_level,
|
24
|
+
:keep_alive,
|
25
|
+
:will_topic,
|
26
|
+
:will_retain,
|
27
|
+
:will_payload,
|
28
|
+
:will_qos,
|
29
|
+
:ack_timeout,
|
30
|
+
:debugging,
|
31
|
+
:debugging_topic
|
17
32
|
|
33
|
+
@@version = "3.1.1"
|
18
34
|
@@port = 1883
|
19
35
|
@@ssl = true
|
36
|
+
@@will_topic = "mqttopia/will"
|
37
|
+
@@will_retain = true
|
38
|
+
@@will_payload = "offline"
|
39
|
+
@@will_qos = 0
|
40
|
+
@@keep_alive = 20
|
41
|
+
@@ack_timeout = 10
|
20
42
|
@@logger = ::Logger.new($stdout)
|
43
|
+
@@logger_level = :info
|
21
44
|
@@debugging = false
|
45
|
+
@@debugging_topic = "mqttopia/test/debugging"
|
22
46
|
|
23
47
|
def self.configure
|
24
48
|
yield self
|
data/lib/tasks/initialize.rake
CHANGED
@@ -34,11 +34,7 @@ namespace :mqttopia do
|
|
34
34
|
# $client.publish('test/topic', 'Hello, MQTT')
|
35
35
|
RUBY
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
else
|
40
|
-
File.write(file_name, content)
|
41
|
-
puts "Created: #{file_name}"
|
42
|
-
end
|
37
|
+
|
38
|
+
File.write(file_name, content) unless File.exist?(file_name)
|
43
39
|
end
|
44
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mqttopia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.25
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Illa Tech
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-01-
|
11
|
+
date: 2025-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|