PRTGController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Validator;
  5. use Illuminate\Support\Facades\DB;
  6. use ElephantIO\Client;
  7. class PRTGController extends Controller{
  8. private $responseController;
  9. private $functionsController;
  10. private $encryptionController;
  11. private $notificationsController;
  12. private $socketClient;
  13. private $monitoredItems;
  14. public function __construct(){
  15. $this->responseController = new ResponseController();
  16. $this->functionsController = new FunctionsController();
  17. $this->encryptionController = new EncryptionController();
  18. $this->notificationsController = new NotificationsController();
  19. $url = 'http://localhost:3200';
  20. $this->socketClient = new Client(Client::engine(Client::CLIENT_4X, $url));
  21. $this->socketClient->initialize();
  22. $this->socketClient->of('/');
  23. $this->monitoredItems = ['R650', 'R450'];
  24. }
  25. public function registerData(Request $request) {
  26. $validator = Validator::make($request->all(), [
  27. 'data' => 'required|string'
  28. ]);
  29. if($validator->fails()){
  30. return $this->responseController->makeResponse(
  31. true,
  32. "Se encontraron uno o más errores.",
  33. $this->responseController->makeErrors(
  34. $validator->errors()->messages()
  35. ),
  36. 401
  37. );
  38. }
  39. $form = $request->all();
  40. $now = $this->functionsController->now();
  41. $nowStr = $now->toDateTimeString();
  42. $dataArr = json_decode($form['data'], true);
  43. foreach($dataArr as $item){
  44. if(array_key_exists('path', $item)){
  45. $routeStr = '';
  46. foreach($item['path'] as $item0){
  47. $routeStr .= "/$item0[name]";
  48. }
  49. foreach($this->monitoredItems as $item0){
  50. if(str_contains($routeStr, $item0)){
  51. if(is_null($item['last_measurement']['display_minimum']) || is_null($item['last_measurement']['display_maximum'])){
  52. $this->notificationsController->emitNotification(
  53. 'S002V01M01ADSI',
  54. "El canal $item[channel][id] no ha recibido información",
  55. "No se ha podido obtener información canal $item[channel][id] - $item[channel][name].",
  56. [[
  57. 'BOTON' => 'Ver detalles',
  58. 'FUNCION' => 'openCorrectiveWorkOrderDetails',
  59. 'PARAMETROS' => json_encode([])
  60. ], [
  61. 'BOTON' => 'Validar orden',
  62. 'FUNCION' => 'validateCorrectiveWorkOrder',
  63. 'PARAMETROS' => json_encode([])
  64. ], [
  65. 'BOTON' => 'Ir al módulo',
  66. 'FUNCION' => 'openModule',
  67. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMCO/ORTR/GEOP')])
  68. ]],
  69. ['0000000000'],
  70. '0000000000',
  71. $form['linea'],
  72. $this->socketClient,
  73. '',
  74. ''
  75. );
  76. }else{
  77. $minVal = floatval($item['last_measurement']['display_minimum']);
  78. $maxVal = floatval($item['last_measurement']['display_maximum']);
  79. $value = floatval($item['last_measurement']['display_value']);
  80. if($value < $minVal){
  81. $this->notificationsController->emitNotification(
  82. 'S002V01M01ADSI',
  83. "Valor mínimo excedido en el canal $item[channel][id]",
  84. "La medida del valor mínimo del canal $item[channel][id] - $item[channel][name] fue excedido.",
  85. [[
  86. 'BOTON' => 'Ver detalles',
  87. 'FUNCION' => 'openCorrectiveWorkOrderDetails',
  88. 'PARAMETROS' => json_encode([])
  89. ], [
  90. 'BOTON' => 'Validar orden',
  91. 'FUNCION' => 'validateCorrectiveWorkOrder',
  92. 'PARAMETROS' => json_encode([])
  93. ], [
  94. 'BOTON' => 'Ir al módulo',
  95. 'FUNCION' => 'openModule',
  96. 'PARAMETROS' => json_encode([$this->encryptionController->encrypt('GMCO/ORTR/GEOP')])
  97. ]],
  98. ['0000000000'],
  99. '0000000000',
  100. $form['linea'],
  101. $this->socketClient,
  102. '',
  103. ''
  104. );
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. $idReport = DB::table('S002V01TPRTG')->insertGetId([
  112. 'PRTG_NULI' => 1,
  113. 'PRTG_FERE' => $nowStr,
  114. 'PRTG_DATO' => $form['data']
  115. ]);
  116. $idReportEnc = $this->encryptionController->encrypt($idReport);
  117. $this->socketClient->emit('new_prtg_report', ['report' => $idReportEnc]);
  118. return $this->responseController->makeResponse(false, 'EXITO');
  119. }
  120. public function getReport($idReport, $idUser, $line) {
  121. DB::enableQueryLog();
  122. $idUser = $this->encryptionController->decrypt($idUser);
  123. if(!$idUser){
  124. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  125. }
  126. $usr = DB::table('S002V01TUSUA')->where([
  127. ['USUA_NULI', '=', $line],
  128. ['USUA_IDUS', '=', $idUser],
  129. ])->first();
  130. if(is_null($usr)){
  131. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  132. }
  133. $idReport = $this->encryptionController->decrypt($idReport);
  134. if(!$idReport){
  135. return $this->responseController->makeResponse(true, 'El ID del reporte solicitado no está encriptado correctamente', [], 400);
  136. }
  137. $report = DB::table('S002V01TPRTG')->select([
  138. 'PRTG_IDRE AS ID_REPORTE',
  139. 'PRTG_FERE AS FECHA_REGISTRO',
  140. 'PRTG_DATO AS DATOS'
  141. ])->where([
  142. ['PRTG_NULI', '=', $line],
  143. ['PRTG_IDRE', '=', $idReport]
  144. ])->first();
  145. if(is_null($report)){
  146. return $this->responseController->makeResponse(true, 'El reporte consultado no está registrado', [], 404);
  147. }
  148. $report->ID_REPORTE = $this->encryptionController->encrypt($report->ID_REPORTE);
  149. $report->DATOS = $this->encryptionController->encrypt($report->DATOS);
  150. return $this->responseController->makeResponse(false, 'EXITO', $report);
  151. }
  152. public function getLastReports($idUser, $line) {
  153. DB::enableQueryLog();
  154. $idUser = $this->encryptionController->decrypt($idUser);
  155. if(!$idUser){
  156. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  157. }
  158. $usr = DB::table('S002V01TUSUA')->where([
  159. ['USUA_NULI', '=', $line],
  160. ['USUA_IDUS', '=', $idUser],
  161. ])->first();
  162. if(is_null($usr)){
  163. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  164. }
  165. $lastReports = DB::table('S002V01TPRTG')->select([
  166. 'PRTG_IDRE AS ID_REPORTE',
  167. 'PRTG_FERE AS FECHA_REGISTRO',
  168. 'PRTG_DATO AS DATOS'
  169. ])->orderBy('PRTG_FERE', 'desc')->limit(5)->get()->all();
  170. foreach($lastReports as $key=>$report){
  171. $report->ID_REPORTE = $this->encryptionController->encrypt($report->ID_REPORTE);
  172. $report->DATOS = $this->encryptionController->encrypt($report->DATOS);
  173. $lastReports[$key] = $report;
  174. }
  175. return $this->responseController->makeResponse(false, 'EXITO', $lastReports);
  176. }
  177. }