Guest User

Untitled

a guest
Dec 14th, 2017
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. -- id_geocerca | id_unidad | cuando_alarma | hi | hf | dom | lun | mar | mie | jue | vie | sab | alarma
  2. -- -------------+-----------+---------------+----------+----------+-----+-----+-----+-----+-----+-----+-----+--------
  3. -- 3 | 1991 | 2 | 11:00:00 | 04:00:00 | t | t | t | t | t | t | t | t
  4. -- 5 | 1991 | 2 | 11:00:00 | 04:00:00 | t | t | t | t | t | t | t | t
  5. -- 6 | 1991 | 2 | 11:00:00 | 04:00:00 | t | t | t | t | t | t | t | t
  6. -- 7 | 1991 | 2 | 11:00:00 | 04:00:00 | t | t | t | t | t | t | t | t
  7.  
  8. -- importante declaran las variables luego de un select
  9. DECLARE
  10. new_id_geocerca INTEGER;
  11. hora_actua TIME WITHOUT TIME ZONE;
  12. dia INTEGER DEFAULT 0;
  13. contador_geo INTEGER DEFAULT 0;
  14. -- alertar BOOLEAN DEFAULT FALSE;
  15. BEGIN
  16. FOR new_id_geocerca IN SELECT id_geocerca FROM geounid WHERE id_unidad = NEW.id_unidad LOOP
  17.  
  18. SELECT id_geocerca , id_unidad , cuando_alarma , hi , hf , dom , lun , mar , mie , jue , vie , sab , alarma
  19. FROM geounid WHERE id_unidad = NEW.id_unidad and id_geocerca = new_id_geocerca;
  20.  
  21. dia := (SELECT EXTRACT(DOW FROM NEW.fecha_hora_gps));
  22. /* esta dentro del dia*/
  23. IF ( (dia = 0 AND dom = TRUE) OR (dia = 1 AND lun = TRUE) OR (dia = 2 AND mar = TRUE) OR
  24.  
  25. (dia = 3 AND mie = TRUE) OR (dia = 4 AND jue = TRUE) OR (dia = 5 AND vie = TRUE) OR
  26.  
  27. (dia = 6 AND sab = TRUE) ) THEN
  28.  
  29. hora_actual := SELECT TO_TIMESTAMP((SELECT to_char(FROM NEW.fecha_hora_gps, 'HH24:MI:SS')), 'HH24:MI:SS')::TIME;
  30. /* esta dentro de la hora*/
  31.  
  32. SELECT tipo_geocerca FROM geocercas WHERE id_geocerca = new_id_geocerca;
  33.  
  34. -- si es circulo
  35. IF ( tipo_geocerca == 1) THEN
  36. -- si es poligono
  37. ELSIF( tipo_geocerca == 2) THEN
  38. -- cuando salga de la geocerca
  39. IF ( cuando_alarma == 1) THEN
  40.  
  41. IF ( hora_actual >= hi AND hora_actual <= hf ) THEN
  42.  
  43. SELECT count(id_geocerca) INTO contador_geo FROM geocercas
  44. WHERE ST_Contains(polygon, ST_GeomFromText(NEW.geo_posicion))
  45. and id_geocerca = new_id_geocerca;
  46.  
  47. IF (contador_geo < 1) THEN
  48. -- Generar alarma o notificacion segun sea el caso
  49. ELSE
  50. RAISE EXCEPTION 'El vehiculo esta dentro de la geocerca';
  51. END IF;
  52.  
  53. ELSE
  54. RAISE EXCEPTION 'No esta dentro de la hora limite de la geocerca';
  55. END IF;
  56. -- cuando entre a la geocerca
  57. ELSIF( cuando_alarma == 2) THEN
  58. IF ( hora_actual >= hi AND hora_actual <= hf ) THEN
  59.  
  60. SELECT count(id_geocerca) INTO contador_geo FROM geocercas
  61. WHERE ST_Contains(polygon, ST_GeomFromText(NEW.geo_posicion))
  62. and id_geocerca = new_id_geocerca;
  63.  
  64. IF (contador_geo >= 1) THEN
  65. -- Generar alarma o notificacion segun sea el caso
  66. ELSE
  67. RAISE EXCEPTION 'El vehiculo esta fuera de la geocerca';
  68. END IF;
  69.  
  70. ELSE
  71. RAISE EXCEPTION 'No esta dentro de la hora limite de la geocerca';
  72. END IF;
  73. -- cuando salga de la geocerca y fuera del horario
  74. ELSIF( cuando_alarma == 3) THEN
  75.  
  76. SELECT count(id_geocerca) INTO contador_geo FROM geocercas
  77. WHERE ST_Contains(polygon, ST_GeomFromText(NEW.geo_posicion))
  78. and id_geocerca = new_id_geocerca;
  79.  
  80. IF (contador_geo < 1) THEN
  81. -- Generar alarma o notificacion segun sea el caso
  82. ELSE
  83. RAISE EXCEPTION 'El vehiculo esta dentro de la geocerca';
  84. END IF;
  85.  
  86. END IF;
  87.  
  88. END IF;
  89.  
  90.  
  91. ELSE
  92.  
  93. RAISE EXCEPTION 'No esta dentro del dia limite de la geocerca';
  94.  
  95. END IF;
  96.  
  97. END LOOP;
  98. RETURN NEW;
  99. END
Add Comment
Please, Sign In to add comment