dji_mqtt_connect 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 824a4b8942fc65705e25588a32df620e49602573b1eb14ed6f1b2c34c98023cd
4
- data.tar.gz: 7f9dc5c06e40c04862baed3c41b8342da77a9fbedb083ab7432b9173180ebce1
3
+ metadata.gz: 3a9ba8caf2ea370ed33848df512fbebcc63dfd4b63dd07947a1a653f56fdb1a8
4
+ data.tar.gz: 16ec8180f911733314fc2be3dbfefc5433c758663b3a52fd9a9fe7e6c00ca370
5
5
  SHA512:
6
- metadata.gz: 58ccec055d15866882fd93521c5d312719012c10e8778e18b99191c2bb48a7173ac0283f7f18daf21fb950d0472c02b3cb861cd1d07b0da0584e62278d62554c
7
- data.tar.gz: ac4f30aa186b37fa04dbadbc49d813648c08ca031809a7c3b73d97fb0d6f7489cc680d74e0a3a83015ebcf77d26d25386c7f8dbecdb4e04e337308bc3a1f7a6c
6
+ metadata.gz: dfacde33b13c8aa12e096013273c8ca597a3ae7cf1a54fde85eaf353a6ff3c0b94bc062f0ad351e6d47bdad0f599c2c12b41f1dde9ddf91bd8ae3b666360e725
7
+ data.tar.gz: ac51091158d1459ca3e6d4666ec86abbae044a04b130e8f4bfc374b7b5dcf315c418b61a25e3a7b78019bc7d3aa024c025656b2de15aa1951e4e1300b82d5c88
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dji_mqtt_connect (0.1.2)
4
+ dji_mqtt_connect (0.1.4)
5
5
  activesupport (>= 6.0, <= 8)
6
6
  dry-struct (~> 1.6)
7
7
  dry-transformer (~> 1.0)
@@ -41,6 +41,11 @@ module DjiMqttConnect
41
41
  Sys::Product::StatusReplyTopicRepository.new(self)
42
42
  end
43
43
 
44
+ # Handles topic thing/product/#{pid}/events
45
+ def thing_product_events_topic
46
+ Thing::Product::EventsTopicRepository.new(self)
47
+ end
48
+
44
49
  # Handles topic thing/product/#{pid}/osd
45
50
  def thing_product_osd_topic
46
51
  Thing::Product::OsdTopicRepository.new(self)
@@ -29,7 +29,7 @@ module DjiMqttConnect
29
29
  transformed_message = attribute_transformer.call(parsed_message)
30
30
 
31
31
  # Build an instance of the class, or a generic message from the current class
32
- message_class = message_class_for_parsed_message(parsed_message, StatusMessage)
32
+ message_class = message_class_from_method_value(parsed_message, StatusMessage)
33
33
  message_class.new transformed_message
34
34
  rescue JSON::ParserError => e
35
35
  raise ParseError.new(e, "Unable to parse message as JSON")
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-struct"
4
+ require "dry-transformer"
5
+ require "json"
6
+
7
+ module DjiMqttConnect
8
+ module Thing::Product
9
+ class EventsMarshal < MessageMarshal
10
+ include Utils::MessageParsing
11
+
12
+ # Rename pesky `method` argument to `_method` and makes a copy of the raw data
13
+ class AttributeTransformer < Dry::Transformer::Pipe
14
+ import Dry::Transformer::HashTransformations
15
+
16
+ define! do
17
+ symbolize_keys
18
+
19
+ copy_keys data: :_data
20
+ rename_keys method: :_method
21
+ end
22
+ end
23
+
24
+ # Attempts to look a the method attribute, and builds a specific Message class for the message
25
+ def load(raw_message)
26
+ # Parse the message from JSON
27
+ parsed_message = JSON.parse(raw_message)
28
+
29
+ # Transform the message
30
+ transformed_message = attribute_transformer.call(parsed_message)
31
+
32
+ # Build an instance of the class, or a generic message from the current class
33
+ message_class = message_class_from_method_value(parsed_message, EventsMessage)
34
+ message_class.new transformed_message
35
+ rescue JSON::ParserError => e
36
+ raise ParseError.new(e, "Unable to parse message as JSON")
37
+ rescue Dry::Struct::Error => e
38
+ raise ParseError.new(e, "Unexpected #{message_class} payload")
39
+ end
40
+
41
+ private
42
+
43
+ def attribute_transformer
44
+ @attribute_transformer ||= AttributeTransformer.new
45
+ end
46
+ end
47
+ end
48
+ end
@@ -30,7 +30,7 @@ module DjiMqttConnect
30
30
  transformed_message = attribute_transformer.call(parsed_message)
