LogicMachine Forum
How to replace? - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: How to replace? (/showthread.php?tid=5266)



How to replace? - Joep - 24.02.2024

Hi all,

I'm working on a Vestaboard API integration. And so far everything is working fine but now i want to replace the status (layout) to a readable string so i can use it in my visualization.

The Vestaboard returns the layout like this:
Code:
{   "currentMessage": {     "layout": "[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,8,5,12,12,15,0,23,15,18,12,4,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]",     "id": "172c7808-52db-4eb9-9ecf-505bd00436bc"   } }
  And i created a JSON string of all numbers and their corresponding characters:
Code:
characters = '{"0":" ","1":"a","2":"b","3":"c","4":"d","5":"e","6":"f","7":"g","8":"h","9":"i","10":"j","11":"k","12":"l","13":"m","14":"n","15":"o","16":"p","17":"q","18":"r","19":"s","20":"t","21":"u","22":"v","23":"w","24":"x","25":"y","26":"z","27":"1","28":"2","29":"3","30":"4","31":"5","32":"6","33":"7","34":"8","35":"9","36":"0","37":"!","38":"@","39":"#","40":"$","41":"(","42":")","44":"-","46":"+","47":"&","48":"=","49":";","50":":","52":"\'","53":"\\"","54":"%","55":",","56":".","59":"/","60":"?","62":"°","63":"red","64":"orange","65":"yellow","66":"green","67":"blue","68":"purple","69":"white","70":"black","71":""}'
So now i need a script that convert the Vestaboard "layout" with the characters string.

Many thanks for your help.


RE: How to replace? - admin - 26.02.2024

Try this. The example has the layout array encoded as a JSON string for some reason. It might be an error in the documentation. If it's not then uncomment line 42 and remove line 43.
Code:
chars =  {   [0] = " ", [1] = "A", [2] = "B", [3] = "C", [4] = "D", [5] = "E",   [6] = "F", [7] = "G", [8] = "H", [9] = "I", [10] = "J", [11] = "K",   [12] = "L", [13] = "M", [14] = "N", [15] = "O", [16] = "P",   [17] = "Q", [18] = "R", [19] = "S", [20] = "T", [21] = "U",   [22] = "V", [23] = "W", [24] = "X", [25] = "Y", [26] = "Z",   [27] = "1", [28] = "2", [29] = "3", [30] = "4", [31] = "5",   [32] = "6", [33] = "7", [34] = "8", [35] = "9", [36] = "0",   [37] = "!", [38] = "@", [39] = "#", [40] = "$", [41] = "(",   [42] = ")", [44] = "-", [46] = "+", [47] = "&", [48] = "=",   [49] = ";", [50] = ":", [52] = "'", [53] = '"', [54] = "%",   [55] = ",", [56] = ".", [59] = "/", [60] = "?", [62] = "°", } function decode(input)   for i, code in ipairs(input) do     input[ i ] = chars[ code ] or ''   end   local res = table.concat(input)   return res:trim() end data = [[ {   "currentMessage": {     "layout": [       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],       [0,0,0,0,0,8,5,12,12,15,0,23,15,18,12,4,0,0,0,0,0,0],       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]     ],     "id": "172c7808-52db-4eb9-9ecf-505bd00436bc"   } } ]] data = json.pdecode(data) -- messages = json.pdecode(data.currentMessage.layout) messages = data.currentMessage.layout for i, message in ipairs(messages) do   messages[ i ] = decode(message) end log(messages)



RE: How to replace? - Joep - 26.02.2024

Thanks for your help admin. Below the complete script to get the Vestaboard message including the colors regarding their character codes

Code:
require('json') require('ltn12') require('socket.http') token = 'YOUR TOKEN' chars =  {   [0] = "[ ]", [1] = "A", [2] = "B", [3] = "C", [4] = "D", [5] = "E",   [6] = "F", [7] = "G", [8] = "H", [9] = "I", [10] = "J", [11] = "K",   [12] = "L", [13] = "M", [14] = "N", [15] = "O", [16] = "P",   [17] = "Q", [18] = "R", [19] = "S", [20] = "T", [21] = "U",   [22] = "V", [23] = "W", [24] = "X", [25] = "Y", [26] = "Z",   [27] = "1", [28] = "2", [29] = "3", [30] = "4", [31] = "5",   [32] = "6", [33] = "7", [34] = "8", [35] = "9", [36] = "0",   [37] = "!", [38] = "@", [39] = "#", [40] = "$", [41] = "(",   [42] = ")", [44] = "-", [46] = "+", [47] = "&", [48] = "=",   [49] = ";", [50] = ":", [52] = "'", [53] = '"', [54] = "%",   [55] = ",", [56] = ".", [59] = "/", [60] = "?", [62] = "°",   [63] = "[R]", [64] = "[O]", [65] = "[Y]", [66] = "[G]",[67] = "[B]",   [68] = "[V]", [69] = "[W]", [70] = "[ ]",[71] = "[ ]" } function decode(input)   for i, code in ipairs(input) do     input[ i ] = chars[ code ] or ''   end   local res = table.concat(input)   return res:trim() end data = {} res, code = socket.http.request({   url = 'https://rw.vestaboard.com/',   method = 'GET',   headers = {     ['X-Vestaboard-Read-Write-Key'] = token,     ['Content-Type'] = 'application/json',   },   sink = ltn12.sink.table(data), }) if res and code == 200 then   data = table.concat(data)   data = json.pdecode(data)   messages = json.pdecode(data.currentMessage.layout)   --log(messages) else   log('Vestaboard error', code, table.concat(data)) end for i, message in ipairs(messages) do   messages[ i ] = decode(message) end log(messages)