Skip to content

Commit

Permalink
Add network state
Browse files Browse the repository at this point in the history
  • Loading branch information
kimble committed Jun 28, 2024
1 parent d0284b6 commit e1dc52a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 11 deletions.
10 changes: 10 additions & 0 deletions adapter-protobuf-java/src/main/proto/adapter.proto
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ message ThingState {
// Rough indicator of battery health
BatteryStatus battery_status = 10;

Network network = 11;

}

reserved 4;
Expand Down Expand Up @@ -387,6 +389,14 @@ message ThingState {

}

message Network {
string mac = 1;
string ip = 2;
int64 last_connect_at_epoch_seconds = 3;
int64 last_disconnect_at_epoch_seconds = 4;
int32 rssi = 5;
}

// All fields are optional, but make sure you include at least
// one of the fields. If your device does not support either of these
// fields you should omit the DeviceInfo from your manifest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,35 @@ object ProtobufParser {
)
}

Adapter.ThingState.ValueCase.NETWORK -> {
ThingState.Network(
thingId = thing.id,
timestamp = lastUpdate,
ip = serializedState.network.ip.ifBlank { null },
mac = serializedState.network.mac.ifBlank { null },
rssi = if (serializedState.network.rssi > 0) {
serializedState.network.rssi
} else {
null
},
lastConnectedAt = if (serializedState.network.lastConnectAtEpochSeconds > 0) {
Instant.ofEpochSecond(serializedState.network.lastConnectAtEpochSeconds)
} else {
null
},
lastDisconnectedAt = if (serializedState.network.lastDisconnectAtEpochSeconds > 0) {
Instant.ofEpochSecond(serializedState.network.lastDisconnectAtEpochSeconds)
} else {
null
}
)
}

Adapter.ThingState.ValueCase.VALUE_NOT_SET, null -> {
log.warn("Unsupported thing state detected: {}", serializedState.valueCase)
null
}

}

if (state != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ object ProtobufSerializer {
thingBuilder.setUri(thing.link.toString())
}
if (thing.timeWithoutMessageBeforeAlert != null) {
thingBuilder.setSecondsWithoutMessageBeforeAlert(thing.timeWithoutMessageBeforeAlert.toSeconds().toInt())
thingBuilder.setSecondsWithoutMessageBeforeAlert(
thing.timeWithoutMessageBeforeAlert.toSeconds().toInt()
)
}

thingBuilder.build()
Expand Down Expand Up @@ -152,6 +154,7 @@ object ProtobufSerializer {
)
.build()
}

is ThingState.Lock -> {
Adapter.ThingState.newBuilder()
.setLastUpdate(state.timestamp.toString())
Expand All @@ -164,6 +167,7 @@ object ProtobufSerializer {
)
.build()
}

is ThingState.DebugLog -> {
Adapter.ThingState.newBuilder()
.setLastUpdate(state.timestamp.toString())
Expand All @@ -190,6 +194,7 @@ object ProtobufSerializer {
)
.build()
}

is ThingState.OpenPosition -> {
Adapter.ThingState.newBuilder()
.setLastUpdate(state.timestamp.toString())
Expand All @@ -205,13 +210,16 @@ object ProtobufSerializer {
is ThingState.BatteryStatus -> {
Adapter.ThingState.newBuilder()
.setLastUpdate(state.timestamp.toString())
.setBatteryStatus(when (state.state) {
ThingState.BatteryStatus.State.EMPTY -> Adapter.ThingState.BatteryStatus.EMPTY
ThingState.BatteryStatus.State.GOOD -> Adapter.ThingState.BatteryStatus.GOOD
ThingState.BatteryStatus.State.POOR -> Adapter.ThingState.BatteryStatus.POOR
})
.setBatteryStatus(
when (state.state) {
ThingState.BatteryStatus.State.EMPTY -> Adapter.ThingState.BatteryStatus.EMPTY
ThingState.BatteryStatus.State.GOOD -> Adapter.ThingState.BatteryStatus.GOOD
ThingState.BatteryStatus.State.POOR -> Adapter.ThingState.BatteryStatus.POOR
}
)
.build()
}

is ThingState.DeviceType -> {
val deviceInfoBuilder = Adapter.ThingState.DeviceInfo.newBuilder()

Expand All @@ -230,14 +238,17 @@ object ProtobufSerializer {
.setDeviceType(deviceInfoBuilder.build())
.build()
}

is ThingState.Online -> {
val onlineBuilder = Adapter.ThingState.Online.newBuilder()

onlineBuilder.setOnlineStatus(when (state.status) {
ThingState.Online.Status.REPORTED_ONLINE -> Adapter.ThingState.Online.OnlineStatus.REPORTED_ONLINE
ThingState.Online.Status.REPORTED_OFFLINE -> Adapter.ThingState.Online.OnlineStatus.REPORTED_OFFLINE
ThingState.Online.Status.ONLINE_STATUS_UNSUPPORTED -> Adapter.ThingState.Online.OnlineStatus.ONLINE_STATUS_UNSUPPORTED
})
onlineBuilder.setOnlineStatus(
when (state.status) {
ThingState.Online.Status.REPORTED_ONLINE -> Adapter.ThingState.Online.OnlineStatus.REPORTED_ONLINE
ThingState.Online.Status.REPORTED_OFFLINE -> Adapter.ThingState.Online.OnlineStatus.REPORTED_OFFLINE
ThingState.Online.Status.ONLINE_STATUS_UNSUPPORTED -> Adapter.ThingState.Online.OnlineStatus.ONLINE_STATUS_UNSUPPORTED
}
)

if (state.lastSeen != null) {
onlineBuilder.setLastSeen(state.lastSeen.toString())
Expand All @@ -251,6 +262,31 @@ object ProtobufSerializer {
.setOnline(onlineBuilder.build())
.build()
}

is ThingState.Network -> {
val networkBuilder = Adapter.ThingState.Network.newBuilder()

state.ip?.let { ip ->
networkBuilder.setIp(ip)
}
state.mac?.let { mac ->
networkBuilder.setMac(mac)
}
state.rssi?.let { rssi ->
networkBuilder.setRssi(rssi)
}
state.lastConnectedAt?.let { timestamp ->
networkBuilder.setLastConnectAtEpochSeconds(timestamp.epochSecond)
}
state.lastDisconnectedAt?.let { timestamp ->
networkBuilder.setLastDisconnectAtEpochSeconds(timestamp.epochSecond)
}

Adapter.ThingState.newBuilder()
.setLastUpdate(state.timestamp.toString())
.setNetwork(networkBuilder.build())
.build()
}
}
} ?: emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ sealed class ThingState {
}
}

data class Network(
override val timestamp: Instant,
override val thingId: ThingId,
val ip: String?,
val mac: String?,
val lastConnectedAt: Instant?,
val lastDisconnectedAt: Instant?,
val rssi: Int?
) : ThingState(), ComparableThingState {
override val key = Key(thingId, "thing.${thingId.value}.network")

override fun hasSameStateAs(other: ThingState): Boolean {
return other is Network && ip == other.ip && mac == other.mac && lastConnectedAt == other.lastConnectedAt && lastDisconnectedAt == other.lastDisconnectedAt && rssi == other.rssi
}

override fun toString(): String {
return "${thingId.value} network: ip=$ip, mac=$mac, last-connected=$lastConnectedAt, last-disconnected=$lastDisconnectedAt, rssi=$rssi"
}
}

data class DebugLog(
override val thingId: ThingId,
val maxLength: Int = DEFAULT_MAX_LENGTH,
Expand Down

0 comments on commit e1dc52a

Please sign in to comment.