Functions.php - Bestand met functies voor PHP-scripts

Gestart door Joost, 14 maart 2021, 08:52:16

Vorige topic - Volgende topic

Joost


<?php 

/* 
----------------------------------------------
---------------- VERSION 1.15 ----------------
-------------- DATE: 10-07-2021---------------
----------------------------------------------
*/

include("/home/pi/domoticz/scriptsjoost/php/settings.php");

// *** FUNCTIONS TO RETRIEVE DATA *** //
// *** FUNCTIONS TO RETRIEVE DATA *** //

//Retrieves status On/Off switch
function Status($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$stat $parsed_json['Status'];
# echo "Status ".$stat."\n";
return $stat;
}

//Retrieves level status
function Level($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$level trim($parsed_json['Level']);
# echo "Status ".$level."\n";
return $level;
}

//Retrieves temperature
function Temperature($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$temp trim($parsed_json['Temp']);
# echo "Status ".$temp."\n";
return $temp;
}

//Retrieves pressure
function Pressure($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$pressure trim($parsed_json['Pressure']);
return $pressure;
}

//Retrieves percentage
function Percentage($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$percentage trim($parsed_json['Data']);
$percentage substr($percentage0, -1);
return $percentage;
}

//Retrieves setpoint
function Setpoint($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$setpoint trim($parsed_json['SetPoint']);
return $setpoint;
}

//Retrieves value from text device
function Text($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$text trim($parsed_json['Data']);
return $text;
}

//Retrieves value from user variable
function Variable($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=command&param=getuservariable&idx='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$value $parsed_json['Value'];
return $value;
}

//Retrieves RainRate
function RainRate($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$rainrate trim($parsed_json['RainRate']);
return $rainrate;
}

//Retrieves Radiation
function Radiation($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$radiation trim($parsed_json['Radiation']);
return $radiation;
}

//Retrieves WindSpeed
function WindSpeed($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$windspeed trim($parsed_json['Speed']);
return $windspeed;
}

//Retrieves WindGust
function WindGust($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$windgust trim($parsed_json['Gust']);
return $windgust;
}

// *** FUNCTIONS TO UPDATE DEVICES *** //
// *** FUNCTIONS TO UPDATE DEVICES *** //

//Switches idx On or Off
function SwitchDevice($idx,$cmd) {
  global 
$domoticz_url;
  
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=switchlight&idx='.$idx.'&switchcmd='.$cmd),true);
  if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
  return 
$reply;
}

//Activates scene
function SwitchScene($idx) {
  global 
$domoticz_url;
  
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=switchscene&idx='.$idx.'&switchcmd=On'),true);
  if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
  return 
$reply;
}

//Sets a dimlevel to a dimmer
function SetLevel($idx,$level) {
  global 
$domoticz_url;
  
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=switchlight&idx='.$idx.'&switchcmd=Set%20Level&level='.$level),true);
  if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
  return 
$reply;
}

// Updates the nvalue and svalue of a device. Setpoint are set this way.
function Updatedevice($idx$nvalue$svalue) {
  global 
$domoticz_url;
  
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue),true);
  if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
  return 
$reply;
}

// Writes a text to the domoticz logfile
function Domlog($msg) {
  global 
$domoticz_url;
  
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=addlogmessage&message='.$msg),true);
  if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
  return 
$reply;
}

// Switch light
function LightOn($idx,$brightness) {
  global 
$domoticz_url;
  
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=setcolbrightnessvalue&idx='.$idx.'&color={"m":2,"t":154,"r":0,"g":0,"b":0,"cw":101,"ww":154}&brightness='.$brightness),true);
  if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
  return 
$reply;
}

// Switch light
function LightOnAlarm($idx,$brightness) {
  global 
$domoticz_url;
  
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=setcolbrightnessvalue&idx='.$idx.'&color={"m":3,"t":0,"r":255,"g":122,"b":76,"cw":0,"ww":0}&brightness='.$brightness),true);
  if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
  return 
$reply;
}

function 
Past($idx) {
global $domoticz_url;
$json_string file_get_contents($domoticz_url.'json.htm?type=devices&rid='.$idx);
$parsed_json json_decode($json_stringtrue);
$parsed_json $parsed_json['result'][0];
$time $parsed_json['LastUpdate'];
# echo "Status ".$temp."\n";
return $time;
}

function 
UpdateVariable($UserVariableName,$UserVariableType,$UserVariableValue) {
  global 
$domoticz_url;
  
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=updateuservariable&vname='.$UserVariableName.'&vtype='.$UserVariableType.'&vvalue='.$UserVariableValue),true);
  if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
  return 
$reply;
}

