#!/usr/bin/perl
#-------------------------------------------------------------
# Simple console client for fast and easy
# event inserting into ELog2. The following parameters
# are recognized:
#  -a		Asynchronous inserting
#  -n           Action name (create, update)
#  -s           Event source (numeric ID)
#  -e           Event type (numeric ID)
#  -o           Object Id
#  -m           Message
#  -f		From timestamp
#  -t 		To timestamp
#  -w		When timestamp
#  -i		Event Id (for update)
#  -p		Event properties, comma separated (prop1=val1,prop2=val2...)
#
# Examples of usage:
#
# Create alert event for object ID=101 with eventsource=4 (User) and default timestamps
# $ ./submitAction -o 101 -e 1 -s 4 -m 'test'
#
# Create info event for object 101 with eventsource=3 (Sensor) and specified timestamps
# $ ./submitActioin -o 101 -e 0 -s 3 -m 'test' -f 1370191600 -t 1370191645 -w 1370191620
#
# Update event with eventid=16, change message
# $ ./submitAction -n update -i 16 -m 'another message'
#-------------------------------------------------------------

use strict;
use warnings;

use lib "/opt/sarch/common/lib";
use lib "/opt/sarch/fw/bin/perl/FwIface";
use lib "/opt/sarch/fw/bin/perl";
use lib "/opt/sarch/elog/bin/perl";
use lib "/opt/sarch/elog/bin/perl/ELogIface";

use NextCAM::Servicing::Client;
use FwIface::Types;
use ELog;
use Getopt::Std;

# Parse cmd line
my %args = ();
getopts("an:N:P:o:e:s:f:t:w:W:m:p:i:l:", \%args);

# CONS
my $HOST = "s_master";
my $PORT = 10000;

# VARS
my $name = $args{n} || 'create';
my $async = $args{a};
my $eventsource = $args{s};
my $eventtype = $args{e};
my $objid = $args{o};
my $msg = $args{m};
my $note = $args{N};
my $utc_from = $args{f};
my $utc_to = $args{t};
my $utc_when = $args{w};
my $props = $args{p};
my $eventid = $args{i};
my $priority = $args{P};
my $lifespan = $args{l};
my $witnesses = $args{W};

# Create instance of elog client
my $service_client = NextCAM::Servicing::Client->new($HOST, $PORT);
my $elog_client = ELogClient->new($service_client->getProtocol("skm.eventlog"));

my %params;
$params{objid}     = $objid       if defined $objid;
$params{msg}       = $msg         if defined $msg;
$params{source}    = $eventsource if defined $eventsource;
$params{eventtype} = $eventtype   if defined $eventtype;
$params{from}      = $utc_from    if defined $utc_from;
$params{to}        = $utc_to      if defined $utc_to;
$params{when}      = $utc_when    if defined $utc_when;
$params{eventid}   = $eventid     if defined $eventid;
$params{note}	   = $note	  if defined $note;
$params{priority}  = $priority    if defined $priority;
$params{lifespan}  = $lifespan    if defined $lifespan;
$params{witnesses} = $witnesses   if defined $witnesses;

# Properties
if ($props) {
    my @pv = split(/,/, $props);
    foreach my $pv (@pv) {
        my ($prop,$val) = split /=/, $pv;
        if (defined $prop) {
    	    my $pname = "property.".$prop;
    	    $params{$pname} = $val;
	}
    }
}
    
my $action = Action->new({
    name => $name,
    parameters => \%params
});

my $resp;
my $err = '';
eval {
    $resp = $async ? $elog_client->submitActionA($action) : $elog_client->submitAction($action);
};
if ($@) {
    if (UNIVERSAL::isa($@, "FwException")) {
	$err = "FwException: ". $@->{errorId}.": ".$@->{what};
    }
    elsif (UNIVERSAL::isa($@, 'Thrift::TException')) {
	$err = "Thrift::TException";
    }
    else {
	$err = "Unknown error: $@";
    }
    
    die $err;
}
else {
    if ($resp and not $async and UNIVERSAL::isa($resp, 'ActionResponse') and $name eq 'create') {
	print $resp->{parameters}{eventid} . "\n";
    }
}
