| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- <template>
- <view class="container">
- <!-- 头部区域 -->
- <BluetoothHeader
- @ble-data-received="handleBleDataReceived"
- />
-
-
- <view class="main-content">
- <!-- 接收窗口 -->
- <view class="receive-section">
- <text class="section-title">接收窗口</text>
- <view class="receive-window">
- <scroll-view scroll-y="true" class="receive-textarea">
- <view
- v-for="(log, index) in sortedWriteLogs"
- :key="index"
- class="log-item"
- >
- <view class="log-time">{{log.time}}{{log.data}}</view>
- </view>
- </scroll-view>
- </view>
- </view>
- <!-- 输入控制区域 -->
- <view class="input-section">
- <u-row customStyle="margin-bottom: 10px" gutter="10">
- <u-col span="8">
- <u-input
- v-model="inputValue"
- placeholder="输入指令"
- class="command-input"
- ></u-input>
- </u-col>
- <u-col span="4">
- <u-checkbox-group
- placement="column">
- <u-checkbox v-model="cmdChecked" label="cmd" @change="changeCmd"></u-checkbox>
- </u-checkbox-group>
- </u-col>
- </u-row>
- <!-- 操作按钮 -->
- <u-row justify="end" customStyle="margin-bottom: 10px">
- <u-col span="4">
- <u-button type="info" size="mini" class="write-btn" @click="sendMsg">指令发送</u-button>
- </u-col>
- </u-row>
- <u-row customStyle="margin-bottom: 10px" gutter="10">
- <u-col span="6">
- <u-button text="清空发送" @click="removeLogsByType('rx')"></u-button>
- </u-col>
- <u-col span="6">
- <u-button text="清空接受" @click="removeLogsByType('tx')"></u-button>
- </u-col>
- </u-row>
- </view>
- </view>
- <DeviceStatusInfo/>
- </view>
- </template>
- <script>
- import ecBLEApp from '@/utils/ecBLE/ecBLEApp.js';
- import {getAgreement} from '@/utils/modbus.js';
- import BluetoothHeader from '@/pages/components/header.vue';
- import DeviceStatusInfo from '@/pages/components/DeviceStatusInfo.vue';
- export default {
- components: {BluetoothHeader,DeviceStatusInfo},
- data() {
- return {
- agreementTypeName:'',
- communicationLink: false,
- communicationTimer_3: null, // 添加这一行用于保存定时器 ID
- writeLogs: [],
- showSex: false,
- checkboxValue1: [],
- inputValue: '',
- cmdChecked: false,
- carriageReturnChecked: false,
- wtypeValue: '',
- wtypeChecked: false,
- componentType: '单面',
- activeTab: 'receive',
- componentList: [
- { name: '单面'},
- { name: '双面'},
- { name: '多面'}
- ]
- }
- },
- onShow() {
- // 从全局变量加载日志
- this.writeLogs = ecBLEApp.getGlobalWriteLogs()
- // 添加监听器以实时更新
- ecBLEApp.addLogListener(this.onLogUpdate);
- this.agreementTypeName = getAgreement();
- },
- onHide() {
- // 移除监听器
- ecBLEApp.removeLogListener(this.onLogUpdate)
- },
- computed: {
- // 修改排序计算属性
- sortedWriteLogs() {
- return this.writeLogs.slice().sort((a, b) => {
- return new Date(b.time) - new Date(a.time); // 倒序排列
- });
- }
- },
- methods: {
- // 处理从子组件传递过来的蓝牙数据
- handleBleDataReceived(data) {
- console.log('在父组件中接收到蓝牙数据:', data);
- this.updateSensorData(data);
- },
- updateSensorData(data) {
- },
- sendMsg(){
- uni.showModal({
- title: '提示',
- content: `是否确定发送消息`+this.inputValue+`?`,
- success: (res) => {
- if (res.confirm) {
- ecBLEApp.writeBLECharacteristicValueTwo(this.inputValue,!this.cmdChecked);
- }
- }
- });
- },
- changeCmd(e) {
- this.cmdChecked = e
- },
- changeWtype(e){
- this.wtypeChecked = e
- },
- // 日志更新回调
- onLogUpdate(logs) {
- // this.writeLogs = [...logs] // 创建新数组以触发响应式更新
- this.writeLogs.splice(0, this.writeLogs.length, ...logs)
- },
- loadLogs() {
- // 从全局变量获取日志
- const newLogs = ecBLEApp.getGlobalWriteLogs()
- // 使用数组变更方法保持响应式
- this.writeLogs.splice(0, this.writeLogs.length, ...newLogs)
- },
- selectValue(e) {
- this.componentType = e.name
- },
- switchTab(tab) {
- this.activeTab = tab;
- },
- sendCommand() {
- // 发送指令逻辑
- console.log('发送指令:', this.inputValue);
- },
- writeType() {
- // 类型写入逻辑
- console.log('写入类型:', this.wtypeValue);
- },
- clearSend() {
- this.inputValue = '';
- },
- clearReceive() {
- // 清空接收窗口
- console.log('清空接收窗口');
- },
- removeLogsByType(type) {
- uni.showModal({
- title: '确认删除',
- content: `确定要删除所有类型为 '${type}' 的日志吗?`,
- success: (res) => {
- if (res.confirm) {
- const result = ecBLEApp.removeWriteDataByType(type)
- if (result.success) {
- uni.showToast({
- title: `已删除${result.deletedCount}条日志`,
- icon: 'success'
- })
- // 重新加载日志
- this.loadLogs();
- console.log('删除日志结果:', result)
- console.log(this.writeLogs)
- } else {
- uni.showToast({
- title: '删除失败',
- icon: 'none'
- })
- }
- }
- }
- })
- },
- }
- }
- </script>
- <style lang="scss" >
- @import '@/static/scss/custom-theme.scss';
- .container {
- height: calc(100vh - 120px);
- display: flex;
- flex-direction: column;
- background-color: #f5f5f5;
- }
- .status-bar {
- height: 60rpx;
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 0 20rpx;
- color: white;
- font-size: 24rpx;
- }
- .status-left {
- display: flex;
- align-items: center;
- gap: 20rpx;
- }
- .status-center {
- flex: 1;
- text-align: center;
- }
- .app-title {
- font-size: 32rpx;
- font-weight: bold;
- }
- .status-right {
- display: flex;
- align-items: center;
- gap: 10rpx;
- }
- .network-info {
- font-size: 20rpx;
- }
- .signal {
- font-size: 20rpx;
- }
- .signal-bars {
- display: flex;
- gap: 2rpx;
- }
- .bar {
- width: 4rpx;
- height: 12rpx;
- background-color: white;
- border-radius: 1rpx;
- }
- .battery {
- font-size: 20rpx;
- }
- .main-content {
- flex: 1;
- padding: 20rpx;
- background-color: #f5f5f5;
- height: calc(100% - 140rpx);
- }
- .receive-section {
- margin-bottom: 30rpx;
- }
- .section-title {
- font-size: 28rpx;
- font-weight: bold;
- color: #333;
- margin-bottom: 10rpx;
- display: block;
- }
- .receive-window {
- background-color: white;
- border-radius: 10rpx;
- border: 2rpx solid #e0e0e0;
- height: 40vh;
- overflow: auto;
- }
- .receive-textarea {
- height: 100%;
- padding: 20rpx;
- }
- .placeholder-text {
- color: #999;
- font-size: 24rpx;
- }
- .input-section {
- background-color: white;
- border-radius: 10rpx;
- padding: 20rpx;
- }
- .input-row {
- display: flex;
- align-items: center;
- margin-bottom: 20rpx;
- gap: 20rpx;
- }
- .input-group {
- display: flex;
- align-items: center;
- flex: 1;
- gap: 10rpx;
- }
- .command-input, .wtype-input {
- flex: 1;
- }
- .scroll-btn {
- width: 60rpx;
- height: 60rpx;
- }
- .checkbox-group {
- display: flex;
- align-items: center;
- }
- .send-btn {
- width: 120rpx;
- }
- .component-section {
- margin-bottom: 20rpx;
- }
- .component-select {
- width: 100%;
- }
- .wtype-section {
- margin-bottom: 20rpx;
- }
- .write-btn {
- padding: 2px;
- color: #a7a7a7;
- margin: 5px;
- height: 30px;
- }
- .action-buttons {
- display: flex;
- gap: 20rpx;
- margin-bottom: 20rpx;
- }
- .clear-btn {
- flex: 1;
- }
- .device-info {
- display: flex;
- align-items: center;
- gap: 10rpx;
- margin-bottom: 10px;
- }
- .info-label {
- font-size: 28rpx;
- color: #333;
- }
- .info-value {
- font-size: 28rpx;
- color: #666;
- }
- ::v-deep .input-section .u-button--mini{
- width: 100px !important;
- height: 30px !important;
- }
- .typeSelect{
- font-size: 16px;
- color: #fff;
- font-weight: 600;
- display: flex;
- }
- </style>
|