-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathledclient.coffee
210 lines (166 loc) · 5.76 KB
/
ledclient.coffee
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
fs = require "fs"
console.log fs
util = require "util"
events = require "events"
net = require "net"
Canvas = require "canvas"
xmppClient = require "xmpp-client"
winston = require "winston"
myLevels =
levels:
protocol: 0,
debug: 1,
info: 2,
warn: 3,
error: 4,
,
colors:
protocol: 'grey',
debug: 'blue',
info: 'green',
warn: 'yellow',
error: 'red'
logger = new (winston.Logger) (
levels: myLevels.levels
transports: [
new (winston.transports.Console)(
level: 'debug',
colorize: 'true'
)
new (winston.transports.File)(
filename: 'ledclient_coffee.log',
colorize: 'true'
)
]
)
winston.addColors myLevels.colors
logger.protocol "protocol output"
logger.debug "debug output"
logger.info "info output"
logger.warn "warn output"
logger.error "error output"
# make setInterval less painful
makeInterval = (ms, callback) ->
setInterval callback, ms
nnl = "\r\n"
socket = new net.Socket
socket.connected = false
class Renderer extends events.EventEmitter
constructor: ->
#Current state of renderer (busy with drawing?)
@busy = false
#The canvas for the g3d2 to draw on
@canvas = new Canvas 72, 32
#Holds the curent buffer for the wall
@wallBuffer = new Buffer @canvas.width * @canvas.height
#message list of xmmpp messages queued (and to be rendered)
@msg_list = []
queue: (from, msg, stanza) ->
logger.info "Starting Renderer::queue"
logger.debug from
logger.debug msg
sender = from.split("/")[1]
sender ?= "channel"
sender = sender.trim()
sender += ":"
time = new Date().toTimeString().split ":"
time = "#{time[0]}:#{time[1]}".trim()
@msg_list.push [time, sender, msg.trim()]
@emit 'new_message'
drawMsg: ->
logger.debug "Starting Renderer::drawMsg"
@draw @msg_list.shift()
draw: ([time, sender, msg]) ->
@busy = true
#The context of the canvas
context = @canvas.getContext '2d'
context.fillStyle = '#ffffff'
logger.debug "Starting Renderer::draw"
msg_width = context.measureText(msg).width
#Position of first char of message
msg_x = @canvas.width
@interval_id = makeInterval 42, =>
context.clearRect 0, 0, @canvas.width, @canvas.height
context.save()
context.font = '11px Impact'
context.scale -1,1
context.translate [email protected], 0
context.fillText msg, msg_x, 9
context.restore()
context.font = '8px Impact'
context.fillText time, 0, 18
context.fillText sender, 25, 18
context.font = '11px Impact'
context.fillText msg, msg_x, 30
--msg_x
imageData = context.getImageData(0, 0, @canvas.width, @canvas.height).data
for i in [[email protected]]
@wallBuffer[i] = toHex(imageData[i*4+3],1).charCodeAt 0
logger.protocol "Wall buffer is #{@wallBuffer}"
logger.protocol "led wall connection is #{socket.connected}"
if socket.connected is true
logger.protocol "03#{@wallBuffer.toString('ascii')}#{nnl}"
socket.write "03#{@wallBuffer.toString('ascii')}#{nnl}"
if msg_x <= -msg_width - 1
clearInterval @interval_id
@busy = false
logger.debug "Message drawing done"
@emit 'done'
renderer = new Renderer
toHex = (n, l) ->
n = n.toString 16
while n.length < l
n = "0"+n
return n
connectWallserver = (host, port) ->
logger.debug "Wallport " + port
logger.debug "Wallserver " + host
socket.connect port,host, ->
socket.connected = true
lvl = '03'
socket.write "04#{lvl}#{nnl}"
logger.info "wallserver connected"
fs.readFile './config', 'utf-8', (err, data) ->
logger.info "Reading configuration File"
return logger.error err if err?
config = JSON.parse(data)
logger.debug "Jid: #{config.jid}"
logger.debug "Password: #{config.password}"
logger.debug "Muc: #{config.muc}"
logger.info "Connecting with xmpp"
jid = config.jid
password = config.password
client = new xmppClient.Client {jid, password}, ->
client.addListener 'online', ->
logger.info "Online"
#Communicating directly with the client does not work at the moment
#client.addListener 'message', (from, msg, stanza) ->
# renderer.queue from, msg, stanza
#Communication inside the muc
client.room config.muc, (status) ->
logger.debug "entered muc with status #{status}"
##TO BE TESTED
@addListener 'message', (from, msg, stanza) ->
renderer.queue from, msg, stanza
logger.info "Connecting with wall server"
connectWallserver config.wallserver, config.wallport
socket.on 'data', (data) ->
return logger.error data if data == 'bad'
logger.protocol data
socket.on 'error', (data) ->
logger.error data
socket.on 'close', (data) ->
logger.info "wallserver closing connection"
socket.connected = false
logger.info "Retrying to connect"
connectWallserver config.wallserver, config.wallport
renderer.on 'new_message', ->
logger.debug "Renderer reacting on signal new_message"
if not @busy
@drawMsg()
renderer.on 'done', ->
logger.debug "Renderer reacting on signal done"
unless @msg_list.length is 0
@drawMsg()
else
socket.write "02FFFF00#{nnl}"