551 lines
16 KiB
PHP
551 lines
16 KiB
PHP
<?php
|
|
|
|
class Rimoapi {
|
|
|
|
public static function changeBuildingAddress($apikey, $building_external_id, $_data) {
|
|
if(!$apikey) return false;
|
|
if(!$building_external_id) return false;
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$update_data = [];
|
|
foreach($_data as $key => $value) {
|
|
if(!in_array($key, ["address", "city", "zipCode", "district", "country", "lotNumber", "district2"])) continue;
|
|
if($value) {
|
|
$update_data[$key] = $value;
|
|
}
|
|
}
|
|
|
|
if(!count($update_data)) return false;
|
|
|
|
// send request to Rimo Api
|
|
$params = [];
|
|
$params['apiKey'] = $apikey;
|
|
$params['buildingId'] = $building_external_id;
|
|
$params = array_merge($params, $update_data);
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'PUT',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
//echo $qs."\n";
|
|
|
|
$createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_CHANGE_BUILDING_ADDRESS;
|
|
$put_url = $createOrderEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Address Change in Rimo: $put_url");
|
|
$response = file_get_contents($put_url, false, $ctx);
|
|
//var_dump($response);exit;
|
|
$log->debug(__METHOD__.": response: ".print_r($response,true));
|
|
if($response === false) {
|
|
$log->error("Fehler beim Update der Adresse in RIMO ".$building_external_id);
|
|
return false;
|
|
}
|
|
|
|
|
|
$resp_data = json_decode($response);
|
|
if(!$resp_data->id) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function getFtuData($oaid, $home_external_id) {
|
|
//$oaid = $oaid;
|
|
$log = mfLoghandler::singleton();
|
|
|
|
// query Home to get FTU data from RIMO - GET /queryHomeWithId
|
|
$params = [];
|
|
$params['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
$params["homeId"] = $home_external_id;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
//echo $qs."\n";
|
|
|
|
$queryHomeEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_QUERY_HOME;
|
|
$get_url = $queryHomeEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Getting Home to fetch FTU: $get_url");
|
|
$response = file_get_contents($get_url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
$log->error("Fehler beim auslesen der FTU ".$oaid);
|
|
return false;
|
|
}
|
|
|
|
$resp_data = json_decode($response);
|
|
if(!is_object($resp_data)) {
|
|
$log->error(__METHOD__.": OAID ".$oaid.": Cannot fetch Home from RIMO! Invalid Response!");
|
|
return false;
|
|
}
|
|
|
|
return $resp_data;
|
|
}
|
|
|
|
public static function getOaid($oaid) {
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$params = [];
|
|
$params['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
//echo $qs."\n";
|
|
|
|
$getOaidEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_GET_OAID;
|
|
$getOaidEp .= "/".$oaid;
|
|
$get_url = $getOaidEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Getting OAID from Rimo: $get_url");
|
|
$response = file_get_contents($get_url, false, $ctx);
|
|
//var_dump($response);exit;
|
|
if($response === false) {
|
|
$log->error("Fehler beim abfragen der OAID in RIMO ".$oaid);
|
|
return false;
|
|
}
|
|
|
|
$resp_data = json_decode($response);
|
|
if(!is_object($resp_data)) {
|
|
$log->error(__METHOD__.": OAID ".$oaid.": Cannot fetch OAID from RIMO! Invalid Response!");
|
|
return false;
|
|
}
|
|
|
|
return $resp_data;
|
|
}
|
|
|
|
public static function createOaid($oaid) {
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$params = [];
|
|
$params['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
$params['oaidName'] = $oaid;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'POST',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
//echo $qs."\n";
|
|
|
|
$createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_CREATE_OAID;
|
|
$post_url = $createOrderEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Creating OAID in Rimo: $post_url");
|
|
$response = file_get_contents($post_url, false, $ctx);
|
|
//var_dump($response);exit;
|
|
if($response === false) {
|
|
$log->error("Fehler beim Erstellen der OAID in RIMO ".$oaid);
|
|
return false;
|
|
}
|
|
|
|
$resp_data = json_decode($response);
|
|
if(!$resp_data->id || !$resp_data->name) {
|
|
$log->warning(__METHOD__.": Create OAID returned no ID or oaid name ".$oaid);
|
|
return false;
|
|
}
|
|
|
|
return $resp_data;
|
|
}
|
|
|
|
public static function unassignOaid($oaid, $ftu_external_id) {
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$params = [];
|
|
$params['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'DELETE',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
|
|
$unassignEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_UNASSIGN_OAID_TO_FTU;
|
|
$unassignEp = str_replace("{oaidName}", $oaid, $unassignEp);
|
|
$unassignEp = str_replace("{ftuExternalId}", $ftu_external_id, $unassignEp);
|
|
|
|
$delete_url = $unassignEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Unassigning OAID from FTU $ftu_external_id: $delete_url");
|
|
$response = file_get_contents($delete_url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
$log->error("Fehler beim Unassignen der OAID in RIMO ".$oaid.": $delete_url");
|
|
return false;
|
|
}
|
|
|
|
$resp_data = json_decode($response);
|
|
return $resp_data;
|
|
}
|
|
|
|
public static function assignOaid($oaid, $ftu_external_id) {
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$params = [];
|
|
$params['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'POST',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
|
|
$assignEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_ASSIGN_OAID_TO_FTU;
|
|
$assignEp = str_replace("{oaidName}", $oaid, $assignEp);
|
|
$assignEp = str_replace("{ftuExternalId}", $ftu_external_id, $assignEp);
|
|
|
|
$post_url = $assignEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Assigning OAID to current FTU $ftu_external_id: $post_url");
|
|
$response = file_get_contents($post_url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
$log->error("Fehler beim Assignen der OAID ".$oaid." in RIMO ".$oaid);
|
|
return false;
|
|
}
|
|
|
|
$resp_data = json_decode($response);
|
|
return $resp_data;
|
|
}
|
|
|
|
public static function createWorkorder($home_id, $data = []) {
|
|
$log = mfLoghandler::singleton();
|
|
|
|
if(!$home_id) return false;
|
|
|
|
$create_fields = [
|
|
"company" => "company",
|
|
"firstname" => "firstName",
|
|
"lastname" => "lastName",
|
|
"phone" => "phone",
|
|
"email" => "email",
|
|
"orderId" => "orderId"
|
|
];
|
|
|
|
$params = [];
|
|
$params['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'POST',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
foreach($data as $field => $value) {
|
|
if(array_key_exists($field, $create_fields)) {
|
|
$params[$create_fields[$field]] = $value;
|
|
}
|
|
}
|
|
|
|
$params["homeId"] = $home_id;
|
|
|
|
$qs = http_build_query($params);
|
|
//echo $qs."\n";
|
|
|
|
$createOrderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_CREATE_WORKORDER;
|
|
$post_url = $createOrderEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Creating Workorder: $post_url");
|
|
|
|
$response = file_get_contents($post_url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
return false;
|
|
}
|
|
|
|
return json_decode($response);
|
|
}
|
|
|
|
public static function updateWorkorder($rimo_id, Array $data = []) {
|
|
$log = mfLoghandler::singleton();
|
|
|
|
if(!$rimo_id || !count($data)) {
|
|
$log->debug(__METHOD__.": no rimo id or no update data");
|
|
return false;
|
|
}
|
|
|
|
$contact_fields = [
|
|
"company" => "company",
|
|
"firstname" => "firstName",
|
|
"lastname" => "lastName",
|
|
"phone" => "phone",
|
|
"email" => "email"
|
|
];
|
|
|
|
$update_data = [];
|
|
|
|
foreach($data as $field => $value) {
|
|
if(array_key_exists($field, $contact_fields)) {
|
|
$update_data[$contact_fields[$field]] = $value;
|
|
}
|
|
}
|
|
|
|
//var_dump($update_data);
|
|
if(!count($update_data)) {
|
|
$log->debug(__METHOD__.": no valid update data");
|
|
return false;
|
|
}
|
|
|
|
$update_data["orderId"] = $rimo_id;
|
|
$update_data['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'PUT',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($update_data);
|
|
|
|
$updateWorkorderEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_UPDATE_WORKORDER;
|
|
$put_url = $updateWorkorderEp."?".$qs;
|
|
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Updating Workorder Contact $rimo_id: $put_url");
|
|
$response = file_get_contents($put_url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
$log->error("Fehler beim Updaten der Workorder $rimo_id in RIMO");
|
|
return false;
|
|
}
|
|
|
|
$resp_data = json_decode($response);
|
|
return $resp_data;
|
|
}
|
|
|
|
public static function getWorkorderAhaReport($apikey, $rimo_id) {
|
|
$log = mfLoghandler::singleton();
|
|
|
|
if(!$rimo_id) {
|
|
$log->debug(__METHOD__.": no workorder id");
|
|
return false;
|
|
}
|
|
|
|
$items = self::getFilenames($apikey, $rimo_id);
|
|
|
|
if(!is_object($items)) {
|
|
return false;
|
|
}
|
|
|
|
if(!is_array($items->item) || !count($items->item)) {
|
|
return false;
|
|
}
|
|
|
|
foreach($items->item as $item) {
|
|
if(!$item->name) continue;
|
|
|
|
if(!preg_match('/_AHA.pdf$/i', $item->name)) continue;
|
|
|
|
$filename = $item->name;
|
|
}
|
|
|
|
if(!$filename) {
|
|
return false;
|
|
}
|
|
|
|
// fetch file
|
|
|
|
$params = [];
|
|
$params['apiKey'] = $apikey;
|
|
$params['objectId'] = $rimo_id;
|
|
$params['fileNames'] = $filename;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'GET',
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
|
|
$getFileEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_GET_FILES;
|
|
|
|
$get_url = $getFileEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Retrieving file from Rimo: $get_url");
|
|
$response = file_get_contents($get_url, false, $ctx);
|
|
|
|
if(!$response) return false;
|
|
|
|
// rimo returns multipart download
|
|
// check if multipart boundary exists
|
|
|
|
if(strpos($response, "\n--") !== false) {
|
|
// file begins with \n--, so boundary exists
|
|
$boundary_to_pos = strpos($response, "\r\n\r\n");
|
|
|
|
if($boundary_to_pos === false) {
|
|
$log->warn(__METHOD__.": Cannot find end of boundary");
|
|
return false;
|
|
}
|
|
$new_resp = substr($response, $boundary_to_pos + 4); // including \r\n\r\n
|
|
|
|
// get boundary label
|
|
$m = [];
|
|
if(!preg_match("/\n(--[^\r\n]+)\r?\n/", $response, $m)) {
|
|
$log->warn(__METHOD__.": Cannot find boundary label beginning");
|
|
return false;
|
|
}
|
|
if(!array_key_exists(1, $m)) {
|
|
$log->warn(__METHOD__.": Cannot find boundary label");
|
|
return false;
|
|
}
|
|
$boundary_label = trim($m[1]);
|
|
if(!$boundary_label) {
|
|
$log->warn(__METHOD__.": boundary label empty");
|
|
return false;
|
|
}
|
|
|
|
$boundary_end = $boundary_label."--";
|
|
|
|
$boundary_end_pos = strrpos($new_resp, "\r\n".$boundary_end);
|
|
//echo "$boundary_end $boundary_end_pos\n";exit;
|
|
|
|
$filecontent = substr($new_resp, 0, $boundary_end_pos);
|
|
//echo "$filecontent";exit;
|
|
|
|
} else {
|
|
$filecontent = $response;
|
|
}
|
|
|
|
|
|
$finfo = new Finfo(FILEINFO_MIME);
|
|
$mimetype = $finfo->buffer($filecontent);
|
|
|
|
//var_dump($mimetype);exit;
|
|
|
|
if(!stripos($mimetype, "pdf")) {
|
|
return false;
|
|
}
|
|
|
|
return $filecontent;
|
|
}
|
|
|
|
public static function getFilenames($apikey, $rimo_id) {
|
|
if(!$rimo_id) return false;
|
|
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$params = [];
|
|
$params['apiKey'] = $apikey;
|
|
$params['objectId'] = $rimo_id;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
|
|
$getFileEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_GET_FILENAMES;
|
|
|
|
$get_url = $getFileEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Getting Filenames from Rimo: $get_url");
|
|
$response = file_get_contents($get_url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
$log->error("Error retrieving filenames from RIMO for $rimo_id");
|
|
return false;
|
|
}
|
|
|
|
$resp_data = json_decode($response);
|
|
return $resp_data;
|
|
}
|
|
|
|
public static function getBuildingGeoJson($building_id) {
|
|
if(!$building_id) return false;
|
|
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$params = [];
|
|
$params['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
$params['buildingId'] = $building_id;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
|
|
$getFileEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_GET_JSON_FOR_BUILDING;
|
|
|
|
$get_url = $getFileEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Getting GeoJson from Rimo: $get_url");
|
|
$response = file_get_contents($get_url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
$log->error("Error retrieving GeoJson from RIMO for $building_id");
|
|
return false;
|
|
}
|
|
|
|
$resp_data = json_decode($response);
|
|
return $resp_data;
|
|
}
|
|
|
|
public static function addRemark($object_id, $text) {
|
|
if(!$object_id || !$text) return false;
|
|
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$params = [];
|
|
$params['apiKey'] = RIMO_API_JSON_APIKEY;
|
|
$params['objectId'] = $object_id;
|
|
$params['text'] = $text;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'POST',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$qs = http_build_query($params);
|
|
|
|
$addRemarkEp = RIMO_API_JSON_URL.RIMO_API_JSON_EP_ADD_REMARK;
|
|
|
|
$get_url = $addRemarkEp."?".$qs;
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$log->debug(__METHOD__.": Adding Remark to $object_id: $get_url");
|
|
$response = file_get_contents($get_url, false, $ctx);
|
|
|
|
if($response === false) {
|
|
$log->error("Error adding Remark to $object_id");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
} |