@@ -3,6 +3,8 @@ package br.com.firstsoft.core.os.hwinfo
3
3
import br.com.firstsoft.core.os.resource.NativeResourceLoader
4
4
import kotlinx.coroutines.CoroutineScope
5
5
import kotlinx.coroutines.Dispatchers
6
+ import kotlinx.coroutines.Job
7
+ import kotlinx.coroutines.flow.cancellable
6
8
import kotlinx.coroutines.flow.collectLatest
7
9
import kotlinx.coroutines.flow.map
8
10
import kotlinx.coroutines.launch
@@ -14,6 +16,7 @@ object HwInfoProcessManager {
14
16
private var process: Process ? = null
15
17
private const val MAX_RETRIES = 1
16
18
private var currentRetries = 0
19
+ private var pollingJob: Job ? = null
17
20
18
21
init {
19
22
currentRetries = 0
@@ -29,7 +32,8 @@ object HwInfoProcessManager {
29
32
command(" cmd.exe" , " /c" , file)
30
33
}.start()
31
34
32
- observeHwInfoPollingTime()
35
+ pollingJob?.cancel()
36
+ pollingJob = observeHwInfoPollingTime()
33
37
}
34
38
35
39
fun stop () {
@@ -40,6 +44,11 @@ object HwInfoProcessManager {
40
44
process = null
41
45
}
42
46
47
+ private fun restart () {
48
+ stop()
49
+ start()
50
+ }
51
+
43
52
private fun overwriteSettings () {
44
53
try {
45
54
val sourceSettings = NativeResourceLoader .load(" /hwinfo/HWiNFO64.INI.src" )
@@ -55,21 +64,21 @@ object HwInfoProcessManager {
55
64
56
65
HwInfoReader
57
66
.currentData
67
+ .cancellable()
58
68
.map { it.header }
59
69
.collectLatest {
60
70
accumulator + = 500
71
+ if (accumulator < TimeUnit .SECONDS .toMillis(5 )) return @collectLatest
61
72
62
- if (accumulator >= TimeUnit .SECONDS .toMillis(5 )) {
63
- accumulator = 0
64
-
65
- if (lastPollTime != it.pollTime) {
73
+ accumulator = 0
74
+ when {
75
+ lastPollTime != it.pollTime -> {
66
76
lastPollTime = it.pollTime
67
77
currentRetries = 0
68
- } else if (currentRetries < MAX_RETRIES ) {
78
+ }
79
+ lastPollTime == it.pollTime && currentRetries < MAX_RETRIES -> {
69
80
currentRetries++
70
- stop()
71
- start()
72
- return @collectLatest
81
+ restart()
73
82
}
74
83
}
75
84
}
0 commit comments