dji_mqtt_connect 0.1.7 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ module DjiMqttConnect
6
6
  # Client for communicating with DJI Cloud API MQTT Messages
7
7
  # see: https://developer.dji.com/doc/cloud-api-tutorial/en/server-api-reference/mqtt/topic-definition.html
8
8
  class Client
9
- def self.build(mqtt_address, username:, password:, &block)
9
+ def self.build(mqtt_address, username:, password:, client_name: DjiMqttConnect.client_name, &block)
10
10
  mqtt_url = URI.parse(mqtt_address)
11
11
  mqtt_host = mqtt_url.host
12
12
  mqtt_port = mqtt_url.port.to_i
@@ -18,7 +18,7 @@ module DjiMqttConnect
18
18
  mqtt_client = MQTT::Client.connect(
19
19
  host: mqtt_host,
20
20
  port: mqtt_port,
21
- client_id: DjiMqttConnect.client_id,
21
+ client_id: MQTT::Client.generate_client_id(client_name),
22
22
  username: username,
23
23
  password: password,
24
24
  ssl: !%w[tcp mqtt ws].include?(mqtt_scheme)
@@ -26,6 +26,33 @@ module DjiMqttConnect
26
26
 
27
27
  attribute :sensor_index, Types::Integer
28
28
  end
29
+
30
+ def in_the_sky?
31
+ in_the_sky == 1
32
+ end
33
+
34
+ def aircraft_message
35
+ if in_the_sky?
36
+ Translations.aircraft_in_the_sky_hms_event_code_message(
37
+ code: code,
38
+ sensor_index: args.sensor_index,
39
+ component_index: args.component_index
40
+ )
41
+ else
42
+ Translations.aircraft_hms_event_code_message(
43
+ code: code,
44
+ sensor_index: args.sensor_index,
45
+ component_index: args.component_index
46
+ )
47
+ end
48
+ end
49
+
50
+ def dock_message
51
+ Translations.dock_hms_event_code_message(
52
+ code: code,
53
+ sensor_index: args.sensor_index
54
+ )
55
+ end
29
56
  end
30
57
  end
31
58
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module DjiMqttConnect
4
4
  class Railtie < Rails::Railtie
5
+ # Add all error code translations to I18n
6
+ DjiMqttConnect::Translations.load_translations!
7
+
5
8
  # Allow ActiveJob to serialize DjiMqttConnect::Message objects
6
9
  config.active_job.custom_serializers << Railties::MessageSerializer
7
10
 
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DjiMqttConnect
4
+ # https://developer.dji.com/doc/cloud-api-tutorial/en/feature-set/dock-feature-set/hms.html
5
+ module Translations
6
+ module_function
7
+
8
+ def load_translations!
9
+ I18n.load_path += Dir[File.expand_path(File.join(__dir__, "../../config/locales/*.yml"))]
10
+ end
11
+
12
+ def aircraft_in_the_sky_hms_event_code_message(code:, sensor_index:, component_index:)
13
+ I18n.translate(
14
+ "fpv_tip_#{code}_in_the_sky",
15
+ alarmid: code,
16
+ index: sensor_index + 1,
17
+ component_index: component_index + 1,
18
+ battery_index: battery_position(sensor_index),
19
+ scope: "dji_mqtt_connect.hms_event_codes",
20
+ default: code
21
+ )
22
+ end
23
+
24
+ def aircraft_hms_event_code_message(code:, sensor_index:, component_index:)
25
+ I18n.translate(
26
+ "fpv_tip_#{code}",
27
+ alarmid: code,
28
+ index: sensor_index + 1,
29
+ component_index: component_index + 1,
30
+ battery_index: battery_position(sensor_index),
31
+ scope: "dji_mqtt_connect.hms_event_codes",
32
+ default: code
33
+ )
34
+ end
35
+
36
+ def dock_hms_event_code_message(code:, sensor_index:)
37
+ I18n.translate(
38
+ "dock_tip_#{code}",
39
+ alarmid: code,
40
+ dock_cover_index: dock_cover_position(sensor_index),
41
+ charging_rod_index: dock_charging_rod_position(sensor_index),
42
+ scope: "dji_mqtt_connect.hms_event_codes",
43
+ default: code
44
+ )
45
+ end
46
+
47
+ def position_name(position)
48
+ I18n.translate(position, scope: "dji_mqtt_connect.position", default: position)
49
+ end
50
+
51
+ # If the text contains "%battery_index" and sensor_index is 0, use "left" to replace "%battery_index", otherwise, use "right" to replace.
52
+ def battery_position(sensor_index)
53
+ case sensor_index
54
+ when 0
55
+ position_name("left")
56
+ when 1
57
+ position_name("right")
58
+ else
59
+ position_name("unknown")
60
+ end
61
+ end
62
+
63
+ # If the text contains "%dock_cover_index" and sensor_index is 0, use "left" to replace "%dock_cover_index", otherwise, use "right" to replace.
64
+ def dock_cover_position(sensor_index)
65
+ case sensor_index
66
+ when 0
67
+ position_name("left")
68
+ when 1
69
+ position_name("right")
70
+ else
71
+ position_name("unknown")
72
+ end
73
+ end
74
+
75
+ # If the text contains "%charging_rod_index", when sensor_index is 0, replace "%charging_rod_index" with "front". When sensor_index is 1, use "back" instead. When sensor_index is 2, use "left" instead. When sensor_index is 3, use "right" instead.
76
+ def dock_charging_rod_position(sensor_index)
77
+ case sensor_index
78
+ when 0
79
+ position_name("front")
80
+ when 1
81
+ position_name("back")
82
+ when 2
83
+ position_name("left")
84
+ when 3
85
+ position_name("right")
86
+ else
87
+ position_name("unknown")
88
+ end
89
+ end
90
+ end
91
+ end
@@ -21,7 +21,12 @@ module DjiMqttConnect
21
21
  Longitude = NullInteger | Types::JSON::Decimal.constrained(gteq: -180, lteq: 180)
22
22
 
23
23
  # Device Details
24
- DeviceDomain = Types::Coercible::Integer # dock returns this value as string
24
+ AIRCRAFT_DOMAIN = 0
25
+ PAYLOAD_DOMAIN = 1
26
+ REMOTE_CONTROLLER_DOMAIN = 2
27
+ DOCK_DOMAIN = 3
28
+
29
+ DeviceDomain = Types::Coercible::Integer.enum(AIRCRAFT_DOMAIN, PAYLOAD_DOMAIN, REMOTE_CONTROLLER_DOMAIN, DOCK_DOMAIN) # dock returns this value as string
25
30
  DeviceType = Types::Integer
26
31
  DeviceSubType = Types::Integer
27
32
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DjiMqttConnect
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.9"
5
5
  end
@@ -11,6 +11,7 @@ module DjiMqttConnect
11
11
  autoload :Message, "dji_mqtt_connect/message"
12
12
  autoload :MessageMarshal, "dji_mqtt_connect/message_marshal"
13
13
  autoload :TopicRepository, "dji_mqtt_connect/topic_repository"
14
+ autoload :Translations, "dji_mqtt_connect/translations"
14
15
  autoload :Types, "dji_mqtt_connect/types"
15
16
 
16
17
  module Sys
@@ -92,7 +93,7 @@ module DjiMqttConnect
92
93
  autoload :MessageSerializer, "dji_mqtt_connect/railties/message_serializer"
93
94
  end
94
95
 
95
- cattr_accessor :client_id, default: MQTT::Client.generate_client_id(name)
96
+ cattr_accessor :client_name, default: name
96
97
  cattr_accessor :logger, default: ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new($stdout)).tap { |logger| logger.level = Logger::INFO }
97
98
  end
98
99
 
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.7
4
+ version: 0.1.9
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-19 00:00:00.000000000 Z
11
+ date: 2023-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -103,6 +103,8 @@ files:
103
103
  - Gemfile.lock
104
104
  - README.md
105
105
  - Rakefile
106
+ - config/locales/dji_mqtt_connect.en.yml
107
+ - config/locales/dji_mqtt_connect.zh.yml
106
108
  - dji_mqtt_connect.gemspec
107
109
  - lib/dji_mqtt_connect.rb
108
110
  - lib/dji_mqtt_connect/client.rb
@@ -150,6 +152,7 @@ files:
150
152
  - lib/dji_mqtt_connect/topics/thing/product/osd.rb
151
153
  - lib/dji_mqtt_connect/topics/thing/product/requests.rb
152
154
  - lib/dji_mqtt_connect/topics/thing/product/requests_reply.rb
155
+ - lib/dji_mqtt_connect/translations.rb
153
156
  - lib/dji_mqtt_connect/types.rb
154
157
  - lib/dji_mqtt_connect/utils/message_parsing.rb
155
158
  - lib/dji_mqtt_connect/utils/message_sanitizer.rb