@@ -26,11 +26,8 @@ import kotlinx.android.synthetic.main.sensor_client_fragment.clusterNameSpinner
26
26
import kotlinx.android.synthetic.main.sensor_client_fragment.deviceIdEd
27
27
import kotlinx.android.synthetic.main.sensor_client_fragment.endpointIdEd
28
28
import kotlinx.android.synthetic.main.sensor_client_fragment.lastValueTv
29
+ import kotlinx.android.synthetic.main.sensor_client_fragment.readSensorBtn
29
30
import kotlinx.android.synthetic.main.sensor_client_fragment.sensorGraph
30
- import kotlinx.android.synthetic.main.sensor_client_fragment.view.clusterNameSpinner
31
- import kotlinx.android.synthetic.main.sensor_client_fragment.view.readSensorBtn
32
- import kotlinx.android.synthetic.main.sensor_client_fragment.view.sensorGraph
33
- import kotlinx.android.synthetic.main.sensor_client_fragment.view.watchSensorBtn
34
31
import kotlinx.android.synthetic.main.sensor_client_fragment.watchSensorBtn
35
32
import kotlinx.coroutines.CoroutineScope
36
33
import kotlinx.coroutines.launch
@@ -52,53 +49,56 @@ class SensorClientFragment : Fragment() {
52
49
savedInstanceState : Bundle ?
53
50
): View {
54
51
scope = viewLifecycleOwner.lifecycleScope
52
+ return inflater.inflate(R .layout.sensor_client_fragment, container, false )
53
+ }
55
54
56
- return inflater.inflate(R .layout.sensor_client_fragment, container, false ).apply {
57
- ChipClient .getDeviceController(requireContext()).setCompletionListener(null )
58
- deviceIdEd.setOnEditorActionListener { textView, actionId, _ ->
59
- if (actionId == EditorInfo .IME_ACTION_DONE ) {
60
- updateAddress(textView.text.toString())
61
- resetSensorGraph() // reset the graph on device change
62
- }
63
- actionId == EditorInfo .IME_ACTION_DONE
64
- }
65
- endpointIdEd.setOnEditorActionListener { textView, actionId, _ ->
66
- if (actionId == EditorInfo .IME_ACTION_DONE )
67
- resetSensorGraph() // reset the graph on endpoint change
68
- actionId == EditorInfo .IME_ACTION_DONE
55
+ override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
56
+ super .onViewCreated(view, savedInstanceState)
57
+
58
+ ChipClient .getDeviceController(requireContext()).setCompletionListener(null )
59
+ deviceIdEd.setOnEditorActionListener { textView, actionId, _ ->
60
+ if (actionId == EditorInfo .IME_ACTION_DONE ) {
61
+ updateAddress(textView.text.toString())
62
+ resetSensorGraph() // reset the graph on device change
69
63
}
70
- clusterNameSpinner.adapter = makeClusterNamesAdapter()
71
- clusterNameSpinner.onItemSelectedListener = object : AdapterView .OnItemSelectedListener {
72
- override fun onNothingSelected (parent : AdapterView <* >? ) = Unit
73
- override fun onItemSelected (parent : AdapterView <* >? , view : View ? , position : Int , id : Long ) {
74
- resetSensorGraph() // reset the graph on cluster change
75
- }
64
+ actionId == EditorInfo .IME_ACTION_DONE
65
+ }
66
+ endpointIdEd.setOnEditorActionListener { textView, actionId, _ ->
67
+ if (actionId == EditorInfo .IME_ACTION_DONE )
68
+ resetSensorGraph() // reset the graph on endpoint change
69
+ actionId == EditorInfo .IME_ACTION_DONE
70
+ }
71
+ clusterNameSpinner.adapter = makeClusterNamesAdapter()
72
+ clusterNameSpinner.onItemSelectedListener = object : AdapterView .OnItemSelectedListener {
73
+ override fun onNothingSelected (parent : AdapterView <* >? ) = Unit
74
+ override fun onItemSelected (parent : AdapterView <* >? , view : View ? , position : Int , id : Long ) {
75
+ resetSensorGraph() // reset the graph on cluster change
76
76
}
77
+ }
77
78
78
- readSensorBtn.setOnClickListener { scope.launch { readSensorCluster() } }
79
- watchSensorBtn.setOnCheckedChangeListener { _, isChecked ->
80
- if (isChecked) {
81
- scope.launch { subscribeSensorCluster() }
82
- } else {
83
- unsubscribeSensorCluster()
84
- }
79
+ readSensorBtn.setOnClickListener { scope.launch { readSensorCluster() } }
80
+ watchSensorBtn.setOnCheckedChangeListener { _, isChecked ->
81
+ if (isChecked) {
82
+ scope.launch { subscribeSensorCluster() }
83
+ } else {
84
+ unsubscribeSensorCluster()
85
85
}
86
+ }
86
87
87
- val currentTime = Calendar .getInstance().time.time
88
- sensorGraph.addSeries(sensorData)
89
- sensorGraph.viewport.isXAxisBoundsManual = true
90
- sensorGraph.viewport.setMinX(currentTime.toDouble())
91
- sensorGraph.viewport.setMaxX(currentTime.toDouble() + MIN_REFRESH_PERIOD_S * 1000 * MAX_DATA_POINTS )
92
- sensorGraph.gridLabelRenderer.padding = 20
93
- sensorGraph.gridLabelRenderer.numHorizontalLabels = 4
94
- sensorGraph.gridLabelRenderer.setHorizontalLabelsAngle(150 )
95
- sensorGraph.gridLabelRenderer.labelFormatter = object : LabelFormatter {
96
- override fun setViewport (viewport : Viewport ? ) = Unit
97
- override fun formatLabel (value : Double , isValueX : Boolean ): String {
98
- if (! isValueX)
99
- return " %.2f" .format(value)
100
- return SimpleDateFormat (" H:mm:ss" ).format(Date (value.toLong())).toString()
101
- }
88
+ val currentTime = Calendar .getInstance().time.time
89
+ sensorGraph.addSeries(sensorData)
90
+ sensorGraph.viewport.isXAxisBoundsManual = true
91
+ sensorGraph.viewport.setMinX(currentTime.toDouble())
92
+ sensorGraph.viewport.setMaxX(currentTime.toDouble() + MIN_REFRESH_PERIOD_S * 1000 * MAX_DATA_POINTS )
93
+ sensorGraph.gridLabelRenderer.padding = 20
94
+ sensorGraph.gridLabelRenderer.numHorizontalLabels = 4
95
+ sensorGraph.gridLabelRenderer.setHorizontalLabelsAngle(150 )
96
+ sensorGraph.gridLabelRenderer.labelFormatter = object : LabelFormatter {
97
+ override fun setViewport (viewport : Viewport ? ) = Unit
98
+ override fun formatLabel (value : Double , isValueX : Boolean ): String {
99
+ if (! isValueX)
100
+ return " %.2f" .format(value)
101
+ return SimpleDateFormat (" H:mm:ss" ).format(Date (value.toLong())).toString()
102
102
}
103
103
}
104
104
}
@@ -243,13 +243,9 @@ class SensorClientFragment : Fragment() {
243
243
},
244
244
" subscribe" to { device: Long , endpointId: Int , callback: ReadCallback ->
245
245
val cluster = ChipClusters .TemperatureMeasurementCluster (device, endpointId)
246
- cluster.reportMeasuredValueAttribute(callback)
247
- cluster.subscribeMeasuredValueAttribute(object : ChipClusters .DefaultClusterCallback {
248
- override fun onSuccess () = Unit
249
- override fun onError (ex : Exception ) {
250
- callback.onError(ex)
251
- }
252
- }, MIN_REFRESH_PERIOD_S , MAX_REFRESH_PERIOD_S )
246
+ cluster.subscribeMeasuredValueAttribute(callback,
247
+ MIN_REFRESH_PERIOD_S ,
248
+ MAX_REFRESH_PERIOD_S )
253
249
},
254
250
" unitValue" to 0.01 ,
255
251
" unitSymbol" to " \u00B0 C"
@@ -261,13 +257,9 @@ class SensorClientFragment : Fragment() {
261
257
},
262
258
" subscribe" to { device: Long , endpointId: Int , callback: ReadCallback ->
263
259
val cluster = ChipClusters .PressureMeasurementCluster (device, endpointId)
264
- cluster.reportMeasuredValueAttribute(callback)
265
- cluster.subscribeMeasuredValueAttribute(object : ChipClusters .DefaultClusterCallback {
266
- override fun onSuccess () = Unit
267
- override fun onError (ex : Exception ) {
268
- callback.onError(ex)
269
- }
270
- }, MIN_REFRESH_PERIOD_S , MAX_REFRESH_PERIOD_S )
260
+ cluster.subscribeMeasuredValueAttribute(callback,
261
+ MIN_REFRESH_PERIOD_S ,
262
+ MAX_REFRESH_PERIOD_S )
271
263
},
272
264
" unitValue" to 1.0 ,
273
265
" unitSymbol" to " hPa"
@@ -279,13 +271,9 @@ class SensorClientFragment : Fragment() {
279
271
},
280
272
" subscribe" to { device: Long , endpointId: Int , callback: ReadCallback ->
281
273
val cluster = ChipClusters .RelativeHumidityMeasurementCluster (device, endpointId)
282
- cluster.reportMeasuredValueAttribute(callback)
283
- cluster.subscribeMeasuredValueAttribute(object : ChipClusters .DefaultClusterCallback {
284
- override fun onSuccess () = Unit
285
- override fun onError (ex : Exception ) {
286
- callback.onError(ex)
287
- }
288
- }, MIN_REFRESH_PERIOD_S , MAX_REFRESH_PERIOD_S )
274
+ cluster.subscribeMeasuredValueAttribute(callback,
275
+ MIN_REFRESH_PERIOD_S ,
276
+ MAX_REFRESH_PERIOD_S )
289
277
},
290
278
" unitValue" to 0.01 ,
291
279
" unitSymbol" to " %"
0 commit comments