-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendMessage.rb
389 lines (359 loc) · 9.23 KB
/
sendMessage.rb
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
require "execjs"
require "open-uri"
require "sqlite3"
require "json"
require "ntcipAccess"
require "pngFromNTCIP"
module Test
class FontNumberList
def initialize()
@fontNumberList = []
end
def addFontNumber(theFontName, theFontNumber)
@fontNumberList << FontNameNumber.new(theFontName, theFontNumber)
end
def number
@fontNumberList.number
end
def [](key)
if key.kind_of?(Integer)
return @fontNumberList[key].number
else
for i in [email protected]
return @fontNumberList[i].number if (key == @fontNumberList[i].name)
end
end
end
end
class FontNameNumber
def initialize(theFontName, theFontNumber)
@name = theFontName
@number = theFontNumber
end
def name
@name
end
def number
@number
end
end
class FieldCodeList
def initialize()
@fieldCodeList = []
end
def addFieldCode(theFieldCode)
@fieldCodeList << theFieldCode
end
def as_json(options={})
{
fieldCodeList: @fieldCodeList,
}
end
def list
@fieldCodeList
end
def to_json(*options)
as_json(*options).to_json(*options)
end
end
class FieldCode
def initialize(name, description, length, currentValue)
@name = name
@description = description
@length = length
@currentValue = currentValue
end
def name
@name
end
def value
@currentValue
end
def as_json(options={})
{
name: @name,
description: @description,
length: @length,
currentValue: @currentValue,
}
end
def to_json(*options)
as_json(*options).to_json(*options)
end
end
FC_DELIM_START = "\u00AB".encode('utf-8')
FC_DELIM_END = "\u00BB".encode('utf-8')
#######
# compile the javascript program
#######
source = open("http://127.0.0.1/r5/drawingObjects.js").read
context = ExecJS.compile(source)
#####
# get the command line arguments
#####
messageName = ""
udpPort = "163"
ipAddress = "127.0.0.1"
communityName = "administrator"
fieldCodeList = ""
messageNumber = 3
graphicStartingIndex = 28
makePNGFile = "no"
ARGV.each do |arg|
case arg[0..1]
when '-m'
messageName=arg.partition(':').last
when '-n'
messageNumber=arg.partition(':').last.to_i
when '-g'
graphicStartingIndex=arg.partition(':').last.to_i
when '-a'
ipAddress = arg.partition(':').last
when '-p'
udpPort = arg.partition(':').last
when '-c'
communityName = arg.partition(':').last
when '-f'
fieldCodeList = arg.partition(':').last
when '-i'
makePNGFile = arg.partition(':').last
end
end
######
# get the message data
#####
if nil == messageName
puts "Message Name Required"
exit
end
db = SQLite3::Database.open "messages.db"
stm = db.prepare "SELECT MessageData from messages WHERE Messagename = ?;"
stm.bind_param 1, messageName
rs = stm.execute
messageString = ""
rs.each do |row|
theMessage = row
messageString = theMessage.to_s
messageString = messageString[3..-4]
break;
end
######
# the message data has double quotes escaped with a backslash
# this is good for passing to the javascript program
# but it does not work in ruby
# so make a copy and strip off the backslashes
#####
messageString2 = messageString.gsub(/\\/,"")
#puts messageString2
######
# parse the message data
######
parsedMessage = JSON.parse(messageString2)
signHeight = parsedMessage["height"]
signWidth = parsedMessage["width"]
onTimes = []
offTimes = []
parsedMessage["pagesExport"].each {|page|
onTimes << page["onTime"]
offTimes << page["offTime"]
}
#puts "h "+signHeight.to_s+" w "+signWidth.to_s
######
# get the list of fonts that are used
######
fnnList = FontNumberList.new
fontList = "'{\"fontList\":[";
numFonts = 0
parsedMessage["fontList"].each do |fontName|
puts fontName
######
## open the font file
#####
font = open("http://127.0.0.1/r5/font-"+fontName+".json").read
#####
## grab font name and number for
## so we can populate the font tag in a MULTI string
#####
parsedFont = JSON.parse(font)
fnnList.addFontNumber(parsedFont["name"], parsedFont["fontnumber"]);
#####
# get ready to pass this font to ExecJS
#####
font = font.strip
fontList += font;
fontList += ",";
numFonts += 1
end
if 0 < numFonts
fontList = fontList[0...-1]
end
fontList += "]}'";
puts fontList
######
# get the list of field codes that are used
######
fieldCodeData = fieldCodeList.split(',')
fcList = FieldCodeList.new
parsedMessage["fieldCodeList"].each do |fieldCode|
stm = db.prepare "SELECT Name, Description, Length, CurrentValue from fieldcodes WHERE Name= ?;"
stm.bind_param 1, fieldCode
rs = stm.execute
row = rs.next
#######
# replace field code values with those supplied on the command line
#######
fieldCodeData.each do |fc|
fcName = fc.partition('=').first
fcValue = fc.partition('=').last
if 0 == row[0].casecmp(fcName)
row[3] = fcValue
end
end
fcList.addFieldCode( FieldCode.new(row[0], row[1], row[2], row[3]));
end
#####
## generate a JSON string
## escape the embedded quotes
#####
fieldCodeList = fcList.to_json.to_s.gsub(/"/,'\"')
#puts fieldCodeList
#####
# invoke the javascript exportThisMessage function
#####
#puts fontList
bitmapObjectString = context.eval('exportThisMessage("'+messageString+'",'+fontList+',"'+fieldCodeList+'")')
puts bitmapObjectString
bitmapObject = JSON.parse(bitmapObjectString)
graphicType = bitmapObject["graphicType"];
setter = NTCIPAccess::NTCIPGraphics.new(:port => udpPort, :host=> ipAddress, :community => communityName)
i = graphicStartingIndex
pageNumber = 1
bitmapObject["bitmaps"].each {|bitmap|
#####
# don't send graphic if page is text only
#####
if false == parsedMessage["pagesExport"][pageNumber-1]["textOnly"]
result = setter.set_graphic(imageArray: bitmap, graphicIndex: i, graphicName: messageName+"-P"+pageNumber.to_s)
puts "set result "+result.to_s
end
#####
# bump the graphic index. It will be used when we send the multi string
#####
i = i+1
pageNumber = pageNumber+1
}
#####
# produce a MULTI string
# to show each of these pages
# include page timing
#####
multiString = ""
needNP = false
i = graphicStartingIndex
numberOfPages = parsedMessage["pagesExport"].length
puts numberOfPages
parsedMessage["pagesExport"].each {|page|
if true == needNP
multiString << "[np]"
end
needNP = true
onTime = (page["onTime"].to_f*10).to_i
offTime = (page["offTime"].to_f*10).to_i
if(1 < numberOfPages)
multiString << "[pt"+onTime.to_s+"o"+offTime.to_s+"]"
end
if false == page["textOnly"]
#####
# not text only, so tell about the graphic
#####
multiString << "[g"+i.to_s+"]"
else
#####
# text only, so add the text to the multi string
#####
#####
# don't send trailing blank lines
#####
numLines = page["graphicObjectExport"].size;
(page["graphicObjectExport"].size-1).downto(0) do |index|
obj = page["graphicObjectExport"][index]
if "" == obj["text"]
numLines -= 1
end
end
lineNum = 1
page["graphicObjectExport"].each {|obj|
if 1 == lineNum
#####
# add page and line justification
#####
multiString << "[jp"+page["pageJustification"].to_s+"]"
multiString << "[jl"+page["lineJustification"].to_s+"]"
end
if 1 < lineNum && numLines >= lineNum
#####
# add new line
#####
multiString << "[nl]"
end
#####
# add the font number
#####
if numLines >= lineNum
multiString << "[fo"+fnnList[(obj["fontName"])].to_s+"]"
end
#####
# now add the text
#####
text = obj["text"]
#####
## substitute field code values
#####
fcList.list.each do |fc|
text = text.gsub( FC_DELIM_START+fc.name+FC_DELIM_END, fc.value)
end
#####
## add this to the multi string
#####
multiString << text
lineNum += 1
}
end
i = i+1
}
puts multiString
setter = NTCIPAccess::NTCIPMessage.new(:port => udpPort, :host=>ipAddress, :community => communityName)
result = setter.set_message(messageMemoryType: ENUM_dmsMessageMemoryType::CHANGEABLE, messageNumber: messageNumber, messageMultiString: multiString, messageOwner: "Doug2")
case result
when :noError
puts "Success"
puts "multiString " + setter.messageMultiString.to_s
puts "owner " + setter.messageOwner.to_s
puts "beacon " + setter.messageBeacon.to_s
puts "CRC " + setter.messageCRC.to_s
puts "pixelService " + setter.messagePixelService.to_s
puts "runTimePriority " + setter.messageRunTimePriority.to_s
puts "status " + setter.messageStatus.to_s
when :failure
puts "Failure"
else
puts "result "+result.to_s
end
activateResult = setter.activate_message()
puts "after activate " + activateResult[0].to_s + " " + activateResult[1].to_s + " " + activateResult[2].to_s
puts makePNGFile
if 0 == makePNGFile.casecmp('yes')
puts "2"
bitsPerPixel = 1
if 2 < parsedMessage["allowedColors"].length
bitsPerPixel = 2
end
#####
## produce .png image files
## scaled x 3
#####
#pfn = PNGFromNTCIP.new(signWidth, signHeight, bitsPerPixel, 3, bitmapObjectString, messageName)
pfn = SinglePNGFromNTCIP.new(signWidth, signHeight, bitsPerPixel, 3, bitmapObjectString, messageName)
pfn.toPNG
end
end