package App::POS::Hardware::Scales::RUBY_RK100;
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("none");
  $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;

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

    if ($count_in > 0) {
      $string_in =~ /^\W(\d\d\d\d)\s/;
      $s->debug("chars read: $count_in, string_read: #$string_in#");
      if (defined $1) { 
        my @a = split //,$1;
        @a = reverse @a;
        my $tmp = join('',@a);
        if (defined $tmp && $tmp ne '') {
          if ($tmp * 1 > 0) {
            $tmp = $tmp * 1 / 1000;
            $clean_after = 1;
            open(N, "> /opt/pos/common/tmp/scale.txt");
            print N sprintf("%.3f", $tmp)."\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;

