|
|
|
|
@ -38,7 +38,9 @@ class HMG_Geofence:NSObject{
|
|
|
|
|
|
|
|
|
|
private static var shared_:HMG_Geofence?
|
|
|
|
|
class func shared() -> HMG_Geofence{
|
|
|
|
|
assert(shared_ != nil, "HMG_Geofence is not initialized, call initGeofencing() function first.")
|
|
|
|
|
if HMG_Geofence.shared_ == nil{
|
|
|
|
|
HMG_Geofence.initGeofencing()
|
|
|
|
|
}
|
|
|
|
|
return shared_!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -48,28 +50,33 @@ class HMG_Geofence:NSObject{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func register(geoZones:[GeoZoneModel]){
|
|
|
|
|
self.geoZones = geoZones
|
|
|
|
|
|
|
|
|
|
// self.geoZones?.forEach({ (zone) in
|
|
|
|
|
// startMonitoring(zone: zone)
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
let z = geoZones[14]
|
|
|
|
|
if monitoredRegions().filter { $0.identifier == z.identifier()}.isEmpty{
|
|
|
|
|
startMonitoring(zone: z)
|
|
|
|
|
}
|
|
|
|
|
self.geoZones = geoZones
|
|
|
|
|
|
|
|
|
|
let monitoredRegions_ = monitoredRegions()
|
|
|
|
|
self.geoZones?.forEach({ (zone) in
|
|
|
|
|
if let region = zone.toRegion(locationManager: locationManager){
|
|
|
|
|
if let already = monitoredRegions_.first(where: {$0.identifier == zone.identifier()}){
|
|
|
|
|
debugPrint("Already monitering region: \(already)")
|
|
|
|
|
}else{
|
|
|
|
|
startMonitoring(region: region)
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
debugPrint("Invalid region: \(zone.latitude ?? "invalid_latitude"),\(zone.longitude ?? "invalid_longitude"),r\(zone.radius ?? 0) | \(zone.identifier())")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func monitoredRegions() -> Set<CLRegion>{
|
|
|
|
|
return locationManager.monitoredRegions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CLLocationManager Delegates
|
|
|
|
|
extension HMG_Geofence : CLLocationManagerDelegate{
|
|
|
|
|
|
|
|
|
|
func startMonitoring(zone: GeoZoneModel) {
|
|
|
|
|
func startMonitoring(region: CLCircularRegion) {
|
|
|
|
|
if !CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -82,13 +89,9 @@ extension HMG_Geofence : CLLocationManagerDelegate{
|
|
|
|
|
debugPrint(message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let fenceRegion = region(with: zone){
|
|
|
|
|
locationManager.startMonitoring(for: fenceRegion)
|
|
|
|
|
locationManager.requestState(for: fenceRegion)
|
|
|
|
|
debugPrint("Monitering region: \(fenceRegion.center) | \(fenceRegion.identifier)")
|
|
|
|
|
}else{
|
|
|
|
|
debugPrint("Invalid region: \(zone.latitude ?? ""),\(zone.longitude ?? ""),r\(zone.radius ?? 0) | \(zone.identifier())")
|
|
|
|
|
}
|
|
|
|
|
locationManager.startMonitoring(for: region)
|
|
|
|
|
locationManager.requestState(for: region)
|
|
|
|
|
debugPrint("Starts monitering region: \(region)")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
|
|
|
|
|
@ -120,70 +123,58 @@ extension HMG_Geofence : CLLocationManagerDelegate{
|
|
|
|
|
extension HMG_Geofence{
|
|
|
|
|
|
|
|
|
|
func handleEvent(for region: CLRegion!, transition:Transition, location:CLLocation?) {
|
|
|
|
|
if let zone = geoZone(by: region.identifier){
|
|
|
|
|
notifyUser(forZone: zone, transition: transition, location: locationManager.location)
|
|
|
|
|
notifyServer(forZone: zone, transition: transition, location: locationManager.location)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func region(with geoZone: GeoZoneModel) -> CLCircularRegion? {
|
|
|
|
|
|
|
|
|
|
if !geoZone.identifier().isEmpty,
|
|
|
|
|
let radius = geoZone.radius, let lat = geoZone.latitude?.removeSpace(), let long = geoZone.longitude?.removeSpace(),
|
|
|
|
|
let radius_d = Double("\(radius)"), let lat_d = Double(lat), let long_d = Double(long){
|
|
|
|
|
|
|
|
|
|
let coordinate = CLLocationCoordinate2D(latitude: lat_d, longitude: long_d)
|
|
|
|
|
let region = CLCircularRegion(center: coordinate, radius: radius_d, identifier: geoZone.identifier())
|
|
|
|
|
|
|
|
|
|
region.notifyOnEntry = true
|
|
|
|
|
region.notifyOnExit = true
|
|
|
|
|
return region
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
notifyUser(forRegion: region, transition: transition, location: locationManager.location)
|
|
|
|
|
notifyServer(forRegion: region, transition: transition, location: locationManager.location)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func geoZone(by id: String) -> GeoZoneModel? {
|
|
|
|
|
return geoZones?.first(where: { $0.identifier() == id})
|
|
|
|
|
var zone:GeoZoneModel? = nil
|
|
|
|
|
if let zones_ = geoZones{
|
|
|
|
|
zone = zones_.first(where: { $0.identifier() == id})
|
|
|
|
|
}else{
|
|
|
|
|
// let jsonArray = UserDefaults.standard.string(forKey: "hmg-geo-fences")
|
|
|
|
|
}
|
|
|
|
|
return zone
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func notifyUser(forZone:GeoZoneModel, transition:Transition, location:CLLocation?){
|
|
|
|
|
if UIApplication.shared.applicationState == .active {
|
|
|
|
|
mainViewController.showAlert(withTitle: transition.name(), message: forZone.identifier())
|
|
|
|
|
} else {
|
|
|
|
|
let notificationContent = UNMutableNotificationContent()
|
|
|
|
|
notificationContent.title = transition.name()
|
|
|
|
|
notificationContent.body = forZone.identifier()
|
|
|
|
|
notificationContent.sound = UNNotificationSound.default
|
|
|
|
|
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
|
|
|
|
|
let request = UNNotificationRequest(identifier: "\(Date().timeIntervalSinceNow)",
|
|
|
|
|
content: notificationContent,
|
|
|
|
|
trigger: trigger)
|
|
|
|
|
UNUserNotificationCenter.current().add(request) { error in
|
|
|
|
|
if let error = error {
|
|
|
|
|
print("Error: \(error)")
|
|
|
|
|
}
|
|
|
|
|
func notifyUser(forRegion:CLRegion, transition:Transition, location:CLLocation?){
|
|
|
|
|
if let zone = geoZone(by: forRegion.identifier){
|
|
|
|
|
if UIApplication.shared.applicationState == .active {
|
|
|
|
|
mainViewController.showAlert(withTitle: transition.name(), message: zone.message())
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func notifyServer(forZone:GeoZoneModel, transition:Transition, location:CLLocation?){
|
|
|
|
|
flutterMethodChannel?.invokeMethod("getLogGeofenceFullUrl", arguments: nil){ fullUrlString in
|
|
|
|
|
if let url = fullUrlString as? String{
|
|
|
|
|
flutterMethodChannel?.invokeMethod("getDefaultHttpParameters", arguments: nil){ params in
|
|
|
|
|
if let body = params as? [String : Any]{
|
|
|
|
|
httpPostRequest(urlString: url, jsonBody: body){ (status,json) in
|
|
|
|
|
if let json_ = json , status{
|
|
|
|
|
}else{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func notifyServer(forRegion:CLRegion, transition:Transition, location:CLLocation?){
|
|
|
|
|
showNotification(title: "Notifying server..." , subtitle: forRegion.identifier, message: "")
|
|
|
|
|
|
|
|
|
|
if let idString = forRegion.identifier.split(separator: "_").first, let idInt = Int(idString){
|
|
|
|
|
let body:[String:Any] = ["PointsID":idInt,"GeoType":transition.rawValue,"PatientID":"1231755"]
|
|
|
|
|
let url = "https://hmgwebservices.com/Services/Patients.svc/REST/GeoF_InsertPatientFileInfo"
|
|
|
|
|
httpPostRequest(urlString: url, jsonBody: body){ (status,json) in
|
|
|
|
|
showNotification(title: transition.name(), subtitle: forRegion.identifier, message: status ? "Success: notified to server ✔️" : "Failed to notified server ✖️")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// func notifyServer(forRegion:GeoZoneModel, transition:Transition, location:CLLocation?){
|
|
|
|
|
// flutterMethodChannel?.invokeMethod("getLogGeofenceFullUrl", arguments: nil){ fullUrlString in
|
|
|
|
|
// if let url = fullUrlString as? String{
|
|
|
|
|
// flutterMethodChannel?.invokeMethod("getDefaultHttpParameters", arguments: nil){ params in
|
|
|
|
|
// if var body = params as? [String : Any]{
|
|
|
|
|
// body.updateValue(forZone.geofenceId, forKey: "PointsID")
|
|
|
|
|
// body.updateValue(transition.rawValue, forKey: "GeoType")
|
|
|
|
|
// httpPostRequest(urlString: url, jsonBody: body){ (status,json) in
|
|
|
|
|
// showNotification(title: transition.name(), subtitle: forZone.identifier(), message: status ? "Success: sent to server ✔️" : "Failed: sent to server ✖️")
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|