example.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. function stringToTable( sta )
  2. local tablesta = {}
  3. local i
  4. for i=1, #sta do
  5. tablesta[i] = sta:byte(i)
  6. end
  7. return tablesta
  8. end
  9. function tableTostring(cmd)
  10. local ret=""
  11. local i
  12. for i=1,#cmd do
  13. ret=ret..string.char(cmd[i])
  14. end
  15. return ret
  16. end
  17. function stringToHexstring(str)
  18. local ret = ''
  19. for i=1, #str do
  20. ret = ret .. string.format("%02x", str:byte(i))
  21. end
  22. return ret
  23. end
  24. function decode(cmd)
  25. local tb
  26. if cjson == nil then
  27. cjson = (require 'JSON')
  28. tb = cjson:decode(cmd)
  29. else
  30. tb = cjson.decode(cmd)
  31. end
  32. return tb
  33. end
  34. function getKVTable(t,keyName,valueName)
  35. local kvTable = {}
  36. for i=1, #t do
  37. key = t[i][''..keyName]
  38. val = t[i][''..valueName]
  39. kvTable[''..key] = val
  40. end
  41. return kvTable
  42. end
  43. function table2json(t)
  44. local function serialize(tbl)
  45. local tmp = {}
  46. for k, v in pairs(tbl) do
  47. local k_type = type(k)
  48. local v_type = type(v)
  49. local key = (k_type == "string" and "\"" .. k .. "\":")
  50. or (k_type == "number" and "")
  51. local value = (v_type == "table" and serialize(v))
  52. or (v_type == "boolean" and tostring(v))
  53. or (v_type == "string" and "\"" .. v .. "\"")
  54. or (v_type == "number" and v)
  55. tmp[#tmp + 1] = key and value and tostring(key) .. tostring(value) or nil
  56. end
  57. if table.maxn(tbl) == 0 then
  58. return "{" .. table.concat(tmp, ",") .. "}"
  59. else
  60. return "[" .. table.concat(tmp, ",") .. "]"
  61. end
  62. end
  63. assert(type(t) == "table")
  64. return serialize(t)
  65. end
  66. function jds2pri( bizcode, cmd )
  67. --return err, length, bin
  68. local bin
  69. if bizcode == 1002 then
  70. local json = decode(cmd)
  71. local streams = json["streams"]
  72. local tabstreams = getKVTable(streams, 'stream_id', 'current_value')
  73. bin = {0xbb, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,0xfa, 0x44}
  74. if (tabstreams["beep_switch"] == "0") then
  75. bin[2] = 3
  76. elseif (tabstreams["beep_switch"] == "1") then
  77. bin[2] = 2
  78. end
  79. elseif bizcode == 1004 then --获取快照
  80. bin = {0xbb, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,0xfa, 0x44}
  81. else --错误的code指令,返回获取快照
  82. bin = {0xbb, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,0xfa, 0x44}
  83. end
  84. local ret = tableTostring(bin)
  85. return 0, string.len(ret), ret
  86. end
  87. function pri2jds( bizcode, length, bin )
  88. --return err, jstr, bizcode
  89. local bin_cmd = {}
  90. local pwr
  91. local jdstr = ""
  92. bin_cmd = stringToTable(bin)
  93. for i=1, #bin_cmd do
  94. print(string.format("lua_script bin_cmd[%02d] %02x", i, bin_cmd[i]))
  95. end
  96. pwr = bin_cmd[1];
  97. jdstr = string.format('{"code":0,"streams":[{"current_value":%x,"stream_id":"switch"}],"msg":"done"}', pwr)
  98. return 0, jdstr, 102
  99. end