|
板凳
楼主 |
发表于 2017-2-13 14:28:15
|
只看该作者
相关JS代码
- var GizwitsSDK;
- var GizwitsDevice;
- var deviceMac;
- var deviceDid;
- var devicePasscode;
- var uid;
- var token;
- var Switch1_OnOff;
- var Switch2_OnOff;
- var Switch3_OnOff;
- var Switch4_OnOff;
- var Voltage;
- var Current;
- var Power;
- var Consume;
- var Temperature;
- var Humidity;
- //var Infrared;
- var AntiShake = 0;
- var AntiTimeOut;
- //var ConsumeValue
- apiready = function () {
- GizwitsSDK = api.require("gizWifiSDK");
- GizwitsDevice = api.require("gizWifiDevice");
- //GizwitsSDK.setLogLevel({"logLevel": "all", "printDataLevel": true});
- if(api.systemType == 'ios') {
- $api.setStorage('isBinding',false);
-
- params = {
- "device": {
- "mac": deviceMac,
- "did": deviceDid
- }
- };
- GizwitsDevice.getIsOnline(params, function(ret, err){
- if (!ret.isOnline) {
- //设备已断开
- alert('设备断开连接');
- back2DeviceList();
- }
- });
- }
- //恢复Frame事件**注册
- api.addEventListener({
- name: 'control'
- }, function (ret, err) {
- updateInfo();
- getDevOnline();
- });
-
- updateInfo();
- getDevOnline();
- //断开连接的菜单事件**注册
- api.addEventListener({
- name: 'disconnect'
- }, function (ret, err) {
- disconnect();
- });
- //断开连接的菜单事件**注册
- api.addEventListener({
- name: 'unbind'
- }, function (ret, err) {
- unBind();
- });
- //获取设备状态的菜单事件**注册
- api.addEventListener({
- name: 'getStatus'
- }, function (ret, err) {
- getStatus();
- });
- Switch1_OnOff = document.getElementById("showText_Switch1_on_off");
- Switch2_OnOff = document.getElementById("showText_Switch2_on_off");
- Switch3_OnOff = document.getElementById("showText_Switch3_on_off");
- Switch4_OnOff = document.getElementById("showText_Switch4_on_off");
- // Motor_Speed = document.getElementById("positionLED_motor");
- Humidity = document.getElementById("showText_hum");
- Temperature = document.getElementById("showText_temp");
- Voltage = document.getElementById("showText_Voltage");
- Current = document.getElementById("showText_Current");
- Power = document.getElementById("showText_Power");
- Consume = document.getElementById("showText_Consume");
- };
- function getDevOnline() {
- params = {
- "device": {
- "mac": deviceMac,
- "did": deviceDid
- }
- };
- GizwitsDevice.getIsConnected(params,function(ret, err){
- if(ret.isConnected){
- GizwitsDevice.registerNotifications(params, onMyStatus);
- setTimeout(function () {
- getStatus();
- }, 500);
- }else{
- alert('设备不在线,不可以做控制,但可以解除绑定');
- }
- });
- }
- //更新设备信息
- function updateInfo() {
- var mResult = getTokenAndUid();
- uid = mResult.uid;
- token = mResult.token;
- deviceInfo = getKeyAndParam("deviceInfo");
- deviceMac = deviceInfo.mac;
- deviceDid = deviceInfo.did;
- devicePasscode = deviceInfo.passcode;
- }
- function back2DeviceList() {
- sortTo.toDevList();
- }
- //登录设备
- var loginDevice = function (macDevice, didDevice, passcodeDevice) {
- params = {
- "uid": uid,
- "token": token,
- "device": {
- "mac": macDevice,
- "did": didDevice
- }
- };
- GizwitsDevice.login(params, function (ret, err) {
- if (ret) {
- alert('登录设备成功');
- } else {
- alert('登录设备失败');
- }
- });
- };
- //设备状态回调
- function onMyStatus(ret, err) {
- // alert($api.jsonToStr(ret)+' ==123321== '+$api.jsonToStr(err));
- // if (AntiShake > 0) {
- // return;
- // }
- if (ret) {
- if(api.systemType == 'ios') {
- var isBinding = $api.getStorage('isBinding');
- // if(isBinding) alert('断开aaaaa:'+isBinding);
- // alert('onMyStatus: '+$api.jsonToStr(ret));
- if (typeof(ret.isConnected) != "undefined") {
- if (!ret.isConnected) {
- //设备已断开
- // alert('isBinding: '+isBinding);
- if(isBinding=='false') {
- alert('设备断开连接');
- back2DeviceList();
- return;
- }
- }
- }
- }
- else {
- if (typeof(ret.isConnected) != "undefined") {
- if (!ret.isConnected) {
- alert('设备断开连接');
- back2DeviceList();
- return;
- }
- }
- }
-
- var status = ret.status;
- if(status != '' && status != null){
- mStatus = ret.status.data.entity0;
- // alert($api.jsonToStr(mStatus));
- // if (!mStatus.LED_R) return;
- //状态更新
-
- //Motor_Speed.value = mStatus.Motor_Speed ? mStatus.Motor_Speed : 0;
- //Motor_SpeedShowText.innerHTML = mStatus.Motor_Speed ? mStatus.Motor_Speed : 0;
- Switch1_OnOff.value = mStatus.Switch1_OnOff ? 1 : 0;
- Switch2_OnOff.value = mStatus.Switch2_OnOff ? 1 : 0;
- Switch3_OnOff.value = mStatus.Switch3_OnOff ? 1 : 0;
- Switch4_OnOff.value = mStatus.Switch4_OnOff ? 1 : 0;
- Humidity.innerHTML = mStatus.Humidity ? mStatus.Humidity : 0;
- Temperature.innerHTML = mStatus.Temperature ? mStatus.Temperature : 0;
- Voltage.innerHTML = mStatus.Voltage ? mStatus.Voltage : 0;
- Current.innerHTML = mStatus.Current ? mStatus.Current : 0;
- Power.innerHTML = mStatus.Power ? mStatus.Power : 0;
- Consume.innerHTML = mStatus.Consume ? mStatus.Consume : 0;
-
- } else {
- // alert('控制失败');
- }
- };
- //获取设备状态
- function getStatus() {
- params = {
- "device": {
- "mac": deviceMac,
- "did": deviceDid
- },
- "data": {
- "cmd": 2
- }
- };
- GizwitsDevice.write(params, onMyStatus);
- };
- //主动断开连接
- function disconnect() {
- params = {
- "device": {
- "mac": deviceMac,
- "did": deviceDid
- }
- };
- // alert(JSON.stringify(params));
- // alert('disconnect_start');
- GizwitsDevice.disconnect(params, function (ret, err) {
- // alert('disconnect_end');
- // alert(JSON.stringify(ret)+' === '+JSON.stringify(err));
- if (ret) {
- if (!ret.isConnected) {
- //断开连接跳界面
- back2DeviceList();
- }
- }
- });
- };
- //解绑设备
- function unBind() {
- params = {
- "uid": uid,
- "token": token,
- "passcode": devicePasscode,
- "did": deviceDid
- };
- GizwitsSDK.unbindDevice(params, function (ret, err) {
- if (ret) {
- //解绑成功跳界面
- alert('解绑成功');
- back2DeviceList();
- } else {
- alert('解绑失败');
- }
- }
- );
- };
- //选择器控件发送控制指令方法
- function selectorListener(num) {
- switch (num) {
- case 1:
- valueSend = document.getElementById("showText_Switch1_on_off").value;
- attrname = "Switch1_OnOff";
- break;
- case 2:
- valueSend = document.getElementById("showText_Switch2_on_off").value;
- attrname = "Switch2_OnOff";
- case 3:
- valueSend = document.getElementById("showText_Switch3_on_off").value;
- attrname = "Switch3_OnOff";
- break;
- case 4:
- valueSend = document.getElementById("showText_Switch4_on_off").value;
- attrname = "Switch4_OnOff";
- break;
- }
- sendControlMsg(attrname, valueSend);
- };
- //滑动控件发送控制指令方法
- /*function sliderSend(num) {
- switch (num) {
- case 1:
- atrrName = "LED_R";
- atrrValue = document.getElementById("positionLED_red").value;
- break;
- case 2:
- atrrName = "LED_G";
- atrrValue = document.getElementById("positionLED_green").value;
- break;
- case 3:
- atrrName = "LED_B";
- atrrValue = document.getElementById("positionLED_blue").value;
- break;
- case 4:
- atrrName = "Motor_Speed";
- atrrValue = document.getElementById("positionLED_motor").value;
- break;
- }
- sendControlMsg(atrrName, atrrValue);
- };
- */
- //发送控制指令方法
- function sendControlMsg(name, value) {
- sendMsg = '{"device": {"mac":"' + deviceMac + '","did": "' + deviceDid + '"},"data": {"cmd": 1,' +
- '"entity0": {' + name +
- ':"' + value + '"}}}';
- sendMsgJson = eval('(' + sendMsg + ')');
- AntiShake = 1;
- clearTimeout(AntiTimeOut);
- AntiTimeOut=setTimeout("AntiShake=0;", 2000);
- GizwitsDevice.write(sendMsgJson, onMyStatus);
- }
- /*
- //获取对应的控件
- function getElement(num) {
- var slider;
- var text;
- switch (num) {
- case 1:
- text = document.getElementById("showText_LED_red");
- slider = document.getElementById("positionLED_red");
- break;
- case 2:
- text = document.getElementById("showText_LED_green");
- slider = document.getElementById("positionLED_green");
- break;
- case 3:
- text = document.getElementById("showText_LED_blue");
- slider = document.getElementById("positionLED_blue");
- break;
- case 4:
- text = document.getElementById("showText_LED_motor");
- slider = document.getElementById("positionLED_motor");
- break;
- }
- ;
- var valForSliderText = {
- "slider": slider,
- "textShow": text
- };
- return valForSliderText;
- };
- */
- //滑动控件的事件监听,根据滑动控件的变动更新对应的文字和发送指令
- function sliderShowText(num) {
- var slider;
- var value;
- var text;
- var myElement = getElement(num);
- slider = myElement.slider;
- text = myElement.textShow;
- value = slider.value;
- text.innerHTML = value;
- sliderSend(num);
- };
- //减一键点击事件监听
- function sub(num) {
- var slider;
- var value;
- var text;
- sliderSend(num);
- var myElement = getElement(num);
- slider = myElement.slider;
- text = myElement.textShow;
- value = slider.value;
- value--;
- slider.value = value;
- text.innerHTML = value;
- sliderSend(num);
- };
- //加一键点击事件监听
- function add(num) {
- var slider;
- var value;
- var text;
- var myElement = getElement(num);
- slider = myElement.slider;
- text = myElement.textShow;
- value = slider.value;
- value++;
- slider.value = value;
- text.innerHTML = value;
- sliderSend(num);
- };
- /*function buttonListener() {
- ConsumeValue = document.getElementById("showText_Consume").value;
- }
- */
- function complete(){
- api.openWin({
- name: 'share',
- url: '../html/complete.html'
- });
-
- }
复制代码 |
|