Files
backend_vib360_laravel/app/Services/TelemetryService.php
2025-05-21 18:16:45 +05:30

179 lines
5.9 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']
],
'StaticTorque_value' => [
['threshold' => 5000, 'color' => 'green']
],
'StaticPower_value' => [
['threshold' => 5000, 'color' => 'green']
],
'StaticTorsion_value' => [
['threshold' => 6, 'color' => 'green']
],
'PowerLoss_value' => [
['threshold' => 2.5, 'color' => 'green'],
['threshold' => 5, 'color' => 'orange'],
['threshold' => 7.5, 'color' => 'orange']
],
'GlobalLevel_value' => [
['threshold' => 2, 'color' => 'green'],
['threshold' => 6, 'color' => 'orange'],
['threshold' => 8, 'color' => 'orange']
],
'default' => [
['threshold' => 25, 'color' => 'red'],
['threshold' => 50, 'color' => 'orange'],
['threshold' => 70, 'color' => 'orange']
]
];
}
public function getThresholdLimit($deviceId): array
{
$device = Device::find($deviceId);
return [
'ChannelSpeed' => [
['min' => 0, 'max' => $device->speed_limit]
],
'PowerLoss_value' => [
['min' => 0, 'max' => 10]
],
'GlobalLevel_value' => [
['min' => 0, 'max' => 10]
],
'StaticTorque_value' => [
['min' => 0, 'max' => $device->torque_limit]
],
'StaticPower_value' => [
['min' => 0, 'max' => $device->power_limit]
],
'StaticTorsion_value' => [
['min' => 0, 'max' => 6]
],
'default' => [
['min' => 0, 'max' => 100]
]
];
}
public function getHealthStatus(float $value, array $thresholds): string
{
foreach ($thresholds as $t) {
if ($value < $t['threshold']) {
return match ($t['color']) {
'red' => 'Alert',
'orange' => 'Attention',
'green' => 'Stable',
default => $t['threshold'] == 70 ? 'Stable' : 'Alert'
};
}
}
return 'Stable';
}
public function transformTelemetryData(string $deviceId, array $data, array $displayNameMap, bool $isActive): 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($deviceId)[$key] ?? $this->getThresholdLimit()['default'];
$status = $this->getHealthStatus($value, $thresholds);
$statusColor = match($status) {
'Alert' => 'red',
'Attention' => 'orange',
'Stable' => 'green'
};
$transformedTelemetry[] = [
'display_name' => $displayNameMap[$key] ?? $key,
'value' => number_format((float)$value, 2, '.', ''),
'health_status' => !$isActive ? 'Offline' : $status,
'status_color' => !$isActive ? 'gray' : $statusColor,
'thresholds' => $thresholds,
'limit' => $thresholdLimits
];
}
}
}
return [
'telemetry' => $transformedTelemetry,
'dateTime' => $dateTime
];
}
}