Skip to content

Instantly share code, notes, and snippets.

@zeisler
Created November 7, 2018 23:32
Show Gist options
  • Save zeisler/f489b3611c1bd158b1cdeab61f9dd228 to your computer and use it in GitHub Desktop.
Save zeisler/f489b3611c1bd158b1cdeab61f9dd228 to your computer and use it in GitHub Desktop.
Uses the https://watchman.online WIFI Thermometer
//
// AppDelegate.swift
// watchman_temp
//
// Created by Dustin Zeisler on 11/7/18.
// Copyright © 2018 Dustin Zeisler. All rights reserved.
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.callWatchMan()
self.constructMenu()
_ = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(callWatchMan), userInfo: nil, repeats: true)
}
var ipAddress = "192.168.1.163"
var productId = "059c40102418"
@objc func callWatchMan() {
if let button = statusItem.button {
let urlString = "http://\(self.ipAddress)/\(self.productId)&Stats"
let url = URL(string: urlString)!
print(url)
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
let string = String(data: data, encoding: .utf8)!
DispatchQueue.main.async { // Make sure you're on the main thread here
button.title = self.parseTemp(input: string) + " " + self.parseHum(input: string)
}
}
task.resume()
}
}
func constructMenu() {
let menu = NSMenu()
menu.addItem(NSMenuItem(title: "Product ID: \(self.productId)", action: #selector(callWatchMan), keyEquivalent: "I"))
menu.addItem(NSMenuItem(title: "IP Address: \(self.ipAddress)", action: #selector(callWatchMan), keyEquivalent: "P"))
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "Quit WatchMan", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
statusItem.menu = menu
}
func parseTemp(input: String) -> String {
let matches = self.matches(for: "Temp:(\\d{2}\\.?\\d{0,2})", in: input)
var returnValue = "?"
if matches.count == 1 {
returnValue = matches[0]
}
return returnValue.replacingOccurrences(of: "Temp:", with: "") + "°F"
}
func parseHum(input: String) -> String {
let matches = self.matches(for: "Humi:(\\d{2}\\.?\\d{0,2})%", in: input)
var returnValue = "?"
if matches.count == 1 {
returnValue = matches[0]
}
return returnValue.replacingOccurrences(of: "Humi:", with: "") + "H"
}
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let nsString = text as NSString
let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
return results.map {
return nsString.substring(with: $0.range)
}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment