package App::POS::Hardware::Scales::DIBAL;
use warnings;
use strict;
use Time::HiRes qw( sleep );
use App::POS::Hardware::Scales;

use base qw( App::POS::Hardware::Scales );

sub setup_port {
  my $s = shift;

  my $PortObj = $s->serial_obj();
  $PortObj->baudrate(9600);
  $PortObj->parity("even");
  $PortObj->databits(8);
  $PortObj->stopbits(1);
  $PortObj->handshake("none");
}

sub start {
  my $s = shift;

  my $PortObj = $s->serial_obj();

  my $sleep = 0.2;
  my $clean_after = 0;
  my $str = chr(0x44).chr(0x0D).chr(0x0A);

  while (1) {
    $PortObj->write($str);
    my( $count_in, $string_in ) = $PortObj->read(1024);

    $s->debug("chars read: $count_in, string_read: #$string_in#");

    if ($count_in >= 5) {
      my( $num ) = $string_in =~ /(\d+\.\d+)/;

      if (defined $num && $num*1 > 0) {
        $clean_after = 1;
        open(N, "> /opt/pos/common/tmp/scale.txt");
        print N sprintf("%.3f", $num)."\n";
        close(N);
      }
      elsif ($clean_after == 1) {
        $clean_after = 0;
        open(N, "> /opt/pos/common/tmp/scale.txt");
        print N "0\n";
        close(N);
      }
    }

    sleep $sleep;
  }
}

1;

