Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HomeKit 2020

tokorom
September 19, 2020

HomeKit 2020

iOSDC Japan 2020 9/19 TrackA のセッション

tokorom

September 19, 2020
Tweet

More Decks by tokorom

Other Decks in Programming

Transcript

  1. ॴ༑ଠ:VUB5PLPSP J04UW04"QQ1SPHSBNNFS
    )PNF,JU
    J04%$+BQBO

    View full-size slide

  2. @tokorom tokorom
    Spinners Inc.
    ॴ ༑ଠ / Yuta Tokoro

    View full-size slide

  3. )PNF,JU֓ཁ
    )PNF,JUͱ͸

    View full-size slide

  4. )PNF,JU֓ཁ
    )PNF,JUશମ૾
    w J1IPOFͳͲͷ"QQMF੡඼͔Β༷ʑͳσόΠε
    Λ؅ཧɾίϯτϩʔϧ͢ΔͨΊͷ࿮૊Έ
    )PNF,JU

    View full-size slide

  5. )PNF,JU֓ཁ
    )PNF,JU'SBNFXPSL
    w αʔυύʔςΟ੡ΞϓϦ͕ରԠ੡඼Λίϯτ
    ϩʔϧ͢ΔͨΊͷ'SBNFXPSL
    )PNF,JU
    'SBNFXPSL

    View full-size slide

  6. )PNF,JU֓ཁ
    )PNF,JU"%,
    w )PNF,JUʹରԠσόΠεΛ։ൃ͢ΔͨΊͷΩ
    οτ
    )PNF,JU
    "%,

    View full-size slide

  7. )PNF,JU֓ཁ
    )"1
    w "QQMF੡඼ͱ)PNF,JUରԠσόΠε͕૬ޓʹ
    ௨৴͢ΔͨΊͷϓϩτίϧ
    )PNF,JU
    "DDFTTPSZ
    1SPUPDPM
    )"1

    View full-size slide

  8. "HFOEB
    w )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    w )PNF,JUͷ#SJEHFʹ͍ͭͯ
    w )PNFCSJEHFͰ)PNF,JUະରԠ੡඼Λ)PNF,JUରԠ
    w )PNF,JU"%,Ͱ࡞Δࣗ࡞ΞΫηαϦ

    View full-size slide

  9. "HFOEB
    w )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    w )PNF,JUͷ#SJEHFʹ͍ͭͯ
    w )PNFCSJEHFͰ)PNF,JUະରԠ੡඼Λ)PNF,JUରԠ
    w )PNF,JU"%,Ͱ࡞Δࣗ࡞ΞΫηαϦ

    View full-size slide

  10. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    )PNF,JU'SBNFXPSLͰͰ͖Δ͜ͱ

    View full-size slide

  11. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    )PNF,JU'SBNFXPSLͰͰ͖Δ͜ͱ

    View full-size slide

  12. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    )PNF,JU'SBNFXPSLͰͰ͖Δ͜ͱ
    w ϗʔϜ΍ϧʔϜͳͲͷάϧʔϐϯάͷ؅ཧ
    w σόΠεͷ؅ཧͱૢ࡞
    w σόΠεΛҰ౓ʹૢ࡞͢Δγʔϯͷ؅ཧ
    w ΦʔτϝʔγϣϯͷͨΊͷτϦΨʔͷ؅ཧ
    w ϗʔϜʹΞΫηεͰ͖ΔϢʔβʔͷ؅ཧ
    w ϗʔϜ΍σόΠεͷมߋݕ஌

    View full-size slide

  13. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    ؅ཧཁૉͷߏ੒

    View full-size slide

  14. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    )PNF,JUͷߏ੒

    View full-size slide

  15. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    ిٿΛ఺౮ͤ͞Δίʔυ
    let power = home
    .servicesWithTypes([HMServiceTypeLightbulb])?
    .first?
    .characteristics
    .filter { $0.characteristicType == HMCharacteristicTypePowerState }
    .first
    power.writeValue(true) { error in
    }

    View full-size slide

  16. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    αϙʔτ͍ͯ͠ΔΩϟϥΫλʔ
    let power = home
    .servicesWithTypes([HMServiceTypeLightbulb])?
    .first?
    .characteristics
    .filter { $0.characteristicType == HMCharacteristicTypePowerState }
    .first
    power.writeValue(true) { error in
    }

    View full-size slide

  17. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    αϙʔτ͍ͯ͠ΔΩϟϥΫλʔ

    View full-size slide

  18. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    Ӆ͠ΩϟϥΫλʔΛࢀর͢Δ
    &MHBUP&WF8FBUIFS8JSFMFTT0VUEPPS4FOTPS

    View full-size slide

  19. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    Ӆ͠ΩϟϥΫλʔΛࢀর͢Δ
    w ؾԹ
    w ࣪౓
    w ؾѹ)PNF,JUະαϙʔτͷΩϟϥΫλʔʂ

    View full-size slide

  20. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    Ӆ͠ΩϟϥΫλʔΛࢀর͢Δ
    let power = home
    .servicesWithTypes([HMServiceTypeLightbulb])?
    .first?
    .characteristics
    .filter { $0.characteristicType == HMCharacteristicTypePowerState }
    .first
    power.writeValue(true) { error in
    }

    View full-size slide

  21. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    Ӆ͠ΩϟϥΫλʔΛࢀর͢Δ
    let airPressure = home
    .servicesWithTypes(["E863F00A-079E-48FF-8F27-9C2605A29F52"])?
    .first?
    .characteristics
    .filter { $0.characteristicType == "E863F10F-079E-48FF-8F27-9C2605A29F52" }
    .first
    airPressure.readValue { error in
    let value = airPressure.value as? Float
    }

    View full-size slide

  22. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    Ӆ͠ΩϟϥΫλʔΛࢀর͢Δ
    for characteristic in service.characteristics {
    print("ID: \(characteristic.characteristicType)”)
    print("આ໌: \(characteristic.metadata?.manufacturerDescription ?? "")")
    }

    View full-size slide

  23. )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    ).$BNFSB7JFX

    View full-size slide

  24. )PNF,JUͰͰ͖Δ͜ͱ۩ମྫ
    ଞϓϥοτϑΥʔϜΑΓࡉ΍͔ͳઃఆ
    w ΫϦεϚεϥΠτΛʮ೔ͷೖʯʹ఺౮͢Δ
    w Ոʹ୭΋͍ͳ͘ͳͬͨΒશͯͷిؾΛফ౮ͯ͠ώʔλʔΛࢭΊΔ
    w ࣗಈ఺౮ͨ͠ϥΠτΛඵޙʹফ౮͢Δ
    w ೔தͰτΠϨ͕े෼໌Δ͚Ε͹ࣗಈ఺౮͠ͳ͍

    View full-size slide

  25. "HFOEB
    w )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    w )PNF,JUͷ#SJEHFʹ͍ͭͯ
    w )PNFCSJEHFͰ)PNF,JUະରԠ੡඼Λ)PNF,JUରԠ
    w )PNF,JU"%,Ͱ࡞Δࣗ࡞ΞΫηαϦ

    View full-size slide

  26. )PNF,JUͷ#SJEHFʹ͍ͭͯ
    )VFͷྫ

    View full-size slide

  27. )PNF,JUͷ#SJEHFʹ͍ͭͯ
    )VFͷྫ
    )PNF,JUະରԠʂʁ

    View full-size slide

  28. )PNF,JUͷ#SJEHFʹ͍ͭͯ
    )VFͷྫ
    )VF#SJEHF

    View full-size slide

  29. )PNF,JUͷ#SJEHFʹ͍ͭͯ
    )VFͷྫ
    )VF#SJEHF

    View full-size slide

  30. )PNF,JUͷ#SJEHFʹ͍ͭͯ
    )PNFCSJEHF
    IUUQTIPNFCSJEHFJP

    View full-size slide

  31. )PNF,JUͷ#SJEHFʹ͍ͭͯ
    )VFͷྫ
    )PNFCSJEHF
    "
    #
    $ %

    View full-size slide

  32. "HFOEB
    w )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    w )PNF,JUͷ#SJEHFʹ͍ͭͯ
    w )PNFCSJEHFͰ)PNF,JUະରԠ੡඼Λ)PNF,JUରԠ
    w )PNF,JU"%,Ͱ࡞Δࣗ࡞ΞΫηαϦ

    View full-size slide

  33. )PNFCSJEHF
    ରԠσόΠεͷ۩ମྫ
    ϧϯό
    #SPBEMJOL3.NJOJ
    ʢ੺֎ઢϦϞίϯʣ
    4&4".&
    ʢεϚʔτϩοΫʣ
    Χϝϥ

    View full-size slide

  34. )PNFCSJEHF
    ࣗ୐Ͱ)PNFCSJEHFΛ࢖͏ʹ͸
    "
    #
    $ %

    View full-size slide

  35. )PNFCSJEHF
    ࣗ୐Ͱ)PNFCSJEHFΛ࢖͏ʹ͸
    "
    #
    $ %
    IUUQTIPNFCSJEHFJP

    View full-size slide

  36. )PNFCSJEHF
    )PNFCSJEHFΛ)PNFʹ௥Ճ

    View full-size slide

  37. )PNFCSJEHF
    ϓϥάΠϯΛࣗ࡞
    IUUQTHJUIVCDPNIPNFCSJEHFIPNFCSJEHFQMVHJOUFNQMBUF

    View full-size slide

  38. "HFOEB
    w )PNF,JU'SBNFXPSLͬ͘͟Γೖ໳
    w )PNF,JUͷ#SJEHFʹ͍ͭͯ
    w )PNFCSJEHFͰ)PNF,JUະରԠ੡඼Λ)PNF,JUରԠ
    w )PNF,JU"%,Ͱ࡞Δࣗ࡞ΞΫηαϦ

    View full-size slide

  39. )PNF,JU"%,
    ֓ཁ
    w )PNF,JU"DDFTTPSZ%FWFMPQNFOU,JU
    w ΞΫηαϦʔϝʔΧʔ͕)PNF,JUରԠσόΠεΛ։ൃ͢ΔͨΊͷΩοτ
    w ೥݄ʹΦʔϓϯιʔε൛͕ϦϦʔε
    w झຯͰͳΒࣗ༝ʹ։ൃͯ͠0,

    View full-size slide

  40. )PNF,JU"%,
    )PNFCSJEHFͱͷҧ͍
    )PNFCSJEHF
    #
    $ %
    "

    View full-size slide

  41. )PNF,JU"%,
    )PNFCSJEHFͱͷҧ͍
    "%,
    #
    $ %
    "
    "%,
    "%,
    "%,

    View full-size slide

  42. )PNF,JU"%,
    ࣗ෼ͰΞΫηαϦʔΛ࡞Δ
    IUUQTHJUIVCDPNBQQMF)PNF,JU"%,

    View full-size slide

  43. )PNF,JU"%,
    ࣗ෼ͰΞΫηαϦʔΛ࡞Δ
    $ brew install [υΩϡϝϯτʹॻ͔Ε͍ͯΔ΋ͷ͍͔ͭ͘]
    $ make all
    $ ./Output/লུ/Debug/IP/Applications/Lightbulb.OpenSSL

    View full-size slide

  44. )PNF,JU"%,
    ࣗ෼ͰΞΫηαϦʔΛ࡞Δ
    Info [com.apple.mfi.HomeKit.Core:AccessoryServer] Accessory server starting.

    View full-size slide

  45. )PNF,JU"%,
    ࣗ෼ͰΞΫηαϦʔΛ࡞Δ

    View full-size slide

  46. )PNF,JU"%,
    ࣗ෼ͰΞΫηαϦʔΛ࡞Δ
    Info HandleLightBulbOnWrite: true
    ...
    Info HandleLightBulbOnWrite: false

    View full-size slide

  47. )PNF,JU"%,
    ࣗ෼ͰΞΫηαϦʔΛ࡞Δ
    lightbulb_slack/Applications/Lightbulb/App.c
    #include
    HAPError HandleLightBulbOnWrite(
    // ...
    if (value) {
    system("./handleLightBulbOn");
    }
    // ...
    }

    View full-size slide

  48. )PNF,JU"%,
    ࣗ෼ͰΞΫηαϦʔΛ࡞Δ
    #!/usr/bin/swift
    import Foundation
    let slackURL = URL(string: "Incoming Webhook URL")
    var request = URLRequest(url: slackURL!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-type")
    let message = "ϥΠτ͕఺౮"
    let postData = "{\"text\": \"\(message)\"}".data(using: .utf8)
    request.httpBody = postData
    let semaphore = DispatchSemaphore(value: 0)
    let session = URLSession(configuration: URLSessionConfiguration.default)
    let task = session.dataTask(with: request) { _, _, _ in
    semaphore.signal()
    }
    task.resume()
    semaphore.wait()

    View full-size slide

  49. )PNF,JU"%,
    ࣗ෼ͰΞΫηαϦʔΛ࡞Δ

    View full-size slide

  50. .PSF*OGPSNBUJPO
    w J041SPHSBNNJOH
    w Ξοϓϧͷ)PNF1PEͱ)PNF,JUٕज़ຊ
    IUUQTQFBLTDDCPPLTJ04
    IUUQTCPPUIQNKBJUFNT

    View full-size slide