Skip to content

Commit 6ab498f

Browse files
committed
1. 暂时添加了memory和stateswitch
1 parent 59a9c33 commit 6ab498f

File tree

7 files changed

+64
-27
lines changed

7 files changed

+64
-27
lines changed

wechaty-puppet/src/main/kotlin/Puppet.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import io.github.wechaty.io.github.wechaty.watchdag.WatchDog
1414
import io.github.wechaty.io.github.wechaty.watchdag.WatchdogFood
1515
import io.github.wechaty.io.github.wechaty.watchdag.WatchdogListener
1616
import io.github.wechaty.listener.*
17+
import io.github.wechaty.memorycard.MemoryCard
1718
import io.github.wechaty.schemas.*
1819
import io.github.wechaty.utils.FutureUtils
1920
import io.github.wechaty.utils.JsonUtils
@@ -42,7 +43,7 @@ abstract class Puppet : EventEmitter {
4243
private val HEARTBEAT_COUNTER = AtomicLong()
4344
private val HOSTIE_KEEPALIVE_TIMEOUT = 15 * 1000L
4445
private val DEFAULT_WATCHDOG_TIMEOUT = 60L
45-
// private var memory: MemoryCard
46+
private var memory: MemoryCard
4647

4748
private val executorService = Executors.newSingleThreadScheduledExecutor()
4849

@@ -66,9 +67,15 @@ abstract class Puppet : EventEmitter {
6667
count.addAndGet(1)
6768
this.puppetOptions = puppetOptions
6869

69-
// this.memory = MemoryCard()
70-
// this.memory.load()
71-
70+
// for test
71+
this.memory = MemoryCard()
72+
try {
73+
this.memory.load()
74+
log.debug("Puppet, constructor() memory.load() done")
75+
}
76+
catch (e: Exception) {
77+
log.warn("Puppet, constructor() memory.load() rejection: {}", e)
78+
}
7279

7380
val timeOut = puppetOptions.timeout ?: DEFAULT_WATCHDOG_TIMEOUT
7481
watchDog = WatchDog(1000 * timeOut, "puppet")
@@ -911,13 +918,14 @@ abstract class Puppet : EventEmitter {
911918
}
912919
}
913920

914-
// fun setMemory(memoryCard: MemoryCard){
915-
// this.memory = memoryCard
916-
// }
921+
fun setMemory(memoryCard: MemoryCard) {
922+
log.debug("Puppet, setMemory()")
923+
this.memory = memoryCard
924+
}
917925

918-
// fun getEventBus(): EventBus {
919-
// return eb
920-
// }
926+
override fun toString(): String {
927+
return "Puppet#${count}<${this.puppetOptions?.name}>(${this.memory.getName()})"
928+
}
921929

922930
companion object {
923931
private val log = LoggerFactory.getLogger(Puppet::class.java)

wechaty/src/main/kotlin/io/github/wechaty/Wechaty.kt

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import io.github.wechaty.eventEmitter.EventEmitter
66
import io.github.wechaty.eventEmitter.Listener
77
import io.github.wechaty.io.github.wechaty.schemas.EventEnum
88
import io.github.wechaty.listener.*
9-
//import io.github.wechaty.memorycard.MemoryCard
9+
import io.github.wechaty.memorycard.MemoryCard
1010
import io.github.wechaty.schemas.*
11+
import io.github.wechaty.status.StateSwitch
1112
import io.github.wechaty.user.*
1213
import io.github.wechaty.user.manager.*
1314
import org.slf4j.LoggerFactory
1415
import java.util.*
1516
import java.util.concurrent.locks.ReentrantLock
1617

17-
18+
val PUPPET_MEMORY_NAME = "puppet"
1819
class Wechaty private constructor(private var wechatyOptions: WechatyOptions) : EventEmitter() {
1920

2021
private val LOCK = ReentrantLock()
@@ -26,12 +27,12 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
2627
private val globalPluginList: MutableList<WechatyPlugin> = mutableListOf()
2728

2829
@Volatile
29-
private var readyState = StateEnum.OFF
30+
private var readyState = StateSwitch("Wechaty")
3031

3132
@Volatile
32-
private var status = StateEnum.OFF
33+
private var status = StateSwitch("WechatyReady")
3334

34-
// private var memory:MemoryCard? = null
35+
private var memory: MemoryCard? = null
3536

3637
val tagManager: TagManager = TagManager(this)
3738
val contactManager = ContactManager(this)
@@ -41,16 +42,21 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
4142
val imageManager = ImageManager(this)
4243

4344
init {
44-
// this.memory = wechatyOptions.memory
45+
if (wechatyOptions.memory == null) {
46+
this.memory = MemoryCard(wechatyOptions.name)
47+
}
48+
else {
49+
this.memory = wechatyOptions.memory
50+
}
4551
installGlobalPlugin()
4652
}
4753

4854

4955
fun start(await: Boolean = false):Wechaty {
50-
5156
initPuppet()
5257
puppet.start().get()
53-
status = StateEnum.ON
58+
// status = StateEnum.ON
59+
status.on(StateEnum.ON)
5460
emit(EventEnum.START, "")
5561

5662
if (await) {
@@ -220,6 +226,11 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
220226
private fun initPuppet() {
221227
// this.puppet = GrpcPuppet(puppetOptions)
222228
this.puppet = PuppetManager.resolveInstance(wechatyOptions).get()
229+
if (this.memory == null) {
230+
throw Exception("no memory")
231+
}
232+
val puppetMemory = this.memory!!.multiplex(PUPPET_MEMORY_NAME)
233+
this.puppet.setMemory(puppetMemory)
223234
initPuppetEventBridge(puppet)
224235
}
225236

@@ -317,7 +328,8 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
317328
puppet.on(it, object : PuppetReadyListener {
318329
override fun handler(payload: EventReadyPayload) {
319330
emit(EventEnum.READY);
320-
readyState = StateEnum.ON
331+
// readyState = StateEnum.ON
332+
readyState.on(StateEnum.ON)
321333
}
322334
})
323335
}
@@ -438,6 +450,26 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
438450
}
439451
}, "StartMain-shutdown-hook"))
440452
}
453+
454+
override fun toString(): String {
455+
if (this.wechatyOptions == null) {
456+
return "default"
457+
}
458+
val first = if (this.wechatyOptions != null && this.puppet != null) {
459+
this.wechatyOptions.puppet
460+
}
461+
else {
462+
"puppet"
463+
}
464+
465+
val second = if (this.memory != null) {
466+
this.memory!!.getName()
467+
}
468+
else {
469+
"default"
470+
}
471+
return "Wechaty#<${first}><${second}>"
472+
}
441473
}
442474

