@@ -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
125147protocol 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 ]
0 commit comments