UserInfo.swift 12.5 KB
//
//  UserInfo.swift
//  Twear
//
//  Created by yangbin on 2021/12/8.
//

import UIKit
import HandyJSON
import SwiftDate

class AdminModel: NSObject, HandyJSON {
    var userInfo: UserInfoModel = UserInfoModel()
    var device: DeviceModel = DeviceModel()
    required override init() { }
}


enum Platform: String, HandyJSONEnum {
    case _816 = "816"
    case _818 = "818"
    case _828 = "828"
    case other = "other"
}

class RemindModel: NSObject, HandyJSON {
    var isOn: Bool = false
    var startDate: Date = Date()
    var endDate: Date = Date()
    var interval: Int = 0
    var cycle: Int = 127
    
    init(isOn: Bool, startDate: Date, endDate: Date) {
        super.init()
        self.isOn = isOn
        self.startDate = startDate
        self.endDate = endDate
    }
    
    init(type: String) {
        super.init()
        self.isOn = false
        switch type {
        case "disturb":
            self.startDate = DateInRegion(year: 2000, month: 1, day: 1, hour: 22, minute: 0).date
            self.endDate = DateInRegion(year: 2000, month: 1, day: 1, hour: 22, minute: 0).date
        case "sedentary", "drink":
            self.startDate = DateInRegion(year: 2000, month: 1, day: 1, hour: 9, minute: 0).date
            self.endDate = DateInRegion(year: 2000, month: 1, day: 1, hour: 23, minute: 00).date
            self.interval = 45
        default:
            break
        }
    }
    override func isEqual(_ object: Any?) -> Bool {
        if let otherFoo = object as? RemindModel {
            return self.isOn == otherFoo.isOn && self.startDate == otherFoo.startDate && self.endDate == otherFoo.endDate && self.interval == otherFoo.interval && self.cycle == otherFoo.cycle
        }
        return false
    }
    
    
    class func toRemindModel(_ bytes: [UInt8]) -> RemindModel {
        let remind = RemindModel()
        remind.isOn = bytes[0] == 1
        if bytes.count == 5 {
            remind.startDate = DateInRegion(year: 2000, month: 1, day: 1, hour: Int(bytes[1]), minute: Int(bytes[2])).date
            remind.endDate = DateInRegion(year: 2000, month: 1, day: 1, hour: Int(bytes[3]), minute: Int(bytes[4])).date
        } else if bytes.count == 6 {
            remind.startDate = DateInRegion(year: 2000, month: 1, day: 1, hour: Int(bytes[1]), minute: 0).date
            remind.endDate = DateInRegion(year: 2000, month: 1, day: 1, hour: Int(bytes[2]), minute: 0).date
            remind.cycle = Int(bytes[3])
            remind.interval = bytesToInt(Array(bytes[4..<bytes.count]))
        } else if bytes.count == 8 {
            remind.startDate = DateInRegion(year: 2000, month: 1, day: 1, hour: Int(bytes[1]), minute: Int(bytes[2])).date
            remind.endDate = DateInRegion(year: 2000, month: 1, day: 1, hour: Int(bytes[3]), minute: Int(bytes[4])).date
            remind.cycle = Int(bytes[5])
            remind.interval = bytesToInt(Array(bytes[6..<bytes.count]))
        }
        return remind
    }
    
    func mapping(mapper: HelpingMapper) {
        mapper <<<
            startDate <-- CustomDateFormatTransform(formatString: "HH:mm")
        mapper <<<
            endDate <-- CustomDateFormatTransform(formatString: "HH:mm")
    }
    
    required override init() { }

}

class AlarmClockModel: NSObject, HandyJSON {
    var date: Date = Date()
    var isOn: Bool = false
    var cycle: Int = 0
    var remark: String = ""
    var cycleStr: String? {
        get {
            let array = UInt8(cycle).toAlarmClockCycle()
            if array[0] == 1 {
                return LocString("不重复")
            } else {
                var str = ""
//                array.remove(at: 0)
                for i in 1..<8 {
                    if array[i] == 1 {
                        switch i {
                        case 1:
                            str += " \(LocString("周一"))"
                        case 2:
                            str += " \(LocString("周二"))"
                        case 3:
                            str += " \(LocString("周三"))"
                        case 4:
                            str += " \(LocString("周四"))"
                        case 5:
                            str += " \(LocString("周五"))"
                        case 6:
                            str += " \(LocString("周六"))"
                        case 7:
                            str += " \(LocString("周日"))"
                        default:
                            break
                        }
                    }
                }
                if str != "" {
                    str = str.substring(fromIndex: 1)
                }
                return str
            }
        }
    }
    
