Files
kopalnia_2026/app/Services/SyndisService.php
T
2026-09-03 12:21:17 +02:00

154 lines
5.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Services;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use SoapClient;
/**
* Integracja z SYNDIS (SOAP) - pobór aktualnego pomiaru studni i zapis do bazy.
* Port legacy soap.php (SyndisConnector) + ajax_getdata4well.php.
* Funkcje składowane MySQL zastąpione własnym kodem ([[port-db-functions-to-php]]).
*
* SKALA: SYNDIS zwraca wydajność w m³/min; do bazy zapisujemy m³/h (×60) -
* baza zawsze m³/h ([[flow-unit-display]]).
*/
class SyndisService
{
/** Detale seg_dev_resources_det aktualizowane przy pobraniu (DRD_RESOURCES_DETAIL => klucz danych SYNDIS / wyliczenie). */
private function db()
{
return DB::connection('softspm');
}
/**
* Pobiera aktualne dane studni z SYNDIS i zapisuje pomiar. Zwraca true gdy
* zapisano (pompa załączona), false gdy pompa wyłączona / brak danych.
*
* @throws \Throwable gdy SOAP/SYNDIS niedostępny (obsługiwane w kontrolerze)
*/
public function refreshWell(int $drs, string $wellName, string $user): bool
{
$data = $this->getWellData($wellName);
if ((int) $this->raw($data, 'pumpOnOff') !== 1) {
return false; // legacy: zapis tylko gdy pompa pracuje
}
$v = fn (string $k) => $this->num($this->raw($data, $k));
// Przeliczenia jak legacy:
$pp = $v('pressureInPistonBody') - (($v('barometricPressuremA') * 10) / 98);
$lp = (float) ($this->lastDetailValue(240, $drs) ?? 0);
$hh = $lp - $v('dynamicWaterTable');
$qH = $v('pumpEfficiency') * 60; // m³/min -> m³/h
$now = Carbon::now()->toDateTimeString();
$meas = [
'RHD_DRS_ID' => $drs,
'RHD_DATE' => $now,
'RHD_HD_VAL' => $v('dynamicWaterTable'),
'RHD_Q_VAL' => $qH,
'RHD_P1_VAL' => $v('pressureBeforeThrottle'),
'RHD_P2_VAL' => $v('pressureAfterThrottle'),
'RHD_I_VAL' => $v('currentL1'),
'RHD_U_VAL' => $v('voltageL12'),
'RHD_PE_VAL' => $v('activePower'),
'RHD_COS_VAL' => $v('cos'),
'RHD_PP_VAL' => $pp,
'RHD_HH_VAL' => $hh,
'RHD_WTEMP_VAL' => $v('waterTemperature'),
'RHD_DGN_VAL' => $v('currentDiagnosticsOperationState'),
'RHD_AUDIT_CD' => $now,
];
// Detale do podmiany w seg_dev_resources_det (jak legacy delete+insert).
$details = [
200 => $qH,
205 => $v('dynamicWaterTable'),
215 => $v('pressureBeforeThrottle'),
220 => $v('pressureAfterThrottle'),
245 => $v('waterTemperature'),
260 => $v('activePower'),
265 => $v('activeEnergy'),
270 => $v('currentL1'),
275 => $v('voltageL12'),
280 => $v('cos'),
401 => $pp,
402 => $hh,
];
$this->db()->transaction(function () use ($drs, $meas, $details, $user, $now) {
$this->db()->table('rep_soap_syndis_quality_data')
->where('SQD_DRS_ID', $drs)->update(['SQD_AUDIT_CD' => $now]);
$this->db()->table('rep_his_data_everyday')->insert($meas);
$this->db()->table('rep_his_data_demand')->insert($meas);
foreach ($details as $detail => $value) {
$this->db()->table('seg_dev_resources_det')
->where('DRD_RESOURCES_DETAIL', $detail)
->where('DRD_DRS_ID', $drs)
->delete();
$this->db()->table('seg_dev_resources_det')->insert([
'DRD_DRS_ID' => $drs,
'DRD_RESOURCES_DETAIL'=> $detail,
'DRD_VALUE' => (string) $value,
'DRD_EXPORT_SEGAP' => '1',
'DRD_AUDIT_USR' => $user,
'DRD_AUDIT_DATE' => $now,
]);
}
});
return true;
}
/** Wywołanie SOAP GetWellData -> tablica (port SyndisConnector::GetWellDataAsArray). */
private function getWellData(string $wellName): array
{
if (! class_exists(SoapClient::class)) {
throw new \RuntimeException('Rozszerzenie PHP soap nie jest dostępne na serwerze.');
}
$cfg = config('softspm.syndis');
$client = new SoapClient($cfg['wsdl'], ['trace' => 1, 'cache_wsdl' => WSDL_CACHE_NONE]);
if (! empty($cfg['location'])) {
$client->__setLocation($cfg['location']);
}
return $this->objectToArray($client->GetWellData(['id' => $wellName]));
}
/** Ostatnia wartość detalu (port getLastValue) - dla Lp przy przeliczeniu Hh. */
private function lastDetailValue(int $detail, int $drs): ?string
{
return $this->db()->table('seg_dev_resources_det')
->where('DRD_RESOURCES_DETAIL', $detail)
->where('DRD_DRS_ID', $drs)
->orderByDesc('DRD_AUDIT_DATE')->orderByDesc('DRD_ID')
->value('DRD_VALUE');
}
/** SYNDIS zwraca pola jako {'value' => x}; wyciąga surową wartość. */
private function raw(array $data, string $key): mixed
{
return $data[$key]['value'] ?? null;
}
private function num(mixed $v): float
{
return (float) str_replace(',', '.', (string) ($v ?? 0));
}
private function objectToArray(mixed $d): mixed
{
if (is_object($d)) {
$d = get_object_vars($d);
}
return is_array($d) ? array_map([$this, 'objectToArray'], $d) : $d;
}
}