Skip to content

Commit f9d9d4b

Browse files
committed
Save running animations to files to read in on restart
1 parent d856a30 commit f9d9d4b

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/main/java/animatedledstrip/server/AnimatedLEDStripServer.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import animatedledstrip.utils.delayBlocking
3434
import org.apache.commons.cli.DefaultParser
3535
import org.tinylog.Logger
3636
import org.tinylog.configuration.Configuration
37+
import java.io.File
3738
import java.io.FileInputStream
3839
import java.io.FileNotFoundException
3940
import java.util.*
@@ -126,6 +127,10 @@ class AnimatedLEDStripServer<T : AnimatedLEDStrip>(
126127
/* Start and stop methods */
127128

128129
fun start(): AnimatedLEDStripServer<T> {
130+
val dir = File(".animations")
131+
if (!dir.isDirectory)
132+
dir.mkdirs()
133+
129134
running = true
130135
Logger.debug { "Ports: $ports" }
131136
ports.forEach {

src/main/java/animatedledstrip/server/AnimationHandler.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import kotlinx.coroutines.GlobalScope
3131
import kotlinx.coroutines.launch
3232
import kotlinx.coroutines.newFixedThreadPoolContext
3333
import org.tinylog.Logger
34+
import java.io.File
35+
import java.io.FileInputStream
36+
import java.io.ObjectInputStream
3437
import java.lang.Math.random
3538

3639

@@ -52,6 +55,21 @@ internal class AnimationHandler(private val leds: AnimatedLEDStrip, threadCount:
5255
val continuousAnimations = mutableMapOf<String, ContinuousRunAnimation>()
5356

5457

58+
init {
59+
GlobalScope.launch {
60+
File(".animations/").walk().forEach {
61+
if (!it.isDirectory && it.name.endsWith(".anim")) try {
62+
ObjectInputStream(FileInputStream(it)).apply {
63+
val obj = readObject() as AnimationData
64+
addAnimation(obj)
65+
close()
66+
}
67+
} catch (e: ClassCastException) {
68+
}
69+
}
70+
}
71+
}
72+
5573
/**
5674
* Adds a new animation.
5775
*

src/main/java/animatedledstrip/server/ContinuousRunAnimation.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ package animatedledstrip.server
2626
import animatedledstrip.animationutils.Animation
2727
import animatedledstrip.animationutils.AnimationData
2828
import animatedledstrip.leds.AnimatedLEDStrip
29-
import kotlinx.coroutines.GlobalScope
30-
import kotlinx.coroutines.Job
31-
import kotlinx.coroutines.launch
29+
import kotlinx.coroutines.*
3230
import org.tinylog.Logger
31+
import java.io.File
32+
import java.io.FileOutputStream
33+
import java.io.ObjectOutputStream
34+
import java.nio.file.Files
35+
import java.nio.file.Paths
3336

3437
/**
3538
* Class for running an animation that repeats until stopped.
@@ -52,6 +55,8 @@ internal class ContinuousRunAnimation(
5255

5356
private var job: Job? = null
5457

58+
private val fileName = "$id.anim"
59+
5560

5661
init {
5762
sendStartAnimation() // Send animation to GUI
@@ -63,6 +68,14 @@ internal class ContinuousRunAnimation(
6368
*/
6469
fun runAnimation() {
6570
job = GlobalScope.launch(handler.animationThreadPool) {
71+
launch {
72+
withContext(Dispatchers.IO) {
73+
ObjectOutputStream(FileOutputStream(".animations/$fileName")).apply {
74+
writeObject(params)
75+
close()
76+
}
77+
}
78+
}
6679
Logger.trace { "params: $params" }
6780
while (continueAnimation) leds.run(params)
6881
sendEndAnimation()
@@ -78,6 +91,8 @@ internal class ContinuousRunAnimation(
7891
Logger.debug { "Animation $id ending" }
7992
if (continueAnimation) continueAnimation = false
8093
else job?.cancel()
94+
if (File(".animations/$id").exists())
95+
Files.delete(Paths.get(".animations/$fileName"))
8196
}
8297

8398

src/main/java/animatedledstrip/server/SocketConnections.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ object SocketConnections {
7777
var connected = false
7878
private set
7979
private var socOut: ObjectOutputStream? = null
80-
var textBased = false
8180

8281
/**
8382
* Open the connection

0 commit comments

Comments
 (0)