| 1 |
efrain |
1 |
//
|
|
|
2 |
// SyncAdapter.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 2/24/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import Foundation
|
|
|
9 |
|
|
|
10 |
import Foundation
|
|
|
11 |
import Alamofire
|
|
|
12 |
import SwiftyJSON
|
|
|
13 |
|
|
|
14 |
class SyncAdapter
|
|
|
15 |
{
|
|
|
16 |
|
| 9 |
efrain |
17 |
private let appDao = AppDao.sharedInstance
|
|
|
18 |
private let syncDao = SyncDao.sharedInstance
|
| 1 |
efrain |
19 |
private var inProgress = false;
|
|
|
20 |
|
|
|
21 |
@objc func updateTimer() {
|
|
|
22 |
//print("updateTimer")
|
|
|
23 |
|
|
|
24 |
if inProgress {
|
|
|
25 |
return
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
inProgress = true
|
|
|
29 |
let myQue = DispatchQueue(label: "syncQue")
|
|
|
30 |
myQue.async {
|
|
|
31 |
self.sync{ success in
|
|
|
32 |
self.inProgress = false;
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
func sync(completionHandler : @escaping (_ success : Bool) -> Void) {
|
|
|
38 |
let recordsSync = self.syncDao.selectBatch()
|
|
|
39 |
|
|
|
40 |
if recordsSync.count > 0 {
|
|
|
41 |
var availableForOtherOperation = true;
|
|
|
42 |
for recordSync in recordsSync
|
|
|
43 |
{
|
|
|
44 |
print(recordSync)
|
|
|
45 |
|
|
|
46 |
if recordSync.type == Constants.SYNC_ADAPTER_TYPE_DEVICE {
|
|
|
47 |
availableForOtherOperation = false
|
|
|
48 |
registerDevice(record: recordSync)
|
|
|
49 |
}
|
|
|
50 |
else if availableForOtherOperation && recordSync.type == Constants.SYNC_ADAPTER_TYPE_FCM {
|
|
|
51 |
availableForOtherOperation = false
|
|
|
52 |
registerFcm(record: recordSync)
|
|
|
53 |
}
|
|
|
54 |
else if availableForOtherOperation && recordSync.type == Constants.SYNC_ADAPTER_TYPE_SYNC {
|
|
|
55 |
availableForOtherOperation = false
|
|
|
56 |
sendSync(record: recordSync)
|
|
|
57 |
}
|
|
|
58 |
else if recordSync.data.isEmpty {
|
|
|
59 |
syncDao.remove(id: recordSync.id)
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
completionHandler(true)
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
func sendSync(record: SyncModel)
|
|
|
67 |
{
|
| 11 |
efrain |
68 |
let appData = appDao.selectOne()
|
| 1 |
efrain |
69 |
|
|
|
70 |
let parameters = [
|
|
|
71 |
Constants.POST_SYNC_FIELD_DEVICE_UUID: "\(appData.deviceUuid)",
|
|
|
72 |
Constants.POST_SYNC_FIELD_DATA: "\(record.data)",
|
|
|
73 |
Constants.POST_SYNC_FIELD_SYNC_ID: "\(record.id)"
|
|
|
74 |
]
|
|
|
75 |
let headers: HTTPHeaders = [
|
|
|
76 |
.accept(Constants.HTTP_HEADER_ACCEPT)
|
|
|
77 |
]
|
|
|
78 |
|
|
|
79 |
AF.request(Config.URL_SYNC, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON {response in
|
|
|
80 |
|
| 11 |
efrain |
81 |
//print("Send Sync ")
|
| 1 |
efrain |
82 |
|
|
|
83 |
switch response.result {
|
|
|
84 |
case .success:
|
|
|
85 |
let result = try? JSON(data: response.data!)
|
|
|
86 |
if result?["success"] ?? "" != false{
|
|
|
87 |
let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
|
|
|
88 |
if sync_id > 0 {
|
|
|
89 |
self.syncDao.remove(id: sync_id)
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
break
|
|
|
93 |
case .failure:
|
| 11 |
efrain |
94 |
// print("JSON = \(String(describing: Error.self))")
|
| 1 |
efrain |
95 |
break
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
func registerFcm(record: SyncModel) {
|
|
|
101 |
|
| 9 |
efrain |
102 |
let appData = appDao.selectOne()
|
|
|
103 |
|
| 1 |
efrain |
104 |
let deviceUuid = appData.deviceUuid
|
|
|
105 |
let parameters = [
|
|
|
106 |
Constants.POST_FCM_FIELD_APPLICATION_ID: "\(Constants.GLOBAL_APPLICATION_ID)",
|
|
|
107 |
Constants.POST_FCM_FIELD_DEVICE_UUID: "\(deviceUuid)",
|
|
|
108 |
Constants.POST_FCM_FIELD_TOKEN: "\(record.data)",
|
|
|
109 |
Constants.POST_FCM_FIELD_SYNC_ID: "\(record.id)"
|
|
|
110 |
]
|
|
|
111 |
|
|
|
112 |
let headers: HTTPHeaders = [
|
|
|
113 |
.accept(Constants.HTTP_HEADER_ACCEPT)
|
|
|
114 |
]
|
|
|
115 |
|
|
|
116 |
AF.request(Config.URL_FCM, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{response in
|
|
|
117 |
|
|
|
118 |
print("Send FCM")
|
|
|
119 |
|
|
|
120 |
switch response.result {
|
|
|
121 |
case .success:
|
|
|
122 |
let result = try? JSON(data: response.data!)
|
|
|
123 |
let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
|
|
|
124 |
if sync_id > 0 {
|
|
|
125 |
self.syncDao.remove(id: sync_id)
|
|
|
126 |
}
|
|
|
127 |
break
|
|
|
128 |
case .failure:
|
| 11 |
efrain |
129 |
// print("JSON = \(String(describing: Error.self))")
|
| 1 |
efrain |
130 |
break
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
func registerDevice(record: SyncModel)
|
|
|
137 |
{
|
| 9 |
efrain |
138 |
|
|
|
139 |
|
| 1 |
efrain |
140 |
let version = UIDevice.current.systemVersion
|
|
|
141 |
let model = UIDevice.current.localizedModel
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
let parameters = [
|
|
|
146 |
Constants.POST_DEVICE_FIELD_APPLICATION_ID: "1",
|
|
|
147 |
Constants.POST_DEVICE_FIELD_DEVICE_UUID: "\(record.data)",
|
|
|
148 |
Constants.POST_DEVICE_FIELD_MANUFACTURER: "Apple",
|
|
|
149 |
Constants.POST_DEVICE_FIELD_BRAND: "Apple",
|
|
|
150 |
Constants.POST_DEVICE_FIELD_VERSION: "\(version)",
|
|
|
151 |
Constants.POST_DEVICE_FIELD_MODEL: "\(model)",
|
|
|
152 |
Constants.POST_DEVICE_FIELD_PLATFORM: "Iphone",
|
|
|
153 |
Constants.POST_DEVICE_FIELD_SYNC_ID: "\(record.id)"
|
|
|
154 |
]
|
|
|
155 |
|
|
|
156 |
let headers: HTTPHeaders = [
|
|
|
157 |
.accept(Constants.HTTP_HEADER_ACCEPT)
|
|
|
158 |
]
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
print("URL DEVICE : \(Config.URL_DEVICE) ")
|
|
|
162 |
|
|
|
163 |
AF.request(Config.URL_DEVICE, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON {response in
|
|
|
164 |
|
|
|
165 |
print("Send Device: ")
|
|
|
166 |
|
|
|
167 |
switch response.result {
|
|
|
168 |
case .success:
|
|
|
169 |
let result = try? JSON(data: response.data!)
|
|
|
170 |
if result?["success"] ?? "" != false {
|
|
|
171 |
let aes = result?["data"]["aes"].stringValue ?? ""
|
|
|
172 |
let password = result?["data"]["password"].stringValue ?? ""
|
|
|
173 |
|
|
|
174 |
if !aes.isEmpty && !password.isEmpty {
|
|
|
175 |
|
|
|
176 |
|
| 9 |
efrain |
177 |
var appData = self.appDao.selectOne()
|
|
|
178 |
appData.deviceAes = aes
|
|
|
179 |
appData.devicePassword = password
|
| 11 |
efrain |
180 |
|
|
|
181 |
print("update query : 1")
|
| 9 |
efrain |
182 |
self.appDao.update(model: appData)
|
| 1 |
efrain |
183 |
}
|
|
|
184 |
|
|
|
185 |
let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
|
|
|
186 |
if sync_id > 0 {
|
|
|
187 |
self.syncDao.remove(id: sync_id)
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
break
|
|
|
191 |
case .failure:
|
| 11 |
efrain |
192 |
//print("JSON = \(String(describing: Error.self))")
|
| 1 |
efrain |
193 |
break
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
}
|