From 42e5a4bb51383e6fd77e239e07b7a440fa596141 Mon Sep 17 00:00:00 2001 From: Haydart Date: Tue, 7 Nov 2017 18:53:30 +0100 Subject: [PATCH 1/4] Refactor code --- .../rmakowiecki/smartalarmcore/AlarmController.kt | 5 +++-- .../peripheral/soundalarm/SoundAlarmPeriphery.kt | 5 +++++ .../remote/AlarmBackendInteractor.kt | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt diff --git a/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt b/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt index 32c8d8e..e85a94f 100644 --- a/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt +++ b/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt @@ -84,9 +84,10 @@ class AlarmController( .applyIoSchedulers() .subscribeBy( onNext = { - updateAlarmTriggerState(it) + // updateAlarmTriggerState(it) if (it == TRIGGERED) { - reportAlarmIncident(AlarmTriggerReason.MOTION_SENSOR) +// reportAlarmIncident(AlarmTriggerReason.MOTION_SENSOR) + logD("motion sensor triggered") } } ) diff --git a/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt b/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt new file mode 100644 index 0000000..2c4ee36 --- /dev/null +++ b/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt @@ -0,0 +1,5 @@ +package pl.rmakowiecki.smartalarmcore.peripheral.soundalarm + +class SoundAlarmPeriphery { + +} diff --git a/app/src/main/java/pl/rmakowiecki/smartalarmcore/remote/AlarmBackendInteractor.kt b/app/src/main/java/pl/rmakowiecki/smartalarmcore/remote/AlarmBackendInteractor.kt index 68650cb..6ec5e4e 100644 --- a/app/src/main/java/pl/rmakowiecki/smartalarmcore/remote/AlarmBackendInteractor.kt +++ b/app/src/main/java/pl/rmakowiecki/smartalarmcore/remote/AlarmBackendInteractor.kt @@ -133,12 +133,22 @@ class AlarmBackendInteractor(private val activity: AlarmActivity) : AlarmBackend } override fun uploadIncidentPhoto(photoBytes: ByteArray, uniqueIncidentId: String, photoNumber: Int): Single = Single.create { emitter -> + val photoFileName = "$uniqueIncidentId#$photoNumber.jpg" + storageNode.child(CORE_DEVICE_DIRECTORY) .child(IMAGES_DIRECTORY) .child(getCurrentBackendUser()?.uid ?: "non_assignable_incidents") - .child("$uniqueIncidentId#$photoNumber.jpg") + .child(photoFileName) .putBytes(photoBytes) - .addOnCompleteListener { emitter.onSuccess(it.isSuccessful) } + .addOnCompleteListener { + databaseNode + .child(getCurrentBackendUser()?.uid) + .child("incidents") + .child(uniqueIncidentId) + .child("photos") + .child("$photoNumber") + .setValue(it.isSuccessful) + } } } From a51b1e25a0cc36cbe0bdc660c9d50d28b9118a10 Mon Sep 17 00:00:00 2001 From: Haydart Date: Tue, 7 Nov 2017 19:01:53 +0100 Subject: [PATCH 2/4] Start & stop sound siren when needed --- .../java/pl/rmakowiecki/smartalarmcore/AlarmController.kt | 8 ++++++-- .../peripheral/soundalarm/SoundAlarmPeriphery.kt | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt b/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt index e85a94f..3829244 100644 --- a/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt +++ b/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmController.kt @@ -7,6 +7,7 @@ import pl.rmakowiecki.smartalarmcore.extensions.applyIoSchedulers import pl.rmakowiecki.smartalarmcore.extensions.logD import pl.rmakowiecki.smartalarmcore.peripheral.AlarmTriggerPeripheralDevice import pl.rmakowiecki.smartalarmcore.peripheral.camera.CameraPeripheryContract +import pl.rmakowiecki.smartalarmcore.peripheral.soundalarm.SoundAlarmPeriphery import pl.rmakowiecki.smartalarmcore.remote.AlarmBackendContract import pl.rmakowiecki.smartalarmcore.remote.models.AlarmTriggerReason import pl.rmakowiecki.smartalarmcore.remote.models.SecurityIncident @@ -16,6 +17,7 @@ class AlarmController( private val beamBreakDetector: AlarmTriggerPeripheralDevice, private val motionSensor: AlarmTriggerPeripheralDevice, private val camera: CameraPeripheryContract, + private val soundAlarmPeriphery: SoundAlarmPeriphery, private val backendInteractor: AlarmBackendContract, private val usbSetupProvider: UsbSetupProviderContract ) { @@ -71,7 +73,8 @@ class AlarmController( updateAlarmTriggerState(it) if (it == TRIGGERED) { reportAlarmIncident(AlarmTriggerReason.BEAM_BREAK_DETECTOR) - } + soundAlarmPeriphery.startSiren() + } else soundAlarmPeriphery.stopSiren() } ) } @@ -88,7 +91,8 @@ class AlarmController( if (it == TRIGGERED) { // reportAlarmIncident(AlarmTriggerReason.MOTION_SENSOR) logD("motion sensor triggered") - } + soundAlarmPeriphery.startSiren() + } else soundAlarmPeriphery.stopSiren() } ) } diff --git a/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt b/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt index 2c4ee36..09d86f4 100644 --- a/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt +++ b/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt @@ -2,4 +2,11 @@ package pl.rmakowiecki.smartalarmcore.peripheral.soundalarm class SoundAlarmPeriphery { + fun startSiren() { + + } + + fun stopSiren() { + + } } From a84ea25ff755a45ea1284e69f19e684783ff1380 Mon Sep 17 00:00:00 2001 From: Haydart Date: Tue, 7 Nov 2017 19:08:59 +0100 Subject: [PATCH 3/4] Implement siren alarm periphery --- .../soundalarm/SoundAlarmPeriphery.kt | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt b/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt index 09d86f4..f6e276e 100644 --- a/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt +++ b/app/src/main/java/pl/rmakowiecki/smartalarmcore/peripheral/soundalarm/SoundAlarmPeriphery.kt @@ -1,12 +1,38 @@ package pl.rmakowiecki.smartalarmcore.peripheral.soundalarm +import com.google.android.things.pio.Gpio +import com.google.android.things.pio.PeripheralManagerService +import pl.rmakowiecki.smartalarmcore.extensions.logE + +private const val PIN_NAME = "BCM21" + class SoundAlarmPeriphery { - fun startSiren() { + private lateinit var alarmSirenGpio: Gpio + init { + initAndRegisterGpioCallback() } - fun stopSiren() { + private fun initAndRegisterGpioCallback() { + val service = PeripheralManagerService() + try { + alarmSirenGpio = service.openGpio(PIN_NAME) + alarmSirenGpio.apply { + setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW) + } + + } catch (ex: Exception) { + ex.printStackTrace() + logE("GPIO exception") + } + } + fun startSiren() { + alarmSirenGpio.value = true + } + + fun stopSiren() { + alarmSirenGpio.value = false } } From 39723680266c4c16e28fb2a9f4e6ea35110befab Mon Sep 17 00:00:00 2001 From: Haydart Date: Tue, 7 Nov 2017 21:36:04 +0100 Subject: [PATCH 4/4] Fix build --- .../ExampleInstrumentedTest.java | 26 ------------------- .../smartalarmcore/AlarmActivity.kt | 3 ++- 2 files changed, 2 insertions(+), 27 deletions(-) delete mode 100644 app/src/androidTest/java/pl/rmakowiecki/smartalarmcore/ExampleInstrumentedTest.java diff --git a/app/src/androidTest/java/pl/rmakowiecki/smartalarmcore/ExampleInstrumentedTest.java b/app/src/androidTest/java/pl/rmakowiecki/smartalarmcore/ExampleInstrumentedTest.java deleted file mode 100644 index 8d5763c..0000000 --- a/app/src/androidTest/java/pl/rmakowiecki/smartalarmcore/ExampleInstrumentedTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package pl.rmakowiecki.smartalarmcore; - -import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.*; - -/** - * Instrumentation test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() throws Exception { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("pl.rmakowiecki.smartalarmcore", appContext.getPackageName()); - } -} diff --git a/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmActivity.kt b/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmActivity.kt index eb1ca6a..0347806 100644 --- a/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmActivity.kt +++ b/app/src/main/java/pl/rmakowiecki/smartalarmcore/AlarmActivity.kt @@ -8,6 +8,7 @@ import android.util.Log import pl.rmakowiecki.smartalarmcore.peripheral.beam.BeamBreakDetectorPeriphery import pl.rmakowiecki.smartalarmcore.peripheral.camera.CameraPeripheryContract import pl.rmakowiecki.smartalarmcore.peripheral.motion.MotionSensorPeriphery +import pl.rmakowiecki.smartalarmcore.peripheral.soundalarm.SoundAlarmPeriphery import pl.rmakowiecki.smartalarmcore.remote.AlarmBackendContract import pl.rmakowiecki.smartalarmcore.setup.UsbSetupProviderContract @@ -17,7 +18,6 @@ class AlarmActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) -// printWifiNetworkStatus() test alarmController = initSystemController() } @@ -25,6 +25,7 @@ class AlarmActivity : AppCompatActivity() { BeamBreakDetectorPeriphery(), MotionSensorPeriphery(), CameraPeripheryContract.create(this), + SoundAlarmPeriphery(), AlarmBackendContract.create(this), UsbSetupProviderContract.create(this) )