Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit d47a7dc

Browse files
daullmerJ0B10
authored andcommitted
first working version of tipeeestream input
1 parent 40dac62 commit d47a7dc

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ libraryDependencies += "net.dv8tion" % "JDA" % "3.8.3_463"
6464
//Serial Communication
6565
libraryDependencies += "com.fazecast" % "jSerialComm" % "[2.0.0,3.0.0)"
6666

67+
// Socket.io
68+
libraryDependencies += "io.socket" % "socket.io-client"% "1.0.0"
6769
// ---------------------------------------------------------------------------------------------------------------------
6870
// PLUGIN FRAMEWORK DEFINITIONS
6971
// ---------------------------------------------------------------------------------------------------------------------
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package org.codeoverflow.chatoverflow.requirement.service.tipeeestream
2+
3+
import java.util.function.Consumer
4+
5+
import io.socket.client.{IO, Socket}
6+
import org.codeoverflow.chatoverflow.WithLogger
7+
import org.codeoverflow.chatoverflow.api.io.dto.event.tipeeestream.{TipeeeStreamDonation, TipeeeStreamEvent, TipeeeStreamFollow, TipeeeStreamSubscription}
8+
import org.codeoverflow.chatoverflow.connector.Connector
9+
import org.json.{JSONException, JSONObject}
10+
11+
import scala.collection.mutable.ListBuffer
12+
13+
/**
14+
* The tipeeestream connector connects to the socket.io service to work with incoming events.
15+
*
16+
* @param sourceIdentifier the name of the tipeeestream account
17+
*/
18+
class TipeeeStreamConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger {
19+
private val eventHandler = ListBuffer[Consumer[TipeeeStreamEvent]]()
20+
private val apiKey = "apiKey"
21+
private val username = "username"
22+
override protected var requiredCredentialKeys: List[String] = List(apiKey, username)
23+
override protected var optionalCredentialKeys: List[String] = List()
24+
private var socket: Socket = _
25+
26+
/**
27+
* Starts the connector, e.g. creates a connection with its platform.
28+
*/
29+
override def start(): Boolean = {
30+
socket = IO.socket("https://sso-cf.tipeeestream.com").connect()
31+
socket.on("connect", (_: Any) => {
32+
logger info "Connected to TipeeStream Socket.io"
33+
})
34+
socket.emit("join-room", getAuthenticationObject)
35+
logger info "emitted credentials to TipeeSetream Socket.io api"
36+
socket.on("new-event", (objects: Array[AnyRef]) => {
37+
serializeObjectToObject(objects)
38+
})
39+
true
40+
}
41+
42+
def addIncomingEventHandler(handler: Consumer[TipeeeStreamEvent]): Unit = {
43+
eventHandler += handler
44+
}
45+
46+
private def serializeObjectToObject(objects : Array[AnyRef]) : Unit = {
47+
val json: JSONObject = objects(0).asInstanceOf[JSONObject]
48+
val event: JSONObject = json.getJSONObject("event")
49+
val eventType: String = event.getString("type")
50+
eventType match {
51+
case "subscription" =>
52+
Subscription(event)
53+
case "donation" =>
54+
Donation(event)
55+
case "follow" =>
56+
Follow(event)
57+
case _ =>
58+
}
59+
}
60+
61+
@throws[JSONException]
62+
private def Donation(event: JSONObject): Unit = {
63+
val parameter = event.getJSONObject("parameters")
64+
val user = parameter.getString("username")
65+
val message = parameter.getString("formattedMessage")
66+
val amount = parameter.getDouble("amount")
67+
val donation: TipeeeStreamDonation = new TipeeeStreamDonation(null, user, message, amount, null, null)
68+
eventHandler.foreach(_.accept(donation))
69+
}
70+
71+
@throws[JSONException]
72+
private def Subscription(event: JSONObject): Unit = {
73+
val parameter = event.getJSONObject("parameters")
74+
val user = parameter.getString("username")
75+
val message = parameter.getString("formattedMessage")
76+
val resub = parameter.getInt("resub")
77+
val subscription: TipeeeStreamSubscription = new TipeeeStreamSubscription(null, user, message, resub)
78+
eventHandler.foreach(_.accept(subscription))
79+
}
80+
81+
@throws[JSONException]
82+
private def Follow(event: JSONObject): Unit = {
83+
val parameter = event.getJSONObject("parameters")
84+
val user = parameter.getString("username")
85+
val message = parameter.getString("message")
86+
val follow: TipeeeStreamFollow = new TipeeeStreamFollow(null, user, message)
87+
eventHandler.foreach(_.accept(follow))
88+
}
89+
90+
/**
91+
* This stops the activity of the connector, e.g. by closing the platform connection.
92+
*/
93+
override def stop(): Boolean = {
94+
socket.close()
95+
true
96+
}
97+
98+
private def getAuthenticationObject: JSONObject = {
99+
val obj = new JSONObject()
100+
obj.put("room", credentials.get.getValue(apiKey).get)
101+
obj.put("username", credentials.get.getValue(username).get)
102+
obj
103+
}
104+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.codeoverflow.chatoverflow.requirement.service.tipeeestream
2+
3+
import org.codeoverflow.chatoverflow.api.io.dto.event.tipeeestream.TipeeeStreamEvent
4+
5+
import scala.collection.mutable.ListBuffer
6+
7+
class TipeeeStreamListener {
8+
9+
private val messageEventListener = ListBuffer[TipeeeStreamEvent => Unit]()
10+
11+
def onMessage(event: TipeeeStreamEvent): Unit = {
12+
messageEventListener.foreach(listener => listener(event))
13+
}
14+
15+
def addMessageEventListener(listener: TipeeeStreamEvent => Unit): Unit = {
16+
messageEventListener += listener
17+
}
18+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.codeoverflow.chatoverflow.requirement.service.tipeeestream.impl
2+
3+
import java.util.function.Consumer
4+
5+
import org.codeoverflow.chatoverflow.WithLogger
6+
import org.codeoverflow.chatoverflow.api.io.dto.event.tipeeestream.{TipeeeStreamDonation, TipeeeStreamEvent, TipeeeStreamFollow, TipeeeStreamSubscription}
7+
import org.codeoverflow.chatoverflow.api.io.input.chat.TipeeeStreamInput
8+
import org.codeoverflow.chatoverflow.registry.Impl
9+
import org.codeoverflow.chatoverflow.requirement.InputImpl
10+
import org.codeoverflow.chatoverflow.requirement.service.tipeeestream.TipeeeStreamConnector
11+
12+
import scala.collection.mutable.ListBuffer
13+
14+
@Impl(impl = classOf[TipeeeStreamInput], connector = classOf[TipeeeStreamConnector])
15+
class TipeeeStreamInputImpl extends InputImpl[TipeeeStreamConnector] with TipeeeStreamInput with WithLogger {
16+
private val donationHandler = ListBuffer[Consumer[TipeeeStreamDonation]]()
17+
private val subscriptionHandler = ListBuffer[Consumer[TipeeeStreamSubscription]]()
18+
private val followHandler = ListBuffer[Consumer[TipeeeStreamFollow]]()
19+
20+
def onEvent[T <: TipeeeStreamEvent] (event: T ): Unit = {
21+
event match {
22+
case event: TipeeeStreamDonation => donationHandler.foreach(_.accept(event.asInstanceOf[TipeeeStreamDonation]))
23+
case event: TipeeeStreamFollow => followHandler.foreach(_.accept(event.asInstanceOf[TipeeeStreamFollow]))
24+
case event: TipeeeStreamSubscription => subscriptionHandler.foreach(_.accept(event.asInstanceOf[TipeeeStreamSubscription]))
25+
}
26+
}
27+
28+
override def start(): Boolean = {
29+
sourceConnector.get.addIncomingEventHandler(onEvent)
30+
true
31+
}
32+
33+
/**
34+
* Let's you register a simple handler immediately react on new subscriptions
35+
*
36+
* @param handler the consumer t ehandle incoming messages
37+
*/
38+
override def registerSubscriptionHandler(handler: Consumer[TipeeeStreamSubscription]): Unit = {
39+
subscriptionHandler += handler
40+
}
41+
42+
override def registerDonationHandler(handler: Consumer[TipeeeStreamDonation]): Unit = {
43+
donationHandler += handler
44+
}
45+
46+
override def registerFollowHandler(handler: Consumer[TipeeeStreamFollow]): Unit = {
47+
followHandler += handler
48+
}
49+
}

0 commit comments

Comments
 (0)