geo-engine-node 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +93 -0
- package/package.json +9 -0
package/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { create } from 'axios';
|
|
2
|
+
|
|
3
|
+
const DEFAULTS = {
|
|
4
|
+
managementUrl: 'https://api.geoengine.dev',
|
|
5
|
+
ingestUrl: 'http://ingest.geoengine.dev',
|
|
6
|
+
timeout: 10000
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
class GeoEngine {
|
|
10
|
+
/**
|
|
11
|
+
* Inicializa el cliente de Geo-Engine
|
|
12
|
+
* @param {string} apiKey - Tu API Key (sk_...)
|
|
13
|
+
* @param {Object} options - Configuración opcional { managementUrl, ingestUrl }
|
|
14
|
+
*/
|
|
15
|
+
constructor(apiKey, options = {}) {
|
|
16
|
+
if (!apiKey) throw new Error('GeoEngine: API Key es requerida');
|
|
17
|
+
|
|
18
|
+
this.apiKey = apiKey;
|
|
19
|
+
this.config = { ...DEFAULTS, ...options };
|
|
20
|
+
|
|
21
|
+
this.client = create({
|
|
22
|
+
timeout: this.config.timeout,
|
|
23
|
+
headers: {
|
|
24
|
+
'Content-Type': 'application/json',
|
|
25
|
+
'X-API-Key': this.apiKey,
|
|
26
|
+
'User-Agent': 'GeoEngineNode/1.0.0'
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Envía una ubicación al motor de ingestión
|
|
33
|
+
* @param {string} deviceId - ID del dispositivo (ej: 'camion-01')
|
|
34
|
+
* @param {number} lat - Latitud
|
|
35
|
+
* @param {number} lng - Longitud
|
|
36
|
+
*/
|
|
37
|
+
async sendLocation(deviceId, lat, lng) {
|
|
38
|
+
try {
|
|
39
|
+
const payload = {
|
|
40
|
+
device_id: deviceId,
|
|
41
|
+
latitude: lat,
|
|
42
|
+
longitude: lng,
|
|
43
|
+
timestamp: Math.floor(Date.now() / 1000)
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const res = await this.client.post(`${this.config.ingestUrl}/ingest`, payload);
|
|
47
|
+
return res.data;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
this._handleError(error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Crea una nueva geocerca
|
|
55
|
+
* @param {string} name - Nombre de la zona
|
|
56
|
+
* @param {Array<Array<number>>} coordinates - Array de puntos [[lat, lng], ...]
|
|
57
|
+
* @param {string} webhookUrl - URL para recibir alertas
|
|
58
|
+
*/
|
|
59
|
+
async createGeofence(name, coordinates, webhookUrl) {
|
|
60
|
+
try {
|
|
61
|
+
// Convertir formato simple [[lat,lng]] a GeoJSON
|
|
62
|
+
// GeoJSON usa [Lng, Lat], invertimos el orden si viene como [Lat, Lng]
|
|
63
|
+
const polygon = coordinates.map(p => [p[1], p[0]]);
|
|
64
|
+
// Cerrar polígono si es necesario
|
|
65
|
+
if (polygon[0][0] !== polygon[polygon.length-1][0]) {
|
|
66
|
+
polygon.push(polygon[0]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const payload = {
|
|
70
|
+
name,
|
|
71
|
+
webhook_url: webhookUrl,
|
|
72
|
+
geojson: {
|
|
73
|
+
type: 'Polygon',
|
|
74
|
+
coordinates: [polygon]
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const res = await this.client.post(`${this.config.managementUrl}/geofences`, payload);
|
|
79
|
+
return res.data;
|
|
80
|
+
} catch (error) {
|
|
81
|
+
this._handleError(error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
_handleError(error) {
|
|
86
|
+
if (error.response) {
|
|
87
|
+
throw new Error(`GeoEngine API Error: ${error.response.status} - ${JSON.stringify(error.response.data)}`);
|
|
88
|
+
}
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export default GeoEngine;
|