<template>
	<view class="content contentBg">
		<uni-nav-bar class="nvbar" leftIcon="left" @clickLeft="navigatorBack">
			<view slot="left">
				<uni-icons type="back" size="23" color="#FFFFFF"></uni-icons>
			</view>
			<view slot="default" class="center uninavebartext">
				{{Status.navbar.title}}
			</view>
			<view slot="right">
				<view class="navbarRight center">
					<view class="imgContent" :class="{'visibilityHidden':Status.apiType!=item.apiType}"
						  @click.stop="handleRightClick(item,index)" v-for="item,index in Status.navbar.icons">
						<image class="img" :src="item.src" mode="aspectFit"></image>
						<view class="baber" v-if="item.math">{{item.math>9?'9+':item.math}}</view>
					</view>

				</view>

			</view>

		</uni-nav-bar>

		<view class="eq" :style="{marginTop:Status.navbar.height+'px'}">
			<view class="leftImg" @click.stop="previewImg(device.devicePic?device.devicePic:formData.img)">
				<image class="img" :src="device.devicePic?device.devicePic:formData.img" mode="aspectFit"></image>

			</view>
			<view class="rightTxt">
				<view class="row">
					<image class="img" src="/static/images/common/battry.png" mode="aspectFit"></image>
					<view class="txt">
						<view class="bigTxt">{{formData.sta_battery}}%</view>
						<view class="smallTxt">电量</view>
					</view>
				</view>
				<view class="row">
					<image class="img" src="/static/images/common/time.png" mode="aspectFit"></image>
					<view class="txt">
						<view class="bigTxt">{{formData.sta_system?dic.sta_system[formData.sta_system]:"" }}</view>
						<view class="smallTxt">设备状态</view>
					</view>
				</view>
			</view>
		</view>
		<view class="eqinfo">

			<view class="item">
				<text class="lbl">设备名称</text>
				<text class="value">{{device.deviceName}}</text>
			</view>
			<view class="item">
				<text class="lbl">Mac地址</text>
				<text class="value">{{device.deviceMac}}</text>
			</view>
			<view class="item">
				<text class="lbl">蓝牙名称</text>
				<text class="value">{{device.bluetoothName}}</text>
			</view>
			<view class="item" @click.top="bleStatuToggle">
				<text class="lbl">蓝牙状态</text>
				<text class="value" :class="formData.bleStatu?'green':'red'">{{getbleStatu}}</text>
			</view>
			<!-- <view class="item">
			<text class="lbl">设备状态</text>
			<text class="value">{{formData.sta_system?dic.sta_system[formData.sta_system]:"" }}</text>
		</view> -->
		</view>

		<view class="lampMode">
			<view class="sosContent">
				<view class="btnSos openSos center" :class="{active:formData.sta_SOSType===1}" @click="sosSetting(1)">
					{{formData.sta_SOSType===1?'报警中':'强制报警'}}
				</view>
				<view class="btnSos closeSos center" @click="sosSetting(0)">解除</view>

			</view>

		</view>



		<view class="warn">
			<view class="header" @click="setUsrToggle()">
				<text>
					报警模式
				</text>

				<uni-icons color="#ffffffde" size="20" :type="Status.usrToggle?'up':'down'">

				</uni-icons>


			</view>
			<view class="warnBody">
				<view class="modeSetting">

					<view class="arrow" @click.stop="openVolume(item,index)" v-if="item.show"
						  v-for="item,index in dic.sta_VoiceType" :class="{'active':formData.sta_VoiceType===item.key,
						  'displayNone' :!Status.usrToggle && index>
						3
						}">
						<view class="outCircle">
							<view class="item">
								<view class="text">{{item.name}}</view>
							</view>
						</view>

					</view>


				</view>
			</view>
		</view>


		<view class="lamp">
			<view class="title">
				<text>语音播报</text>
			</view>
		</view>

		<view class="lampMode">
			<view class="mode fleft " v-on:click.stop="openVolume(null,'8')"
				  :class="{'active':formData.sta_VoiceType==='8'}">
				<view class="leftImg">
					<image class="img" :class="{'displayNone':formData.sta_VoiceType==='8'}"
						   src="/static/images/common/play.png" mode="aspectFit"></image>
					<image class="img" :class="{'displayNone':formData.sta_VoiceType!=='8'}"
						   src="/static/images/common/pauseActive.png" mode="aspectFit"></image>
				</view>
				<view class="rightTxt">
					<text class="bigTxt">播放语音</text>
				</view>
			</view>

			<view class="mode fleft marginLeft" v-on:click.stop="audioManager()">
				<view class="leftImg">
					<image class="img" src="/static/images/common/uploadCloud.png" mode="aspectFit"></image>

				</view>
				<view class="rightTxt">
					<text class="bigTxt">语音管理</text>

				</view>
			</view>

			<view class="clear"></view>
		</view>



		<view class="lamp">
			<view class="title">
				<text>警示灯</text>

			</view>
		</view>

		<view class="lampMode">
			<view class="mode fleft " v-for="item,index in dic.sta_LightType" :class="{'active':formData.sta_LightType===item.key,
				'marginLeft':index%2===1,
				'displayNone':!item.show
				}" v-on:click.stop="lightTypeSet(item,index)">
				<view class="leftImg">
					<image class="img" :class="{'displayNone':formData.sta_LightType===item.key}"
						   src="/static/images/lightImg/light.png" mode="aspectFit"></image>
					<image class="img" :class="{'displayNone':formData.sta_LightType!==item.key}"
						   src="/static/images/lightImg/lightActive.png" mode="aspectFit"></image>

				</view>
				<view class="rightTxt">
					<text class="bigTxt">{{item.name}}</text>

				</view>
			</view>



			<view class="clear"></view>
		</view>



		<view class="warn" style="padding-bottom: 20rpx;">
			<view class="header">
				<text>调节</text>

			</view>
			<view class="header paddingTop0">
				<text class="sliderTxt">亮度</text>
				<text class="sliderVal">{{formData.sta_LightDimmer}}%</text>
			</view>

			<view class="slider-container">
				<slider min="10" max="100" step="10" :disabled="false" :value="formData.sta_LightDimmer"
						activeColor="#bbe600" backgroundColor="#686767" block-size="20" block-color="#ffffffde"
						@change="onBrightnessChanged" @changing="onBrightnessChanging" class="custom-slider" />

			</view>
			<view class="line"></view>
			<view class="header paddingTop0">
				<text class="sliderTxt">频率</text>
				<text class="sliderVal">{{formData.sta_LightFreq}}HZ</text>
			</view>

			<view class="slider-container">
				<slider min="0.5" max="10" step="0.5" :disabled="false" :value="formData.sta_LightFreq"
						activeColor="#bbe600" backgroundColor="#686767" block-size="20" block-color="#ffffffde"
						@change="onFreqChanged" @changing="onFreqChanging" class="custom-slider" />

			</view>
			<view class="line"></view>
			<view class="header paddingTop0">
				<text class="sliderTxt">音量</text>
				<text class="sliderVal">{{formData.sta_VoiceVolume}}</text>
			</view>

			<view class="slider-container">
				<slider min="1" max="8" step="1" :disabled="false" :value="formData.sta_VoiceVolume"
						activeColor="#bbe600" backgroundColor="#686767" block-size="20" block-color="#ffffffde"
						@change="onVolumeChanged" @changing="onVolumeChanging" class="custom-slider" />
			</view>






		</view>

	<ProParams :id="device.id"></ProParams>


	
		<MsgBox ref="msgPop" />
		<global-loading ref="loading" />
	</view>
