#!/usr/bin/perl -w
#  $Id: probe 29580 2013-09-30 13:57:20Z teetov $
# -----------------------------------------------------------------------------
#  Purpose:
#    - probe the camera
#    - pre-tune camera befor for retriever (RC and Audio)
# -----------------------------------------------------------------------------
#  Call:
#    probe DEVID=123   
#    probe DEVID=123 PROBE=FAST  
#    probe DEVID=123 DEVIP=192.168.17.177 [ ... ]
#    probe DEVIP=192.168.17.177 USRNAME=admin PASSWD=pass PROBE=DEFINE
# -----------------------------------------------------------------------------
#  Does:
#   0. can be called without DEVID argument. 
#      In this case DEVIP,USRNAME,PASSWD should be provided in command line
#   1. load $APL_CONF/<DEVID>/conf if DEVID is provided
#   2. combine conf and args into one hash
#   3. connect to camera over http and read MODELID and FIRMWARE
#   4. report MODELID|FIRMWARE|STATUS to $APL_CONF/<DEVID>/conf.probe and stdout
#   5. if PROBE=DEFINE then get
#         IMAGESIZE_LIST,MEDIA_FORMAT_LIST
#         AUDIO_LIST,AUDIO_FORMAT_LIST
#         SNAPSHOT (picture)
#   6. set RC camera attributes    if mpeg4 | h264
#   7. set AUDIO camera attributes if AUDIO is on 
#   8. example output:
#       AUDIO_SET=OK
#       FIRMWARE=5.02
#       MODELID=Q1755
#       RC_SET=OK
#       STATUS=OK
#       -------------
#       AUDIO_SET=NONE
#       FIRMWARE=5.02
#       MODELID=Q1755
#       RC_SET=NONE
#       STATUS=OK
#       SNAPSHOT=/tmp/probe/192.168.17.177-12367576123.jpg
#       IMAGESIZE_LIST=640x480,480x360,320x240,240x180,176x144,160x120
#       MEDIA_FORMAT_LIST=mjpeg,h264
#       AUDIO_LIST=off,on
#       AUDIO_FORMAT_LIST=g711,g726,aac
#   9. sample errors: 
#       STATUS=ERROR: PCE-0001 [101] configuration is not found
#       STATUS=ERROR: PCE-0002 [101] USRNAME and PASSWD should provided
#       STATUS=ERROR: PCE-0003 [101] DEVIP is not defined
#       STATUS=ERROR: PCE-0500 [101] Device does not respond (http://207.107.163.123:80)
#       STATUS=ERROR: PCE-0401 [101] Authorization error 
#       STATUS=ERROR: PCE-0403 [101] Forbidden 
#       STATUS=ERROR: PCE-0030 [101] Cannot get MODELID
#       note: [101] is DEVID
#  10. warnings:
#       STATUS=WARNING: PCW-0001 [101] MODELID does not match configuration
#       STATUS=WARNING: PCW-0002 [101] FIRMWARE does not match configuration
#       STATUS=WARNING: PCW-0009 [101] PROBE=RESET is not supported
#       
# -----------------------------------------------------------------------------
#  Note:
#   1. CAMERAMODEL file is obsolite. ptz_axisv2.pl & ptz_udp.pl has to be modified
#   2. Script mast be in the directory .../camera/<BRAND>/bin/
# -----------------------------------------------------------------------------
#  Author: teetov, 03/22/10
#  Edited by:
#  QA by:
#  Copyright: videoNEXT Network Solutions, Inc, 2010
# -----------------------------------------------------------------------------
#
use strict;
use Device::Conf;
#use Data::Dumper;
# cons -------------------------------------------------------------------------
my $DEMO_DIR='/opt/demo/data';
my $SAMPLE_PATH="$ENV{APL_VAR}/probe/image";

# --------- check required parameters: DEVID,USRNAME,PASSWD,
my $conf=ProbeInit();  # uses <BRAND> from path and ARGV
ProbeErr("PCE-0001","configuration is not found") if not defined $conf->{DEVID}; 
ProbeErr("PCE-0003","DEVIP is not defined")       if not defined $conf->{DEVIP};
ProbeErr("PCE-0002","USRNAME and PASSWD should be provided") 
                 if not defined $conf->{USRNAME} or not defined $conf->{PASSWD};
ProbeWarn("PCW-0009","PROBE=RESET is not supported") if $conf->{PROBE} eq 'RESET';
#------------------------------------------------------------------------------
# Probe camera MODELID & FIRMWARE 
#------------------------------------------------------------------------------
my %result=(MODELID=>'1.0',FIRMWARE=>'1.0',STATUS=>'OK',RC_SET=>'NONE',AUDIO_SET=>'NONE');
my ($n1,$n2,$n3,$demo_id)=$conf->{DEVIP}=~/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
ProbeErr("PCE-0500","Wrong device address",$conf->{DEVIP}) if $n1+$n2+$n3!=0;
ProbeErr("PCE-0500","Location does not exists",$DEMO_DIR)  if !opendir(DIR,$DEMO_DIR);
my @list= grep { /^$demo_id-\w+\.\d+x\d+\.(mjpg|h264)/ } readdir(DIR);
closedir DIR;
ProbeErr("PCE-0500","Device does not exist","$DEMO_DIR/$demo_id.*") if not @list;
$list[0]=~/\d+-(\w+)/;
$result{MODELID}=$1;


#------------------------------------------------------------------------------
# get Sample picture 
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# get video and audio properties
#------------------------------------------------------------------------------
if($conf->{PROBE} eq 'DEFINE') {
 %result=(%result,(IMAGESIZE_LIST=>'CIF',MEDIA_FORMAT_LIST=>'mjpg',AUDIO_FORMAT_LIST=>'',AUDIO_LIST=>'disable:DISABLE',CAMERA_LIST=>'1'));
 my %size;
 my %media;
 my $src;
 foreach(@list) {
   next if not /^(\d+-\w+\.(\d+x\d+))\.(\w+)$/; 
   $src=$1 if not $src;
   $size{$2}=1;
   $media{$3}=1;
 } 
 $result{MEDIA_FORMAT_LIST}=join',',keys %media;
 $result{IMAGESIZE_LIST}=join',',keys %size;
 $result{SNAPSHOT_LIST}='1:';
 if( -f "$DEMO_DIR/$demo_id-$result{MODELID}.jpg") {
   `cp $DEMO_DIR/$demo_id-$result{MODELID}.jpg $SAMPLE_PATH/$conf->{DEVIP}-1-$demo_id.jpg >/dev/null 2>/dev/null`;
    $result{SNAPSHOT_LIST}.="$SAMPLE_PATH/$conf->{DEVIP}-1-$demo_id.jpg";
 }
 $result{SOURCE_BASE}="$DEMO_DIR/$demo_id-$result{MODELID}";
 $result{SOURCE}="$DEMO_DIR/$src";
}
#------------------------------------------------------------------------------
# interim report & exit 
#------------------------------------------------------------------------------
ProbeResult(\%result)  if $conf->{PROBE} =~ /^(DEFINE|FAST)$/;
ProbeResult(\%result)  if $conf->{DEVID}==0;

#------------------------------------------------------------------------------
# set RC (rate control) parameters + qality + imagesize
#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# set AudioParams
#------------------------------------------------------------------------------
# final report-----------------------------------------------------------------------
ProbeResult(\%result);

