简体   繁体   English

使用Firebase控制多个Nest恒温器

[英]Controlling multiple Nest Thermostats using Firebase

Is there an easy way to discover and differentiate thermostat devicesIDs? 有没有一种简单的方法来发现和区分恒温器设备ID? I would have expected to be able to find the deviceId somewhere in the device interface or by using the Firebase library, but I haven't found either to be the case. 我本来希望能够在设备界面中的某个位置或使用Firebase库找到deviceId,但事实并非如此。

My Firebase object is created as such (url ending at thermostats): 我的Firebase对象是这样创建的(URL以恒温器结尾):

Firebase fb = new Firebase("https://developer-api.nest.com/devices/thermostats/");

To change a property on the thermostat, my code looks like this... 要更改恒温器的属性,我的代码如下所示:

/*This method does an http get (not firebase) to get the entire thermostats data structure as a JSON Object. / *此方法执行http get(不是firebase)以将整个恒温器数据结构作为JSON对象获取。 It then iterates through each thermostat deviceId structure looking for a matching room name. 然后,它遍历每个恒温器deviceId结构以查找匹配的房间名称。 When it finds a match, it breaks and returns the deviceID as a string. 找到匹配项时,它会中断并以字符串形式返回deviceID。 The device id is needed to change all data specific to that thermostat device using firebase. 需要使用设备ID才能使用Firebase更改特定于该恒温器设备的所有数据。 */ * /

deviceId = getDeviceIdFromRoomName(roomName); 

//I then perform an action like so to change the "target_temperature_f" to a new value. //然后执行类似的操作,将“ target_temperature_f”更改为新值。

fb.child(deviceId).child(NestConstants.TARGET_TEMP_F).setValue(newValue);

It seems like there should be an easier, more reliable way to do this using the Firebase library, but I haven't been able to find it. 似乎应该有使用Firebase库执行此操作的更简单,更可靠的方法,但我一直无法找到它。 Any suggestions? 有什么建议么?

To help visualize, this is the data structure that I'm parsing and I'm looking for suggestions on a better way to acquire "CRfcK-QwEZLAr4qxHshPmZyFayBdIYv5" (if it exists). 为了使可视化,这是我正在解析的数据结构,我正在寻找有关获取“ CRfcK-QwEZLAr4qxHshPmZyFayBdIYv5”(如果存在)的更好方法的建议。

{
    "thermostats": {
        "CRfcK-QwEZLAr4qxHshPmZyFayBdIYv5": {
            "locale": "en-US",
            "temperature_scale": "F",
            "is_using_emergency_heat": false,
            "has_fan": true,
            "software_version": "4.1",
            "has_leaf": true,
            "device_id": "CRfcK-QwEZLAr4qxHshPmZyFayBdIYv5",
            "name": "Master Bedroom",
            "can_heat": true,
            "can_cool": false,
            "hvac_mode": "heat",
            "target_temperature_c": 18.5,
            "target_temperature_f": 66,
            "target_temperature_high_c": 24,
            "target_temperature_high_f": 75,
            "target_temperature_low_c": 20,
            "target_temperature_low_f": 68,
            "ambient_temperature_c": 12,
            "ambient_temperature_f": 54,
            "away_temperature_high_c": 24,
            "away_temperature_high_f": 76,
            "away_temperature_low_c": 12.5,
            "away_temperature_low_f": 55,
            "structure_id": "xF7P6wyrR7-8VHerOAcHYDMBc_odGpO2CIpQO_g5EpM13LINO9Oxdg",
            "fan_timer_active": false,
            "name_long": "Master Bedroom Thermostat",
            "is_online": true
        }``

Disclaimer: I haven't worked with the NEST API. 免责声明:我尚未使用NEST API。 My answer below is based on experience with the regular Firebase API. 我的以下回答基于常规Firebase API的经验。

From the way it looks thermostats is a Firebase list . 从外观上看, thermostats是一个Firebase列表 In that case you can easily handle all children using something like this: 在这种情况下,您可以使用以下方法轻松处理所有孩子:

var thermostats = new Firebase("https://developer-api.nest.com/devices/thermostats/");
thermostats .on('child_added', function(snapshot) {
  var thermostat = snapshot.val();
  alert(thermostat.device_id+' is '+thermostat.name);
});

I'm assuming you're using the JavaScript API here btw. 我假设您在这里使用JavaScript API。

I'm not sure I'm understanding you correctly here, but have you tried calling the structure ID? 我不确定我是否在这里正确理解了您,但是您是否尝试调用结构ID? When I do that, it lists all thermostats associated with that structure. 当我这样做时,它将列出与该结构关联的所有恒温器。 Once you have all the thermostats, could you call them and store the associated name? 拥有所有恒温器后,您可以调用它们并存储相关名称吗? If I'm way off, let me know and I'll try to look at it again. 如果我要离开,请告诉我,我将尝试再次查看。

Greg, 格雷格

The structure contains a list of associated device ids. 该结构包含关联设备ID的列表。 I suggest to start listening on structures if discoverability is your intent. 如果可发现性是您的意图,我建议您开始侦听结构。 You need to do that only once as the device ids don't change. 您只需执行一次操作,因为设备ID不会更改。 Once you traverse structures and get your desired device ids you may never need to perform that step ever again. 遍历结构并获得所需的设备ID后,您可能再也不需要执行该步骤了。

Lev. 列夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM