-
Notifications
You must be signed in to change notification settings - Fork 2
/
BeaconLookup.swift
executable file
·133 lines (114 loc) · 5.49 KB
/
BeaconLookup.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// BeaconLookup.swift
//
//
// Created by Bernd Plontsch on 29/08/14.
// Copyright (c) 2014 Bernd Plontsch. All rights reserved.
//
import UIKit
import CoreLocation
public class BeaconLookup: NSObject {
public class func lookUpForCredentials(beaconUUID:NSString,beaconMajorID:Int,beaconMinorID:Int)->NSString {
var region:CLBeaconRegion
let uuid:NSUUID = NSUUID(UUIDString: beaconUUID)!
region = CLBeaconRegion(proximityUUID: uuid, major: CLBeaconMajorValue(beaconMajorID), minor: CLBeaconMinorValue(beaconMinorID), identifier: beaconUUID)
let lookedUpString:NSString = lookUpForCLBeacon(region).string as NSString!
return lookedUpString
}
public class func lookUpSpotForCredentials(beaconUUID:NSString,beaconMajorID:Int,beaconMinorID:Int)->NSString {
var region:CLBeaconRegion
let uuid:NSUUID = NSUUID(UUIDString: beaconUUID)!
region = CLBeaconRegion(proximityUUID: uuid, major: CLBeaconMajorValue(beaconMajorID), minor: CLBeaconMinorValue(beaconMinorID), identifier: beaconUUID)
var lookedUpString:NSString? = lookUpForCLBeacon(region).spot
if lookedUpString != nil {
return lookedUpString!
} else {
return "Beacon"
}
}
public class func lookUpForCLBeacon(beacon:CLBeaconRegion)->(room:NSString?, spot:NSString?, string:NSString) {
let lookUpMajor:NSString = beacon.major.stringValue;
let lookUpMinor:NSString = beacon.minor.stringValue;
let lookUpUUID:NSString = beacon.proximityUUID.UUIDString;
let beaconDictionary:NSDictionary = openBeaconCredentialsDictionary()
let UUIDDictionary:NSDictionary = beaconDictionary.objectForKey("UUID") as NSDictionary
let majorIDDictionary:NSDictionary = (beaconDictionary.objectForKey("Major") as NSDictionary).objectForKey("ID") as NSDictionary
let minorIDDictionary:NSDictionary = (beaconDictionary.objectForKey("Minor") as NSDictionary).objectForKey("ID") as NSDictionary
//BEACON NAME
var beaconName:NSString?
if UUIDDictionary.valueForKey(lookUpUUID) != nil {
beaconName = UUIDDictionary.valueForKey(lookUpUUID) as? NSString
} else {
beaconName = "Beacon"
}
//ROOM
var beaconRoom:NSString? = majorIDDictionary.valueForKey(lookUpMajor) as? NSString
if beaconRoom == nil {
var beaconRoomRange:NSString? = lookUpRange(lookUpMajor.integerValue, rangeDictionary: (beaconDictionary.objectForKey("Major") as NSDictionary).objectForKey("Range") as NSDictionary)
if beaconRoomRange != nil {
beaconRoom = beaconRoomRange
}
}
//SPOT
var beaconSpot:NSString? = minorIDDictionary.valueForKey(lookUpMinor) as? NSString
if beaconSpot == nil {
var beaconSpotRange:NSString? = lookUpRange(lookUpMinor.integerValue, rangeDictionary: (beaconDictionary.objectForKey("Minor") as NSDictionary).objectForKey("Range") as NSDictionary)
if beaconSpotRange != nil {
beaconSpot = beaconSpotRange
}
}
//LABEL
var label:NSString = "Beacon"
if (beaconSpot != nil && beaconRoom != nil) {
label = NSString(format: "%@ %@", beaconSpot!,beaconRoom!)
} else {
if (beaconSpot == nil && beaconRoom != nil) {
label = beaconRoom!
}
if (beaconSpot != nil && beaconRoom == nil) {
label = beaconSpot!
}
if beaconSpot == nil && beaconRoom == nil && beaconName != nil {
label = beaconName!
}
}
return (beaconRoom,beaconSpot,label)
}
public class func lookUpRange(identifier:Int, rangeDictionary:NSDictionary)->NSString? {
var lookedUpString:NSString?
rangeDictionary.enumerateKeysAndObjectsUsingBlock { (key, object, stop) -> Void in
let from:Int? = Int(object.valueForKey("from") as NSNumber)
let to:Int? = Int(object.valueForKey("to") as NSNumber)
if from != nil && to != nil {
if identifier > from && identifier < to {
lookedUpString = key as? NSString
stop.memory = true
}
}
}
return lookedUpString
}
public class func openBeaconCredentialsDictionary()->NSDictionary {
let path:NSString = NSBundle(forClass: BeaconLookup.self).pathForResource("OpenBeaconCredentials", ofType: "plist")!
let openBeaconCredentialsDictionary:NSDictionary = NSDictionary(contentsOfFile: path)!
return openBeaconCredentialsDictionary
}
public class func lookUpNameForUUID(uuidString:NSString)->NSString {
let beacons:NSDictionary = openBeaconCredentialsDictionary().objectForKey("UUID") as NSDictionary
let name:NSString? = beacons.objectForKey(uuidString) as? NSString
if name != nil {
return name!
}
return "Beacon"
}
public class func allBeacons()->NSArray {
let beacons:NSDictionary = openBeaconCredentialsDictionary().objectForKey("UUID") as NSDictionary
var allBeacons:NSMutableArray = NSMutableArray()
beacons.enumerateKeysAndObjectsUsingBlock { (item, count, stop) -> Void in
if item != nil {
allBeacons.addObject(item as NSString)
}
}
return allBeacons
}
}