Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alarm siren #5

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,14 +18,14 @@ class AlarmActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// printWifiNetworkStatus() test
alarmController = initSystemController()
}

private fun initSystemController() = AlarmController(
BeamBreakDetectorPeriphery(),
MotionSensorPeriphery(),
CameraPeripheryContract.create(this),
SoundAlarmPeriphery(),
AlarmBackendContract.create(this),
UsbSetupProviderContract.create(this)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
) {
Expand Down Expand Up @@ -71,7 +73,8 @@ class AlarmController(
updateAlarmTriggerState(it)
if (it == TRIGGERED) {
reportAlarmIncident(AlarmTriggerReason.BEAM_BREAK_DETECTOR)
}
soundAlarmPeriphery.startSiren()
} else soundAlarmPeriphery.stopSiren()
}
)
}
Expand All @@ -84,10 +87,12 @@ class AlarmController(
.applyIoSchedulers()
.subscribeBy(
onNext = {
updateAlarmTriggerState(it)
// updateAlarmTriggerState(it)
if (it == TRIGGERED) {
reportAlarmIncident(AlarmTriggerReason.MOTION_SENSOR)
}
// reportAlarmIncident(AlarmTriggerReason.MOTION_SENSOR)
logD("motion sensor triggered")
soundAlarmPeriphery.startSiren()
} else soundAlarmPeriphery.stopSiren()
}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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 {

private lateinit var alarmSirenGpio: Gpio

init {
initAndRegisterGpioCallback()
}

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,22 @@ class AlarmBackendInteractor(private val activity: AlarmActivity) : AlarmBackend
}

override fun uploadIncidentPhoto(photoBytes: ByteArray, uniqueIncidentId: String, photoNumber: Int): Single<Boolean> = 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)
}
}
}

Expand Down