MeshbluKit 1.5.1

MeshbluKit 1.5.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2015
SPMSupports SPM

Maintained by Octoblu.



 
Depends on:
Alamofire~> 2.0.2
SwiftyJSON~> 2.3.0
 

  • By
  • Sqrt of Octoblu

MeshbluKit

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

MeshbluKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "MeshbluKit"

API

Is device registered?

isNotRegistered() -> Bool

Set Credentials

setCredentials(uuid: String, token: String)

Claim Device

claimDevice(uuid: String, handler: (Result<JSON, NSError>) -> ())

Get Devices

devices(options: [String: AnyObject], handler: (Result<JSON, NSError>) -> ())

Delete Device

deleteDevice(uuid: String, handler: (Result<JSON, NSError>) -> ())

Get Data

getData(uuid: String, options: [String: AnyObject], handler: (Result<JSON, NSError>) -> ())

Send Data Message

data(uuid: String, message: [String: AnyObject], handler: (Result<JSON, NSError>) -> ())

Send Message

message(message: [String: AnyObject], handler: (Result<JSON, NSError>) -> ())

Generate New Session Token

generateToken(uuid: String, handler: (Result<JSON, NSError>) -> ())

Reset Token

resetToken(uuid: String, handler: (Result<JSON, NSError>) -> ())

Update

update(uuid: String, properties: [String: AnyObject], handler: (Result<JSON, NSError>) -> ())
// OR
update(properties: [String: AnyObject], handler: (Result<JSON, NSError>) -> ())

Update Dangerously

updateDangerously(uuid: String, properties: [String: AnyObject], handler: (Result<JSON, NSError>) -> ())

Register Device

register(device: [String: AnyObject], handler: (Result<JSON, NSError>) -> ())

Get PublicKey

getPublicKey(uuid: String, handler: (Result<JSON, NSError>) -> ())

Who am i?

whoami(handler: (Result<JSON, NSError>) -> ())

Usage:

import Foundation
import MeshbluKit

class MeshbluExample : AnyObject {
  var meshbluHttp: MeshbluHttp

  init(meshbluConfig: [String: AnyObject]){
    self.meshbluHttp = MeshbluHttp(meshbluConfig: meshbluConfig)
    let uuid = meshbluConfig["uuid"] as? String
    let token = meshbluConfig["token"] as? String
    if uuid != nil && token != nil {
      self.meshbluHttp.setCredentials(uuid!, token: token!)
    }
  }

  init(meshbluHttp: MeshbluHttp) {
    self.meshbluHttp = meshbluHttp
  }

  func getMeshbluClient() -> MeshbluHttp {
    return self.meshbluHttp
  }

  func register() {
    let device = [
      "type": "device:ios-device", // Set your own device type
      "online" : "true"
    ]

    self.meshbluHttp.register(device) { (result) -> () in
      switch result {
      case let .Failure(error):
        println("Failed to register")
      case let .Success(success):
        let json = success.value
        let uuid = json["uuid"].stringValue
        let token = json["token"].stringValue
        println("Register device: uuid: \(uuid) and token: \(token)")

        self.meshbluHttp.setCredentials(uuid, token: token)
      }
    }
  }

  func sendMessage(payload: [String: AnyObject], handler: (Result<JSON, NSError>) -> ()){
    var message : [String: AnyObject] = [
      "devices" : ["*"],
      "payload" : payload,
      "topic" : "some-topic"
    ]

    self.meshbluHttp.message(message) {
      (result) -> () in
      handler(result)
      println("Message Sent: \(message)")
    }
  }
}

Author

Sqrt of Octoblu, [email protected]

License

MeshbluKit is available under the MIT license. See the LICENSE file for more info.