Files
alamosConnectPHP/alamosConnect.php

400 lines
13 KiB
PHP

<?php
/*
Nenninger N-Tools.de
Version 1.4 (241022)
Version 1.5 260725 PHP 8.5 -> curl_close is deprecated
Version 1.6 260817 ?shiftBook=false
Version 1.7 260818 getAllAlarms($_limit=99999)
*/
class alamosConnect{
private $VERSION = '1.7';
private $source = 'UNKNOWN';
private $alamosServer = '';
private $user = "";
private $password = "";
private $cURL;
private $lasterror;
private $JWTToken = '';
private $location = [];
public $responseCode;
//private $debug = true;
function __construct($user,$password,$server = '') {
if($server != ''){
$this->alamosServer = $server;
}
$this->user = $user;
$this->password = $password;
$this->cURL = curl_init();
curl_setopt($this->cURL, CURLOPT_VERBOSE, 0);
curl_setopt($this->cURL, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $this->cURL, CURLOPT_COOKIESESSION, true );
curl_setopt( $this->cURL, CURLOPT_COOKIEFILE, NULL);
curl_setopt( $this->cURL, CURLINFO_HEADER_OUT, true);
}
function __destruct() {
$this->logout();
//curl_close($this->cURL);
}
function request($_url, $_data='', $_token='', $_request=''){
$header = [];
$header[] = "Accept: application/json";
$header[] = 'Content-Type:application/json';
if($_token != '')
$header[] = "Authorization: JWT ".$_token;
curl_setopt($this->cURL, CURLOPT_URL, $_url);
if($_data != ''){
curl_setopt($this->cURL, CURLOPT_POST, true);
curl_setopt($this->cURL, CURLOPT_POSTFIELDS,json_encode($_data));
}else{
curl_setopt($this->cURL, CURLOPT_POST, false);
}
if($_request != ''){
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST, $_request);
}else{
if($_data != ''){
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST,'POST');
}else{
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST,'GET');
}
}
curl_setopt($this->cURL, CURLOPT_HTTPHEADER, $header);
if(isset($this->debug))
echo "\n\nsenddata->:\n".print_r($_data,true);
$data = curl_exec($this->cURL);
if(!curl_errno($this->cURL)){
$this->responseCode = curl_getinfo($this->cURL, CURLINFO_RESPONSE_CODE);
if(isset($this->debug)){
echo "\n\nSendheader->:\n".print_r(curl_getinfo($this->cURL, CURLINFO_HEADER_OUT),true);
echo "\n\nretdata->:\n".print_r($data,true);
echo "\n\nrCURLStatus->:\n".print_r(curl_error($this->cURL),true);
echo "\n\nrCURLRETURNStatus->:\n".print_r(curl_getinfo($this->cURL, CURLINFO_RESPONSE_CODE),true);
}
$dataDecoded = json_decode($data);
if(!isset($dataDecoded->status)){
$this->lasterror = '';
return $dataDecoded;
}else{
$this->lasterror = $data;
return false;
}
} else {
$this->lasterror = 'Curl error: ' . curl_error($this->cURL);
return false;
}
}
/*
Öffentliche Funktionen
*/
/*
Ausgabe Fehlermeldung
*/
public function get_last_error(){
return $this->lasterror;
}
/*
Server setzen
*/
public function setServer($_servver){
$this->alamosServer = $_servver;
return $this->alamosServer;
}
/*
Login
*/
public function login(){
$data = [
"username" => $this->user,
"password" => $this->password,
"source" => $this->source
];
$response = $this->request($this->alamosServer."/rest/login",$data);
if($response !== false){
if(isset($response->token)){
$this->JWTToken = $response->token;
$this->password = 'not longer needed';
$this->location['lat'] = $response->account->settings->lat;
$this->location['lng'] = $response->account->settings->lng;
$this->location['street'] = $response->account->settings->street;
$this->location['house'] = $response->account->settings->house;
$this->location['city'] = $response->account->settings->city;
$this->location['postalCode'] = $response->account->settings->postalCode;
return true;
}
}
return false;
}
/*
Abmelden
*/
public function logout(){
$response = $this->request($this->alamosServer."/rest/sso/isOn");
if($response !== false){
$this->JWTToken = '';
return $response;
}
return false;
}
/*
Fahrzeugstatus
*/
public function getVehicles(){
$response = $this->request($this->alamosServer."/rest/vehicles",'',$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
/*
Personen
*/
public function getAddressbook(){
$response = $this->request($this->alamosServer."/rest/addressbook",'',$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
/*
Alamierungsübersicht
*/
public function getAddressbookAlarmOverview(){
$response = $this->request($this->alamosServer."/rest/addressbook/overview",'',$this->JWTToken);
$ret = [];
if($response !== false){
$ret = $response->persons;
$pageCount = intval($response->totalPages);
if($pageCount > 1){
$c = $pageCount -1;
for($i=1;$i<=$c;$i++){
$response = $this->request($this->alamosServer."/rest/addressbook/overview?page=".$i,'',$this->JWTToken);
foreach($response->persons as $person)
array_push($ret,$person);
}
}
return $ret;
}
return false;
}
/*
Einheiten zum Alamieren
*/
public function getUnits(){
$response = $this->request($this->alamosServer."/rest/units/search",'',$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
/*
Alarme (max 100)
*/
public function getAlarms(){
$response = $this->request($this->alamosServer."/rest/alarm?shiftBook=false",'',$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
/*
Alarme ohne Begrenzung (99999)
aber andere Strucktur
*/
public function getAllAlarms($_limit=99999){
$response = $this->request($this->alamosServer."/rest/alarm/paginated/simple/?page=0&limit=".$_limit."&filter=&ordering=DESC&startDate=0&shiftBook=false",'',$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
/*
Feedbacks zu den Alarmen
*/
public function getFeedbackToAlarm($_alarmID){
$response = $this->request($this->alamosServer."/rest/alarm/feedback/".$_alarmID,'',$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
/*
Details zu den Alarmen
*/
public function getDetailsToAlarm($_alarmID){
$response = $this->request($this->alamosServer."/rest/alarm/".$_alarmID."?shiftBook=false",'',$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
/*
Alarm senden
$this->location['lat'] = $response->account->settings->lat;
$this->location['lng'] = $response->account->settings->lng;
$this->location['street'] = $response->account->settings->street;
$this->location['house'] = $response->account->settings->house;
$this->location['city'] = $response->account->settings->city;
$this->location['postalCode'] = $response->account->settings->postalCode;
{"units":"28ebdcc09be94c23bde05920b4388bb0;8941914f03a7415db960a01717f0bfed","data":{"message":"Test mit mehreren","house":"99","postalCode":"555","street":"strasse","city":"allzenau","quickEditActive":false,"quickEditIDs":"","keyword":"âï¸ THL Person in Höhe","keyword_category":"âï¸","keyword_color":"#FF8926","keyword_misc":"Absturz \/ Höhe","keyword_ident":"T2013","keyword_ser":"SER17","keyword_description":"P retten Höhen und Tiefen","source":"Mobiler Alarm","trigger":"Admin"}}
{"data":{"message":"Text","keyword":"Stichwort","keyword_description":"Beschreibung","withStatistic":false,"generateExternalId":false,"alarmType":"MANUAL"},"units":"a616a56d254a40aa90f4adbff4a1f494","vehicles":[],"externalId":""}
*/
public function sendAlarm($_units,$_keyword,$_description,$_text,$_location=[],$_withStatistic=true){
$data = [];
if(count($_units) == 0)
exit;
$data['units'] = implode(';',$_units);
$data['data'] = [];
$data['data']['keyword'] = $_keyword;
$data['data']['keyword_description'] = $_description;
$data['data']['message'] = $_text;
$data['data']['withStatistic'] = $_withStatistic;
if(count($_location) == 0){
$data['data'] = array_merge($data['data'],$this->location);
}
//print_r($data);
$response = $this->request($this->alamosServer."/rest/alarm",$data,$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
/* Alarm beenden */
public function closeAlarm($_id=''){
if($_id == '')
exit;
$date = new DateTimeImmutable();
$milli = (int) $date->format('Uv');
$data = [];
$data['state'] = 'CLOSED';
$data['timestamp'] = $milli;
$data['vehicleIds'] = [];
//print_r($data);
$response = $this->request($this->alamosServer."/rest/alarm/".$_id,$data,$this->JWTToken,"PUT");
if($response !== false){
return $response;
}
return false;
}
/* Alarm löschen */
public function deleteAlarm($_id=''){
if($_id == '')
exit;
echo $this->alamosServer."/rest/alarm/".$_id;
$response = $this->request($this->alamosServer."/rest/alarm/".$_id,'',$this->JWTToken,"DELETE");
if($response !== false){
return $response;
}
return false;
}
/* Termin anlegen */
public function createEvent($_calendarID,$_event){
//EventTemplate holen
$data = $this->request($this->alamosServer."/rest/eventPlanning/calendars/".$_calendarID."/events/new?type=EVENT&date=2024-09-30",'',$this->JWTToken,"GET");
/*
{"id":null,"calendarId":"13c3cff9-0c98-4321-810b-b617c6b2b50c","calendarName":"OALZ","title":"Test","body":null,"color":"#030bfc","serialAppointmentId":null,"serialAppointmentIteration":0,"serialAppointmentDistance":0,"serialAppointmentWeekday":"NONE","startDate":"2024-09-24T12:00:00+02:00","endDate":"2024-09-24T13:00:00+02:00","latestPossibleFeedback":"","fullDay":false,"hasNotifications":false,"type":"EVENT","status":"COMPLETED","cancelledReason":null,"location":null,"responsiblePerson":null,"topic":null,"minParticipants":0,"maxParticipants":0,"firstNotification":{"active":false,"duration":1,"asPush":true,"asEmail":false,"timeUnit":"HOURS","dueDate":null},"secondNotification":{"active":false,"duration":1,"asPush":true,"asEmail":false,"timeUnit":"DAYS","dueDate":null},"withFeedback":false,"fromIcalSync":false},"persons":[]}
*/
$data->event->title = $_event[0];
$data->event->body = $_event[1];
$data->event->startDate = $_event[2];
$data->event->endDate = $_event[3];
//print_r($data);
$response = $this->request($this->alamosServer."/rest/eventPlanning/calendars/".$_calendarID."/events?notify=false",$data,$this->JWTToken,"POST");
if($response !== false){
return $response;
}
return false;
}
/* Termin Liste pro Monat */
public function getEventsUser($_yearMonth){
//EventTemplate holen
$response = $this->request($this->alamosServer."/rest/eventPlanning/events/user/".$_yearMonth."?surrounding=true",'',$this->JWTToken,"GET");
if($response !== false){
return $response;
}
return false;
}
/* Termin ändern */
public function changeEvent($_calendarID,$_eventID,$_event){
//EventTemplate holen
$data = $this->request($this->alamosServer."/rest/eventPlanning/calendars/".$_calendarID."/events/".$_eventID,'',$this->JWTToken,"GET");
/*
{"id":null,"calendarId":"13c3cff9-0c98-4321-810b-b617c6b2b50c","calendarName":"OALZ","title":"Test","body":null,"color":"#030bfc","serialAppointmentId":null,"serialAppointmentIteration":0,"serialAppointmentDistance":0,"serialAppointmentWeekday":"NONE","startDate":"2024-09-24T12:00:00+02:00","endDate":"2024-09-24T13:00:00+02:00","latestPossibleFeedback":"","fullDay":false,"hasNotifications":false,"type":"EVENT","status":"COMPLETED","cancelledReason":null,"location":null,"responsiblePerson":null,"topic":null,"minParticipants":0,"maxParticipants":0,"firstNotification":{"active":false,"duration":1,"asPush":true,"asEmail":false,"timeUnit":"HOURS","dueDate":null},"secondNotification":{"active":false,"duration":1,"asPush":true,"asEmail":false,"timeUnit":"DAYS","dueDate":null},"withFeedback":false,"fromIcalSync":false},"persons":[]}
*/
if($this->responseCode == 200){
$data->event->title = $_event[0];
$data->event->body = $_event[1];
$data->event->startDate = $_event[2];
$data->event->endDate = $_event[3];
//print_r($data);
$response = $this->request($this->alamosServer."/rest/eventPlanning/calendars/".$_calendarID."/events/".$_eventID,$data,$this->JWTToken,"PUT");
if($response !== false){
return $response;
}
return false;
}else{
return false;
}
}
/* Termin löschen */
public function deleteEvent($_calendarID,$_eventID){
$response = $this->request($this->alamosServer."/rest/eventPlanning/calendars/".$_calendarID."/events/".$_eventID."?deleteSeries=false",'',$this->JWTToken,"DELETE");
if($response !== false){
return $response;
}
return false;
}
/* Nachricht senden */
public function sendMessage($_groups,$_title,$_message){
/*{"title":"Status","message":"Text","color":"#337AB7","type":"INFO","feedback":false,"groupIds":["group_ad7edb295654483cb66e1897aefd3577"],"schedule":{"infoMessageScheduleType":"NOW"}}*/
$data = [];
if(count($_groups) == 0)
exit;
$data['groupIds'] = $_groups;
$data['title'] = $_title;
$data['message'] = $_message;
$data['color'] = '#fff300';
$data['type'] = 'INFO';
$data['feedback'] = false;
$data['schedule']['infoMessageScheduleType'] = 'NOW';
//echo json_encode($data);
//$response = false;
$response = $this->request($this->alamosServer."/rest/messages",$data,$this->JWTToken);
if($response !== false){
return $response;
}
return false;
}
}
?>