Skip to content

Commit 0f02298

Browse files
committed
Fix updated firebase function logic
1 parent 236034e commit 0f02298

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ class AlarmController(
103103
.reportSecurityIncident(SecurityIncident(AlarmTriggerReason.BEAM_BREAK_DETECTOR, reportTimestamp))
104104
.flatMapObservable { incidentModel ->
105105
cameraPhotoObservable
106-
.flatMapSingle { photo ->
107-
backendInteractor.uploadIncidentPhoto(photo, incidentModel.generatedId)
106+
.flatMapSingle { (photo, photoNumber) ->
107+
backendInteractor.uploadIncidentPhoto(photo, incidentModel.generatedId, photoNumber)
108108
}
109109
}.applyIoSchedulers()
110110
.subscribeBy(

app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/camera/CameraPeriphery.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ class CameraPeriphery(private var context: Context?) : CameraPeripheryContract {
2828
private var cameraCaptureSession: CameraCaptureSession? = null
2929
private var imageReadProcessor: ImageReader? = null
3030
private var cameraId: String = ""
31+
private var photosEmittedInSequence = 0
3132

32-
private val photoPublishSubject: PublishSubject<ByteArray> = PublishSubject.create()
33+
private val photoPublishSubject: PublishSubject<Pair<ByteArray, Int>> = PublishSubject.create()
3334

3435
private val cameraStateCallback = object : CameraDevice.StateCallback() {
3536
override fun onOpened(camera: CameraDevice) {
@@ -89,7 +90,7 @@ class CameraPeriphery(private var context: Context?) : CameraPeripheryContract {
8990
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
9091
val outputStream = ByteArrayOutputStream()
9192
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream)
92-
photoPublishSubject.onNext(outputStream.toByteArray())
93+
photoPublishSubject.onNext(Pair(outputStream.toByteArray(), ++photosEmittedInSequence))
9394
}
9495
}
9596

@@ -117,8 +118,9 @@ class CameraPeriphery(private var context: Context?) : CameraPeripheryContract {
117118
}
118119
}
119120

120-
override fun capturePhoto(): Observable<ByteArray> {
121+
override fun capturePhoto(): Observable<Pair<ByteArray, Int>> {
121122
takePicture()
123+
photosEmittedInSequence = 0
122124
return photoPublishSubject
123125
}
124126

app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/camera/CameraPeripheryContract.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import io.reactivex.Observable
66
interface CameraPeripheryContract {
77
fun openCamera()
88
fun closeCamera()
9-
fun capturePhoto(): Observable<ByteArray>
9+
fun capturePhoto(): Observable<Pair<ByteArray, Int>>
1010

1111
companion object {
1212
fun create(context: Context) = CameraPeriphery(context)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface AlarmBackendContract {
1212
fun observeAlarmArmingState(): Observable<AlarmArmingState>
1313
fun updateAlarmState(alarmState: AlarmTriggerState)
1414
fun reportSecurityIncident(securityIncident: SecurityIncident): Single<SecurityIncidentResponse>
15-
fun uploadIncidentPhoto(photoBytes: ByteArray, uniqueIncidentId: String): Single<Boolean>
15+
fun uploadIncidentPhoto(photoBytes: ByteArray, uniqueIncidentId: String, photoNumber: Int): 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ class AlarmBackendInteractor(private val activity: AlarmActivity) : AlarmBackend
125125
}
126126
}
127127

128-
override fun uploadIncidentPhoto(photoBytes: ByteArray, uniqueIncidentId: String): Single<Boolean> = Single.create { emitter ->
128+
override fun uploadIncidentPhoto(photoBytes: ByteArray, uniqueIncidentId: String, photoNumber: Int): Single<Boolean> = Single.create { emitter ->
129+
129130
storageNode.child(CORE_DEVICE_DIRECTORY)
130131
.child(IMAGES_DIRECTORY)
131132
.child(getCurrentBackendUser()?.uid ?: "non_assignable_incidents")
132-
.child("$uniqueIncidentId.jpg")
133+
.child("$uniqueIncidentId#$photoNumber.jpg")
133134
.putBytes(photoBytes)
134135
.addOnCompleteListener { emitter.onSuccess(it.isSuccessful) }
135136
}

functions/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ exports.generateThumbnail = functions.storage.object().onChange(event => {
133133
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);
134134

135135
const pathParts = fileDir.split('/')
136+
const fileNameParts = fileName.split('#')
136137

137138
const coreDeviceUid = pathParts[pathParts.length - 1]
138-
const incidentUid = fileName.slice(0, -4);
139+
const incidentUid = fileNameParts[0] //fileName is in format "x#y.jpg", where x is UID and y is the order number of the photo in photo sequence
139140

140141
// Exit if this is triggered on a file that is not an image.
141142
if (!event.data.contentType.startsWith('image/')) {
@@ -161,7 +162,7 @@ exports.generateThumbnail = functions.storage.object().onChange(event => {
161162
return;
162163
}
163164

164-
if (!fileName.endsWith('#1')) {
165+
if (!fileName.endsWith('#1.jpg')) {
165166
console.log('This is not the first photo in the sequence.');
166167
return;
167168
}

0 commit comments

Comments
 (0)