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

CCAPI ver110 support #184

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.8.0 - 2021-10-30

### Added

- Support for CCAPI ver110. This breaks compatibility with devices activated for ver100.

### Changed

#### UI/UX

#### Code Quality

## 1.7.0 - 2020-10-12

### Added
Expand Down
11 changes: 6 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "io.numbersprotocol.starlingcapture"
minSdkVersion 26
targetSdkVersion 30
versionCode 7
versionName "1.7.0"
versionCode 8
versionName "1.8.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -117,9 +117,10 @@ dependencies {
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"

implementation "androidx.room:room-runtime:2.2.5"
implementation "androidx.room:room-ktx:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
implementation "androidx.room:room-runtime:2.3.0"
implementation "androidx.room:room-ktx:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
kapt "org.xerial:sqlite-jdbc:3.34.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for compiling on M1 Macs.


implementation "com.google.android.material:material:1.3.0-alpha03"

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="io.numbersprotocol.starlingcapture">

<uses-feature
Expand All @@ -9,6 +10,10 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
Comment on lines +13 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably unnecessary since I commented out the WiFi network binding.



<application
android:name=".BaseApplication"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ suspend fun CanonCameraControlApi.waitUntilConnected(

@JsonClass(generateAdapter = true)
data class Api(
val url: String,
val path: String,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the big breaking change.

val get: Boolean,
val post: Boolean,
val put: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.numbersprotocol.starlingcapture.source.canon

import android.app.Service
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import androidx.core.app.NotificationCompat
import io.numbersprotocol.starlingcapture.R
import io.numbersprotocol.starlingcapture.collector.ProofCollector
Expand All @@ -21,21 +26,39 @@ class CanonCameraControlService : Service(), CoroutineScope by CoroutineScope(Di
private val notificationUtil: NotificationUtil by inject()
private val foregroundNotificationId = notificationUtil.createNotificationId()
private val errorNotificationId = notificationUtil.createNotificationId()
private lateinit var address: String
private lateinit var canonCameraControlApi: CanonCameraControlApi
private val proofCollector: ProofCollector by inject()
private lateinit var foregroundNotificationBuilder: NotificationCompat.Builder
private val beforeDestroyCallbacks = mutableSetOf<() -> Unit>()

@FlowPreview
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
// bindProcessToWiFiNetwork()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expectedly breaks Internet traffic. Proper fix will come from #22

initializeCanonCameraControlApi(intent)
startForeground()
connectToCamera()
return super.onStartCommand(intent, flags, startId)
}

/**
* Default to WiFi network transport (even when Internet in unavailable on that interface) since
* our camera has no Internet connectivity. This tells Android to still connect to the local IP
* when mobile data is available on the phone.
*/
private fun bindProcessToWiFiNetwork() {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val request = NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build()
val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
connectivityManager.bindProcessToNetwork(network)
}
}
connectivityManager?.requestNetwork(request, networkCallback)
}

private fun initializeCanonCameraControlApi(intent: Intent) {
val address = intent.extras!!.getString(CAMERA_ADDRESS)!!
address = intent.extras!!.getString(CAMERA_ADDRESS)!!
canonCameraControlApi = CanonCameraControlApi.create(address)
}

Expand All @@ -52,6 +75,7 @@ class CanonCameraControlService : Service(), CoroutineScope by CoroutineScope(Di

@FlowPreview
private fun connectToCamera() = launch {
Timber.v("Connecting to camera...")
canonCameraControlApi.waitUntilConnected { e ->
if (e !is CancellationException) {
notificationUtil.notifyException(e, foregroundNotificationId)
Expand Down Expand Up @@ -80,20 +104,25 @@ class CanonCameraControlService : Service(), CoroutineScope by CoroutineScope(Di
}
}

private fun getContentFlow(url: String) = flow {
private fun getContentFlow(path: String) = flow {
val url = "http://$address$path"
Timber.v("Getting content stream at $url")
emit(MediaStream(canonCameraControlApi.getContent(url).byteStream(), MimeType.fromUrl(url)))
}.retry { e ->
(e is HttpException && e.code() == 503).also {
Timber.e("Getting content stream failed with error: %s", e.message)
Timber.w("Service is currently unavailable. Is the shooting processing in progress?")
delay(POLLING_INTERVAL_MILLIS)
}
}

private suspend fun storeAndCollect(mediaStream: MediaStream) {
Timber.v("Storing new content...")
val cachedFile = createCachedFile(mediaStream)
cachedFile.copyFromInputStream(mediaStream.inputStream)
proofCollector.storeAndCollect(cachedFile, mediaStream.mimeType)
cachedFile.delete()
Timber.i("New content stored.")
}

private fun createCachedFile(mediaStream: MediaStream) = File.createTempFile(
Expand Down