-
Notifications
You must be signed in to change notification settings - Fork 5
/
mParticleTask.brs
79 lines (74 loc) · 3.15 KB
/
mParticleTask.brs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'*************************************************************
' mParticle Roku SDK - mParticle Task Node for Scene Graph
' Copyright 2019 mParticle, Inc.
'*************************************************************
sub init()
m.port = createObject("roMessagePort")
m.top.observeField("mParticleApiCall", m.port)
m.top.functionName = "setupRunLoop"
m.top.control = "RUN"
end sub
sub setupRunLoop()
options = m.global.mparticleOptions
options.addReplace("batchUploads", true)
mParticleStart(options, m.port)
m.mparticle = mparticle()
m.top[mParticleConstants().SCENEGRAPH_NODES.CURRENT_USER_NODE] = m.mparticle.identity.getCurrentUser()
m.top[mParticleConstants().SCENEGRAPH_NODES.CURRENT_SESSION_NODE] = m.mparticle._internal.sessionManager.getCurrentSession()
while true
msg = wait(15 * 1000, m.port)
if (msg = invalid) then
m.mparticle._internal.sessionManager.updateLastEventTime(m.mparticle._internal.utils.unixTimeMillis())
m.mparticle._internal.networking.queueUpload()
m.mparticle._internal.networking.processUploads()
else
mt = type(msg)
if mt = "roSGNodeEvent"
if msg.getField() = mParticleConstants().SCENEGRAPH_NODES.API_CALL_NODE
executeApiCall(msg.getData())
end if
else if (mt = "roUrlEvent")
if m.mparticle.isMparticleEvent(msg.getSourceIdentity())
identityResult = m.mparticle.onUrlEvent(msg)
if (identityResult <> invalid) then
m.top[mParticleConstants().SCENEGRAPH_NODES.IDENTITY_RESULT_NODE] = identityResult
if (identityResult.httpcode = 200) then
m.top[mParticleConstants().SCENEGRAPH_NODES.CURRENT_USER_NODE] = m.mparticle.identity.getCurrentUser()
end if
end if
end if
else
print "Error: unrecognized event type '"; mt ; "'"
end if
end if
end while
end sub
function executeApiCall(apiCall as object)
args = apiCall.args
length = args.count()
if (apiCall.methodName.Instr("identity/") = 0) then
target = m.mparticle.identity
methodName = apiCall.methodName.Replace("identity/", "")
else if (apiCall.methodName.Instr("media/") = 0) then
target = m.mparticle.media
methodName = apiCall.methodName.Replace("media/", "")
else
target = m.mparticle
methodName = apiCall.methodName
end if
if (length = 0) then
target[methodName]()
else if (length = 1) then
target[methodName](args[0])
else if (length = 2) then
target[methodName](args[0], args[1])
else if (length = 3) then
target[methodName](args[0], args[1], args[2])
else if (length = 4) then
target[methodName](args[0], args[1], args[2], args[3])
else if (length = 5) then
target[methodName](args[0], args[1], args[2], args[3], args[4])
else if (length = 6) then
target[methodName](args[0], args[1], args[2], args[3], args[4], args[5])
end if
end function