// Conbee devices bijwerken
function callAPI($method$url$data){
   
$curl curl_init();
   switch (
$method){
      case 
"POST":
         
curl_setopt($curlCURLOPT_POST1);
         if (
$data)
            
curl_setopt($curlCURLOPT_POSTFIELDS$data);
         break;
      case 
"PUT":
         
curl_setopt($curlCURLOPT_CUSTOMREQUEST"PUT");
         if (
$data)
            
curl_setopt($curlCURLOPT_POSTFIELDS$data);  
         break;
      default:
         if (
$data)
            
$url sprintf("%s?%s"$urlhttp_build_query($data));
   }
   
// OPTIONS:
   
curl_setopt($curlCURLOPT_URL$url);
   
curl_setopt($curlCURLOPT_HTTPHEADER, array(
 
//     'APIKEY: 111111111111111111111',
      
'Content-Type: application/json',
   ));
   
curl_setopt($curlCURLOPT_RETURNTRANSFER1);
   
curl_setopt($curlCURLOPT_HTTPAUTHCURLAUTH_BASIC);
   
// EXECUTE:
   
$result curl_exec($curl);
   if(!
$result){die("Connection Failure");}
   
curl_close($curl);
   return 
$result;
}


// *** FUNCTIONS TO SEND NOTIFICATIONS AND WRITE TO LOG *** //
// *** FUNCTIONS TO SEND NOTIFICATIONS AND WRITE TO LOG *** //

// Send notification to via Settings in Domoticz
function Notification($subject,$thebody) {
   global 
$domoticz_url;
    
$reply=json_decode(file_get_contents($domoticz_url.'json.htm?type=command&param=sendnotification&subject='.$subject.'&body='.$thebody),true);
   if(
$reply['status']=='OK'$reply='OK';else $reply='ERROR';
   return 
$reply;
}

function 
Telegram($msg,$silent,$to){
global $telegrambot1;
global $telegrambot2;
global $telegrambot3;
global $telegrambot4;
global $telegrambot5;
global $telegrambot6;
global $telegrambot7;
global $telegrambot8;
global $telegrambot9;
global $chatID1;
global $chatID2;
if ($to==1){ //Domoticz bot
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot1.'/sendMessage?chat_id='.$chatID1.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
if ($to==2){ //Zonnepanelen bot
foreach($chatID2 as $chatIDs) {
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot2.'/sendMessage?chat_id='.$chatIDs.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
}
if ($to==3){ //Crimesite
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot3.'/sendMessage?chat_id='.$chatID1.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
if ($to==4){ //Flitsmeldingen
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot4.'/sendMessage?chat_id='.$chatID1.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
if ($to==5){ //Flitsmeldingen
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot5.'/sendMessage?chat_id='.$chatID1.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
if ($to==6){ //Flitsmeldingen
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot6.'/sendMessage?chat_id='.$chatID1.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
if ($to==7){ //Flitsmeldingen
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot7.'/sendMessage?chat_id='.$chatID1.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
if ($to==8){ //Flitsmeldingen
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot8.'/sendMessage?chat_id='.$chatID1.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
if ($to==9){ //Flitsmeldingen
$result=json_decode(file_get_contents('https://api.telegram.org/bot'.$telegrambot9.'/sendMessage?chat_id='.$chatID1.'&text='.urlencode($msg).'&disable_notification='.$silent));
}
}

$LogFile '/tmp/log/domoticz.log';
function 
Logwrite($msg3,$msg2 NULL) {
global $LogFile;
$time    microtime(true);
$dFormat "Y-m-d H:i:s";
$mSecs   =  $time floor($time);
$mSecs   =  substr(number_format($mSecs,3),1);
$fp fopen($LogFile,"a+");
fwrite($fpsprintf("%s%s %s %s\r\n"date($dFormat), $mSecs$msg3$msg2));
fclose($fp);
}

// *** FUNCTIONS MISCELLANEOUS *** //
// *** FUNCTIONS MISCELLANEOUS *** //

function distance($lat1$lon1$lat2$lon2) { 
$pi80 M_PI 180
$lat1 *= $pi80
$lon1 *= $pi80
$lat2 *= $pi80
$lon2 *= $pi80
$r 6372.797// mean radius of Earth in km 
$dlat $lat2 $lat1
$dlon $lon2 $lon1
$a sin($dlat 2) * sin($dlat 2) + cos($lat1) * cos($lat2) * sin($dlon 2) * sin($dlon 2); 
$c atan2(sqrt($a), sqrt($a)); 
$km $r $c
return $km
}

?>