sneha #64
@@ -464,132 +464,271 @@ class TelemetryController extends Controller
|
||||
// }
|
||||
|
||||
|
||||
// public function telemetryDataAsset(Request $request)
|
||||
// {
|
||||
// $token = readHeaderToken();
|
||||
// $customerId = User::where('id', $token['sub'])->value('customer_id');
|
||||
|
||||
// $validator = Validator::make($request->all(), [
|
||||
// 'asset_id' => 'required|string',
|
||||
// ]);
|
||||
|
||||
// if ($validator->fails()) {
|
||||
// return jsonResponseWithErrorMessage($validator->errors()->first(), 400);
|
||||
// }
|
||||
|
||||
// $assetId = $request->input('asset_id');
|
||||
|
||||
// // Set timestamps in milliseconds for database query
|
||||
// $endTsMs = now()->timestamp * 1000; // Current time in milliseconds
|
||||
// $startTsMs = $endTsMs - (30 * 60 * 1000); // 5 minutes ago in milliseconds
|
||||
|
||||
// $devices = Device::with([
|
||||
// 'deviceProfile',
|
||||
// 'timeseriesKeys' => function ($query) {
|
||||
// $query->where('display_on_dashboard', true)
|
||||
// ->orWhere('display_on_popup', true)
|
||||
// ->select(['id', 'device_profile_xid', 'key_name', 'display_name', 'display_on_dashboard', 'display_on_popup']);
|
||||
// }
|
||||
// ])->where('asset_id', $assetId)
|
||||
// ->where('customer_id', $customerId)
|
||||
// ->get();
|
||||
|
||||
// if ($devices->isEmpty()) {
|
||||
// return response()->json(['error' => 'No devices found for the asset'], 404);
|
||||
// }
|
||||
|
||||
// $response = $devices->map(function ($device) use ($startTsMs, $endTsMs) {
|
||||
// $keysData = $device->timeseriesKeys;
|
||||
// $keyNames = $keysData->pluck('key_name')->toArray();
|
||||
|
||||
// // Get telemetry data for start and end times
|
||||
// $startTelemetry = $this->customerInfoService->getTelemetryData($device, $keyNames, $startTsMs, $startTsMs);
|
||||
// $endTelemetry = $this->customerInfoService->getTelemetryData($device, $keyNames, $endTsMs, $endTsMs);
|
||||
|
||||
// $alarmMap = $this->getDeviceAlarmsForTelemetry($device->id);
|
||||
// $alertMessages = TimeseriesAlertMessage::whereIn('timeseries_key_master_xid', $keysData->pluck('id'))
|
||||
// ->orderBy('min_value', 'asc')
|
||||
// ->get()
|
||||
// ->groupBy('timeseries_key_master_xid');
|
||||
|
||||
// $telemetry = $keysData->map(function ($keyData) use ($startTelemetry, $endTelemetry, $alarmMap, $alertMessages) {
|
||||
// $startData = collect($startTelemetry[$keyData->key_name] ?? [])->last();
|
||||
// $endData = collect($endTelemetry[$keyData->key_name] ?? [])->last();
|
||||
|
||||
// $startValue = floatval($startData['value'] ?? 0);
|
||||
// $endValue = floatval($endData['value'] ?? 0);
|
||||
|
||||
// // Determine trend based on start vs end values
|
||||
// $trend = null;
|
||||
// if ($startValue > 0) {
|
||||
// if ($endValue > $startValue) {
|
||||
// $trend = 'upward';
|
||||
// } elseif ($endValue < $startValue) {
|
||||
// $trend = 'downward';
|
||||
// } else {
|
||||
// $trend = 'stable';
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Format timestamp properly (convert to seconds if in milliseconds)
|
||||
// if (!isset($currentData['ts'])) {
|
||||
// $timestamp = now()->timestamp;
|
||||
// } elseif (is_float($currentData['ts']) || $currentData['ts'] > 9999999999) {
|
||||
// $timestamp = intval($currentData['ts'] / 1000);
|
||||
// } else {
|
||||
// $timestamp = $currentData['ts'];
|
||||
// }
|
||||
|
||||
// // Color code logic
|
||||
// $colorCode = null;
|
||||
// if ($endValue == 0) {
|
||||
// $colorCode = "grey";
|
||||
// } else {
|
||||
// if (isset($alertMessages[$keyData->id])) {
|
||||
// foreach ($alertMessages[$keyData->id] as $alertMsg) {
|
||||
// if (is_null($alertMsg->min_value) || is_null($alertMsg->max_value)) {
|
||||
// $colorCode = 'green';
|
||||
// break;
|
||||
// }
|
||||
// if ($endValue >= floatval($alertMsg->min_value) && $endValue <= floatval($alertMsg->max_value)) {
|
||||
// $colorCode = $alertMsg->color_code;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// return [
|
||||
// 'key_name' => $keyData->key_name,
|
||||
// 'display_name' => $keyData->display_name,
|
||||
// 'display_on_dashboard' => $keyData->display_on_dashboard,
|
||||
// 'display_on_popup' => $keyData->display_on_popup,
|
||||
// 'timestamp' => $timestamp,
|
||||
// 'value' => $endValue,
|
||||
// 'start_value' => $startValue > 0 ? $startValue : null,
|
||||
// 'trend' => $trend,
|
||||
// 'alert' => isset($alarmMap[$keyData->key_name]),
|
||||
// 'color_code' => $colorCode,
|
||||
// ];
|
||||
// })->filter()->values();
|
||||
|
||||
// return [
|
||||
// 'device_id' => (string) $device->id,
|
||||
// 'device_profile_name' => (string) $device->deviceProfile->name,
|
||||
// 'asset_id' => (string) $device->asset_id,
|
||||
// 'device_profile_id' => (string) $device->device_profile_id,
|
||||
// 'telemetry' => $telemetry,
|
||||
// ];
|
||||
// });
|
||||
|
||||
// return response()->json([
|
||||
// 'telemetry' => $response,
|
||||
// 'time_range' => [
|
||||
// 'startTs' => intval($startTsMs / 1000), // Current period start (seconds)
|
||||
// 'endTs' => intval($endTsMs / 1000), // Current period end (seconds)
|
||||
// ]
|
||||
// ]);
|
||||
// }
|
||||
|
||||
public function telemetryDataAsset(Request $request)
|
||||
{
|
||||
$token = readHeaderToken();
|
||||
$customerId = User::where('id', $token['sub'])->value('customer_id');
|
||||
{
|
||||
// Get authenticated user's customer ID
|
||||
$token = readHeaderToken();
|
||||
$customerId = User::where('id', $token['sub'])->value('customer_id');
|
||||
dd($customerId);
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'asset_id' => 'required|string',
|
||||
]);
|
||||
if (!$customerId) {
|
||||
return response()->json(['error' => 'Unauthorized - Invalid user'], 401);
|
||||
}
|
||||
|
||||
if ($validator->fails()) {
|
||||
return jsonResponseWithErrorMessage($validator->errors()->first(), 400);
|
||||
}
|
||||
// Validate request
|
||||
$validator = Validator::make($request->all(), [
|
||||
'asset_id' => 'required|string',
|
||||
]);
|
||||
|
||||
$assetId = $request->input('asset_id');
|
||||
if ($validator->fails()) {
|
||||
return jsonResponseWithErrorMessage($validator->errors()->first(), 400);
|
||||
}
|
||||
|
||||
// Set timestamps in milliseconds for database query
|
||||
$endTsMs = now()->timestamp * 1000; // Current time in milliseconds
|
||||
$startTsMs = $endTsMs - (30 * 60 * 1000); // 5 minutes ago in milliseconds
|
||||
$assetId = $request->input('asset_id');
|
||||
|
||||
$devices = Device::with([
|
||||
'deviceProfile',
|
||||
$assetExists = Asset::where('id', $assetId)
|
||||
->where('customer_xid', $customerId)->exists();
|
||||
|
||||
// dd($assetExists);
|
||||
|
||||
if (!$assetExists) {
|
||||
return response()->json(['error' => 'Asset not found or access denied'], 404);
|
||||
}
|
||||
|
||||
// Set timestamps in milliseconds for database query
|
||||
$endTsMs = now()->timestamp * 1000; // Current time in milliseconds
|
||||
$startTsMs = $endTsMs - (30 * 60 * 1000); // 30 minutes ago in milliseconds
|
||||
|
||||
// Get devices that belong to both the asset and customer
|
||||
$devices = Device::with([
|
||||
'deviceProfile:id,name',
|
||||
'timeseriesKeys' => function ($query) {
|
||||
$query->where('display_on_dashboard', true)
|
||||
->orWhere('display_on_popup', true)
|
||||
->select(['id', 'device_profile_xid', 'key_name', 'display_name', 'display_on_dashboard', 'display_on_popup']);
|
||||
->select(['id', 'device_profile_xid', 'key_name', 'display_name',
|
||||
'display_on_dashboard', 'display_on_popup']);
|
||||
}
|
||||
])->where('asset_id', $assetId)
|
||||
->where('customer_id', $customerId)
|
||||
->get();
|
||||
])
|
||||
->where('asset_id', $assetId)
|
||||
->where('customer_id', $customerId)
|
||||
->select(['id', 'device_profile_id', 'asset_id', 'customer_id'])
|
||||
->get();
|
||||
|
||||
if ($devices->isEmpty()) {
|
||||
return response()->json(['error' => 'No devices found for the asset'], 404);
|
||||
}
|
||||
if ($devices->isEmpty()) {
|
||||
return response()->json(['error' => 'No devices found for this asset'], 404);
|
||||
}
|
||||
|
||||
$response = $devices->map(function ($device) use ($startTsMs, $endTsMs) {
|
||||
$keysData = $device->timeseriesKeys;
|
||||
$keyNames = $keysData->pluck('key_name')->toArray();
|
||||
$response = $devices->map(function ($device) use ($startTsMs, $endTsMs) {
|
||||
$keysData = $device->timeseriesKeys;
|
||||
$keyNames = $keysData->pluck('key_name')->toArray();
|
||||
|
||||
// Get telemetry data for start and end times
|
||||
$startTelemetry = $this->customerInfoService->getTelemetryData($device, $keyNames, $startTsMs, $startTsMs);
|
||||
$endTelemetry = $this->customerInfoService->getTelemetryData($device, $keyNames, $endTsMs, $endTsMs);
|
||||
// Get telemetry data for start and end times
|
||||
$startTelemetry = $this->customerInfoService->getTelemetryData($device, $keyNames, $startTsMs, $startTsMs);
|
||||
$endTelemetry = $this->customerInfoService->getTelemetryData($device, $keyNames, $endTsMs, $endTsMs);
|
||||
|
||||
$alarmMap = $this->getDeviceAlarmsForTelemetry($device->id);
|
||||
$alertMessages = TimeseriesAlertMessage::whereIn('timeseries_key_master_xid', $keysData->pluck('id'))
|
||||
->orderBy('min_value', 'asc')
|
||||
->get()
|
||||
->groupBy('timeseries_key_master_xid');
|
||||
$alarmMap = $this->getDeviceAlarmsForTelemetry($device->id);
|
||||
$alertMessages = TimeseriesAlertMessage::whereIn('timeseries_key_master_xid', $keysData->pluck('id'))
|
||||
->orderBy('min_value', 'asc')
|
||||
->get()
|
||||
->groupBy('timeseries_key_master_xid');
|
||||
|
||||
$telemetry = $keysData->map(function ($keyData) use ($startTelemetry, $endTelemetry, $alarmMap, $alertMessages) {
|
||||
$startData = collect($startTelemetry[$keyData->key_name] ?? [])->last();
|
||||
$endData = collect($endTelemetry[$keyData->key_name] ?? [])->last();
|
||||
$telemetry = $keysData->map(function ($keyData) use ($startTelemetry, $endTelemetry, $alarmMap, $alertMessages) {
|
||||
$startData = collect($startTelemetry[$keyData->key_name] ?? [])->last();
|
||||
$endData = collect($endTelemetry[$keyData->key_name] ?? [])->last();
|
||||
|
||||
$startValue = floatval($startData['value'] ?? 0);
|
||||
$endValue = floatval($endData['value'] ?? 0);
|
||||
$startValue = floatval($startData['value'] ?? 0);
|
||||
$endValue = floatval($endData['value'] ?? 0);
|
||||
|
||||
// Determine trend based on start vs end values
|
||||
$trend = null;
|
||||
if ($startValue > 0) {
|
||||
if ($endValue > $startValue) {
|
||||
$trend = 'upward';
|
||||
} elseif ($endValue < $startValue) {
|
||||
$trend = 'downward';
|
||||
} else {
|
||||
$trend = 'stable';
|
||||
// Determine trend
|
||||
$trend = null;
|
||||
if ($startValue > 0) {
|
||||
if ($endValue > $startValue) {
|
||||
$trend = 'upward';
|
||||
} elseif ($endValue < $startValue) {
|
||||
$trend = 'downward';
|
||||
} else {
|
||||
$trend = 'stable';
|
||||
}
|
||||
}
|
||||
|
||||
// Format timestamp
|
||||
$timestamp = now()->timestamp;
|
||||
if (isset($endData['ts'])) {
|
||||
$timestamp = ($endData['ts'] > 9999999999) ? intval($endData['ts'] / 1000) : $endData['ts'];
|
||||
}
|
||||
|
||||
$colorCode = null;
|
||||
if ($endValue != 0 && isset($alertMessages[$keyData->id])) {
|
||||
foreach ($alertMessages[$keyData->id] as $alertMsg) {
|
||||
if (is_null($alertMsg->min_value) && is_null($alertMsg->max_value)) {
|
||||
$colorCode = 'green';
|
||||
break;
|
||||
}
|
||||
if ($endValue >= floatval($alertMsg->min_value) && $endValue <= floatval($alertMsg->max_value)) {
|
||||
$colorCode = $alertMsg->color_code;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Format timestamp properly (convert to seconds if in milliseconds)
|
||||
if (!isset($currentData['ts'])) {
|
||||
$timestamp = now()->timestamp;
|
||||
} elseif (is_float($currentData['ts']) || $currentData['ts'] > 9999999999) {
|
||||
$timestamp = intval($currentData['ts'] / 1000);
|
||||
} else {
|
||||
$timestamp = $currentData['ts'];
|
||||
}
|
||||
|
||||
// Color code logic
|
||||
$colorCode = null;
|
||||
if ($endValue == 0) {
|
||||
$colorCode = "grey";
|
||||
} else {
|
||||
if (isset($alertMessages[$keyData->id])) {
|
||||
foreach ($alertMessages[$keyData->id] as $alertMsg) {
|
||||
if (is_null($alertMsg->min_value) || is_null($alertMsg->max_value)) {
|
||||
$colorCode = 'green';
|
||||
break;
|
||||
}
|
||||
if ($endValue >= floatval($alertMsg->min_value) && $endValue <= floatval($alertMsg->max_value)) {
|
||||
$colorCode = $alertMsg->color_code;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'key_name' => $keyData->key_name,
|
||||
'display_name' => $keyData->display_name,
|
||||
'display_on_dashboard' => $keyData->display_on_dashboard,
|
||||
'display_on_popup' => $keyData->display_on_popup,
|
||||
'timestamp' => $timestamp,
|
||||
'value' => $endValue,
|
||||
'start_value' => $startValue > 0 ? $startValue : null,
|
||||
'trend' => $trend,
|
||||
'alert' => isset($alarmMap[$keyData->key_name]),
|
||||
'color_code' => $colorCode,
|
||||
];
|
||||
})->filter()->values();
|
||||
}
|
||||
|
||||
return [
|
||||
'device_id' => (string) $device->id,
|
||||
'device_profile_name' => (string) $device->deviceProfile->name,
|
||||
'asset_id' => (string) $device->asset_id,
|
||||
'device_profile_id' => (string) $device->device_profile_id,
|
||||
'telemetry' => $telemetry,
|
||||
'key_name' => $keyData->key_name,
|
||||
'display_name' => $keyData->display_name,
|
||||
'display_on_dashboard' => $keyData->display_on_dashboard,
|
||||
'display_on_popup' => $keyData->display_on_popup,
|
||||
'timestamp' => $timestamp,
|
||||
'value' => $endValue,
|
||||
'start_value' => $startValue > 0 ? $startValue : null,
|
||||
'trend' => $trend,
|
||||
'alert' => isset($alarmMap[$keyData->key_name]),
|
||||
'color_code' => $colorCode ?: ($endValue == 0 ? 'grey' : 'green'),
|
||||
];
|
||||
});
|
||||
})->filter()->values();
|
||||
|
||||
return response()->json([
|
||||
'telemetry' => $response,
|
||||
'time_range' => [
|
||||
'startTs' => intval($startTsMs / 1000), // Current period start (seconds)
|
||||
'endTs' => intval($endTsMs / 1000), // Current period end (seconds)
|
||||
]
|
||||
]);
|
||||
}
|
||||
return [
|
||||
'device_id' => (string) $device->id,
|
||||
'device_profile_name' => (string) $device->deviceProfile->name,
|
||||
'asset_id' => (string) $device->asset_id,
|
||||
'device_profile_id' => (string) $device->device_profile_id,
|
||||
'telemetry' => $telemetry,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'telemetry' => $response,
|
||||
'time_range' => [
|
||||
'startTs' => intval($startTsMs / 1000),
|
||||
'endTs' => intval($endTsMs / 1000),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
private function getDeviceAlarmsForTelemetry($deviceId)
|
||||
{
|
||||
|
||||
@@ -17,177 +17,7 @@ class TimeseriesAlertMessageController extends Controller
|
||||
{
|
||||
$this->customerInfoService = $customerInfoService;
|
||||
}
|
||||
// public function alertMessage(Request $request){
|
||||
// $token = readHeaderToken();
|
||||
// if (!$token) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'error' => 'Authorization token required'
|
||||
// ], 401);
|
||||
// }
|
||||
|
||||
// $validator = Validator::make($request->all(), [
|
||||
// 'device_id' => 'required|string',
|
||||
// 'startTs' => 'nullable|string',
|
||||
// 'endTs' => 'nullable|string',
|
||||
// ]);
|
||||
|
||||
// if ($validator->fails()) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'error' => $validator->errors()->first()
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
|
||||
// $deviceId = $request->input('device_id');
|
||||
|
||||
// // $deviceWithTelemetry = Device::with([
|
||||
// // 'deviceProfile',
|
||||
// // 'timeseriesKeys' => function ($query) {
|
||||
// // $query->where(function ($q) {
|
||||
// // $q->where('display_on_dashboard', true)
|
||||
// // ->orWhere('display_on_popup', true);
|
||||
// // })
|
||||
// // ->select('id','device_profile_xid', 'display_name')
|
||||
// // ->with(['timeseriesAlert' => function($query) { // Use singular name to match model
|
||||
// // $query->select('id', 'timeseries_key_master_xid', 'min_value', 'max_value', 'color_code', 'alert_msg');
|
||||
// // }]);
|
||||
// // }
|
||||
// // ])
|
||||
// // ->where('id', $deviceId)
|
||||
// // ->firstOrFail();
|
||||
|
||||
|
||||
// }
|
||||
|
||||
// public function alertMessage(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// // Authentication
|
||||
// $token = readHeaderToken();
|
||||
// if (!$token) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'message' => 'Authorization token required'
|
||||
// ], 401);
|
||||
// }
|
||||
|
||||
// // Validation
|
||||
// $validator = Validator::make($request->all(), [
|
||||
// 'device_id' => 'required|string|exists:devices,id',
|
||||
// 'startTs' => 'nullable|numeric',
|
||||
// 'endTs' => 'nullable|numeric|gte:startTs',
|
||||
// ]);
|
||||
|
||||
// if ($validator->fails()) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'message' => $validator->errors()->first()
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
// // Fetch device with relationships
|
||||
// $device = Device::with([
|
||||
// 'deviceProfile',
|
||||
// 'timeseriesKeys' => function ($query) {
|
||||
// $query->where(function ($q) {
|
||||
// $q->where('display_on_dashboard', true)
|
||||
// ->orWhere('display_on_popup', true);
|
||||
// })
|
||||
// ->select('id', 'device_profile_xid', 'key_name', 'display_name')
|
||||
// ->with(['timeseriesAlert' => function ($query) {
|
||||
// $query->select('id', 'timeseries_key_master_xid', 'min_value', 'max_value', 'color_code', 'alert_msg');
|
||||
// }]);
|
||||
// }
|
||||
// ])->findOrFail($request->device_id);
|
||||
|
||||
// // Get telemetry data
|
||||
// $telemetryData = $this->customerInfoService->getTelemetryDataDevice(
|
||||
// $device,
|
||||
// $device->timeseriesKeys->pluck('key_name')->toArray(),
|
||||
// $request->startTs,
|
||||
// $request->endTs
|
||||
// );
|
||||
|
||||
// if (isset($telemetryData['error'])) {
|
||||
// throw new \Exception($telemetryData['error']);
|
||||
// }
|
||||
|
||||
// // Process data
|
||||
// $result = [
|
||||
// 'alerts' => [],
|
||||
// 'normal_telemetry' => []
|
||||
// ];
|
||||
|
||||
// foreach ($device->timeseriesKeys as $key) {
|
||||
// if (!isset($telemetryData[$key->key_name])) continue;
|
||||
|
||||
// $dataPoint = $telemetryData[$key->key_name][0] ?? null;
|
||||
// if (!$dataPoint || !isset($dataPoint['value'])) continue;
|
||||
|
||||
// $item = [
|
||||
// 'key' => $key->key_name,
|
||||
// 'display_name' => $key->display_name,
|
||||
// 'value' => $dataPoint['value'],
|
||||
// 'timestamp' => $dataPoint['ts'] ?? null
|
||||
// ];
|
||||
|
||||
// // Check alerts
|
||||
// // Inside your alertMessage controller method, modify the alert check:
|
||||
// foreach ($key->timeseriesAlert as $alert) {
|
||||
// $isBelowMin = $alert->min_value !== null && $dataPoint['value'] < $alert->min_value;
|
||||
// $isAboveMax = $alert->max_value !== null && $dataPoint['value'] > $alert->max_value;
|
||||
|
||||
// if ($isBelowMin || $isAboveMax) {
|
||||
// Log::warning("Alert triggered", [
|
||||
// 'device' => $device->id,
|
||||
// 'key' => $key->key_name,
|
||||
// 'value' => $dataPoint['value'],
|
||||
// 'threshold_min' => $alert->min_value,
|
||||
// 'threshold_max' => $alert->max_value,
|
||||
// 'condition' => $isBelowMin ? 'below_min' : 'above_max'
|
||||
// ]);
|
||||
|
||||
// $item['alert'] = [
|
||||
// 'message' => $alert->alert_msg,
|
||||
// 'color_code' => $alert->color_code,
|
||||
// 'thresholds' => [
|
||||
// 'min' => $alert->min_value,
|
||||
// 'max' => $alert->max_value
|
||||
// ]
|
||||
// ];
|
||||
// $result['alerts'][] = $item;
|
||||
// continue 2;
|
||||
// }
|
||||
// }
|
||||
|
||||
// $result['normal_telemetry'][] = $item;
|
||||
// }
|
||||
|
||||
// return response()->json([
|
||||
// 'success' => true,
|
||||
// 'device' => [
|
||||
// 'id' => $device->id,
|
||||
// 'name' => $device->name,
|
||||
// 'profile' => $device->deviceProfile
|
||||
// ],
|
||||
// 'data' => $result
|
||||
// ]);
|
||||
// } catch (\Exception $e) {
|
||||
// Log::error("Alert processing failed: {$e->getMessage()}", [
|
||||
// 'device_id' => $request->device_id ?? null,
|
||||
// 'trace' => $e->getTraceAsString()
|
||||
// ]);
|
||||
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'message' => 'Failed to process alerts: ' . $e->getMessage()
|
||||
// ], 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
public function alertMessage(Request $request)
|
||||
public function alertMessage(Request $request, $deviceId)
|
||||
{
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
@@ -199,7 +29,6 @@ class TimeseriesAlertMessageController extends Controller
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'device_id' => 'required|string|exists:devices,id',
|
||||
'startTs' => 'nullable|numeric',
|
||||
'endTs' => 'nullable|numeric|gte:startTs',
|
||||
]);
|
||||
@@ -222,7 +51,7 @@ class TimeseriesAlertMessageController extends Controller
|
||||
$query->select('id', 'timeseries_key_master_xid', 'min_value', 'max_value', 'alert_msg');
|
||||
}]);
|
||||
}
|
||||
])->findOrFail($request->device_id);
|
||||
])->findOrFail($deviceId);
|
||||
|
||||
$telemetryData = $this->customerInfoService->getTelemetryDataDevice(
|
||||
$device,
|
||||
@@ -269,14 +98,11 @@ class TimeseriesAlertMessageController extends Controller
|
||||
'device_id' => $device->id,
|
||||
'key_name' => $key->key_name,
|
||||
'display_name' => $key->display_name,
|
||||
'alert_msg' => $formattedAlertMsg, // Return as an array
|
||||
'alert_msg' => $formattedAlertMsg,
|
||||
];
|
||||
break; // Stop at the first matching alert
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
@@ -286,7 +112,7 @@ class TimeseriesAlertMessageController extends Controller
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Alert processing failed: {$e->getMessage()}", [
|
||||
'device_id' => $request->device_id ?? null,
|
||||
'device_id' => $deviceId ?? null,
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
@@ -297,4 +123,114 @@ class TimeseriesAlertMessageController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// public function alertMessage(Request $request)
|
||||
// {
|
||||
// try {
|
||||
// $token = readHeaderToken();
|
||||
// if (!$token) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'message' => 'Authorization token required'
|
||||
// ], 401);
|
||||
// }
|
||||
|
||||
// $validator = Validator::make($request->all(), [
|
||||
// 'device_id' => 'required|string|exists:devices,id',
|
||||
// 'startTs' => 'nullable|numeric',
|
||||
// 'endTs' => 'nullable|numeric|gte:startTs',
|
||||
// ]);
|
||||
|
||||
// if ($validator->fails()) {
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'message' => $validator->errors()->first()
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
// $device = Device::with([
|
||||
// 'timeseriesKeys' => function ($query) {
|
||||
// $query->where(function ($q) {
|
||||
// $q->where('display_on_dashboard', true)
|
||||
// ->orWhere('display_on_popup', true);
|
||||
// })
|
||||
// ->select('id', 'device_profile_xid', 'key_name', 'display_name')
|
||||
// ->with(['timeseriesAlert' => function ($query) {
|
||||
// $query->select('id', 'timeseries_key_master_xid', 'min_value', 'max_value', 'alert_msg');
|
||||
// }]);
|
||||
// }
|
||||
// ])->findOrFail($request->device_id);
|
||||
|
||||
// $telemetryData = $this->customerInfoService->getTelemetryDataDevice(
|
||||
// $device,
|
||||
// $device->timeseriesKeys->pluck('key_name')->toArray(),
|
||||
// $request->startTs,
|
||||
// $request->endTs
|
||||
// );
|
||||
|
||||
// if (isset($telemetryData['error'])) {
|
||||
// throw new \Exception($telemetryData['error']);
|
||||
// }
|
||||
|
||||
// $existingKeys = array_keys($telemetryData);
|
||||
// $result = [];
|
||||
|
||||
// foreach ($device->timeseriesKeys as $key) {
|
||||
// if (!in_array($key->key_name, $existingKeys, true)) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $dataPoint = $telemetryData[$key->key_name][0] ?? null;
|
||||
// if (!$dataPoint || !isset($dataPoint['value'])) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $currentValue = (float)$dataPoint['value'];
|
||||
|
||||
// foreach ($key->timeseriesAlert as $alert) {
|
||||
// $minValue = $alert->min_value !== null ? (float)$alert->min_value : null;
|
||||
// $maxValue = $alert->max_value !== null ? (float)$alert->max_value : null;
|
||||
|
||||
// $isBelowMin = $minValue !== null && $currentValue < $minValue;
|
||||
// $isAboveMax = $maxValue !== null && $currentValue > $maxValue;
|
||||
|
||||
// if ($isBelowMin || $isAboveMax) {
|
||||
// // Convert alert message into an array and remove existing numbers
|
||||
// $formattedAlertMsg = array_map(
|
||||
// fn($line, $index) => ($index + 1) . ". " . preg_replace('/^\d+\.\s*/', '', trim($line)),
|
||||
// explode(";", $alert->alert_msg),
|
||||
// array_keys(explode(";", $alert->alert_msg))
|
||||
// );
|
||||
|
||||
// $result[] = [
|
||||
// 'device_id' => $device->id,
|
||||
// 'key_name' => $key->key_name,
|
||||
// 'display_name' => $key->display_name,
|
||||
// 'alert_msg' => $formattedAlertMsg, // Return as an array
|
||||
// ];
|
||||
// break; // Stop at the first matching alert
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// }
|
||||
|
||||
// return response()->json([
|
||||
// 'success' => true,
|
||||
// 'alerts' => $result
|
||||
// ]);
|
||||
|
||||
// } catch (\Exception $e) {
|
||||
// Log::error("Alert processing failed: {$e->getMessage()}", [
|
||||
// 'device_id' => $request->device_id ?? null,
|
||||
// 'trace' => $e->getTraceAsString()
|
||||
// ]);
|
||||
|
||||
// return response()->json([
|
||||
// 'success' => false,
|
||||
// 'message' => 'Failed to process alerts: ' . $e->getMessage()
|
||||
// ], 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ Route::middleware(['customerApiBasicAuth'])->group(function () {
|
||||
Route::post('/fetch/report', [CustomerApiDownloadsController::class, 'fetchReport'])->name('fetch-report');
|
||||
Route::post('/destroy/report', [CustomerApiDownloadsController::class, 'destroyReport'])->name('destroy-report');
|
||||
|
||||
Route::post('/alert-message', [TimeseriesAlertMessageController::class, 'alertMessage']);
|
||||
Route::get('/alert-message/{deviceId}', [TimeseriesAlertMessageController::class, 'alertMessage']);
|
||||
Route::get('/user-assets', [UserAssetLinkController::class, 'index']);
|
||||
Route::post('/store/report', [CustomerApiDownloadsController::class, 'storePdfData'])->name('store-report');
|
||||
Route::post('/fetch/report', [CustomerApiDownloadsController::class, 'fetchReport'])->name('fetch-report');
|
||||
|
||||
Reference in New Issue
Block a user