@@ -14,21 +14,21 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
1414import io.chirp.chirpsdk.interfaces.ChirpEventListener
1515import io.chirp.chirpsdk.models.ChirpSDKState
1616import io.chirp.chirpsdk.models.ChirpError
17+ import io.chirp.chirpsdk.models.ChirpErrorCode
1718
1819
19- class ChirpsdkPlugin (val activity : Activity ) : MethodCallHandler {
20+ class ChirpsdkPlugin (private val activity : Activity ) : MethodCallHandler {
2021
21- // TODO: Don't see the reason why we need multiple StreamHandlers for the same ChirpEventListener events.
22- // I don't think that StreamHandler is limited to one single event type.
2322 val stateStreamHandler = StateStreamHandler ()
2423 val sendingStreamHandler = SendingStreamHandler ()
2524 val sentStreamHandler = SentStreamHandler ()
2625 val receivingStreamHandler = ReceivingStreamHandler ()
2726 val receivedStreamHandler = ReceivedStreamHandler ()
2827 val errorStreamHandler = ErrorStreamHandler ()
2928
29+ lateinit var chirpSDK: ChirpSDK
30+
3031 companion object {
31- lateinit var chirpSDK: ChirpSDK
3232
3333 @JvmStatic
3434 fun registerWith (registrar : Registrar ) {
@@ -50,129 +50,170 @@ class ChirpsdkPlugin(val activity: Activity) : MethodCallHandler {
5050 }
5151 }
5252
53- override fun onMethodCall (call : MethodCall , result : Result ) {
54-
55- // TODO: I think a switch->case is a better option here instead of multiple if/else
56- if (call.method == " init" ) {
57- val arguments = call.arguments as java.util.HashMap <* , * >
58- val appKey = arguments[" key" ] as String
59- val appSecret = arguments[" secret" ] as String
60- chirpSDK = ChirpSDK (activity, appKey, appSecret)
53+ private fun isInitialised (call : MethodCall , result : Result ): Boolean {
54+ if (! ::chirpSDK.isInitialized) {
55+ val errorCode = ChirpErrorCode .CHIRP_SDK_NOT_INITIALISED .code.toString()
56+ result.error(errorCode, " ChirpSDK not initialised" , null )
57+ return false
6158 }
62- else if (call.method == " version" ) {
63- result.success(chirpSDK.version)
64- }
65- else if (call.method == " setConfig" ) {
66- val config: String = call.arguments as String
67- val error: ChirpError = chirpSDK.setConfig(config)
68- if (error.code > 0 ) {
69- // TODO: How do you know that errorHandler comes from this specific method call?
70- // Why not returning here instead? We are awaiting for the operation to complete anyway.
71- // Same for all other interface method calls.
72- errorStreamHandler.send(error.code, error.message)
73- } else {
74- chirpSDK.onSending { payload: ByteArray , channel: Int ->
75- /* *
76- * onSending is called when a send event begins.
77- * The data argument contains the payload being sent.
78- */
79- activity.runOnUiThread {
80- sendingStreamHandler.send(payload, channel)
81- }
82- }
83- chirpSDK.onSent { payload: ByteArray , channel: Int ->
84- /* *
85- * onSent is called when a send event has completed.
86- * The payload argument contains the payload data that was sent.
87- */
88- activity.runOnUiThread {
89- sentStreamHandler.send(payload, channel)
90- }
91- }
92- chirpSDK.onReceiving { channel: Int ->
93- /* *
94- * onReceiving is called when a receive event begins.
95- * No data has yet been received.
96- */
97- activity.runOnUiThread {
98- receivingStreamHandler.send(channel)
99- }
100- }
101- chirpSDK.onReceived { payload: ByteArray? , channel: Int ->
102- /* *
103- * onReceived is called when a receive event has completed.
104- * If the payload was decoded successfully, it is passed in payload.
105- * Otherwise, payload is null.
106- */
107- activity.runOnUiThread {
108- if (payload != null ) {
109- receivedStreamHandler.send(payload, channel)
110- } else {
111- // TODO: Decode failure is not an error and should not processed as an error
112- errorStreamHandler.send(0 , " Chirp: Decode failed." )
113- }
114- }
115- }
116- chirpSDK.onStateChanged { oldState: ChirpSDKState , newState: ChirpSDKState ->
117- /* *
118- * onStateChanged is called when the SDK changes state.
119- */
120- activity.runOnUiThread {
121- stateStreamHandler.send(oldState.code, newState.code)
122- }
123- }
124- chirpSDK.onSystemVolumeChanged { oldVolume: Float , newVolume: Float ->
125- /* *
126- * onSystemVolumeChanged is called when the system volume is changed.
127- */
128- }
59+ return true
60+ }
61+
62+ private fun init (call : MethodCall , result : Result ) {
63+ val arguments = call.arguments as java.util.HashMap <* , * >
64+ val appKey = arguments[" key" ] as String
65+ val appSecret = arguments[" secret" ] as String
66+ chirpSDK = ChirpSDK (activity, appKey, appSecret)
67+ result.success(ChirpErrorCode .CHIRP_SDK_OK .code)
68+ }
69+
70+ private fun version (call : MethodCall , result : Result ) {
71+ if (! isInitialised(call, result)) return ;
72+ result.success(chirpSDK.version)
73+ }
74+
75+ private fun setCallbacks () {
76+ chirpSDK.onSending { payload: ByteArray , channel: Int ->
77+ /* *
78+ * onSending is called when a send event begins.
79+ * The data argument contains the payload being sent.
80+ */
81+ activity.runOnUiThread {
82+ sendingStreamHandler.send(payload, channel)
12983 }
13084 }
131- else if (call.method == " start" ) {
132- val error: ChirpError = chirpSDK.start()
133- if (error.code > 0 ) {
134- errorStreamHandler.send(error.code, error.message)
85+ chirpSDK.onSent { payload: ByteArray , channel: Int ->
86+ /* *
87+ * onSent is called when a send event has completed.
88+ * The payload argument contains the payload data that was sent.
89+ */
90+ activity.runOnUiThread {
91+ sentStreamHandler.send(payload, channel)
13592 }
13693 }
137- else if (call.method == " stop" ) {
138- val error: ChirpError = chirpSDK.stop()
139- if (error.code > 0 ) {
140- errorStreamHandler.send(error.code, error.message)
94+ chirpSDK.onReceiving { channel: Int ->
95+ /* *
96+ * onReceiving is called when a receive event begins.
97+ * No data has yet been received.
98+ */
99+ activity.runOnUiThread {
100+ receivingStreamHandler.send(channel)
141101 }
142102 }
143- else if (call.method == " send" ) {
144- val payload = call.arguments as ByteArray
145- val error: ChirpError = chirpSDK.send(payload)
146- if (error.code > 0 ) {
147- errorStreamHandler.send(error.code, error.message)
103+ chirpSDK.onReceived { payload: ByteArray? , channel: Int ->
104+ /* *
105+ * onReceived is called when a receive event has completed.
106+ * If the payload was decoded successfully, it is passed in payload.
107+ * Otherwise, payload is null.
108+ */
109+ activity.runOnUiThread {
110+ receivedStreamHandler.send(payload, channel)
148111 }
149112 }
150- else if (call.method == " sendRandom" ) {
151- val payload = chirpSDK.randomPayload(0 )
152- val error: ChirpError = chirpSDK.send(payload)
153- if (error.code > 0 ) {
154- errorStreamHandler.send(error.code, error.message)
113+ chirpSDK.onStateChanged { oldState: ChirpSDKState , newState: ChirpSDKState ->
114+ /* *
115+ * onStateChanged is called when the SDK changes state.
116+ */
117+ activity.runOnUiThread {
118+ stateStreamHandler.send(oldState.code, newState.code)
155119 }
156120 }
157- else if (call.method == " isValidPayload" ) {
158- val payload = call.arguments as ByteArray
159- result.success(payload.size <= chirpSDK.maxPayloadLength())
160- }
161- else if (call.method == " getState" ) {
162- result.success(chirpSDK.getState().code)
163- }
164- else if (call.method == " maxPayloadLength" ) {
165- result.success(chirpSDK.maxPayloadLength())
121+ chirpSDK.onSystemVolumeChanged { oldVolume: Float , newVolume: Float ->
122+ /* *
123+ * onSystemVolumeChanged is called when the system volume is changed.
124+ */
166125 }
167- else if (call.method == " channelCount" ) {
168- result.success(chirpSDK.getChannelCount())
169- }
170- else if (call.method == " transmissionChannel" ) {
171- result.success(chirpSDK.getTransmissionChannel())
126+ }
127+
128+ private fun setConfig (call : MethodCall , result : Result ) {
129+ if (! isInitialised(call, result)) return
130+ val config: String = call.arguments as String
131+ val error: ChirpError = chirpSDK.setConfig(config)
132+
133+ // TODO: brainstorm if this is the best approach, maybe we should just report error here?
134+ result.success(error.code)
135+ if (error.code == 0 ) {
136+ setCallbacks()
172137 }
173- else {
174- result.notImplemented()
138+ }
139+
140+ private fun start (call : MethodCall , result : Result ) {
141+ if (! isInitialised(call, result)) return
142+ val error: ChirpError = chirpSDK.start()
143+ result.success(error.code)
144+ }
145+
146+ private fun stop (call : MethodCall , result : Result ) {
147+ if (! isInitialised(call, result)) return
148+ val error: ChirpError = chirpSDK.stop()
149+ result.success(error.code)
150+ }
151+
152+ private fun send (call : MethodCall , result : Result ) {
153+ if (! isInitialised(call, result)) return
154+ val payload = call.arguments as ByteArray
155+ val error: ChirpError = chirpSDK.send(payload)
156+ result.success(error.code)
157+ }
158+
159+ private fun randomPayload (call : MethodCall , result : Result ) {
160+ if (! isInitialised(call, result)) return
161+ result.success(chirpSDK.randomPayload(0 ))
162+ }
163+
164+ private fun isValidPayload (call : MethodCall , result : Result ) {
165+ if (! isInitialised(call, result)) return
166+ val payload = call.arguments as ByteArray
167+ result.success(payload.size <= chirpSDK.maxPayloadLength())
168+ }
169+
170+ private fun getState (call : MethodCall , result : Result ) {
171+ if (! isInitialised(call, result)) return
172+ result.success(chirpSDK.getState().code)
173+ }
174+
175+ private fun maxPayloadLength (call : MethodCall , result : Result ) {
176+ if (! isInitialised(call, result)) return
177+ result.success(chirpSDK.maxPayloadLength())
178+ }
179+
180+ private fun channelCount (call : MethodCall , result : Result ) {
181+ if (! isInitialised(call, result)) return
182+ result.success(chirpSDK.getChannelCount())
183+ }
184+
185+ private fun transmissionChannel (call : MethodCall , result : Result ) {
186+ if (! isInitialised(call, result)) return
187+ result.success(chirpSDK.getTransmissionChannel())
188+ }
189+
190+ private fun errorCodeToString (call : MethodCall , result : Result ) {
191+ if (! isInitialised(call, result)) return
192+ val code = call.arguments as Int
193+ result.success(ChirpError (code).message)
194+ }
195+
196+ override fun onMethodCall (call : MethodCall , result : Result ) {
197+
198+ when (call.method) {
199+ " init" -> init (call, result)
200+ " version" -> version(call, result)
201+ " setConfig" -> setConfig(call, result)
202+ " start" -> start(call, result)
203+ " stop" -> stop(call, result)
204+ " send" -> send(call, result)
205+ " randomPayload" -> randomPayload(call, result)
206+ " isValidPayload" -> isValidPayload(call, result)
207+ " getState" -> getState(call, result)
208+ " maxPayloadLength" -> maxPayloadLength(call, result)
209+ " channelCount" -> channelCount(call, result)
210+ " transmissionChannel" -> transmissionChannel(call, result)
211+ " errorCodeToString" -> errorCodeToString(call, result)
212+ else -> {
213+ result.notImplemented()
214+ }
175215 }
216+
176217 }
177218}
178219
@@ -250,7 +291,7 @@ class ReceivedStreamHandler : StreamHandler {
250291 eventSink = sink
251292 }
252293
253- fun send (data : ByteArray , channel : Int ) {
294+ fun send (data : ByteArray? , channel : Int ) {
254295 eventSink?.success(mapOf (" data" to data,
255296 " channel" to channel))
256297 }
0 commit comments