    class func toAlarmClock(_ bytes: [UInt8]) -> AlarmClockModel? {
        let alarmClock = AlarmClockModel()
        if Int(bytes[3]) == 0 {
            return nil
        }
        alarmClock.date = DateInRegion(year: 2000, month: 1, day: 1, hour:  Int(bytes[0]), minute: Int(bytes[1])).date
        alarmClock.cycle = Int(bytes[2])
        alarmClock.isOn = Int(bytes[4]) == 1
        return alarmClock
    }
    
    init(date: Date, isOn: Bool, cycle: Int) {
        self.date = date
        self.isOn = isOn
        self.cycle = cycle
    }
    
    func mapping(mapper: HelpingMapper) {
        mapper <<<
            date <-- CustomDateFormatTransform(formatString: "HH:mm")
    }
    
    
    func toBytes() -> [UInt8] {
        let bytes: [UInt8] = [UInt8(self.date.hour), UInt8(self.date.minute), UInt8(self.cycle), 0x01, isOn ? 0x01 : 0x00]
        print("闹钟:\(bytes)")
        return [UInt8(self.date.hour), UInt8(self.date.minute), UInt8(self.cycle), 0x01, isOn ? 0x01 : 0x00]
    }
    
    
    
    required override init() { }
}


class MessagePushModel: NSObject, HandyJSON {
    var value: Int {
        get {
            return bitsToInt([other ? 1 : 0, facebook ? 1 : 0, ins ? 1 : 0, linkedin ? 1 : 0, twitter ? 1 : 0, messenger ? 1 : 0, whatsapp ? 1 : 0, wechat ? 1 : 0, qq ? 1 : 0, sms ? 1 : 0, call ? 1 : 0])
        }
    }

    var call: Bool = false
    var sms: Bool = false
    var qq: Bool = false
    var wechat: Bool = false
    var whatsapp: Bool = false
    var messenger: Bool = false
    var twitter: Bool = false
    var linkedin: Bool = false
    var ins: Bool = false
    var facebook: Bool = false
    var other: Bool = false
    
    var app: Bool {
        get {
            return qq || wechat || whatsapp || messenger || twitter || linkedin || ins || facebook || other
        }
    }
    
    
    private func bitsToInt(_ bits: [Int]) -> Int {
        let decimalValue = bits.reduce(0) { v, byte in
            return v << 1 | Int(byte)
        }
        print(decimalValue)
        return Int(decimalValue)
    }
    
    required override init() { }
}

class MenstrualModel: NSObject, HandyJSON {
    var days: Int = 0
    var cycle: Int = 0
    var lastDate: Date?
//    var calendar: MenstrualCalendarModel = MenstrualCalendarModel()
    
    init(days: Int, cycle: Int, lastDate: Date) {
        super.init()
        self.days = days
        self.cycle = cycle
        self.lastDate = lastDate
    }
    
    func mapping(mapper: HelpingMapper) {
        mapper <<<
            lastDate <-- CustomDateFormatTransform(formatString: "yyyy-MM-dd")
    }
    
    required override init() { }
}

class DeviceModel: NSObject, HandyJSON {
    var uuid: String = ""
    var name: String = ""
//    var typeRaw: String = "other"
    var isSync: Bool = false
    var isConnected: Bool = false
    var mac: String = ""
    var push: MessagePushModel = MessagePushModel()
    var distanceUnit: Int = 0
    var temperatureUnit: Int = 0
    var timeFormat: Int = 0
    var wrist: Bool = false
    var disturb: RemindModel = RemindModel(type: "disturb")
    var drink: RemindModel = RemindModel(type: "drink")
    var sedentary: RemindModel = RemindModel(type: "sedentary")
    var alarmClocks: [AlarmClockModel] = []
    
    var type: String = ""
    var languageRaw: String = "system"
    var version: String = ""
    var allFunctions: [String] = ["BloodPressure", "BloodOxygen", "HeartRate", "Sleep", "Train",  "WomenHealth", "MotionRecord"]
    var homePage: [String] = ["BloodPressure", "BloodOxygen", "HeartRate", "Sleep", "Train",  "WomenHealth", "MotionRecord"]
    var contacts: [ContactModel] = []
    var queryArray: [String] {
        get {
            var array = allFunctions
            if let i = array.firstIndex(of: "MotionRecord") {
                array.remove(at: i)
            }
            if let i = array.firstIndex(of: "WomenHealth") {
                array.remove(at: i)
            }
            if platform == ._818 ||  platform == ._828 {
                if let i = array.firstIndex(of: "BloodPressure") {
                    array.remove(at: i)
                }
                if let i = array.firstIndex(of: "BloodOxygen") {
                    array.remove(at: i)
                }
            }
            array.append(contentsOf: ["Step", "Setting"])
            return array
        }
    }

