Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Beacon support #10

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
40 changes: 23 additions & 17 deletions nervous/Controllers/AxonController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class AxonController {
init(){
startAxonHTTPServer()
}



func startAxonHTTPServer(){

do {
Expand Down Expand Up @@ -69,44 +68,51 @@ class AxonController {

}


// route to get any axon resource
self.server.GET["/nervousnet-axons/:axonname/:resource"] = { r in
if let filename = r.params[":resource"], axonname = r.params[":axonname"] {
return self.returnRawResponse("\(self.axonDir)/\(axonname)/\(axonname)-master/\(filename)");
}
return .NotFound

}





// route to get any axon resource
self.server.POST["/nervousnet-api/log"] = { r in
print("axon debug:")
print(String(bytes: r.body, encoding: NSUTF8StringEncoding))
return .OK(.Json(""))
}

self.server.GET["/nervousnet-api/deviceid"] = { r in
let uuid = UIDevice.currentDevice().identifierForVendor!.UUIDString
return .OK(.Json(["uuid": uuid]))
}

// route to get any axon resource
self.server.GET["/nervousnet-api/raw-sensor-data/:sensor/"] = { r in
if let sensor = r.params[":sensor"] {

print(sensor)

let data = self.laeController.getData(sensor)

print(data)

if(sensor == "BLE"){
let jsonObject: NSDictionary = ["blepacket": data[0] as! String]
return .OK(.Json(jsonObject))
}else if(sensor == "GPS"){
let jsonObject: NSDictionary = ["lat": data[0], "long":data[1]]
return .OK(.Json(jsonObject))
}else if(sensor == "GPS"){
let jsonObject: NSDictionary = ["lat": data[0], "long":data[1]]
return .OK(.Json(jsonObject))
}else if(sensor == "Beacon"){
return .OK(.Json(data))
}else{
let jsonObject: NSDictionary = ["x": data[0], "y":data[1], "z": data[2]]
return .OK(.Json(jsonObject))

}


}

return .NotFound

}


Expand Down
66 changes: 47 additions & 19 deletions nervous/Controllers/BeaconController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,61 @@ protocol BeaconControllerDelegate {
}


let _sharedInstance = BeaconController()

class BeaconController : NSObject, SensorProtocol{

var delegate: BeaconControllerDelegate?
let locationManager : CLLocationManager
let beaconRegion : CLBeaconRegion
let beaconUUIDString = NSUUID(UUIDString: "3C77C2A5-5D39-420F-97FD-E7735CC7F317")!
let beaconIdentifier = "ch.ethz.nervous"
let beaconMajor:CLBeaconMajorValue = 33091

var beaconRegions = [CLBeaconRegion]()
var beaconData = [AnyObject]()
var _tmpBeacons = [String:AnyObject]()
override init() {

self.locationManager = CLLocationManager()
self.beaconRegion = CLBeaconRegion(proximityUUID: beaconUUIDString, identifier: beaconIdentifier)
}


class var sharedInstance: BeaconController {
return _sharedInstance
}

func requestAuthorization() {
locationManager.requestAlwaysAuthorization()
}



func getdata() -> [AnyObject] {
var xx = [AnyObject]()
_tmpBeacons.keys.forEach({ (key:String) in
xx.append(_tmpBeacons[key]!)
})
return xx
}

func addBeaconData(data:[[String:String]]) {
beaconRegions = []
data.forEach { (kv:[String:String]) in
let br = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: kv["uuid"]!)!, identifier: kv["identity"]!)
beaconRegions.append(br)
}
}

func startSensorUpdates() {
locationManager.delegate = self
locationManager.startMonitoringForRegion(beaconRegion)
locationManager.startRangingBeaconsInRegion(beaconRegion)
for beaconRegion in beaconRegions {
locationManager.startMonitoringForRegion(beaconRegion)
locationManager.startRangingBeaconsInRegion(beaconRegion)
}
}



func stopSensorUpdates() {
locationManager.stopMonitoringForRegion(beaconRegion)
locationManager.stopRangingBeaconsInRegion(beaconRegion)
for beaconRegion in beaconRegions {
locationManager.stopMonitoringForRegion(beaconRegion)
locationManager.stopRangingBeaconsInRegion(beaconRegion)
}
beaconData = []
}

}

extension BeaconController: CLLocationManagerDelegate {
Expand All @@ -62,7 +83,14 @@ extension BeaconController: CLLocationManagerDelegate {
}

func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {


self.beaconData = beacons.map({ (beacon) -> [String:String] in
let bb = ["uuid": beacon.proximityUUID.UUIDString, "major": String(beacon.major), "minor": String(beacon.minor), "proximity": String(beacon.proximity.rawValue), "accuracy": String(format:"%f", beacon.accuracy)]
_tmpBeacons[bb["uuid"]!] = bb
return bb
})


//send new beacons to delegates
if let delegate = self.delegate {
delegate.controller(self, didRangeBeacons: beacons)
Expand Down
8 changes: 7 additions & 1 deletion nervous/Controllers/LAEController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ class LAEController : NSObject {
data.append(sen.lat)
data.append(sen.long)
}


if sensor == "Beacon" {
let sen = BeaconController.sharedInstance
// data = sen.beaconData
data = sen.getdata()
}

return data
}

Expand Down
15 changes: 12 additions & 3 deletions nervous/Controllers/ViewControllers/AxonViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class AxonViewController: UIViewController {
}

@IBAction func closeAxonButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: {});
self.dismissViewControllerAnimated(true, completion: {
let beaconController = BeaconController.sharedInstance
beaconController.stopSensorUpdates()
});
}

override func viewWillAppear(animated: Bool) {
Expand All @@ -38,12 +41,18 @@ class AxonViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.



print("firing up the axon...")
let url = AxonStore.getLocalAxonURL(axonName);
let request = NSURLRequest(URL: url!)

let beacons = AxonStore.getLocalAxonBeacons(axonName)
if beacons.count > 0 {
let beaconController = BeaconController.sharedInstance
beaconController.addBeaconData(beacons)
beaconController.startSensorUpdates()
}

axonWebView.loadRequest(request);
axonWebView.scrollView.bounces = false;
}
Expand Down
10 changes: 10 additions & 0 deletions nervous/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>xip.io</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
<key>NSLocationAlwaysUsageDescription</key>
<string>Provides access to bluetooth beacons.</string>
Expand Down
Loading