|
@@ -11,112 +11,55 @@ import CoreLocation
|
|
|
import UIKit
|
|
|
import CoreData
|
|
|
|
|
|
-struct Person: Codable {
|
|
|
+struct GPS_POS_LOG: Codable {
|
|
|
var latitude: Double
|
|
|
var longitude: Double
|
|
|
- var test: Int
|
|
|
+ var ts: String
|
|
|
}
|
|
|
|
|
|
-var t:RepeatingTimer = RepeatingTimer(timeInterval: 10)
|
|
|
+func sendToHttpServer(gpsPosLog: [GPS_POS_LOG]) {
|
|
|
|
|
|
-//let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
-//var logData: [NSManagedObject] = []
|
|
|
-
|
|
|
-//let managedContext = appDelegate.persistentContainer.viewContext
|
|
|
-//let entity = NSEntityDescription.entity(forEntityName: "GPS", in: managedContext)!
|
|
|
-
|
|
|
-//let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
|
|
|
-//appDelegate?.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)
|
|
|
-
|
|
|
-func setupBackgroudTask() {
|
|
|
- t.eventHandler = timerTask
|
|
|
- //print("Timer Fired")
|
|
|
- //}
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func activateBackgroundTask() {
|
|
|
- t.resume()
|
|
|
-}
|
|
|
-
|
|
|
-func deactivateBackgroundTask() {
|
|
|
- t.suspend()
|
|
|
-}
|
|
|
-
|
|
|
-func timerTask() {
|
|
|
- print("Timer Fired")
|
|
|
-
|
|
|
-/*
|
|
|
- return
|
|
|
- let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "GPS")
|
|
|
- print("Timer Fired B")
|
|
|
+ print("sendToHttpServer()")
|
|
|
+
|
|
|
+ let jsonEncoder = JSONEncoder()
|
|
|
+ jsonEncoder.outputFormatting = .prettyPrinted
|
|
|
+
|
|
|
+ var uploadData:Data?
|
|
|
do {
|
|
|
- print("Timer Fired C")
|
|
|
- logData = try managedContext.fetch(fetchRequest)
|
|
|
- print("Timer Fired D")
|
|
|
- print( "Cnt:" + String(logData.count) )
|
|
|
- print("Timer Fired E")
|
|
|
- } catch let error as NSError {
|
|
|
- print("Could not fetch. \(error), \(error.userInfo)")
|
|
|
+ uploadData = try jsonEncoder.encode(gpsPosLog)
|
|
|
+ } catch {
|
|
|
+ print(error.localizedDescription)
|
|
|
+ return
|
|
|
}
|
|
|
- print("Timer Fired F")*/
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-//class SendPOSTData {
|
|
|
+ //print( gpsPosLog )
|
|
|
|
|
|
- func placeOrder(didUpdateLocations locations: [CLLocation]) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- print("placeOrder")
|
|
|
- locations.forEach { (location) in
|
|
|
-
|
|
|
- //let managedContext = appDelegate.persistentContainer.viewContext
|
|
|
- //let entity = NSEntityDescription.entity(forEntityName: "GPS", in: managedContext)!
|
|
|
-
|
|
|
- let person = Person(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude, test: 564)
|
|
|
- let jsonEncoder = JSONEncoder()
|
|
|
- jsonEncoder.outputFormatting = .prettyPrinted
|
|
|
-
|
|
|
- var uploadData:Data?
|
|
|
- do {
|
|
|
- uploadData = try jsonEncoder.encode(person)
|
|
|
- //let endcodeStringPerson = String(data: uploadData! , encoding: .utf8)!
|
|
|
- //print(endcodeStringPerson)
|
|
|
- } catch {
|
|
|
- print(error.localizedDescription)
|
|
|
- }
|
|
|
-
|
|
|
- let url = URL(string: "http://chef.maya.se/gpsapi/registerGPSlocation.php")!
|
|
|
- var request = URLRequest(url: url)
|
|
|
- request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
|
- request.httpMethod = "POST"
|
|
|
-
|
|
|
- let task = URLSession.shared.uploadTask(with: request, from: uploadData) { data, response, error in
|
|
|
- if let error = error {
|
|
|
- print ("error: \(error)")
|
|
|
- return
|
|
|
- }
|
|
|
- guard let response = response as? HTTPURLResponse,
|
|
|
- (200...299).contains(response.statusCode) else {
|
|
|
- print ("server error")
|
|
|
- return
|
|
|
- }
|
|
|
- if let mimeType = response.mimeType,
|
|
|
- mimeType == "text/html",
|
|
|
- let data = data,
|
|
|
- let dataString = String(data: data, encoding: .utf8) {
|
|
|
- print ("got data: \(dataString)")
|
|
|
- }
|
|
|
- else {
|
|
|
- print("KALLE:" + String(response.mimeType!))
|
|
|
- }
|
|
|
- }
|
|
|
- task.resume()
|
|
|
+ let url = URL(string: "http://chef.maya.se/gpsapi/registerGPSlocation.php")!
|
|
|
+ var request = URLRequest(url: url)
|
|
|
+ request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
|
+ request.httpMethod = "POST"
|
|
|
+
|
|
|
+ let task = URLSession.shared.uploadTask(with: request, from: uploadData) { data, response, error in
|
|
|
+ if let error = error {
|
|
|
+ print ("error: \(error)")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ guard let response = response as? HTTPURLResponse,
|
|
|
+ (200...299).contains(response.statusCode) else {
|
|
|
+ print ("200-299 server error")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if let mimeType = response.mimeType,
|
|
|
+ mimeType == "text/html",
|
|
|
+ let data = data,
|
|
|
+ let dataString = String(data: data, encoding: .utf8) {
|
|
|
+ print ("got data: \(dataString)")
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ print("KALLE:" + String(response.mimeType!))
|
|
|
}
|
|
|
}
|
|
|
+ task.resume()
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
//}
|