Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MQTTManager.swift
- // TemperatureApp
- //
- // Created by Radoka on 2/10/18.
- // Copyright © 2018 radoslav.genov.1992. All rights reserved.
- //
- import UIKit
- import CocoaMQTT
- class MQTTManager: NSObject, CocoaMQTTDelegate {
- static let singleton = MQTTManager()
- let mqttClient = CocoaMQTT(clientID: "iOS Device Emulator", host: "192.168.0.101", port: 1883)
- var connected = false
- override init() {
- super.init()
- print("MQTT Initilalized")
- mqttClient.username = "user"
- mqttClient.password = "pass"
- mqttClient.keepAlive = 60
- mqttClient.delegate = self
- connect()
- }
- public func connect(){
- if mqttClient.connState != .connected && mqttClient.connState != .connecting {
- mqttClient.connect()
- }
- }
- }
- extension MQTTManager {
- func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
- //nothing
- }
- func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
- //nothing
- }
- func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
- //nothing
- }
- func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
- if let string = message.string {
- print(string)
- }
- }
- func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topic: String) {
- print("Subscribed to topic: ", topic)
- }
- func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) {
- }
- func mqttDidPing(_ mqtt: CocoaMQTT) {
- print("PING")
- }
- func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
- print("PONG")
- }
- func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
- print("Disconnected with error: ", err!)
- }
- func mqtt(mqtt: CocoaMQTT, didConnect host: String, port: Int) {
- print("Connected to MQTT server.")
- connected = true
- }
- func subscribeToTopic(topic: String) {
- if mqttClient.connState == .connected {
- print("Subscribe to: ", topic)
- mqttClient.subscribe(topic, qos: CocoaMQTTQOS.qos1)
- } else {
- print("Can't subscribe to (topic). Not connected.")
- }
- }
- }
- //
- // MainTableViewController.swift
- // TemperatureApp
- //
- // Created by Radoka on 2/9/18.
- // Copyright © 2018 radoslav.genov.1992. All rights reserved.
- //
- import UIKit
- import CocoaMQTT
- import CoreData
- class MainTableViewController: UITableViewController {
- // MARK: - Table view data source
- @IBOutlet weak var temperature: UILabel!
- let mqttManager = MQTTManager.singleton
- var container: NSPersistentContainer? = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- }
- @IBAction func connectionTest(_ sender: UISwitch) {
- if sender.isOn {
- mqttManager.connect()
- print("connected")
- mqttManager.subscribeToTopic(topic: "rpi/gpio")
- } else {
- print("not connected")
- }
- }
- override func numberOfSections(in tableView: UITableView) -> Int {
- // #warning Incomplete implementation, return the number of sections
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of rows
- return 3
- }
- public func setTemperature(){
- //Update text of temperature label
- }
- }
- class MainTableViewController: UITableViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // observe temperature
- NotificationCenter.default.addObserver(forName: NSNotification.Name.init("post_temperature"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
- self?.temperature.text = notification.object as? String ?? ""
- }
- }
- }
- extension MQTTManager {
- func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
- if let string = message.string {
- // post temperature
- NotificationCenter.default.post(name: NSNotification.Name.init("post_temperature"), object: string)
- }
- }
- }
Add Comment
Please, Sign In to add comment