Files
backend_vib360_laravel/app/Services/TelemetryService.php
2025-05-15 15:28:59 +05:30

160 lines
5.0 KiB
PHP

<?php
namespace App\Services;
use App\Models\Device;
use App\Models\TimeseriesKeyMaster;
use App\Models\TimeseriesAlertMessage;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
class TelemetryService
{
protected $customerInfoService;
protected $adminService;
public function __construct(CustomerInfoService $customerInfoService, AdminService $adminService)
{
$this->customerInfoService = $customerInfoService;
$this->adminService = $adminService;
}
public function getDeviceHealth(array $values): string
{
$hasRed = false;
$hasOrange = false;
foreach ($values as $value) {
if ($value['key_name'] == 'PowerLoss_value') {
if ($value['value'] > 5 && $value['value'] < 10) {
$hasRed = true;
break;
} elseif ($value['value'] > 2.5 && $value['value'] < 5) {
$hasOrange = true;
}
} else {
if ($value['value'] > 0 && $value['value'] < 31) {
$hasRed = true;
break;
} elseif ($value['value'] < 71) {
$hasOrange = true;
}
}
}
return $hasRed ? 'red' : ($hasOrange ? 'orange' : 'green');
}
public function getAssetHealth(array $deviceHealthStatuses): string
{
if (in_array('red', $deviceHealthStatuses)) {
return 'red';
}
return in_array('orange', $deviceHealthStatuses) ? 'orange' : 'green';
}
public function convertToUserTimezone($utcDatetime, $userTimezone = 'Asia/Kolkata', $format = 'd-M-Y H:i:s')
{
return Carbon::parse($utcDatetime, 'UTC')
->setTimezone($userTimezone)
->format($format);
}
public function getThresholdMap(): array
{
return [
'ChannelSpeed' => [
['threshold' => 5000, 'color' => 'green']
],
'PowerLoss_value' => [
['threshold' => 2.5, 'color' => 'green'],
['threshold' => 5, 'color' => 'orange'],
['threshold' => 7.5, 'color' => 'red']
],
'GlobalLevel_value' => [
['threshold' => 2, 'color' => 'green'],
['threshold' => 6, 'color' => 'orange'],
['threshold' => 8, 'color' => 'red']
],
'default' => [
['threshold' => 25, 'color' => 'red'],
['threshold' => 50, 'color' => 'orange'],
['threshold' => 70, 'color' => 'green']
]
];
}
public function getThresholdLimit(): array
{
return [
'ChannelSpeed' => [
['min' => 0, 'color' => 50000]
],
'PowerLoss_value' => [
['min' => 0, 'color' => 10]
],
'GlobalLevel_value' => [
['min' => 0, 'color' => 10]
],
'StaticTorque_value' => [
['min' => 0, 'color' => 50000]
],
'StaticPower_value' => [
['min' => 0, 'color' => 500000]
],
'default' => [
['min' => 0, 'color' => 100]
]
];
}
public function getHealthStatus(float $value, array $thresholds): string
{
foreach (array_reverse($thresholds) as $t) {
if ($value > $t['threshold']) {
return match ($t['color']) {
'red' => 'Alert',
'orange' => 'Attention',
'green' => 'Stable',
default => 'Stable'
};
}
}
return 'Stable';
}
public function transformTelemetryData(array $data, array $displayNameMap): array
{
$transformedTelemetry = [];
$dateTime = null;
if (!empty($data) && is_array($data)) {
foreach ($data as $key => $items) {
foreach ($items as $item) {
$dateTime = Carbon::createFromTimestamp($item['ts'] / 1000)->format('d-M-Y H:i:s');
$value = $item['value'];
$thresholds = $this->getThresholdMap()[$key] ?? $this->getThresholdMap()['default'];
$thresholdLimits = $this->getThresholdLimit()[$key] ?? $this->getThresholdLimit()['default'];
$status = $this->getHealthStatus($value, $thresholds);
$transformedTelemetry[] = [
'display_name' => $displayNameMap[$key] ?? $key,
'value' => number_format((float)$value, 2),
'health_status' => $status,
'thresholds' => $thresholds,
'limit' => $thresholdLimits
];
}
}
}
return [
'telemetry' => $transformedTelemetry,
'dateTime' => $dateTime
];
}
}