基于ApiCloud demo APP开发应用无法与设备交换数据
问题描述:APP通过ApiCloud云编译可正常安装到手机,并能通过扫码连接到虚拟设备,登录注册一切正常
但是虚拟设备推送数值APP界面无反应(一直是默认初始值0)
改变APP上数值虚拟设备也没有反应
请问问题出在哪里?
相关HTML代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
<title></title>
<link rel="stylesheet" type="text/css" href="../css/api.css" />
<link rel="stylesheet" type="text/css" href="../css/common.css" />
<link rel="stylesheet" type="text/css" href="../frozenui/1.2.1/css/global.css"/>
<link rel="stylesheet" type="text/css" href="../css/IotController.css"/>
</head>
<body>
<center>
<h1>实时充电信息</h1>
</center>
</div>
<div id="content">
<div class="listSelector">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
开关1
</p></th>
<th class="thTwo">
<select class="selector" id="showText_Swith1_on_off" onchange="selectorListener(1)">
<option value=0>关闭</option>
<option value=1>开启</option>
</select></th>
</tr>
</table>
</div>
<div class="listSelector">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
开关2
</p></th>
<th class="thTwo">
<selectclass="selector" id="showText_Swith2_on_off" onchange="selectorListener(2)">
<option value=0>关闭</option>
<option value=1>开启</option>
</select></th>
</tr>
</table>
</div>
<div class="listSelector">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
开关3
</p></th>
<th class="thTwo">
<selectclass="selector" id="showText_Swith3_on_off" onchange="selectorListener(3)">
<option value=0>关闭</option>
<option value=1>开启</option>
</select></th>
</tr>
</table>
</div>
<div class="listSelector">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
开关4
</p></th>
<th class="thTwo">
<selectclass="selector" id="showText_Swith4_on_off" onchange="selectorListener(4)">
<option value=0>关闭</option>
<option value=1>开启</option>
</select></th>
</tr>
</table>
</div>
<div class="listShow">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
电压
</p></th>
<th class="thTwo">
<p class="listShowText" id="showText_Voltage">
0
</p></th>
</tr>
</table>
</div>
<div class="listShow">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
电流
</p></th>
<th class="thTwo">
<p class="listShowText" id="showText_Current">
0
</p></th>
</tr>
</table>
</div>
<div class="listShow">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
功率
</p></th>
<th class="thTwo">
<p class="listShowText" id="showText_Power">
0
</p></th>
</tr>
</table>
</div>
<div class="listShow">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
电量
</p></th>
<th class="thTwo">
<p class="listShowText" id="showText_Consume">
0
</p></th>
</tr>
</table>
</div>
<div class="listShow">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
环境温度
</p></th>
<th class="thTwo">
<p class="listShowText" id="showText_temp">
0
</p></th>
</tr>
</table>
</div>
<div class="listShow">
<table class="table">
<tr>
<th class="thOne">
<pclass="listTitleText" >
环境湿度
</p></th>
<th class="thTwo">
<p class="listShowText" id="showText_hum">
0%
</p></th>
</tr>
</table>
</div>
<div>
</div class="listShow">
<div class="compelteBtn">
<input style="border:1px solid #000000 ; background-color:#008B8B"
type="button" value="充电完成" onclick="sortTo.toShare()" ><!--onchange="buttonListener(0)"/-->
</div>
</div>
</body>
<script type = "text/javascript" src = "../script/GobalConfig.js"></script>
<script type="text/javascript" src="../script/api.js"></script>
<script type = "text/javascript" src = "../script/common.js"></script>
<script type="text/javascript" src="../script/sort.js"></script>
<script type = "text/javascript" src = "../script/IotController.js"></script>
<script type = "text/javascript" src = "../script/complete.js"></script>
</html>
相关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'
});
}
虚拟设备定义的数据点名称:showText_Switch1_on_off
showText_Switch2_on_off
showText_Switch3_on_off
showText_Switch4_on_off
showText_hum
showText_temp
showText_Voltage
showText_Current
showText_Power
showText_Consume
demo来自于:https://github.com/gizwits/gokit_demo_in_apicloud 有人吗QAQ QAQ QAQ QAQ 这个一年前的demo是比较旧的,可能一些SDK更新导致接口的变更没修正,有些不可预估的错误,你可以将问题详细发送到club@gizwits.com这个邮箱。我们会及时给你解决 boboP 发表于 2017-2-15 10:53
这个一年前的demo是比较旧的,可能一些SDK更新导致接口的变更没修正,有些不可预估的错误,你可以将问题详 ...
发邮件已经好几天了,请问一般需要多久才能有回复呢??? 我的也是,望官方及时解决,谢谢!
页:
[1]