@openmdm/plugin-geofence 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present OpenMDM Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,199 @@
1
+ import { Device, DeviceLocation, MDMPlugin } from '@openmdm/core';
2
+ export { MDMPlugin } from '@openmdm/core';
3
+
4
+ /**
5
+ * OpenMDM Geofencing Plugin
6
+ *
7
+ * Provides location-based policy enforcement and monitoring.
8
+ * Supports circular and polygon geofence zones with enter/exit actions.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { createMDM } from '@openmdm/core';
13
+ * import { geofencePlugin } from '@openmdm/plugin-geofence';
14
+ *
15
+ * const mdm = createMDM({
16
+ * database: drizzleAdapter(db),
17
+ * plugins: [
18
+ * geofencePlugin({
19
+ * onEnter: async (device, zone) => {
20
+ * console.log(`Device ${device.id} entered ${zone.name}`);
21
+ * },
22
+ * onExit: async (device, zone) => {
23
+ * console.log(`Device ${device.id} left ${zone.name}`);
24
+ * },
25
+ * }),
26
+ * ],
27
+ * });
28
+ * ```
29
+ */
30
+
31
+ interface GeofencePluginOptions {
32
+ /**
33
+ * Callback when device enters a geofence zone
34
+ */
35
+ onEnter?: (device: Device, zone: GeofenceZone) => Promise<void>;
36
+ /**
37
+ * Callback when device exits a geofence zone
38
+ */
39
+ onExit?: (device: Device, zone: GeofenceZone) => Promise<void>;
40
+ /**
41
+ * Callback when device is inside a zone during heartbeat
42
+ */
43
+ onInside?: (device: Device, zone: GeofenceZone) => Promise<void>;
44
+ /**
45
+ * Default dwell time (ms) before triggering enter event (default: 0)
46
+ */
47
+ defaultDwellTime?: number;
48
+ /**
49
+ * Enable location history tracking (default: false)
50
+ */
51
+ trackHistory?: boolean;
52
+ /**
53
+ * Maximum history entries per device (default: 1000)
54
+ */
55
+ maxHistoryEntries?: number;
56
+ }
57
+ interface GeofenceZone {
58
+ id: string;
59
+ name: string;
60
+ description?: string;
61
+ type: 'circle' | 'polygon';
62
+ enabled: boolean;
63
+ center?: {
64
+ latitude: number;
65
+ longitude: number;
66
+ };
67
+ radius?: number;
68
+ vertices?: Array<{
69
+ latitude: number;
70
+ longitude: number;
71
+ }>;
72
+ onEnter?: GeofenceAction;
73
+ onExit?: GeofenceAction;
74
+ policyOverride?: string;
75
+ schedule?: GeofenceSchedule;
76
+ metadata?: Record<string, unknown>;
77
+ createdAt: Date;
78
+ updatedAt: Date;
79
+ }
80
+ interface GeofenceAction {
81
+ /** Action type */
82
+ type: 'notify' | 'command' | 'policy' | 'webhook' | 'none';
83
+ /** Notification message */
84
+ notification?: {
85
+ title: string;
86
+ body: string;
87
+ };
88
+ /** Command to execute */
89
+ command?: {
90
+ type: string;
91
+ payload?: Record<string, unknown>;
92
+ };
93
+ /** Policy to apply */
94
+ policyId?: string;
95
+ /** Webhook to call */
96
+ webhook?: {
97
+ url: string;
98
+ method?: 'GET' | 'POST';
99
+ headers?: Record<string, string>;
100
+ body?: Record<string, unknown>;
101
+ };
102
+ }
103
+ interface GeofenceSchedule {
104
+ /** Days of week (0=Sunday, 6=Saturday) */
105
+ daysOfWeek?: number[];
106
+ /** Start time (HH:mm) */
107
+ startTime?: string;
108
+ /** End time (HH:mm) */
109
+ endTime?: string;
110
+ /** Timezone (default: UTC) */
111
+ timezone?: string;
112
+ }
113
+ interface DeviceZoneState {
114
+ deviceId: string;
115
+ zoneId: string;
116
+ inside: boolean;
117
+ enteredAt?: Date;
118
+ exitedAt?: Date;
119
+ dwellTime?: number;
120
+ }
121
+ interface LocationHistoryEntry {
122
+ deviceId: string;
123
+ location: DeviceLocation;
124
+ zones: string[];
125
+ timestamp: Date;
126
+ }
127
+ interface CreateGeofenceZoneInput {
128
+ name: string;
129
+ description?: string;
130
+ type: 'circle' | 'polygon';
131
+ enabled?: boolean;
132
+ center?: {
133
+ latitude: number;
134
+ longitude: number;
135
+ };
136
+ radius?: number;
137
+ vertices?: Array<{
138
+ latitude: number;
139
+ longitude: number;
140
+ }>;
141
+ onEnter?: GeofenceAction;
142
+ onExit?: GeofenceAction;
143
+ policyOverride?: string;
144
+ schedule?: GeofenceSchedule;
145
+ metadata?: Record<string, unknown>;
146
+ }
147
+ interface UpdateGeofenceZoneInput {
148
+ name?: string;
149
+ description?: string;
150
+ enabled?: boolean;
151
+ center?: {
152
+ latitude: number;
153
+ longitude: number;
154
+ };
155
+ radius?: number;
156
+ vertices?: Array<{
157
+ latitude: number;
158
+ longitude: number;
159
+ }>;
160
+ onEnter?: GeofenceAction;
161
+ onExit?: GeofenceAction;
162
+ policyOverride?: string;
163
+ schedule?: GeofenceSchedule;
164
+ metadata?: Record<string, unknown>;
165
+ }
166
+ /**
167
+ * Calculate distance between two points using Haversine formula
168
+ */
169
+ declare function haversineDistance(lat1: number, lon1: number, lat2: number, lon2: number): number;
170
+ /**
171
+ * Check if point is inside circular zone
172
+ */
173
+ declare function isInsideCircle(point: {
174
+ latitude: number;
175
+ longitude: number;
176
+ }, center: {
177
+ latitude: number;
178
+ longitude: number;
179
+ }, radiusMeters: number): boolean;
180
+ /**
181
+ * Check if point is inside polygon using ray casting algorithm
182
+ */
183
+ declare function isInsidePolygon(point: {
184
+ latitude: number;
185
+ longitude: number;
186
+ }, vertices: Array<{
187
+ latitude: number;
188
+ longitude: number;
189
+ }>): boolean;
190
+ /**
191
+ * Check if zone is active according to schedule
192
+ */
193
+ declare function isZoneScheduleActive(schedule: GeofenceSchedule | undefined): boolean;
194
+ /**
195
+ * Create geofencing plugin
196
+ */
197
+ declare function geofencePlugin(options?: GeofencePluginOptions): MDMPlugin;
198
+
199
+ export { type CreateGeofenceZoneInput, type DeviceZoneState, type GeofenceAction, type GeofencePluginOptions, type GeofenceSchedule, type GeofenceZone, type LocationHistoryEntry, type UpdateGeofenceZoneInput, geofencePlugin, haversineDistance, isInsideCircle, isInsidePolygon, isZoneScheduleActive };