Skip to content

Expose NWK address as hex value through REST API - #8199

Merged
manup merged 12 commits into
dresden-elektronik:masterfrom
SwoopX:nwkaddress
May 6, 2025
Merged

Expose NWK address as hex value through REST API#8199
manup merged 12 commits into
dresden-elektronik:masterfrom
SwoopX:nwkaddress

Conversation

@SwoopX

@SwoopX SwoopX commented Apr 30, 2025

Copy link
Copy Markdown
Collaborator

Currently, the NWK address is exposed as decimal value. Exposing it as hex value may aid in expediting debugging, as some log line only provide the NWK address.

@ebaauw

ebaauw commented May 1, 2025

Copy link
Copy Markdown
Collaborator

This will work, but would it be cleaner to implement this logic only once, in ResourceItem::toVariant()? Ideally, I’d create a subclass for RAttrNwkAddress, but that seems too big a change.

@SwoopX

SwoopX commented May 2, 2025

Copy link
Copy Markdown
Collaborator Author

This will work, but would it be cleaner to implement this logic only once, in ResourceItem::toVariant()? Ideally, I’d create a subclass for RAttrNwkAddress, but that seems too big a change.

@ebaauw Thanks Erik for the great suggestion. I always tend to overlook those functions, dunno why :) If @manup agrees, I'm more than happy to move the code into the toVariant() function.

@manup

manup commented May 2, 2025

Copy link
Copy Markdown
Member

Hmm no not really. I'm against bringing item specifics into toVariant() or even ResourceItem this method should know nothing about specific items. The moment we start this it would grow to another monster function over time. toVariant() is called in myny context which aren't export to REST-API. Note the RAttrNwkAddress item was never meant to be exposed to the API, it's main purpose is for internal addressing in the Device code.

Lets take a step back to see what this is about: exposing the item to the REST-API and here the external presentation is different from the internal one.

I'd propose for now we add a free standing helper function in resource.h/cpp.

QVariant R_ItemToRestApiVariant(const ResourceItem *item)
{
    if (item)
    {
        const ResourceItemDescriptor *rid = item->descriptor();
        if (rid.suffix == RStateUnicorn)
        {
            // do special stuff
            // return special stuff
        }
        return item->toVariant();
    }
    return {};
}

In future this can be specified genericly in the item JSON descriptions like "this item should be exposed as hex string in the REST API". I have some plans here but this will take a while.

@ebaauw

ebaauw commented May 3, 2025

Copy link
Copy Markdown
Collaborator

I'm against bringing item specifics into toVariant() or even ResourceItem this method should know nothing about specific items.

We currently have quite some resource item descriptor specific logic in ResourceItem::toString(), which is called by ResourceItem::toVariant(). To me this is cleaner (less dirty) than having a helper function outside the ResourceItem class.

In future this can be specified genericly in the item JSON descriptions

I like that, but that's a significant refactor, probably beyond the scope of this PR.

@SwoopX

SwoopX commented May 5, 2025

Copy link
Copy Markdown
Collaborator Author

Let's start small and then let it grow as necessary 😉

Made the suggested changes hoping they're ok.

@manup manup left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice almost there, I have two small change requests.

Comment thread resource.cpp Outdated
Comment thread resource.cpp
const ResourceItemDescriptor rid = item->descriptor();
if (rid.suffix == RAttrNwkAddress)
{
return QString("0x") + QString("%1").arg(item->toNumber(), 4, 16, QLatin1Char('0')).toUpper();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest a small change to save two QString constructions :)

return QString("0x%1").arg(item->toNumber(), 4, 16, QLatin1Char('0')).toUpper();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid, I did that on purpose, as the suggested amendment does not keep the small letter X. See the output of

dbgCluster = QString("0x%1").arg(ind.clusterId(), 4, 16, QLatin1Char('0')).toUpper();
dbgCmd = QString("0x%1").arg(zclFrame.commandId(), 2, 16, QLatin1Char('0')).toUpper();
in the respective logs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok indeed that makes sense. Until the item.json display extensions are ready that's ok for now.

@manup manup added this to the v2.30.1 milestone May 6, 2025
@manup
manup merged commit 5a5d9f9 into dresden-elektronik:master May 6, 2025
@SwoopX
SwoopX deleted the nwkaddress branch May 16, 2025 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants