ConnectedHumber / Air-Quality-Web

The web interface and JSON api for the ConnectedHumber Air Quality Monitoring Project.
https://sensors.connectedhumber.org/
Mozilla Public License 2.0
9 stars 4 forks source link

Display the 'last seen' for devices #36

Closed sbrl closed 4 years ago

sbrl commented 5 years ago

Now that we're acquiring a number of devices (and the naming scheme, while great at defining a device's characteristics, is terrible at making it easy to tell devices apart quickly), we should probably look into implementing a 'last seen' feature, so that it becomes obvious which devices are currently offline.

bsimmo commented 5 years ago

'Last Seen' is not working correctly.

This may be for some setups. Since moving in to release from beta, last seen is not working correctly. It's time frame seems off.

Windows10 Chrome Windows 10 Edge (Chromium based dev build) Android Chrome

All exhibit this for me, seems consistent. After clearing out all the cache etc (shoudn't need ot be done for a user). It (seems it) is taking the first time the tab-data for a that sensor was viewed as the "time ago" point.

Windows10 Edge (current EdgeHTML based), i'm unsure as everything seems fine at the moment, just HCCSENSOR02 is showing 1 day ago, but graph is up to date.

sbrl commented 5 years ago

Ah, I think it's a caching issue @bsimmo. I should fix that.

bsimmo commented 5 years ago

Just been having a play with this as I realised the new api went live and I haven't tried it. Is it possible to add the "last seen" also to the nearest location.

For my programming usage,

- I ask for the nearest three sensors to my location.
- Loop : 
-- I requesting the _device-info&device-id_ for the sensor
-- Check the last seen against current time and if within 15 mins I take it as a live sensor (for example)
-- If not 'live', loop to next sensor with new info request
etc.
I now know which sensor is my current live sensor.

It would be easier for me if I could

- ask for nearest three sensors
- loop -- Check the last seen against current time and if within 15 mins I take it as a live sensor (for example)
I now know which sensor is my current live sensor.

Is it as simple as adding the * line here ?

    public function get_near_location(float $lat, float $long, int $count) {
        $s = $this->get_static;

        $result = $this->database->query(
            "SELECT
                {$s("table_name")}.{$s("column_device_id")} AS id,
                {$s("table_name")}.{$s("column_device_name")} AS name,
                {$s("table_name")}.{$s("column_lat")} AS latitude,
                {$s("table_name")}.{$s("column_long")} AS longitude,
                ST_DISTANCE_SPHERE(POINT(:latitude, :longitude), {$s("table_name")}.{$s("column_point")}) AS distance_calc
*               MAX($data_repo_col_datetime) AS last_seen
            FROM {$s("table_name")}
*               JOIN $data_repo_table_meta ON
*                    $data_repo_col_device_id = {$s("table_name")}.{$s("column_device_id")}"
            WHERE {$s("table_name")}.{$s("column_point")} IS NOT NULL
            ORDER BY ST_DISTANCE_SPHERE(POINT(:latitude_again, :longitude_again), {$s("table_name")}.{$s("column_point")})
            LIMIT :count;", [
                "latitude" => $lat,
                "longitude" => $long,
                "latitude_again" => $lat,
                "longitude_again" => $long,
                "count" => $count
            ]
        )->fetchAll();

        // Calculate the *actual* distance in metres.
        // This is complicated and requires nasty formulae, so we're using a library here
        // FUTURE: Apparently said library supports caching with PSR-6 - maybe we could take advantage of a PSR-6 implementation both here and elsewhere?
        $loc = new Coordinate($lat, $long);
        foreach($result as &$item) {
            $item["distance_actual"] = $this->distance_calculator->getDistance(
                $loc,
                new Coordinate(
                    floatval($item["latitude"]),
                    floatval($item["longitude"])
                )
            );
        }

        return $result;
    }
sbrl commented 5 years ago

Hey! Almost, but it should be pretty easy to add last_seen to list-devices-near.

I've made the change in a678a89b952e2131a697cf12fc917c66b900af5d, so it should be live at https://aq.connectedhumber.org/beta/api.php 10 minutes after this comment is posted.

bsimmo commented 4 years ago

Now that we're acquiring a number of devices (and the naming scheme, while great at defining a device's characteristics, is terrible at making it easy to tell devices apart quickly), we should probably look into implementing a 'last seen' feature, so that it becomes obvious which devices are currently offline.

  • [x] Return last-seen information in the HTTP API
  • [x] Display the last time a device was seen in the popup
  • [ ] Change the colour of the marker for offline devices

Can this be closed now, the last target is not needed since they now are removed from the map?

and/or open a new ticket for enhancement, e.g. colour markers for data over an hour old (no recent data) and over a day old etc.. to give an indication of problems on the map?

sbrl commented 4 years ago

Good idea, we can close this issue now @bsimmo.

I think I've got an issue open for the marker colour already, but I can't remember which one it is