-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcontrol.client.js
159 lines (136 loc) · 4.27 KB
/
webcontrol.client.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
export default class WebControl {
ACTIONS = {
screen: {
urlRedirect: (url) => {
window.location.href = url
},
getSpecialNumber: (data) => {
document.getElementById(this.specialNumberTag).textContent = data.specialNumber
sessionStorage.setItem('specialNumber', data.specialNumber)
}
},
controller: {
linkController: (response) => {
if (response !== 'error') {
sessionStorage.setItem('specialNumber', response.specialNumber)
sessionStorage.setItem('widgetOn', true)
return response
} else {
this.clearControllerStorage()
}
}
}
}
CUSTOMACTIONS = {
sendData: (value) => {
const specialNumber = parseInt(sessionStorage.getItem('specialNumber'), 10)
this.emit('data', value, this.socket.id, specialNumber)
}
}
constructor (url, type, actions) {
// socket.io-client is global required
// eslint-disable-next-line
this.socket = io(url)
// only allowed values are screen and controller
this.controlType = type
// config has the form of a list of actions
this.actions = actions
}
controlActions (event) {
return this.ACTIONS[this.controlType][event]
}
onAction (event) {
this.socket.on(event, this.controlActions(event))
}
customActions (event) {
return this.CUSTOMACTIONS[event]
}
emit (event, data, param1, param2) {
this.socket.emit(event, data, param1, param2)
}
subscribeToEvents () {
this.actions.forEach(action => {
this.onAction(action)
})
}
getSpecialNumber () {
return sessionStorage.getItem('specialNumber')
}
}
export class WebControlScreen extends WebControl {
constructor (url, actions, specialNumberTag = 'specialNumber') {
super(url, 'screen', actions)
this.screen()
this.subscribeToEvents()
this.specialNumberTag = specialNumberTag
}
screen () {
const socket = this.socket
socket.on('connect', () => {
socket.emit('getSpecialNumber', socket.id, parseInt(this.getSpecialNumber(), 10))
})
this.onAction('getSpecialNumber')
}
}
export class WebControlController extends WebControl {
constructor (url) {
super(url, 'controller', [])
this.controller()
}
controller () {
const socket = this.socket
this.saveTabSessionId()
socket.on('connect', () => {
socket.emit('alreadyLinked', parseInt(this.getSpecialNumber(), 10), socket.id, this.getTabSessionId())
})
socket.on('alreadyLinked', (value) => {
value ? sessionStorage.setItem('widgetOn', true) : sessionStorage.setItem('widgetOn', false)
this.postAlreadyLinkedFunction(value)
})
this.onAction('linkController')
this.postAlreadyLinkedFunction = (value) => {}
}
// receives a function that will excute after alreadyLinked() with the returned value as param
postAlreadyLinked (aFunction) {
this.postAlreadyLinkedFunction = aFunction
}
linkController (numberValue) {
this.socket.emit('linkController', parseInt(numberValue, 10), this.socket.id, this.getTabSessionId())
this.alreadyLinked(numberValue, this.getTabSessionId())
}
alreadyLinked (numberValue, sessionId) {
this.socket.emit('alreadyLinked', parseInt(numberValue, 10), this.socket.id, sessionId)
}
send (value) {
this.customActions('sendData')(value)
}
// the function 'func' is to execute an action after receiving the
// 'unpairController' event. If no argument is passed, then 'func' is
// used as an empty function and therefore has no effect
unpair (numberValue, func = () => {}) {
this.socket.emit('unpair', parseInt(numberValue, 10), this.socket.id)
this.unpairController(func)
}
unpairController (func) {
this.socket.on('unpairController', () => {
this.clearControllerStorage()
sessionStorage.setItem('widgetOn', false)
func()
})
}
createTabSessionId () {
return Math.random().toString(36).substring(2)
}
getTabSessionId () {
return sessionStorage.getItem('tabSessionId')
}
saveTabSessionId () {
if (!sessionStorage.getItem('tabSessionId')) {
sessionStorage.setItem('tabSessionId', this.createTabSessionId())
}
}
clearControllerStorage () {
sessionStorage.removeItem('specialNumber')
sessionStorage.removeItem('widgetOn')
}
}