আপনি আপনার নিজস্ব কাস্টম বৈশিষ্ট্যের জন্য সমর্থন যোগ করতে পারেন যা স্ট্যান্ডার্ড বৈশিষ্ট্যের বাইরে অতিরিক্ত ক্ষমতা প্রদান করে। এই ধরণের কাস্টম বৈশিষ্ট্যগুলিকে নির্মাতা-নির্দিষ্ট বৈশিষ্ট্য বা MS বৈশিষ্ট্য বলা হয়। MS বৈশিষ্ট্যগুলিকে স্ট্যান্ডার্ড .matter IDL ফর্ম্যাটে সংজ্ঞায়িত করা হয়, তারপর একটি iOS মডিউলে রূপান্তরিত করা হয় যা আপনার অ্যাপে আমদানি করা যেতে পারে।
iOS Home APIs SDK-তে একটি কোড জেনারেটর রয়েছে যা এই রূপান্তরটি সম্পাদন করতে পারে। অতিরিক্তভাবে, প্রয়োজনে, এই কোড জেনারেটর ব্যবহার করে অস্থায়ী বৈশিষ্ট্যও তৈরি করা যেতে পারে।
পূর্বশর্ত
কোড জেনারেটর ব্যবহার করার জন্য, আপনার প্রয়োজন:
- পাইথন ৩.১০ বা তার পরবর্তী সংস্করণ।
- আপনার MS বৈশিষ্ট্যের সংজ্ঞা সহ একটি
.matterIDL ফাইল। এই ফাইলটিতে কেবলclient clusterসংজ্ঞা থাকা উচিত। আপনি ম্যানুয়ালি একটি তৈরি করতে পারেন, অথবা আপনার ডিভাইস ফার্মওয়্যারের জন্য Matter SDK বিল্ড প্রক্রিয়ার অংশ হিসাবে তৈরি করা ফাইলগুলি ব্যবহার করতে পারেন।
Matter কোডজেন টুলটিতে কিছু পাইথন নির্ভরতা রয়েছে যা ইনস্টল করা প্রয়োজন। এটি একটি এককালীন পদক্ষেপ যা SDK এর একটি নতুন সংস্করণ ইন্টিগ্রেটেড করার পরে করা উচিত যাতে Xcode স্যান্ডবক্সে টুলটি চালানো যায়।
swift package plugin --allow-network-connections "all" --allow-writing-to-package-directory matter-codegen-init IDL ফর্ম্যাট সম্পর্কে আরও তথ্যের জন্য, GitHub-এ matter/idl দেখুন। /tests/inputs ডিরেক্টরিতে বেশ কয়েকটি নমুনা IDL ফাইল রয়েছে। সমস্ত Matter ক্লাস্টারের জন্য সম্পূর্ণ IDL ফাইল, যা সমস্ত প্ল্যাটফর্মে (হোম API-এর জন্য iOS মডিউল সহ) জেনারেট করা ফাইলের উৎস, controller-clusters.matter- এ পাওয়া যাবে।
সুইফট ফাইল তৈরি করুন
কোড জেনারেটরটি SDK এর সাথে একত্রিত এবং Swift Package Manager (SwiftPM) এর সাথে একীভূত। যদিও XCode কোড জেনারেটরটিকে SwiftPM প্লাগইন হিসেবে ব্যবহার করে, আপনার প্রকল্পকে প্যাকেজ পরিচালনার জন্য SwiftPM ব্যবহার করতে হবে না।
- আপনার প্রকল্পে SDK ইন্টিগ্রেট করুন। নির্দেশাবলীর জন্য iOS SDK সেট আপ করুন দেখুন।
- প্লাগইনটি সেট আপ করুন। যেহেতু প্লাগইনটি একটি স্যান্ডবক্সে চলে, তাই আপনাকে কিছু নির্ভরতা ইনস্টল করতে হবে:
swift package plugin matter-codegen-init \ --allow-network-connections all \ --allow-writing-to-package-directory জেনারেট করা কোডের জন্য একটি নেমস্পেস নির্বাচন করুন এবং এটিকে IDL ফাইলে একটি
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 ফাইল এক্সটেনশনকে স্বীকৃতি দেয়। প্লাগইনটি স্বয়ংক্রিয়ভাবে কোড জেনারেটরটি চালু করবে এবং আউটপুট সুইফট ফাইলগুলিকে বর্তমান টার্গেটে যুক্ত করবে। এটি জেনারেটেড ফাইলগুলিকে সোর্স কন্ট্রোলে কমিট করার প্রয়োজন এড়ায় এবং নিশ্চিত করে যে বৈশিষ্ট্যগুলি সর্বদা জেনারেটরের বান্ডেলড সংস্করণ ব্যবহার করে তৈরি করা হয়। যদি আপনার অ্যাপটি ইতিমধ্যেই SwiftPM ব্যবহার করে থাকে, তাহলে আমরা এই প্লাগইনটি ব্যবহার করার পরামর্শ দিচ্ছি ।
প্লাগইনটি ব্যবহার করতে:
- আপনার অ্যাপের একটি টার্গেটে আপনার
.matterফাইলগুলি যোগ করুন। সেই টার্গেটে নিম্নলিখিত প্লাগইন স্নিপেট যোগ করুন:
.target( name: "MyAppTarget", plugins: [.plugin(name: "MatterCodegenPlugin")] ),
মডিউলটি ব্যবহার করুন
জেনারেট করা আউটপুট ব্যবহার করতে, ফাইল(গুলি) আপনার Xcode প্রজেক্টে কপি করুন। এই ক্ষেত্রে, ফাইলগুলি হল MyCompany.swift এবং MyCustom.swift ।
যদি আপনি আপনার বৈশিষ্ট্যের জন্য একটি পৃথক কাঠামো ব্যবহার করেন, তাহলে প্রযোজ্য মডিউলটি আমদানি করতে একটি import বিবৃতি ব্যবহার করুন।
তাহলে MS বৈশিষ্ট্যগুলি এখন হোম API-এর মাধ্যমে স্ট্যান্ডার্ড Matter বৈশিষ্ট্যগুলির মতোই উপলব্ধ হওয়া উচিত, যতক্ষণ না সেই MS বৈশিষ্ট্যগুলি আপনার Matter ফার্মওয়্যারে সংজ্ঞায়িত করা হয়। আপনার MS বৈশিষ্ট্য নামের সাথে কেবল একটি স্ট্যান্ডার্ড বৈশিষ্ট্যের নাম প্রতিস্থাপন করুন।
উদাহরণস্বরূপ, যদি আপনার MS বৈশিষ্ট্যের নাম MyCustomTrait হয়, তাহলে নিম্নলিখিত কলটি MyCustomTrait এর সমস্ত বৈশিষ্ট্য ফেরত দেয়:
let myCustomTrait = deviceType.traits[MyCompany.MyCustomTrait.self]
উদাহরণ
যদি আপনি IDL ফর্ম্যাটের সাথে পরিচিত না হন, তাহলে নমুনা ফাইলের জন্য matter/idl/tests/inputs ডিরেক্টরিগুলি দেখুন।
আইডিএল ইনপুট
IDL-তে একটি খুব সহজ MS বৈশিষ্ট্য এভাবে সংজ্ঞায়িত করা যেতে পারে:
// 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 স্পেসিফিকেশনের প্রস্তুতকারক এক্সটেনসিবল আইডেন্টিফায়ার (MEI) বিভাগটি দেখুন। আপনার IDL ফাইলে প্রতিটি MS বৈশিষ্ট্যের জন্য একটি উপযুক্ত দশমিক বৈশিষ্ট্য আইডি ব্যবহার করতে ভুলবেন না।
যদি আজ আপনার ডিভাইসে MS বৈশিষ্ট্য ব্যবহার করা হয়, তাহলে সম্ভবত আপনি এটি ইতিমধ্যেই এই ফর্ম্যাটে সংজ্ঞায়িত করেছেন।
সুইফট আউটপুট
দুটি Swift ফাইল, MyCustom.swift (যার নামকরণ করা হয়েছে বৈশিষ্ট্য অনুসারে) এবং MyCompany.swift (যার নামকরণ করা হয়েছে নামকরণ করা হয়েছে) নির্দিষ্ট আউটপুট ডিরেক্টরিতে পাওয়া যাবে। এই ফাইলগুলি বিশেষভাবে হোম API গুলির সাথে ব্যবহারের জন্য ফর্ম্যাট করা হয়েছে।
একবার উপলব্ধ হয়ে গেলে (যেমন আপনার অ্যাপের Xcode প্রজেক্টে), ফাইলগুলি Use the module এ বর্ণিত হিসাবে ব্যবহার করা যেতে পারে।
আমার কাস্টম.সুইফট
`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
মাইকম্পানি.সুইফট
/// Namespace for all MyCompany Traits and DeviceTypes.
public enum MyCompany { }