index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <view class="container">
  3. <!-- 头部区域 -->
  4. <view class="header">
  5. <u-row>
  6. <u-col span="2">
  7. <view class="status-btn" :class="{ active_btn: communicationLink_one }">Link</view>
  8. </u-col>
  9. <u-col span="2">
  10. <view class="status-btn" :class="{ active_btn: bleConnected }">blue</view>
  11. </u-col>
  12. <u-col span="6" textAlign="left">
  13. <text class="title">长沙亿旭智能</text>
  14. </u-col>
  15. <u-col span="2">
  16. </u-col>
  17. </u-row>
  18. </view>
  19. <!-- 主要内容区域 -->
  20. <view class="main-content">
  21. <!-- 显示区域 -->
  22. <view class="display-area">
  23. <view class="display-window">
  24. <view
  25. v-for="(item, index) in deviceListDataShow"
  26. :key="item.id"
  27. :class="['list-item', { 'list-item-selected': item.id === selectedDeviceId }]"
  28. hover-class="list-item-hover"
  29. hover-start-time="0"
  30. hover-stay-time="100"
  31. @click="selectblue(item.id)"
  32. >
  33. <image v-if="item.manufacturer==='eciot'" src="/static/img/ecble.png" class="list-item-img"></image>
  34. <text class="list-item-name">{{item.id}}:{{ item.name }}&nbsp;</text>
  35. <image v-if="item.rssi >= -41" src="/static/img/s5.png" mode="aspectFit" class="list-item-rssi-img"></image>
  36. <image v-else-if="item.rssi >= -55" src="/static/img/s4.png" mode="aspectFit"
  37. class="list-item-rssi-img"></image>
  38. <image v-else-if="item.rssi >= -65" src="/static/img/s3.png" mode="aspectFit"
  39. class="list-item-rssi-img"></image>
  40. <image v-else-if="item.rssi >= -75" src="/static/img/s2.png" mode="aspectFit"
  41. class="list-item-rssi-img"></image>
  42. <image v-else="item.rssi < -75" src="/static/img/s1.png" mode="aspectFit"
  43. class="list-item-rssi-img"></image>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- 控制按钮区域 -->
  48. <view class="control-area">
  49. <!-- 蓝牙控制行 -->
  50. <view class="control-row">
  51. <u-button type="info" size="medium" class="control-btn" @click="closeBlue()">断开蓝牙</u-button>
  52. <u-button type="info" size="medium" class="control-btn" @click="openBluetoothAdapter()">搜索蓝牙</u-button>
  53. <u-button type="info" size="medium" class="control-btn" @click="listViewTap()">连接蓝牙</u-button>
  54. <u-button type="info" size="medium" class="control-btn" @click="readAddress()">读取地址</u-button>
  55. </view>
  56. <!-- 设备地址行 -->
  57. <u-row gutter="10" customStyle="margin-bottom: 10px">
  58. <u-col span="3">
  59. <view class="label-box">设备地址:</view>
  60. </u-col>
  61. <u-col span="5">
  62. <u-input v-model="deviceAddress" type="number" class="address-input"></u-input>
  63. </u-col>
  64. <u-col span="4">
  65. <u-button type="info" size="medium" class="control-btn" @click="updataAddress()">更新通讯地址</u-button>
  66. </u-col>
  67. </u-row>
  68. <!-- 新设备地址行 -->
  69. <u-row gutter="10" customStyle="margin-bottom: 10px">
  70. <u-col span="3">
  71. <view class="label-box">设备新地址:</view>
  72. </u-col>
  73. <u-col span="5">
  74. <u-input v-model="newDeviceAddress" type="number" placeholder="" class="address-input"></u-input>
  75. </u-col>
  76. <u-col span="4">
  77. <u-button type="info" size="medium" class="control-btn" @click="writeAddress()">写地址</u-button>
  78. </u-col>
  79. </u-row>
  80. </view>
  81. </view>
  82. </view>
  83. </template>
  84. <script>
  85. import {ecBLE,ecUI,initBLE,writeRegister,startHeartbeat,stopHeartbeat,setGlobalSlaveAddress,getGlobalSlaveAddress} from '@/utils/modbus.js';
  86. import i18 from '@/utils/i18.js';
  87. let deviceListData = [];
  88. let ctx;
  89. export default {
  90. data() {
  91. return {
  92. editStatus: false,
  93. selectedDeviceId: null, //选中的设备
  94. timer: "", //设备列表数据定时刷新
  95. deviceListDataShow: [
  96. {
  97. id: 1,
  98. name: "设备1",
  99. rssi: -55
  100. },
  101. {
  102. id: 2,
  103. name: "设备2",
  104. rssi: -55
  105. },
  106. ], //蓝牙设备列表
  107. showTimer: null,
  108. connected: false,
  109. sendData: "",
  110. newDeviceAddress:'',
  111. deviceAddress:'',
  112. communicationLink_one: false,
  113. communicationTimer: null, // 添加这一行用于保存定时器 ID
  114. }
  115. },
  116. computed: {
  117. bleConnected() {
  118. return this.$store.getters['ble/connected']
  119. },
  120. bleData() {
  121. return this.$store.getters['ble/data']
  122. }
  123. },
  124. watch: {
  125. bleData: {
  126. handler(newData, oldData) {
  127. if (newData) {
  128. console.log('接收到蓝牙数据:------------------')
  129. // 清除之前的定时器
  130. if (this.communicationTimer) {
  131. console.log('清除之前的定时器');
  132. clearTimeout(this.communicationTimer)
  133. this.communicationTimer = null
  134. }
  135. // 设置通信链接状态为 true
  136. this.communicationLink_one = true
  137. this.updateSensorData(newData)
  138. }
  139. },
  140. deep: true, // 深度监听,确保嵌套对象变化时也能触发
  141. immediate: true // 立即执行一次
  142. }
  143. },
  144. onLoad() {
  145. uni.setNavigationBarTitle({
  146. title: "蓝牙配网"
  147. })
  148. ecUI.showLoading("正在初始化蓝牙模块")
  149. ctx = this;
  150. clearInterval(this.timer);
  151. this.timer = setInterval(() => {
  152. ctx.deviceListDataShow = JSON.parse(JSON.stringify(deviceListData))
  153. }, 800)
  154. initBLE();
  155. },
  156. onUnload() {
  157. ecBLE.stopBluetoothDevicesDiscovery();
  158. ecBLE.closeBLEConnection()
  159. },
  160. onShow() {
  161. if (this.showTimer != null) {
  162. clearTimeout(this.showTimer);
  163. }
  164. this.showTimer = setTimeout(() => {
  165. ctx.openBluetoothAdapter()
  166. }, 100)
  167. },
  168. methods: {
  169. readAddress(){
  170. writeRegister("GET_ADDRESS", null);
  171. },
  172. closeBlue(){
  173. ecUI.showLoading("正在断开并重新扫描");
  174. ecBLE.closeBLEConnection();
  175. stopHeartbeat();
  176. // 重新开始蓝牙扫描以获取设备列表
  177. deviceListData = []; // 清空当前设备列表
  178. this.startBluetoothDevicesDiscovery(); // 开始重新扫描
  179. // 可选:在一段时间后隐藏加载提示
  180. setTimeout(() => {
  181. this.communicationLink_one = false
  182. ecUI.hideLoading();
  183. }, 2000);
  184. },
  185. updateSensorData(data) {
  186. console.log('接收到的数据地址:', data.Addres_23);
  187. this.deviceAddress = data.Addres_23;
  188. this.communicationTimer = setTimeout(() => {
  189. console.log('通信链接已结束')
  190. this.communicationLink_one = false
  191. this.communicationTimer = null
  192. }, 5000)
  193. },
  194. updataAddress(){
  195. if (this.deviceAddress == null || this.deviceAddress == '') {
  196. this.$modal.showToast("请输入通讯地址");
  197. return;
  198. }
  199. let value = parseInt(this.deviceAddress, 10);
  200. setGlobalSlaveAddress(value);
  201. startHeartbeat();
  202. },
  203. writeAddress(){
  204. ecUI.showLoading("正在写入地址,请稍后!")
  205. stopHeartbeat();
  206. setTimeout(() => {
  207. writeRegister("WRITE_ADDRESS",this.newDeviceAddress);
  208. }, 1000);
  209. setTimeout(() => {
  210. this.communicationLink_one = false
  211. ecUI.hideLoading();
  212. }, 2000);
  213. },
  214. i18(text) {
  215. return text;
  216. },
  217. $t(title) {
  218. return title;
  219. },
  220. selectblue(id) { //选中需要连接的蓝牙
  221. this.selectedDeviceId = id;
  222. },
  223. listViewTap() { //连接蓝牙
  224. const id = this.selectedDeviceId;
  225. // 校验设备ID
  226. if (!id) {
  227. this.$modal.showToast("请选择一个设备");
  228. return;
  229. }
  230. // 清除旧的连接状态
  231. this.connected = false;
  232. ecUI.showLoading("正在连接蓝牙");
  233. // 建立连接
  234. ecBLE.createBLEConnection(id);
  235. // 设置连接超时
  236. setTimeout(() => {
  237. this.handleConnectionTimeout();
  238. }, 5000);
  239. },
  240. // 超时处理函数
  241. handleConnectionTimeout() {
  242. this.connected = this.bleConnected;
  243. ecUI.hideLoading();
  244. if (!this.connected) {
  245. this.$modal.showToast(i18('连接失败'));
  246. this.startBluetoothDevicesDiscovery();
  247. }
  248. },
  249. openBluetoothAdapter() { //打开蓝牙
  250. ecBLE.onBluetoothAdapterStateChange(res => {
  251. ecUI.hideLoading()
  252. if (res.ok) {
  253. ctx.startBluetoothDevicesDiscovery()
  254. } else {
  255. ecUI.showModal(
  256. this.$t('buletooth.tip'),
  257. `Bluetooth adapter error | ${res.errCode} | ${res.errMsg}`,
  258. () => {
  259. }
  260. )
  261. }
  262. })
  263. ecBLE.openBluetoothAdapter()
  264. },
  265. startBluetoothDevicesDiscovery() {
  266. ecBLE.stopBluetoothDevicesDiscovery();
  267. console.log('start search')
  268. ecBLE.onBluetoothDeviceFound(res => {
  269. let isRight = true;
  270. if (!isRight) {
  271. return;
  272. }
  273. for (const item of deviceListData) {
  274. if (item.id === res.id) {
  275. item.name = res.name
  276. item.rssi = res.rssi
  277. return
  278. }
  279. }
  280. let manufacturer = ''
  281. if (res.name.length === 11 && res.name.startsWith('@')) {
  282. manufacturer = 'eciot'
  283. }
  284. if (res.name.length === 15 && res.name.startsWith('BT_')) {
  285. manufacturer = 'eciot'
  286. }
  287. manufacturer = 'eciot'
  288. deviceListData.push({
  289. id: res.id,
  290. name: res.name,
  291. rssi: res.rssi,
  292. manufacturer,
  293. })
  294. })
  295. ecBLE.startBluetoothDevicesDiscovery()
  296. },
  297. }
  298. }
  299. </script>
  300. <style lang="scss" >
  301. .container {
  302. height: calc(100vh - 100px);
  303. background-color: #f5f5f5;
  304. display: flex;
  305. flex-direction: column;
  306. }
  307. // 头部样式
  308. .header {
  309. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  310. padding: 20rpx;
  311. .status-btn {
  312. background-color: #ff4757;
  313. border: none;
  314. margin-left: 0px;
  315. padding: 0px;
  316. width: 40px;
  317. text-align: center;
  318. color: white;
  319. border-radius: 4px;
  320. height: 20px;
  321. line-height: 20px;
  322. }
  323. .active_btn {
  324. background-color: yellowgreen !important;
  325. }
  326. .title {
  327. color: #fff;
  328. font-size: 36rpx;
  329. font-weight: bold;
  330. }
  331. }
  332. // 主要内容区域
  333. .main-content {
  334. flex: 1;
  335. padding: 15px;
  336. display: flex;
  337. flex-direction: column;
  338. gap: 20px;
  339. }
  340. // 显示区域
  341. .display-area {
  342. .display-window {
  343. width: 100%;
  344. height: 230px;
  345. background-color: #000;
  346. border: 2px solid #ddd;
  347. border-radius: 8px;
  348. overflow: auto;
  349. }
  350. }
  351. // 控制区域
  352. .control-area {
  353. display: flex;
  354. flex-direction: column;
  355. gap: 15px;
  356. margin-top: 10px;
  357. }
  358. .control-row {
  359. display: flex;
  360. align-items: center;
  361. gap: 10px;
  362. margin-bottom: 10px;
  363. .control-btn {
  364. flex: 1;
  365. background-color: white;
  366. color: #333;
  367. border: 1px solid #ddd;
  368. border-radius: 6px;
  369. font-size: 14px;
  370. height: 40px;
  371. font-size: 13px;
  372. }
  373. .label-box {
  374. width: 80px;
  375. }
  376. .address-input {
  377. flex: 1;
  378. background-color: white;
  379. border: 1px solid #ddd;
  380. border-radius: 6px;
  381. height: 40px;
  382. padding: 0 12px;
  383. }
  384. }
  385. .list-item {
  386. height: 40px;
  387. background-color: #000;
  388. color: #fff;
  389. position: relative;
  390. }
  391. .list-item-selected {
  392. background-color: #767a82;
  393. color: #000;
  394. }
  395. .list-item-name {
  396. position: absolute;
  397. font-size: 16px;
  398. left: 20px;
  399. top: 0px;
  400. line-height: 40px;
  401. color: #fff;
  402. max-width: 80%;
  403. overflow-x: hidden;
  404. }
  405. .list-item-rssi-img {
  406. position: absolute;
  407. width: 20px;
  408. height: 20px;
  409. right: 20px;
  410. top: 13px;
  411. }
  412. .list-item-rssi {
  413. position: absolute;
  414. width: 40px;
  415. height: 40px;
  416. right: 0px;
  417. font-size: 12px;
  418. font-weight: bold;
  419. display: flex;
  420. justify-content: center;
  421. color: white;
  422. line-height: 50px;
  423. }
  424. .list-item-line {
  425. position: absolute;
  426. height: 1px;
  427. width: 100vw;
  428. left: 20px;
  429. background-color: #c6c6c8;
  430. }
  431. ::v-deep .u-button--medium{
  432. font-size: 13px !important;
  433. }
  434. </style>