|
|
|
|
@ -129,8 +129,10 @@ extension HMG_Geofence : CLLocationManagerDelegate{
|
|
|
|
|
extension HMG_Geofence{
|
|
|
|
|
|
|
|
|
|
func handleEvent(for region: CLRegion!, transition:Transition, location:CLLocation?) {
|
|
|
|
|
notifyUser(forRegion: region, transition: transition, location: locationManager.location)
|
|
|
|
|
notifyServer(forRegion: region, transition: transition, location: locationManager.location)
|
|
|
|
|
if let userProfile = userProfile(){
|
|
|
|
|
notifyUser(forRegion: region, transition: transition, location: locationManager.location, userProfile: userProfile)
|
|
|
|
|
notifyServer(forRegion: region, transition: transition, location: locationManager.location, userProfile: userProfile)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func geoZone(by id: String) -> GeoZoneModel? {
|
|
|
|
|
@ -144,25 +146,14 @@ extension HMG_Geofence{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 notifyUser(forRegion:CLRegion, transition:Transition, location:CLLocation?, userProfile:[String:Any?]){
|
|
|
|
|
if let patientId = userProfile["PatientID"] as? Int{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func notifyServer(forRegion:CLRegion, transition:Transition, location:CLLocation?){
|
|
|
|
|
df.dateFormat = "MMM/dd/yyyy hh:mm:ss"
|
|
|
|
|
var userInfo = UserDefaults.standard.string(forKey: "flutter.imei-user-data")
|
|
|
|
|
if(userInfo == nil){
|
|
|
|
|
userInfo = UserDefaults.standard.string(forKey: "flutter.user-profile")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let userProfile = userProfile(),
|
|
|
|
|
let patientId = userProfile["PatientID"] as? Int{
|
|
|
|
|
func notifyServer(forRegion:CLRegion, transition:Transition, location:CLLocation?, userProfile:[String:Any?]){
|
|
|
|
|
if let patientId = userProfile["PatientID"] as? Int{
|
|
|
|
|
|
|
|
|
|
if let idString = forRegion.identifier.split(separator: "_").first, let idInt = Int(idString){
|
|
|
|
|
let body:[String:Any] = [
|
|
|
|
|
@ -170,19 +161,18 @@ extension HMG_Geofence{
|
|
|
|
|
"GeoType":transition.rawValue,
|
|
|
|
|
"PatientID":patientId
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
var logs = UserDefaults.init(suiteName: "GeoFenceLog")?.dictionary(forKey: "GEOFENCE_LOGS") ?? [:]
|
|
|
|
|
var geo = (logs[forRegion.identifier] as? [String]) ?? []
|
|
|
|
|
|
|
|
|
|
let url = "https://hmgwebservices.com/Services/Patients.svc/REST/GeoF_InsertPatientFileInfo"
|
|
|
|
|
httpPostRequest(urlString: url, jsonBody: body){ (status,json) in
|
|
|
|
|
let status_ = status ? "Notified" : "Not notified"
|
|
|
|
|
let status_ = status ? "Notified successfully:" : "Failed to notify:"
|
|
|
|
|
showNotification(title: transition.name(), subtitle: forRegion.identifier, message: status_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var logs = UserDefaults.init(suiteName: "GeoFenceLog")?.dictionary(forKey: "GEOFENCE_LOGS") ?? [:]
|
|
|
|
|
if var geo = logs[forRegion.identifier] as? [String]{
|
|
|
|
|
geo.append("\(status_) at \(df.string(from: Date()))")
|
|
|
|
|
}else{
|
|
|
|
|
logs.updateValue(["\(status_) at \(df.string(from: Date()))"], forKey: forRegion.identifier)
|
|
|
|
|
}
|
|
|
|
|
geo.append("\(status_) \(transition.name()) at \(Date().toString(format: "dd/MMM/yyy hh:mm:ss"))")
|
|
|
|
|
logs.updateValue( geo, forKey: forRegion.identifier)
|
|
|
|
|
|
|
|
|
|
UserDefaults.init(suiteName: "GeoFenceLog")?.set(logs, forKey: "GEOFENCE_LOGS")
|
|
|
|
|
}
|
|
|
|
|
|