Android ऐप्लिकेशन डेवलपर, Home APIs का इस्तेमाल करके Thread Border Router (TBR) को मैनेज कर सकते हैं.
GoogleBorderRouterDevice को डिवाइस की दो मुख्य विशेषताओं का इस्तेमाल करके लागू किया जाता है:
ThreadNetworkCapabilities,
जो border router की सुविधाओं की जांच करने के लिए, रीड-ओनली एट्रिब्यूट उपलब्ध कराता है. साथ ही,
ThreadNetworkManagement,
जो कमिश्नर (ईपीएसकेसी) के लिए, कुछ समय के लिए पहले से शेयर की गई कुंजी का इस्तेमाल करके, नेटवर्क लाइफ़साइकल के निर्देशों और क्रेडेंशियल शेयर करने की सुविधा को मैनेज करता है. स्ट्रक्चर-लेवल पर इंटरनेट ऐक्सेस की नीतियों को मैनेज करने के लिए, ThreadNetworkSettings trait का इस्तेमाल किया जाता है.
किसी भी सुविधा का इस्तेमाल करने या एट्रिब्यूट को अपडेट करने से पहले, हमेशा यह देख लें कि डिवाइस के लिए एट्रिब्यूट और निर्देश काम करते हैं या नहीं. ज़्यादा जानकारी के लिए, Androidपर डिवाइसों को कंट्रोल करना लेख पढ़ें.
| Home APIs के लिए डिवाइस टाइप | Traits | Kotlin सैंपल के तौर पर मिला ऐप्लिकेशन | इस्तेमाल का उदाहरण |
|---|---|---|---|
|
बॉर्डर राऊटर
|
ज़रूरी ट्रेट google ThreadNetworkCapabilities google ThreadNetworkManagement |
बॉर्डर राऊटर |
किसी डिवाइस के बारे में बुनियादी जानकारी पाना
Android के लिए सैंपल के तौर पर मिले ऐप्लिकेशन में लागू किया गया
BasicInformation ट्रेट में, किसी डिवाइस के लिए वेंडर का नाम, वेंडर आईडी, प्रॉडक्ट आईडी, प्रॉडक्ट का नाम (इसमें मॉडल की जानकारी शामिल होती है), और सॉफ़्टवेयर के वर्शन जैसी जानकारी शामिल होती है:
// Get device basic information. All general information traits are on the RootNodeDevice type. device.type(RootNodeDevice).first().standardTraits.basicInformation?.let { basicInformation -> println("vendorName ${basicInformation.vendorName}") println("vendorId ${basicInformation.vendorId}") println("productId ${basicInformation.productId}") println("productName ${basicInformation.productName}") println("softwareVersion ${basicInformation.softwareVersion}") }
बॉर्डर राऊटर की सुविधाओं की जांच करना
ThreadNetworkCapabilities ट्रेट का इस्तेमाल करके, border router की सिर्फ़ पढ़ने की सुविधाओं की जांच की जा सकती है. जैसे, ईपीएसकेसी सहायता और इंटरनेट का ऐक्सेस सेटिंग कॉन्फ़िगरेशन.
suspend fun checkBorderRouterCapabilities(device: HomeDevice) {
// Filter for GoogleBorderRouterDevice device type
val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
if (gtbrDevice == null) {
println("Device is not a Google border router.")
return
}
// Retrieve the ThreadNetworkCapabilities trait
val capabilitiesTrait = gtbrDevice.trait(ThreadNetworkCapabilities)
if (capabilitiesTrait == null) {
println("ThreadNetworkCapabilities trait not found on device.")
return
}
val isEpskcSupported = capabilitiesTrait.epskcSupported ?: false
val internetAccessOption = capabilitiesTrait.internetAccessOption?.name ?: "None"
val isIasSupported = !internetAccessOption.equals("None", ignoreCase = true)
println("ePSKc Supported: $isEpskcSupported")
println("Internet Access Setting Supported: $isIasSupported")
}
Thread क्रेडेंशियल शेयर करने की सुविधा (ePSKc) मैनेज करना
Thread क्रेडेंशियल शेयर करने के लिए, कमिश्नर (ePSKc) के लिए कुछ समय के लिए पहले से शेयर की गई कुंजी का इस्तेमाल किया जाता है. ePSKc मोड, कुछ समय के लिए इस्तेमाल होने वाली सुरक्षित पासकी जनरेट करता है. इसका इस्तेमाल करके, बाहरी डिवाइस या कमिश्नर, Thread नेटवर्क डेटासेट को सुरक्षित तरीके से ऐक्सेस कर सकते हैं.
ePSKc मोड चालू करना
suspend fun startEpskcSession(device: HomeDevice, durationSeconds: Short): ActivateEpskcModeCommand.Response? {
val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)
if (mgmtTrait == null) {
println("ThreadNetworkManagement trait not found.")
return null
}
return try {
val response = mgmtTrait.activateEpskcMode(
optionalArgs = { this.requestedDurationSeconds = durationSeconds }
)
println("ePSKc Session Activated!")
println("Status: ${response.status}")
println("Ephemeral PSKc: ${response.epskc}")
println("Valid Duration (s): ${response.validDurationSeconds}")
response
} catch (e: Exception) {
println("Failed to activate ePSKc mode: ${e.message}")
null
}
}
ePSKc मोड बंद करना
suspend fun stopEpskcSession(device: HomeDevice) {
val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)
mgmtTrait?.deactivateEpskcMode()
println("ePSKc mode deactivated.")
}
ePSKc के बंद होने से जुड़े इवेंट देखें
TBR, ePSKc सेशन खत्म होने पर इवेंट ट्रिगर करते हैं. उदाहरण के लिए, कुंजी का इस्तेमाल होने, सेशन खत्म होने या मैन्युअल तरीके से रद्द किए जाने पर.
suspend fun observeEpskcEvents(device: HomeDevice) {
val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)
mgmtTrait?.epskcModeDeactivatedEventFlow()?.collect { event ->
println("ePSKc Session Ended. Reason: ${event.reason}")
when (event.reason?.name) {
"KeyUsed" -> println("Key was successfully used to commission a device.")
"Expired" -> println("Session timed out before the key was used.")
"Cancelled" -> println("Session was manually cancelled.")
}
}
}
Thread नेटवर्क की सदस्यता मैनेज करना
किसी TBR को नए Thread नेटवर्क में शामिल होने का निर्देश दिया जा सकता है. इसके लिए, चालू ऑपरेशनल डेटासेट टीएलवी उपलब्ध कराएं. इसके अलावा, उसे मौजूदा नेटवर्क से हटने का निर्देश भी दिया जा सकता है.
suspend fun joinNetwork(device: HomeDevice, datasetTlvs: ByteArray) {
val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)
try {
val response = mgmtTrait?.joinNetwork(operationalDatasetTlvs = datasetTlvs)
println("Join network command sent. Status: ${response?.status}")
} catch (e: Exception) {
println("Join network failed: ${e.message}")
}
}
suspend fun leaveNetwork(device: HomeDevice) {
val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)
try {
mgmtTrait?.leaveNetwork()
println("Leave network command sent successfully.")
} catch (e: Exception) {
println("Leave network failed: ${e.message}")
}
}
स्ट्रक्चर लेवल पर इंटरनेट का ऐक्सेस कॉन्फ़िगर करना
ThreadNetworkSettings विशेषता, अपडेट की जा सकने वाली विशेषता है. यह Structure (घर या इमारत को दिखाता है) से जुड़ी होती है. इससे डेवलपर, TBR के लिए पूरे होम स्ट्रक्चर में इंटरनेट का ऐक्सेस करने की नीति को कॉन्फ़िगर कर सकते हैं.
suspend fun updateStructureInternetAccess(structure: Structure, enableInternetAccess: Boolean) {
// Retrieve the ThreadNetworkSettings trait for the structure
val settingsTrait = structure.trait(ThreadNetworkSettings).firstOrNull()
if (settingsTrait == null) {
println("ThreadNetworkSettings trait not found on structure.")
return
}
val option = if (enableInternetAccess) {
InternetAccessOption.InternetAccessOptionAll
} else {
InternetAccessOption.InternetAccessOptionNone
}
try {
settingsTrait.update {
setInternetAccessOption(option)
}
println("Successfully updated Thread internet access policy to: ${option.name}")
} catch (e: Exception) {
println("Failed to update Thread internet access policy: ${e.message}")
}
}