|
|
|
|
@ -1,17 +1,24 @@
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:core';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:isolate';
|
|
|
|
|
import 'dart:math';
|
|
|
|
|
import 'dart:ui';
|
|
|
|
|
|
|
|
|
|
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/core/model/geofencing/requests/GeoZonesRequestModel.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/core/model/geofencing/requests/LogGeoZoneRequestModel.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/core/model/geofencing/responses/GeoZonesResponseModel.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/core/service/geofencing/GeofencingServices.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/locator.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/uitl/LocalNotification.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:geofencing/geofencing.dart';
|
|
|
|
|
|
|
|
|
|
class HMG_Geofence {
|
|
|
|
|
class HMG_Geofencing {
|
|
|
|
|
var _testTrigger = false;
|
|
|
|
|
static var _isolatePortName = "hmg_geofencing_send_port";
|
|
|
|
|
|
|
|
|
|
List<GeoZonesResponseModel> _zones;
|
|
|
|
|
@ -21,19 +28,21 @@ class HMG_Geofence {
|
|
|
|
|
|
|
|
|
|
final BuildContext context;
|
|
|
|
|
final List<GeofenceEvent> triggers = List();
|
|
|
|
|
ReceivePort port = ReceivePort();
|
|
|
|
|
|
|
|
|
|
HMG_Geofence(this.context) {
|
|
|
|
|
HMG_Geofencing(this.context) {
|
|
|
|
|
triggers.add(GeofenceEvent.enter);
|
|
|
|
|
triggers.add(GeofenceEvent.exit);
|
|
|
|
|
// triggers.add(GeofenceEvent.dwell);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initiate(List<GeoZonesResponseModel> zones) async {
|
|
|
|
|
_zones = zones;
|
|
|
|
|
Future<HMG_Geofencing> loadZones() async{
|
|
|
|
|
_zones = await locator<GeofencingServices>().getAllGeoZones(GeoZonesRequestModel());
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init() async {
|
|
|
|
|
// debug check (Testing Geo Zones)
|
|
|
|
|
if (kDebugMode) {
|
|
|
|
|
// debug check
|
|
|
|
|
addTestingGeofences();
|
|
|
|
|
}
|
|
|
|
|
_saveZones();
|
|
|
|
|
@ -42,7 +51,13 @@ class HMG_Geofence {
|
|
|
|
|
_registerIsolatePort();
|
|
|
|
|
_registerGeofences().then((value) {
|
|
|
|
|
debugPrint(value.toString());
|
|
|
|
|
if(_testTrigger) {
|
|
|
|
|
var events = [GeofenceEvent.enter,GeofenceEvent.exit];
|
|
|
|
|
events.shuffle();
|
|
|
|
|
transitionTrigger(value, null, events.first);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _saveZones() {
|
|
|
|
|
@ -91,59 +106,84 @@ class HMG_Geofence {
|
|
|
|
|
_zones.add(GeoZonesResponseModel.get("24.7087913,46.6656461", 150, "csO"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void transitionTrigger(List<String> id, Location location, GeofenceEvent event) async {
|
|
|
|
|
id.forEach((element) {
|
|
|
|
|
LocalNotification.showNow(
|
|
|
|
|
title: "GeofenceEvent: ${event.toString()}",
|
|
|
|
|
subtitle: element,
|
|
|
|
|
onClickNotification: (payload) {
|
|
|
|
|
debugPrint(payload);
|
|
|
|
|
},
|
|
|
|
|
payload: element);
|
|
|
|
|
static void transitionTrigger(List<String> id, Location location, GeofenceEvent event) {
|
|
|
|
|
var dataToSend = id.map((element) => {"event": event, "geofence_id": element}).toList() ?? [];
|
|
|
|
|
final SendPort send = IsolateNameServer.lookupPortByName(_isolatePortName);
|
|
|
|
|
send?.send(dataToSend);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReceivePort _port = ReceivePort();
|
|
|
|
|
void _registerIsolatePort() async{
|
|
|
|
|
IsolateNameServer.registerPortWithName(_port.sendPort, _isolatePortName);
|
|
|
|
|
_port.listen((dynamic data) {
|
|
|
|
|
|
|
|
|
|
Future result = AppSharedPreferences().getStringWithDefaultValue(HMG_GEOFENCES,"[]");
|
|
|
|
|
result.then((jsonString){
|
|
|
|
|
|
|
|
|
|
List jsonList = json.decode(jsonString) ?? [];
|
|
|
|
|
List<GeoZonesResponseModel> geoList = jsonList.map((e) => GeoZonesResponseModel().fromJson(e)).toList() ?? [];
|
|
|
|
|
|
|
|
|
|
(data as List).forEach((element) async {
|
|
|
|
|
GeofenceEvent geofenceEvent = element["event"];
|
|
|
|
|
String geofence_id = element["geofence_id"];
|
|
|
|
|
|
|
|
|
|
GeoZonesResponseModel geoZone = _findByGeofenceFrom(geoList, by: geofence_id);
|
|
|
|
|
if(geoZone != null) {
|
|
|
|
|
LocalNotification.getInstance().showNow(
|
|
|
|
|
title: "GeofenceEvent: ${_nameOf(geofenceEvent)}",
|
|
|
|
|
subtitle: geoZone.description,
|
|
|
|
|
payload: json.encode(geoZone.toJson()));
|
|
|
|
|
|
|
|
|
|
_logGeoZoneToServer(zoneId: geoZone.geofId, transition: _idOf(geofenceEvent));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 700));
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
// id.forEach((element) {
|
|
|
|
|
// final SendPort send = IsolateNameServer.lookupPortByName(_isolatePortName);
|
|
|
|
|
// send?.send({"event": event, "geofence_id": element});
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _registerIsolatePort() {
|
|
|
|
|
IsolateNameServer.registerPortWithName(port.sendPort, _isolatePortName);
|
|
|
|
|
AppSharedPreferences pref = AppSharedPreferences();
|
|
|
|
|
var jsonString = pref.getString(HMG_GEOFENCES);
|
|
|
|
|
List jsonList = json.decode(jsonString);
|
|
|
|
|
List<GeoZonesResponseModel> geoList = jsonList.map((e) => GeoZonesResponseModel().fromJson(e));
|
|
|
|
|
|
|
|
|
|
port.listen((dynamic data) {
|
|
|
|
|
GeofenceEvent event = data["event"];
|
|
|
|
|
String geofence_id = data["geofence_id"];
|
|
|
|
|
|
|
|
|
|
GeoZonesResponseModel geofence = _findByGeofenceIdFrom(geoList, geofence_id);
|
|
|
|
|
|
|
|
|
|
LocalNotification.showNow(
|
|
|
|
|
title: "GeofenceEvent: ${_nameOf(event)}",
|
|
|
|
|
subtitle: geofence.description,
|
|
|
|
|
onClickNotification: (payload) {
|
|
|
|
|
debugPrint(payload);
|
|
|
|
|
},
|
|
|
|
|
payload: json.encode(geofence.toJson()));
|
|
|
|
|
_logGeoZoneToServer({int zoneId, int transition}){
|
|
|
|
|
locator<GeofencingServices>()
|
|
|
|
|
.logGeoZone(LogGeoZoneRequestModel(GeoType: transition, PointsID: zoneId ?? 1))
|
|
|
|
|
.then((response){
|
|
|
|
|
|
|
|
|
|
}).catchError((error){
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeoZonesResponseModel _findByGeofenceIdFrom(List<GeoZonesResponseModel> list, String geofence_id) {
|
|
|
|
|
var have = list.where((element) => element.geofenceId() == geofence_id).toList().first;
|
|
|
|
|
GeoZonesResponseModel _findByGeofenceFrom(List<GeoZonesResponseModel> list, { String by}) {
|
|
|
|
|
var have = list.where((element) => element.geofenceId() == by).toList().first;
|
|
|
|
|
return have;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String _nameOf(GeofenceEvent event) {
|
|
|
|
|
if (event == GeofenceEvent.exit) {
|
|
|
|
|
return "Exit";
|
|
|
|
|
} else if (event == GeofenceEvent.enter) {
|
|
|
|
|
return "Enter";
|
|
|
|
|
} else if (event == GeofenceEvent.dwell) {
|
|
|
|
|
return "Well";
|
|
|
|
|
} else {
|
|
|
|
|
event.toString();
|
|
|
|
|
switch (event) {
|
|
|
|
|
case GeofenceEvent.enter:
|
|
|
|
|
return "Enter";
|
|
|
|
|
case GeofenceEvent.exit:
|
|
|
|
|
return "Exit";
|
|
|
|
|
case GeofenceEvent.dwell:
|
|
|
|
|
return "dWell";
|
|
|
|
|
default:
|
|
|
|
|
return event.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int _idOf(GeofenceEvent event){
|
|
|
|
|
switch (event) {
|
|
|
|
|
case GeofenceEvent.enter:
|
|
|
|
|
return 1;
|
|
|
|
|
case GeofenceEvent.exit:
|
|
|
|
|
return 2;
|
|
|
|
|
case GeofenceEvent.dwell:
|
|
|
|
|
return 3;
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|