Skip to content

Commit 3d6541d

Browse files
Added first basic implementation of nutella.location
1 parent 63246a0 commit 3d6541d

File tree

98 files changed

+6711
-155
lines changed

Some content is hidden

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

98 files changed

+6711
-155
lines changed

Common/NutellaConfigDelegate.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// NutellaConfigDelegate.swift
3+
//
4+
//
5+
// Created by Gianluca Venturini on 24/01/15.
6+
//
7+
//
8+
9+
import Foundation
10+
11+
@objc protocol NutellaConfigDelegate {
12+
var runId: String { get }
13+
var componentId: String { get }
14+
var resourceId: String? { get }
15+
}
16+
17+

Common/NutellaDelegate.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,4 @@ import Foundation
1212
This protocol allows client to control the asynchronous callbacks.
1313
*/
1414
@objc public protocol NutellaDelegate {
15-
16-
/**
17-
Called when a message is received and is not part of publish/subscriber communication.
18-
19-
:param: channel The name of the Nutella chennal on which the message is received.
20-
:param: message The message.
21-
:param: from The actor name of the client that sent the message.
22-
*/
23-
optional func messageReceived(channel: String, message: AnyObject, from: String)
24-
25-
/**
26-
A response to a previos request is received.
27-
28-
:param: channelName The Nutella channel on which the message is received.
29-
:param: requestName The optional name of request.
30-
:param: response The dictionary/array/string containing the JSON representation.
31-
*/
32-
optional func responseReceived(channelName: String, requestName: String?, response: AnyObject)
33-
34-
/**
35-
A request is received on a Nutella channel that was previously handled (with the handleRequest).
36-
37-
:param: channelName The name of the Nutella chennal on which the request is received.
38-
:param: request The dictionary/array/string containing the JSON representation of the request.
39-
*/
40-
optional func requestReceived(channelName: String, request: AnyObject) -> AnyObject?
4115
}

Common/NutellaLocation.swift

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
//
2+
// NutellaLocation.swift
3+
//
4+
//
5+
// Created by Gianluca Venturini on 24/01/15.
6+
//
7+
//
8+
9+
import Foundation
10+
import CoreLocation
11+
12+
public class NutellaLocation: NSObject, NutellaNetDelegate, CLLocationManagerDelegate {
13+
14+
var _configDelegate: NutellaConfigDelegate?
15+
var configDelegate: NutellaConfigDelegate? {
16+
get {
17+
return _configDelegate
18+
}
19+
20+
set(delegate) {
21+
_configDelegate = delegate
22+
self.net.configDelegate = delegate
23+
}
24+
}
25+
26+
var regions = [CLBeaconRegion]()
27+
28+
let locationManager = CLLocationManager()
29+
30+
let net: NutellaNet
31+
32+
init(locationServer: String) {
33+
34+
// Initialize nutella net instance
35+
net = NutellaNet(host: locationServer, clientId: nil)
36+
37+
super.init()
38+
39+
println("Location initialization")
40+
41+
net.delegate = self
42+
43+
// Request the authorization to access the user location in every moment
44+
locationManager.requestAlwaysAuthorization()
45+
46+
locationManager.delegate = self
47+
}
48+
49+
public func startMonitoringRegions(uuids: [String]) {
50+
for uuid in uuids {
51+
var region: CLBeaconRegion = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: uuid), identifier: uuid)
52+
self.locationManager.startRangingBeaconsInRegion(region)
53+
self.regions.append(region)
54+
}
55+
}
56+
57+
public func startMonitorning() {
58+
// Request the beacon cloud service for the uuids list
59+
60+
self.net.asyncRequest("beacon/beacons/uuids", message: [], requestName: "uuids")
61+
}
62+
63+
public func stopMonitoring() {
64+
for region in self.regions {
65+
self.locationManager.stopMonitoringForRegion(region)
66+
}
67+
}
68+
69+
public func locationManager(manager: CLLocationManager, didRangeBeacons: [AnyObject], inRegion: CLBeaconRegion) {
70+
println("Monitor region")
71+
for beacon in didRangeBeacons {
72+
println(beacon.proximityUUID);
73+
println(beacon.major);
74+
println(beacon.minor);
75+
println(beacon.proximity);
76+
}
77+
}
78+
79+
80+
81+
// MARK: NutellaNetDelegate
82+
83+
/*
84+
public func messageReceived(channel: String, message: AnyObject, from: String) {
85+
86+
}
87+
*/
88+
89+
public func responseReceived(channelName: String, requestName: String?, response: AnyObject) {
90+
if requestName == "uuids" {
91+
if let r = response as? Dictionary<String, [String]> {
92+
if let uuids = response["uuids"] as? [String] {
93+
self.startMonitoringRegions(uuids)
94+
}
95+
}
96+
}
97+
}
98+
99+
/*
100+
public func requestReceived(channelName: String, request: AnyObject) -> AnyObject? {
101+
102+
}
103+
*/
104+
105+
/*
106+
107+
// MARK: ESTBeaconManagerDelegate
108+
func beaconManager(manager: ESTBeaconManager, didRangeBeacons: [ESTBeacon], inRegion: ESTBeaconRegion) {
109+
println("I've found \(didRangeBeacons.count) in range")
110+
111+
for beacon in didRangeBeacons {
112+
//println("----")
113+
//println("Majour: \(beacon.major)")
114+
//println("Minor: \(beacon.minor)")
115+
//println("RSSI: \(beacon.rssi)")
116+
//println("Distance: \(beacon.distance)")
117+
//sprintln("Name: \(beacon.name)")
118+
119+
if let name: String = beacon.name {
120+
if beacon.distance.doubleValue > 0 {
121+
println("Name: \(beacon.name)")
122+
123+
if let actorName = configDelegate?.actorName {
124+
var location: [String: AnyObject] = ["rid": actorName,
125+
"proximity": ["rid": name,
126+
"distance": beacon.distance.stringValue,
127+
"rssi": String(beacon.rssi)]]
128+
129+
net.publish("location/update", message: location)
130+
println("Message published")
131+
}
132+
}
133+
134+
}
135+
}
136+
}
137+
138+
*/
139+
}

