mqttopia 0.2.0 → 0.2.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: e187ae43b2ef47585aa3e90f989bf95e4b6a5a2ce5435050b6197b1de69d5867
4
- data.tar.gz: f28d85e56435d9a70eff252b638c98d1506de4ca58678a09de79cf01bf6ed9e4
3
+ metadata.gz: 1999a274eff111b5027805925f4c9053adfe992c6ecff4a4f45212d56477978f
4
+ data.tar.gz: 115798acdb489d407fafc0c53e9dc46624c422555c061b424a066d82d51c06c4
5
5
  SHA512:
6
- metadata.gz: 38e1e64174271e7df513c72fd8f2475cb35de740407e93c5ecadb6552ccd435d9e1c9a6c79436b1b5b9d20525180c6138abfb2584b0e46334e3ac6a2fdecbdd4
7
- data.tar.gz: 3052bd4fe4f365a2bc4e58894ef043df13f2ef43af3eb33ab110f72a4f1db2243bbd670eb9cafeafe2d658891e3c6076b0c98d2fd941af06cfaaf4ac4877ff52
6
+ metadata.gz: 8a2e463f81874aafb17370878bf95290f1a0ccd67a712814687820435f305ada601afa07b5b1431db03988effd8afb2df4b4ec31347b85865bc3913fc23465b1
7
+ data.tar.gz: 66bbeca571ae0dfdb159cea9c1c31767555a3ee52c5de8ac2ec73c5ed5213342e85edf68eed4230ff55a1ad85a3edf79c573c1cc4f68f1995907200850b9b4c7
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mqttopia
4
+ module Serializers
5
+ module LiveLocation
6
+ module_function
7
+
8
+ KEYS = %i[tid batt loc].freeze
9
+
10
+ def serialize(body)
11
+ return unless valid_live_location?(body[:live_location])
12
+
13
+ {
14
+ trip_id: body[:trip_id],
15
+ live_location: serialize_live_location(
16
+ body.dig(:live_location, :loc)&.merge!(batt: body.dig(:live_location, :batt))
17
+ ),
18
+ logged_at: body.dig(:live_location, :loc, :ts)&.to_s,
19
+ received_at: current_timestamp
20
+ }
21
+ end
22
+
23
+ def serialize_live_location(live_location)
24
+ {
25
+ latitude: live_location[:lat],
26
+ longitude: live_location[:lon],
27
+ accuracy: live_location[:acc],
28
+ altitude: live_location[:alt],
29
+ speed: live_location[:spd],
30
+ battery: live_location[:batt]
31
+ }
32
+ end
33
+
34
+ def valid_live_location?(live_location)
35
+ live_location.is_a?(Hash) && KEYS.all? { |key| live_location.key?(key) }
36
+ end
37
+
38
+ def current_timestamp
39
+ DateTime.now.strftime("%Q")
40
+ end
41
+ end
42
+ end
43
+ end
@@ -24,6 +24,7 @@ module Mqttopia
24
24
  longitude: point[:lon],
25
25
  accuracy: point[:acc],
26
26
  altitude: point[:alt],
27
+ action_type: action_type_mapper(point[:act]),
27
28
  current_trip_point_id: point[:ctp_id]
28
29
  }
29
30
  end
@@ -35,6 +36,13 @@ module Mqttopia
35
36
  def current_timestamp
36
37
  DateTime.now.strftime("%Q")
37
38
  end
39
+
40
+ def action_type_mapper(action_type)
41
+ {
42
+ "0" => "arrival",
43
+ "1" => "departure"
44
+ }.fetch(action_type&.to_s) { nil }
45
+ end
38
46
  end
39
47
  end
40
48
  end
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "services/trip_metrics"
4
- require_relative "services/test_debug"
5
- require_relative "services/trip_points"
3
+ # require_relative "services/trip_metrics"
4
+ # require_relative "services/test_debug"
5
+ # require_relative "services/trip_points"
6
+ # require_relative "services/live_locations"
7
+ Dir[File.join(__dir__, "services", "*.rb")].sort.each { |file| require_relative file }
8
+
6
9
  require_relative "../topics/services"