</template>

<script>
	import gbk from '@/utils/gbk.js'
	import BleTool from '@/utils/BleHelper.js'
	import BleReceive from '@/utils/BleReceive';
	import {
		showLoading,
		hideLoading,
		updateLoading
	} from '@/utils/loading.js';
	import {
		MsgSuccess,
		MsgError,
		MsgClose,
		MsgWarning,
		showPop,
		MsgInfo
	} from '@/utils/MsgPops.js';
	import request, {
		baseURL
	} from '@/utils/request.js';

	import Common from '@/utils/Common.js'
	const pagePath = "/pages/100/HBY100";

	var ble = null;
	var these = null;
	var recei = null;
	var interval = null;
	var brightnessTimer = null;
	var volumeTimer = null;
	var hzTimer = null;
	export default {
		data() {
			return {
				Status: {
					apiType: '',
					navbar: {
						icons: [{
							src: '/static/images/common/shape.png',
							callback: this.gotoShare,
							apiType: 'listA'
						}],
						title: 'HBY100',
						height:90

					},
					curr: 0,
					total: 0,
					pageHide: false,
					
					BottomMenu: {
						show: false,
						showHeader: true,
						menuItems: [{
							text: '强光',
							icon: '/static/images/lightImg/qiang.png'
						}],
						activeIndex: -1,
						bgColor: '#2a2a2a',
						itemBgColor: '#3a3a3a',
						textColor: '#ffffffde',
						textAlign: 'flex-start',
						title: '主灯模式',
						showDivider: false,
						dividerColor: '#00000000',
						dividerThickness: '0rpx',
						dividerMargin: '10rpx',
						itemHeight: '80rpx',
						type: '',
						showBtn: true,
						btnBgColor: "#bbe600",
						btnText: "确定",
						btnTextColor: "#232323de",
						showMask: true,
						maskBgColor: '#00000066',
						showClose: false
					},
					usrToggle: false,
				},
				formData: {
					img: '/static/images/common/HBY100J.png',
					sta_battery: '',
					xuhang: '',
					blename: '',
					deviceId: '',
					deviceName: '',
					RSSI: '',
					statu: '',
					macAddress: '',
					bleStatu: false,
					sta_address: '',
					sta_VoiceType: '',
					sta_VoiceVolume: 1,
					sta_LightType: '',
					sta_LightFreq: 0.5,
					sta_LightDimmer: 10,
					sta_system: '',
					warnTime: 0,
					sta_SOSType: 0
				},
				dic: {
					sta_VoiceType: {
						"2": {
							key: "2",
							name: '公安',
							show: true
						},
						"1": {
							key: "1",
							name: '消防',
							show: true
						},					
						"3": {
							key: "3",
							name: '交警',
							show: true
						},						
						"5": {
							key: "5",
							name: '应急',
							show: true
						},
						
						"6": {
							key: "6",
							name: '医疗',
							show: true
						},
						"4": {
							key: "4",
							name: '市政',
							show: true
						},										
						"7": {
							key: "7",
							name: '铁道',
							show: true
						},
						"8": {
							key: "8",
							name: 'app语音',
							show: false
						},
						"0": {
							key: "0",
							name: '关闭',
							show: false
						}
					},

					sta_LightType: [{
							key: "1",
							name: '红蓝',
							show: true
						},
						{
							key: "2",
							name: '红闪',
							show: true
						},
						{
							key: "3",
							name: '黄闪',
							show: true
						},
						{
							key: "4",
							name: '蓝闪',
							show: true
						},
						{
							key: "5",
							name: '红流水',
							show: true
						},
						{
							key: "6",
							name: '黄流水',
							show: true
						},
						{
							key: "7",
							name: '蓝流水',
							show: true
						},
						{
							key: "0",
							name: '关闭',
							show: false
						}
					],


					sta_system: {
						"0": '关机',
						"1": '充电中',//仅充电
						"2": '未充电',//开机未充电
						"3": '充电中',//开机且充电
						"": ""
					}

				},
				device: {
					id: "",
					deviceName: "",
					deviceImei: "",
					deviceMac: "",
					communicationMode: 0,
					devicePic: "",
					typeName: "",
					bluetoothName: null,
					deviceStatus: null,
					bindingTime: "",
					onlineStatus: 0,
					battery: "0",
					latitude: null,
					longitude: null,
					alarmStatus: null,
					detailPageUrl: "/pages/650/HBY650",
					showConfirm: false
				},
				permissions: [],
				audioData: {
					packetCtn: 0,
					hexs: []
				}
			}
		},
		created() {
			this.Status.navbar.height = uni.getSystemInfoSync().statusBarHeight + 44;
		},
		onUnload() {
			console.log("页面卸载,释放资源");
			ble.removeAllCallback(pagePath);
		},
		onLoad: function() {
			these = this;
			recei = BleReceive.getBleReceive();
			ble = BleTool.getBleTool();

			this.$watch("formData.sta_battery", (newVal, oldVal) => {
				if (newVal <= 20 && this.formData.sta_system == 2) {
					MsgError("设备电量低",'',these);
				}
			});

			ble.addReceiveCallback(this.bleValueNotify, pagePath);
			ble.addStateBreakCallback(this.bleStateBreak, pagePath);
			ble.addStateRecoveryCallback(this.bleStateRecovry, pagePath);
			ble.addDisposeCallback(this.deviceDispose, pagePath);
			ble.addRecoveryCallback(this.deviceRecovry, pagePath);


			let eventChannel = this.getOpenerEventChannel();

			eventChannel.on('detailData', function(data) {

				// console.log("收到父页面的参数：" + JSON.stringify(data));
				var device = data.data;
				these.device = device;
				these.Status.apiType = data.apiType;
				these.Status.navbar.title = device.deviceName;
				if (data.apiType !== 'listA') {
					Common.getdeviceShareId(data.data.id).then(res => {
						if (res.code == 200) {
							if (res.data.permission) {
								these.permissions = res.data.permission.split(',');
							}
						}
					});
				}
				let f =this.getDevice();
				if (!f) {


					these.showBleUnConnect();

					return;
				}
				let form = f.formData;
				if (form) {
					let keys = Object.keys(form);
					for (var i = keys.length; i >= 0; i--) {
						let key = keys[i];
						these.formData[key] = form[key];
					}
				}
				these.formData.blename = f.name ? f.name : "Unname";
				these.formData.deviceName = device.deviceName;

				these.formData.bleStatu = false;
				these.formData.deviceId = f.deviceId;
				these.formData.bleStatu = 'connecting';
				ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
					these.formData.bleStatu = true;
				}).catch(ex => {
					these.formData.bleStatu = 'err';
                    MsgError("连接错误:" + ex.msg, "确定", these);
				});
				these.setBleFormData();



			});

		},
		onHide: function() {
			this.Status.pageHide = true;
		},
		onShow() {
			this.Status.pageHide = false;

			let f = this.getDevice();
			if (f) {
				these.formData.bleStatu = 'connecting';
				ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
					console.log("连接成功")
					these.formData.bleStatu = true;
                }).catch(ex => {
                    these.formData.bleStatu = 'err';
                    MsgError("连接错误:" + ex.msg, "确定", these);
                });;
			}
		},
		computed: {
			getbleStatu() {
				if (this.formData.bleStatu === true) {
					return '已连接';
				}
				if (this.formData.bleStatu === 'connecting') {
					return '连接中';
				}
				if (this.formData.bleStatu === 'dicconnect') {
					return '正在断开';
				}
				if (this.formData.bleStatu === 'err') {
					return '连接异常';
				}
				return '未连接';
			}
		},
		methods: {
			handleRightClick(item, s) {
				if (item && item.callback) {
					item.callback(item, s);
				} else {
					uni.showModal({
						content: '敬请期待'
					})

				}
			},
			navigatorBack() {
				uni.navigateBack();
			},
			gotoShare(item, s) {
				uni.navigateTo({
					url: '/pages/common/share/index',
					events: {
						ack: function(data) {}
					},
					success: (res) => {
						let json = {
							persissonType: '100'
						};
						Object.assign(json, this.device);
						res.eventChannel.emit('share', {
							data: json
						});
					}
				})
			},
			audioManager() {
				if (!this.permissions.includes('52') && this.Status.apiType !== 'listA') {

					showPop({
						message: '无操作权限',
						iconUrl: "/static/images/common/uploadErr.png",
						borderColor: "#e034344d",
						buttonBgColor: "#E03434",
						okCallback: null,
						buttonText: "确定"
					},these)
					return;
				}
				uni.navigateTo({
					url: '/pages/common/audioManager/AudioList',
					events: {
						audioApply: function(id) {
							//应用某个语音时，将数据发送给设备
							console.log("准备发送数据", id);
							showLoading(these, {
								text: '准备发送数据'
							});

							these.audioData.hexs = uni.getStorageSync(Common.pcmStorageKey + "_" + id);
							console.log("hexs.length=", these.audioData.hexs.length)
							these.HoldYouHand();
						}
					},
					success(res) {
						let channel = res.eventChannel;
						let f = these.getDevice();
						channel.emit("deviceData", {
							ble: f,
							device: these.device
						});
					}
				});
			},
			sendHex(repeatNo) {
				let f = this.getDevice();
				var task = () => {
					try {
						console.log("开始发送数据");
						if (!(this.audioData.hexs.length > 0 && this.audioData.packetCtn > 0)) {
							console.log("没有数据发送");
							uni.showModal({
								title: '提示',
								content: '没有数据发送'
							})
							return;
						}
						console.log("11111");
						let curr = repeatNo ? repeatNo : 1;
						let total = repeatNo ? repeatNo : this.audioData.packetCtn;
						let packetSize = 482;
						this.Status.curr = 0;
						this.Status.total = total;
						console.log("131212122");
						let sendNextPacket = () => {
							try {
								if (curr > total) {
									console.log("发送完成1111");
									if (!repeatNo) {

										setTimeout(() => {
											let cmd = {
												ins_VoiceDate: "end"
											};
											ble.sendString(f.deviceId, cmd).then(res => {

												updateLoading(these, {
													text: "发送完成,等待设备响应"
												});
											}).catch(er => {
												hideLoading(these);
												uni.showModal({
													title: '错误',
													content: er.msg
												});
											});
										}, 2000);


									} else {
										updateLoading(these, {
											text: "补发完成,等待设备响应"
										});

									}

									return;
								}
								let start = (curr - 1) * packetSize;
								let end = curr * packetSize;

								let orderNo = Common.decimalToHexLittleEndian(curr, 4, true);
								let json = "ins_VoiceDate:" + orderNo;
								let pcmData = this.audioData.hexs.slice(start, end);
								for (var p = 0; p < pcmData.length; p++) {
									json += pcmData[p];
								}
								// console.log("准备发送数据:", json)
								let header = [0x69, 0x6E, 0x73, 0x5F, 0x56, 0x6F, 0x69, 0x63, 0x65, 0x44, 0x61,
									0x74, 0x65, 0x3A
								];

								let bufferSize = pcmData.length + 16;
								let buffer = new ArrayBuffer(bufferSize);
								let dataView = new DataView(buffer);


								for (let i = 0; i < header.length; i++) {
									dataView.setUint8(i, header[i]);
								}
								// console.log("curr=" + curr + "orderNo=", orderNo);
								dataView.setUint8(14, '0x' + orderNo.substring(0, 2));
								dataView.setUint8(15, '0x' + orderNo.substring(2, 4));

								for (let i = 0; i < pcmData.length; i++) {
									dataView.setUint8(16 + i, '0x' + pcmData[i]);
								}

								let intval = 20;
								if (plus.os.name == 'iOS') {
									intval += 12;
								}

								ble.sendData(f.deviceId, buffer, null, null, intval)
									.then(res => {
										if (repeatNo) {


											updateLoading(these, {
												text: "补发成功"
											});
										} else {

											updateLoading(these, {
												text: "发送成功," + curr + "/" + total
											});
										}

										// console.log("发送成功," + curr + "/" + total);
										this.Status.curr = curr;
										curr++;

										setTimeout(sendNextPacket, intval);
									}).catch(ex => {
										if (ex.code == '10007') {
											// console.error("发送第" + curr + "包失败");
											setTimeout(sendNextPacket, intval);
										} else {
											// console.error("发送第" + curr + "包失败", ex);
											hideLoading(this);

											uni.showModal({
												title: '错误',
												content: ex.msg
											});
										}
									});
							} catch (errr) {
								console.error('errr=', errr)
							}
						}

						sendNextPacket();
					} catch (arg) {
						console.error("arg=", arg)
					}
				}

				setTimeout(task, 0);
			},
			HoldYouHand() {

				console.log("this.audioData.hexs=", this.audioData.hexs);
				console.log("this.audioData.hexs.length=", this.audioData.hexs.length);
				let cnt = Math.ceil(this.audioData.hexs.length / 482);
				console.log("cnt=", cnt)

				this.audioData.packetCtn = cnt;
				let json = {
					ins_VoiceDate: [this.audioData.packetCtn]
				}


				let f = this.getDevice();
				ble.sendString(f.deviceId, json).then(res => {
					updateLoading(these, {
						text: "握手成功,等待设备响应"
					});
				}).catch(ex => {
					MsgError(ex.msg,'',these);
				});
			},

onFreqChanging(e){
	this.formData.sta_LightFreq = e.detail.value;
},
			//频率
			onFreqChanged(e) {
				let f = this.getDevice();
				// #ifdef  APP-PLUS
				if (!f) {
					this.showBleUnConnect();
					return;
				}
				// #endif
				// #ifdef  H5
				f = {
					deviceId: '12345'
				}
				// #endif
				this.formData.sta_LightFreq = e.detail.value;

				// 清除之前的定时器
				if (hzTimer) {
					clearTimeout(hzTimer);
				}

				// 设置新的定时器，控制发送频率
				hzTimer = setTimeout(() => {



					let arr = (this.formData.sta_LightFreq + '').split('.');

					let json = {
						ins_LightFreq: [parseInt(arr[0]), parseInt(arr.length > 1 ? arr[1] : '0')]
					}


					ble.sendString(f.deviceId, json, f.writeServiceId, f.wirteCharactId, 30).then(res => {
						these.setBleFormData();
					}).catch(ex => {
						uni.showModal({
							title: '错误',
							content: ex.msg
						})
					});



				}, 100);
			},
			onVolumeChanging(e){
				this.formData.sta_VoiceVolume = e.detail.value;
			},
			//音量
			onVolumeChanged(e) {
				let f = this.getDevice();
				// #ifdef  APP-PLUS
				if (!f) {
					this.showBleUnConnect();
					return;
				}
				// #endif
				// #ifdef  H5
				f = {
					deviceId: '12345'
				}
				// #endif
				this.formData.sta_VoiceVolume = e.detail.value;

				// 清除之前的定时器
				if (volumeTimer) {
					clearTimeout(volumeTimer);
				}

				// 设置新的定时器，控制发送频率
				volumeTimer = setTimeout(() => {

					let json = {
						ins_VoiceVolume: [e.detail.value]
					}

					ble.sendString(f.deviceId, json, f.writeServiceId, f.wirteCharactId, 30).then(res => {
						these.setBleFormData();
					}).catch(ex => {
						uni.showModal({
							title: '错误',
							content: ex.msg
						})
					});



				}, 100);
			},
			onBrightnessChanging(e){
					this.formData.sta_LightDimmer = e.detail.value;
			},
			// 亮度调节
			onBrightnessChanged(e) {
				let f = this.getDevice();
				// #ifdef  APP-PLUS
				if (!f) {
					this.showBleUnConnect();
					return;
				}
				// #endif
				// #ifdef  H5
				f = {
					deviceId: '12345'
				}
				// #endif
				this.formData.sta_LightDimmer = e.detail.value;

				// 清除之前的定时器
				if (brightnessTimer) {
					clearTimeout(brightnessTimer);
				}

				// 设置新的定时器，控制发送频率
				brightnessTimer = setTimeout(() => {

					let json = {
						ins_LightDimmer: [e.detail.value]
					}

					ble.sendString(f.deviceId, json, f.writeServiceId, f.wirteCharactId, 30).then(res => {
						these.setBleFormData();
					}).catch(ex => {
						uni.showModal({
							title: '错误',
							content: ex.msg
						})
					});



				}, 100);
			},
			lightTypeSet(item, index) {

				if (!this.permissions.includes('51') && this.Status.apiType !== 'listA') {

					showPop({
						message: '无操作权限',
						iconUrl: "/static/images/common/uploadErr.png",
						borderColor: "#e034344d",
						buttonBgColor: "#E03434",
						okCallback: null,
						buttonText: "确定"
					},these)
					return;
				}
				let f = this.getDevice();
				// #ifdef  APP-PLUS
				if (!f) {
					this.showBleUnConnect();
					return;
				}
				// #endif
				// #ifdef  H5
				f = {
					deviceId: '12345'
				}
				// #endif
				let val = item.key;
				if (this.formData.sta_LightType === val) {
					val = '0';
				}

				let json = {
					ins_LightType: [parseInt(val)]
				}

				ble.sendString(f.deviceId, json, f.writeServiceId, f.wirteCharactId, 30)
					.then(res => {
						this.formData.sta_LightType = val;
						these.setBleFormData();
					})
					.catch(ex => {
						uni.showModal({
							title: '错误',
							content: ex.msg
						})
					});


			},
			setUsrToggle() {
				this.Status.usrToggle = !this.Status.usrToggle;
			},
			openVolume(item, index) {

				if (!this.permissions.includes('50') && this.Status.apiType !== 'listA') {

					showPop({
						message: '无操作权限',
						iconUrl: "/static/images/common/uploadErr.png",
						borderColor: "#e034344d",
						buttonBgColor: "#E03434",
						okCallback: null,
						buttonText: "确定"
					},these)
					return;
				}

				let f = this.getDevice();
				// #ifdef  APP-PLUS
				if (!f) {
					this.showBleUnConnect();
					return;
				}
				// #endif
				// #ifdef  H5
				f = {
					deviceId: '12345'
				}
				// #endif
				if (!item) {
					item = this.dic.sta_VoiceType[index];
				}
				// console.log("item=", item);
				let val = item.key;
				if (this.formData.sta_VoiceType === val) {
					val = '0';
				}
				var json = {
					ins_VoiceType: [parseInt(val)]
				}

				ble.sendString(f.deviceId, json, f.writeServiceId, f.wirteCharactId, 30)
					.then(res => {
						this.formData.sta_VoiceType = val;
						these.setBleFormData();
					}).catch(ex => {
						MsgError(ex.msg,'',these);
					});
			},

			showUnWarn(val) {
				showPop({
					message: '确定解除声光报警模式?',
					iconUrl: "/static/images/common/uploadErr.png",
					borderColor: "#e034344d",
					buttonBgColor: "#E03434",
					okCallback: () => {


						this.sosSetting(val, true);
					},
					buttonText: "解除",
					showCancel: true,
					buttonCancelText: '取消'

				},these);
			},
			sosSetting(item, isOk) {

				if (!this.permissions.includes('50') && this.Status.apiType !== 'listA') {

					showPop({
						message: '无操作权限',
						iconUrl: "/static/images/common/uploadErr.png",
						borderColor: "#e034344d",
						buttonBgColor: "#E03434",
						okCallback: null,
						buttonText: "确定"
					},these)
					return;
				}

				if (item === 0 && this.formData.sta_SOSType === item) {
					return;
				}

				let f = this.getDevice();

				// #ifdef  APP-PLUS											
				if (!f) {
					this.showBleUnConnect();
					return;
				}
				// #endif
				// #ifdef  H5
				f = {
					deviceId: '12345'
				}
				// #endif

				let task = () => {
					var json = {
						ins_warning: [parseInt(item)]
					}
					json = JSON.stringify(json);
					ble.sendString(f.deviceId, json, f.writeServiceId, f.wirteCharactId, 30).then(res => {

							if (this.formData.sta_SOSType === item) {
								this.formData.sta_SOSType = 0;
							} else {
								this.formData.sta_SOSType = item;
							}

							if (this.formData.sta_SOSType == 1) {
								this.formData.warnTime = 0;
								clearInterval(interval);
								interval = setInterval(() => {
									this.formData.warnTime += 1;
								}, 1000)
							} else {
								this.formData.warnTime = 0;
								clearInterval(interval);
							}
							this.setBleFormData();

						})
						.catch(ex => {
							MsgError(ex.msg,'',these);
						});
				}

				if (item === 1) {
					if (item === this.formData.sta_SOSType && !isOk) {
						this.showUnWarn(0);

					} else {
						showPop({
							message: '确定开启强制报警?',
							iconUrl: "/static/images/common/uploadErr.png",
							borderColor: "#e034344d",
							buttonBgColor: "#E03434",
							okCallback: task,
							buttonText: "开启",
							showCancel: true,
							buttonCancelText: '取消'
						},these);
					}


				} else {
					if (this.formData.sta_SOSType === 1 && !isOk) {
						this.showUnWarn(item);
					} else {
						task();
					}

				}


			},
			deviceRecovry(res) {
				if (this.Status.pageHide) {
					return;
				}
				if (res.deviceId == these.formData.deviceId) {
					this.formData.bleStatu = true;
					
				}

			},
			deviceDispose(res) {
				// if (this.Status.pageHide) {
				// 	return;
				// }
				if (res.deviceId == these.formData.deviceId) {
					if (res.device) {
						these.formData.bleStatu = 'connecting';
					} else {
						this.formData.bleStatu = false;
					}
					setTimeout(() => {
						hideLoading(these, 1000);
					});
					uni.showToast({
						icon: 'fail',
						title: '蓝牙连接已断开'
					});
				}

			},
			bleStateBreak() {
				if (this.Status.pageHide) {
					return;
				}
				//蓝牙适配器不可用
				this.formData.bleStatu = false;
				uni.showToast({
					icon: 'fail',
					title: '蓝牙已不可用'
				})
				hideLoading(this);
			},
			bleStateRecovry() {
				console.log("蓝牙可用");
				if (this.Status.pageHide) {
					return;
				}
				console.log("蓝牙可用");
				//蓝牙适配器再次可用,尝试恢复连接
				showLoading(this, {
					text: "蓝牙恢复可用,正在连接设备"
				});
				these.formData.bleStatu = 'connecting';
				ble.LinkBlue(these.formData.deviceId).then(() => {
					these.formData.bleStatu = true;
					updateLoading(these, {
						text: '连接成功'
					});
				}).catch(ex => {
					these.formData.bleStatu = 'err';
					updateLoading(these, {
						text: ex.msg
					})
				}).finally(() => {
					setTimeout(() => {
						hideLoading(these);
					}, 1000);

				});
			},
			previewImg(img) {
				if (!img) {
					return;
				}

				uni.previewImage({
					urls: [img]
				})
			},

			bleValueNotify: function(receive, device, path, recArr) { //订阅消息
				if (receive.deviceId !== this.formData.deviceId) {
					return;
				}

				let json = recei.ReceiveData(receive, device, pagePath, recArr);

				console.log("收到设备的数据", json)
				let key = 'sta_VoiceDate';
				if (key in json) {

					let val = json[key];
					console.log("22222", val)
					if (val === 'start') {
						//开始发包
						console.log("开始发送数据");
						updateLoading(these, {
							text: "开始发送"
						});
						this.sendHex();
						return;
					} else if (val === 'finish') {


						updateLoading(these, {
							text: "发送成功"
						});
						setTimeout(() => {
							hideLoading(this);
						}, 1500)
						return;
					} else {
						//重新发送某一包
						if (val > this.audioData.packetCtn) {

							return;
						}

						this.sendHex(val);
						return;
					}
				}

				let keys = Object.keys(json);

				keys.forEach(key => {
					if (key in this.formData) {
						// console.log("key=",key+",value=",json[key]);
						this.formData[key] = json[key] + "";
					}
				});





			},
			getDevice: function() {

				let f = ble.data.LinkedList.find((v) => {
					let flag=v.macAddress == these.device.deviceMac ;
					if(!flag && v.device){
						flag= v.device.id==these.device.id;						
					}
					if(flag){
						these.formData.deviceId = v.deviceId;
					}
					return flag;
				});
				
				// #ifdef WEB
				f = {
					deviceId: '123'
				}
				// #endif
				
				return f;
			},

			bleStatuToggle() {
				let f = this.getDevice();
				if (!f) {
					this.showBleUnConnect();
					return;
				}
				if (this.formData.bleStatu === true) {
					this.formData.bleStatu = 'dicconnect';
					ble.disconnectDevice(f.deviceId).finally(r => {
						this.formData.bleStatu = false;
					});
					return;
				}

				if (this.formData.bleStatu === false || this.formData.bleStatu === 'err') {
					this.formData.bleStatu = 'connecting';
					ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
						these.formData.bleStatu = true;
					}).catch(ex => {
						these.formData.bleStatu = 'err';
                        MsgError("连接错误:" + ex.msg, "确定", these);
					});
					return;
				}


			},


			setBleFormData() {
				ble.data.LinkedList.find((v) => {
					if (v.deviceId == these.formData.deviceId) {
						v.formData = these.formData;

						return true;
					}
					return false;
				});

				uni.setStorageSync(ble.StorageKey, ble.data.LinkedList);
			},
			showBleUnConnect() {

				showPop({
					message: "蓝牙未连接过该设备,请使用蓝牙重新添加该设备",
					iconUrl: "/static/images/common/uploadErr.png",
					borderColor: "#e034344d",
					buttonBgColor: "#E03434",
					buttonText: '去连接',
                    buttonTextColor: '#FFFFFFde',
					okCallback: function() {

						uni.navigateTo({
							url: "/pages/common/addBLE/addEquip",
							events: {
								BindOver: function(data) {
									console.log(data)
									these.formData.bleStatu = true;
								}
							},
							success: function(res) {
								// 通过eventChannel向被打开页面传送数据
								res.eventChannel.emit('detailData', {
									data: these.device
								})
							},
							fail(ex) {
								console.log("跳转失败", ex);
							}
						})
					}
				},these);
			},

			



			
	
		}
	}
