ISS alerter

Post your questions and help other users.

Moderator: Martin

Post Reply
robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

ISS alerter

Post by robchoc » 27 Oct 2018 11:18

I'm currently working on a script that will inform me when the ISS (International Space Station) is above my location.

More information about the api here http://open-notify.org/Open-Notify-API/

I have the URL with my longitude and latitude
http://api.open-notify.org/iss-pass.jso ... -2.1586632
Not even sure if I'm doing this the right way as they show this on the site:

Code: Select all

$.getJSON('http://api.open-notify.org/iss-pass.json?lat=45.0&lon=-122.3&alt=20&n=5&callback=?', function(data) {
    data['response'].forEach(function (d) {
        var date = new Date(d['risetime']*1000);
         $('#isspass').append('<li>' + date.toString() + '</li>');
    });
});
This is the reply I get back from the api:

Code: Select all

{
  "message": "success", 
  "request": {
    "altitude": 100, 
    "datetime": 1540638319, 
    "latitude": 52.6138046, 
    "longitude": -2.1586632, 
    "passes": 5
  }, 
  "response": [
    {
      "duration": 556, 
      "risetime": 1540642292
    }, 
    {
      "duration": 481, 
      "risetime": 1540702554
    }, 
    {
      "duration": 621, 
      "risetime": 1540708228
    }, 
    {
      "duration": 643, 
      "risetime": 1540713992
    }, 
    {
      "duration": 641, 
      "risetime": 1540719778
    }
  ]
}
I only need the duration and the risetimes for my script which will eventually send me a page message to let me know when the ISS is above me.

What would be the best way to work with this data from the api?
so that I can achieve the following:

Get 5 sets of dates and times and also the duration for each set.

Look forward to your replies with this one and thanks in advance.

robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Re: ISS alerter

Post by robchoc » 27 Oct 2018 13:03

I tried the following but it does not seem to work as it did not even trigger the dubug dialog in the flow:

Code: Select all

data = fromJSON(issget);

duration1 = data[0]['duration'];
risetime1 = data[0]['risetime'];

duration2 = data[1]['duration'];
risetime2 = data[1]['risetime'];

duration3 = data[2]['duration'];
risetime3 = data[2]['risetime'];

duration4 = data[3]['duration'];
risetime4 = data[3]['risetime'];

duration5 = data[4]['duration'];
risetime5 = data[4]['risetime'];
This is the contents of my data variable:

Code: Select all

{message=success, request={altitude=100, datetime=1540643970, latitude=52.6138046, longitude=-2.1586632, passes=5}, response=[{duration=481, risetime=1540702554}, {duration=621, risetime=1540708228}, {duration=643, risetime=1540713992}, {duration=641, risetime=1540719778}, {duration=594, risetime=1540725567}]}

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: ISS alerter

Post by Desmanto » 27 Oct 2018 13:44

Use action http request and put
URL : http://api.open-notify.org/iss-pass.jso ... -2.1586632
The json will be saved to {response} (if you don't change it)

If you need to get your current location, you can look at this : viewtopic.php?f=5&t=7457
I have share the flow there to get the location and use the lat long in the url.

Then use this action script to parse the JSON

Code: Select all

iss = fromJSON(response);

show = newList();
for(i in iss["response"])
{
  time = i["risetime"] * 1000;
  time = "{time,dateformat,HH:mm:ss  dd-MMM-yyyy}";
  duration = i["duration"];
  addElement(show, "Time : {time}, duration : {duration} seconds");
}
Automagic is very JSON/XML friendly, which is why I love it so much. using fromJSON(), we will convert the {response} to a nested map/list object. The risetime and duration is list of 5 elements located at the iss["response"] (you can find this out by attaching condition debug dialog to it). So we create a temporary list holder {show}, then loop upon iss["response"]. Each loop will deal with each of 5 elements

The time is in seconds, so we have to times it with 1000 to get android miliseconds time. I don't if the website gives you the correct time based on the timezone of the location or your current location. In case any timezone error, you have to adjust it using addHours(). Show this time in the HH:mm:ss dd-MMM-yyyy, example 11:55:54 28-Oct-2018. The get the duration. Arrange them into a string and add it to the {show}.

As result, {show} contains 5 of the time and duration, each in string format. You can then use Action Message Dialog to show it.
Title : ISS Alerter
Message : {show,listformat}

@robchoc : It is fun right to try other script? :) I always try to solve as much as possible, so I can learn more. ;) For the script, when dealing with multiple elements, always use loop to make it easier to access repeated elements. I use for() loop in most cases, and only while() for some special case.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Re: ISS alerter

Post by robchoc » 27 Oct 2018 15:04

Thanks so much for that, you make it seem so easy.
I think I understand what is going on now.

I need to minus an hour as it is in UTC so I'm thinking I need to use addHours(-1, time), but where do I put that in the script as it does not seem to work for me.

I definitely need to learn a bit more about Json, for loops and data parsing with these types of scripts.

Thanks for all the help

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: ISS alerter

Post by Desmanto » 27 Oct 2018 16:42

addHours() put the time in the first parameter, it should be addHours(time, -1). So the script now becomes

Code: Select all

iss = fromJSON(response);

show = newList();
for(i in iss["response"])
{
  time = addHours(i["risetime"] * 1000, -1);
  time = "{time,dateformat,HH:mm:ss  dd-MMM-yyyy}";
  duration = i["duration"];
  addElement(show, "Time : {time}, duration : {duration} seconds");
}
I also know almost nothing about json last year. But today I can easily parse it using Automagic Script.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Re: ISS alerter

Post by robchoc » 28 Oct 2018 17:12

How would I just pick the first element and add the first Time into {time} and the duration into {duration}
Same question with just the 4th element.

I'm asking these questions just to get my head around how things work with Json parsing. :)

Thanks

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: ISS alerter

Post by Desmanto » 28 Oct 2018 18:30

Do you mean to access the first element time and duration? Use

Code: Select all

time1 = iss["response"][0]["risetime"] * 1000;
duration1 = iss["response"][0]["duration"];
Same with 4th element, just change the 0 to 3 (list index start from 0).

How can we determine whether to use "response" or 0? Depends on it is list or map. You can check in the debug dialog, tap the change value. The json structure obtained for the time and duration is a map-list-map.
First level : you got 3 keys (map) : "message", "request", "response". You need the response part, so to access it use iss["response"]. We are now in the ["response"] sublevel
Second level : you got 5 elements (list) : from index 0 to 4. You need the first element, so to access it use iss["response"][0]. We are now in the ["response"][0] sublevel
Third level : you got 2 keys (map) : "duration" and "risetime". To access each, use iss["response"][0]["duration"] and iss["response"][0]["risetime"].
There is no fourth level anymore, since the last value is number.

So it is not about the json, it is about the structure of the nested map/list. But of course, json usually have this kind of mixed structure.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

robchoc
Posts: 81
Joined: 20 Jun 2018 12:38

Re: ISS alerter

Post by robchoc » 28 Oct 2018 18:55

That is fantastic, I understand it 100% now.
That debug dialog is so useful and this is what made me understand the structure. Change value was a hidden gem for me.

Thank you so much for all the explanations. :)

Post Reply