आपके पास अपनी कस्टम ट्रेट के लिए सहायता जोड़ने का विकल्प होता है. ये ट्रेट, स्टैंडर्ड ट्रेट के मुकाबले ज़्यादा सुविधाएं देती हैं. इस तरह की कस्टम विशेषता को मैन्युफ़ैक्चरर के हिसाब से तय की गई विशेषता या एमएस विशेषता कहा जाता है. एमएस ट्रेट को स्टैंडर्ड .matter आईडीएल फ़ॉर्मैट में तय किया जाता है. इसके बाद, इसे iOS मॉड्यूल में बदला जाता है. इस मॉड्यूल को आपके ऐप्लिकेशन में इंपोर्ट किया जा सकता है.
iOS Home APIs SDK में एक कोड जनरेटर शामिल होता है, जो इस कन्वर्ज़न को पूरा कर सकता है. इसके अलावा, अगर ज़रूरत हो, तो इस कोड जनरेटर का इस्तेमाल करके, अस्थायी तौर पर उपलब्ध सुविधाओं को भी जनरेट किया जा सकता है.
ज़रूरी शर्तें
कोड जनरेटर का इस्तेमाल करने के लिए, आपको इनकी ज़रूरत होगी:
- Python 3.10 या उसके बाद का वर्शन.
- आपकी एमएस की विशेषताओं की परिभाषा वाली
.matterआईडीएल फ़ाइल. इस फ़ाइल में सिर्फ़client clusterकी परिभाषाएं होनी चाहिए. इसे मैन्युअल तरीके से बनाया जा सकता है. इसके अलावा, Matterएसडीके बिल्ड प्रोसेस के तहत जनरेट किए गए यूआईडी का इस्तेमाल भी किया जा सकता है.
Matter कोड जनरेट करने वाले टूल के लिए, Python की कुछ ज़रूरी शर्तें हैं. इन्हें इंस्टॉल करना ज़रूरी है. यह एक बार किया जाने वाला चरण है. इसे एसडीके का नया वर्शन इंटिग्रेट करने के बाद किया जाना चाहिए, ताकि यह पक्का किया जा सके कि टूल को Xcode सैंडबॉक्स में चलाया जा सकता है.
swift package plugin --allow-network-connections "all" --allow-writing-to-package-directory matter-codegen-initआईडीएल फ़ॉर्मैट के बारे में ज़्यादा जानकारी के लिए, GitHub पर matter/idl देखें. वहां मौजूद /tests/inputs डायरेक्ट्री में, आईडीएल फ़ाइलों के कई सैंपल दिए गए हैं. सभी Matter क्लस्टर के लिए पूरी आईडीएल फ़ाइल, controller-clusters.matter पर देखी जा सकती है. यह सभी प्लैटफ़ॉर्म पर जनरेट की गई फ़ाइलों का सोर्स है. इसमें Home API के लिए iOS मॉड्यूल भी शामिल हैं.
Swift फ़ाइलें जनरेट करना
कोड जनरेटर, एसडीके के साथ बंडल किया जाता है और इसे Swift Package Manager (SwiftPM) में इंटिग्रेट किया जाता है. कोड जनरेटर को XCode, SwiftPM प्लगिन के तौर पर शुरू करता है. हालांकि, आपके प्रोजेक्ट के लिए पैकेज मैनेजमेंट के लिए SwiftPM का इस्तेमाल करना ज़रूरी नहीं है.
- एसडीके को अपने प्रोजेक्ट में इंटिग्रेट करें. निर्देशों के लिए, iOS SDK टूल सेट अप करना लेख पढ़ें.
- प्लगिन सेट अप करें. प्लगिन सैंडबॉक्स में चलता है. इसलिए, आपको कुछ डिपेंडेंसी इंस्टॉल करनी होंगी:
swift package plugin matter-codegen-init \ --allow-network-connections all \ --allow-writing-to-package-directory जनरेट किए गए कोड के लिए कोई नेमस्पेस तय करें और उसे आईडीएल फ़ाइल में
pragma swiftके तौर पर जोड़ें. उदाहरण के लिए,MyCompany:// pragma kotlin(package=com.mycompany.matter.cluster, generate_namespace=true) // pragma swift(package=MyCompany, generate_namespace=true) client cluster SimpleCustom = 4294048768 { attribute int16u clusterAttr = 1; // Global Attributes readonly attribute command_id generatedCommandList[] = 65528; readonly attribute command_id acceptedCommandList[] = 65529; readonly attribute event_id eventList[] = 65530; readonly attribute attrib_id attributeList[] = 65531; readonly attribute bitmap32 featureMap = 65532; readonly attribute int16u clusterRevision = 65533; }जनरेटर चलाएं:
swift package plugin matter-codegen Clusters/MyCustomCluster.matterजनरेट की गई
.swiftफ़ाइलों को अपने ऐप्लिकेशन प्रोजेक्ट में जोड़ा जा सकता है. इसके अलावा, अगर आपको उन्हें किसी अलग मॉड्यूल में रखना है, तो उन्हें किसी फ़्रेमवर्क में भी जोड़ा जा सकता है.
दूसरा तरीका: एट्रिब्यूट की वैल्यू अपने-आप जनरेट होना
कोड जनरेट करने वाले टूल में एक अतिरिक्त प्लगिन शामिल होता है, जो .matter
फ़ाइल एक्सटेंशन को पहचानता है. यह प्लगिन, कोड जनरेटर को अपने-आप चालू कर देगा. साथ ही, आउटपुट वाली Swift फ़ाइलों को मौजूदा टारगेट में जोड़ देगा. इससे जनरेट की गई फ़ाइलों को सोर्स कंट्रोल में सेव करने की ज़रूरत नहीं पड़ती. साथ ही, यह पक्का किया जाता है कि जनरेटर के बंडल किए गए वर्शन का इस्तेमाल करके, हमेशा ट्रेट जनरेट की जाएं. अगर आपका ऐप्लिकेशन पहले से ही SwiftPM का इस्तेमाल कर रहा है, तो हमारा सुझाव है कि आप इस प्लगिन का इस्तेमाल करें.
प्लगिन का इस्तेमाल करने के लिए:
- अपने ऐप्लिकेशन में किसी टारगेट में
.matterफ़ाइलें जोड़ें. उस टारगेट में यह प्लगिन स्निपेट जोड़ें:
.target( name: "MyAppTarget", plugins: [.plugin(name: "MatterCodegenPlugin")] ),
मॉड्यूल का इस्तेमाल करना
जनरेट किए गए आउटपुट का इस्तेमाल करने के लिए, फ़ाइलों को अपने Xcode प्रोजेक्ट में कॉपी करें. इस मामले में, फ़ाइलें MyCompany.swift और MyCustom.swift हैं.
अगर आपने अपनी विशेषताओं के लिए अलग फ़्रेमवर्क का इस्तेमाल किया है, तो लागू होने वाले मॉड्यूल को इंपोर्ट करने के लिए, import
स्टेटमेंट का इस्तेमाल करें.
इसके बाद, एमएस की सुविधाएं Home API के ज़रिए उसी तरह उपलब्ध होनी चाहिए जिस तरह स्टैंडर्ड Matter की सुविधाएं उपलब्ध होती हैं. हालांकि, ऐसा तब तक होगा, जब तक एमएस की सुविधाएं आपके Matter के फ़र्मवेयर में तय की गई हों. स्टैंडर्ड ट्रेट के नाम की जगह, एमएस ट्रेट का नाम डालें.
उदाहरण के लिए, अगर आपकी एमएस ट्रेट का नाम MyCustomTrait है, तो यहां दिया गया कॉल, MyCustomTrait के सभी एट्रिब्यूट दिखाता है:
let myCustomTrait = deviceType.traits[MyCompany.MyCustomTrait.self]
उदाहरण
अगर आपको आईडीएल फ़ॉर्मैट के बारे में नहीं पता है, तो सैंपल फ़ाइलों के लिए matter/idl/tests/inputs डायरेक्ट्री देखें.
आईडीएल इनपुट
आईडीएल में, एमएस की बहुत ही सामान्य विशेषता को इस तरह से तय किया जा सकता है:
// mycustom.matter
// pragma kotlin(package=com.mycompany.matter.cluster, generate_namespace=true)
// pragma swift(package=MyCompany, generate_namespace=true)
client cluster MyCustom = 4294048768 {
attribute int16u clusterAttr = 1;
// Global Attributes
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute event_id eventList[] = 65530;
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
इस उदाहरण में, 4294048768 का ट्रेट आईडी, हेक्साडेसिमल में 0xFFF1FC00 से मेल खाता है. इसमें 0xFFF1 का प्रीफ़िक्स, टेस्ट वेंडर आईडी को दिखाता है. साथ ही, 0xFC00 का सफ़िक्स, मैन्युफ़ैक्चरर के हिसाब से तय की गई ट्रेट के लिए रिज़र्व की गई वैल्यू है. ज़्यादा जानकारी के लिए, Matter स्पेसिफ़िकेशन का मैन्युफ़ैक्चरर एक्सटेंसिबल आइडेंटिफ़ायर (एमईआई) सेक्शन देखें. पक्का करें कि आपने अपनी आईडीएल फ़ाइल में, हर एमएस ट्रेट के लिए सही डेसिमल ट्रेट आईडी का इस्तेमाल किया हो.
अगर आपके डिवाइस में आज एमएस की विशेषताओं का इस्तेमाल किया जा रहा है, तो हो सकता है कि आपने इसे पहले से ही इस फ़ॉर्मैट में तय किया हो.
Swift आउटपुट
MyCustom.swift (ट्रेट के नाम पर) और MyCompany.swift (नेमस्पेस के नाम पर) नाम की दो Swift फ़ाइलें, बताई गई आउटपुट डायरेक्ट्री में मिल सकती हैं. इन फ़ाइलों को खास तौर पर Home API के साथ इस्तेमाल करने के लिए फ़ॉर्मैट किया जाता है.
उपलब्ध होने के बाद (जैसे, आपके ऐप्लिकेशन के Xcode प्रोजेक्ट में), इन फ़ाइलों का इस्तेमाल मॉड्यूल का इस्तेमाल करना में बताए गए तरीके से किया जा सकता है.
MyCustom.swift
`MyCustom.swift` देखने के लिए, बड़ा करें पर क्लिक करें
// This file contains machine-generated code. public import Foundation @_spi(GoogleHomeInternal) import GoogleHomeSDK /* * This file was machine generated via the code generator * in `codegen.clusters.swift.CustomGenerator` * */ extension MyCompany { /// :nodoc: public struct MyCustomTrait: MatterTrait { /// No supported events for `MyCustomTrait`. public static let supportedEventTypes: [Event.Type] = [] /// No supported commands for `MyCustomTrait`. public static let supportedCommandTypes: [Command.Type] = [] public static let identifier = MyCompany.MyCustomTrait.makeTraitID(for: 4294048768) public let metadata: TraitMetadata /// List of attributes for the `MyCustomTrait`. public let attributes: MyCompany.MyCustomTrait.Attributes private let interactionProxy: InteractionProxy public init(decoder: TraitDecoder, interactionProxy: InteractionProxy?, metadata: TraitMetadata) throws { guard let interactionProxy = interactionProxy else { throw HomeError.invalidArgument("InteractionProxy parameter required.") } let unwrappedDecoder = try decoder.unwrapPayload(namespace: Self.identifier.namespace) self.interactionProxy = interactionProxy self.attributes = try Attributes(decoder: unwrappedDecoder) self.metadata = metadata } // Internal for testing. internal init(attributes: MyCompany.MyCustomTrait.Attributes = .init(), interactionProxy: InteractionProxy?, metadata: TraitMetadata = .init()) throws { guard let interactionProxy = interactionProxy else { throw HomeError.invalidArgument("InteractionProxy parameter required.") } self.interactionProxy = interactionProxy self.attributes = attributes self.metadata = metadata } public func encode(with encoder: TraitEncoder) throws { encoder.wrapPayload(namespace: Self.identifier.namespace) try self.attributes.encode(with: encoder) } public func update(_ block: @Sendable (MutableAttributes) -> Void) async throws -> Self { let mutable = MutableAttributes(attributes: self.attributes) block(mutable) if self.interactionProxy.strictOperationValidation { guard self.attributes.$clusterAttr.isSupported || !mutable.clusterAttrIsSet else { throw HomeError.invalidArgument("clusterAttr is not supported.") } } let updatedTrait = try MyCompany.MyCustomTrait(attributes: self.attributes.apply(mutable), interactionProxy: self.interactionProxy, metadata: self.metadata) try await self.interactionProxy.update(trait: mutable, useTimedInteraction: false) return updatedTrait } } } // MARK: - ForceReadableTrait extension MyCompany.MyCustomTrait: ForceReadableTrait { public func forceRead() async throws { try await self.interactionProxy.forceRead(traitID: Self.identifier) } } // MARK: - Attributes extension MyCompany.MyCustomTrait { /// Attributes for the `MyCustomTrait`. public struct Attributes: Sendable { // Attributes required at runtime. /** A list of the attribute IDs of the attributes supported by the cluster instance. */ /// Nullable: false. @TraitAttribute public var attributeList: [UInt32]? /// Nullable: false. @TraitAttribute public var clusterAttr: UInt16? /** A list of server-generated commands (server to client) which are supported by this cluster server instance. */ /// Nullable: false. @TraitAttribute public var generatedCommandList: [UInt32]? /** A list of client-generated commands which are supported by this cluster server instance. */ /// Nullable: false. @TraitAttribute public var acceptedCommandList: [UInt32]? /** Whether the server supports zero or more optional cluster features. A cluster feature is a set of cluster elements that are mandatory or optional for a defined feature of the cluster. If a cluster feature is supported by the cluster instance, then the corresponding bit is set to 1, otherwise the bit is set to 0 (zero). */ /// Nullable: false. @TraitAttribute public var featureMap: UInt32? /** The revision of the server cluster specification supported by the cluster instance. */ /// Nullable: false. @TraitAttribute public var clusterRevision: UInt16? internal init( clusterAttr: UInt16? = nil, generatedCommandList: [UInt32]? = nil, acceptedCommandList: [UInt32]? = nil, attributeList: [UInt32]? = nil, featureMap: UInt32? = nil, clusterRevision: UInt16? = nil ) { self._clusterAttr = .init( wrappedValue: clusterAttr, isSupported: attributeList?.contains(0x01) ?? false, isNullable: false ) self._generatedCommandList = .init( wrappedValue: generatedCommandList, isSupported: attributeList?.contains(0x0FFF8) ?? false, isNullable: false ) self._acceptedCommandList = .init( wrappedValue: acceptedCommandList, isSupported: attributeList?.contains(0x0FFF9) ?? false, isNullable: false ) self._attributeList = .init( wrappedValue: attributeList, isSupported: attributeList?.contains(0x0FFFB) ?? false, isNullable: false ) self._featureMap = .init( wrappedValue: featureMap, isSupported: attributeList?.contains(0x0FFFC) ?? false, isNullable: false ) self._clusterRevision = .init( wrappedValue: clusterRevision, isSupported: attributeList?.contains(0x0FFFD) ?? false, isNullable: false ) } fileprivate init(decoder: TraitDecoder) throws { let decodedAttributeList: [UInt32] = try decoder.decodeOptionalArray(tag: 0x0FFFB) ?? [] var generatedAttributeList = [UInt32]() generatedAttributeList.append(0x0FFFB) let clusterAttrValue: UInt16? = try decoder.decodeOptional(tag: 0x01) let clusterAttrIsSupported = clusterAttrValue != nil if clusterAttrIsSupported { generatedAttributeList.append(0x01) } self._clusterAttr = .init( wrappedValue: clusterAttrIsSupported ? clusterAttrValue : nil, isSupported: clusterAttrIsSupported, isNullable: false ) let generatedCommandListValue: [UInt32]? = try decoder.decodeOptionalArray(tag: 0x0FFF8) let generatedCommandListIsSupported = generatedCommandListValue != nil if generatedCommandListIsSupported { generatedAttributeList.append(0x0FFF8) } self._generatedCommandList = .init( wrappedValue: generatedCommandListIsSupported ? generatedCommandListValue : nil, isSupported: generatedCommandListIsSupported, isNullable: false ) let acceptedCommandListValue: [UInt32]? = try decoder.decodeOptionalArray(tag: 0x0FFF9) let acceptedCommandListIsSupported = acceptedCommandListValue != nil if acceptedCommandListIsSupported { generatedAttributeList.append(0x0FFF9) } self._acceptedCommandList = .init( wrappedValue: acceptedCommandListIsSupported ? acceptedCommandListValue : nil, isSupported: acceptedCommandListIsSupported, isNullable: false ) let featureMapValue: UInt32? = try decoder.decodeOptional(tag: 0x0FFFC) let featureMapIsSupported = featureMapValue != nil if featureMapIsSupported { generatedAttributeList.append(0x0FFFC) } self._featureMap = .init( wrappedValue: featureMapIsSupported ? featureMapValue : nil, isSupported: featureMapIsSupported, isNullable: false ) let clusterRevisionValue: UInt16? = try decoder.decodeOptional(tag: 0x0FFFD) let clusterRevisionIsSupported = clusterRevisionValue != nil if clusterRevisionIsSupported { generatedAttributeList.append(0x0FFFD) } self._clusterRevision = .init( wrappedValue: clusterRevisionIsSupported ? clusterRevisionValue : nil, isSupported: clusterRevisionIsSupported, isNullable: false ) self._attributeList = .init( wrappedValue: generatedAttributeList, isSupported: true, isNullable: false ) } fileprivate func apply(_ update: MyCompany.MyCustomTrait.MutableAttributes) -> Self { let clusterAttrValue = update.clusterAttrIsSet ? update.clusterAttr : self.clusterAttr let generatedCommandListValue = self.generatedCommandList let acceptedCommandListValue = self.acceptedCommandList let attributeListValue = self.attributeList let featureMapValue = self.featureMap let clusterRevisionValue = self.clusterRevision return MyCompany.MyCustomTrait.Attributes( clusterAttr: clusterAttrValue, generatedCommandList: generatedCommandListValue, acceptedCommandList: acceptedCommandListValue, attributeList: attributeListValue, featureMap: featureMapValue, clusterRevision: clusterRevisionValue ) } } } extension MyCompany.MyCustomTrait.Attributes: TraitEncodable { public static var identifier: String { MyCompany.MyCustomTrait.identifier } public func encode(with encoder: TraitEncoder) throws { try encoder.encode(tag: 0x01, value: self.clusterAttr) try encoder.encode(tag: 0x0FFF8, value: self.generatedCommandList) try encoder.encode(tag: 0x0FFF9, value: self.acceptedCommandList) try encoder.encode(tag: 0x0FFFB, value: self.attributeList) try encoder.encode(tag: 0x0FFFC, value: self.featureMap) try encoder.encode(tag: 0x0FFFD, value: self.clusterRevision) } } // MARK: - Hashable & Equatable extension MyCompany.MyCustomTrait: Hashable { public static func ==(lhs: MyCompany.MyCustomTrait, rhs: MyCompany.MyCustomTrait) -> Bool { return lhs.identifier == rhs.identifier && lhs.attributes == rhs.attributes && lhs.metadata == rhs.metadata } public func hash(into hasher: inout Hasher) { hasher.combine(identifier) hasher.combine(attributes) hasher.combine(metadata) } } extension MyCompany.MyCustomTrait.Attributes: Hashable { public static func ==(lhs: MyCompany.MyCustomTrait.Attributes, rhs: MyCompany.MyCustomTrait.Attributes) -> Bool { var result = true result = lhs.clusterAttr == rhs.clusterAttr && result result = lhs.generatedCommandList == rhs.generatedCommandList && result result = lhs.acceptedCommandList == rhs.acceptedCommandList && result result = lhs.attributeList == rhs.attributeList && result result = lhs.featureMap == rhs.featureMap && result result = lhs.clusterRevision == rhs.clusterRevision && result return result } public func hash(into hasher: inout Hasher) { hasher.combine(self.clusterAttr) hasher.combine(self.generatedCommandList) hasher.combine(self.acceptedCommandList) hasher.combine(self.attributeList) hasher.combine(self.featureMap) hasher.combine(self.clusterRevision) } } // MARK: - MutableAttributes extension MyCompany.MyCustomTrait { public final class MutableAttributes: TraitEncodable { public static let identifier: String = MyCompany.MyCustomTrait.identifier private let baseAttributes: Attributes fileprivate var clusterAttr: UInt16? private(set) public var clusterAttrIsSet = false public func setClusterAttr(_ value: UInt16) { self.clusterAttr = value self.clusterAttrIsSet = true } public func clearClusterAttr() { self.clusterAttr = nil self.clusterAttrIsSet = false } internal init(attributes: MyCompany.MyCustomTrait.Attributes) { self.baseAttributes = attributes } public func encode(with encoder: TraitEncoder) throws { // MutableAttributes is encoded individually, e.g. through update(...), // therefore uddm wrapping needs to be applied. encoder.wrapPayload(namespace: Self.identifier.namespace) if self.clusterAttrIsSet { try encoder.encode(tag: 0x01, value: self.clusterAttr) } } } } // MARK: - Attributes definitions extension MyCompany.MyCustomTrait { public enum Attribute: UInt32, Field { case clusterAttr = 1 case generatedCommandList = 65528 case acceptedCommandList = 65529 case attributeList = 65531 case featureMap = 65532 case clusterRevision = 65533 public var id: UInt32 { self.rawValue } public var type: FieldType { switch self { case .clusterAttr: return .uint16 case .generatedCommandList: return .uint32 case .acceptedCommandList: return .uint32 case .attributeList: return .uint32 case .featureMap: return .uint32 case .clusterRevision: return .uint16 } } } public static func attribute(id: UInt32) -> (any Field)? { return Attribute(rawValue: id) } } // MARK: - Attribute fieldSelect definitions extension TypedReference where T == MyCompany.MyCustomTrait { public var clusterAttr: TypedExpression<UInt16> { fieldSelect(from: self, selectedField: T.Attribute.clusterAttr) } public var generatedCommandList: TypedExpression<[UInt32]> { fieldSelect(from: self, selectedField: T.Attribute.generatedCommandList) } public var acceptedCommandList: TypedExpression<[UInt32]> { fieldSelect(from: self, selectedField: T.Attribute.acceptedCommandList) } public var attributeList: TypedExpression<[UInt32]> { fieldSelect(from: self, selectedField: T.Attribute.attributeList) } public var featureMap: TypedExpression<UInt32> { fieldSelect(from: self, selectedField: T.Attribute.featureMap) } public var clusterRevision: TypedExpression<UInt16> { fieldSelect(from: self, selectedField: T.Attribute.clusterRevision) } } extension Updater where T == MyCompany.MyCustomTrait { public func setClusterAttr(_ value: UInt16) { self.set(Parameter(field: T.Attribute.clusterAttr, value: value)) } } // MARK: - Struct Fields definitions
MyCompany.swift
/// Namespace for all MyCompany Traits and DeviceTypes.
public enum MyCompany { }