| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\Facades\DB;
- use ElephantIO\Client;
- class PRTGController extends Controller{
- private $responseController;
- private $functionsController;
- private $encryptionController;
- private $notificationsController;
- private $socketClient;
- private $monitoredItems;
- public function __construct(){
- $this->responseController = new ResponseController();
- $this->functionsController = new FunctionsController();
- $this->encryptionController = new EncryptionController();
- $this->notificationsController = new NotificationsController();
- $url = 'http://localhost:3200';
- $this->socketClient = new Client(Client::engine(Client::CLIENT_4X, $url));
- $this->socketClient->initialize();
- $this->socketClient->of('/');
-
- $this->monitoredItems = ['R650', 'R450'];
- }
- public function registerData(Request $request) {
- $validator = Validator::make($request->all(), [
- 'data' => 'required|string'
- ]);
-
- if($validator->fails()){
- return $this->responseController->makeResponse(
- true,
- "Se encontraron uno o más errores.",
- $this->responseController->makeErrors(
- $validator->errors()->messages()
- ),
- 401
- );
- }
- $form = $request->all();
- $now = $this->functionsController->now();
- $nowStr = $now->toDateTimeString();
- $dataArr = json_decode($form['data'], true);
- foreach($dataArr as $item){
- if(array_key_exists('path', $item)){
- $routeStr = '';
- foreach($item['path'] as $item0){
- $routeStr .= "/$item0[name]";
- }
- foreach($this->monitoredItems as $item0){
- if(str_contains($routeStr, $item0)){
- if(is_null($item['last_measurement']['display_minimum']) || is_null($item['last_measurement']['display_maximum'])){
- $this->notificationsController->emitNotification(
- 'S002V01M01ADSI',
- "El canal $item[channel][id] no ha recibido información",
- "No se ha podido obtener información canal $item[channel][id] - $item[channel][name].",
- [[
- 'BOTON' => 'Ver detalles',
- 'FUNCION' => 'openCorrectiveWorkOrderDetails',
- 'PARAMETROS' => json_encode([])
- ], [
- 'BOTON' => 'Validar orden',
- 'FUNCION' => 'validateCorrectiveWorkOrder',
- 'PARAMETROS' => json_encode([])
- ], [
- 'BOTON' => 'Ir al módulo',
- 'FUNCION' => 'openModule',
- 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMCO/ORTR/GEOP')])
- ]],
- ['0000000000'],
- '0000000000',
- $form['linea'],
- $this->socketClient,
- '',
- ''
- );
- }else{
- $minVal = floatval($item['last_measurement']['display_minimum']);
- $maxVal = floatval($item['last_measurement']['display_maximum']);
- $value = floatval($item['last_measurement']['display_value']);
-
- if($value < $minVal){
- $this->notificationsController->emitNotification(
- 'S002V01M01ADSI',
- "Valor mínimo excedido en el canal $item[channel][id]",
- "La medida del valor mínimo del canal $item[channel][id] - $item[channel][name] fue excedido.",
- [[
- 'BOTON' => 'Ver detalles',
- 'FUNCION' => 'openCorrectiveWorkOrderDetails',
- 'PARAMETROS' => json_encode([])
- ], [
- 'BOTON' => 'Validar orden',
- 'FUNCION' => 'validateCorrectiveWorkOrder',
- 'PARAMETROS' => json_encode([])
- ], [
- 'BOTON' => 'Ir al módulo',
- 'FUNCION' => 'openModule',
- 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMCO/ORTR/GEOP')])
- ]],
- ['0000000000'],
- '0000000000',
- $form['linea'],
- $this->socketClient,
- '',
- ''
- );
- }
- }
- }
- }
- }
- }
- $idReport = DB::table('S002V01TPRTG')->insertGetId([
- 'PRTG_NULI' => 1,
- 'PRTG_FERE' => $nowStr,
- 'PRTG_DATO' => $form['data']
- ]);
- $idReportEnc = $this->encryptionController->encrypt($idReport);
- $this->socketClient->emit('new_prtg_report', ['report' => $idReportEnc]);
- return $this->responseController->makeResponse(false, 'EXITO');
- }
- public function getReport($idReport, $idUser, $line) {
- DB::enableQueryLog();
- $idUser = $this->encryptionController->decrypt($idUser);
- if(!$idUser){
- return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
- }
- $usr = DB::table('S002V01TUSUA')->where([
- ['USUA_NULI', '=', $line],
- ['USUA_IDUS', '=', $idUser],
- ])->first();
- if(is_null($usr)){
- return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
- }
- $idReport = $this->encryptionController->decrypt($idReport);
- if(!$idReport){
- return $this->responseController->makeResponse(true, 'El ID del reporte solicitado no está encriptado correctamente', [], 400);
- }
- $report = DB::table('S002V01TPRTG')->select([
- 'PRTG_IDRE AS ID_REPORTE',
- 'PRTG_FERE AS FECHA_REGISTRO',
- 'PRTG_DATO AS DATOS'
- ])->where([
- ['PRTG_NULI', '=', $line],
- ['PRTG_IDRE', '=', $idReport]
- ])->first();
- if(is_null($report)){
- return $this->responseController->makeResponse(true, 'El reporte consultado no está registrado', [], 404);
- }
- $report->ID_REPORTE = $this->encryptionController->encrypt($report->ID_REPORTE);
- $report->DATOS = $this->encryptionController->encrypt($report->DATOS);
-
- return $this->responseController->makeResponse(false, 'EXITO', $report);
- }
- public function getLastReports($idUser, $line) {
- DB::enableQueryLog();
- $idUser = $this->encryptionController->decrypt($idUser);
- if(!$idUser){
- return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
- }
- $usr = DB::table('S002V01TUSUA')->where([
- ['USUA_NULI', '=', $line],
- ['USUA_IDUS', '=', $idUser],
- ])->first();
- if(is_null($usr)){
- return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
- }
- $lastReports = DB::table('S002V01TPRTG')->select([
- 'PRTG_IDRE AS ID_REPORTE',
- 'PRTG_FERE AS FECHA_REGISTRO',
- 'PRTG_DATO AS DATOS'
- ])->orderBy('PRTG_FERE', 'desc')->limit(5)->get()->all();
- foreach($lastReports as $key=>$report){
- $report->ID_REPORTE = $this->encryptionController->encrypt($report->ID_REPORTE);
- $report->DATOS = $this->encryptionController->encrypt($report->DATOS);
- $lastReports[$key] = $report;
- }
-
- return $this->responseController->makeResponse(false, 'EXITO', $lastReports);
- }
- }
|