Blame view

HDFwear/Home/Model/WeatherModel.swift 7.05 KB
f2cf74c7   yangbin   1.0.20(4)
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
  //
  //  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 AMapWeatherModel: NSObject, HandyJSON {
      var date: Date?
      var nighttemp: Int = 0
      var daytemp: Int = 0
      var dayweather: String = ""
      required override init() { }
  }
  
  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_cloudy"
              case "阵雨", "强阵雨", "雷阵雨", "强雷阵雨", "雷阵雨伴有冰雹", "小雨", "中雨", "大雨", "极端降雨", "毛毛雨/细雨", "细雨", "毛毛雨", "暴雨", "大暴雨", "特大暴雨", "冻雨", "小到中雨", "中到大雨", "大到暴雨", "暴雨到大暴雨", "大暴雨到特大暴雨", "雨":
                  return "weather_rain"
              case "小雪", "中雪", "大雪", "暴雪", "雨夹雪", "雨雪天气", "阵雨夹雪", "阵雪", "小到中雪", "中到大雪", "大到暴雪", "雪":
                  return "weather_snow"
              default:
                  return "weather_sunny"
              }
              
  
              
  
              
  
              
  
              
               
          }
      }
      
  
      
      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 = .sunny
          }
          
      }
      
      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
      }
      
      class func aMapWeather(_ array: [AMapWeatherModel], city: String) -> [WeatherModel] {
          var weatherArray: [WeatherModel] = []
          for daily in array {
              weatherArray.append(WeatherModel(city: city, tempMin: "\(daily.nighttemp)", tempMax: "\(daily.daytemp)", textDay: daily.dayweather))
          }
          return weatherArray
      }
      
      
      func toBytes() -> [UInt8] {
          var weatherType: WeatherType = .sunny
          switch text {
          case "雨":
              weatherType = .rain
          case "雪":
              weatherType = .snow
          case "晴":
              weatherType = .sunny
          case "多云":
              weatherType = .overcast
          default:
              break
          }
          switch CurDevice.platform {
          case ._828:
              var min = self.lowest
              if min < 0 {
                  min = 256 + min
              }
              var max = self.highest
              if max < 0 {
                  max = 256 + max
              }
f2cf74c7   yangbin   1.0.20(4)
151
152
              let weatherBytes: [UInt8] = [UInt8(min), UInt8(max), UInt8(weatherType.rawValue)]
              return weatherBytes
53fae286   daifengyi   feat:remove platf...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  //        case ._818:
  //            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(weatherType.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(weatherType.rawValue), UInt8(cityData.count)]
  //            weatherBytes.append(contentsOf: [UInt8](cityData))
  //            print(weatherBytes)
  //            return weatherBytes
f2cf74c7   yangbin   1.0.20(4)
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
          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() { }
      
  }