Ankündigung

Einklappen
Keine Ankündigung bisher.

LBS Programmier Hilfe für Pioneer TV

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

    LBS Programmier Hilfe für Pioneer TV

    Hab mich mal wieder mit meinen nicht vorhandenen Kenntnissen an einem LBS versucht. Prinzipiell scheint es zu funktionieren, zumindest besser als beim letzten Versuch. Bloß bei 2 Punkten komm ich leider iwie nicht weiter. Vielleicht hat ja jemand mit mehr Ahnung wie ich den ein oder anderen Tip.


    Zum LBS. Hab mir diesen zusammen kopiert von jonofe's Baustein für den Denon AVR 19000199 sowie von lennartc's Baustein für den Pioneer AVR 19000630 der ja ebenfalls auf dem Denon Baustein von jonofe basiert.

    Jetzt zu den Problemen:

    1. Nach meinem Verständnis sollte der LBS so funktionieren dass der Status der Ausgänge in der angegeben Zeit an e#5 immer wieder abgefragt wird.
    Das tut er aber leider nicht und ich weiß auch nicht warum. Der Status wird nur einmalig beim Starten von Edomi bzw. beim Starten des LBS abgefragt.
    Bei der ersten/einmaligen Abfrage funktioniert es auch und der Status der Ausgänge wird im LBS angezeigt.
    Beim eingeben von Befehlen werden die Ausgänge auch aktualisiert.

    2. Die Ausgänge werden zwar gesetzt, aber sollten nach meinem Verständnis eigentlich auf die zugewiesenen Namen umgebogen werden.
    Passieren sollte das in der "function read".
    Am Beispiel vom Ausgang Source, der Fernseher liefert zurück S01 bis S06 und genau das wird im LBS in den Ausgängen auch angezeigt, was ja soweit schonmal ganz gut ist. Aber ich hätte gerne das z.B. bei S01 = HDMI1 angezeigt wird, so wie im LBS angegeben.
    Dieser Punkt stört mich eigentlich weniger, aber ich würde es trotzdem gerne verstehen was ich falsch gemacht habe.


    Hier mal das Log zum Baustein:
    Code:
    2017-10-29 11:43:45    028423    9952    debug    LBS19000199 [v0.1.0]:    Pioneer TV LBS started
    2017-10-29 11:43:45    036570    9952    debug    LBS19000199 [v0.1.0]:    Daemon is not running
    2017-10-29 11:43:45    045418    9952    debug    LBS19000199 [v0.1.0]:    Daemon is not running
    2017-10-29 11:43:45    045775    9952    debug    LBS19000199 [v0.1.0]:    Starting daemon ...
    2017-10-29 11:43:45    061180    9952    debug    LBS19000199 [v0.1.0]:    Pioneer TV LBS ended
    2017-10-29 11:43:45    076714    9966    debug    EXE19000199 [v0.1.0]:    PowerOn Delay: 0
    2017-10-29 11:43:45    077116    9966    debug    EXE19000199 [v0.1.0]:    Pioneer TV Daemon started
    2017-10-29 11:43:45    077616    9966    debug    EXE19000199 [v0.1.0]:    Socket created
    2017-10-29 11:43:45    077832    9966    debug    EXE19000199 [v0.1.0]:    Socket binded
    2017-10-29 11:43:45    079651    9966    debug    EXE19000199 [v0.1.0]:    Socket connected
    2017-10-29 11:43:45    079890    9966    debug    EXE19000199 [v0.1.0]:    SENDING: INP
    2017-10-29 11:43:45    080419    9966    debug    EXE19000199 [v0.1.0]:    SENDING: AVS
    2017-10-29 11:43:45    080884    9966    debug    EXE19000199 [v0.1.0]:    SENDING: SZM
    2017-10-29 11:43:45    081171    9966    debug    EXE19000199 [v0.1.0]:    SENDING: VOL
    2017-10-29 11:43:45    081437    9966    debug    EXE19000199 [v0.1.0]:    SENDING: AMT
    2017-10-29 11:43:46    084213    9966    debug    EXE19000199 [v0.1.0]:    RECEIVED:
    2017-10-29 11:43:46    085205    9966    debug    EXE19000199 [v0.1.0]:    RECEIVED: INPS03
    2017-10-29 11:43:46    086926    9966    debug    EXE19000199 [v0.1.0]:    RECEIVED: AVSS01
    2017-10-29 11:43:46    088588    9966    debug    EXE19000199 [v0.1.0]:    RECEIVED: SZMS05
    2017-10-29 11:43:46    090276    9966    debug    EXE19000199 [v0.1.0]:    RECEIVED: VOL000
    2017-10-29 11:43:46    091245    9966    debug    EXE19000199 [v0.1.0]:    RECEIVED: AMTS00
    Hier der aktive Baustein:
    Unbenannt.JPG


    Und hier der Code des LBS:
    PHP-Code:
    ###[DEF]###
    [name        = Pioneer-TV    v0.1.0 ]

    [e#1        = Start/Stop #init=1]
    [e#2        = IP ]
    [e#3        = Port #init=4001 ]
    [e#4        = Command ]
    [e#5        = Polling intervall #init=250]
    [e#6        = Log level #init=8]
    [e#7        = Power On delay #init=0 ]

    [a#1        = Result ]
    [a#3        = Source ]
    [a#4        = AV-Selection ]
    [a#5        = Screen-Mode ]
    [a#6        = Volume ]
    [a#7        = Mute ]
    [a#8        = Power Supply ]

    [v#1        = 0 ]    // message queue id


    [v#100        = 0.1.0 ]                    // Version
    [v#101        = 19000199 ]            // LBS ID
    [v#102        = Pioneer-TV ]        // LBS name
    [v#103        = 0 ]                            // Log level
    [v#104        = 1 ]                            // One logfile per instance
    [v#105        = 0 ]                            // log ID in each line

    ###[/DEF]###


    ###[HELP]###
    This LBS controls and polls the Pioneer TV PDP-LX5090, PDP-LX6090, KRP-500, KRP-600 models connected via a Ethernet-to-RS-232C adapter
    (e.g. Moxa NPort 5110). It may work with other Pioneer TV as well.
    A command can be sent to input E4. In general all command are supported. Refer to the Pioneer documentation,
    which commands are valid for your TV. (http://www.awe-europe.com/documents/Control%20Docs/Pioneer/Archive/g9KuroRs232.pdf)
    The daemon part of the LBS regularly reads the changes indicated by the TV. This includes even changes, which are
    made by your normal IR remote and manual changes at the device itself. Therefore the outputs
    should be always consistent to the real status. With the polling intervall you can specify how fast the LBS
    recognizes changes, which happen to the device.    

    E1: Start/Stop of LBS (1= Start | 0=Stop) Status of LBS (1=active | 0=stopped) is written to A8
    E2: IP - IP address of your RS-232/Ethernet converter in TCP/IP Server mode (should work with ser2net as well)
    E3: Port - Port, which you have configured you TCP/IP Server on
    E4: Command - Command to be executed, according to the protocol specification.
        Spec. with all command can be found here: http://www.awe-europe.com/documents/Control%20Docs/Pioneer/Archive/g9KuroRs232.pdf
    E5: Polling intervall - Specifies how often the LBS will poll for changes.
    E6: Debug - Enables debug output to the tracelog (1=ON, 0=OFF)
    E7: Delays the start of the status polling daemon by x seconds to allow the device to be switched on via A8 (Power-Supply)
        If you want to use that feature you should use a value >=15 seconds, because experience shows, that the TV needs
        around 15 seconds to become available for a connection.  

    A1: Result of the Command (OK|ERROR)
    A2: Power - Current main power status (0=OFF|1=ON)
    A3: Source - Currently selected input source
    A4: AV-Selection - Shows the current AV-Selection
    A5: Screen-Mode - Shows the current Screen mode
    A6: Volume - Current main volume value (0-60)
    A7: Mute - Indicates whether mute is on or off (0=mute_off | 1=mute_on)
    A8: Power-Supply - Gives 0 after the Pioneer TV daemon is stopped. It gives 1 after the Pioneer TV LBS was startet.
         The daemon will delay the "real" start (connection to the device) by E7 seconds to allow the device to be ready after switching on.
         This output can be used to switch the main power supply of the device without getting errors in the log.

    V1: Containes the message queue id, which is used to transfer commands from LBS to EXEC part

    V100: Version
    V101: LBS Number
    V102: Log file name
    V103: Log level
    V104: One log file per LBS instance
    V105: log ID in each log entry

    Prerequistes needed:
    ====================
    You need to install php-process module before you can use this LBS, because it uses a message queue.
    To install php-process you have to login as root to your edomi server and execute

        yum install php-process


    Changelog
    =========
    v0.1.0:    Initial version

    ###[/HELP]###


    ###[LBS]###
    <?

    function LB_LBSID_logging($id, $msg, $var = NULL, $priority = 8)
    {
        $E = getLogicEingangDataAll($id);
        $logLevel = getLogicElementVar($id, 103);
        if (is_int($priority) && $priority <= $logLevel && $priority > 0) {
            $logLevelNames = array(
                'none',
                'emerg',
                'alert',
                'crit',
                'err',
                'warning',
                'notice',
                'info',
                'debug'
            );
            $version = getLogicElementVar($id, 100);
            $lbsNo = getLogicElementVar($id, 101);
            $logName = getLogicElementVar($id, 102) . "-LBS$lbsNo";
            $logName = preg_replace('/ /', '_', $logName);
            if (logic_getVar($id, 104))
                $logName .= "-$id";
            if (logic_getVar($id, 105) == 1)
                $msg .= " ($id)";
            strpos($_SERVER['SCRIPT_NAME'], $lbsNo) ? $scriptname = 'EXE' . $lbsNo : $scriptname = 'LBS' . $lbsNo;
            writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t" . $msg);
            if (isset($var)) {
                writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t================ ARRAY/OBJECT START ================");
                writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t" . json_encode($var));
                writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t================ ARRAY/OBJECT  END  ================");
            }
        }
    }

    function LB_LBSID_exec($id, $action = false)
    {
        $lbsNo = logic_getVar($id, 101);
        $isRunning = shell_exec('/bin/ps x | /bin/grep EXE"' . $lbsNo . '.php ' . $id . '" | /bin/grep -v "grep"');
        $pid = false;
        if ($isRunning) {
            $split = explode(' ', trim($isRunning));
            $pid = $split[0];
            LB_LBSID_logging($id, 'Daemon is running with PID: ' . $pid);
        } else
            LB_LBSID_logging($id, 'Daemon is not running ');
        switch ($action) {
            case 'stop':
                if ($isRunning && is_numeric($pid)) {
                    LB_LBSID_logging($id, 'Terminating daemon ...');
                    posix_kill($pid, SIGTERM);
                }
                break;
            case 'start':
                if (! $isRunning) {
                    LB_LBSID_logging($id, 'Starting daemon ...');
                    callLogicFunctionExec(LBSID, $id); // start daemon
                }
                break;
            case 'restart':
                if ($isRunning && is_numeric($pid)) {
                    LB_LBSID_logging($id, 'Restarting daemon ...');
                    posix_kill($pid, SIGTERM);
                }
                callLogicFunctionExec(LBSID, $id); // start daemon
                break;
            default:
                return $pid;
                break;
        }

        // check current status again and return PID or false
        $isRunning = shell_exec('/bin/ps x | /bin/grep EXE"' . $lbsNo . '.php ' . $id . '" | /bin/grep -v "grep"');
        if ($isRunning) {
            $split = explode(' ', trim($isRunning));
            $pid = $split[0];
            return $pid;
        } else
            return false;
    }

    function LB_LBSID($id)
    {
        if ($E = getLogicEingangDataAll($id)) {
            setLogicElementVar($id, 103, $E[6]['value']); // set loglevel to #VAR 103
            LB_LBSID_logging($id, 'Pioneer TV LBS started');

            if ($E[1]['refresh'] == 1) {
                if ($E[1]['value'] != 0) {
                    if (! LB_LBSID_exec($id, 'status')) { // dieses Konstrukt stellt sicher, dass das EXEC-Script nur einmal gestartet wird
                        logic_setOutput($id, 8, 1);
                        $msg_queue_id = hexdec(uniqid());
                        setLogicElementVar($id, 1, $msg_queue_id);
                        LB_LBSID_exec($id, 'start');
                    }
                } elseif ($E[1]['value'] == 0) {
                    // stop daemon
                    LB_LBSID_logging($id, "Stopping Pioneer TV daemon...");
                    LB_LBSID_exec($id, 'stop');
                    logic_setOutput($id, 8, 0);
                }
            } else
                if ($E[4]['refresh'] == 1) {
                    $cmd = $E[4]['value'];
                    LB_LBSID_logging($id, 'Command sent to daemon: ' . $cmd);
                    $msg_queue_id = getLogicElementVar($id, 1);
                    $msg_queue = msg_get_queue($msg_queue_id);
                    LB_LBSID_logging($id, "Create Message Queue with ID: " . dechex($msg_queue_id));
                    msg_send($msg_queue, 1, $cmd, true);
                }
            LB_LBSID_logging($id, 'Pioneer TV LBS ended');
        }
    }
    ?>
    ###[/LBS]###




    ###[EXEC]###
    <?
    require (dirname(__FILE__) . "/../../../../main/include/php/incl_lbsexec.php");
    set_time_limit(0);
    sql_connect();

    // sleep Power-On Delay (E7) seconds
    $E = logic_getInputs($id);
    $powerOnDelay = intval($E[7]['value']);
    logging($id, "PowerOn Delay: $powerOnDelay");
    if ($powerOnDelay > 0)
        sleep($powerOnDelay);

    // wait for E7-seconds to give time to Power-Supply-Output A13 to switch on the device

    //
    // error_off() : switch off error reporting
    // error_on() : switch on error reporting
    //
    function myErrorHandler($errno, $errstr, $errfile, $errline)
    {
        global $id;
        logging($id, "File: $errfile | Error: $errno | Line: $errline | $errstr ");
    }

    function error_off()
    {
        $error_handler = set_error_handler("myErrorHandler");
        error_reporting(0);
    }

    function error_on()
    {
        restore_error_handler();
        error_reporting(E_ALL);
    }

    function logging($id, $msg, $var = NULL, $priority = 8)
    {
        $E = getLogicEingangDataAll($id);
        $logLevel = getLogicElementVar($id, 103);
        if (is_int($priority) && $priority <= $logLevel && $priority > 0) {
            $logLevelNames = array(
                'none',
                'emerg',
                'alert',
                'crit',
                'err',
                'warning',
                'notice',
                'info',
                'debug'
            );
            $version = getLogicElementVar($id, 100);
            $lbsNo = getLogicElementVar($id, 101);
            $logName = getLogicElementVar($id, 102) . "-LBS$lbsNo";
            $logName = preg_replace('/ /', '_', $logName);
            if (logic_getVar($id, 104))
                $logName .= "-$id";
            if (logic_getVar($id, 105) == 1)
                $msg .= " ($id)";
            strpos($_SERVER['SCRIPT_NAME'], $lbsNo) ? $scriptname = 'EXE' . $lbsNo : $scriptname = 'LBS' . $lbsNo;
            writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t" . $msg);
            if (isset($var)) {
                writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t================ ARRAY/OBJECT START ================");
                writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t" . json_encode($var));
                writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t================ ARRAY/OBJECT  END  ================");
            }
        }
    }

    $E = getLogicEingangDataAll($id);
    if ($E) {
        logging($id, 'Pioneer TV Daemon started');
        $intervall = $E[5]['value'];
        if (! is_numeric($intervall) || $intervall < 100)
            $intervall = 100;
        $ip = $E[2]['value'];
        $port = $E[3]['value'];

        $msg_queue_id = getLogicElementVar($id, 1);
        $msg_queue = msg_get_queue($msg_queue_id);

        $pioneer = new pioneer($ip, $port);
        $status = $pioneer->get_status();

        while (getSysInfo(1)) { // Hauptschleife (wird beim Beenden oder Neustart von EDOMI verlassen)
            unset($command);
            $result = msg_receive($msg_queue, 1, $msgtype, 4096, $command, true, MSG_IPC_NOWAIT, $msg_error);
            if ($command) {
                logging($id, 'Command to be executed: ', $command);
                $pioneer->sendCmd($command);
            }
            $pioneer->read();
            if ($command == 'PON')
                sleep(1);
            else
                if (substr($command, 0, 3) == 'INP')
                    sleep(2);
                else
                    if (isset($command))
                        sleep(1);
                    else
                        usleep($intervall * 1000); // CPU-Last verteilen (die Länge der Pause sollte je nach Bedarf angepasst werden - je länger, desto ressourcenschonender)
        }
        logging($id, 'Pioneer TV Daemon stopped');
    }

    class pioneer
    {
        private $moxa_ip;
        private $moxa_port;
        private $moxa_connection;
        private $input;    
        private $AVselection;
        private $screenmode;
        private $volume;
        private $mute;


        function __construct($ip, $port)
        {
            $this->moxa_ip = $ip;
            $this->moxa_port = $port;
            $this->connect();
        }

        function __destruct()
        {
            socket_close($this->moxa_connection);
        }

        function connect()
        {
            global $id;        
            // Create socket connection to Pioneer TV
            do {
                $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if (! $sock) {
                    logging($id,"Cannot create socket!", NULL, 6);
                    sleep(3);
                } else
                    logging($id,"Socket created");
            } while (! $sock);

            // Bind the source address
            do {
                $bind = socket_bind($sock, global_serverIP);
                if (! $bind) {
                    logging($id,"Cannot bind to socket!", NULL, 6);
                    sleep(3);
                } else
                    logging($id,"Socket binded");
            } while (! $bind);

            // Connect to destination address
            do {
                @$connect = socket_connect($sock, $this->moxa_ip, $this->moxa_port);
                if (! $connect) {
                    logging($id,"Cannot connect to socket!", NULL, 6);
                    sleep(3);
                } else {
                    $this->moxa_connection = $sock;
                    logging($id,"Socket connected");
                }
            } while (! $connect);

            socket_set_nonblock($this->moxa_connection);
            socket_set_option($this->moxa_connection, SOL_SOCKET, SO_KEEPALIVE, 1);
        }

        function sendCmd($cmd)
        {
            global $id, $debug;
            if ($this->moxa_connection) {
                logging($id, 'SENDING: ' . $cmd);
                if (socket_write($this->moxa_connection, "\x02**".$cmd."\x03"))
                    setLogicLinkAusgang($id, 1, 'OK');
                else
                    setLogicLinkAusgang($id, 1, 'ERROR');
            }
        }

        function read()
        {
            global $id, $debug;
            if ($this->moxa_connection) {
                $e = socket_get_option($this->moxa_connection, SOL_SOCKET, SO_ERROR);
                if ($e != 0)
                    $this->connect();
                $buf = socket_read($this->moxa_connection, 2048);
                if ($buf != '') {
                    $responses = explode("\x02",$buf);
                    foreach ($responses as $response) {
                        logging($id, 'RECEIVED: ' . $response);
                        $cmd = substr($response, 0, 3);
                        switch ($cmd) {
                            case 'INP':
                                $input = substr($response, 3);
                                switch ($input) {
                                    case 'S01':
                                        $input = 'HDMI1';
                                        break;
                                    case 'S02':
                                        $input = 'COMPONENTE';
                                        break;
                                    case 'S03':
                                        $input = 'HDMI2';
                                        break;
                                    case 'S04':
                                        $input = 'HDMI3';
                                        break;
                                    case 'S05':
                                        $input = 'COMPOSITE';
                                        break;
                                    case 'S06':
                                        $input = 'PC';
                                        break;
                                }
                                setLogicLinkAusgang($id,3,$input);
                                break;
                            case 'AVS':
                                $AVselection = substr($response, 3);
                                switch ($AVselection) {
                                    case 'S01':
                                        $AVselection = 'STANDARD';
                                        break;
                                    case 'S02':
                                        $AVselection = 'DYNAMIC';
                                        break;
                                    case 'S03':
                                        $AVselection = 'MOVIE';
                                        break;
                                    case 'S04':
                                        $AVselection = 'GAME';
                                        break;
                                    case 'S05':
                                        $AVselection = 'SPORT';
                                        break;
                                    case 'S06':
                                        $AVselection = 'PURE';
                                        break;
                                    case 'S07':
                                        $AVselection = 'USER';
                                        break;
                                    case 'S08':
                                        $AVselection = 'ISFDAY';
                                        break;
                                    case 'S09':
                                        $AVselection = 'ISFNIGHT';
                                        break;
                                    case 'S10':
                                        $AVselection = 'OPTIMUM';
                                        break;
                                    case 'S11':
                                        $AVselection = 'ISFAUTO';
                                        break;                                   
                                }
                                setLogicLinkAusgang($id,4,$AVselection);
                                break;
                            case 'SZM':
                                $screenmode = substr($response, 3);
                                switch ($screenmode) {
                                    case 'S00':
                                        $screenmode = 'DOTbyDOT';
                                        break;                                
                                    case 'S01':
                                        $screenmode = '4:3';
                                        break;
                                    case 'S02':
                                        $screenmode = 'FULL';
                                        break;
                                    case 'S03':
                                        $screenmode = 'ZOOM';
                                        break;
                                    case 'S04':
                                        $screenmode = 'CINEMA';
                                        break;
                                    case 'S05':
                                        $screenmode = 'WIDE';
                                        break;
                                    case 'S06':
                                        $screenmode = 'FULL14:9';
                                        break;
                                    case 'S07':
                                        $screenmode = 'CINEMA14:9';
                                        break;
                                    case 'S11':
                                        $screenmode = 'AUTO';
                                        break;         
                                    case 'S12':
                                        $screenmode = 'WIDE2';
                                        break;                                                                 
                                }
                                setLogicLinkAusgang($id,5,$screenmode);
                                break;                                                            
                            case 'VOL':
                                $vol = substr($response,3,3);
                                if (is_numeric($vol)) setLogicLinkAusgang($id,6,(int)$vol);
                                break;                                                                 
                            case 'AMT':
                                $mute = substr($response, 3);
                                switch ($mute) {
                                    case 'S00':
                                        $mute = 'OFF';
                                        break;                                
                                    case 'S01':
                                        $mute = 'ON';
                                        break;                                                               
                                }
                                setLogicLinkAusgang($id,7,$mute);
                                break;    
                        }
                    }
                }
            }
        }

        // requests all relevant data for the LBS outputs
        function get_status()
        {
            $this->sendCmd("INP");
            $this->sendCmd("AVS");
            $this->sendCmd("SZM");
            $this->sendCmd("VOL");
            $this->sendCmd("AMT");
        }
    }

    msg_remove_queue($msg_queue);

    sql_disconnect();
    ?>
    ###[/EXEC]###

    Und hier noch der Link zur Dokumentation zum Protokoll des Fernsehers:
    http://www.awe-europe.com/documents/...9KuroRs232.pdf
    Gruß Ben

    #2
    Der 2te Punkt hat sich glaub ich erledigt. Hinter der Antwort des TV's kommt laut Beschreibung noch ein \x03.
    Was man halt im Log nicht sieht. Hab jetzt einfach die Zeichen die er Auswerten soll so begrenzt.

    Vorher:
    PHP-Code:
                if ($buf != '') {
                    
    $responses explode("\x02",$buf);
                    foreach (
    $responses as $response) {
                        
    logging($id'RECEIVED: ' $response);
                        
    $cmd substr($response03);
                        switch (
    $cmd) {
                            case 
    'INP':
                                
    $input substr($response3);
                                switch (
    $input) {
                                    case 
    'S01':
                                        
    $input 'HDMI1';
                                        break; 


    Nacher:
    PHP-Code:
                                $input substr($response33); 



    Oder gibts da einen eleganteren/besseren Weg?
    Gruß Ben

    Kommentar

    Lädt...
    X