Files
2026-02-12 15:03:37 +01:00

174 lines
6.1 KiB
PHP

<?php
namespace application\Api\v1\Modules\Operationaldata;
use application\Api\v1\Modules;
use PreorderCtag;
require_once(APPDIR."/Api/v1/Modules/ApiControllerModule.php");
/*
* API Endpoints for Operationaldata from SNOPP
*/
class SnoppCitycom extends Modules\ApiControllerModule
{
public function init()
{
}
public function getOntStatus($req_id) {
if(!$req_id) {
return \mfResponse::BadRequest(["message" => "id missing"]);
}
$wohneinheit = false;
if(is_numeric($req_id)) {
$id = $req_id;
$wohneinheit = new \ADBWohneinheit($id);
}
if(!$wohneinheit || !$wohneinheit->id) {
$oaid = $req_id;
$wohneinheit = \ADBWohneinheitModel::getFirst(["oaid" => $oaid]);
if (!$wohneinheit) {
return \mfResponse::NotFound(["message" => "Home not found"]);
}
}
$cc = new \Citycom_OanApiClient(CITYCOM_OAN_API_USER, CITYCOM_OAN_API_PASS);
$data = $cc->getOntStatus($wohneinheit->oaid);
$status = [];
$status["online"] = ($data->{"oper-status"} == "present" && $data->status == "Confirmed") ? 1 : 0;
$status["uptime"] = ($data->up_time) ? (date('U') - $data->up_time) : null;
$status["downtime"] = null;
$status["rx"] = $data->{"opt-signal-level"};
$status["tx"] = $data->tx_opt_level_dbm;
$status["distance"] = null;
return \mfResponse::Ok(["status" => $status]);
}
public function getOntTelemetry($req_id) {
if(!$req_id) {
return \mfResponse::BadRequest(["message" => "id missing"]);
}
$wohneinheit = false;
if(is_numeric($req_id)) {
$id = $req_id;
$wohneinheit = new \ADBWohneinheit($id);
}
if(!$wohneinheit || !$wohneinheit->id) {
$oaid = $req_id;
$wohneinheit = \ADBWohneinheitModel::getFirst(["oaid" => $oaid]);
if (!$wohneinheit) {
return \mfResponse::NotFound(["message" => "Home not found"]);
}
}
$cc = new \Citycom_OanApiClient(CITYCOM_OAN_API_USER, CITYCOM_OAN_API_PASS);
$data = $cc->getOntStatusDetail($wohneinheit->oaid);
print_r($data);exit;
return \mfResponse::Ok(["status" => $status]);
}
/**
* @param $req_id
* @return array
*
* Takes Order from SNOPP and updates CC service with correct product and
* if service is finished, updates billing_date of cc service and preorder status to 500
*/
public function orderService($req_id) {
if(!$req_id) {
return \mfResponse::BadRequest(["message" => "id missing"]);
}
$cc_api_client = new \Citycom_OanApiClient(CITYCOM_OAN_API_USER, CITYCOM_OAN_API_PASS);
$cc_api = new \Citycom_OanApiHelper($cc_api_client);
$bb_up = $this->post["bb_up"];
$bb_down = $this->post["bb_down"];
$execution_date = false;
/*if($this->post["execution_date"]) {
try {
$execution_date = new \DateTime($this->post["execution_date"]);
} catch(\Exception $e) {
return \mfResponse::BadRequest(["message" => "Invalid Timestamp format"]);
}
}*/
if(!is_numeric($bb_down) || !$bb_down || !is_numeric($bb_up) || !$bb_up || !$bb_down > 10000 || $bb_up > 10000) {
return \mfResponse::BadRequest(["message" => "Invalid bandwidth"]);
}
$wohneinheit = false;
if(is_numeric($req_id)) {
$id = $req_id;
$wohneinheit = new \ADBWohneinheit($id);
}
if(!$wohneinheit || !$wohneinheit->id) {
$oaid = $req_id;
$wohneinheit = \ADBWohneinheitModel::getFirst(["oaid" => $oaid]);
if (!$wohneinheit) {
return \mfResponse::NotFound(["message" => "Home not found"]);
}
}
$preorder = \PreorderModel::getFirstActive(["adb_wohneinheit_id" => $wohneinheit->id]);
if(!$preorder) {
return \mfResponse::NotFound(["message" => "Home not found"]);
}
// if all services are ordered and active, finish order and return active
$ctag = PreorderCtag::getFirstActive(["preorder_id" => $preorder->id, "service_type" => "inet"]);
if(!$ctag) {
return \mfResponse::NotFound(["message" => "Home not found"]);
}
$data["up"] = $bb_up;
$data["down"] = $bb_down;
$data["product_name"] = false;
$data["company"] = $preorder->company;
$data["firstname"] = $preorder->firstname;
$data["lastname"] = $preorder->lastname;
$data["phone"] = $preorder->phone;
$data["mobile"] = $preorder->mobile;
if($preorder->campaign->fulfillment == "citycom_oan") {
$data["product_name"] = "Estmk Greenstream OAN $bb_down/$bb_up";
}
// try to update product with bandwidth provided by snopp.
// updateService() only updates if values are changed.
if(!$cc_api->updateService($ctag->ext_id, $data)) {
$this->log->error(__METHOD__.": Error updating service {$ctag->ext_id} for preorder {$preorder->id}");
}
// check if service is active, if not return deferred
//$ctag = PreorderCtag::getFirstActive(["preorder_id" => $preorder->id, "service_type" => "inet"]);
if($ctag->ext_id && $ctag->ext_status == "finished") {
if($preorder->status->code < 500) {
$preorder->setNewStatusCode(500);
$preorder->save();
}
// update billing date in citycom service
$this->log->debug(__METHOD__.": Updating billing_date for {$ctag->ext_name} (Preorder {$preorder->id} {$preorder->oaid})");
$cc_api->updateService($ctag->ext_id, ["billing_date" => (new \DateTime("now"))->format("Y-m-d")]);
return \mfResponse::Ok(["message" => "Service active already", "activation_status" => "active"]);
}
return \mfResponse::Ok(["message" => "Services ordered, not yet finished", "activation_status" => "deferred"]);
}
}