Callback.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Callback.swift
  3. // Runner
  4. //
  5. // Created by Yayan Dwi on 15/04/20.
  6. // Copyright © 2020 The Chromium Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import nuSDKService
  10. import Network
  11. import NotificationBannerSwift
  12. class Callback : CallBack {
  13. var sID: String = "Callback"
  14. func connectionStateChanged(sUserID: String!, sDeviceID: String!, bConState: Bool!, nConType: Int!, nConSubType: Int!, nCLMConStat: UInt8!) {
  15. //print(sUserID, "/", sDeviceID, "/", bConState, "/", nConType, "/", nConSubType, "/", nCLMConStat)
  16. if let dispatch = Nexilis.dispatch, bConState {
  17. dispatch.leave()
  18. }
  19. if let delegate = Nexilis.shared.connectionDelegate {
  20. delegate.connectionStateChanged(userId: sUserID, deviceId: sDeviceID, state: bConState)
  21. }
  22. InquiryThread.default.set(wait: nCLMConStat == 0)
  23. OutgoingThread.default.set(wait: nCLMConStat == 0)
  24. }
  25. func gpsStateChanged(nState: Int!) {
  26. }
  27. func sleepStateChanged(bState: Bool!) {
  28. }
  29. func callStateChanged(nState: Int!, sMessage: String!) -> Int {
  30. //print(nState,"/",sMessage)
  31. if nState == Nexilis.AUDIO_CALL_INCOMING || nState == Nexilis.VIDEO_CALL_INCOMING {
  32. if let delegate = Nexilis.shared.callDelegate {
  33. if !Nexilis.showLibraryNotification {
  34. delegate.onStatusCall(state: nState, message: sMessage)
  35. } else {
  36. delegate.onIncomingCall(state: nState, message: sMessage)
  37. }
  38. }
  39. } else {
  40. // if nstate == Nexilis.AUDIO_CALL_END {
  41. // let pin = sMessage.split(separator: ",")[0]
  42. // if let call = Palio.shared.callManager.call(with: String(pin)), !call.hasVideo, !call.hasEnded {
  43. // call.reconnectingCall()
  44. // return 1
  45. // }
  46. // }
  47. if let delegate = Nexilis.shared.callDelegate {
  48. delegate.onStatusCall(state: nState, message: sMessage)
  49. }
  50. }
  51. return 1
  52. }
  53. func bcastStateChanged(nState: Int!, sMessage: String!) -> Int {
  54. //print("LS CALLBACK ",nState,"/",sMessage)
  55. if let delegate = Nexilis.shared.streamingDelagate {
  56. delegate.onStartLS(state: nState, message: sMessage)
  57. }
  58. if let delegate = Nexilis.shared.streamingDelagate {
  59. delegate.onJoinLS(state: nState, message: sMessage)
  60. }
  61. if let delegate = Nexilis.shared.seminarDelegate {
  62. delegate.onStartSeminar(state: nState, message: sMessage)
  63. }
  64. if let delegate = Nexilis.shared.seminarDelegate {
  65. delegate.onJoinSeminar(state: nState, message: sMessage)
  66. }
  67. return 1
  68. }
  69. func sshareStateChanged(nState: Int!, sMessage: String!) -> Int {
  70. //print("Screen sharing state: ",nState,"/",sMessage)
  71. switch nState {
  72. case 0:
  73. if (sMessage.starts(with: "Initiating")){
  74. if let delegate = Nexilis.shared.screenSharingDelegate {
  75. delegate.onStartScreenSharing(state: nState, message: sMessage)
  76. }
  77. }
  78. case 12:
  79. if let delegate = Nexilis.shared.screenSharingDelegate {
  80. delegate.onStartScreenSharing(state: nState, message: sMessage)
  81. }
  82. case 22:
  83. if let delegate = Nexilis.shared.screenSharingDelegate {
  84. delegate.onJoinScreenSharing(state: nState, message: sMessage)
  85. }
  86. case 88:
  87. if let delegate = Nexilis.shared.screenSharingDelegate {
  88. delegate.onStartScreenSharing(state: nState, message: sMessage)
  89. }
  90. if let delegate = Nexilis.shared.screenSharingDelegate {
  91. delegate.onJoinScreenSharing(state: nState, message: sMessage)
  92. }
  93. default:
  94. break
  95. }
  96. return 1
  97. }
  98. func incomingData(sPacketID: String!, oData: AnyObject!) throws {
  99. Nexilis.incomingData(packetId: sPacketID!, data: oData!)
  100. }
  101. func lateResponse(sPacketID: String!, sResponse: String!) throws {
  102. }
  103. func asycnACKReceived(sPacketID: String!) throws {
  104. //print("asycnACKReceived: \(sPacketID)")
  105. DispatchQueue.global().async {
  106. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  107. do {
  108. OutgoingThread.default.delOutgoing(fmdb: fmdb, packageId: sPacketID)
  109. } catch {
  110. rollback.pointee = true
  111. print("Access database error: \(error.localizedDescription)")
  112. }
  113. })
  114. }
  115. }
  116. func locationUpdated(lTime: Int64!, sLocationInfo: String!) {
  117. }
  118. func resetDB() {
  119. }
  120. }
  121. class NetworkMonitor {
  122. static let shared = NetworkMonitor()
  123. private let monitor = NWPathMonitor()
  124. private var isMonitoring = false
  125. var isConnected: Bool = false
  126. var fromDisconnect = false
  127. private init() {}
  128. func startMonitoring() {
  129. guard !isMonitoring else { return }
  130. monitor.pathUpdateHandler = { path in
  131. self.canAccessGoogle(completion: { [self] connected in
  132. self.isConnected = connected
  133. InquiryThread.default.set(wait: !connected)
  134. OutgoingThread.default.set(wait: !connected)
  135. if !connected {
  136. fromDisconnect = true
  137. // DispatchQueue.main.async {
  138. // let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  139. // imageView.tintColor = .white
  140. // let banner = FloatingNotificationBanner(title: "Check your connection".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  141. // banner.show()
  142. // }
  143. }
  144. if connected && fromDisconnect {
  145. fromDisconnect = false
  146. DispatchQueue.main.async {
  147. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  148. imageView.tintColor = .white
  149. let banner = FloatingNotificationBanner(title: "You're Connected".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
  150. banner.show()
  151. }
  152. }
  153. })
  154. }
  155. let queue = DispatchQueue.global(qos: .background)
  156. monitor.start(queue: queue)
  157. isMonitoring = true
  158. }
  159. func stopMonitoring() {
  160. guard isMonitoring else { return }
  161. monitor.cancel()
  162. isMonitoring = false
  163. }
  164. func canAccessGoogle(completion: @escaping (Bool) -> Void) {
  165. guard let url = URL(string: "https://www.google.com") else {
  166. completion(false)
  167. return
  168. }
  169. var request = URLRequest(url: url)
  170. request.timeoutInterval = 5
  171. let task = URLSession.shared.dataTask(with: request) { _, response, error in
  172. if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
  173. completion(true)
  174. } else {
  175. completion(false)
  176. }
  177. }
  178. task.resume()
  179. }
  180. }