Skip to content

Commit

Permalink
improve null handling, eliminate magic numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineThreepwood committed Apr 2, 2024
1 parent c73a181 commit 0f2d66c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class InfluxDbconnectorApp : AbstractAppController() {

// connect to db
connectToDatabase()
while (!task!!.isCancelled) {
while (task?.isCancelled == false) {
try {
verifyConnection()
break
Expand All @@ -120,7 +120,7 @@ class InfluxDbconnectorApp : AbstractAppController() {
}

// lookup bucked
while (!task!!.isCancelled) {
while (task?.isCancelled == false) {
try {
// check if bucked found
databaseBucket
Expand All @@ -144,11 +144,11 @@ class InfluxDbconnectorApp : AbstractAppController() {
Thread.currentThread().interrupt()
return@submit
}
if (!task!!.isCancelled && isConnected) {
if (task?.isCancelled == false && isConnected) {
try {
// write initial heartbeat
logger.debug("initial heartbeat")
writeApi!!.writePoint(
writeApi?.writePoint(
bucketName, org, Point.measurement(InfluxDbProcessor.HEARTBEAT_MEASUREMENT)
.addField(InfluxDbProcessor.HEARTBEAT_FIELD, InfluxDbProcessor.HEARTBEAT_OFFLINE_VALUE)
.time(System.currentTimeMillis() - 1, WritePrecision.MS)
Expand Down Expand Up @@ -194,10 +194,10 @@ class InfluxDbconnectorApp : AbstractAppController() {
// finish task

logger.debug("finish task")
if (task != null && !task!!.isDone) {
task!!.cancel(true)
task?.takeIf { !it.isDone }?.let { task ->
task.cancel(true)
try {
task!![5, TimeUnit.SECONDS]
task[5, TimeUnit.SECONDS]
} catch (ex: CancellationException) {
// that's what we are waiting for.
} catch (ex: Exception) {
Expand All @@ -206,10 +206,11 @@ class InfluxDbconnectorApp : AbstractAppController() {
}

logger.debug("finish heartbeat")
if (heartbeat != null && !heartbeat!!.isDone) {
heartbeat?.cancel(true)

heartbeat?.takeIf { !it.isDone }?.let { heartbeat ->
heartbeat.cancel(true)
try {
heartbeat!![5, TimeUnit.SECONDS]
heartbeat[5, TimeUnit.SECONDS]
} catch (ex: CancellationException) {
// that's what we are waiting for.
} catch (ex: Exception) {
Expand Down Expand Up @@ -392,7 +393,9 @@ class InfluxDbconnectorApp : AbstractAppController() {
.forEach { point.addTag("label_" + it.key, it.getValue(0)) }

if (values > 0) {
writeApi!!.writePoint(bucketName!!, org!!, point)
org.let { org ->
writeApi?.writePoint(bucketName, org, point)
}
}
} catch (ex: CouldNotPerformException) {
ExceptionPrinter.printHistory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class PresenceDetector : Manageable<Location>, DataProvider<PresenceState> {

try {
// if motion is still detected just restart the timeout.
if (location!!.data.motionState.value == MotionState.State.MOTION &&
if (location?.data?.motionState?.value == MotionState.State.MOTION &&
durationSinceLastMotion < MOTION_TIMEOUT
) {
GlobalCachedExecutorService.submit {
Expand Down Expand Up @@ -190,13 +190,13 @@ class PresenceDetector : Manageable<Location>, DataProvider<PresenceState> {

buttonUnitPool.activate()

if ((location!!.config.locationConfig.locationType == LocationType.TILE)) {
if (location?.config?.locationConfig?.locationType == LocationType.TILE) {
connectionUnitPool.activate()
}

// start initial timeout
presenceTimeout?.start()
updateMotionState(location!!.data.motionState)
location?.data?.motionState?.let { updateMotionState(it) }
}


Expand Down Expand Up @@ -398,10 +398,11 @@ class PresenceDetector : Manageable<Location>, DataProvider<PresenceState> {

companion object {
@JvmField
val PRESENCE_TEST_TIMEOUT: Duration = Duration.ofMillis(50)
val PRESENCE_TIMEOUT: Duration =
Duration.ofSeconds(60).takeIf { JPService.testMode().not() }
?: Duration.ofMillis(50)
Duration.ofMinutes(1).takeIf { JPService.testMode().not() }
?: PRESENCE_TEST_TIMEOUT
@JvmField
val MOTION_TIMEOUT: Duration = Duration.ofMinutes(60)
val MOTION_TIMEOUT: Duration = Duration.ofHours(1)
}
}

0 comments on commit 0f2d66c

Please sign in to comment.