11package io.github.wechaty
22
3- import io.github.wechaty.Puppet
43import io.github.wechaty.filebox.FileBox
54import io.github.wechaty.schemas.*
65import org.slf4j.LoggerFactory
6+ import java.util.*
7+ import java.util.concurrent.CompletableFuture
78import java.util.concurrent.Future
9+ import kotlin.concurrent.scheduleAtFixedRate
810
911class MockPuppet (puppetOptions : PuppetOptions ) : Puppet(puppetOptions) {
12+
13+ private val timer: Timer
14+
15+ init {
16+ timer = Timer ()
17+ }
18+
1019 override fun start (): Future <Void > {
1120
1221 log.info(" MockPuppet start()" )
@@ -29,20 +38,55 @@ class MockPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
2938
3039
3140 val userPayload = MockData .getFakeContactPayload()
32- TODO ( " Not yet implemented " )
41+ cacheContactPayload.put(userPayload.id, userPayload )
3342
43+ setId(userPayload.id)
44+
45+ emit(" login" , EventLoginPayload (userPayload.id))
46+
47+ timer.scheduleAtFixedRate(0 , 5000 ) {
48+ val fromContactPayload = MockData .getFakeContactPayload()
49+ cacheContactPayload.put(fromContactPayload.id, fromContactPayload)
50+ val messagePayload = MockData .getMessagePayload(fromContactPayload.id, userPayload.id)
51+
52+ cacheMessagePayload.put(messagePayload.id, messagePayload)
53+ log.info(" MockPuppet start() schedule pretending received a new message:${messagePayload.id} " )
54+ emit(" message" , EventMessagePayload (messagePayload.id))
55+ }
56+ return CompletableFuture .completedFuture(null )
3457 }
3558
3659 override fun stop (): Future <Void > {
37- TODO (" Not yet implemented" )
60+ log.info(" MockPuppet stop()" )
61+ // TODO StateSwitch
62+ /*
63+ if (this.state.off()) {
64+ log.warn('PuppetMock', 'stop() is called on a OFF puppet. await ready(off) and return.')
65+ await this.state.ready('off')
66+ return
67+ }
68+
69+ this.state.off('pending')
70+ */
71+ timer.cancel()
72+
73+ // this.state.off(true)
74+ return CompletableFuture .completedFuture(null )
3875 }
3976
4077 override fun logout (): Future <Void > {
41- TODO (" Not yet implemented" )
78+ log.info(" MockPuppet logout()" )
79+ val id = getId() ? : throw Exception (" logout before login?" )
80+
81+ emit(" logout" , EventLogoutPayload (id, " test" ))
82+ setId(null )
83+
84+ return CompletableFuture .completedFuture(null )
4285 }
4386
4487 override fun ding (data : String? ) {
45- TODO (" Not yet implemented" )
88+ log.info(" MockPuppet ding($data ?:'')" )
89+ emit(" dong" , EventDongPayload (data ? : " " ))
4690 }
4791
4892 override fun contactSelfName (name : String ): Future <Void > {
0 commit comments