#!/usr/bin/env perl
use strict;
use warnings;
use 5.026;
use Path::Tiny;

use Chart::Plotly;
use Chart::Plotly::Plot;
use Chart::Plotly::Trace::Scatter;

my @rows = path($ARGV[0])->lines_utf8({chomp=>1});

my @column_names = qw(humidity temperature battery);
my %data_for_src;
for my $row (@rows) {
    my %data;my $src;
    ($data{dt},$src,@data{@column_names}) =
        $row =~ m{
                     \A \s*
                     ( \d{4}-\d{2}-\d{2} \s+ \d{2}:\d{2}:\d{2} )
                     \s+
                     (?: [0-9a-f:]+ ):([0-9a-f]+) # only take the last byte of the MAC, it's enough
                     \s+
                     ( [0-9.]+ )
                     \s+
                     ( [0-9.]+ )
                     \s+
                     ( [0-9.]+ )
                     \s* \z
             }x or next;
    for my $column ('dt',@column_names) {
        push $data_for_src{$src}->{$column}->@*, $data{$column}
    }
}

my %plots;
for my $column (@column_names) {
    my $plot = Chart::Plotly::Plot->new(
        layout => { title => $column },
    );
    for my $src (sort keys %data_for_src) {
        my $trace = Chart::Plotly::Trace::Scatter->new(
            x => $data_for_src{$src}->{dt},
            y => $data_for_src{$src}->{$column},
            name => $src,
        );
        $plot->add_trace($trace);
    }
    $plots{$column} = $plot;
}

Chart::Plotly::show_plot(@plots{@column_names});
