Android ऐप्लिकेशन डेवलपर, Home API का इस्तेमाल करके Thread बॉर्डर राऊटर को मैनेज कर सकते हैं.
GoogleBorderRouterDevice को दो मुख्य डिवाइसों की विशेषताओं का इस्तेमाल करके लागू किया जाता है:
ThreadNetworkCapabilities,
यह बॉर्डर राउटर की सुविधाओं की जांच करने के लिए, सिर्फ़ पढ़ने के लिए एट्रिब्यूट उपलब्ध कराता है. साथ ही,
ThreadNetworkManagement,
यह नेटवर्क लाइफ़साइकल कमांड और क्रेडेंशियल शेयर करने की सुविधा को मैनेज करता है. इसके लिए, कमिश्नर (ePSKc) के लिए कुछ समय के लिए पहले से शेयर की गई कुंजी का इस्तेमाल किया जाता है. स्ट्रक्चर के लेवल पर इंटरनेट ऐक्सेस करने से जुड़ी नीतियों को ThreadNetworkSettings ट्रेट का इस्तेमाल करके मैनेज किया जाता है.
किसी भी सुविधा का इस्तेमाल करने या एट्रिब्यूट अपडेट करने से पहले, हमेशा यह जांच करें कि डिवाइस के लिए एट्रिब्यूट और कमांड काम करती हैं या नहीं. ज़्यादा जानकारी के लिए, Androidपर डिवाइसों को कंट्रोल करना लेख पढ़ें.
| Home APIs डिवाइस टाइप | विशेषताएं | Kotlin का सैंपल ऐप्लिकेशन | इस्तेमाल का उदाहरण |
|---|---|---|---|
|
बॉर्डर राऊटर
|
ज़रूरी एट्रिब्यूट google ThreadNetworkCapabilities google ThreadNetworkManagement |
बॉर्डर राऊटर |
किसी डिवाइस के बारे में बुनियादी जानकारी पाना
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 ट्रेट का इस्तेमाल करके, बॉर्डर राउटर की सिर्फ़ पढ़ने की सुविधाओं (जैसे कि ePSKc सपोर्ट और इंटरनेट ऐक्सेस की सेटिंग कॉन्फ़िगरेशन) की जांच की जा सकती है.
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 के बंद होने से जुड़े इवेंट देखना
थ्रेड बॉर्डर राउटर, 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 नेटवर्क की सदस्यता मैनेज करना
Thread बॉर्डर राऊटर को, चालू ऑपरेशनल डेटासेट टीएलवी उपलब्ध कराकर, किसी नए 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 (घर या इमारत को दिखाने वाला) से जुड़ी होती है. इससे डेवलपर, Thread बॉर्डर राऊटर के लिए, पूरे स्ट्रक्चर में इंटरनेट ऐक्सेस करने की नीति को कॉन्फ़िगर कर सकते हैं.
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}")
}
}