CreatePlan.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <div class="create-plan-wrapper">
  3. <div class="header">
  4. Create Plan
  5. </div>
  6. <div class="content">
  7. <a-form ref="valueRef" layout="horizontal" :hideRequiredMark="true" :rules="rules" :model="planBody"
  8. labelAlign="left">
  9. <a-form-item label="Plan Name" name="name" :labelCol="{ span: 23 }">
  10. <a-input style="background: black;" placeholder="Please enter plan name" v-model:value="planBody.name" />
  11. </a-form-item>
  12. <!-- 航线 -->
  13. <a-form-item label="Flight Route" :wrapperCol="{ offset: 7 }" name="file_id">
  14. <router-link :to="{ name: 'select-plan' }" @click="selectRoute">
  15. Select Route
  16. </router-link>
  17. </a-form-item>
  18. <a-form-item v-if="planBody.file_id" style="margin-top: -15px;">
  19. <div class="wayline-panel" style="padding-top: 5px;">
  20. <div class="title">
  21. <a-tooltip :title="wayline.name">
  22. <div class="pr10" style="width: 120px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden;">
  23. {{ wayline.name }}</div>
  24. </a-tooltip>
  25. <div class="ml10">
  26. <UserOutlined />
  27. </div>
  28. <a-tooltip :title="wayline.user_name">
  29. <div class="ml5 pr10"
  30. style="width: 80px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden;">{{
  31. wayline.user_name }}</div>
  32. </a-tooltip>
  33. </div>
  34. <div class="ml10 mt5" style="color: hsla(0,0%,100%,0.65);">
  35. <span>
  36. <RocketOutlined />
  37. </span>
  38. <span class="ml5">{{ DEVICE_NAME[wayline.drone_model_key] }}</span>
  39. <span class="ml10">
  40. <CameraFilled style="border-top: 1px solid; padding-top: -3px;" />
  41. </span>
  42. <span class="ml5" v-for="payload in wayline.payload_model_keys" :key="payload.id">
  43. {{ DEVICE_NAME[payload] }}
  44. </span>
  45. </div>
  46. <div class="mt5 ml10" style="color: hsla(0,0%,100%,0.35);">
  47. <span class="mr10">Update at {{ new Date(wayline.update_time).toLocaleString() }}</span>
  48. </div>
  49. </div>
  50. </a-form-item>
  51. <!-- 设备 -->
  52. <a-form-item label="Device" :wrapperCol="{ offset: 10 }" v-model:value="planBody.dock_sn" name="dock_sn">
  53. <router-link :to="{ name: 'select-plan' }" @click="selectDevice">Select Device</router-link>
  54. </a-form-item>
  55. <a-form-item v-if="planBody.dock_sn" style="margin-top: -15px;">
  56. <div class="panel" style="padding-top: 5px;">
  57. <div class="title">
  58. <a-tooltip :title="dock.nickname">
  59. <div class="pr10" style="width: 120px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden;">
  60. {{ dock.nickname }}</div>
  61. </a-tooltip>
  62. </div>
  63. <div class="ml10 mt5" style="color: hsla(0,0%,100%,0.65);">
  64. <span>
  65. <RocketOutlined />
  66. </span>
  67. <span class="ml5">{{ dock.children?.nickname ?? 'No drone' }}</span>
  68. </div>
  69. </div>
  70. </a-form-item>
  71. <!-- 任务类型 -->
  72. <a-form-item label="Plan Timer" class="plan-timer-form-item">
  73. <div style="white-space: nowrap;">
  74. <a-radio-group v-model:value="planBody.task_type" button-style="solid">
  75. <a-radio-button v-for="type in TaskTypeOptions" :value="type.value" :key="type.value">
  76. {{ type.label }}
  77. </a-radio-button>
  78. </a-radio-group>
  79. </div>
  80. </a-form-item>
  81. <a-form-item label="Date"
  82. v-if="planBody.task_type === TaskType.Timed || planBody.task_type === TaskType.Condition"
  83. name="select_execute_date" :labelCol="{ span: 23 }">
  84. <a-range-picker v-model:value="planBody.select_execute_date"
  85. :disabledDate="(current: any) => current < moment().subtract(1, 'days')" format="YYYY-MM-DD"
  86. :placeholder="['Start Time', 'End Time']" style="width: 100%;" />
  87. </a-form-item>
  88. <a-form-item label="Time"
  89. v-if="planBody.task_type === TaskType.Timed || planBody.task_type === TaskType.Condition"
  90. name="select_execute_time" ref="select_execute_time" :labelCol="{ span: 23 }" :autoLink="false">
  91. <div class="mb10 flex-row flex-align-center flex-justify-around" v-for="n in planBody.select_time_number"
  92. :key="n">
  93. <a-time-picker v-model:value="planBody.select_time[n - 1][0]" format="HH:mm:ss" show-time
  94. placeholder="Start Time" :style="planBody.task_type === TaskType.Condition ? 'width: 40%' : 'width: 82%'"
  95. @change="() => $refs.select_execute_time.onFieldChange()" />
  96. <template v-if="planBody.task_type === TaskType.Condition">
  97. <div><span style="color: white;">-</span></div>
  98. <a-time-picker v-model:value="planBody.select_time[n - 1][1]" format="HH:mm:ss" show-time
  99. placeholder="End Time" style="width: 40%;" />
  100. </template>
  101. <div class="ml5" style="font-size:18px">
  102. <PlusCircleOutlined class="mr5" style="color: #1890ff" @click="addTime" />
  103. <MinusCircleOutlined :style="planBody.select_time_number === 1 ? 'color: gray' : 'color: red;'"
  104. @click="removeTime" />
  105. </div>
  106. </div>
  107. </a-form-item>
  108. <template v-if="planBody.task_type === TaskType.Condition">
  109. <!-- battery capacity -->
  110. <a-form-item label="Start task when battery level reaches" :labelCol="{ span: 23 }"
  111. name="min_battery_capacity">
  112. <a-input-number class="width-100" v-model:value="planBody.min_battery_capacity" :min="50" :max="100"
  113. :formatter="(value: number) => `${value}%`" :parser="(value: string) => value.replace('%', '')">
  114. </a-input-number>
  115. </a-form-item>
  116. <!-- storage capacity -->
  117. <a-form-item label="Start task when storage level reaches (MB)" :labelCol="{ span: 23 }"
  118. name="storage_capacity">
  119. <a-input-number v-model:value="planBody.min_storage_capacity" class="width-100">
  120. </a-input-number>
  121. </a-form-item>
  122. </template>
  123. <a-form-item label="RTH Altitude Relative to Dock (m)" :labelCol="{ span: 23 }" name="rth_altitude">
  124. <a-input-number v-model:value="planBody.rth_altitude" :min="20" :max="1500" class="width-100" required>
  125. </a-input-number>
  126. </a-form-item>
  127. <a-form-item label="Lost Action" :labelCol="{ span: 23 }" name="out_of_control_action">
  128. <div style="white-space: nowrap;">
  129. <a-radio-group v-model:value="planBody.out_of_control_action" button-style="solid">
  130. <a-radio-button v-for="action in OutOfControlActionOptions" :value="action.value" :key="action.value">
  131. {{ action.label }}
  132. </a-radio-button>
  133. </a-radio-group>
  134. </div>
  135. </a-form-item>
  136. <a-form-item class="width-100" style="margin-bottom: 40px;">
  137. <div class="footer">
  138. <a-button class="mr10" style="background: #3c3c3c;" @click="closePlan">Cancel
  139. </a-button>
  140. <a-button type="primary" @click="onSubmit" :disabled="disabled">OK
  141. </a-button>
  142. </div>
  143. </a-form-item>
  144. </a-form>
  145. </div>
  146. </div>
  147. <div v-if="drawerVisible"
  148. style="position: absolute; left: 335px; width: 280px; height: 100vh; float: right; top: 0; z-index: 1000; color: white; background: #282828;">
  149. <div>
  150. <router-view :name="routeName" />
  151. </div>
  152. <div style="position: absolute; top: 15px; right: 10px;">
  153. <a style="color: white;" @click="closePanel">
  154. <CloseOutlined />
  155. </a>
  156. </div>
  157. </div>
  158. </template>
  159. <script lang="ts" setup>
  160. import { computed, reactive, ref } from 'vue'
  161. import { ERouterName } from '/@/types'
  162. import { useMyStore } from '/@/store'
  163. import { WaylineFile } from '/@/types/wayline'
  164. import { Device, DEVICE_NAME } from '/@/types/device'
  165. import { createPlan, CreatePlan } from '/@/api/wayline'
  166. import { getRoot } from '/@/root'
  167. import { TaskType, OutOfControlActionOptions, OutOfControlAction, TaskTypeOptions } from '/@/types/task'
  168. import { RuleObject } from 'ant-design-vue/es/form/interface'
  169. import { getWorkspaceId } from '/@/utils/index'
  170. import moment from 'moment';
  171. const root = getRoot()
  172. const store = useMyStore()
  173. const wayline = computed<WaylineFile>(() => {
  174. return store.state.waylineInfo
  175. })
  176. const dock = computed<Device>(() => {
  177. return store.state.dockInfo
  178. })
  179. const disabled = ref(false)
  180. const routeName = ref('')
  181. const planBody = reactive({
  182. name: '',
  183. file_id: computed(() => store.state?.waylineInfo.id),
  184. dock_sn: computed(() => store.state?.dockInfo.device_sn),
  185. task_type: TaskType.Immediate,
  186. select_execute_date: [moment(), moment()] as any,
  187. select_time_number: 1,
  188. select_time: [[]] as any[],
  189. rth_altitude: '',
  190. out_of_control_action: OutOfControlAction.ReturnToHome,
  191. min_battery_capacity: 90 as number,
  192. min_storage_capacity: undefined as number | undefined,
  193. })
  194. const drawerVisible = ref(false)
  195. const valueRef = ref()
  196. const rules = {
  197. name: [
  198. { required: true, message: 'Please enter plan name.' },
  199. { max: 20, message: 'Length should be 1 to 20' }
  200. ],
  201. file_id: [{ required: true, message: 'Select Route' }],
  202. dock_sn: [{ required: true, message: 'Select Device' }],
  203. select_execute_time: [{
  204. validator: async (rule: RuleObject, value: any[]) => {
  205. validEndTime()
  206. validStartTime()
  207. if (planBody.select_time.length < planBody.select_time_number) {
  208. throw new Error('Select time')
  209. }
  210. validOverlapped()
  211. }
  212. }],
  213. select_execute_date: [{ required: true, message: 'Select date' }],
  214. rth_altitude: [
  215. {
  216. validator: async (rule: RuleObject, value: string) => {
  217. if (!/^[0-9]{1,}$/.test(value)) {
  218. throw new Error('RTH Altitude Require number')
  219. }
  220. },
  221. }
  222. ],
  223. min_battery_capacity: [
  224. {
  225. validator: async (rule: RuleObject, value: any) => {
  226. if (TaskType.Condition === planBody.task_type && !value) {
  227. throw new Error('Please enter battery capacity')
  228. }
  229. },
  230. }
  231. ],
  232. out_of_control_action: [{ required: true, message: 'Select Lost Action' }],
  233. }
  234. function validStartTime(): Error | void {
  235. for (let i = 0; i < planBody.select_time.length; i++) {
  236. if (!planBody.select_time[i][0]) {
  237. throw new Error('Select start time')
  238. }
  239. }
  240. }
  241. function validEndTime(): Error | void {
  242. if (TaskType.Condition !== planBody.task_type) return
  243. for (let i = 0; i < planBody.select_time.length; i++) {
  244. if (!planBody.select_time[i][1]) {
  245. throw new Error('Select end time')
  246. }
  247. if (planBody.select_time[i][0] && planBody.select_time[i][1].isSameOrBefore(planBody.select_time[i][0])) {
  248. throw new Error('End time should be later than start time')
  249. }
  250. }
  251. }
  252. function validOverlapped(): Error | void {
  253. if (TaskType.Condition !== planBody.task_type) return
  254. const arr = planBody.select_time.slice()
  255. arr.sort((a, b) => a[0].unix() - b[0].unix())
  256. arr.forEach((v, i, arr) => {
  257. if (i > 0 && v[0] < arr[i - 1][1]) {
  258. throw new Error('Overlapping time periods.')
  259. }
  260. })
  261. }
  262. function onSubmit() {
  263. valueRef.value.validate().then(() => {
  264. disabled.value = true
  265. const createPlanBody = { ...planBody } as unknown as CreatePlan
  266. if (planBody.select_execute_date.length === 2) {
  267. createPlanBody.task_days = []
  268. for (let i = planBody.select_execute_date[0]; i.isSameOrBefore(planBody.select_execute_date[1]); i.add(1, 'days')) {
  269. createPlanBody.task_days.push(i.unix())
  270. }
  271. }
  272. createPlanBody.task_periods = []
  273. if (TaskType.Immediate !== planBody.task_type) {
  274. for (let i = 0; i < planBody.select_time.length; i++) {
  275. const result = []
  276. result.push(planBody.select_time[i][0].unix())
  277. if (TaskType.Condition === planBody.task_type) {
  278. result.push(planBody.select_time[i][1].unix())
  279. }
  280. createPlanBody.task_periods.push(result)
  281. }
  282. }
  283. createPlanBody.rth_altitude = Number(createPlanBody.rth_altitude)
  284. if (wayline.value && wayline.value.template_types && wayline.value.template_types.length > 0) {
  285. createPlanBody.wayline_type = wayline.value.template_types[0]
  286. }
  287. createPlan(getWorkspaceId(), createPlanBody)
  288. .then(res => {
  289. disabled.value = false
  290. }).finally(() => {
  291. closePlan()
  292. })
  293. }).catch((e: any) => {
  294. console.log('validate err', e)
  295. })
  296. }
  297. function closePlan() {
  298. root.$router.push('/' + ERouterName.TASK)
  299. }
  300. function closePanel() {
  301. drawerVisible.value = false
  302. routeName.value = ''
  303. }
  304. function selectRoute() {
  305. drawerVisible.value = true
  306. routeName.value = 'WaylinePanel'
  307. }
  308. function selectDevice() {
  309. drawerVisible.value = true
  310. routeName.value = 'DockPanel'
  311. }
  312. function addTime() {
  313. valueRef.value.validateFields(['select_execute_time']).then(() => {
  314. planBody.select_time_number++
  315. planBody.select_time.push([])
  316. })
  317. }
  318. function removeTime() {
  319. if (planBody.select_time_number === 1) return
  320. planBody.select_time_number--
  321. planBody.select_time.splice(planBody.select_time_number)
  322. }
  323. </script>
  324. <style lang="scss">
  325. .create-plan-wrapper {
  326. background-color: #232323;
  327. color: fff;
  328. padding-bottom: 0;
  329. height: 100vh;
  330. display: flex;
  331. flex-direction: column;
  332. width: 285px;
  333. .header {
  334. height: 52px;
  335. border-bottom: 1px solid #4f4f4f;
  336. font-weight: 700;
  337. font-size: 16px;
  338. padding-left: 10px;
  339. display: flex;
  340. align-items: center;
  341. }
  342. ::-webkit-scrollbar {
  343. display: none;
  344. }
  345. .content {
  346. height: calc(100% - 54px);
  347. overflow-y: auto;
  348. form {
  349. margin: 10px;
  350. }
  351. form label,
  352. input,
  353. .ant-input,
  354. .ant-calendar-range-picker-separator,
  355. .ant-input:hover,
  356. .ant-time-picker .anticon,
  357. .ant-calendar-picker .anticon {
  358. background-color: #232323;
  359. color: #fff;
  360. }
  361. .ant-input-suffix {
  362. color: #fff;
  363. }
  364. .plan-timer-form-item {
  365. .ant-radio-button-wrapper {
  366. background-color: #232323;
  367. color: #fff;
  368. width: 33%;
  369. text-align: center;
  370. &.ant-radio-button-wrapper-checked {
  371. background-color: #1890ff;
  372. }
  373. }
  374. }
  375. }
  376. .footer {
  377. display: flex;
  378. padding: 10px 0;
  379. button {
  380. width: 45%;
  381. color: #fff;
  382. border: 0;
  383. }
  384. }
  385. }
  386. .wayline-panel {
  387. background: #3c3c3c;
  388. margin-left: auto;
  389. margin-right: auto;
  390. margin-top: 10px;
  391. height: 90px;
  392. width: 95%;
  393. font-size: 13px;
  394. border-radius: 2px;
  395. cursor: pointer;
  396. .title {
  397. display: flex;
  398. color: white;
  399. flex-direction: row;
  400. align-items: center;
  401. height: 30px;
  402. font-weight: bold;
  403. margin: 0px 10px 0 10px;
  404. }
  405. }
  406. .panel {
  407. background: #3c3c3c;
  408. margin-left: auto;
  409. margin-right: auto;
  410. margin-top: 10px;
  411. height: 70px;
  412. width: 95%;
  413. font-size: 13px;
  414. border-radius: 2px;
  415. cursor: pointer;
  416. .title {
  417. display: flex;
  418. color: white;
  419. flex-direction: row;
  420. align-items: center;
  421. height: 30px;
  422. font-weight: bold;
  423. margin: 0px 10px 0 10px;
  424. }
  425. }
  426. </style>