edit.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 100010:
  157. this.errorToast('已连接')
  158. break
  159. case 100011:
  160. this.errorToast('配对设备需要配对码')
  161. break
  162. case 100012:
  163. this.errorToast('连接超时')
  164. break
  165. case 100013:
  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.navigateBack()
  181. },
  182. // ------------------获取设备数据列表
  183. async getTypeList() {
  184. const {
  185. data: res
  186. } = await uni.$http.post('v2/api-device/device/type_list')
  187. // console.log(JSON.parse(JSON.stringify(res.data.list)), '获取的数据返回');
  188. // 筛选出无创水光
  189. for (let key in res.data.list) {
  190. if (res.data.list[key].id != 1 && res.data.list[key].id != 15) {
  191. this.typeList.push(res.data.list[key])
  192. }
  193. }
  194. // 将数据保存到本地
  195. uni.setStorageSync('typeList', this.typeList)
  196. },
  197. // ------------------选择设备配置
  198. selectOne(type, index, item) {
  199. switch (type) {
  200. case 'type_sn':
  201. this.selectTypeIndex = index
  202. this.selectDetectorsIndex = 0
  203. this.selectModeIndex = 0
  204. this.selectDetectorsIndexA = 99
  205. this.selectModeIndexA = 99
  206. this.equipmentNum = '' //重置探头
  207. this.modeNum = '' //重置模式或强度
  208. // console.log(item)
  209. //赋值哪个厂家的设备
  210. if (item.type_sn.includes('ZK')) {
  211. this.TYPE_SN = 'ZK'
  212. } else {
  213. this.TYPE_SN = 'LD'
  214. }
  215. break
  216. case 'detectors':
  217. this.selectDetectorsIndex = index
  218. this.selectDetectorsIndexA = index
  219. this.selectModeIndexA = 99
  220. this.equipmentNum = Number(item.detector_sn).toString(16) < 10 ? '0' + Number(item.detector_sn)
  221. .toString(16) : Number(item.detector_sn).toString(16) //设备探头编号(16进制)
  222. // console.log(this.equipmentNum)
  223. // console.log(item)
  224. this.minClass = item.config.power.min //最小值
  225. this.maxClass = item.config.power.max //最大值
  226. this.incrementClass = item.config.power.increment //增幅
  227. this.modeClass = item.config.power.default //默认值
  228. break
  229. case 'mode':
  230. this.selectModeIndex = index
  231. this.selectModeIndexA = index
  232. this.modeNum = Number(item.value).toString(16) < 10 ? '0' + Number(item.value).toString(16) :
  233. Number(item.value).toString(16)
  234. // console.log(this.modeNum)
  235. break
  236. }
  237. },
  238. // -----------------开始计时
  239. begin() {
  240. if (!this.istimer) {
  241. this.timer = setInterval(this.startTimer, 1000);
  242. this.istimer = true
  243. }
  244. },
  245. //------------暂停计时
  246. pauseTime() {
  247. if (this.timer) {
  248. clearInterval(this.timer);
  249. this.istimer = false
  250. }
  251. },
  252. // -------------结束计时
  253. finishTime() {
  254. if (this.timer) {
  255. clearInterval(this.timer);
  256. this.istimer = false
  257. this.time = '00:00:00'
  258. this.startTime = ''
  259. this.seconds = 0
  260. this.minutes = 0
  261. this.hour = 0
  262. }
  263. },
  264. // -----------------计时器
  265. startTimer() {
  266. this.seconds += 1;
  267. if (this.seconds >= 60) {
  268. this.seconds = 0;
  269. this.minutes = Number(this.minutes) + 1;
  270. }
  271. if (this.minutes >= 60) {
  272. this.minutes = 0;
  273. this.hour = Number(this.hour) + 1;
  274. }
  275. this.time = (this.hour < 10 ? '0' + this.hour : this.hour) + ':' + (this.minutes < 10 ? '0' + this
  276. .minutes :
  277. this.minutes) + ':' + (this.seconds < 10 ? '0' + this.seconds : this.seconds);
  278. },
  279. // ----------------------操作设备开启暂停结束
  280. operate(type) {
  281. // 强度编号
  282. let modeNum = Number(this.modeClass).toString(16) < 10 ? '0' + Number(this.modeClass).toString(16) :
  283. Number(this.modeClass).toString(16)
  284. // 设备模式编号
  285. if (this.typeList[this.selectTypeIndex].detectors[this.selectDetectorsIndex].config.mode?.list || this
  286. .typeList[this.selectTypeIndex].detectors[this.selectDetectorsIndex].config.power?.list) {
  287. modeNum = this.modeNum
  288. }
  289. // console.log(modeNum);
  290. // 蓝牙指令操作码
  291. let code =
  292. `AABB12343838373244363235444138${this.equipmentNum}03${type=='open'?this.START:this.PAUSE}${modeNum}0101`
  293. // 判断是否是(中科)厂家的设备
  294. if (this.TYPE_SN == 'ZK') {
  295. code = `a9800100${this.equipmentNum}${modeNum}0101${type=='open'?this.START:this.PAUSE}3cc3`
  296. }
  297. if (!this.equipmentNum) {
  298. uni.showToast({
  299. title: '请选择探头',
  300. icon: "none"
  301. })
  302. return
  303. }
  304. if (this.typeList[this.selectTypeIndex].detectors[this.selectDetectorsIndex].config.mode?.list || this
  305. .typeList[this.selectTypeIndex].detectors[this.selectDetectorsIndex].config.power?.list) {
  306. if (this.selectModeIndexA == 99) {
  307. uni.showToast({
  308. title: '请选择模式',
  309. icon: "none"
  310. })
  311. return
  312. }
  313. }
  314. // 操作 开始 暂停 结束
  315. if (type == 'open') {
  316. this.send(code, '启动成功')
  317. } else if (type == 'pause') {
  318. this.send(code, '暂停成功')
  319. } else {
  320. this.send(code, '结束操作')
  321. }
  322. },
  323. // --------------减少强度
  324. reduce() {
  325. this.modeClass = this.modeClass - this.incrementClass
  326. if (this.modeClass <= this.minClass) {
  327. this.modeClass = this.minClass
  328. return
  329. }
  330. },
  331. //---------------增加强度
  332. add() {
  333. this.modeClass = Number(this.modeClass) + Number(this.incrementClass)
  334. if (this.modeClass >= this.maxClass) {
  335. this.modeClass = this.maxClass
  336. return
  337. }
  338. },
  339. // --------------排气
  340. pqCLick() {
  341. // 判断是否是排气指令(16)进制
  342. this.send(this.PQCODE, '启动成功')
  343. },
  344. // -------------------发送数据
  345. send(newMsg, title = '执行成功') {
  346. console.log(newMsg);
  347. wx.showLoading({
  348. title: '正在启动',
  349. mask: true
  350. })
  351. // 向蓝牙设备发送一个0x00的16进制数据
  352. let that = this
  353. let buffer = new ArrayBuffer(Math.ceil(newMsg.length / 2))
  354. let dataView = new DataView(buffer)
  355. var index = 0
  356. for (var i = 0; i < newMsg.length; i++) {
  357. if (i % 2 < 1) {
  358. dataView.setUint8(index, '0x' + newMsg.substr(i, 2))
  359. index++
  360. }
  361. }
  362. // console.log(that.deviceId, that.serviceId, that.characteristicId);
  363. // return
  364. uni.writeBLECharacteristicValue({
  365. deviceId: that.deviceId, // 设备ID
  366. serviceId: that.serviceId, // 服务UUID
  367. characteristicId: that.characteristicId, // 特征值
  368. value: buffer,
  369. success(res) {
  370. if (newMsg != that.PQCODE || newMsg != that.PQCLOSECODE) {
  371. uni.hideLoading()
  372. uni.showToast({
  373. title,
  374. icon: 'none',
  375. })
  376. }
  377. // ------排气指令
  378. if (newMsg == that.PQCODE) {
  379. uni.showLoading({
  380. title: '正在排气',
  381. mask: true
  382. })
  383. setTimeout(() => {
  384. console.log(1);
  385. that.send(that.PQCLOSECODE, '排气完成')
  386. }, 2000)
  387. }
  388. // ------排气指令
  389. if (newMsg == that.PQCLOSECODE) {
  390. uni.hideLoading()
  391. uni.showToast({
  392. title: '排气完成',
  393. icon: 'success'
  394. })
  395. }
  396. // 开始计时
  397. if (title == '启动成功') {
  398. that.begin()
  399. if (this.startTime == '') {
  400. this.startTime = formatDate(new Date(), 'YYYY/MM/DD/hh/mm/ss')
  401. }
  402. } else if (title == '暂停成功' || title == '排气完成') {
  403. that.pauseTime()
  404. } else {
  405. that.finishTime()
  406. }
  407. console.log(res, '发送数据成功')
  408. },
  409. fail(err) {
  410. uni.hideLoading()
  411. that.blueError(err.errCode)
  412. console.error(err)
  413. }
  414. })
  415. },
  416. }
  417. }
  418. </script>
  419. <style lang="less" scoped>
  420. .top {
  421. padding-left: 30px;
  422. span {
  423. font-size: 16px;
  424. }
  425. .title {
  426. font-weight: 700;
  427. margin-right: 40px;
  428. }
  429. .id {
  430. margin-right: 20px;
  431. }
  432. .startTime {
  433. margin: 0 20px;
  434. }
  435. .totalTime {
  436. margin: 0 20px;
  437. }
  438. }
  439. .type {
  440. margin-left: 100px;
  441. margin-top: 40px;
  442. width: 85%;
  443. .pattern {
  444. display: flex;
  445. overflow-x: scroll;
  446. .facilityType {
  447. margin-right: 80px;
  448. }
  449. }
  450. .buttonOP {
  451. display: flex;
  452. .item {
  453. padding-right: 20px;
  454. padding-left: 6px;
  455. height: 100px;
  456. background-color: #b8b8b8;
  457. margin-right: 20px;
  458. border-radius: 24px;
  459. overflow: hidden;
  460. display: flex;
  461. justify-content: center;
  462. align-items: center;
  463. img {
  464. width: 90px;
  465. height: 90px;
  466. border-radius: 24px;
  467. margin-right: 20px;
  468. }
  469. }
  470. button {
  471. flex-shrink: 0;
  472. margin: 0;
  473. background-color: #b8b8b8;
  474. color: #fff;
  475. margin-right: 50px;
  476. }
  477. .selectType {
  478. background-color: #1e98d7;
  479. }
  480. }
  481. .buttonOPO {
  482. display: flex;
  483. align-items: center;
  484. button {
  485. flex-shrink: 0;
  486. margin: 0;
  487. background-color: #f8c126;
  488. color: #fff;
  489. }
  490. .strength {
  491. width: 200px;
  492. background-color: #b8b8b8;
  493. padding: 14px;
  494. text-align: center;
  495. border-radius: 12px;
  496. margin: 0 4px;
  497. }
  498. }
  499. .facilityType {
  500. p {
  501. font-size: 18px;
  502. margin-bottom: 30px;
  503. margin-top: 30px;
  504. }
  505. }
  506. }
  507. .bottomBox {
  508. width: 90%;
  509. display: flex;
  510. margin-top: 100px;
  511. margin-left: 30px;
  512. justify-content: space-between;
  513. .back {
  514. button {
  515. background-color: #1e98d7;
  516. color: #fff;
  517. }
  518. }
  519. .bottomBox-right {
  520. display: flex;
  521. button {
  522. margin-right: 20px;
  523. width: 120px;
  524. color: #fff;
  525. }
  526. .open {
  527. background-color: #03bf16;
  528. }
  529. .pause {
  530. background-color: #1e98d7;
  531. }
  532. .finish {
  533. background-color: #d9001b;
  534. }
  535. }
  536. }
  537. </style>