edit.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <template>
  2. <view>
  3. <view class="top">
  4. <span class='title'>编辑蓝牙设备</span><span class='id'>设备id</span><span>{{deviceId}}</span><span
  5. class='startTime'>开始时间</span><span>{{startTime}}</span><span class='totalTime'
  6. style='font-weight: 700;'>总时长</span><span style='font-weight: 700;'>{{time}}</span>
  7. </view>
  8. <view class="type">
  9. <view class="facilityType">
  10. <p>设备类型</p>
  11. <view class="buttonOP" v-if="typeList">
  12. <view class="item" @click="selectOne('type_sn',index,item)"
  13. :class="{selectType:index==selectTypeIndex}" v-for="(item,index) in typeList" :key="index">
  14. <img :src="item.cover_url" alt="">
  15. <span>{{item.name}}</span>
  16. </view>
  17. </view>
  18. </view>
  19. <view class="facilityType">
  20. <p>探头</p>
  21. <view class="buttonOP" v-if="typeList!=''">
  22. <view class="item" @click="selectOne('detectors',index,item)"
  23. :class="{selectType:index==selectDetectorsIndexA}"
  24. v-for="(item,index) in typeList[selectTypeIndex].detectors" :key="index">
  25. <img :src="item.cover_url" alt="">
  26. <span>{{item.detector_name}}</span>
  27. </view>
  28. </view>
  29. </view>
  30. <!-- mode中的模式 -->
  31. <view class="pattern">
  32. <view class="facilityType"
  33. v-if="typeList!=''&&typeList[selectTypeIndex].detectors[selectDetectorsIndex].config.mode&&typeList[selectTypeIndex].detectors[selectDetectorsIndex].config.mode.list">
  34. <p>模式</p>
  35. <view class="buttonOP">
  36. <button @click="selectOne('mode',index,item)" :class="{selectType:index==selectModeIndexA}"
  37. v-for="(item,index) in typeList[selectTypeIndex].detectors[selectDetectorsIndex].config.mode.list">{{item.name}}</button>
  38. </view>
  39. </view>
  40. <!-- power中的模式 -->
  41. <view class="facilityType"
  42. v-else-if="typeList!=''&&typeList[selectTypeIndex].detectors[selectDetectorsIndex].config.power.list">
  43. <p>模式</p>
  44. <view class="buttonOP">
  45. <button @click="selectOne('mode',index,item)" :class="{selectType:index==selectModeIndexA}"
  46. v-for="(item,index) in typeList[selectTypeIndex].detectors[selectDetectorsIndex].config.power.list">{{item.name}}</button>
  47. <button style="margin-left: 100px; background-color: #374b5f;" @click="pqCLick">点击排气</button>
  48. </view>
  49. </view>
  50. <view class="facilityType" v-else>
  51. <p>强度</p>
  52. <view class="buttonOPO">
  53. <button @click="reduce">减少</button>
  54. <view class="strength">
  55. {{modeClass}}
  56. </view><button @click="add">增加</button>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. <view class="bottomBox">
  62. <view class="back">
  63. <button @click="back">返回蓝牙匹配</button>
  64. </view>
  65. <view class="bottomBox-right">
  66. <button @click="operate('open')" class="open">开始</button>
  67. <button @click="operate('pause')" class="pause">暂停</button>
  68. <button @click="operate('finish')" class="finish">结束</button>
  69. </view>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import {
  75. formatDate
  76. } from '../../utils.js'
  77. export default {
  78. data() {
  79. return {
  80. deviceId: '', //蓝牙设备ID
  81. serviceId: '', //服务UUID
  82. characteristicId: '', //特征值
  83. typeList: [], //设备数据列表
  84. selectTypeIndex: 0, //选择中设备类型index
  85. selectDetectorsIndex: 0, //探头index
  86. selectDetectorsIndexA: 99, //探头index
  87. selectModeIndex: 0, //模式index
  88. selectModeIndexA: 99, //模式index
  89. TYPE_SN: 'ZK', //设备名称
  90. PQCODE: "AABB12343838373244363235444138010101010101", //开始排气
  91. PQCLOSECODE: "AABB12343838373244363235444138010201010101", //关闭排气
  92. modeClass: 0, //强度等级
  93. minClass: '', //最小强度等级
  94. maxClass: '', //小大强度等级
  95. incrementClass: '', //强度增幅
  96. START: '01', //开始
  97. PAUSE: '00', //暂停
  98. equipmentNum: '', //设备编号
  99. modeNum: '01', //模式编号
  100. startTime: "", //开始时间
  101. time: '00:00:00', //初始展示时间
  102. timer: '',
  103. istimer: false, //是否暂停
  104. hour: 0, //计时(时)
  105. minutes: 0, //计时(分)
  106. seconds: 0, //计时(秒)
  107. };
  108. },
  109. onLoad(options) {
  110. // console.log(options, 333333);
  111. this.deviceId = options.deviceId
  112. this.serviceId = options.serviceId
  113. this.characteristicId = options.characteristicId
  114. let typeList = uni.getStorageSync('typeList')
  115. if (!typeList) {
  116. // 获取设备列表
  117. this.getTypeList()
  118. } else {
  119. this.typeList = typeList
  120. }
  121. },
  122. methods: {
  123. // -----------蓝牙报错提示
  124. blueError(code) {
  125. switch (code) {
  126. case 10000:
  127. this.errorToast('未初始化蓝牙适配器,请返回重新连接')
  128. break
  129. case 10001:
  130. this.errorToast('当前蓝牙适配器不可用')
  131. break
  132. case 10002:
  133. this.errorToast('没有找到指定设备')
  134. break
  135. case 10003:
  136. this.errorToast('连接失败')
  137. break
  138. case 10004:
  139. this.errorToast('没有找到指定服务,请返回重新连接')
  140. break
  141. case 10005:
  142. this.errorToast('没有找到指定特征值')
  143. break
  144. case 10006:
  145. this.errorToast('当前连接已断开')
  146. break
  147. case 10007:
  148. this.errorToast('当前特征值不支持此操作')
  149. break
  150. case 10008:
  151. this.errorToast('其余所有系统上报的异常')
  152. break
  153. case 10009:
  154. this.errorToast('Android 系统特有,系统版本低于 4.3 不支持 BLE')
  155. break
  156. case 10010:
  157. this.errorToast('已连接')
  158. break
  159. case 10011:
  160. this.errorToast('配对设备需要配对码')
  161. break
  162. case 10012:
  163. this.errorToast('连接超时')
  164. break
  165. case 10013:
  166. this.errorToast('连接 deviceld 为空或者是格式不正确')
  167. break
  168. }
  169. },
  170. // -----------统一错误封装
  171. errorToast(err) {
  172. uni.showToast({
  173. title: `${err}`,
  174. icon: 'none',
  175. duration: 2500
  176. })
  177. },
  178. // -----------------返回蓝牙匹配
  179. back() {
  180. uni.$emit('updateData', true)
  181. uni.navigateBack({
  182. delta: 1
  183. })
  184. },
  185. // ------------------获取设备数据列表
  186. async getTypeList() {
  187. const {
  188. data: res
  189. } = await uni.$http.post('v2/api-device/device/type_list')
  190. // console.log(JSON.parse(JSON.stringify(res.data.list)), '获取的数据返回');
  191. // 筛选出无创水光
  192. for (let key in res.data.list) {
  193. if (res.data.list[key].id != 1 && res.data.list[key].id != 15 && res.data.list[key].id != 10) {
  194. this.typeList.push(res.data.list[key])
  195. }
  196. }
  197. // 将数据保存到本地
  198. uni.setStorageSync('typeList', this.typeList)
  199. },
  200. // ------------------选择设备配置
  201. selectOne(type, index, item) {
  202. switch (type) {
  203. case 'type_sn':
  204. this.selectTypeIndex = index
  205. this.selectDetectorsIndex = 0
  206. this.selectModeIndex = 0
  207. this.selectDetectorsIndexA = 99
  208. this.selectModeIndexA = 99
  209. this.equipmentNum = '' //重置探头
  210. this.modeNum = '' //重置模式或强度
  211. console.log(item)
  212. //赋值哪个厂家的设备
  213. if (item.type_sn.includes('ZK')) {
  214. this.TYPE_SN = 'ZK'
  215. } else {
  216. this.TYPE_SN = 'LD'
  217. }
  218. break
  219. case 'detectors':
  220. this.selectDetectorsIndex = index
  221. this.selectDetectorsIndexA = index
  222. this.selectModeIndexA = 99
  223. this.equipmentNum = Number(item.detector_sn).toString(16) < 10 ? '0' + Number(item.detector_sn)
  224. .toString(16) : Number(item.detector_sn).toString(16) //设备探头编号(16进制)
  225. // console.log(this.equipmentNum)
  226. console.log(item)
  227. this.minClass = item.config.power.min //最小值
  228. this.maxClass = item.config.power.max //最大值
  229. this.incrementClass = item.config.power.increment //增幅
  230. this.modeClass = item.config.power.default //默认值
  231. break
  232. case 'mode':
  233. this.selectModeIndex = index
  234. this.selectModeIndexA = index
  235. this.modeNum = Number(item.value).toString(16) < 10 ? '0' + Number(item.value).toString(16) :
  236. Number(item.value).toString(16)
  237. // console.log(this.modeNum)
  238. break
  239. }
  240. },
  241. // -----------------开始计时
  242. begin() {
  243. if (!this.istimer) {
  244. this.timer = setInterval(this.startTimer, 1000);
  245. this.istimer = true
  246. }
  247. },
  248. //------------暂停计时
  249. pauseTime() {
  250. if (this.timer) {
  251. clearInterval(this.timer);
  252. this.istimer = false
  253. }
  254. },
  255. // -------------结束计时
  256. finishTime() {
  257. if (this.timer) {
  258. clearInterval(this.timer);
  259. this.istimer = false
  260. this.time = '00:00:00'
  261. this.startTime = ''
  262. this.seconds = 0
  263. this.minutes = 0
  264. this.hour = 0
  265. }
  266. },
  267. // -----------------计时器
  268. startTimer() {
  269. this.seconds += 1;
  270. if (this.seconds >= 60) {
  271. this.seconds = 0;
  272. this.minutes = Number(this.minutes) + 1;
  273. }
  274. if (this.minutes >= 60) {
  275. this.minutes = 0;
  276. this.hour = Number(this.hour) + 1;
  277. }
  278. this.time = (this.hour < 10 ? '0' + this.hour : this.hour) + ':' + (this.minutes < 10 ? '0' + this
  279. .minutes :
  280. this.minutes) + ':' + (this.seconds < 10 ? '0' + this.seconds : this.seconds);
  281. },
  282. // ----------------------操作设备开启暂停结束
  283. operate(type) {
  284. // 强度编号
  285. let modeNum = Number(this.modeClass).toString(16) < 10 ? '0' + Number(this.modeClass).toString(16) :
  286. Number(this.modeClass).toString(16)
  287. // 设备模式编号
  288. if (this.typeList[this.selectTypeIndex].detectors[this.selectDetectorsIndex].config.mode?.list || this
  289. .typeList[this.selectTypeIndex].detectors[this.selectDetectorsIndex].config.power?.list) {
  290. modeNum = this.modeNum
  291. }
  292. // console.log(modeNum);
  293. // 蓝牙指令操作码
  294. let code =
  295. `AABB12343838373244363235444138${this.equipmentNum}03${type=='open'?this.START:this.PAUSE}${modeNum}0101`
  296. // 判断是否是(中科)厂家的设备
  297. if (this.TYPE_SN == 'ZK') {
  298. code = `a9800100${this.equipmentNum}${modeNum}0101${type=='open'?this.START:this.PAUSE}3cc3`
  299. }
  300. if (!this.equipmentNum) {
  301. uni.showToast({
  302. title: '请选择探头',
  303. icon: "none"
  304. })
  305. return
  306. }
  307. if (this.typeList[this.selectTypeIndex].detectors[this.selectDetectorsIndex].config.mode?.list || this
  308. .typeList[this.selectTypeIndex].detectors[this.selectDetectorsIndex].config.power?.list) {
  309. if (this.selectModeIndexA == 99) {
  310. uni.showToast({
  311. title: '请选择模式',
  312. icon: "none"
  313. })
  314. return
  315. }
  316. }
  317. // 操作 开始 暂停 结束
  318. if (type == 'open') {
  319. this.send(code, '启动成功')
  320. } else if (type == 'pause') {
  321. this.send(code, '暂停成功')
  322. } else {
  323. this.send(code, '结束操作')
  324. }
  325. },
  326. // --------------减少强度
  327. reduce() {
  328. this.modeClass = this.modeClass - this.incrementClass
  329. if (this.modeClass <= this.minClass) {
  330. this.modeClass = this.minClass
  331. return
  332. }
  333. },
  334. //---------------增加强度
  335. add() {
  336. this.modeClass = Number(this.modeClass) + Number(this.incrementClass)
  337. if (this.modeClass >= this.maxClass) {
  338. this.modeClass = this.maxClass
  339. return
  340. }
  341. },
  342. // --------------排气
  343. pqCLick() {
  344. // 判断是否是排气指令(16)进制
  345. this.send(this.PQCODE, '启动成功')
  346. },
  347. // -------------------发送数据
  348. send(newMsg, title = '执行成功') {
  349. console.log(newMsg);
  350. wx.showLoading({
  351. title: '正在启动',
  352. mask: true
  353. })
  354. // 向蓝牙设备发送一个0x00的16进制数据
  355. let that = this
  356. let buffer = new ArrayBuffer(Math.ceil(newMsg.length / 2))
  357. let dataView = new DataView(buffer)
  358. var index = 0
  359. for (var i = 0; i < newMsg.length; i++) {
  360. if (i % 2 < 1) {
  361. dataView.setUint8(index, '0x' + newMsg.substr(i, 2))
  362. index++
  363. }
  364. }
  365. // console.log(that.deviceId, that.serviceId, that.characteristicId);
  366. // return
  367. uni.writeBLECharacteristicValue({
  368. deviceId: that.deviceId, // 设备ID
  369. serviceId: that.serviceId, // 服务UUID
  370. characteristicId: that.characteristicId, // 特征值
  371. value: buffer,
  372. success(res) {
  373. if (newMsg != that.PQCODE || newMsg != that.PQCLOSECODE) {
  374. uni.hideLoading()
  375. uni.showToast({
  376. title,
  377. icon: 'none',
  378. })
  379. }
  380. // ------排气指令
  381. if (newMsg == that.PQCODE) {
  382. uni.showLoading({
  383. title: '正在排气',
  384. mask: true
  385. })
  386. setTimeout(() => {
  387. console.log(1);
  388. that.send(that.PQCLOSECODE, '排气完成')
  389. }, 2000)
  390. }
  391. // ------排气指令
  392. if (newMsg == that.PQCLOSECODE) {
  393. uni.hideLoading()
  394. uni.showToast({
  395. title: '排气完成',
  396. icon: 'success'
  397. })
  398. }
  399. // 开始计时
  400. if (title == '启动成功') {
  401. that.begin()
  402. if (this.startTime == '') {
  403. this.startTime = formatDate(new Date(), 'YYYY/MM/DD/hh/mm/ss')
  404. }
  405. } else if (title == '暂停成功' || title == '排气完成') {
  406. that.pauseTime()
  407. } else {
  408. that.finishTime()
  409. }
  410. console.log(res, '发送数据成功')
  411. },
  412. fail(err) {
  413. uni.hideLoading()
  414. that.blueError(err.errCode)
  415. console.error(err)
  416. }
  417. })
  418. },
  419. }
  420. }
  421. </script>
  422. <style lang="less" scoped>
  423. .top {
  424. padding-left: 30px;
  425. span {
  426. font-size: 16px;
  427. }
  428. .title {
  429. font-weight: 700;
  430. margin-right: 40px;
  431. }
  432. .id {
  433. margin-right: 20px;
  434. }
  435. .startTime {
  436. margin: 0 20px;
  437. }
  438. .totalTime {
  439. margin: 0 20px;
  440. }
  441. }
  442. .type {
  443. margin-left: 100px;
  444. margin-top: 40px;
  445. width: 85%;
  446. .pattern {
  447. display: flex;
  448. overflow-x: scroll;
  449. .facilityType {
  450. margin-right: 80px;
  451. }
  452. }
  453. .buttonOP {
  454. display: flex;
  455. .item {
  456. padding-right: 20px;
  457. padding-left: 6px;
  458. height: 100px;
  459. background-color: #b8b8b8;
  460. margin-right: 20px;
  461. border-radius: 24px;
  462. overflow: hidden;
  463. display: flex;
  464. justify-content: center;
  465. align-items: center;
  466. img {
  467. width: 90px;
  468. height: 90px;
  469. border-radius: 24px;
  470. margin-right: 20px;
  471. }
  472. }
  473. button {
  474. flex-shrink: 0;
  475. margin: 0;
  476. background-color: #b8b8b8;
  477. color: #fff;
  478. margin-right: 50px;
  479. }
  480. .selectType {
  481. background-color: #1e98d7;
  482. }
  483. }
  484. .buttonOPO {
  485. display: flex;
  486. align-items: center;
  487. button {
  488. flex-shrink: 0;
  489. margin: 0;
  490. background-color: #f8c126;
  491. color: #fff;
  492. }
  493. .strength {
  494. width: 200px;
  495. background-color: #b8b8b8;
  496. padding: 14px;
  497. text-align: center;
  498. border-radius: 12px;
  499. margin: 0 4px;
  500. }
  501. }
  502. .facilityType {
  503. p {
  504. font-size: 18px;
  505. margin-bottom: 30px;
  506. margin-top: 30px;
  507. }
  508. }
  509. }
  510. .bottomBox {
  511. width: 90%;
  512. display: flex;
  513. margin-top: 100px;
  514. margin-left: 30px;
  515. justify-content: space-between;
  516. .back {
  517. button {
  518. background-color: #1e98d7;
  519. color: #fff;
  520. }
  521. }
  522. .bottomBox-right {
  523. display: flex;
  524. button {
  525. margin-right: 20px;
  526. width: 120px;
  527. color: #fff;
  528. }
  529. .open {
  530. background-color: #03bf16;
  531. }
  532. .pause {
  533. background-color: #1e98d7;
  534. }
  535. .finish {
  536. background-color: #d9001b;
  537. }
  538. }
  539. }
  540. </style>