App-Device-Chip-sensor

 view release on metacpan or  search on metacpan

bin/device-chip-sensor-exporter  view on Meta::CPAN

#!/usr/bin/perl

use v5.26;
use warnings;
use experimental 'signatures';

use utf8;

use Object::Pad 0.66;

use Future::AsyncAwait;
use Future::IO::Impl::IOAsync;

use Net::Prometheus;
use Metrics::Any::Adapter "Prometheus";
use Metrics::Any '$metrics';
our $metrics;

eval { require Future::AsyncAwait::Metrics };

$metrics->make_counter( chip_failures =>
   name        => "app_device_chip_sensor_chip_failures",
   description => "Number of times a failure has been reported, per chip",
   labels      => [qw( chip )],
);

STDOUT->autoflush(1);
STDOUT->binmode( ":encoding(UTF-8)" );

my $PORT;
my $VERBOSE;

my $STRFTIME = "%Y-%m-%d %H:%M:%S";

class MyApp :isa(App::Device::Chip::sensor)
{
   use Carp;
   use POSIX qw( strftime );
   use Scalar::Util qw( refaddr reftype );

   method OPTSPEC
   {
      return $self->SUPER::OPTSPEC, (
         'port=i'     => \$PORT,
         'verbose+'   => \$VERBOSE,
         'config|c=s' => sub { $self->read_config_file( $_[1] ); },
      );
   }

   method read_config_file ( $path )
   {
      require YAML;

      # Config file is YAML and can override a number of settings, and add chips
      my $config = YAML::LoadFile( $path );

      if( $config->{adapters} ) {
         foreach my $c ( ( delete $config->{adapters} )->@* ) {
            my $type = delete $c->{type} // croak "Expected adapter config to have a 'type'";
            my $chips = delete $c->{chips} // croak "Expected adapter config to have a 'chips'";

            # TODO: It'd be really nice if Device::Chip::Adapter->make( $type, %opts ) existed
            my $desc = $type . join( ":", "", map { "$_=$c->{$_}" } sort keys %$c );
            my $adapter = Device::Chip::Adapter->new_from_description( $desc );

            foreach my $c ( $chips->@* ) {
               my ( $type, %config ) = ( reftype($c)//"" eq "HASH" )
                  ? ( delete $c->{type}, $c->%* )
                  : ( $c, () );

               $self->add_chip(
                  type      => $type,
                  adapter   => $adapter,
                  mountopts => $config{mountopts},
                  config    => $config{config},
               );
            }
         }
      }

      if( exists $config->{port} ) {
         $PORT = $config->{port};
         delete $config->{port};
      }

      foreach my $opt (qw( interval best_effort filter )) {
         exists $config->{$opt} and
            $self->$opt = delete $config->{$opt};



( run in 1.083 second using v1.01-cache-2.11-cpan-39bf76dae61 )