    var platform: Platform = .other

    init(uuid: String, name: String, platform: Platform, mac: String) {
        super.init()
        self.uuid = uuid
        self.name = name
        self.platform = platform
        self.mac = mac
        switch platform {
        case ._816:
            self.allFunctions = ["BloodPressure", "BloodOxygen", "HeartRate", "Sleep", "Train",  "MotionRecord"]
            self.homePage = ["BloodPressure", "BloodOxygen", "HeartRate", "Sleep", "Train",  "MotionRecord"]
//            self.queryArray = ["BloodPressure", "BloodOxygen", "HeartRate", "Sleep", "Train", "Step", "Setting"]
        case ._818:
            self.allFunctions = ["BloodPressure", "HeartRate", "Sleep", "Train", "MotionRecord", "WomenHealth"]
            self.homePage = ["BloodPressure", "HeartRate", "Sleep", "Train", "MotionRecord", "WomenHealth"]
        case ._828:
            self.allFunctions = ["BloodPressure", "BloodOxygen", "HeartRate", "Sleep", "Train", "MotionRecord", "WomenHealth"]
            self.homePage = ["BloodPressure", "BloodOxygen", "HeartRate", "Sleep", "Train", "MotionRecord", "WomenHealth"]
//            self.queryArray = ["BloodPressure", "Step", "HeartRate", "Sleep", "Train", "Setting"]
        case .other:
            break
        }
    }
    
    required override init() { }
}

class ContactModel: NSObject, HandyJSON {
    var name: String = ""
    var number: String = ""
    var isSOS: Bool = false
    var isSelected: Bool = false
    
    init(name: String, number: String) {
        super.init()
        self.name = name
        self.number = number
    }
    
    func toBytes(_ index: Int) -> [UInt8] {
        var bytes: [UInt8] = [UInt8](repeating: 0x00, count: 54)
        if name == "" && number == "" {
            return bytes
        }
        var phoneNumber = number
        phoneNumber = phoneNumber.replacingOccurrences(of: " ", with: "")
        
        for (i, scalar) in name.unicodeScalars.enumerated() {
            if i < 10 {
                bytes[i*2+30] = UInt8(scalar.value & 0xFF)
                bytes[i*2+31] = UInt8(scalar.value >> 8)
            }
        }
        //54, 65, 108, 101, 110
//        let num = number.replacingOccurrences(of: " ", with: "")
        for (i, scalar) in phoneNumber.unicodeScalars.enumerated() {
            if i < 13 {
                bytes[i*2] = UInt8(scalar.value & 0xFF)
                bytes[i*2+1] = UInt8(scalar.value >> 8)

            }
           
        }
        bytes[50] = UInt8(phoneNumber.count)
        bytes[51] = UInt8(name.count)
        bytes[52] = UInt8(index)
        bytes[53] = isSOS ? 0x01 : 0x00
        return bytes
//        l2KeyValue[startIndex+50]=(byte)numberLength;
       //            l2KeyValue[startIndex+51]=(byte)nameLength;
       //            l2KeyValue[startIndex+52]=(byte)(i+1);
       //            l2KeyValue[startIndex+53]=(byte)(sos[i]);
    }
    
    required override init() { }
    
}

class UserInfoModel: NSObject, HandyJSON {
    var name: String = ""
    var gender: Int = 1 // 1男 0女
    var height: Int = 170 //cm
    var weight: Int = 65 //kg
    var age: Int = 24
    var phone: String = ""
    var userId: Int = Int(arc4random_uniform(89999999) + 10000000)
    var stepsGoal: Int = 8000
    var birth: Date = (DateInRegion().date - 24.years).dateAtStartOf(.year)
    var menstrual: MenstrualModel = MenstrualModel()
    var contacts: [ContactModel] = []
//    var avatarUrl: String = "" //头像
    
    func mapping(mapper: HelpingMapper) {
        mapper <<<
            birth <-- CustomDateFormatTransform(formatString: "yyyy-MM-dd")
    }
    
    
    required override init() { }
}