Skip to content

Commit

Permalink
[gateway] use dates instead of timestamp for states log
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed May 16, 2024
1 parent 5347016 commit a575f40
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
26 changes: 26 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/extensions/GsonBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.capcom.smsgateway.extensions

import android.os.Build
import com.google.gson.GsonBuilder
import java.util.TimeZone

fun GsonBuilder.setDateFormatISO8601() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.setDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
)
} else {
//get device timezone
val timeZone = TimeZone.getDefault()
this.setDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS" + when (timeZone.rawOffset) {
0 -> "Z"
else -> "+" + (timeZone.rawOffset / 3600000).toString().padStart(
2,
'0'
) + ":" + ((timeZone.rawOffset % 3600000) / 60000).toString()
.padStart(2, '0')
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.ktor.http.hostWithPort
import io.ktor.serialization.gson.gson
import me.capcom.smsgateway.BuildConfig
import me.capcom.smsgateway.domain.MessageState
import me.capcom.smsgateway.extensions.setDateFormatISO8601
import java.util.Date

class GatewayApi(
Expand All @@ -33,7 +34,9 @@ class GatewayApi(
agent = "me.capcom.smsgateway/" + BuildConfig.VERSION_NAME
}
install(ContentNegotiation) {
gson()
gson {
this.setDateFormatISO8601()
}
}
expectSuccess = true
}
Expand Down Expand Up @@ -95,7 +98,7 @@ class GatewayApi(
val id: String,
val state: MessageState,
val recipients: List<RecipientState>,
val states: Map<MessageState, Long>
val states: Map<MessageState, Date>
)

data class Message(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import me.capcom.smsgateway.modules.messages.data.SendParams
import me.capcom.smsgateway.modules.messages.data.SendRequest
import me.capcom.smsgateway.modules.messages.events.MessageStateChangedEvent
import me.capcom.smsgateway.services.PushService
import java.util.Date

class GatewayModule(
private val messagesService: MessagesService,
Expand Down Expand Up @@ -90,7 +91,7 @@ class GatewayModule(
it.error
)
},
message.states.associate { it.state.toApiState() to it.updatedAt }
message.states.associate { it.state.toApiState() to Date(it.updatedAt) }
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SendStateWorker(appContext: Context, params: WorkerParameters) :
}

companion object {
private const val RETRY_COUNT = 3
private const val RETRY_COUNT = 10

private const val MESSAGE_ID = "messageId"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import io.ktor.server.routing.routing
import me.capcom.smsgateway.R
import me.capcom.smsgateway.data.entities.Message
import me.capcom.smsgateway.domain.MessageState
import me.capcom.smsgateway.extensions.setDateFormatISO8601
import me.capcom.smsgateway.helpers.SettingsHelper
import me.capcom.smsgateway.modules.localserver.domain.Device
import me.capcom.smsgateway.modules.localserver.domain.PostMessageRequest
Expand All @@ -43,7 +44,6 @@ import me.capcom.smsgateway.modules.messages.data.SendRequest
import me.capcom.smsgateway.modules.notifications.NotificationsService
import org.koin.android.ext.android.inject
import java.util.Date
import java.util.TimeZone
import kotlin.concurrent.thread

class WebService : Service() {
Expand Down Expand Up @@ -77,24 +77,7 @@ class WebService : Service() {
if (me.capcom.smsgateway.BuildConfig.DEBUG) {
setPrettyPrinting()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.setDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
)
} else {
//get device timezone
val timeZone = TimeZone.getDefault()
this.setDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS" + when (timeZone.rawOffset) {
0 -> "Z"
else -> "+" + (timeZone.rawOffset / 3600000).toString().padStart(
2,
'0'
) + ":" + ((timeZone.rawOffset % 3600000) / 60000).toString()
.padStart(2, '0')
}
)
}
this.setDateFormatISO8601()
}
}
install(StatusPages) {
Expand Down

0 comments on commit a575f40

Please sign in to comment.