Skip to content

Commit 7177410

Browse files
Added full support to monitoring client API
1 parent 40560f0 commit 7177410

File tree

100 files changed

+4990
-123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+4990
-123
lines changed

Common/NutellaLocation.swift

Lines changed: 132 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class NLResource {
4141
self.parameters = parameters
4242

4343
self.notifyUpdate = false
44+
self.notifyEnter = false
45+
self.notifyExit = false
4446
}
4547
convenience init(rid: String) {
4648
self.init(model: NLResourceModel.UNKNOWN,
@@ -59,6 +61,10 @@ class NLResource {
5961
// Satates that when an update is received the NutellaLocationDelegate must be notified of it
6062
var notifyUpdate: Bool
6163

64+
// Satates that when a resource enter/exit the range the NutellaLocationDelegate must be notified of it
65+
var notifyEnter: Bool
66+
var notifyExit: Bool
67+
6268
// Resource coordinates
6369
var continuous: NLResourceContinuous?
6470
var discrete: NLResourceDiscrete?
@@ -110,7 +116,6 @@ public class NLManagedResource: NLManaged {
110116
public var continuous: NLManagedResourceContinuous
111117
public var discrete: NLManagedResourceDiscrete
112118

113-
// Every time the resource is modified the NutellaLocationDelegate is notified
114119
public var notifyUpdate: Bool? {
115120
get {
116121
return resource?.notifyUpdate
@@ -120,6 +125,23 @@ public class NLManagedResource: NLManaged {
120125
}
121126
}
122127

128+
public var notifyEnter: Bool? {
129+
get {
130+
return resource?.notifyEnter
131+
}
132+
set(notifyEnter) {
133+
resource?.notifyEnter = notifyEnter!
134+
}
135+
}
136+
137+
public var notifyExit: Bool? {
138+
get {
139+
return resource?.notifyExit
140+
}
141+
set(notifyExit) {
142+
resource?.notifyExit = notifyExit!
143+
}
144+
}
123145
}
124146

125147
protocol NLManagedResourceDelegate : class {
@@ -298,8 +320,6 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
298320

299321
resource.delegate = self
300322

301-
println("Location initialization")
302-
303323
net.delegate = self
304324

305325
// Request the authorization to access the user location in every moment
@@ -346,7 +366,6 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
346366
}
347367

348368
public func locationManager(manager: CLLocationManager, didRangeBeacons: [AnyObject], inRegion: CLBeaconRegion) {
349-
println("Monitor region")
350369
for clBeacon in didRangeBeacons {
351370
println(clBeacon.proximityUUID);
352371
println(clBeacon.major);
@@ -379,7 +398,6 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
379398
if let clientResource = self._resource {
380399
if(clientResource.type == NLResourceType.STATIC &&
381400
resource.type == NLResourceType.DYNAMIC) {
382-
println("Send beacon update");
383401
self.net.publish("location/resource/update", message: [
384402
"rid": beacon!.rid,
385403
"proximity": ["rid": myRid,
@@ -404,6 +422,82 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
404422
}
405423
}
406424

425+
func updateResource(resource: Dictionary<String, AnyObject>) {
426+
if let rid = resource["rid"] as? String {
427+
if let type = resource["type"] as? String {
428+
if let model = resource["model"] as? String {
429+
if let parameters = resource["parameters"] as? Dictionary<String, AnyObject> {
430+
var r = self.resource.resources[rid]
431+
432+
if r == nil {
433+
r = NLResource(rid: rid)
434+
}
435+
436+
switch(type) {
437+
case "STATIC":
438+
r!.type = NLResourceType.STATIC
439+
break
440+
case "DYNAMIC":
441+
r!.type = NLResourceType.DYNAMIC
442+
break
443+
default:
444+
r!.type = NLResourceType.UNKNOWN
445+
}
446+
447+
switch(model) {
448+
case "IMAC":
449+
r!.model = NLResourceModel.IMAC
450+
break
451+
case "IPHONE":
452+
r!.model = NLResourceModel.IPHONE
453+
break
454+
case "IPAD":
455+
r!.model = NLResourceModel.IPAD
456+
break
457+
case "IBEACON":
458+
r!.model = NLResourceModel.IBEACON
459+
break
460+
default:
461+
r!.model = NLResourceModel.UNKNOWN
462+
}
463+
464+
if let continuous = resource["continuous"] as? Dictionary<String, AnyObject> {
465+
r!.trackingSystem = NLResourceTrackingSystem.CONTINUOUS
466+
if let x = continuous["x"] as? Double {
467+
if let y = continuous["y"] as? Double {
468+
r!.continuous = NLResourceContinuous(x: x, y: y)
469+
}
470+
}
471+
}
472+
473+
if let discrete = resource["discrete"] as? Dictionary<String, AnyObject> {
474+
r!.trackingSystem = NLResourceTrackingSystem.DISCRETE
475+
if let x = discrete["x"] as? Double {
476+
if let y = discrete["y"] as? Double {
477+
r!.discrete = NLResourceDiscrete(x: x, y: y)
478+
}
479+
}
480+
}
481+
482+
if let proximity = resource["proximity"] as? Dictionary<String, AnyObject> {
483+
r!.trackingSystem = NLResourceTrackingSystem.PROXIMITY
484+
if let baseStationRid = proximity["rid"] as? String {
485+
if let distance = proximity["distance"] as? Double {
486+
r!.proximity = NLResourceProximity(rid: rid, distance: distance)
487+
}
488+
}
489+
}
490+
491+
// Update the resource if updates enabled
492+
if r?.notifyUpdate == true {
493+
self.delegate?.resourceUpdated(NLManagedResource(resource: r!, delegate: self))
494+
}
495+
}
496+
}
497+
}
498+
}
499+
}
500+
407501

408502

409503
// MARK: NutellaNetDelegate
@@ -412,93 +506,51 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
412506
if channel == "location/resources/updated" {
413507
if let resources = message["resources"] as? [Dictionary<String, AnyObject>] {
414508
for resource in resources {
415-
if let rid = resource["rid"] as? String {
416-
if let type = resource["type"] as? String {
417-
if let model = resource["model"] as? String {
418-
if let parameters = resource["parameters"] as? Dictionary<String, AnyObject> {
419-
var r = self.resource.resources[rid]
420-
421-
if r == nil {
422-
r = NLResource(rid: rid)
423-
}
424-
425-
switch(type) {
426-
case "STATIC":
427-
r!.type = NLResourceType.STATIC
428-
break
429-
case "DYNAMIC":
430-
r!.type = NLResourceType.DYNAMIC
431-
break
432-
default:
433-
r!.type = NLResourceType.UNKNOWN
434-
}
435-
436-
switch(model) {
437-
case "IMAC":
438-
r!.model = NLResourceModel.IMAC
439-
break
440-
case "IPHONE":
441-
r!.model = NLResourceModel.IPHONE
442-
break
443-
case "IPAD":
444-
r!.model = NLResourceModel.IPAD
445-
break
446-
case "IBEACON":
447-
r!.model = NLResourceModel.IBEACON
448-
break
449-
default:
450-
r!.model = NLResourceModel.UNKNOWN
451-
}
452-
453-
if let continuous = resource["continuous"] as? Dictionary<String, AnyObject> {
454-
r!.trackingSystem = NLResourceTrackingSystem.CONTINUOUS
455-
if let x = continuous["x"] as? Double {
456-
if let y = continuous["y"] as? Double {
457-
r!.continuous = NLResourceContinuous(x: x, y: y)
458-
}
459-
}
460-
}
461-
462-
if let discrete = resource["discrete"] as? Dictionary<String, AnyObject> {
463-
r!.trackingSystem = NLResourceTrackingSystem.DISCRETE
464-
if let x = discrete["x"] as? Double {
465-
if let y = discrete["y"] as? Double {
466-
r!.discrete = NLResourceDiscrete(x: x, y: y)
467-
}
468-
}
469-
}
470-
471-
if let proximity = resource["proximity"] as? Dictionary<String, AnyObject> {
472-
r!.trackingSystem = NLResourceTrackingSystem.PROXIMITY
473-
if let baseStationRid = proximity["rid"] as? String {
474-
if let distance = proximity["distance"] as? Double {
475-
r!.proximity = NLResourceProximity(rid: rid, distance: distance)
476-
}
477-
}
478-
}
479-
480-
// Update the resource if updates enabled
481-
if r?.notifyUpdate == true {
482-
self.delegate?.managedResourceUpdated(NLManagedResource(resource: r!, delegate: self))
483-
}
484-
}
485-
}
486-
}
487-
}
509+
updateResource(resource)
488510
}
489511
}
490512
}
491513

492514
// Resource enter
493515
if let match = channel.rangeOfString("^location/resource/static/.*/enter$", options: .RegularExpressionSearch) {
494516
let baseStationRid = channel.substringWithRange(Range<String.Index>(start: advance(channel.startIndex, 25), end: advance(channel.endIndex,-6)))
495-
println("ENTER")
517+
518+
// Update the resources
519+
if let resources = message["resources"] as? [Dictionary<String, AnyObject>] {
520+
for resource in resources {
521+
updateResource(resource)
522+
if let rid = resource["rid"] as? String {
523+
let dynamicResource = self.resource.resources[rid]
524+
let staticResource = self.resource.resources[baseStationRid]
525+
526+
if dynamicResource != nil && staticResource != nil && staticResource?.notifyEnter == true {
527+
self.delegate?.resourceEntered(NLManagedResource(resource: dynamicResource!, delegate: self),
528+
staticResource: NLManagedResource(resource: staticResource!, delegate: self))
529+
}
530+
}
531+
}
532+
}
496533
}
497534

498535
// Resource exit
499536
if let match = channel.rangeOfString("^location/resource/static/.*/exit$", options: .RegularExpressionSearch) {
500537
let baseStationRid = channel.substringWithRange(Range<String.Index>(start: advance(channel.startIndex, 25), end: advance(channel.endIndex,-5)))
501-
println("EXIT")
538+
539+
// Update the resources
540+
if let resources = message["resources"] as? [Dictionary<String, AnyObject>] {
541+
for resource in resources {
542+
updateResource(resource)
543+
if let rid = resource["rid"] as? String {
544+
let dynamicResource = self.resource.resources[rid]
545+
let staticResource = self.resource.resources[baseStationRid]
546+
547+
if dynamicResource != nil && staticResource != nil && staticResource?.notifyExit == true {
548+
self.delegate?.resourceExited(NLManagedResource(resource: dynamicResource!, delegate: self),
549+
staticResource: NLManagedResource(resource: staticResource!, delegate: self))
550+
}
551+
}
552+
}
553+
}
502554
}
503555
}
504556

@@ -638,7 +690,6 @@ public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDel
638690
Update the resource on server
639691
*/
640692
func updateResource(resource: NLResource) {
641-
println("Update resource " + resource.rid)
642693
var message: [String:AnyObject] = [
643694
"rid": resource.rid
644695
]

Common/NutellaLocationDelegate.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ import Foundation
1313
*/
1414
public protocol NutellaLocationDelegate {
1515

16-
func managedResourceUpdated(resource: NLManagedResource)
16+
func resourceUpdated(resource: NLManagedResource)
17+
func resourceEntered(dynamicResource: NLManagedResource, staticResource: NLManagedResource)
18+
func resourceExited(dynamicResource: NLManagedResource, staticResource: NLManagedResource)
1719
}

MobileGUI/NutellaMobileGUI/NutellaMobileGUI/Base.lproj/Main.storyboard

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<connections>
7575
<outlet property="segmentedControl" destination="eQE-kf-Im5" id="Lz0-uk-0pd"/>
7676
<outlet property="startButton" destination="A7L-ly-HWM" id="Ua3-kj-c33"/>
77+
<outlet property="stepper" destination="9Zn-RX-mSd" id="3ie-bu-zvG"/>
7778
<outlet property="textLabel" destination="IkC-5w-UhW" id="Kpb-Yk-Meb"/>
7879
</connections>
7980
</viewController>

MobileGUI/NutellaMobileGUI/NutellaMobileGUI/ViewController.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ViewController: UIViewController, NutellaNetDelegate, NutellaLocationDeleg
1616
@IBOutlet weak var segmentedControl: UISegmentedControl!
1717
@IBOutlet weak var textLabel: UITextField!
1818
@IBOutlet weak var startButton: UIButton!
19+
@IBOutlet weak var stepper: UIStepper!
1920

2021
@IBAction func startMonitoring(sender: AnyObject) {
2122
self.startButton.enabled = false
@@ -28,10 +29,21 @@ class ViewController: UIViewController, NutellaNetDelegate, NutellaLocationDeleg
2829
nutella?.resourceId = resourceId
2930
nutella?.netDelegate = self
3031
nutella?.locationDelegate = self
32+
33+
nutella?.location.resource[resourceId!]?.notifyEnter = true
3134
}
35+
3236
@IBAction func ipadSelectionChanged(sender: AnyObject) {
37+
nutella?.location.resource[nutella!.resourceId!]?.notifyEnter = false
38+
3339
var resourceId = segmentedControl.titleForSegmentAtIndex(segmentedControl.selectedSegmentIndex)
40+
41+
nutella?.location.resource[resourceId!]?.notifyEnter = true
42+
3443
nutella?.resourceId = resourceId
44+
if let x = nutella?.location.resource[resourceId!]?.continuous.x {
45+
self.stepper.value = x
46+
}
3547
}
3648

3749
@IBAction func positionChanged(sender: AnyObject) {
@@ -58,12 +70,25 @@ class ViewController: UIViewController, NutellaNetDelegate, NutellaLocationDeleg
5870
println("Response received")
5971
}
6072

61-
func managedResourceUpdated(resource: NLManagedResource) {
73+
// MARK: NutellaLocationDelegate
74+
func resourceUpdated(resource: NLManagedResource) {
6275
// Update resource
6376
println(resource.rid)
6477
println(resource.continuous.x)
6578
println(resource.continuous.y)
6679
}
80+
81+
func resourceEntered(dynamicResource: NLManagedResource, staticResource: NLManagedResource) {
82+
println("---- ENTER ----")
83+
println(dynamicResource.rid)
84+
println(staticResource.rid)
85+
}
86+
87+
func resourceExited(dynamicResource: NLManagedResource, staticResource: NLManagedResource) {
88+
println("---- EXIT ----")
89+
println(dynamicResource.rid)
90+
println(staticResource.rid)
91+
}
6792

6893
}
6994

Nutella.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)