【问题标题】:AJAX GET command to to get states o the lightsAJAX GET 命令获取灯的状态
【发布时间】: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


【解决方案1】:

您声明您通过对 URL 的调用获得 JSON 响应,因此:

function getState(app, lightId)
{
    return $.ajax({
        type: 'GET',
        async: false,
        dataType: 'json',
        url: 'http://' + app.getHueBridgeIpAddress() +'/api/' + app.user + '/lights/' + lightId,
        success: function(data) {
            return data;
        },
        error: function(a, err) {}
    });
};

var jsontext = JSON.parse(getState(app,lightId));
app.lightGetState = jsontext[1].state.on;

实际上,您真的不应该使用async: false,因为它已被弃用。您应该删除 return 方法并在该 success: 函数中进行处理。

作为解释: 您在 GET 示例中显示的数据是 JSON,因此您需要将其解析为我称为 jsontext 的对象。棘手的一点是密钥是'1',但是一旦我们有了它(jsontext[1]),我们就会得到state 密钥,然后是on 密钥,它将返回'true''false',并且您可以在以后的代码中使用这些字符串

【讨论】:

  • 我的评论太记录了,所以我发布了一个答案。
  • 感谢您的回答。我试图在我的代码中使用它: function getstate(){ var estado = app.lightGetState(2); document.getElementById("textbox8").value = estado;并且 textbox8 中的结果是“未定义”。我错过了什么?最好的问候和感谢您的合作。
猜你喜欢
  • 2010-10-22
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 2019-07-19
  • 2012-02-15
  • 2013-04-21
  • 1970-01-01
相关资源
最近更新 更多