edit.vue 16 KB

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