31
31
 
32
32
  # Build an instance of the class, or a generic message from the current class
33
- message_class = message_class_for_parsed_message(parsed_message, RequestsMessage)
33
+ message_class = message_class_from_method_value(parsed_message, RequestsMessage)
34
34
  message_class.new transformed_message
35
35
  rescue JSON::ParserError => e
36
36
  raise ParseError.new(e, "Unable to parse message as JSON")
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DjiMqttConnect
4
+ module Thing::Product
5
+ class EventsMessage < Message
6
+ attribute :bid, Types::UUID
7
+ attribute :tid, Types::UUID
8
+ attribute :timestamp, Types::Timestamp
9
+
10
+ attribute :_data, Types::Hash
11
+ attribute :_method, Types::String
12
+
13
+ def to_s
14
+ # Include data method for Generic messages
15
+ instance_of?(EventsMessage) ? "#{super}[#{_method}]" : super
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DjiMqttConnect
4
+ module Thing::Product
5
+ class HmsEventsMessage < EventsMessage
6
+ attribute :_method, Types::String.enum("hms")
7
+
8
+ attribute :data do
9
+ attribute :list, Types::Array do
10
+ # {"0":"Inform","1":"Notice","2":"Alarm"}
11
+ attribute :level, Types::Integer.enum(0, 1, 2)
12
+
13
+ # {"0":"flight mission","1":"device management","2":"media","3":"hms"}
14
+ attribute :module, Types::Integer.enum(0, 1, 2, 3)
15
+
16
+ # {"0":"on the ground","1":"in the sky"}
17
+ attribute :in_the_sky, Types::Integer.enum(0, 1)
18
+
19
+ attribute :code, Types::String
20
+
21
+ # {"0":"No","1":"Yes"}
22
+ attribute :imminent, Types::Integer.enum(0, 1)
23
+
24
+ attribute :args do
25
+ attribute :component_index, Types::Integer
26
+
27
+ attribute :sensor_index, Types::Integer
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -30,7 +30,7 @@ module DjiMqttConnect
30
30
  attribute :credentials do
31
31
  attribute :access_key_id, Types::String
32
32
  attribute :access_key_secret, Types::String
33
- attribute :expire, Types::Integer
33
+ attribute :expire, Types::Integer.constrained(gt: 0)
34
34
  attribute :security_token, Types::String
35
35
  end
36
36
  attribute :endpoint, Types::String
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DjiMqttConnect
4
+ module Thing::Product
5
+ class EventsTopicRepository < TopicRepository
6
+ EVENTS_TOPIC_REGEX = /\Athing\/product\/(?<device_sn>.+)\/events\z/
7
+
8
+ def listen!
9
+ listen_to_topic("thing/product/+/events") do |topic, raw_message|
10
+ logger.debug(raw_message)
11
+
12
+ matched_topic = EVENTS_TOPIC_REGEX.match(topic)
13
+ raise Error, "Unknown topic: #{topic}" unless matched_topic
14
+
15
+ device_sn = matched_topic[:device_sn]
16
+ message = EventsMarshal.load(raw_message)
17
+
18
+ logger.info("Received #{message} from #{device_sn}")
19
+
20
+ if message.instance_of?(EventsMessage)
21
+ # Broadcast an unsupported message event
22
+ broadcast(:unsupported_message, topic, raw_message)
23
+ else
24
+ # Build event name and broadcast (i.e. ::HmsEventsMessage => hms_event)
25
+ event_name = message.class.name.demodulize.sub(/sMessage\z/, "").underscore.to_sym
26
+ broadcast(event_name, device_sn, message)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -5,9 +5,12 @@ module DjiMqttConnect
5
5
  module MessageParsing
6
6
  private
7
7
 
8
- def message_class_for_parsed_message(parsed_message, generic_class)
8
+ def message_class_from_method_value(parsed_message, generic_class)
9
+ message_method = parsed_message["method"]
10
+ return generic_class if message_method.blank?
11
+
9
12
  # update_topo => UpdateTopo
