12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181 |
- const app = getApp();
- const {
- getBanner,
- listByDevice
- } = require('../../utils/api.js');
- import timeUtil from '../../utils/time_util.js';
- import strings from '../../utils/strings.js';
- import store from '../../utils/store.js';
- import route_constant from '../../utils/route_constant.js';
- import route_util from '../../utils/route_util.js';
- import {
- BtHelper
- } from '../../devices/bt_helper.js';
- Page({
- data: {
- nvabarData: {
- showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
- title: 'OhPlay', //导航栏 中间的标题
- },
- ///下午好
- isLogin: false,
- greeting: "",
- bannerList: [{
- "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20241028142233669038262.png"
- },
- {
- "pic": "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20240823145816541223911.png"
- }
- ],
- autoplay: true,
- interval: 3000, // 切换时间间隔
- duration: 500, // 滑动动画时长
- circular: true, // 衔接滑动
- indexPage: 0,
- ///是否展示频道
- showChannel: false,
- ////首页
- navBarHeight: app.globalData.navBarHeight,
- MenuButtonheight: app.globalData.MenuButtonheight,
- MenuButtonTop: app.globalData.MenuButtonTop,
- actionIndex: null,
- luoma: ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", " Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"],
- channelData: [],
- deviceList: [],
- deviceListIndex: null,
- isOneLoading: true,
- uid: null,
- isSetWake: false,
- thisDeviceMac: null,
- battery: 4, // 0≤电量<20,0格
- ////我的界面
- loginStatus: true,
- nickName: "未登录",
- userPic: './../../img/head_pic.png',
- intervalId: null,
- autoConnected: false,
- },
- onLoad(options) {
- var that = this;
- that.onDeviceLoad();
- that.onUserInfoLoad();
- that.onBannerLoad();
- },
- onShow() {
- var that = this;
- that.onHomeShow();
- that.onBannerShow();
- },
- onHomeShow() {
- var that = this;
- var userInfo = wx.getStorageSync("userInfo") || "";
- if (!strings.isEmpty(userInfo)) {
- // 获取设备本地数据
- if (app.globalData.client == null) {
- app.connect();
- } else if (that.data.deviceListIndex == null && that.data.deviceList.length > 0) {
- that.actionDevice(0)
- }
- } else {
- if (app.globalData.client == null) {
- app.connect();
- }
- }
- },
- onDeviceLoad() {
- var that = this;
- BtHelper.getInstance().initBluetoothAdapter();
- var l = store.getStore("deviceList");
- if (!strings.isEmpty(l)) {
- that.updateDeviceList(l, true);
- }
- ///监听蓝牙设备
- BtHelper.getInstance().getBluetoothDevices();
- ///3秒去处理一下
- BtHelper.getInstance().startScan(null, null, null);
- var isFirst = true;
- ///再秒再对比一次
- that.data.intervalId = setInterval(async function () {
- isFirst = false;
- that.compareList();
- }, isFirst ? 6 * 1000 : 12 * 1000);
- },
- async compareList() {
- var that = this;
- var deviceList = that.data.deviceList;
- if (deviceList.length > 0) {
- if (BtHelper.getInstance().getCallBackConnect() == null) {
- var compareList = BtHelper.getInstance().getCompareList();
- var dissmissDevice = BtHelper.getInstance().getDissmissDevice();
- ///去掉未连接的离线的
- for (var i = 0; i < dissmissDevice.length; i++) {
- for (var j = 0; j < compareList.length; j++) {
- if (compareList[j].deviceId == dissmissDevice[i].deviceId) {
- compareList.splice(j, 1);
- break;
- }
- }
- }
- if (compareList.length > 0) {
- ///对比在线的蓝牙设备
- var isChanged = false;
- for (var i = 0; i < deviceList.length; i++) {
- var tempItem = deviceList[i];
- if (tempItem.connectType != 3) {
- var has = false;
- for (var j = 0; j < compareList.length; j++) {
- // "state":"online" "offline" MW-SR1(4G_WIFI)
- if (tempItem.deviceId === compareList[j].deviceId) {
- has = true;
- break;
- }
- }
- if (has) {
- if (tempItem.state != "online") {
- isChanged = true;
- tempItem.state = "online";
- }
- } else {
- if (tempItem.state != "offline") {
- isChanged = true;
- tempItem.state = "offline";
- }
- }
- }
- }
- if (isChanged) {
- that.updateDeviceList(deviceList, false);
- }
- ///没有连接则连接第一个在线的蓝牙
- if (!that.data.autoConnected) {
- var dList = that.data.deviceList;
- if (dList.length > 0) {
- for (var i = 0; i < dList.length; i++) {
- var item = dList[i];
- if (item.connectType == 1 && item.state == "online") {
- item.name = item.devName;
- if (that.data.deviceListIndex == null) {
- BtHelper.getInstance().connect(item, function (data) {
- if (data) {
- that.addConnectBlueDevice(item);
- }
- });
- }
- break;
- }
- }
- }
- }
- }
- BtHelper.getInstance().startScan(null, null, null);
- }
- }
- },
- // 回调
- mqttCallback(type, option) {
- // console.log("gadsfadsfadsfa==888===" + type);
- var that = this;
- let payloads = null;
- if (option) {
- payloads = JSON.parse(option.payload);
- };
- switch (type) {
- ///先走订阅
- case "connect":
- that.subscribeDevicesStatus();
- break;
- ///再走有在线
- case "message_onoffline":
- that.onlineDevice(payloads);
- break;
- case "message":
- ///获取频道数据
- ///锁定播放哪一个频道
- if (payloads.type === "get_position" && payloads.other) {
- let actionIndex = null;
- that.data.channelData.map((v, index) => {
- if (v.channelNum === payloads.other.channel) {
- actionIndex = index;
- }
- });
- that.setData({
- actionIndex,
- });
- }
- ///获取播放状态
- else if (payloads.type === "play" || payloads.type === "play_state") {
- var deviceList = that.data.deviceList;
- var deviceListIndex = that.data.deviceListIndex;
- if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
- return
- };
- var deviceId = deviceList[deviceListIndex].deviceId;
- // 接收设备当前播放状态
- const obj = {
- DstDeviceName: that.getThisDeviceID(deviceId)
- }
- app.PubMsg({
- type: "get_position",
- ...obj
- });
- }
- ///获取频道列表数据
- else if (payloads.type === "get_dev_info") {
- // 接收设备当前信息
- that.getchannelData(payloads.other.ProdModel);
- // 电量
- that.setData({
- battery: that._battery(payloads.other.Power),
- })
- // 当前设备木有设置定时
- that.setData({
- isSetWake: false,
- });
- payloads.other.alarm.map((v) => {
- if (v.enable === "1") {
- that.setData({
- isSetWake: true
- });
- }
- });
- // 更新信息
- ///连上就调用2次
- var isUpdate = false;
- // payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
- var deviceList = that.data.deviceList;
- if (!strings.isEmpty(deviceList)) {
- for (var i = 0; i < deviceList.length; i++) {
- if (payloads.SrcDeviceName) {
- var deviceId = deviceList[i].deviceId;
- var splitDeviceId = deviceId.split("BLUFI_");
- if (splitDeviceId.length == 2) {
- var index = payloads.SrcDeviceName.indexOf(splitDeviceId[1]);
- if (index !== -1 && deviceList[i].ProdModel != payloads.other.ProdModel && deviceList[i].devName != payloads.other.devName) {
- isUpdate = true;
- deviceList[i].ProdModel = payloads.other.ProdModel;
- deviceList[i].devName = payloads.other.devName;
- break;
- }
- }
- }
- }
- }
- ///数据有更新
- if (isUpdate) {
- that.updateDeviceList(deviceList, false);
- }
- } else if (payloads.type === "battery" && payloads.other) {
- that.setData({
- battery: that._battery(payloads.other.battery),
- })
- }
- break;
- default:
- }
- },
- // 格式化电量
- _battery(battery) {
- let _battery = 0;
- if (battery < 20) {
- _battery = 0
- } else if (20 <= battery && battery < 40) {
- _battery = 1
- } else if (40 <= battery && battery < 60) {
- _battery = 2
- } else if (60 <= battery && battery < 80) {
- _battery = 3
- } else if (80 <= battery && battery <= 100) {
- _battery = 4
- } else if (battery > 100) {
- _battery = 5
- };
- return _battery
- },
- // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
- ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
- /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
- // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
- onlineDevice(payloads) {
- // 设置在线状态
- var that = this;
- // console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
- ///是否更新过在线离线状态
- var isUpdate = false;
- var deviceList = that.data.deviceList;
- if (!strings.isEmpty(deviceList)) {
- for (var i = 0; i < deviceList.length; i++) {
- if (payloads.uuid) {
- var deviceId = deviceList[i].deviceId;
- var splitDeviceId = deviceId.split("BLUFI_");
- if (splitDeviceId.length == 2) {
- var index = payloads.uuid.indexOf(splitDeviceId[1]);
- if (index !== -1) {
- if (deviceList[i].state != payloads.state) {
- isUpdate = true;
- deviceList[i].state = payloads.state;
- break;
- }
- }
- }
- }
- }
- }
- ///数据有更新
- if (isUpdate) {
- that.updateDeviceList(deviceList, false);
- }
- ///当前没有连接设备,则去连接第一个wifi设备
- var deviceListIndex = that.data.deviceListIndex;
- if (deviceListIndex === null) {
- var list = that.data.deviceList;
- for (var i = 0; i < list.length; i++) {
- if (list[i].state === "online" && list[i].connectType == 3) {
- if (that.data.isLogin) {
- that.actionDevice(i);
- }
- break;
- }
- }
- } else {
- // 当前播放设备离线
- if (that.data.deviceList.length > deviceListIndex && that.data.deviceList[deviceListIndex].state !== "online") {
- that.setData({
- actionIndex: null,
- deviceListIndex: null,
- });
- };
- }
- },
- // 订阅设备在线状态
- subscribeDevicesStatus() {
- var that = this;
- var deviceList = that.data.deviceList;
- if (!strings.isEmpty(deviceList)) {
- for (var i = 0; i < deviceList.length; i++) {
- if (deviceList[i].connectType == 3) {
- let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
- app.subscribe(topic);
- }
- }
- }
- },
- subscribeCurrDevice() {
- var that = this;
- if (!(app.globalData.client && app.globalData.client.connected)) {
- console.log("未连接MQTT服务器");
- const str = setInterval(() => {
- clearInterval(str);
- that.subscribeCurrDevice();
- }, 500);
- return;
- };
- var deviceList = that.data.deviceList;
- var deviceListIndex = that.data.deviceListIndex;
- if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
- return
- };
- var device = that.data.deviceList[deviceListIndex];
- var tempDeviceId = device.deviceId;
- ///删除当前选中这个
- deviceList.splice(deviceListIndex, 1);
- ///添加到第一个去
- deviceList.unshift(device);
- that.setData({
- deviceListIndex: 0,
- autoConnected: true,
- deviceList: deviceList,
- });
- let topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
- app.subscribe(topic);
- const obj = {
- DstDeviceName: that.getThisDeviceID(tempDeviceId)
- };
- app.PubMsg({
- type: "get_dev_info",
- ...obj
- });
- },
- actionMusic(e) {
- var that = this;
- if (e.currentTarget.dataset.index === that.data.actionIndex) {
- return;
- };
- if (that.data.deviceListIndex === null) {
- wx.showToast({
- title: '请选择设备',
- icon: "none"
- })
- return;
- };
- that.setData({
- actionIndex: e.currentTarget.dataset.index
- });
- var deviceList = that.data.deviceList;
- var deviceListIndex = that.data.deviceListIndex;
- if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
- return
- };
- var deviceId = deviceList[deviceListIndex].deviceId;
- const other = {
- "url": "",
- "media_data": "",
- "user_id": `${app.globalData.userInfo.deviceUid}`,
- "timestamp": `${Math.round(new Date() / 1000)}`,
- "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
- "order": "",
- "resource_from": "",
- "songAlbumID": "",
- "version": 3,
- "is_debug": app.globalData.is_debug
- };
- app.PubMsg({
- type: "play",
- DstDeviceName: that.getThisDeviceID(deviceId),
- other
- });
- },
- getThisDeviceID(deviceId) {
- return `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
- },
- getchannelData(clientType) {
- var that = this;
- var deviceList = that.data.deviceList;
- var deviceListIndex = that.data.deviceListIndex;
- if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
- return;
- };
- var deviceId = deviceList[deviceListIndex].deviceId;
- listByDevice({
- clientType: clientType,
- unShowLoad: true,
- }).then((res) => {
- that.setData({
- channelData: res
- });
- // 接收设备当前播放状态
- const obj = {
- DstDeviceName: that.getThisDeviceID(deviceId)
- }
- app.PubMsg({
- type: "get_position",
- ...obj
- });
- wx.setStorageSync("channelData", res);
- })
- },
- ///去频道详情
- onTapToChannel() {
- var that = this;
- if (that.data.channelData.length > that.data.actionIndex) {
- wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
- wx.navigateTo({
- url: './../channelDetails/channelDetails'
- });
- }
- },
- onTapIndex(e) {
- var that = this;
- var index = e.currentTarget.dataset.index;
- var indexPage = that.data.indexPage;
- if (indexPage != index) {
- that.setData({
- indexPage: index,
- });
- }
- },
- goMeAbout() {
- wx.navigateTo({
- url: './../about/about',
- })
- },
- onTapLogin() {
- var that = this;
- ///退出登录
- if (that.data.isLogin) {
- that.logOut();
- }
- // 登录
- else {
- route_util.jump(route_constant.login);
- }
- },
- ///退出登录
- logOut() {
- var that = this;
- ///有设备在线被选中,则让它不被选择
- var deviceListIndex = that.data.deviceListIndex;
- if (deviceListIndex != null) {
- var deviceList = that.data.deviceList;
- if (deviceList.length > deviceListIndex) {
- if (deviceList[deviceListIndex].connectType == 3) {
- if (that.data.thisDeviceMac !== null) {
- app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
- that.setData({
- actionIndex: null,
- deviceListIndex: null,
- });
- };
- }
- }
- }
- wx.removeStorageSync('userInfo');
- wx.removeStorageSync('token');
- var nickName = "未登录";
- var greeting = that.getGreetBuNickName(nickName);
- that.setData({
- isLogin: false,
- greeting: greeting,
- nickName: nickName,
- userPic: "./../../img/head_pic.png",
- });
- },
- ///添加设备
- addDevice() {
- var that = this;
- if (!that.data.isLogin) {
- route_util.jump(route_constant.login);
- return;
- }
- ///跳转设备列表
- route_util.jump(route_constant.deviceList);
- },
- ///点击item
- // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
- onTapItem(e) {
- var that = this;
- if (!that.data.isLogin) {
- route_util.jump(route_constant.login);
- return;
- }
- var item = e.currentTarget.dataset.item;
- console.log("点击item:", item)
- var connectType = item.connectType;
- // wifi只支持在线点击
- if (connectType == 3 && item.state === "online") {
- if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
- that.goWake();
- } else {
- that.actionDevice(e.currentTarget.dataset.index)
- }
- return;
- }
- if (item.connectType != "1") {
- return
- }
- ///去蓝牙连接处理
- if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
- // 蓝牙才能连接
- route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
- } else if (item.state === "offline") {
- console.log("去连接蓝牙")
- } else {
- ///item
- // {"connectType":1,"deviceId":"E4:9F:80:09:40:EC","name":"E4:9F:80:09:40:EC","state":"online","ProdModel":"MW-SR1(4G_WIFI)","devName":"猫王音响x阿基米德 SR1 妙播收音机"}
- item.name = item.devName;
- BtHelper.getInstance().connect(item, function (data) {
- if (data) {
- that.addConnectBlueDevice(item);
- }
- });
- }
- },
- actionDevice(index) {
- var that = this;
- if (!that.data.isLogin) {
- return;
- }
- if (that.data.deviceList.length <= index) {
- return;
- };
- const device = that.data.deviceList[index];
- if (device.state !== "online") {
- return;
- };
- // 取消订阅
- if (that.data.thisDeviceMac !== null) {
- app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
- };
- that.setData({
- deviceListIndex: index,
- autoConnected: true,
- thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
- });
- // app.PubMsg({
- // type: "get_dev_info",
- // DstDeviceName: that.getThisDeviceID()
- // });
- that.subscribeCurrDevice();
- },
- goWake() {
- var that = this;
- if (that.data.deviceListIndex === null) {
- return;
- };
- console.log("gadsfqwwerqwerqweqr====" + JSON.stringify(that.data.deviceList[that.data.deviceListIndex]));
- wx.navigateTo({
- url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
- });
- },
- ///点击banner事件
- onTapBanner() {
- var that = this;
- var item = e.currentTarget.dataset.item;
- },
- ///删除当前设备
- deleteDevice(e) {
- var that = this;
- var index = e.currentTarget.dataset.index;
- wx.showModal({
- title: '确定删除?',
- success: function (res) {
- if (res.confirm) {
- if (that.data.deviceList[index].connectType == 3) {
- that.cancelWifi(index, false);
- } else {
- that.cancelBlue(index, false);
- }
- }
- }
- });
- },
- ///是否是同一个蓝牙
- isTheSameBlue(connectDevice) {
- var that = this;
- var deviceListIndex = that.data.deviceListIndex;
- if (deviceListIndex == null) {
- return false;
- }
- var deviceList = that.data.deviceList;
- if (deviceList.length > deviceListIndex) {
- if (deviceList[deviceListIndex].connectType == 3) {
- return false;
- } else {
- if (deviceList[deviceListIndex].deviceId == connectDevice.deviceId) {
- return true;
- } else {
- return false;
- }
- }
- } else {
- return false;
- }
- },
- ///断开当前的
- async cancelCurrent() {
- var that = this;
- var deviceListIndex = that.data.deviceListIndex;
- if (deviceListIndex == null) {
- return;
- }
- var deviceList = that.data.deviceList;
- if (deviceList.length > deviceListIndex) {
- if (deviceList[deviceListIndex].connectType == 3) {
- await that.cancelWifi(deviceListIndex, true);
- } else {
- await that.cancelBlue(deviceListIndex, true);
- }
- }
- },
- /// 断开蓝牙连接
- async cancelBlue(index, onlyCancel) {
- var that = this;
- // await BtHelper.getInstance().disconnect();
- if (onlyCancel) {
- return;
- }
- const id = that.data.deviceList[index].deviceId;
- const deviceList = that.data.deviceList.filter((item, i) => {
- return id !== item.deviceId
- });
- store.setStore("deviceList", deviceList);
- that.setData({
- deviceList,
- });
- // 当前没有设备
- // if (deviceList.length === 0) {
- that.setData({
- deviceListIndex: null,
- });
- // }
- },
- /// 断开连接wifi
- async cancelWifi(index, onlyCancel) {
- var that = this;
- // 取消订阅
- app.unsubscribe(`/AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}/user/pub_response`);
- if (onlyCancel) {
- return;
- }
- const id = that.data.deviceList[index].deviceId;
- let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
- const deviceList = that.data.deviceList.filter((item, i) => {
- return id !== item.deviceId
- });
- store.setStore("deviceList", deviceList);
- that.setData({
- deviceList,
- thisDeviceMac: null
- });
- // 当前没有设备
- // if (deviceList.length === 0) {
- that.setData({
- actionIndex: null,
- deviceListIndex: null,
- });
- // }
- if (that.data.deviceListIndex === null) {
- return
- };
- if (index === that.data.deviceListIndex) {
- let index_ = null;
- deviceList.map((v, index) => {
- if (v.state === "online" && index_ === null) {
- index_ = index;
- }
- });
- if (index_ !== null) {
- that.actionDevice(index_);
- } else {
- that.setData({
- actionIndex: null,
- deviceListIndex: null,
- });
- };
- } else {
- deviceList.map((v, index) => {
- if (v.name === name) {
- that.setData({
- deviceListIndex: index,
- autoConnected: true,
- });
- }
- });
- }
- },
- // {"applicationType":"[0, 1]","deviceId":"DB:45:DD:76:42:15","img":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100711728016597.png","offlineImg":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100714667384264.png","connectImg":null,"name":"猫王音响·小王子 OTR-X","bluetoothName":"猫王音响·小王子 OTR-X","bluetoothNames":["猫王音响·小王子 OTR-X"],"isChannelsPlatforms":0,"platform":-1,"typeList":[{"is5g":0,"type":1,"connectType":1,"functionList":[1,3,6],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100644913162836.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100648938942906.png","icon":null,"guideUrl":null}},{"is5g":0,"type":2,"connectType":3,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155903515728925.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155910706032704.png","icon":null,"guideUrl":null}}],"clientType":"MW-2AX(WIFI)","firstVersion":"0.0.1","filter":null,"guideUrl":null,"manufacturer":"ShanJing","deviceType":0,"mac":"9b45dd76e2150000",
- /// deviceList
- /// 连接方式:bt-0,ble-1,upnp-2,mqtt-3
- /// clientType
- // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
- ///新添加蓝牙设备
- addConnectBlueDevice(newDevice) {
- var that = this;
- // 同一个设备
- var deviceList = that.data.deviceList;
- var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
- if (tempList && tempList.length > 0) {
- deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
- };
- console.log("添加蓝牙设备:", newDevice);
- deviceList.unshift({
- /// 蓝牙ble连接
- connectType: 1,
- deviceId: newDevice.deviceId,
- name: newDevice.name,
- state: "online",
- ProdModel: newDevice.ProdModel || newDevice.clientType,
- clientType: newDevice.clientType || newDevice.ProdModel,
- devName: newDevice.name,
- mac: newDevice.mac,
- img: newDevice.img,
- });
- ///在线排序前面,wifi设备排序前面
- that.updateDeviceList(deviceList, false);
- var indexPage = that.data.indexPage;
- if (indexPage != 0) {
- that.setData({
- indexPage: 0,
- deviceListIndex: 0,
- autoConnected: true,
- });
- } else {
- that.setData({
- deviceListIndex: 0,
- autoConnected: true,
- });
- }
- },
- ///新添加wifi设备
- async addConnectWifiDevice(newDevice) {
- var that = this;
- // 同一个设备
- var deviceList = that.data.deviceList;
- var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
- if (tempList && tempList.length > 0) {
- deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
- };
- console.log("添加Wifi设备:" + JSON.stringify(newDevice));
- // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
- deviceList.unshift({
- /// Wifi连接
- connectType: 3,
- deviceId: newDevice.deviceId,
- name: "猫王小王子OTR-X",
- state: "online",
- ProdModel: 3,
- devName: "猫王小王子OTR-X",
- mac: newDevice.deviceId,
- image: "./../../img/min.png",
- });
- ///在线排序前面,wifi设备排序前面
- that.updateDeviceList(deviceList, false);
- var indexPage = that.data.indexPage;
- if (indexPage != 0) {
- that.setData({
- indexPage: 0,
- });
- }
- ///断开蓝牙连接
- // await BtHelper.getInstance().disconnect();
- that.actionDevice(0);
- },
- ///更新列表排序
- updateDeviceList(deviceList, isInit) {
- if (deviceList.length <= 0) {
- return;
- }
- var that = this;
- var finalList = [];
- if (isInit) {
- deviceList[0].state = "offline";
- }
- var isFirstOnline = false;
- if (deviceList[0].state == "online") {
- isFirstOnline = true;
- finalList.push(deviceList[0]);
- }
- ///区分在线和离线
- for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
- if (isInit) {
- deviceList[i].state = "offline";
- }
- }
- var onLineList = [];
- var onNoLineList = [];
- ///添加在线的
- for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
- var device = deviceList[i];
- if (device.state == "online") {
- onLineList.push(device)
- }
- }
- ///添加离线的
- for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
- var device = deviceList[i];
- if (device.state != "online") {
- onNoLineList.push(device)
- }
- }
- // 区分在线wifi和蓝牙 wifi在前 离线在后
- var onLineWifiList = [];
- var onLineBlueList = [];
- ///添加在线wifi
- onLineList.forEach(element => {
- if (element.connectType == 3) {
- onLineWifiList.push(element);
- }
- });
- ///添加在线蓝牙
- onLineList.forEach(element => {
- if (element.connectType != 3) {
- onLineBlueList.push(element)
- }
- });
- finalList = finalList.concat(onLineWifiList);
- finalList = finalList.concat(onLineBlueList);
- ///只需要蓝牙和wifi在线的
- let mDeviceList = []
- mDeviceList = mDeviceList.concat(onLineWifiList);
- mDeviceList = mDeviceList.concat(onLineBlueList);
- getApp().globalData.mDeviceList = mDeviceList;
- ///区分离线wifi和蓝牙 wifi在前 离线在后
- var onNoLineWifiList = [];
- var onNoLineBlueList = [];
- ///添加离线wifi
- onNoLineList.forEach(element => {
- if (element.connectType == 3) {
- onNoLineWifiList.push(element)
- }
- });
- ///添加离线蓝牙
- onNoLineList.forEach(element => {
- if (element.connectType != 3) {
- onNoLineBlueList.push(element)
- }
- });
- finalList = finalList.concat(onNoLineWifiList);
- finalList = finalList.concat(onNoLineBlueList);
- store.setStore("deviceList", finalList);
- that.setData({
- deviceList: finalList
- });
- },
- ///******************************* 可折叠 ********************************************///
- onUserInfoLoad() {
- var that = this;
- var userInfo = wx.getStorageSync("userInfo") || "";
- if (!strings.isEmpty(userInfo)) {
- var phone = userInfo.phone;
- if (!phone) {
- return;
- };
- var nickName = userInfo.nickname || "";
- var greeting = that.getGreetBuNickName(nickName);
- // 获取缓存的频道数据
- var channelData = wx.getStorageSync("channelData") || "";
- if (!strings.isEmpty(channelData)) {
- that.setData({
- channelData: channelData,
- greeting: greeting,
- nickName: nickName,
- userPic: userInfo.headUrl || "",
- isLogin: true,
- })
- } else {
- that.setData({
- greeting: greeting,
- nickName: nickName,
- userPic: userInfo.headUrl || "",
- isLogin: true,
- });
- }
- } else {
- var greeting = that.getGreetBuNickName("");
- that.setData({
- greeting: greeting,
- })
- }
- },
- getGreetBuNickName(nickName) {
- var greeting = timeUtil.getGreet();
- if (nickName != "" && nickName != "未登录") {
- greeting = greeting + ',' + nickName;;
- }
- return greeting;
- },
- onBannerLoad() {
- var that = this;
- // var bannerList = wx.getStorageSync("homeBanner") || [];
- // if (that.data.bannerList.length > 0) {
- // that.setData({
- // bannerList: bannerList
- // });
- // }
- },
- onBannerShow() {
- var that = this;
- // getBanner({}).then((res) => {
- // that.setData({
- // bannerList: res
- // });
- // wx.setStorageSync("homeBanner", res);
- // })
- },
- ///销毁蓝牙
- onUnload() {
- var that = this;
- if (!strings.isEmpty(that.data.intervalId)) {
- clearInterval(that.data.intervalId);
- that.data.intervalId = null;
- }
- BtHelper.getInstance().disconnect();
- if (app.globalData.client === null) {
- return;
- };
- app.globalData.client.end(true);
- app.globalData.client.end(true);
- app.globalData.client = null;
- },
- })
- // addNewDeviceId() {
- // // 连接mqtt
- // var that = this;
- // if (app.globalData.client === null) {
- // app.connect();
- // } else if (app.globalData.newDeviceId) {
- // //监听
- // var deviceList = that.data.deviceList;
- // if (deviceList.length == 0) {
- // return;
- // }
- // ///连接新添加的设备
- // var deviceListIndex = that.data.deviceListIndex;
- // if (deviceListIndex != null) {
- // if (deviceList.length > deviceListIndex) {
- // if (deviceList[deviceListIndex].deviceId != deviceList[0].deviceId) {
- // that.setData({
- // actionIndex: null,
- // deviceListIndex: null,
- // });
- // }
- // } else {
- // that.setData({
- // actionIndex: null,
- // deviceListIndex: null,
- // });
- // }
- // }
- // var topic = `/AIrSMArT_${deviceList[0].deviceId.split("BLUFI_")[1]}/status/onoffline`;
- // app.subscribe(topic);
- // const Timeout = setTimeout(() => {
- // clearTimeout(Timeout);
- // that.actionDevice(0);
- // }, 500);
- // }
- // },
- // tryConnectBle() {
- // var hasBle = false;
- // var hasConnectBle = false;
- // var hasWifi = false;
- // var bleDevice;
- // let that = this
- // that.data.deviceList.forEach(device => {
- // if (device.connectType == 1) {
- // hasBle = true
- // if (device.state == "online") {
- // hasConnectBle = true
- // }
- // bleDevice = device
- // } else if (device.connectType == 3 && device.state == "online") {
- // // wifi
- // hasWifi = true;
- // }
- // });
- // if (!hasWifi && !hasConnectBle && hasBle) {
- // // 没有wifi 没有连接的ble 有未连接的ble
- // console.log("去连接蓝牙")
- // let bt_helper = BtHelper.getInstance()
- // bt_helper.initBluetooth(function (adapterState, hasPermission) {
- // console.log("蓝牙状态", adapterState, hasPermission)
- // if (adapterState && hasPermission) {
- // bt_helper.connect(bleDevice, function (data) {
- // console.log("连接成功:", data)
- // if (data == true) {
- // // 蓝牙模式
- // bleDevice.connectType = 1
- // // 在线
- // bleDevice.state = 'online'
- // bleDevice.ProdModel = bleDevice.clientType
- // that.addConnectBlueDevice(connectDevice);
- // }
- // })
- // } else {}
- // })
- // }
- // },
- // 去掉此功能,先留着吧
- // const other= {
- // "url": "",
- // "media_data": "",
- // "user_id": `${app.globalData.userInfo.deviceUid}`,
- // "timestamp": `${Math.round(new Date() / 1000)}`,
- // "channel_id": `${res[1].channelNum}`,
- // "order": "",
- // "resource_from": "",
- // "songAlbumID":"",
- // "version":3,
- // "is_debug": app.globalData.is_debug
- // };
- // app.PubMsg({
- // type: "play",
- // DstDeviceName: that.getThisDeviceID(),
- // other
- // });
|