Suite à mon message de présentation : XML/JSON pour timers
Voilà les fonctions PHP que j’utilise pour contrôler l’IPX800 :
Je viens d’écrire ça rapidement, je n’ai donc pas encore tout testé (sous-entendu il y a de nombreuses erreurs).
Les fonctions sont assez intuitives à utiliser mais j’écrirai une petite documentation dans les prochaines jours.
Ce qu’il faut savoir :
-Les relais sont toujours numérotés de 1 à 32 (jamais de 0 à 31)
-Dans les fonctions LED_IPX800, PRESET_IPX800, INPUT_IPX800, ANALOG_IPX800 ; $relay est un tableau (pour indiquer un ou plusieurs relais)
-Dans la fonction M2M_IPX800, $cmd est un tableau (afin de passer plusieurs commandes à la suite)
-Dans chaque fonction on indique le numéro de la carte après les avoir définies ainsi :
// Carte 1
$card_ip[1] = '192.168.26.4';
$card_port[1] = '9870';
// Carte 2
$card_ip[2] = '192.168.26.5';
$card_port[2] = '9870';
Voilà mon fichier class_ipix800.php :
[code]<?php
/**
- class_ipix800.php
- Version : 0.3 - 2013-06-22
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License 2
- as published by the Free Software Foundation.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
*/
function XML_IPX800($card)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
$url = ‹ http:// ›.$card_ip[$card].‹ /status.xml ›;
$xml = simplexml_load_file($url);
if(empty($error)) {
if(!@file_get_contents($url)) return false;
else return $xml;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
function ERASE_TIMER_IPX800($card, $timer)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
if(!($timer >= 0 && $timer <= 127)) $error = ‹ Timer id is out of range ›;
if(empty($error)) {
$url = ‹ http:// ›.$card_ip[$card].‹ /protect/timers/timer1.htm?erase= ›.$timer;
if(!@file_get_contents($url)) return false;
else return true;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
function PROG_TIMER_IPX800($card, $timer, $day, $time, $relay, $action)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
if(!($timer >= 0 && $timer <= 127)) $error = ‹ Timer id is out of range ›;
if(!($day >= 0 && $day <= 9)) $error = ‹ Day is out of range ›;
if(!(preg_match(‹ /([01]?[0-9]|2[0-3]):[0-5][0-9]/ ›, $time))) $error = ‹ Time is out of range ›;
if(!($relay >= 1 && $relay <= 32)) $error = ‹ Relay is out of range ›;
if(!(($action >= 1 && $action <= 4) || $action == 7)) $error = ‹ Action is out of range ›;
if(empty($error)) {
$relay = $relay -1;
$url = ‹ http:// ›.$card_ip[$card].‹ /protect/timers/timer1.htm?timer= ›.$timer.‹ &day= ›.$day.‹ &time= ›.$time.‹ &relay= ›.$relay.‹ &action= ›.$action;
if(!@file_get_contents($url)) return false;
else return true;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
function LED_IPX800($card, $relay, $mode = ‹ output ›)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
for($i=0;$i<count($relay); $i++) {
if($relay[$i] >= 1 && $relay[$i] <= 32) {
if($mode == ‹ input ›) $relay[$i] = $relay[$i] + 100;
$relay[$i] = $relay[$i] - 1;
$url[$i] = ‹ http:// ›.$card_ip[$card].‹ /leds.cgi?led= ›.$relay[$i];
if(!@file_get_contents($url[$i])) $error = ‹ Cmd ‹ .$i. › failed ›;
sleep(1);
} else {
$error = ‹ Relay ‹ .$relay[$i]. › is out of range (cmd ‹ .$i. ›) ›;
}
}
if(empty($error)) return true;
else return (DEBUGMODE === true) ? $error : false;
}
function PRESET_IPX800($card, $relay, $state)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
for($i=0;$i<count($relay); $i++) {
if($relay[$i] >= 1 && $relay[$i] <= 32) {
if($state[$i] == 0 || $state[$i] == 1) {
$url[$i] = ‹ http:// ›.$card_ip[$card].‹ /preset.htm?set ›.$relay[$i].‹ = ›.$state[$i];
if(!@file_get_contents($url[$i])) $error = ‹ Cmd ‹ .$i. › failed ›;
sleep(1);
} else {
$error[$i] = ‹ State must be 0 or 1 ›;
}
} else {
$error = ‹ Relay ‹ .$relay[$i]. › is out of range (cmd ‹ .$i. ›) ›;
}
}
if(empty($error)) return true;
else return (DEBUGMODE === true) ? $error : false;
}
function COUNTER_IPX800($card, $counter, $name, $count)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
if(!($counter >= 1 && $timer <= 3)) $error = ‹ Counter id is out of range ›;
if(!($name)) $error = ‹ Name must be set ›;
if(!($count >= 0 && $count <= 999999999)) $error = ‹ Counter value is out of range ›;
if(empty($error)) {
$url = ‹ http:// ›.$card_ip[$card].‹ /protect/assignio/counter.htm?countername ›.$counter.‹ = ›.$name.‹ &counter ›.$counter.‹ = ›.$count;
if(!@file_get_contents($url)) return false;
else return true;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
function OUTPUT_IPX800($card, $relay, $name, $on, $off)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
if(!($relay >= 1 && $relay <= 32)) $error = ‹ Relay is out of range ›;
if(!($name)) $error = ‹ Name must be set ›;
if(!($on >= 0 && $on <= 65535)) $error = ‹ On delay value is out of range ›;
if(!($off >= 0 && $off <= 65535)) $error = ‹ On delay value is out of range ›;
if(empty($error)) {
$url = ‹ http:// ›.$card_ip[$card].‹ /protect/settings/output1.htm?output= ›.$relay.‹ &relayname= ›.$name.‹ &delayon= ›.$on.‹ &delayon= ›.$off;
if(!@file_get_contents($url)) return false;
else return true;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
function INPUT_IPX800($card, $input, $name, $mode, $inv, $relay)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
if(!($input >= 0 && $input <= 31)) $error = ‹ Input is out of range ›;
if(!($name)) $error = ‹ Name must be set ›;
if(!($mode >= 0 && $mode <= 4)) $error = ‹ Mode value is out of range ›;
if($inv) $input_cmd = ‹ &inv=1 ›;
for($i=0;$i<count($relay); $i++) {
if($relay[$i] >= 1 && $relay[$i] <= 32) {
$relay[$i] = $relay[$i] - 1;
$input_cmd .= ‹ l ›.$relay[$i].‹ =1 ›;
} else {
$error = ‹ Relay ‹ .$relay[$i]. › is out of range (cmd ‹ .$i. ›) ›;
}
}
if(empty($error)) {
$url = ‹ http:// ›.$card_ip[$card].‹ /protect/assignio/assign1.htm?input= ›.$input.‹ &inputname= ›.$name.‹ &mode= ›.$mode.$input_cmd;
if(!@file_get_contents($url)) return false;
else return true;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
function ANALOG_IPX800($card, $input, $name, $mode, $max, $maxm, $min, $minm, $relay)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
if(!($input >= 0 && $input <= 3)) $error = ‹ Input is out of range ›;
if(!($name)) $error = ‹ Name must be set ›;
if(!($mode >= 0 && $mode <= 5)) $error = ‹ Mode value is out of range ›;
if(!($max >= 0 && $max <= 1023)) $error = ‹ Max value is out of range ›;
if(!($maxm >= 0 && $maxm <= 2)) $error = ‹ Max mode value is out of range ›;
if(!($min >= 0 && $min <= 1023)) $error = ‹ Min value is out of range ›;
if(!($minm >= 0 && $minm <= 2)) $error = ‹ Min mode value is out of range ›;
for($i=0;$i<count($relay); $i++) {
if($relay[$i] >= 1 && $relay[$i] <=
{
$input_cmd .= ‹ lka ›.$relay[$i].‹ =1 ›;
} else {
$error = ‹ Relay ‹ .$relay[$i]. › is out of range (cmd ‹ .$i. ›) ›;
}
}
if(empty($error)) {
$url = ‹ http:// ›.$card_ip[$card].‹ /protect/assignio/analog1.htm?analog= ›.$input.‹ &name= ›.$name.‹ &selectinput= ›.$mode.‹ &hi= ›.$max.‹ &mhi= ›.$maxm.‹ &lo= ›.$min.‹ &mlo= ›.$minm.$input_cmd;
if(!@file_get_contents($url)) return false;
else return true;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
function PING_IPX800($card, $ip, $time, $retry, $relay)
{
global $card_ip;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
if(!(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))) $error = ‹ IP adress is out of range ›;
if(!($time >= 0 && $time <= 65535)) $error = ‹ Time value is out of range ›;
if(!($retry >= 0 && $retry <= 255)) $error = ‹ Retry value is out of range ›;
if(!($relay >= 1 && $relay <= 32)) $error = ‹ Relay is out of range ›;
if(empty($error)) {
$relay = $relay - 1;
$url = ‹ http:// ›.$card_ip[$card].‹ /protect/settings/ping.htm?pingip= ›.$ip.‹ &pingtime= ›.$time.‹ &pingretry= ›.$retry.‹ &prelay= ›.$relay;
if(!@file_get_contents($url)) return false;
else return true;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
function M2M_IPX800($card, $cmd)
{
global $card_ip, $card_port;
if(empty($card_ip[$card])) $error = ‹ Card is unknow ›;
$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
$error[] = "Unable to create socket: [$errorcode] $errormsg";
}
$connect = @socket_connect($socket, $card_ip[$card], $card_port[$card]);
if ($connect === false) {
$errorcode = socket_last_error($socket);
$errormsg = socket_strerror($errorcode);
$error[] = "Unable to connect: [$errorcode] $errormsg";
}
for($i=0;$i<count($cmd); $i++) {
socket_write($socket, $cmd[$i], strlen($cmd[$i]));
$reponse = socket_read($socket, 64);
}
socket_close($socket);
if(empty($error)) {
return $reponse;
} else {
return (DEBUGMODE === true) ? $error : false;
}
}
?>[/code]