FragmentDynamicInfo.kt 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package com.cr.pages
  2. import android.os.Bundle
  3. import android.view.LayoutInflater
  4. import android.view.View
  5. import android.view.ViewGroup
  6. import android.widget.TextView
  7. import androidx.fragment.app.activityViewModels
  8. import androidx.transition.Visibility
  9. import com.cr.common.CrCameraInfo
  10. import com.cr.cruav.CrApplication
  11. import com.cr.cruav.R
  12. import com.cr.data.CrUtil
  13. import com.cr.data.DEFAULT_STR
  14. import com.cr.event.CrCommonAction
  15. import com.cr.event.EventCommon
  16. import com.cr.viewmodel.CrCameraVM
  17. import com.cr.viewmodel.CrFlightControlVM
  18. import com.cr.widget.CrSpeedWidget
  19. import com.squareup.otto.Subscribe
  20. import dji.sdk.keyvalue.key.CameraKey
  21. import dji.sdk.keyvalue.key.KeyTools
  22. import dji.sdk.keyvalue.value.common.EmptyMsg
  23. import dji.v5.common.callback.CommonCallbacks
  24. import dji.v5.common.error.IDJIError
  25. import dji.v5.manager.account.LoginInfo
  26. import dji.v5.manager.account.LoginInfoUpdateListener
  27. import dji.v5.manager.account.LoginState
  28. import dji.v5.manager.account.UserAccountManager
  29. import org.w3c.dom.Text
  30. /**
  31. * 操作系统:MAC系统
  32. * 创建者:王成
  33. * 创建日期:2023/7/29 16:30
  34. * 描述:大疆动态数据页面
  35. */
  36. class FragmentDynamicInfo : CrFragment(), View.OnClickListener {
  37. // todo: 2023/8/2 绑定模型
  38. private val cameraVm: CrCameraVM by activityViewModels()
  39. private val flightControlVm: CrFlightControlVM by activityViewModels()
  40. // todo: 2023/8/2 组件定义
  41. private var lblCameraISO: TextView? = null // define: 2023/8/2 ISO值
  42. private var lblCameraSpeed: TextView? = null // define: 2023/8/2 快门速度
  43. private var lblCameraFocal: TextView? = null // define: 2023/8/2 相机焦距
  44. private var lblCameraEv: TextView? = null // define: 2023/8/2 补偿值
  45. private var lblSDPhotoCount: TextView? = null // define: 2023/8/2 SD卡中可存储的照片数量
  46. private var lblSDVideoTime: TextView? = null // define: 2023/8/2 SD卡中可以录制的视频时间
  47. private var lblDJILogin: TextView? = null // define: 2023/8/2 是否登录大疆账户
  48. private var flagPhoto: TextView? = null // define: 2023/8/3 照片模式标志
  49. private var flagVideo: TextView? = null // define: 2023/8/3 视频模式标志
  50. private var lblMessage: TextView? = null // define: 2023/8/7 消息
  51. private var lblAltitude: TextView? = null // define: 2023/8/15 相对航高
  52. private var lblTakeoffAltitude: TextView? = null // define: 2023/8/15 起飞海拔
  53. private var lblDistanceByHome: TextView? = null // define: 2023/8/15 返航距离
  54. private var lblAltitudeByHome: TextView? = null // define: 2023/8/15 返航高度
  55. private var speedX: CrSpeedWidget? = null // define: 2023/8/16 x方向速度
  56. private var speedY: CrSpeedWidget? = null // define: 2023/8/16 y方向速度
  57. private var speedZ: CrSpeedWidget? = null // define: 2023/8/16 z方向速度
  58. /**
  59. * 初始化
  60. * @param inflater LayoutInflater
  61. * @param container ViewGroup?
  62. * @param savedInstanceState Bundle?
  63. * @return View?
  64. */
  65. override fun onCreateView(
  66. inflater: LayoutInflater,
  67. container: ViewGroup?,
  68. savedInstanceState: Bundle?
  69. ): View? {
  70. self = this;
  71. mainView = inflater.inflate(R.layout.frag_dynamic_info, null)
  72. // todo: 2023/8/2 挂载控件
  73. joinControls()
  74. // todo: 2023/8/7 注册事件
  75. CrApplication.getEventBus().register(this)
  76. // todo: 2023/8/15 绑定大疆账号登录状态
  77. bindDjiUserAccountLogin()
  78. return mainView
  79. }
  80. /**
  81. * 覆写挂载控件
  82. */
  83. override fun joinControls() {
  84. super.joinControls()
  85. // todo: 2023/8/2 挂载控件
  86. mainView?.let {
  87. lblCameraISO = it.findViewById(R.id.camera_iso)
  88. lblCameraSpeed = it.findViewById(R.id.camera_speed)
  89. lblCameraFocal = it.findViewById(R.id.camera_focal)
  90. lblCameraEv = it.findViewById(R.id.camera_ev)
  91. lblSDPhotoCount = it.findViewById(R.id.sd_photo_count)
  92. lblSDVideoTime = it.findViewById(R.id.sd_video_time)
  93. lblDJILogin = it.findViewById(R.id.dji_login)
  94. // todo: 2023/8/3 挂载相机模式标志
  95. flagPhoto = it.findViewById(R.id.camera_model_photo)
  96. flagPhoto?.visibility = View.GONE
  97. flagVideo = it.findViewById(R.id.camera_model_video)
  98. flagVideo?.visibility = View.GONE
  99. // todo: 2023/8/7 挂载消息控件
  100. lblMessage = it.findViewById(R.id.lbl_message)
  101. // todo: 2023/8/15 挂载返航高度及相对航高
  102. lblAltitude = it.findViewById(R.id.lbl_altitude)
  103. lblTakeoffAltitude = it.findViewById(R.id.lbl_takeoff_altitude)
  104. lblDistanceByHome = it.findViewById(R.id.lbl_home_distance)
  105. lblAltitudeByHome = it.findViewById(R.id.lbl_home_altitude)
  106. // todo: 2023/8/16 挂载速度
  107. speedX = it.findViewById(R.id.speed_x)
  108. speedY = it.findViewById(R.id.speed_y)
  109. speedZ = it.findViewById(R.id.speed_z)
  110. speedX?.setProgressMax(30)
  111. speedY?.setProgressMax(30)
  112. speedZ?.setProgressMax(10)
  113. // todo: 2023/8/2 挂载事件
  114. lblSDPhotoCount?.setOnClickListener(this)
  115. }
  116. }
  117. /**
  118. * 生命周期
  119. * 后台转前台运行时调用
  120. */
  121. override fun onResume() {
  122. super.onResume()
  123. // todo: 2023/8/2 初始化订阅
  124. initObserver()
  125. }
  126. /**
  127. * 生命周期
  128. * 销毁
  129. */
  130. override fun onDestroy() {
  131. // todo: 2023/8/7 取消事件监听
  132. CrApplication.getEventBus().unregister(this)
  133. // todo: 2023/8/15 取消登录状态更新监听绑定
  134. UserAccountManager.getInstance().removeLoginInfoUpdateListener(djiUserAccountLoginListener)
  135. super.onDestroy()
  136. }
  137. /**
  138. * 初始化订阅
  139. */
  140. private fun initObserver() {
  141. // todo: 2023/8/2 订阅相机信息
  142. cameraVm.cameraInfo.observe(requireActivity()) {
  143. it?.let {
  144. // todo: 2023/8/4 相机基本参数
  145. lblCameraISO?.text = it.iso
  146. lblCameraSpeed?.text = it.speed
  147. lblCameraFocal?.text = it.focal
  148. lblCameraEv?.text = it.ev
  149. // todo: 2023/8/4 可拍照数量或可录制视频时长
  150. lblSDPhotoCount?.text = it.photoCount
  151. lblSDVideoTime?.text = it.videoTime
  152. // todo: 2023/8/4 相机模式
  153. when (it.cameraModel) {
  154. CrCameraInfo.CrCameraMode.NON -> {
  155. flagVideo?.visibility = View.GONE
  156. flagPhoto?.visibility = View.GONE
  157. }
  158. CrCameraInfo.CrCameraMode.PHOTO -> {
  159. flagVideo?.visibility = View.GONE
  160. flagPhoto?.visibility = View.VISIBLE
  161. }
  162. CrCameraInfo.CrCameraMode.VIDEO -> {
  163. flagVideo?.visibility = View.VISIBLE
  164. flagPhoto?.visibility = View.GONE
  165. }
  166. }
  167. }
  168. }
  169. // todo: 2023/8/15 订阅飞行器信息
  170. flightControlVm.flightControlInfo.observe(requireActivity()) {
  171. it?.let {
  172. lblAltitude?.text = it.altitudeStr
  173. lblTakeoffAltitude?.text = it.takeoffAltitudeStr
  174. lblAltitudeByHome?.text = it.homeAltitudeStr
  175. lblDistanceByHome?.text = it.homeDistanceStr
  176. // todo: 2023/8/16 更新速度
  177. speedX?.setProgressValue(it.speedX.toInt())
  178. speedY?.setProgressValue(it.speedY.toInt())
  179. speedZ?.setProgressValue(it.speedZ.toInt())
  180. }
  181. }
  182. }
  183. /**
  184. * 绑定大疆登录
  185. */
  186. private fun bindDjiUserAccountLogin() {
  187. var loginState = UserAccountManager.getInstance().loginInfo.loginState
  188. // todo: 2023/8/15 更新登录信息
  189. updateDjiUserAccountLoginInfo(loginState)
  190. // todo: 2023/8/15 绑定监听
  191. UserAccountManager.getInstance().addLoginInfoUpdateListener(djiUserAccountLoginListener)
  192. }
  193. /**
  194. * 更新大疆账户登录状态信息
  195. * @param state LoginState 登录状态
  196. */
  197. private fun updateDjiUserAccountLoginInfo(state: LoginState) {
  198. mainHandler.post {
  199. if (state == LoginState.LOGGED_IN) {
  200. lblDJILogin?.text = "已登录"
  201. } else {
  202. lblDJILogin?.text = DEFAULT_STR
  203. }
  204. }
  205. }
  206. /**
  207. * 大疆账户登录变更监听
  208. */
  209. private val djiUserAccountLoginListener = LoginInfoUpdateListener {
  210. // todo: 2023/8/15 更新登录信息
  211. updateDjiUserAccountLoginInfo(it.loginState)
  212. }
  213. /**
  214. * 视图点击事件
  215. * @param view View
  216. */
  217. override fun onClick(view: View?) {
  218. when (view?.id) {
  219. R.id.sd_photo_count -> {
  220. CrUtil.print("拍照")
  221. dji.v5.manager.KeyManager.getInstance()
  222. .performAction(KeyTools.createKey(CameraKey.KeyStartShootPhoto),
  223. object : CommonCallbacks.CompletionCallbackWithParam<EmptyMsg> {
  224. override fun onSuccess(t: EmptyMsg?) {
  225. CrUtil.print("拍照成功${t.toString()}")
  226. }
  227. override fun onFailure(error: IDJIError) {
  228. CrUtil.print("拍照失败${error.description()}}")
  229. }
  230. })
  231. }
  232. }
  233. }
  234. @Subscribe
  235. fun onMessage(event: EventCommon<String>) {
  236. event?.let {
  237. if (it.action == CrCommonAction.SHOW_MESSAGE) {
  238. lblMessage?.text = event.param
  239. }
  240. }
  241. }
  242. }