Feat: Use datetime for time conversion
drop dependency Posix::strptime in favor of Datetime::Format::Strptime Signed-off-by: Patrick Menschel <menschel.p@posteo.de>
This commit is contained in:
		
							
								
								
									
										26
									
								
								iec1107.pm
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								iec1107.pm
									
									
									
									
									
								
							@@ -4,7 +4,7 @@
 | 
			
		||||
# This is my first my perl module and these resources were a good kickstart
 | 
			
		||||
# https://learn.perl.org/books/beginning-perl/
 | 
			
		||||
# https://wiki.volkszaehler.org/hardware/channels/meters/power/eastron_drs155m
 | 
			
		||||
# Menschel (C) 2020
 | 
			
		||||
# Menschel (C) 2020-2021
 | 
			
		||||
 | 
			
		||||
package iec1107; # we name our package iec1107 as this is the original protocol name
 | 
			
		||||
 | 
			
		||||
@@ -15,8 +15,7 @@ use Carp;
 | 
			
		||||
use Device::SerialPort;
 | 
			
		||||
 | 
			
		||||
#for time conversion
 | 
			
		||||
use POSIX::strptime qw( strptime );
 | 
			
		||||
use POSIX qw{strftime};
 | 
			
		||||
use DateTime::Format::Strptime qw( strptime );
 | 
			
		||||
 
 | 
			
		||||
#constants
 | 
			
		||||
our $SOH = chr(0x01);
 | 
			
		||||
@@ -34,14 +33,14 @@ our $ENDCHARACTER = "!";
 | 
			
		||||
 | 
			
		||||
our %drs110m_values = (
 | 
			
		||||
                   #'<measurement>'=>[<address>,<scalingfunction>,'<unit>'],
 | 
			
		||||
                   'Voltage'       =>[ 0,\&_scale_div_by_10,  'V'],
 | 
			
		||||
                   'Current'       =>[ 1,\&_scale_div_by_10,  'A'],
 | 
			
		||||
                   'Frequency'     =>[ 2,\&_scale_div_by_10, 'Hz'],
 | 
			
		||||
                   'Voltage'       =>[ 0,            \&_scale_div_by_10,  'V'],
 | 
			
		||||
                   'Current'       =>[ 1,            \&_scale_div_by_10,  'A'],
 | 
			
		||||
                   'Frequency'     =>[ 2,            \&_scale_div_by_10, 'Hz'],
 | 
			
		||||
                   'Active Power'  =>[ 3,            \&_scale_mul_by_10,  'W'],
 | 
			
		||||
                   'Reactive Power'=>[ 4,            \&_scale_mul_by_10,'VAr'],
 | 
			
		||||
                   'Apparent Power'=>[ 5,            \&_scale_mul_by_10, 'VA'],
 | 
			
		||||
                   'Active Energy' =>[10,               \&_scale_1_to_1, 'Wh'],
 | 
			
		||||
                   'Time'          =>[31,    \&_scale_to_time,   ''],
 | 
			
		||||
                   'Time'          =>[31, \&_scale_raw_time_to_datetime,   ''],
 | 
			
		||||
                   'Temperature'   =>[32,              \&_scale_to_temp, '°C'],
 | 
			
		||||
                  );
 | 
			
		||||
#actually there are more registers, but who cares about cosphi for example?!
 | 
			
		||||
@@ -177,16 +176,11 @@ sub _scale_1_to_1($){
 | 
			
		||||
  return $val;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
sub _scale_to_time($){
 | 
			
		||||
sub _scale_raw_time_to_datetime($){
 | 
			
		||||
  my ($str) = @_;
 | 
			
		||||
  my $fmt = "%y%m%d0%w%H%M%S";
 | 
			
		||||
  my @time = (POSIX::strptime($str,$fmt))[0..7];
 | 
			
		||||
  if (wantarray){
 | 
			
		||||
    return @time;
 | 
			
		||||
  }
 | 
			
		||||
  else{
 | 
			
		||||
    return strftime("%Y-%m-%d %H:%M:%S",@time);
 | 
			
		||||
  };
 | 
			
		||||
  my $fmt = "%y%m%d%w%H%M%S";
 | 
			
		||||
  my $dt = strptime($fmt, $str);
 | 
			
		||||
  return $dt;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
sub _scale_to_temp($){
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user