use POSIX;
use Device::SerialPort;
use Data::Dumper;
-use Proc::Daemon;
use SVG;
approx_upper_rate => 4000,
plot_lower_limit => 0,
plot_upper_limit => 4000,
- pidfile => "./coral_scanner.pid",
+ pid_file => "./".__PACKAGE__.".pid",
+ log_file => "./".__PACKAGE__.".log",
};
$self->{settings_desc} = {
time_per_pixel => "time in seconds to integrate the counts of the PMT at a given coordinate",
approx_upper_rate => "upper boundary of the counting rate in counts/sec,
is used for setting the value range of the plot",
- plot_lower_limit => "lower contrast setting for the plot",
- plot_upper_limit => "upper contrast setting for the plot",
- pidfile => "/path/to/file of the lockfile for the scanning background process",
+ plot_lower_limit => "lower contrast setting for the plot",
+ plot_upper_limit => "upper contrast setting for the plot",
+ pid_file => "/path/to/file of the lockfile for the scanning background process",
+ stdout => "/path/to/file of the stdout logfile",
+ stderr => "/path/to/file of the stderr logfile",
};
$self->{has_run} = {}; # remember which subs already have run
print "<input type='button' value='(re)plot' id='button_replot'>";
print "<a href='coral_scanner.pl?sub=scan_to_ascii' target='_blank' id='button_ascii'
><input type='button' value='scan to ascii'></a>";
+ print "<a href='coral_scanner.log' target='_blank' id='button_log'
+ ><input type='button' value='view log'></a>";
+ print "<input type='button' value='clear log' id='button_clearlog'>";
print br;
print br;
print "estimated scan duration: ".hms_string($self->scan_ETA());
my $scan_pattern = $tc->scan_pattern();
my $ETA = $self->scan_ETA();
+ print ">>> starting scan\n\n";
+
$self->{status_shm}->updateShm({
action => 'scanning',
abort => 0,
my $points_scanned = 0;
for my $point (@{$scan_pattern->{points}}) {
- $tc->go_xy( x => $point->{x}, y => $point->{y});
+ # attempt to drive to position
+ printf("Drive to point x,y = %3.3f,%3.3f i,j = %d,%d\n" ,$point->{x_rel},$point->{y_rel}, $point->{row},$point->{col});
+
+
+ eval {
+ $tc->go_xy( x => $point->{x}, y => $point->{y});
+ };
+ warn "error from table\n$@" if $@;
- printf("Acquire PMT counts at point x,y = %3.3f,%3.3f i,j = %d,%d\n" ,$point->{x_rel},$point->{y_rel}, $point->{row},$point->{col});
my $delay = $self->{settings}->{time_per_pixel};
+ printf("Acquiring PMT counts for %3.2f seconds\n",$delay);
my $counts = $ro->count(delay => $delay, channel => "signal");
my $col = $point->{col};
my $row = $point->{row};
$points_scanned += 1;
$self->{current_scan}->{data}->[$row]->[$col] = $counts;
- print "counts: $counts\n";
+ print "recorded counts: $counts\n";
print "\n\n";
my $status = $self->{status_shm}->lockAndReadShm();
};
$self->{status_shm}->writeShm($status);
$self->{scan_shm}->writeShm($self->{current_scan});
- print "scan was aborted!\n";
+ print ">>> scan was aborted!\n\n";
# last; # stop the acquisition loop!
exit;
} else {
seconds_left => 0
});
- $self->save_scan_ascii(filename => "./scan.dat");
-
+ print ">>> scan completed!\n\n";
-
-}
-
-
-sub save_scan_ascii {
- my $self = shift;
- my %options = @_;
-
- my $filename = $options{filename};
-
- my @darray = @{$self->{current_scan}->{data}};
-# @darray = sort {$a->{col} <=> $b->{col}} @darray;
-# @darray = sort {$a->{row} <=> $b->{row}} @darray;
-
- open(FILE,">$filename");
- for my $item (@darray){
-
- my $string = join("\t",@$item)."\n";
-# my $string = sprintf("%d\t%d\t%d\n",$item->{row},$item->{col},$item->{counts});
- print $string;
- print FILE $string;
- }
- close(FILE);
+ return "";
}
sub last_scan {
my $self = shift;
return $self->{scan_shm}->readShm();
+}
+sub clear_log {
+ my $self = shift;
+ eval {
+ unlink $self->{settings}->{log_file} or die "could not delete log file";
+ };
+ if ($@) {
+ print $@;
+ return "";
+ }
+ print "log file deleted\n";
+ return "";
}
sub start_scan {
my $self= shift;
- daemonize();
+
+ #start daemon
+ $self->daemon_start() or die "service could not be started (already running?)\n";
$self->scan_sample();
}
my %options = @_;
my $name = $options{name} || "signal";
- daemonize();
+ #start daemon
+ die "service could not be started (already running?)\n" unless ($self->daemon_start());
$self->{pmt_ro}->spectral_scan_onesided(
name => $name
);
- return " ";
+ return "";
}
sub home {
my $self= shift;
- daemonize();
+ #start daemon
+ die "service could not be started (already running?)\n" unless ($self->daemon_start());
+
+ print "homing the table\n";
+
$self->{status_shm}->updateShm({
action => "homing"
});
$self->{status_shm}->updateShm({
action => "idle"
});
+ print "homing completed\n";
+
+ return "";
}
sub stop_scan {
-
1;
package misc_subs;
use POSIX;
+use Proc::Daemon;
+
+use IO::Handle;
BEGIN {
require Exporter;
# set the version for version checking
- our $VERSION = 1.00;
+ our $VERSION = 1.01;
+
+ # revision history
+ # v1.01
+ # removed daemonize
+ # added daemon_start/stop/status based on Proc::Daemon
+
# Inherit from Exporter to export functions and variables
our @ISA = qw(Exporter);
# Functions and variables which are exported by default
- our @EXPORT = qw(printHeader min max echo require_run test hms_string daemonize false_color);
+ our @EXPORT = qw(
+ printHeader
+ min
+ max
+ echo
+ require_run
+ test hms_string
+ false_color
+ daemon_start
+ daemon_stop
+ daemon_status
+ );
# Functions and variables which can be optionally exported
#our @EXPORT_OK = qw($Var1 %Hashit func3);
}
return $string;
}
-sub daemonize {
- # chdir '/' or die "Can't chdir to /: $!";
- defined(my $pid = fork) or die "Can't fork: $!";
- if($pid){
-# printHeader('text/plain') if $isHttpReq;
- print "this instance has terminated, the other one is a demon now\n";
- exit;
- }
- open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
- open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
- open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
- POSIX::setsid or die "Can't start a new session: $!";
- umask 0;
-}
sub false_color {
my $val = shift;
}
+
+sub daemon_start{
+
+ my $self = shift;
+ my $pf = $self->{settings}->{pid_file};
+ my $log = $self->{settings}->{log_file};
+ my $daemon = Proc::Daemon->new(
+ pid_file => $pf,
+ work_dir => "./",
+ );
+
+ my $pid = $daemon->Status($pf);
+
+ if ($pid) {
+ print "Background service already running with pid $pid.\n";
+ return;
+ } else {
+ print "Not running. Starting background service\n";
+ $daemon->Init;
+ open(LOG,"+>>$log");
+ *STDERR = *LOG;
+ *STDOUT = *LOG;
+ LOG->autoflush;
+ return 1;
+ }
+}
+
+sub daemon_stop {
+ my $self = shift;
+ my $pf = $self->{settings}->{pid_file};
+ my $daemon = Proc::Daemon->new(
+ pid_file => $pf,
+ work_dir => "./",
+ );
+ my $pid = $daemon->Status($pf);
+
+ if ($pid) {
+ print "Stopping pid $pid...\n";
+ if ($daemon->Kill_Daemon($pf)) {
+ print "Successfully stopped.\n";
+ } else {
+ print "Could not find $pid. Was it running?\n";
+ }
+ } else {
+ print "Not running, nothing to stop.\n";
+ }
+}
+
+sub daemon_status {
+ my $self = shift;
+ my $pf = $self->{settings}->{pid_file};
+ my $daemon = Proc::Daemon->new(
+ pid_file => $pf,
+ work_dir => "./",
+ );
+ my $pid = $daemon->Status($pf);
+
+ if ($pid) {
+ print "Running with pid $pid.\n";
+ } else {
+ print "Not running.\n";
+ }
+ return $pid;
+}
+
+
+
1;
\ No newline at end of file