Refactor: Sequences for services

Some writing functions such as setting clock or
resetting the energy require a sequence, e.g.
log-in, write, log-off
to actually work.

Signed-off-by: Patrick Menschel <menschel.p@posteo.de>
This commit is contained in:
Patrick Menschel 2021-12-28 13:45:42 +01:00
parent 83bedbe48b
commit cc3e69d385
2 changed files with 18 additions and 10 deletions

View File

@ -138,12 +138,22 @@ sub write_reg($$) {
return $self;
};
sub get_values() {
my $self = shift;
my $res;
unless (ref $self){croak "call with an object, not a class";}
$self->start_communication()->start_programming_mode()->update_values()->log_off();
return $self->regs;
};
sub set_clock() {
my $self = shift;
my $res;
unless (ref $self){croak "call with an object, not a class";}
$self->start_communication()->start_programming_mode();
$res = $self->write_reg(31, _scale_datetime_to_raw_time(DateTime->now()));
# this function reliably returns an ACK
$self->log_off();
return $self;
};
@ -151,8 +161,10 @@ sub reset_energy() {
my $self = shift;
my $res;
unless (ref $self){croak "call with an object, not a class";}
$self->start_communication()->start_programming_mode();
$res = $self->write_reg(0x40, "00000000");
# this function does not reliably return an ACK, to be checked
$self->log_off();
return $self;
};

View File

@ -33,22 +33,18 @@ for my $id (@ids) {
my $drs110m = iec1107->new("port"=>$port,"id"=>$id,"passwd"=>$passwd);
print("Meter: $id\n");
# $drs110m->start_communication()->start_programming_mode()->update_values();#this function concatenation is neat but absolutely destroying readability
$drs110m->start_communication();
$drs110m->start_programming_mode();
$drs110m->set_clock();
#$drs110m->set_clock();
#$drs110m->reset_energy();
my $values = $drs110m->get_values();
$drs110m->update_values();
while ( my ($reg, $val) = each(%{$drs110m->regs})){#Note: this type switching in perl is crazy!
while ( my ($reg, $val) = each(%{$values})){#Note: this type switching in perl is crazy!
print("$reg : $val\n");
};
#print("log off from $serialID\n");
$drs110m->log_off();
print("log off from $id\n");
}