dji_mqtt_connect 0.1.3 → 0.1.4

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: e40cb40801a11ebd25bc5ae79c3b6b332c879cb1ddb026e5e2a4cf8c48415564
4
- data.tar.gz: b0629c19cbc9a6e3c7378727c0fa1a0253c42def15ac0ff0602c64ea8d071357
3
+ metadata.gz: 3a9ba8caf2ea370ed33848df512fbebcc63dfd4b63dd07947a1a653f56fdb1a8
4
+ data.tar.gz: 16ec8180f911733314fc2be3dbfefc5433c758663b3a52fd9a9fe7e6c00ca370
5
5
  SHA512:
6
- metadata.gz: 8bc46c0f8448ae3d551c7bc99f74e432e5a253a6686aff92a7e4798867ff801bcbff895675813646a3fbf727a1d8db93c82e30613eaef14f4d8bf1c0d834624b
7
- data.tar.gz: a91a113aa29a75f636704b2266dff13e10a2e7daeb0e1fe1f3ee4951a8cdf85b255fe88502534fe28e98a512751f6d00731f11ef029311a8281a55427c9557e5
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.3)
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)
@@ -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
@@ -12,8 +12,19 @@ module DjiMqttConnect
12
12
  matched_topic = EVENTS_TOPIC_REGEX.match(topic)
13
13
  raise Error, "Unknown topic: #{topic}" unless matched_topic
14
14
 
15
- # Everything is Unsupported
16
- broadcast(:unsupported_message, topic, raw_message)
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
17
28
  end
18
29
  end
19
30
  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.3"
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"
@@ -56,6 +59,8 @@ module DjiMqttConnect
56
59
  autoload :RequestsReplyTopicRepository, "dji_mqtt_connect/topics/thing/product/requests_reply"
57
60
 
58
61
  # Marshals
62
+ autoload :EventsMarshal, "dji_mqtt_connect/marshals/thing/product/events_marshal"
63
+
59
64
  autoload :OsdMarshal, "dji_mqtt_connect/marshals/thing/product/osd_marshal"
60
65
 
61
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.3
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-12 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