Blame view

Twear/Home/Model/WeatherModel.swift 5.58 KB
75d24c15   yangbin   123
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  //
  //  WeatherModel.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/8.
  //
  
  import UIKit
  import HandyJSON
  import SwiftDate
  
  enum WeatherType: Int, HandyJSONEnum {
      case sunny = 0
      case overcast = 1
      case rain = 2
      case snow = 3
  }
  
  class WeatherModel: NSObject, HandyJSON {
      var city: String = ""
      var highest: Int = -100
      var lowest: Int = -100
      var type: WeatherType = .sunny
      var date: Date?
      
      var text: String = ""
      var icon: String {
          get {
  //            switch type {
  //            case .sunny:
  //                return "weather_sunny"
  //            case .overcast:
  //                return "weather_cloudy"
  //            case .rain:
  //                return "weather_rain"
  //            case .snow:
  //                return "weather_snow"
  //            }
              
              switch text {
              case "晴", "少云", "晴间多云":
                  return "weather_sunny"
              case "阴", "多云":
                  return "weather_overcast"
              case "阵雨", "强阵雨", "雷阵雨", "强雷阵雨", "雷阵雨伴有冰雹", "小雨", "中雨", "大雨", "极端降雨", "毛毛雨/细雨", "细雨", "毛毛雨", "暴雨", "大暴雨", "特大暴雨", "冻雨", "小到中雨", "中到大雨", "大到暴雨", "暴雨到大暴雨", "大暴雨到特大暴雨", "雨":
                  return "weather_rain"
              case "小雪", "中雪", "大雪", "暴雪", "雨夹雪", "雨雪天气", "阵雨夹雪", "阵雪", "小到中雪", "中到大雪", "大到暴雪", "雪":
                  return "weather_snow"
              default:
                  return "weather_snow"
              }
               
          }
      }
      
  
      
      init(city: String, highest: Int, lowest: Int, type: WeatherType) {
          super.init()
          self.city = city
          self.highest = highest
          self.lowest = lowest
          self.type = type
      }
      
      init(city: String, tempMin: String, tempMax: String, textDay: String) {
          super.init()
          self.city = city
          self.highest = Int(tempMax) ?? 0
          self.lowest = Int(tempMin) ?? 0
          self.text = textDay
          switch textDay {
          case "晴", "少云", "晴间多云":
              self.type = .sunny
              self.text = "晴"
          case "阴", "多云":
              self.type = .overcast
              self.text = "多云"
          case "阵雨", "强阵雨", "雷阵雨", "强雷阵雨", "雷阵雨伴有冰雹", "小雨", "中雨", "大雨", "极端降雨", "毛毛雨/细雨", "细雨", "毛毛雨", "暴雨", "大暴雨", "特大暴雨", "冻雨", "小到中雨", "中到大雨", "大到暴雨", "暴雨到大暴雨", "大暴雨到特大暴雨", "雨":
              self.type = .rain
              self.text = "雨"
          case "小雪", "中雪", "大雪", "暴雪", "雨夹雪", "雨雪天气", "阵雨夹雪", "阵雪", "小到中雪", "中到大雪", "大到暴雪", "雪":
              self.text = "雪"
              self.type = .snow
          default:
              self.text = "阴"
              self.type = .overcast
          }
          
      }
      
      class func dailyToWeather(_ array: [Daily], city: String) -> [WeatherModel] {
          var weatherArray: [WeatherModel] = []
          for daily in array {
              weatherArray.append(WeatherModel(city: city, tempMin: daily.tempMin, tempMax: daily.tempMax, textDay: daily.textDay))
          }
          return weatherArray
      }
      
      
      func toBytes() -> [UInt8] {
          switch CurDevice.platform {
          case ._818, ._818_hq7:
              var min = self.lowest
              if min < 0 {
                  min = 128 - min
              }
              var max = self.highest
              if max < 0 {
                  max = 128 - max
              }
              let weatherBytes: [UInt8] = [UInt8(min), UInt8(max), UInt8(self.type.rawValue)]
              return weatherBytes
          case ._816:
              let cityData: Data = self.city.data(using: .utf8) ?? Data()
              var min = self.lowest
              if min < 0 {
                  min = 128 - min
              }
              var max = self.highest
              if max < 0 {
                  max = 128 - max
              }
              var weatherBytes: [UInt8] = [UInt8(min), UInt8(max), UInt8(self.type.rawValue), UInt8(cityData.count)]
              weatherBytes.append(contentsOf: [UInt8](cityData))
              print(weatherBytes)
              return weatherBytes
          default:
              return []
          }
  
      }
      
      func mapping(mapper: HelpingMapper) {
          mapper.specify(property: &lowest, name: "tempMin")
          mapper.specify(property: &highest, name: "tempMax")
          mapper.specify(property: &type, name: "textDay") { rawValue -> WeatherType in
              switch rawValue {
              case "晴", "多云", "少云", "晴间多云":
                  return .sunny
              case "阴":
                  return .overcast
              case "阵雨", "强阵雨", "雷阵雨", "强雷阵雨", "雷阵雨伴有冰雹", "小雨", "中雨", "大雨", "极端降雨", "毛毛雨/细雨", "细雨", "毛毛雨", "暴雨", "大暴雨", "特大暴雨", "冻雨", "小到中雨", "中到大雨", "大到暴雨", "暴雨到大暴雨", "大暴雨到特大暴雨", "雨":
                  return .rain
              case "小雪", "中雪", "大雪", "暴雪", "雨夹雪", "雨雪天气", "阵雨夹雪", "阵雪", "小到中雪", "中到大雪", "大到暴雪", "雪":
                  return .snow
              default:
                  return .overcast
              }
          }
          
          mapper <<<
              date <-- CustomDateFormatTransform(formatString: "yyyy-MM-dd HH:mm")
      }
      
      required override init() { }
      
  }