Help: Bearer Authentication

Posted on
Sat Nov 07, 2020 8:35 pm
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

How could you suggest I could reliably map the correct index to a variable?

JP


Sent from my iPhone using Tapatalk Pro

Posted on
Sat Nov 07, 2020 10:23 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

roquej wrote:
How could you suggest I could reliably map the correct index to a variable?

JP

You shouldn't need to map the value to a variable since the purpose of the plugin is to add device states that include all of the values in the returned payload. Saving the value to a variable is an unneeded extra step IMO.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Nov 08, 2020 9:19 am
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

Is probably just me being think. However, not getting it.

Let’s assume that I want, out of the 5 values provided, to know the temp value. However, if you look at the values:

data_0_sensors_0_value
data_0_sensors_1_value
data_0_sensors_2_value
data_0_sensors_3_value
data_0_sensors_4_value

Which is temp? I can look at the value in the filed “data_0_sensors_X_comp” to see which one is “temp” and then use the value for “data_0_sensors_X_value“. However, “temp” moves around fairly often between the fields. Makes sense?

Again, I may just not be getting it, but guidance would be appreciate it.

JP

Posted on
Sun Nov 08, 2020 9:53 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

Seems we're interested in this bit of the payload:

Code: Select all
u'sensors': [{u'comp': u'humid', u'value': 53.310001373291016}, {u'comp': u'temp', u'value': 78.0980010986328}, {u'comp': u'voc', u'value': 397.0}, {u'comp': u'dust', u'value': 75.30000305175781}, {u'comp': u'co2', u'value': 661.0}]

This payload component is a list of dictionaries. What I'm understanding you to say is that sometimes the temperature value is the second dictionary in the list and sometimes its in another position. If you ask me, that's bad form on the part of the data service. These data elements *should* always at least be in the same position. Not sure why they would've done it this way.

Finding the temperature value with a Python script or a purpose-built plugin would be extremely straightforward. Unfortunately, the GhostXML plugin is always going to parse the data in the way you see it now and I don't know how we can adequately handle your situation. As you can imagine, trying to code something to handle every possible data construction isn't something that's likely to be possible. Wish I had better news.

But you should be able to locate another custom state called something like 'data_0_sensors_X_comp' which will have the value of 'temp'. This will be your key to know which position the temperature value is located. That said, a custom script may be the easier path for you..

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Nov 08, 2020 9:57 am
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

Ok - I wasn’t crazy and yes, not sure why they did it that way. I got it working via a simple, crude script that test for ‘temp’ in the index and then uses the value of the corresponding ‘value’ field.

Thank you again!

JP


Sent from my iPhone using Tapatalk Pro

Posted on
Sun Nov 08, 2020 12:02 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

Yes, that would be the best way I think.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Nov 09, 2020 10:33 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help: Bearer Authentication

This is poor API design. Lists are, by definition, ordered. If you change the contents of the list randomly, then a dictionary (which is generally considered unordered) is a better solution. In this case, I would have made it a dictionary, using the 'comp' value as the key since that's what you use to determine how to interpret the value.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Nov 09, 2020 10:41 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

Agreed. It should be (as Jay said):

Code: Select all
{'temp': XX, 'humid': XX, 'voc': XX, 'dust': XX, 'co2': XX}

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Nov 09, 2020 12:18 pm
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Help: Bearer Authentication

All sounds great, but way over my skill level. Any way that a bit for an example could be provided on how to code using the dictionary approach?

Would be greatly appreciated.

FYI - reconfirmed with many tests, the API keeps changing the order of the list!

JP

Posted on
Mon Nov 09, 2020 12:45 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help: Bearer Authentication

The device maker would have to change their JSON API, which doesn't seem likely (changing APIs is even more painful than getting it wrong in the first place, which is why it's super important to put a lot of thought into it before releasing).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Nov 09, 2020 1:08 pm
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

Got it. Yep, agree and will send them an email stating it.

Thank you!

JP


Sent from my iPhone using Tapatalk Pro

Posted on
Mon Nov 09, 2020 3:10 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help: Bearer Authentication

Note - their JSON is usable as is, because it's possible to pull out the necessary data when you understand their specific API. It just requires more work to parse it out because it's a more complex and dynamic structure (which I believe wasn't really necessary). Dave's plugin would have worked fine with a better structure without having to know the details of this specific API.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Who is online

Users browsing this forum: No registered users and 2 guests