443475

wechaty/src/main/kotlin/io/github/wechaty/WechatyListener.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,5 @@ interface ReadyListener {
7777
@FunctionalInterface
7878
interface MessageListener {
7979
fun handler(message: Message)
80-
8180
}
8281

wechaty/src/main/kotlin/io/github/wechaty/WechatyOptions.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.github.wechaty
22

3-
//import io.github.wechaty.memorycard.MemoryCard
3+
import io.github.wechaty.memorycard.MemoryCard
44
import io.github.wechaty.schemas.PuppetOptions
55

66
class WechatyOptions {
77

8-
// var memory:MemoryCard? = null
8+
var memory: MemoryCard? = null
99

1010
var name:String = "Wechaty"
1111

@@ -17,5 +17,5 @@ class WechatyOptions {
1717
var ioToken:String? = null
1818

1919
}
20-
typealias WechatyPlugin = (Wechaty) -> Unit
20+
typealias WechatyPlugin = (Wechaty) -> Unit
2121

wechaty/src/main/kotlin/io/github/wechaty/user/manager/RoomInvitationManager.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import org.slf4j.LoggerFactory
99
class RoomInvitationManager (wechaty: Wechaty):Accessory(wechaty){
1010

1111

12-
fun load(id:String):RoomInvitation{
12+
fun load(id:String): RoomInvitation {
1313
return RoomInvitation(wechaty,id)
1414
}
1515

16-
1716
companion object {
1817
private val log = LoggerFactory.getLogger(Contact::class.java)
1918
}

wechaty/src/main/kotlin/io/github/wechaty/user/manager/TagManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TagManager(wechaty: Wechaty):Accessory(wechaty){
1818
}!!
1919
}
2020

21-
fun get(id:String):Tag{
21+
fun get(id:String): Tag{
2222
return load(id)
2323
}
2424

wechaty/src/main/resources/log4j2.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
</AsyncRoot>
1212

1313
<Logger name="io.github.wechaty" level="DEBUG" >
14-
<!-- <AppenderRef ref="Console"/>-->
1514
</Logger>
1615
</Loggers>
1716
</Configuration>

0 commit comments

Comments
 (0)