Common/NutellaMain.swift

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,33 @@
77
// jazzy -o doc -a "Gianluca Venturini" -g "https://github.com/nutella-framework/nutella_lib.swift.git" --skip-undocumented
88
//
99

10+
#if DEBUG
11+
let DEBUG = true
12+
#else
13+
let DEBUG = false
14+
#endif
15+
1016
import Foundation
1117

1218
/**
1319
This is the main class that contains all the modules. It acts as a interface with the external world.
1420
*/
15-
public class Nutella: NutellaNetDelegate {
16-
var actorName: String
21+
public class Nutella: NutellaConfigDelegate {
22+
var componentId: String
1723
var runId: String
24+
public var resourceId: String?
25+
1826

1927
/**
2028
Nutella network module, it enable the explicit interaction using MQTT protocol.
2129
*/
2230
public var net: NutellaNet
2331

32+
/**
33+
Nutella location module, it enable the detection of near beacon
34+
*/
35+
public var location: NutellaLocation
36+
2437
/**
2538
The NutellaDelegateused in order to manage the notification about the status of Nutella.
2639
*/
@@ -34,25 +47,34 @@ public class Nutella: NutellaNetDelegate {
3447
:param: runId The run id of the instance of the application.
3548
: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
3649
*/
37-
public init(host: String, actorName: String, runId: String, clientId: String? = nil) {
38-
self.actorName = actorName
50+
public init(brokerHostname: String, runId: String, componentId: String) {
51+
52+
self.componentId = componentId
3953
self.runId = runId
40-
self.net = NutellaNet(host: host, clientId: clientId)
54+
self.net = NutellaNet(host: brokerHostname, clientId: nil)
55+
self.location = NutellaLocation(locationServer: brokerHostname)
4156

42-
self.net.delegate = self
57+
if(DEBUG) {
58+
println("[\(self)] init brokerHostname: \(brokerHostname) runId: \(runId) componentId: \(componentId)")
59+
}
60+
61+
self.net.configDelegate = self
62+
self.location.configDelegate = self
4363
}
4464

45-
// MARK: NutellaNetDelegate
65+
/**
66+
Nutella newtork module delegate.
67+
*/
4668

47-
func messageReceived(channel: String, message: AnyObject, from: String) {
48-
self.delegate?.messageReceived?(channel, message: message, from: from)
69+
public var netDelegate: NutellaNetDelegate? {
70+
get {
71+
return self.net.delegate
72+
}
73+
set(delegate) {
74+
self.net.delegate = delegate
75+
}
4976
}
5077

51-
func responseReceived(channelName: String, requestName: String?, response: AnyObject) {
52-
self.delegate?.responseReceived?(channelName, requestName: requestName, response: response)
53-
}
5478

55-
func requestReceived(channelName: String, request: AnyObject) -> AnyObject? {
56-
return self.delegate?.requestReceived?(channelName, request: request)
57-
}
79+
5880
}

0 commit comments

Comments
 (0)