Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 9 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
17
    private let appData = AppData.sharedInstance
18
 
19
    private var syncDao = SyncDao.sharedInstance
20
    private var inProgress = false;
21
 
22
    @objc func updateTimer() {
23
        //print("updateTimer")
24
 
25
        if inProgress   {
26
            return
27
        }
28
 
29
        inProgress = true
30
        let myQue = DispatchQueue(label: "syncQue")
31
        myQue.async {
32
            self.sync{ success in
33
                self.inProgress = false;
34
            }
35
        }
36
    }
37
 
38
    func sync(completionHandler : @escaping (_ success : Bool) -> Void) {
39
        let recordsSync = self.syncDao.selectBatch()
40
 
41
        if recordsSync.count > 0 {
42
            var availableForOtherOperation = true;
43
            for recordSync in recordsSync
44
            {
45
                print(recordSync)
46
 
47
                if recordSync.type == Constants.SYNC_ADAPTER_TYPE_DEVICE {
48
                    availableForOtherOperation = false
49
                    registerDevice(record: recordSync)
50
                }
51
                else if availableForOtherOperation && recordSync.type == Constants.SYNC_ADAPTER_TYPE_FCM {
52
                    availableForOtherOperation = false
53
                    registerFcm(record: recordSync)
54
                }
55
                else if availableForOtherOperation && recordSync.type == Constants.SYNC_ADAPTER_TYPE_SYNC {
56
                    availableForOtherOperation = false
57
                    sendSync(record: recordSync)
58
                }
59
                else if recordSync.data.isEmpty {
60
                    syncDao.remove(id: recordSync.id)
61
                }
62
            }
63
        }
64
        completionHandler(true)
65
    }
66
 
67
    func sendSync(record: SyncModel)
68
    {
69
 
70
 
71
 
72
        let parameters = [
73
            Constants.POST_SYNC_FIELD_DEVICE_UUID: "\(appData.deviceUuid)",
74
            Constants.POST_SYNC_FIELD_DATA: "\(record.data)",
75
            Constants.POST_SYNC_FIELD_SYNC_ID: "\(record.id)"
76
        ]
77
        let headers: HTTPHeaders = [
78
            .accept(Constants.HTTP_HEADER_ACCEPT)
79
        ]
80
 
81
        AF.request(Config.URL_SYNC, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON {response in
82
 
83
            print("Send Sync  ")
84
 
85
            switch response.result {
86
                case .success:
87
                    let result = try? JSON(data: response.data!)
88
                    if result?["success"] ?? "" != false{
89
                        let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
90
                        if sync_id > 0 {
91
                            self.syncDao.remove(id: sync_id)
92
                        }
93
                    }
94
                break
95
                case .failure:
96
                    print("JSON = \(String(describing: Error.self))")
97
                break
98
            }
99
        }
100
    }
101
 
102
    func registerFcm(record: SyncModel) {
103
 
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:
129
                    print("JSON = \(String(describing: Error.self))")
130
                    break
131
            }
132
        }
133
    }
134
 
135
 
136
    func registerDevice(record: SyncModel)
137
    {
138
 
139
        let version = UIDevice.current.systemVersion
140
        let model = UIDevice.current.localizedModel
141
 
142
 
143
 
144
        let parameters = [
145
            Constants.POST_DEVICE_FIELD_APPLICATION_ID: "1",
146
            Constants.POST_DEVICE_FIELD_DEVICE_UUID: "\(record.data)",
147
            Constants.POST_DEVICE_FIELD_MANUFACTURER: "Apple",
148
            Constants.POST_DEVICE_FIELD_BRAND: "Apple",
149
            Constants.POST_DEVICE_FIELD_VERSION: "\(version)",
150
            Constants.POST_DEVICE_FIELD_MODEL: "\(model)",
151
            Constants.POST_DEVICE_FIELD_PLATFORM: "Iphone",
152
            Constants.POST_DEVICE_FIELD_SYNC_ID: "\(record.id)"
153
        ]
154
 
155
        let headers: HTTPHeaders = [
156
            .accept(Constants.HTTP_HEADER_ACCEPT)
157
        ]
158
 
159
 
160
        print("URL DEVICE : \(Config.URL_DEVICE) ")
161
 
162
        AF.request(Config.URL_DEVICE, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON {response in
163
 
164
            print("Send Device: ")
165
 
166
            switch response.result {
167
                case .success:
168
                    let result = try? JSON(data: response.data!)
169
                    if result?["success"] ?? "" != false {
170
                        let aes = result?["data"]["aes"].stringValue ?? ""
171
                        let password = result?["data"]["password"].stringValue ?? ""
172
 
173
                        if !aes.isEmpty && !password.isEmpty {
174
 
175
 
176
                            self.appData.aes = aes
177
                            self.appData.password = password
178
                            self.appData.save()
179
                        }
180
 
181
                        let sync_id = Int(result?["data"]["sync_id"].stringValue ?? "0") ?? 0
182
                        if sync_id > 0 {
183
                            self.syncDao.remove(id: sync_id)
184
                        }
185
                    }
186
                break
187
                case .failure:
188
                    print("JSON = \(String(describing: Error.self))")
189
                break
190
            }
191
        }
192
    }
193
}