Skip to content

Commit f9619ff

Browse files
Updated APIs
1 parent a7a2cb7 commit f9619ff

File tree

15 files changed

+193
-127
lines changed

15 files changed

+193
-127
lines changed

Common/NutellaConfigDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010

1111
@objc protocol NutellaConfigDelegate {
1212
var runId: String { get }
13+
var appId: String { get }
1314
var componentId: String { get }
1415
var resourceId: String? { get }
1516
}

Common/NutellaLocation.swift

Lines changed: 96 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import Foundation
1010
import CoreLocation
11+
import CoreBluetooth
1112

1213
class NLBeacon {
1314
init(uuid: String,
@@ -309,7 +310,7 @@ func == (left: NLBeacon, right: CLBeacon) -> Bool {
309310
/**
310311
This class enables the communication with RoomPlaces module
311312
*/
312-
public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDelegate, NLManagedResourceDelegate {
313+
public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDelegate, NLManagedResourceDelegate, CBPeripheralDelegate {
313314

314315
var delegate: NutellaLocationDelegate?
315316

@@ -335,6 +336,19 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
335336
if resourceId != nil {
336337
if let resource = self.resource.resources[resourceId!] {
337338
self._resource = resource
339+
340+
self.stopMonitoring()
341+
self.stopVirtualBeacon()
342+
343+
// STATIC => MONITORING
344+
if self._resource?.type == NLResourceType.STATIC {
345+
self.startMonitorning()
346+
}
347+
// DYNAMIC && iBeacon => VIRTUAL BEACON
348+
if self._resource?.type == NLResourceType.DYNAMIC && self._resource?.trackingSystem == NLResourceTrackingSystem.PROXIMITY {
349+
self.startVirtualBeacon()
350+
}
351+
338352
}
339353
}
340354
}
@@ -343,6 +357,7 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
343357
var beacons = [String:NLBeacon]()
344358
var regions = [CLBeaconRegion]()
345359
let locationManager = CLLocationManager()
360+
let peripheralManager = CBPeripheralManager(delegate: nil, queue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))
346361

347362
var resources: [String] {
348363
get {
@@ -399,6 +414,9 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
399414
self.net.subscribe("location/resources/updated");
400415
self.net.subscribe("location/resource/static/#");
401416

417+
// Subscribe to beacon and virtual beacon update
418+
self.net.subscribe("beacon/beacons/added");
419+
402420
}
403421

404422
public func startMonitoringRegions(uuids: [String]) {
@@ -407,6 +425,12 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
407425
self.locationManager.startRangingBeaconsInRegion(region)
408426
self.regions.append(region)
409427
}
428+
429+
// Hardcoded region for virtual beacons
430+
let uuid = "00000000-0000-0000-0000-000000000000"
431+
var region: CLBeaconRegion = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: uuid), identifier: uuid)
432+
self.locationManager.startRangingBeaconsInRegion(region)
433+
self.regions.append(region)
410434
}
411435

412436
public func startMonitorning() {
@@ -419,6 +443,33 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
419443
for region in self.regions {
420444
self.locationManager.stopMonitoringForRegion(region)
421445
}
446+
self.regions = []
447+
}
448+
449+
public func startVirtualBeacon() {
450+
if let rid = self._resource?.rid {
451+
self.net.asyncRequest("beacon/virtual_beacon", message: ["rid": rid], requestName: "virtual_beacon")
452+
}
453+
}
454+
455+
public func startVirtualBeacon(major: Int, minor: Int) {
456+
/*
457+
if peripheralManager.state.rawValue < CBPeripheralManagerState.PoweredOn.rawValue {
458+
println("WARNING: Bluetooth disabled")
459+
}
460+
*/
461+
462+
// Create the region
463+
let uuid = NSUUID(UUIDString: "00000000-0000-0000-0000-000000000000")
464+
var region = CLBeaconRegion(proximityUUID: uuid, major: UInt16(major), minor: UInt16(minor), identifier: "virtual_beacon")
465+
466+
if let peripheralData = region.peripheralDataWithMeasuredPower(-59) {
467+
peripheralManager.startAdvertising(peripheralData)
468+
}
469+
}
470+
471+
public func stopVirtualBeacon() {
472+
peripheralManager.stopAdvertising()
422473
}
423474

424475
public func locationManager(manager: CLLocationManager, didRangeBeacons: [AnyObject], inRegion: CLBeaconRegion) {
@@ -627,6 +678,36 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
627678
}
628679
}
629680
}
681+
682+
// Beacon or virtual beacon added
683+
if channel == "beacon/beacons/added" {
684+
if let beacons = message["beacons"] as? [Dictionary<String, AnyObject>] {
685+
for beacon in beacons {
686+
if let uuid = beacon["uuid"] as? String {
687+
if let minorS = beacon["minor"] as? String {
688+
if let majorS = beacon["major"] as? String {
689+
if let minor = minorS.toInt() {
690+
if let major = majorS.toInt() {
691+
if let rid = beacon["rid"] as? String {
692+
var b = NLBeacon(uuid: uuid,
693+
minor: minor,
694+
major: major,
695+
rid: rid)
696+
self.beacons[rid] = b
697+
698+
if let resource = self.resource.resources[rid] {
699+
b.resource = resource
700+
resource.beacon = b
701+
}
702+
}
703+
}
704+
}
705+
}
706+
}
707+
}
708+
}
709+
}
710+
}
630711
}
631712

