Skip to content

Commit 2ddc5ca

Browse files
committed
Update thumbnail generation function to write thumbnail url to proper node
1 parent 0f98707 commit 2ddc5ca

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ class AlarmController(
7575
private fun reportBeamBreakIncident() {
7676
val reportTimestamp = System.currentTimeMillis()
7777

78-
cameraPhotoSessionDisposable = camera.capturePhoto()
79-
.flatMapSingle { backendInteractor.uploadIncidentPhoto(it, reportTimestamp) }
80-
.flatMapSingle {
81-
backendInteractor.reportSecurityIncident(
82-
SecurityIncident(AlarmTriggerReason.BEAM_BREAK_DETECTOR, reportTimestamp)
83-
)
78+
backendInteractor
79+
.reportSecurityIncident(SecurityIncident(AlarmTriggerReason.BEAM_BREAK_DETECTOR, reportTimestamp))
80+
.flatMapObservable { incidentModel ->
81+
camera.capturePhoto()
82+
.flatMapSingle { photo ->
83+
backendInteractor.uploadIncidentPhoto(photo, incidentModel.generatedId)
84+
}
8485
}.applyIoSchedulers()
8586
.subscribeBy(
8687
onNext = { logD("Security incident report successful? $it") }

app/src/main/java/pl/rmakowiecki/smartalarmcore/remote/AlarmBackendContract.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ interface AlarmBackendContract {
1111
fun signInToBackend(): Single<Boolean>
1212
fun observeAlarmArmingState(): Observable<AlarmArmingState>
1313
fun updateAlarmState(alarmState: AlarmTriggerState)
14-
fun reportSecurityIncident(securityIncident: SecurityIncident): Single<Boolean>
15-
fun uploadIncidentPhoto(photoBytes: ByteArray, reportTimestamp: Long): Single<Boolean>
14+
fun reportSecurityIncident(securityIncident: SecurityIncident): Single<SecurityIncidentResponse>
15+
fun uploadIncidentPhoto(photoBytes: ByteArray, uniqueIncidentId: String): Single<Boolean>
1616

1717
companion object {
1818
fun create(activity: AlarmActivity) = AlarmBackendInteractor(activity)

app/src/main/java/pl/rmakowiecki/smartalarmcore/remote/AlarmBackendInteractor.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,38 @@ class AlarmBackendInteractor(private val activity: AlarmActivity) : AlarmBackend
9191
.addOnCompleteListener { }
9292
}
9393

94-
override fun reportSecurityIncident(securityIncident: SecurityIncident): Single<Boolean> = Single.create { emitter ->
95-
databaseNode.child(getCurrentBackendUser()?.uid)
94+
override fun reportSecurityIncident(securityIncident: SecurityIncident): Single<SecurityIncidentResponse> = Single.create { emitter ->
95+
val securityIncidentNodeReference = databaseNode.child(getCurrentBackendUser()?.uid)
9696
.child(Nodes.INCIDENTS)
9797
.push()
98+
99+
securityIncidentNodeReference
98100
.setValue(RemoteSecurityIncident.from(securityIncident))
99-
.addOnCompleteListener { emitter.onSuccess(it.isSuccessful) }
101+
.addOnCompleteListener {
102+
emitter.onSuccess(
103+
SecurityIncidentResponse(it.isSuccessful, securityIncidentNodeReference.key)
104+
)
105+
}
100106
}
101107

102-
override fun uploadIncidentPhoto(photoBytes: ByteArray, reportTimestamp: Long): Single<Boolean> = Single.create { emitter ->
108+
override fun uploadIncidentPhoto(photoBytes: ByteArray, uniqueIncidentId: String): Single<Boolean> = Single.create { emitter ->
103109
storageNode.child(CORE_DEVICE_DIRECTORY)
104110
.child(IMAGES_DIRECTORY)
105111
.child(getCurrentBackendUser()?.uid ?: "non_assignable_incidents")
106-
.child("$reportTimestamp.jpg")
112+
.child("$uniqueIncidentId.jpg")
107113
.putBytes(photoBytes)
108114
.addOnCompleteListener { emitter.onSuccess(it.isSuccessful) }
109115
}
110116
}
111117

112118
private fun DataSnapshot.getArmingState() = (this.value as Boolean).toArmingState()
113119

114-
class RemoteAlarmStateModel(val active: Boolean, val triggered: Boolean)
120+
class RemoteAlarmStateModel(
121+
val active: Boolean,
122+
val triggered: Boolean
123+
)
124+
125+
class SecurityIncidentResponse(
126+
val isSuccessful: Boolean,
127+
val generatedId: String
128+
)

functions/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ exports.generateThumbnail = functions.storage.object().onChange(event => {
7878
const tempLocalDir = path.dirname(tempLocalFile);
7979
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);
8080

81+
const pathParts = fileDir.split('/')
82+
83+
const coreDeviceUid = pathParts[pathParts.length - 1]
84+
const incidentUid = fileName.slice(0, -4);
85+
8186
// Exit if this is triggered on a file that is not an image.
8287
if (!event.data.contentType.startsWith('image/')) {
8388
console.log('This is not an image.');
@@ -139,7 +144,7 @@ exports.generateThumbnail = functions.storage.object().onChange(event => {
139144
const originalResult = results[1];
140145
const thumbFileUrl = thumbResult[0];
141146
const fileUrl = originalResult[0];
142-
// Add the URLs to the Database
143-
return admin.database().ref('images').push({path: fileUrl, thumbnail: thumbFileUrl});
147+
148+
return admin.database().ref(`/${coreDeviceUid}/incidents/${incidentUid}/thumbnailUrl`).set(thumbFileUrl);
144149
}).then(() => console.log('Thumbnail URLs saved to database.'));
145150
});

0 commit comments

Comments
 (0)