10
- classified_method = ActiveSupport::Inflector.classify(parsed_message["method"])
13
+ classified_method = message_method.split("_").collect! { |w| w.capitalize }.join
11
14
 
12
15
  # UpdateTopo => DjiMqttConnect::Sys::Product::UpdateTopoStatusMessage
13
16
  module_prefix, joiner, class_suffix = generic_class.name.rpartition("::")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DjiMqttConnect
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
  end
@@ -33,6 +33,9 @@ module DjiMqttConnect
33
33
  module Thing
34
34
  module Product
35
35
  # Messages
36
+ autoload :HmsEventsMessage, "dji_mqtt_connect/messages/thing/product/hms_events_message"
37
+ autoload :EventsMessage, "dji_mqtt_connect/messages/thing/product/events_message"
38
+
36
39
  autoload :OsdMessage, "dji_mqtt_connect/messages/thing/product/osd_message"
37
40
  autoload :DockOsdMessage, "dji_mqtt_connect/messages/thing/product/dock_osd_message"
38
41
  autoload :DroneOsdMessage, "dji_mqtt_connect/messages/thing/product/drone_osd_message"
@@ -50,11 +53,14 @@ module DjiMqttConnect
50
53
  autoload :StorageConfigGetRequestsReplyMessage, "dji_mqtt_connect/messages/thing/product/storage_config_get_requests_reply_message"
51
54
 
52
55
  # Topics
56
+ autoload :EventsTopicRepository, "dji_mqtt_connect/topics/thing/product/events"
53
57
  autoload :OsdTopicRepository, "dji_mqtt_connect/topics/thing/product/osd"
54
58
  autoload :RequestsTopicRepository, "dji_mqtt_connect/topics/thing/product/requests"
55
59
  autoload :RequestsReplyTopicRepository, "dji_mqtt_connect/topics/thing/product/requests_reply"
56
60
 
57
61
  # Marshals
62
+ autoload :EventsMarshal, "dji_mqtt_connect/marshals/thing/product/events_marshal"
63
+
58
64
  autoload :OsdMarshal, "dji_mqtt_connect/marshals/thing/product/osd_marshal"
59
65
 
60
66
  autoload :RequestsMarshal, "dji_mqtt_connect/marshals/thing/product/requests_marshal"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dji_mqtt_connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sphere Drones
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-11 00:00:00.000000000 Z
11
+ date: 2023-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -110,6 +110,7 @@ files:
110
110
  - lib/dji_mqtt_connect/factories.rb
111
111
  - lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
112
112
  - lib/dji_mqtt_connect/marshals/sys/product/status_reply_marshal.rb
113
+ - lib/dji_mqtt_connect/marshals/thing/product/events_marshal.rb
113
114
  - lib/dji_mqtt_connect/marshals/thing/product/osd_marshal.rb
114
115
  - lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
115
116
  - lib/dji_mqtt_connect/marshals/thing/product/requests_reply_marshal.rb
@@ -126,6 +127,8 @@ files:
126
127
  - lib/dji_mqtt_connect/messages/thing/product/config_requests_reply_message.rb
127
128
  - lib/dji_mqtt_connect/messages/thing/product/dock_osd_message.rb
128
129
  - lib/dji_mqtt_connect/messages/thing/product/drone_osd_message.rb
130
+ - lib/dji_mqtt_connect/messages/thing/product/events_message.rb
131
+ - lib/dji_mqtt_connect/messages/thing/product/hms_events_message.rb
129
132
  - lib/dji_mqtt_connect/messages/thing/product/osd_message.rb
130
133
  - lib/dji_mqtt_connect/messages/thing/product/remote_osd_message.rb
131
134
  - lib/dji_mqtt_connect/messages/thing/product/requests_message.rb
@@ -139,6 +142,7 @@ files:
139
142
  - lib/dji_mqtt_connect/topic_repository.rb
140
143
  - lib/dji_mqtt_connect/topics/sys/product/status.rb
141
144
  - lib/dji_mqtt_connect/topics/sys/product/status_reply.rb
145
+ - lib/dji_mqtt_connect/topics/thing/product/events.rb
142
146
  - lib/dji_mqtt_connect/topics/thing/product/osd.rb
143
147
  - lib/dji_mqtt_connect/topics/thing/product/requests.rb
144
148
  - lib/dji_mqtt_connect/topics/thing/product/requests_reply.rb