Advertisement
marcusziade

Untitled

Jan 4th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.57 KB | None | 0 0
  1. struct StatusStruct {
  2.     let temp: String
  3.     let mode: Mode
  4.     let fanSpeed: FanSpeed
  5.     let power: Power
  6. }
  7.  
  8. enum Mode: String {
  9.     case cold = "Cold"
  10.     case heat = "Heat"
  11.     case unknown = "Unknown"
  12. }
  13.  
  14. enum Power: String {
  15.     case on = "On"
  16.     case off = "Off"
  17.     case unknown = "Unknown"
  18. }
  19.  
  20. enum FanSpeed: String {
  21.     case night = "Night"
  22.     case one = "1"
  23.     case two = "2"
  24.     case three = "3"
  25.     case four = "4"
  26.     case five = "5"
  27.     case unknown = "Unknown"
  28. }
  29.  
  30. func currentStatus(airconIp: String) {
  31.     var temp = ""
  32.     var mode = Mode.unknown
  33.     var fanSpeed = FanSpeed.unknown
  34.     var power = Power.unknown
  35.  
  36.     let modeMap = ["3": Mode.cold, "4": Mode.heat]
  37.     let powerMap = ["1": Power.on, "0": Power.off]
  38.     let fanMap = ["B": FanSpeed.night, "3": FanSpeed.one, "4": FanSpeed.two, "5": FanSpeed.three, "6": FanSpeed.four, "7": FanSpeed.five]
  39.  
  40.     for value in getCurrentStatus(airconIp) {
  41.         if value.contains("stemp") {
  42.             temp = value.split(separator: "=")[1]
  43.         }
  44.  
  45.         if let modeValue = value.split(separator: "=").last, let modeEnum = modeMap[String(modeValue)] {
  46.             mode = modeEnum
  47.         }
  48.  
  49.         if let powerValue = value.split(separator: "=").last, let powerEnum = powerMap[String(powerValue)] {
  50.             power = powerEnum
  51.         }
  52.  
  53.         if let fanValue = value.split(separator: "=").last, let fanEnum = fanMap[String(fanValue)] {
  54.             fanSpeed = fanEnum
  55.         }
  56.     }
  57.  
  58.     print(StatusStruct(temp: temp, mode: mode, fanSpeed: fanSpeed, power: power))
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement