【发布时间】:2020-01-16 05:49:12
【问题描述】:
我正在尝试做一个 html 应用程序以在 localhost 中运行以允许控制 phillips 色调灯。我正在调整 EVOTings 基本代码来做到这一点。现在我需要做一个函数来了解每个灯泡的状态以在 HMI 上刷新它,但我对 AJAX 代码不满意。 如果我在 API 中执行 GET 命令,我会收到此响应, Example of a GET response
{
"1": {
"state": {
"on": true,
"bri": 172,
"hue": 0,
"sat": 0,
"effect": "none",
"xy": [
0.3124,
0.3301
],
"ct": 0,
"alert": "select",
"colormode": "xy",
"mode": "homeautomation",
"reachable": false
},
"swupdate": {
"state": "notupdatable",
"lastinstall": null
},
"type": "Extended color light",
"name": "Hue color lamp 1",
"modelid": "LCT015",
"manufacturername": "Philips",
"productname": "Hue color lamp",
"capabilities": {
"certified": true,
"control": {
"mindimlevel": 1000,
"maxlumen": 806,
"colorgamuttype": "other",
"ct": {
"min": 0,
"max": 65535
}
},
"streaming": {
"renderer": false,
"proxy": false
}
},
"config": {
"archetype": "sultanbulb",
"function": "mixed",
"direction": "omnidirectional"
},
"uniqueid": "00:17:88:01:04:57:b0:ac-0b",
"swversion": "1.29.0_r21169",
"swconfigid": "3416C2DD",
"productid": "Philips-LCT015-1-A19ECLv5"
},
我有一个 AJAX PUT 函数来改变灯泡状态,
app.lightSetState = function(lightId, state)
{
$.ajax({
type: 'PUT',
dataType: 'json',
url: 'http://' + app.getHueBridgeIpAddress() +'/api/' +
app.user + '/lights/' + lightId + '/state',
data: JSON.stringify(state),
success: function(data) { },
error: function(a, err) { }
});
};
//turn device on
app.lightOn = function()
{
app.lightSetState(app.lightId, {"on":true});
};
//turn device off
app.lightOff = function()
{
app.lightSetState(app.lightId, {"on":false});
};
现在我认为解决方案是这样的,
app.lightGetState = function(lightId, state)
{
$.ajax({
type: 'GET',
dataType: 'json',
url: 'http://' + app.getHueBridgeIpAddress() +'/api/' +
app.user + '/lights/' + lightId,
data: ............. ,
success: ............ ,
error:
});
};
有人可以帮助我吗? 我愿意接受任何有关进行此状态刷新的建议。
最好的问候:)
【问题讨论】:
-
你能澄清你的问题吗?你说你想得到状态,但你的第一块代码的第四行显示状态为'on'
-
嗨@LTPCGO,当我得到灯时,API 会为我提供我发布的信息。正如我所说,我想“捕获”“状态:开启”是真还是假,我不知道该怎么做。目标是在灯亮或灭时更改灯泡图标,并使我的应用程序每次都与灯泡同步。主要问题是我对 AJAX 不满意。提前感谢您的宝贵时间。
-
您需要真正熟悉 JSON,但我会发布答案 :)
-
你展示的第一个代码块是什么代码?
-
最后一段代码什么都不做......是一种可能是解决方案的例子......但如果我这样做 'http://' + app.getHueBridgeIpAddress() +' /api/' + app.user + '/lights/' + lightId 我得到了你在主帖链接中看到的内容...感谢您的合作..
标签: javascript json ajax