7
10
 
8
11
  module Mqttopia
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_model"
3
4
  require_relative "../../helpers/data_extractor"
4
5
 
5
6
  module Mqttopia
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../serializers/live_location"
4
+ require_relative "./base"
5
+
6
+ module Mqttopia
7
+ module Subscriptions
8
+ module Services
9
+ class LiveLocations < Mqttopia::Subscriptions::Services::Base
10
+ class LiveLocationsInvalidError < ArgumentError
11
+ end
12
+
13
+ attr_reader :trip_id, :live_location, :options, :user_id
14
+
15
+ validates :trip_id, presence: true, numericality: { only_integer: true, greater_than: 0 }
16
+ validates :live_location, presence: true
17
+ # validate :live_location_json_format
18
+
19
+ def initialize(trip_id:, live_location:, options: {})
20
+ @trip_id = trip_id
21
+ @live_location = live_location
22
+ @options = options
23
+ @user_id = options[:user_id]
24
+ end
25
+
26
+ def self.call(topic, payload, options = {})
27
+ trip_id = payload[:tid]
28
+ options[:user_id] = topic.include?("user/") && topic.match(%r{user/[^/]+}) && extract_user_id(topic)
29
+
30
+ new(trip_id: trip_id, live_location: payload, options: options).process
31
+ end
32
+
33
+ def process
34
+ raise LiveLocationsInvalidError, errors.full_messages.to_sentence unless valid?
35
+
36
+ Mqttopia::Serializers::LiveLocation.serialize({
37
+ trip_id: trip_id,
38
+ live_location: live_location
39
+ })
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_model"
4
3
  require_relative "../../serializers/test_debug"
5
4
  require_relative "./base"
6
5
 
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_model"
4
3
  require_relative "../../serializers/trip_metric"
5
4
  require_relative "./base"
6
5
 
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_model"
4
3
  require_relative "../../serializers/trip_point"
5
4
  require_relative "./base"
6
5
 
@@ -16,6 +16,12 @@ module Mqttopia
16
16
  serializer: "Mqttopia::Serializers::TripPoint",
17
17
  regex: %r{\Ailla/trips/(?<trip_id>\d+)/driver_trip_point/send(?:/user/(?<user_id>\d+))?\z}
18
18
  },
19
+ live_location: {
20
+ topic_name: "illa/fleet/location/vehicle/:trip_id/user/:user_id",
21
+ service: "Mqttopia::Subscriptions::Services::LiveLocations",
22
+ serializer: "Mqttopia::Serializers::LiveLocation",
23
+ regex: %r{\Ailla/fleet/location/vehicle/(?<trip_id>\d+)(?:/user/(?<user_id>\d+))?\z}
24
+ },
19
25
  test_debug: {
20
26
  topic_name: "test",
21
27
  service: "Mqttopia::Subscriptions::Services::TestDebug",
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mqttopia
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  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.2.0
4
+ version: 0.2.2
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-02-11 00:00:00.000000000 Z
11
+ date: 2025-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -184,11 +184,13 @@ files:
184
184
  - lib/mqttopia/client.rb
185
185
  - lib/mqttopia/helpers/data_extractor.rb
186
186
  - lib/mqttopia/logger.rb
187
+ - lib/mqttopia/serializers/live_location.rb
187
188
  - lib/mqttopia/serializers/test_debug.rb
188
189
  - lib/mqttopia/serializers/trip_metric.rb
189
190
  - lib/mqttopia/serializers/trip_point.rb
190
191
  - lib/mqttopia/subscriptions/redirect.rb
191
192
  - lib/mqttopia/subscriptions/services/base.rb
193
+ - lib/mqttopia/subscriptions/services/live_locations.rb
192
194
  - lib/mqttopia/subscriptions/services/test_debug.rb
193
195
  - lib/mqttopia/subscriptions/services/trip_metrics.rb
194
196
  - lib/mqttopia/subscriptions/services/trip_points.rb