WeatherModel.swift 5.58 KB
//
//  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() { }
    
}