632713
public func responseReceived(channelName: String, requestName: String?, response: AnyObject) {
@@ -674,84 +755,24 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
674755
if let resources = response["resources"] as? [Dictionary<String, AnyObject>] {
675756
for resource in resources {
676757
updateResource(resource)
677-
/*
678-
if let rid = resource["rid"] as? String {
679-
if let model = resource["model"] as? String {
680-
if let type = resource["type"] as? String {
681-
var newResource = NLResource(rid: rid)
682-
if let continuous = resource["continuous"] as? Dictionary<String, AnyObject> {
683-
newResource.trackingSystem = NLResourceTrackingSystem.CONTINUOUS;
684-
if let x = continuous["x"] as? Double {
685-
if let y = continuous["y"] as? Double {
686-
newResource.continuous = NLResourceContinuous(x: x, y: y)
687-
}
688-
}
689-
}
690-
if let discrete = resource["discrete"] as? Dictionary<String, AnyObject> {
691-
newResource.trackingSystem = NLResourceTrackingSystem.DISCRETE;
692-
if let x = discrete["x"] as? Double {
693-
if let y = discrete["y"] as? Double {
694-
newResource.discrete = NLResourceDiscrete(x: x, y: y)
695-
}
696-
}
697-
}
698-
if let proximity = resource["proximity"] as? Dictionary<String, AnyObject> {
699-
newResource.trackingSystem = NLResourceTrackingSystem.PROXIMITY;
700-
if let baseStationRid = proximity["rid"] as? String {
701-
if let distance = proximity["distance"] as? Double {
702-
newResource.proximity = NLResourceProximity(rid: rid, distance: distance)
703-
}
704-
}
705-
}
706-
707-
switch(type) {
708-
case "STATIC":
709-
newResource.type = NLResourceType.STATIC
710-
break
711-
case "DYNAMIC":
712-
newResource.type = NLResourceType.DYNAMIC
713-
break
714-
default:
715-
newResource.type = NLResourceType.UNKNOWN
716-
}
717-
718-
switch(model) {
719-
case "IMAC":
720-
newResource.model = NLResourceModel.IMAC
721-
break
722-
case "IPHONE":
723-
newResource.model = NLResourceModel.IPHONE
724-
break
725-
case "IPAD":
726-
newResource.model = NLResourceModel.IPAD
727-
break
728-
case "IBEACON":
729-
newResource.model = NLResourceModel.IBEACON
730-
break
731-
default:
732-
newResource.model = NLResourceModel.UNKNOWN
733-
}
734-
735-
self.resource.resources[rid] = newResource
736-
737-
// Search the corresponding beacon and connect it
738-
if let beacon = self.beacons[rid] {
739-
beacon.resource = newResource
740-
newResource.beacon = beacon
741-
}
742-
743-
// Set the resource type of the client
744-
if let resourceId = self.resourceId {
745-
if resourceId == rid {
746-
self._resource = newResource
747-
}
748-
}
749-
}
758+
}
759+
}
760+
}
761+
}
762+
763+
if requestName == "virtual_beacon" {
764+
if let r = response as? Dictionary<String, AnyObject> {
765+
if let minorS = r["minor"] as? String {
766+
if let majorS = r["major"] as? String {
767+
if let minor = minorS.toInt() {
768+
if let major = majorS.toInt() {
769+
println(minor);
770+
self.startVirtualBeacon(major, minor: minor)
750771
}
751772
}
752-
*/
753773
}
754774
}
775+
755776
}
756777
}
757778
}

Common/NutellaMain.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Foundation
2121
public class Nutella: NutellaConfigDelegate {
2222
var componentId: String
2323
var runId: String
24+
var appId: String
2425

2526
public var resourceId: String? {
2627
get {
@@ -55,10 +56,11 @@ public class Nutella: NutellaConfigDelegate {
5556
:param: runId The run id of the instance of the application.
5657
:param: clientId The client id used for techinical reason. Do not use it unless you have a valid motivation, the system will take care of generating it if left null
5758
*/
58-
public init(brokerHostname: String, runId: String, componentId: String) {
59+
public init(brokerHostname: String, appId: String, runId: String, componentId: String) {
5960

6061
self.componentId = componentId
6162
self.runId = runId
63+
self.appId = appId
6264
self.net = NutellaNet(host: brokerHostname, clientId: nil)
6365
self.location = NutellaLocation(locationServer: brokerHostname)
6466

@@ -72,7 +74,7 @@ public class Nutella: NutellaConfigDelegate {
7274
self.location.downloadBeaconList()
7375
self.location.downloadResourceList()
7476

75-
self.location.startMonitorning()
77+
//self.location.startMonitorning()
7678
self.location.subscribeResourceUpdate()
7779
}
7880

0 commit comments

Comments
 (0)