uniapp 小程序AP配网

news/2024/7/20 2:16:20 标签: uni-app, 小程序

一、TCPSocket.js 封装TCP协议

class socket {
  constructor() {
    this.connection = {};
  }
  // 创建一个TCP实例
  establish(monitor) {
    this.connection = wx.createTCPSocket();
    this.connection.connect({ address: "192.168.1.1", port: 9100 });
  }
  // 发送消息
  connect(message) {
    this.connection.onConnect(() => {
      this.connection.write(message);
    });
  }
  // 监听端口函数
  onMessage(success, failure) {
    this.connection.onMessage((res) => {
      success(res)
    });
    this.connection.onError((err) => {
      failure(err);
    });
  }

  // 关闭搜索事件
  TCPclose() {
    setTimeout(() => {
      // 3S后,关闭socket
      this.connection.close(() => {
        console.log("TCP关闭");
      });
    }, 3000);
  }
}
const TCPSocket = new socket();
export default TCPSocket;

 二、vue文件中使用TCP

<template>
  <view class="zai-box">
        <view class="container">
          <text class="textName">{{ wifiSSID }}</text>
          <input type="text" placeholder="请输入WLAN密码" v-model="password" />
        </view>
        <button
          class="loginButton"
          :loading="loadingWIfi"
          :disabled="JSON.stringify(password.length) < 8 || loadingWIfi"
          @click="settiing"
        >
          {{ loadingWIfi ? "连接中" : "连接" }}
        </button>
  </view>
</template>

<script>
import TCP from "@/common/util/TCPSocket.js";
export default {
  data() {
    return {
      wifiSSID: "WIFI name",
      password: "",
      isFirst: true,
      wifiCountInterval: null,
      wifiCountDown: 0,
      loadingWIfi: false,
    };
  },
  onHide() {
    this.clearInterval();
  },
  methods: {
    settiing() {
      this.loadingWIfi = true;
      this.onConfirm();
    },
    onConfirm() {
      this.wifiCountDown = 60;
      this.startSMSTimer();
      if (this.isFirst) {
        // TCP实例创建
        // 连接端口
        TCP.establish();
        this.isFirst = false;
      }

      // 请求设备上报状态
      this.udpsend();
      // 开始监听
      TCP.onMessage(
        (res) => {
          if (!res) return;
          // 关闭TCP连接
          TCP.TCPclose();
          console.log(res) // 查看返回数据
          
        },
        (err) => {
          // debug
          console.log(err, "err");
        }
      );
    },
    udpsend() {
      const msgData = {
        event: "network",
        data: {
          ssid: this.wifiSSID,
          password: this.password,
        },
      };
      TCP.connect(JSON.stringify(msgData));
    },
    startSMSTimer() {
      this.wifiCountInterval = setInterval(() => {
        this.wifiCountDown--;
        if (this.wifiCountDown <= 0) {
          clearInterval(this.wifiCountInterval);
          this.wifiCountInterval = null;
          this.$tip.navigateTo("/pagesA/deviceAPNet/APWifiFail");
        }
      }, 1000);
    },
    clearInterval() {
      if (this.wifiCountInterval) {
        clearInterval(this.wifiCountInterval);
        this.wifiCountInterval = null;
      }
    },
  },
};
</script>

<style scoped>
.zai-box {
  padding: 0;
  margin: 0;
}

.container {
  padding: 30rpx;
  margin: 0 0 20rpx;
  font-size: 28rpx;
  color: #20212b;
  background-color: #fff;
  text-align: left;
  font-weight: 400;
}

.loginButton {
  font-size: 36rpx;
  width: 690rpx;
  color: #fff;
  background-color: #00a0e9;
  border-radius: 44rpx;
  margin: 60rpx auto 0;
  border: none;
}

.loginButton[disabled] {
  color: #fff;
  background-color: #dfdfdf;
}

button::after {
  border: none;
}

.textName {
  display: block;
  font-size: 28rpx;
  font-weight: 500;
  color: #20212b;
  margin-bottom: 30rpx;
}
</style>


http://www.niftyadmin.cn/n/5409253.html

相关文章

Yolov8有效涨点,添加多种注意力机制,修改损失函数提高目标检测准确率

目录 简介 CBAM注意力机制原理及代码实现 原理 代码实现 GAM注意力机制 原理 代码实现 修改损失函数 YAML文件 完整代码 &#x1f680;&#x1f680;&#x1f680;订阅专栏&#xff0c;更新及时查看不迷路&#x1f680;&#x1f680;&#x1f680; http://t.csdnimg.c…

AWS ECR(AWS云里面的docker镜像私库)

问题 上一篇文章&#xff0c;在AWS云上面部署了k8s集群&#xff0c;这次接下来&#xff0c;需要在一个docker镜像私库。 步骤 创建docker镜像私库 打开AWS ECR主页&#xff0c;创建一个docker镜像私库&#xff0c;如下图&#xff1a; 设置私有镜像库名称&#xff0c;直接创…

【项目】Boost 搜索引擎

文章目录 1.背景2.宏观原理3.相关技术与开发环境4. 实现原理1.下载2.加载与解析文件2.1获取指定目录下的所有网页文件2.2. 获取网页文件中的关键信息2.3. 对读取文件进行保存 3.索引3.1正排与倒排3.2获取正排和倒排索引3.3建立索引3.3.1正排索引3.3.2倒排索引 4.搜索4.1 初始化…

将组件直接绑定到vue实例上面的写法

怎么将组件直接绑定到vue实例上面&#xff1f; 在实际开发过程中&#xff0c;有多种使用vue组件的方式&#xff0c;有在组件中引入&#xff0c;直接挂载到vue进行全局使用&#xff0c;也有直接挂载到vue实例上面当成vue的一个属性来使用的。下面通过代码来实现将组件直接绑定到…

运用Tensorflow进行目标检测

对象检测是一种计算机视觉技术&#xff0c;它使软件系统能够从给定的图像或视频中检测、定位并跟踪物体。对象检测的一个特殊属性是它能识别对象的类别&#xff08;如人、桌子、椅子等&#xff09;并在给定图像中指出其具体位置坐标。这个位置通常通过在物体周围绘制一个边界框…

JavaWeb03-HTTP协议,Tomcat,Servlet

目录 一、HTTP协议 1.概述 2.特点 3.请求数据格式 &#xff08;1&#xff09;请求行 &#xff08;2&#xff09;请求头 &#xff08;3&#xff09;请求体 &#xff08;4&#xff09;常见请求头 &#xff08;5&#xff09;GET和POST请求区别 4.响应数据格式 &#xf…

代码训练LeetCode(1)合并有序数组详解

代码训练(1)LeetCode之合并两个有序数组 Author: Once Day Date: 2024年3月5日 漫漫长路&#xff0c;才刚刚开始… 全系列文章可参考专栏: 十年代码训练_Once-Day的博客-CSDN博客 参考文章: 88. 合并两个有序数组 - 力扣&#xff08;LeetCode&#xff09;力扣 (LeetCode) …

Redis核心数据结构之SDS(二)

SDS与C字符串的区别 杜绝缓冲区溢出 除了获取字符串长度的复杂度高之外&#xff0c;C字符串不记录自身长度带来的另一个问题是 容易造成缓冲区溢出(buffer overflow).例如<string.h>/strcat函数可以将src字符串 中的内容拼接到dest字符串的末尾: char *strcat(char *d…