『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1331|回复: 5

[基础教程] 请教:属性值中间有原点watch.current_address,nodered怎么写才...

[复制链接]

4

主题

78

帖子

579

积分

高级会员

Rank: 4

积分
579
金钱
501
HASS币
0
发表于 2024-3-12 09:11:54 | 显示全部楼层 |阅读模式
本帖最后由 leozen 于 2024-3-12 09:33 编辑

手表属性值中间有个原点watch.current_address,nodered节点写成 "current_address": msg.data.attributes.watch.current_address,报错:TypeError: Cannot read properties of undefined (reading 'current_address')没有原点的属性读取没问题。
-------------
问了ai,解决了:
To access the `watch.current_address` property in Node-RED, you need to understand how messages work in Node-RED and how to access their properties.


In Node-RED, messages are JavaScript objects that can have any set of properties. The most commonly used property is `payload`, but Node-RED also adds a property called `_msgid` to identify the message's progress through a flow [[1]](https://nodered.org/docs/user-guide/messages).


To access the `watch.current_address` property, you need to follow these steps:


1. Pass the message containing the `watch.current_address` property to a Debug node.
2. In the Debug sidebar, you can view the structure of the message and explore its properties.
3. Hover over the `watch.current_address` property and click the copy button to copy its path to your clipboard [[1]](https://nodered.org/docs/user-guide/messages).


Once you have the path to the `watch.current_address` property, you can use it in a Change or Function node to access and manipulate the property as needed.


---
Learn more:
1. [Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)
2. [Node Red: get data attributes? - Node-RED - Home Assistant Community](https://community.home-assistant ... a-attributes/143540)
3. [Read File from Watch - General - Node-RED Forum](https://discourse.nodered.org/t/read-file-from-watch/3062)



回复

使用道具 举报

6

主题

94

帖子

843

积分

高级会员

Rank: 4

积分
843
金钱
749
HASS币
0
发表于 2024-5-8 10:36:09 | 显示全部楼层
读的话解决了,写的话怎么实现?
回复

使用道具 举报

4

主题

78

帖子

579

积分

高级会员

Rank: 4

积分
579
金钱
501
HASS币
0
 楼主| 发表于 2024-5-12 21:45:40 | 显示全部楼层
本帖最后由 leozen 于 2024-5-12 21:48 编辑
foxmouse 发表于 2024-5-8 10:36
读的话解决了,写的话怎么实现?

没理解你意思。我当时是因为小米手表位置偏移,想用函数修正,需要用到gps数据。你如果是同样需求的话,看debug的信息,复制gps数据的路径粘贴就行了。
我在网上找的函数库改了下,来源应该是这里:https://www.inte.net/news/273411.html
/**
 *  判断经纬度是否超出国境
 */
function isLocationOutOfChina(latitude, longitude) {
    if (longitude < 72.004 || longitude > 137.8347 || latitude < 0.8293 || latitude > 55.8271)
        return true;
    return false;
}


/**
 *  将WGS-84(国际标准)转为GCJ-02(火星坐标):
 */
function transformFromWGSToGCJ(latitude, longitude) {
    var lat = "";
    var lon = "";
    var ee = 0.00669342162296594323;
    var a = 6378245.0;
    var pi = 3.14159265358979324;

    if (isLocationOutOfChina(latitude, longitude)) {
        lat = latitude;
        lon = longitude;
    }
    else {
        var adjustLat = transformLatWithXY(longitude - 105.0, latitude - 35.0);
        var adjustLon = transformLonWithXY(longitude - 105.0, latitude - 35.0);
        var radLat = latitude / 180.0 * pi;
        var magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        var sqrtMagic = Math.sqrt(magic);
        adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        latitude = latitude + adjustLat;
        longitude = longitude + adjustLon;
    }
    return { latitude: latitude, longitude: longitude };

}

/**
 *  将GCJ-02(火星坐标)转为百度坐标:
 */
function transformFromGCJToBaidu(latitude, longitude) {
    var pi = 3.14159265358979324 * 3000.0 / 180.0;

    var z = Math.sqrt(longitude * longitude + latitude * latitude) + 0.00002 * Math.sin(latitude * pi);
    var theta = Math.atan2(latitude, longitude) + 0.000003 * Math.cos(longitude * pi);
    var a_latitude = (z * Math.sin(theta) + 0.006);
    var a_longitude = (z * Math.cos(theta) + 0.0065);

    return { latitude: a_latitude, longitude: a_longitude };
}

/**
 *  将百度坐标转为GCJ-02(火星坐标):
 */
function transformFromBaiduToGCJ(latitude, longitude) {
    var xPi = 3.14159265358979323846264338327950288 * 3000.0 / 180.0;

    var x = longitude - 0.0065;
    var y = latitude - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi);
    var a_latitude = z * Math.sin(theta);
    var a_longitude = z * Math.cos(theta);

    return { latitude: a_latitude, longitude: a_longitude };
}

/**
 *  将GCJ-02(火星坐标)转为WGS-84:
 */
function transformFromGCJToWGS(latitude, longitude) {
    var threshold = 0.00001;

    // The boundary
    var minLat = latitude - 0.5;
    var maxLat = latitude + 0.5;
    var minLng = longitude - 0.5;
    var maxLng = longitude + 0.5;

    var delta = 1;
    var maxIteration = 30;

    while (true) {
        var leftBottom = transformFromWGSToGCJ(minLat, minLng);
        var rightBottom = transformFromWGSToGCJ(minLat, maxLng);
        var leftUp = transformFromWGSToGCJ(maxLat, minLng);
        var midPoint = transformFromWGSToGCJ((minLat + maxLat) / 2, (minLng + maxLng) / 2);
        delta = Math.abs(midPoint.latitude - latitude) + Math.abs(midPoint.longitude - longitude);

        if (maxIteration-- <= 0 || delta <= threshold) {
            return { latitude: (minLat + maxLat) / 2, longitude: (minLng + maxLng) / 2 };
        }

        if (isContains({ latitude: latitude, longitude: longitude }, leftBottom, midPoint)) {
            maxLat = (minLat + maxLat) / 2;
            maxLng = (minLng + maxLng) / 2;
        }
        else if (isContains({ latitude: latitude, longitude: longitude }, rightBottom, midPoint)) {
            maxLat = (minLat + maxLat) / 2;
            minLng = (minLng + maxLng) / 2;
        }
        else if (isContains({ latitude: latitude, longitude: longitude }, leftUp, midPoint)) {
            minLat = (minLat + maxLat) / 2;
            maxLng = (minLng + maxLng) / 2;
        }
        else {
            minLat = (minLat + maxLat) / 2;
            minLng = (minLng + maxLng) / 2;
        }
    }

}

function isContains(point, p1, p2) {
    return (point.latitude >= Math.min(p1.latitude, p2.latitude) && point.latitude <= Math.max(p1.latitude, p2.latitude)) && (point.longitude >= Math.min(p1.longitude, p2.longitude) && point.longitude <= Math.max(p1.longitude, p2.longitude));
}

function transformLatWithXY(x, y) {
    var pi = 3.14159265358979324;
    var lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
    lat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    lat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
    lat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
    return lat;
}

function transformLonWithXY(x, y) {
    var pi = 3.14159265358979324;
    var lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
    lon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    lon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
    lon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
    return lon;
}

/**
 *  将百度转为WGS-84:
 */
function transfromBaidutoWGS84(latitude, longitude){
    if (!isNaN(latitude)&&!isNaN(longitude)){
        var lat = parseFloat(latitude);
        var lon = parseFloat(longitude);
        let res= transformFromBaiduToGCJ(lat, lon);
        let resWGS = transformFromGCJToWGS(res.latitude,res.longitude);
        return {latitude: resWGS.latitude.toFixed(6), longitude: resWGS.longitude.toFixed(6)};
    }else{
        return { latitude: '0', longitude: '0' };
    }

}
/**
 *  将WGS-84转为百度:
 */
function transfromWGS84toBaidu(latitude, longitude){
    if (!isNaN(latitude)&&!isNaN(longitude)){
        var lat = parseFloat(latitude);
        var lon = parseFloat(longitude);
        let res= transformFromWGSToGCJ(lat, lon);
        let resBaidu = transformFromGCJToBaidu(res.latitude,res.longitude);
        return {latitude: resBaidu.latitude.toFixed(6), longitude: resBaidu.longitude.toFixed(6)};
    } else{
        return { latitude: '0', longitude: '0' };
    }

}

//ule.exports = {
    //isLocationOutOfChina: isLocationOutOfChina,
   // transformFromWGSToGCJ: transformFromWGSToGCJ,
   // transformFromGCJToBaidu: transformFromGCJToBaidu,
    //transformFromBaiduToGCJ: transformFromBaiduToGCJ,
  //  transformFromGCJToWGS: transformFromGCJToWGS,
   // transfromBaidutoWGS84:transfromBaidutoWGS84,
    //transfromWGS84toBaidu:transfromWGS84toBaidu
//


msg.location = transfromBaidutoWGS84(parseFloat(msg.data.attributes.latitude), parseFloat(msg.data.attributes.longitude));
//g.location = transformFromGCJToBaidu(parseFloat(msg.location0.latitude), parseFloat(msg.location0.longitude));

msg.payload = {
    "source_type": msg.data.attributes.source_type,
    "longitude": msg.location.longitude ,
    "latitude": msg.location.latitude,
    "gps_accuracy": msg.data.attributes.gps_accuracy,
    "battery_level": msg.data.attributes.battery_level,
    "timestamp": msg.data.attributes.last_updated,
    "current_address": msg.payload,
 };

return msg;


回复

使用道具 举报

6

主题

153

帖子

578

积分

高级会员

Rank: 4

积分
578
金钱
425
HASS币
0
发表于 2024-5-14 10:03:05 | 显示全部楼层
leozen 发表于 2024-5-12 21:45
没理解你意思。我当时是因为小米手表位置偏移,想用函数修正,需要用到gps数据。你如果是同样需求的话,看 ...

还没搞到这一块,先mark一下,谢谢分享
总为浮云能避日,长安不见使人愁
回复

使用道具 举报

6

主题

94

帖子

843

积分

高级会员

Rank: 4

积分
843
金钱
749
HASS币
0
发表于 2024-5-17 17:34:42 | 显示全部楼层
leozen 发表于 2024-5-12 21:45
没理解你意思。我当时是因为小米手表位置偏移,想用函数修正,需要用到gps数据。你如果是同样需求的话,看 ...

谢谢大佬分享,已经明白payload格式了
回复

使用道具 举报

4

主题

78

帖子

579

积分

高级会员

Rank: 4

积分
579
金钱
501
HASS币
0
 楼主| 发表于 2024-5-20 11:47:37 | 显示全部楼层
foxmouse 发表于 2024-5-17 17:34
谢谢大佬分享,已经明白payload格式了

惭愧,我只是文科生小白。折腾了米兔手表后,好长一段时间来,这个米兔手表信息根本不自动同步到米家,白折腾了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-25 00:42 , Processed in 0.086514 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表