Guest User

Untitled

a guest
Feb 11th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. //
  2. // MQTTManager.swift
  3. // TemperatureApp
  4. //
  5. // Created by Radoka on 2/10/18.
  6. // Copyright © 2018 radoslav.genov.1992. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CocoaMQTT
  11.  
  12. class MQTTManager: NSObject, CocoaMQTTDelegate {
  13.  
  14. static let singleton = MQTTManager()
  15. let mqttClient = CocoaMQTT(clientID: "iOS Device Emulator", host: "192.168.0.101", port: 1883)
  16. var connected = false
  17.  
  18. override init() {
  19. super.init()
  20. print("MQTT Initilalized")
  21. mqttClient.username = "user"
  22. mqttClient.password = "pass"
  23. mqttClient.keepAlive = 60
  24. mqttClient.delegate = self
  25. connect()
  26. }
  27.  
  28. public func connect(){
  29. if mqttClient.connState != .connected && mqttClient.connState != .connecting {
  30. mqttClient.connect()
  31. }
  32. }
  33.  
  34. }
  35.  
  36. extension MQTTManager {
  37. func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
  38. //nothing
  39. }
  40.  
  41. func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
  42. //nothing
  43. }
  44.  
  45. func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
  46. //nothing
  47. }
  48.  
  49. func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
  50. if let string = message.string {
  51. print(string)
  52. }
  53. }
  54.  
  55. func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topic: String) {
  56. print("Subscribed to topic: ", topic)
  57. }
  58.  
  59. func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) {
  60.  
  61. }
  62.  
  63. func mqttDidPing(_ mqtt: CocoaMQTT) {
  64. print("PING")
  65. }
  66.  
  67. func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
  68. print("PONG")
  69. }
  70.  
  71. func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
  72. print("Disconnected with error: ", err!)
  73. }
  74.  
  75. func mqtt(mqtt: CocoaMQTT, didConnect host: String, port: Int) {
  76. print("Connected to MQTT server.")
  77. connected = true
  78. }
  79.  
  80. func subscribeToTopic(topic: String) {
  81. if mqttClient.connState == .connected {
  82. print("Subscribe to: ", topic)
  83. mqttClient.subscribe(topic, qos: CocoaMQTTQOS.qos1)
  84. } else {
  85. print("Can't subscribe to (topic). Not connected.")
  86. }
  87. }
  88. }
  89.  
  90. //
  91. // MainTableViewController.swift
  92. // TemperatureApp
  93. //
  94. // Created by Radoka on 2/9/18.
  95. // Copyright © 2018 radoslav.genov.1992. All rights reserved.
  96. //
  97.  
  98. import UIKit
  99. import CocoaMQTT
  100. import CoreData
  101.  
  102.  
  103. class MainTableViewController: UITableViewController {
  104.  
  105. // MARK: - Table view data source
  106. @IBOutlet weak var temperature: UILabel!
  107. let mqttManager = MQTTManager.singleton
  108. var container: NSPersistentContainer? = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer
  109.  
  110.  
  111. override func viewDidLoad() {
  112. super.viewDidLoad()
  113. }
  114.  
  115. override func viewWillAppear(_ animated: Bool) {
  116. super.viewWillAppear(animated)
  117. }
  118.  
  119. @IBAction func connectionTest(_ sender: UISwitch) {
  120. if sender.isOn {
  121. mqttManager.connect()
  122. print("connected")
  123. mqttManager.subscribeToTopic(topic: "rpi/gpio")
  124. } else {
  125. print("not connected")
  126. }
  127. }
  128.  
  129. override func numberOfSections(in tableView: UITableView) -> Int {
  130. // #warning Incomplete implementation, return the number of sections
  131. return 1
  132. }
  133.  
  134. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  135. // #warning Incomplete implementation, return the number of rows
  136. return 3
  137. }
  138.  
  139. public func setTemperature(){
  140. //Update text of temperature label
  141. }
  142.  
  143. }
  144.  
  145. class MainTableViewController: UITableViewController {
  146. override func viewDidLoad() {
  147. super.viewDidLoad()
  148.  
  149. // observe temperature
  150. NotificationCenter.default.addObserver(forName: NSNotification.Name.init("post_temperature"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
  151. self?.temperature.text = notification.object as? String ?? ""
  152. }
  153. }
  154. }
  155.  
  156. extension MQTTManager {
  157. func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
  158. if let string = message.string {
  159. // post temperature
  160. NotificationCenter.default.post(name: NSNotification.Name.init("post_temperature"), object: string)
  161. }
  162. }
  163. }
Add Comment
Please, Sign In to add comment