</script>

<style>
	.content {
		padding: 30rpx;
		box-sizing: border-box;
	}

	.eq {
		width: 100%;
		height: 250rpx;
		border-radius: 8px;
		background: rgba(26, 26, 26, 1);
		box-sizing: border-box;
		padding: 24rpx 28rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: center;
		align-items: center;
		margin-bottom: 24rpx;
	}

	.eq .leftImg {
		box-sizing: border-box;
		padding: 36rpx;
		border-radius: 16rpx;
		background: rgba(42, 42, 42, 0.5);
	}

	.eq .leftImg,
	.eq .rightTxt {
		width: 50%;
		height: 100%;
	}

	.eq .rightTxt {
		display: flex;
		flex-direction: column;
		flex-wrap: nowrap;
		align-content: flex-start;
		justify-content: space-evenly;
		align-items: flex-start;
	}

	.eq .leftImg .img {
		width: 100%;
		height: 100%;
	}

	.eq .rightTxt .img {
		width: 52rpx;
		height: 52rpx;
	}

	.eq .rightTxt .row {
		width: 100%;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: flex-start;
		align-items: center;
		box-sizing: border-box;
		padding-left: 50rpx;
	}

	.eq .rightTxt .row .txt {
		padding-left: 20rpx;
	}

	.eq .bigTxt {
		color: rgba(255, 255, 255, 0.87);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: 400;
	}

	.eq .smallTxt {
		color: rgba(255, 255, 255, 0.6);
		font-family: PingFang SC;
		font-size: 24rpx;
		font-weight: 400;
	}


	.eqinfo {
		border-radius: 16rpx;
		background: rgba(26, 26, 26, 1);
		box-sizing: border-box;
		padding: 14rpx 0rpx;
	}

	.eqinfo .item {
		width: 100%;
		height: 60rpx;
		line-height: 60rpx;
		padding: 0rpx 28rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: space-between;
		align-items: center;
	}

	.eqinfo .lbl {
		color: rgba(255, 255, 255, 0.87);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: 400;

		letter-spacing: 0.07px;
		text-align: left;
	}

	.eqinfo .value {
		color: rgba(255, 255, 255, 0.6);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: 400;

		letter-spacing: 0.07px;
		text-align: left;
	}

	.lamp {
		margin-top: 24rpx;
	}

	.lamp .title {
		width: 100%;
		height: 60rpx;
		line-height: 60rpx;
		padding: 0rpx 28rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: space-between;
		align-items: center;
		font-size: 28rpx;
	}

	.lamp.noPadding .title,
	.lamp.noPadding .smlltitle {
		padding: 0rpx 0rpx !important;
	}

	.lamp .smlltitle {
		width: 100%;

		line-height: 15rpx;
		padding: 0rpx 28rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: space-between;
		align-items: center;
		font-size: 26rpx;
		color: rgba(255, 255, 255, 0.6);


	}


	.lamp .title .img {
		width: 32rpx;
		height: 32rpx;
		margin-top: 20rpx;
	}

	.lampMode {

		width: 100%;
		box-sizing: border-box;

	}

	.lampMode .mode {
		width: calc(calc(100% - 34rpx) / 2);
		border-radius: 8px;
		margin-top: 20rpx;
		background: rgba(26, 26, 26, 1);
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		align-items: center;
		justify-content: flex-start;
		padding: 30rpx;
		box-sizing: border-box;
		border-width: 1px;
		border-style: solid;
		border-color: #00000000;
	}

	.lampMode .mode.active {

		border-color: #aed600 !important;
	}

	.lampMode .mode.active .bigTxt {

		color: #aed600 !important;
	}

	.mode.marginLeft {
		margin-left: 34rpx !important;
	}

	.mode .leftImg {
		width: 70rpx;
		height: 70rpx;
		border-radius: 4px;
		background: rgba(255, 255, 255, 0.06);
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: center;
		align-items: center;
	}

	.mode.active .leftImg {
		background: rgba(255, 255, 255, 0.06);
	}

	.mode .leftImg .img {
		width: 40rpx;
		height: 40rpx;
	}

	.mode .rightTxt {
		display: flex;
		flex-direction: column;
		flex-wrap: nowrap;
		align-content: flex-start;
		justify-content: center;
		align-items: flex-start;
		padding-left: 20rpx;
	}

	.mode .bigTxt {
		color: rgba(255, 255, 255, 0.87);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: 400;

		letter-spacing: 0.07px;

	}

	.mode .smallTxt {
		color: rgba(255, 255, 255, 0.6);
		font-family: PingFang SC;
		font-size: 24rpx;
		font-weight: 400;

		letter-spacing: 0.07px;

	}

	.usrinfo {
		border-radius: 8px;
		width: 100%;
		box-sizing: border-box;
		padding: 24rpx 28rpx;
		background: rgba(26, 26, 26, 1);
	}

	.usrtitle {
		color: rgba(255, 255, 255, 0.87);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: 400;
		height: 40rpx;
		line-height: 40rpx;
		letter-spacing: 0.07px;

	}

	.usrinfo .btnSend {
		line-height: 65rpx;
		border-radius: 8px;
		width: 120rpx;
		height: 65rpx;
		color: rgba(35, 35, 35, 0.87);
		font-family: PingFang SC;
		font-size: 24rpx;
		font-weight: 400;
		letter-spacing: 0.15rpx;
		text-align: center;
		background-color: #BBE600;
	}

	.usrinfo .item {
		border-radius: 8px;
		width: 100%;
		box-sizing: border-box;
		line-height: 90rpx;
		height: 90rpx;
		background: rgba(42, 42, 42, 1);
		margin-top: 24rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: flex-start;
		align-items: center;
	}

	.usrinfo .lbl {
		width: 120rpx;
		height: 100%;
		padding-left: 24rpx;
		box-sizing: border-box;

		text-align: right;

		color: rgba(255, 255, 255, 0.87);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: 400;

		letter-spacing: 0.07px;

	}

	.usrinfo .value {
		width: calc(100% - 120rpx);
		height: 100%;

		font-size: 28rpx;
		font-weight: 400;

		letter-spacing: 0.07px;
		text-align: left;

	}

	.usrinfo .value .uni-input-input {
		color: rgba(255, 255, 255, 0.87);
		font-family: PingFang SC;

	}


	.usrplace {
		color: rgba(255, 255, 255, 0.4);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: 400;
		letter-spacing: 0.07px;
		text-align: left;
	}

	


	.slider-container {
		padding: 0px;
	}




	.addIco {
		width: 100%;
		height: 360rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: center;
		align-items: center;
	}

	.icoContent {
		width: 240rpx;
		height: 240rpx;
		border-radius: 8rpx;


		background: rgba(58, 58, 58, 1);
	}

	.icoContent .img {
		width: 70rpx;
		height: 70rpx;
	}

	.arrowContent {
		border-radius: 16rpx;
		background: rgba(26, 26, 26, 1);
		width: 100%;
		padding: 30rpx 0rpx;
	}

	.modeSetting {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		align-content: center;
		align-items: center;
		justify-content: flex-start;

	}

	.modeSetting .arrow {
		display: flex;
		flex-direction: column;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: center;
		align-items: center;
		width: calc(100% / 3);
		margin-bottom: 25rpx;
	}

	.modeSetting .arrow .text {

		color: rgba(255, 255, 255, 0.6);
		font-family: PingFang SC;
		font-size: 24rpx;
		font-weight: 400;

		letter-spacing: 0.14rpx;

	}

	.modeSetting .arrow.active .text {
		color: rgba(35, 35, 35, 0.87);

	}

	.modeSetting .arrow .outCircle {
		width: 130rpx;
		height: 130rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: center;
		align-items: center;
		border: 4rpx solid #00000000;
		border-radius: 50%;
		background: rgba(42, 42, 42, 1);
	}

	.modeSetting .arrow .item {
		width: 100%;
		height: 100%;

		border-radius: 50%;


		box-sizing: border-box;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: center;
		align-items: center;
	}

	.modeSetting .arrow.active .outCircle {
		border: 4rpx solid rgba(174, 214, 0, 1);
		padding: 4rpx;
	}

	.modeSetting .item.active .p100 {
		background: rgba(174, 214, 0, 1);
		border-radius: 50%;

	}

	.modeSetting .item .img {
		width: 52rpx;
		height: 56rpx;
	}

	.modeSetting .item .img.translate {
		transform: rotate(180deg);
	}

	.modeSetting .arrow .activity {
		display: none;
	}

	.modeSetting .arrow.active .nomal {
		display: none;
	}

	.modeSetting .arrow.active .item {
		background: rgba(174, 214, 0, 1);
	}

	.modeSetting .arrow.active .activity {
		display: block !important;
	}

	.usrinfo .image {
		width: 30rpx;
		height: 40rpx;
	}

	.usrinfo .img {
		width: 30rpx;
		height: 40rpx;
	}

	.content {

		box-sizing: border-box;
		width: 100%;
		min-height: 100vh;
		height: auto;
	}

	.contentBg {
		background-color: #121212;
		color: #ffffffde;
	}

	.fleft {
		float: left;
	}

	.fright {
		float: right;
	}

	.clear {
		clear: both;
	}

	.displayNone {
		display: none !important;
	}

	.p100 {
		width: 100%;
		height: 100%;
	}

	.center {
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: center;
		align-items: center;
	}

	.centerLeft {
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: flex-start;
		align-items: center;
	}

	.warnnig {
		border-radius: 16rpx;
		width: 100%;
		height: 70rpx;
		line-height: 70rpx;
		background: rgba(224, 52, 52, 1);
		box-sizing: border-box;
		padding: 0rpx 30rpx;
		margin-top: 20rpx;
		color: rgba(255, 255, 255, 0.87);
		font-family: 'PingFang SC';
		font-size: 28rpx;
		font-weight: 400;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: space-between;
		align-items: center;
	}

	.netContent {
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: flex-end;
		justify-content: space-around;
		align-items: flex-end;
		width: 50rpx;
	}


	.marginTop10 {
		margin-top: 20rpx;
	}

	.warnnig {
		border-radius: 16rpx;
		width: 100%;
		height: 70rpx;
		line-height: 70rpx;
		background: rgba(224, 52, 52, 1);
		box-sizing: border-box;
		padding: 0rpx 30rpx;
		margin-top: 20rpx;
		color: rgba(255, 255, 255, 0.87);
		font-family: 'PingFang SC';
		font-size: 28rpx;
		font-weight: 400;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		justify-content: space-between;
		align-items: center;
	}

	.sosContent {
		width: 100%;
		height: 110rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding-top: 24rpx;
	}

	.btnSos {
		border-radius: 92rpx;
		height: 100%;
		color: rgba(255, 255, 255, 0.87);
		font-family: PingFang SC;
		font-size: 28rpx;
		font-weight: 400;
		line-height: 110rpx;
		letter-spacing: 0.014rpx;
		text-align: center;

	}

	.openSos {
		width: 62%;
		background: #1a1a1a;
		color: #e03434;
		box-sizing: border-box;
		border: 1rpx solid #e03434;
	}

	.openSos.active {
		background: #e03434 !important;
		color: rgba(255, 255, 255, 0.87) !important;
	}


	.closeSos {
		width: 32%;
		background: #1a1a1a;
	}

	.warn {
		border-radius: 16rpx;
		background: #1a1a1a;
		width: 100%;
		box-sizing: border-box;
		margin-top: 25rpx;
	}

	.warn .header {
		width: 100%;
		box-sizing: border-box;
		padding: 20rpx 25rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		align-content: center;
		align-items: center;
		justify-content: space-between;

	}

	.paddingTop0 {
		padding: 0rpx 25rpx !important;
	}

	.line {
		width: calc(100% - 50rpx);
		height: 0rpx;
		border-bottom: 1rpx solid rgba(255, 255, 255, 0.06);
		margin: 30rpx 25rpx
	}

	.sliderTxt {
		color: rgba(255, 255, 255, 0.6);

		font-family: PingFang SC;
		font-size: 24rpx;
		font-weight: 400;

		letter-spacing: 0.07px;

	}

	.sliderVal {
		color: rgba(174, 214, 0, 1);

		font-family: PingFang SC;
		font-size: 24rpx;
		font-weight: 400;

		letter-spacing: 0.07px;

	}


	.navbarRight {
		width: 40px;
		height: 100%;

	}

	.navbarRight .imgContent {
		width: 36rpx;
		height: 36rpx;
		position: relative;
	}

	.navbarRight .imgContent:first-child {

		width: 38rpx !important;
		height: 38rpx !important;
		margin-top: -2rpx;
	}

	.navbarRight .imgContent .baber {
		position: absolute;
		z-index: 100;
		width: 30rpx;
		height: 30rpx;
		line-height: 30rpx;
		right: -15rpx;
		top: -15rpx;
		border-radius: 50%;
		background: #f12828;
		color: #ffffff;
		font-family: 'PingFang SC';
		font-style: Regular;
		font-size: 20rpx;
		font-weight: 400;

		text-align: center;
		overflow: hidden;
		white-space: nowrap;

	}

	.navbarRight .imgContent .img {
		width: 100%;
		height: 100%;

		box-sizing: border-box;
		position: relative;
	}

	.navbarRight .imgContent .img:last-child {
		padding: 1rpx;
	}

	.nvbar {
		top: 0px;
	}

	/deep/ .uni-navbar--fixed {
		top: 0px;
	}

	.uninavebartext {
		width: 100%;
		font-size: 32rpx;
	}
</style>