Skip to content

Commit

Permalink
so thankful for time machine rn
Browse files Browse the repository at this point in the history
  • Loading branch information
robbiet480 committed Sep 19, 2016
1 parent 9cc67c7 commit 8314894
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 645 deletions.
8 changes: 8 additions & 0 deletions HomeAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
B60616BA1D1F117800249C11 /* US-EN-Daisy-Refrigerator-Leak.wav in Resources */ = {isa = PBXBuildFile; fileRef = B60615B91D1F117700249C11 /* US-EN-Daisy-Refrigerator-Leak.wav */; };
B60616BB1D1F117800249C11 /* US-EN-Daisy-Water-Heater-Leak.wav in Resources */ = {isa = PBXBuildFile; fileRef = B60615BA1D1F117700249C11 /* US-EN-Daisy-Water-Heater-Leak.wav */; };
B624DE181CB8369200F413CE /* DevicesMapViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B624DE171CB8369200F413CE /* DevicesMapViewController.swift */; };
B626AAEF1D8F44DC00A0D225 /* DiscoveryInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B626AAEE1D8F44DC00A0D225 /* DiscoveryInfo.swift */; };
B626AAF11D8F972800A0D225 /* SettingsDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B626AAF01D8F972800A0D225 /* SettingsDetailViewController.swift */; };
B627CB091D83C87B0057173E /* UserNotifications.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B627CB081D83C87B0057173E /* UserNotifications.framework */; };
B627CB0B1D83C87B0057173E /* UserNotificationsUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B627CB0A1D83C87B0057173E /* UserNotificationsUI.framework */; };
B627CB0E1D83C87B0057173E /* NotificationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B627CB0D1D83C87B0057173E /* NotificationViewController.swift */; };
Expand Down Expand Up @@ -391,6 +393,8 @@
B60615B91D1F117700249C11 /* US-EN-Daisy-Refrigerator-Leak.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = "US-EN-Daisy-Refrigerator-Leak.wav"; sourceTree = "<group>"; };
B60615BA1D1F117700249C11 /* US-EN-Daisy-Water-Heater-Leak.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = "US-EN-Daisy-Water-Heater-Leak.wav"; sourceTree = "<group>"; };
B624DE171CB8369200F413CE /* DevicesMapViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DevicesMapViewController.swift; sourceTree = "<group>"; };
B626AAEE1D8F44DC00A0D225 /* DiscoveryInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiscoveryInfo.swift; sourceTree = "<group>"; };
B626AAF01D8F972800A0D225 /* SettingsDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsDetailViewController.swift; sourceTree = "<group>"; };
B627CB071D83C87B0057173E /* NotificationContentExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = NotificationContentExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
B627CB081D83C87B0057173E /* UserNotifications.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotifications.framework; path = System/Library/Frameworks/UserNotifications.framework; sourceTree = SDKROOT; };
B627CB0A1D83C87B0057173E /* UserNotificationsUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UserNotificationsUI.framework; path = System/Library/Frameworks/UserNotificationsUI.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -763,6 +767,7 @@
B657A8E91CA646EB00121384 /* AppDelegate.swift */,
B6F700491CB34FA600753BAB /* GroupViewController.swift */,
B657A8ED1CA646EB00121384 /* SettingsViewController.swift */,
B626AAF01D8F972800A0D225 /* SettingsDetailViewController.swift */,
B6393F8B1CB260E800503916 /* EntityAttributesViewController.swift */,
B657A8EF1CA646EB00121384 /* Main.storyboard */,
B657A8F21CA646EB00121384 /* Assets.xcassets */,
Expand Down Expand Up @@ -833,6 +838,7 @@
B6F02BFB1CB4800C0029ABE7 /* Services.swift */,
B66428111CF7A83200903804 /* PushConfiguration.swift */,
B6FBE3961D46A4E500EE0E36 /* Beacons.swift */,
B626AAEE1D8F44DC00A0D225 /* DiscoveryInfo.swift */,
);
name = "HA API Responses";
sourceTree = "<group>";
Expand Down Expand Up @@ -1439,6 +1445,7 @@
files = (
B6F7004A1CB34FA600753BAB /* GroupViewController.swift in Sources */,
B6F02BE91CB46AF40029ABE7 /* Entity.swift in Sources */,
B626AAEF1D8F44DC00A0D225 /* DiscoveryInfo.swift in Sources */,
B6F02C1A1CB489770029ABE7 /* SunComponent.swift in Sources */,
B62A67A01D80E933009E46F9 /* IdentifyRequest.swift in Sources */,
B6F02C1C1CB489770029ABE7 /* ThermostatComponent.swift in Sources */,
Expand All @@ -1451,6 +1458,7 @@
B6F02C181CB489770029ABE7 /* ScriptComponent.swift in Sources */,
B6F02C161CB489770029ABE7 /* MediaPlayerComponent.swift in Sources */,
B6393F8C1CB260E800503916 /* EntityAttributesViewController.swift in Sources */,
B626AAF11D8F972800A0D225 /* SettingsDetailViewController.swift in Sources */,
B6F02BEB1CB46D090029ABE7 /* GroupComponent.swift in Sources */,
B6F700521CB374F700753BAB /* RootTabBarViewController.swift in Sources */,
B64D047A1CB8908B00B945C8 /* StateChanged.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions HomeAssistant/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
HomeAssistantAPI.sharedInstance.Setup(baseAPIUrl: baseURL, APIPassword: apiPass)
}.then {_ in
HomeAssistantAPI.sharedInstance.Connect()
}.then { _ in
print("Connected!")
}.catch {err -> Void in
print("ERROR", err)
let settingsView = SettingsViewController()
Expand Down
61 changes: 45 additions & 16 deletions HomeAssistant/HAAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class HomeAssistantAPI {

}

func Connect() -> Promise<Bool> {
func Connect() -> Promise<ConfigResponse> {
return Promise { fulfill, reject in
GetConfig().then { config -> Void in
self.loadedComponents = config.Components!
Expand All @@ -112,7 +112,7 @@ public class HomeAssistantAPI {

// self.GetHistory()
self.startStream()
fulfill(true)
fulfill(config)
}.catch {error in
print("Error at launch!", error)
Crashlytics.sharedInstance().recordError((error as Any) as! NSError)
Expand Down Expand Up @@ -770,7 +770,7 @@ public class HomeAssistantAPI {
}

var notificationsEnabled : Bool {
return PermissionScope().statusNotifications() == .authorized
return PermissionScope().statusNotifications() == .authorized && prefs.string(forKey: "endpointARN") != nil
}

var enabledPermissions : [String] {
Expand All @@ -787,6 +787,36 @@ public class HomeAssistantAPI {
show(whistle: Murmur(title: title), action: .show(0.5))
}

func CleanBaseURL(baseUrl: URL) -> (hasValidScheme: Bool, cleanedURL: URL) {
if (baseUrl.absoluteString.hasPrefix("http://") || baseUrl.absoluteString.hasPrefix("https://")) == false {
return (false, baseUrl)
}
var urlComponents = URLComponents()
urlComponents.scheme = baseUrl.scheme
urlComponents.host = baseUrl.host
urlComponents.port = baseUrl.port
if urlComponents.port == nil {
urlComponents.port = (baseUrl.scheme == "http") ? 80 : 443
}
return (true, urlComponents.url!)
}

func GetDiscoveryInfo(baseUrl: URL) -> Promise<DiscoveryInfoResponse> {
let queryUrl = baseUrl.appendingPathComponent("/api/discovery_info")
return Promise { fulfill, reject in
let _ = Alamofire.request(queryUrl).validate().responseObject { (response: DataResponse<DiscoveryInfoResponse>) -> Void in
switch response.result {
case .success:
fulfill(response.result.value!)
case .failure(let error):
CLSLogv("Error on getDiscoveryInfo() request: %@", getVaList([error.localizedDescription]))
Crashlytics.sharedInstance().recordError(error)
reject(error)
}
}
}
}

}

class BonjourDelegate : NSObject, NetServiceBrowserDelegate, NetServiceDelegate {
Expand All @@ -805,13 +835,8 @@ class BonjourDelegate : NSObject, NetServiceBrowserDelegate, NetServiceDelegate

func netServiceDidResolveAddress(_ sender: NetService) {
NSLog("BonjourDelegate.Browser.netServiceDidResolveAddress")
let dataDict = NetService.dictionary(fromTXTRecord: sender.txtRecordData()!)
let baseUrl = copyStringFromTXTDict(dict: dataDict, which: "base_url")
let requiresAPIPassword = (copyStringFromTXTDict(dict: dataDict, which: "requires_api_password") == "true")
let useSSL = (baseUrl![4] == "s")
let version = copyStringFromTXTDict(dict: dataDict, which: "version")
let discoveryInfo : [String:Any] = ["name": sender.name, "baseUrl": baseUrl!, "requires_api_password": requiresAPIPassword, "version": version!, "use_ssl": useSSL]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "homeassistant.discovered"), object: nil, userInfo: discoveryInfo)
let discoveryInfo = DiscoveryInfoFromDict(locationName: sender.name, netServiceDictionary: NetService.dictionary(fromTXTRecord: sender.txtRecordData()!))
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "homeassistant.discovered"), object: nil, userInfo: discoveryInfo.toJSON())
}

func netServiceBrowser(_ netServiceBrowser: NetServiceBrowser, didRemove netService: NetService, moreComing moreServicesComing: Bool) {
Expand Down Expand Up @@ -840,15 +865,19 @@ class BonjourDelegate : NSObject, NetServiceBrowserDelegate, NetServiceDelegate
// NSLog("BonjourDelegate.Browser.netServiceWillPublish:\(sender)");
// }

private func copyStringFromTXTDict(dict: [String : Data], which: String) -> String? {
if let data = dict[which] {
return String(data: data, encoding: .utf8)
} else {
return nil
private func DiscoveryInfoFromDict(locationName: String, netServiceDictionary: [String : Data]) -> DiscoveryInfoResponse {
var outputDict : [String:Any] = [:]
for (key, value) in netServiceDictionary {
outputDict[key] = String(data: value, encoding: .utf8)
if outputDict[key] as? String == "true" || outputDict[key] as? String == "false" {
outputDict[key] = Bool(outputDict[key] as! String)
}
}
outputDict["location_name"] = locationName
return DiscoveryInfoResponse(JSON: outputDict)!
}

// Publisher methods
// Publisher methods

// func netService(sender: NetService, didNotPublish errorDict: [String : NSNumber]) {
// NSLog("BonjourDelegate.Publisher.didNotPublish:\(sender)");
Expand Down
2 changes: 1 addition & 1 deletion HomeAssistant/IdentifyRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IdentifyRequest: Mappable {
DeviceSystemVersion <- map["device.systemVersion"]
DeviceType <- map["device.type"]
Permissions <- map["permissions"]
PushID <- map["pushID"]
PushID <- map["pushId"]
PushSounds <- map["pushSounds"]
PushToken <- map["pushToken"]
}
Expand Down

0 comments on commit 8314894

Please sign in to comment.