Homey Community Forum

Help with Homeyscript!

Hi! With the Homeyscript I can see new possibilities, but I am totally useless on this, appearently! I have no background in programming, but have a good sense of how it works and what can be done. With that being said, I am in desperate need of assistance.
Background: Me and some friends are in to photographing and flying drones, and share our work. Currently, we are using a P2P SW for sharing with automatic downloads. I would like to get notified when AirDC (SW) starts a new download.
AirDC have a good API library that I intend to use, the one I think I should use is this one

The javascript (I guess javascript is okay) looks like this:

I would like to execute this script frequently and if a new download is added, I want to return the attribute “name” of the folder and via Homey create a push notification:

Please, I need some input if I am on the right track here

Seems to me it’s much easier for you to use the HTTP request flow cards app.

Thanks @robertklep, thank you for taking the time to read my post and respond.
At this point, I am very open for any solution! I actually have that app, so why not?! :wink:

I guess I can use the Javascript as is?

XMLHttpRequest won’t work in Homeyscript, you need to use fetch.

Thanks!
I hope it supports fetch.
I will test this and see if I get any wiser!

request.send(); does not seem to work in Homeyscrit either
Any ideas?

Yes: look at the page about fetch that I linked to :stuck_out_tongue: fetch has completely different calling semantics than XMLHttpRequest

Oh, sorry!
I tries the respond.status just to see if I got any status (working/not working). But I only got this output:
:x: Script Error
:warning: ReferenceError: response is not defined
at airdctest.js:11:1
at processTicksAndRejections (internal/process/task_queues.js:97:5)

Can I assume that this is not supported by AirDCs API?!

No, it means your code isn’t correct.

I’ll subtly refer back to my earlier post:stuck_out_tongue_winking_eye:

Yeah, thanks @robertklep!
I know that it is like talking to a five year old. :slight_smile:
I have read the information, but problem is that I do not fully understand the mechanisms so I more or less pick parts on chance.

var request = new fetch(‘https://private-anon-3359b479e7-airdcpp.apiary-mock.com/queue/bundles/{0}/{100}’);

request.onreadystatechange = Function(Response) {

if (this.readyState === 4) {

console.log('Status:', this.status);

console.log('Headers:', this.getAllResponseHeaders());

console.log('Body:', this.responseText);

}

};

response.Headers()

I’ll quote a part from the page I linked to:

A basic fetch request is really simple to set up. Have a look at the following code:

fetch('http://example.com/movies.json')
.then(response => response.json())
 .then(data => console.log(data));

Like I said, XMLHttpRequest and fetch have completely different calling semantics. You just replaced XMLHttpRequest with fetch, but all the other code that’s left in place is specific to XMLHttpRequest and won’t work with fetch.

Thanks @robertklep! I really appreciate your support. I would like to emphasize that I am not lazy, I have read and tried and read some more before posting here.
But when I don’t get it right I tried to get some help here. I will try this. Thanks again

Well, I did some more reading, and some more testing. Are there someone that can help me translate the example code to something that works in Homeyscript or HTTP request flow cards?

var request = new XMLHttpRequest();

request.open(‘GET’, ‘https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/{start}/{count}’);

request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log(‘Status:’, this.status);
console.log(‘Headers:’, this.getAllResponseHeaders());
console.log(‘Body:’, this.responseText);
}
};

request.send();

The attribute I want to return is “name”.

@Exile82, Look at the example that @robertklep gave to you. There is really not much more to do. The data object should contain the name attribute.

const url = “https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/{start}/{count}”;

await fetch(url).then(data => data.json()).then(data => console.log(data[0].name));

// define url
const url = “https://private-anon-3c2bdff3aa-airdcpp.apiary-mock.com/queue/bundles/{start}/{count}”;
// get data
let data = await fetch(url);
// make it json
data = await data.json();
// pick name from json data
let name = data[0].name;
// display it
console.log(name);

1 Like

Thank you so much guys, now it turns up in console.log