JDB0.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. -----------------M3微波炉转换脚本--------------------------------
  2. function string.fromhex(str)
  3. return (str:gsub('..', function (cc)
  4. return string.char(tonumber(cc, 16))
  5. end))
  6. end
  7. function string.tohex(str)
  8. return (str:gsub('.', function (c)
  9. return string.format('%02X', string.byte(c))
  10. end))
  11. end
  12. function getbyte(str,byteindex)
  13. local b15 = string.sub(str, ( byteindex *2)+1, ((byteindex+1) *2))
  14. return b15
  15. end
  16. function subbyte(str,startbyteindex,endbyteindex)
  17. local b15 = string.sub(str, ( (startbyteindex-1) *2)+1, ( endbyteindex *2)+1 )
  18. return b15
  19. end
  20. function arraytostring(tdlist, startIndex, endIndex)
  21. local res ="";
  22. local initIndex = 1
  23. if tdlist[0] ~=nil then
  24. initIndex = 0
  25. end
  26. for i=initIndex,#tdlist do
  27. res = res..string.char(tdlist[i])
  28. end
  29. res = string.tohex(res)
  30. return res;
  31. end
  32. function arraytostringinByte(tdlist)
  33. local res ="";
  34. local initIndex = 1
  35. if tdlist[0] ~=nil then
  36. initIndex = 0
  37. end
  38. for i=initIndex,#tdlist do
  39. res = res..string.char(tdlist[i])
  40. end
  41. return res;
  42. end
  43. function stringtoByteArray(str)
  44. local byteArray = {}
  45. local ret = str
  46. for i=1, #ret do
  47. byteArray[i] = tonumber(string.format("%02x", ret:byte(i)),16)
  48. end
  49. return byteArray
  50. end
  51. function showByteString(str1)
  52. local ret = str1
  53. local str = ''
  54. for i=1, #ret do
  55. str = str .. string.format("%02x", ret:byte(i))
  56. end
  57. return str
  58. end
  59. --指令转换成字节数组
  60. function toArray(strCmd)
  61. local arrayResult = {}
  62. local strCmdLen = string.len(strCmd)
  63. local byteLen = strCmdLen/2
  64. local startIndex = 0
  65. while startIndex <= byteLen do
  66. arrayResult[startIndex+1]=tonumber(getbyte(strCmd,startIndex),16)
  67. startIndex=startIndex+1
  68. end
  69. return arrayResult,byteLen
  70. end
  71. function getKVTable(t,keyName,valueName)
  72. print(serialize(t))
  73. local kvTable = {}
  74. for i=1, #t do
  75. key = t[i][''..keyName]
  76. val = t[i][''..valueName]
  77. kvTable[''..key] = ''..val
  78. end
  79. return kvTable
  80. end
  81. function makeKVArray(t,keyName,valueName)
  82. local resultArray = {}
  83. local i=1
  84. for k,v in pairs(t) do
  85. resultArray[i] = {}
  86. resultArray[i][''..keyName] = k
  87. resultArray[i][''..valueName] = v
  88. i = i+1
  89. end
  90. return resultArray
  91. end
  92. ----对象序列化方法----------
  93. function serializeArray(arrayObj)
  94. local jsonStr = "["
  95. for i=1,#arrayObj do
  96. jsonStr = jsonStr..serialize(arrayObj[i])..','
  97. end
  98. local jsonItemStrLen = string.len(jsonStr)-1;
  99. jsonStr = string.sub(jsonStr,1,jsonItemStrLen)
  100. jsonStr = ''..jsonStr..']'
  101. return jsonStr
  102. end
  103. function serialize(obj)
  104. local lua = ""
  105. local t = type(obj)
  106. if t == "number" then
  107. lua = lua .. obj
  108. elseif t == "boolean" then
  109. lua = lua .. tostring(obj)
  110. elseif t == "string" then
  111. lua = lua .. string.format("%q", obj)
  112. elseif t == "table" then
  113. lua = lua .. "{"
  114. for k, v in pairs(obj) do
  115. --lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"
  116. lua = lua .. serialize(k) .. ":"..serialize(v) .. ","
  117. end
  118. local metatable = getmetatable(obj)
  119. if metatable ~= nil and type(metatable.__index) == "table" then
  120. for k, v in pairs(metatable.__index) do
  121. --lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"
  122. lua = lua .. serialize(k) .. ":"..serialize(v) .. ","
  123. end
  124. end
  125. local luaLen = string.len(lua)-1;
  126. lua = string.sub(lua,1,luaLen)
  127. lua = lua .. "}"
  128. elseif t == "nil" then
  129. return nil
  130. else
  131. error("can not serialize a " .. t .. " type.")
  132. end
  133. return lua
  134. end
  135. function divided(value,num)
  136. value = getNumber(value,10)
  137. num = getNumber(num,10)
  138. local resultInt = (value//num)
  139. local leftInt = value%num
  140. return resultInt,leftInt
  141. end
  142. function getNumber(value,radix)
  143. if value == nil then
  144. return 60621174
  145. else
  146. local typeOfValue = type(value)
  147. if typeOfValue == 'string' then
  148. return tonumber(value,radix)
  149. elseif typeOfValue == 'number' then
  150. return value
  151. end
  152. end
  153. end
  154. function getString(value)
  155. if value == nil then
  156. return "60621174"
  157. else
  158. local typeOfValue = type(value)
  159. if typeOfValue == 'string' then
  160. return value
  161. elseif typeOfValue == 'number' then
  162. return ''..value
  163. end
  164. end
  165. end
  166. -------------------business-----------------
  167. function getcommandfrompowermode(powerMode)
  168. local command = 0;
  169. if powerMode=="0" then
  170. command=0x0A
  171. elseif powerMode=="1" then
  172. command=0x08
  173. elseif powerMode=="2" then
  174. command=0x05
  175. elseif powerMode=="3" then
  176. command=0x03
  177. elseif powerMode=="4" then
  178. command=0x01
  179. end
  180. return command
  181. end
  182. function getpowermodefromcommand(command)
  183. local powerMode = 9
  184. if command=="0A" then
  185. powerMode = 0
  186. elseif command =="08" then
  187. powerMode = 1
  188. elseif command =="05" then
  189. powerMode = 2
  190. elseif command =="03" then
  191. powerMode = 3
  192. elseif command=="01" then
  193. powerMode = 4
  194. end
  195. return powerMode
  196. end
  197. function getcommandfromworkmode(workMode)
  198. local command = 0;
  199. if workMode == "2" then
  200. command = 0x01
  201. elseif workMode == "1" then
  202. command = 0x02
  203. elseif workMode == "3" then
  204. command = 0x04
  205. end
  206. return command
  207. end
  208. function getworkmodefromcommand(command)
  209. local workMode = 0
  210. if command=="01" then
  211. workMode=2
  212. elseif command=="02" then
  213. workMode=1
  214. elseif command=="04" then
  215. workMode=3
  216. elseif command=="08" then
  217. workMode=4
  218. end
  219. return workMode
  220. end
  221. --将json解释成十六进制指令
  222. function convert_to_private(t)
  223. tdlist = {
  224. [0] = 0xAA, --header
  225. [1] = 0x17, --len
  226. [2] = 0xB0, --B0
  227. [3] = 0x00, --00
  228. [4] = 0x00, --00
  229. [5] = 0x00, --00
  230. [6] = 0x00, --00
  231. [7] = 0x00, --00
  232. [8] = 0x00, --00
  233. [9] = 0x02, --msg type 02
  234. [10] = 0x00, --status
  235. [11] = 0x00, --workmode
  236. [12] = 0x00, --time_hour
  237. [13] = 0x00, --time_minute
  238. [14] = 0x00, --wordmode arg1
  239. [15] = 0x00, --is multiSeg command
  240. [16] = 0x00, --00
  241. [17] = 0x00, --00
  242. [18] = 0x00, --00
  243. [19] = 0x00, --00
  244. [20] = 0x00, --00 id1
  245. [21] = 0x00, --00 id2
  246. [22] = 0x00, --00 id3
  247. [23] = 0x00, --checksum
  248. }
  249. local msgLenByteNumber = 23
  250. if t['KG_Cancel']~=nil then
  251. if t['KG_Cancel']['value'] == "1" then
  252. tdlist[10] = 0x01
  253. end
  254. elseif t['KG_Start']~=nil then
  255. if t['KG_Start']['value'] == "0" then
  256. tdlist[10] = 0x03
  257. elseif t['KG_Start']['value'] == "1" then
  258. tdlist[10] = 0x02
  259. end
  260. elseif t['KG_Lock']~=nil then
  261. if t['KG_Lock']['value'] == "0" then
  262. tdlist[10] = 0x01
  263. elseif t['KG_Lock']['value'] == "1" then
  264. tdlist[10] = 0x05
  265. end
  266. elseif t['Status_Update']~=nil then
  267. if t['Status_Update']['value'] == "1" then
  268. tdlist[9] = 0x03
  269. elseif t['Status_Update']['value'] == "0" then
  270. return "DDFF18AA17B0000000000000030000000000000000000000000036";
  271. end
  272. elseif t['WF'] ~= nil then
  273. if t['WF']['extra'] ~= nil then
  274. if t['WF']['extra']['Type'] == "0" then
  275. if t['WF']['extra']['WF_ID'] ~= nil then
  276. if t['WF']['extra']['PreSet'] ~= nil then
  277. local workMode = t['WF']['extra']['WF_ID']
  278. local powerMode = t['WF']['extra']['PreSet']['PowerMode']
  279. local workTime = t['WF']['extra']['PreSet']['WorkTime']
  280. local weight = t['WF']['extra']['PreSet']['Weight']
  281. local min_time = t['WF']['extra']['PreSet']['Min']
  282. local sec_time = t['WF']['extra']['PreSet']['Sec']
  283. if workMode == "2" then
  284. tdlist[11] = 0x01
  285. elseif workMode == "1" then
  286. tdlist[11] = 0x02
  287. elseif workMode == "3" then
  288. tdlist[11] = 0x04
  289. end
  290. if workMode == "2" or workMode=="1" then
  291. local workTimeNMinute,workTimeNSecond = divided(workTime,60)
  292. tdlist[12]=workTimeNMinute
  293. tdlist[13]=workTimeNSecond
  294. if workMode == "2" then
  295. tdlist[14]=getcommandfrompowermode(powerMode)
  296. end
  297. if workMode =="1" then
  298. tdlist[14]=0x01
  299. end
  300. elseif workMode == "3" then
  301. --local weightLen = string.len(weight)-2;
  302. --weight = string.sub(weight,1,weightLen)
  303. local weightN = tonumber(weight)/100
  304. tdlist[15] = weightN
  305. end
  306. tdlist[10]=0x02
  307. end
  308. end
  309. elseif t['WF']['extra']['Type'] == "1" then
  310. local cloundMenuId = t['WF']['extra']['WF_ID']
  311. local totalStep = t['WF']['extra']['StepNum']
  312. local totalStepN = tonumber(totalStep)
  313. if totalStepN > 0 then
  314. local b16 = totalStep.."1"
  315. tdlist[16]=tonumber(b16,16)
  316. local cloundMenuIdNHH, cloundMenuIdNHHLeft = divided(cloundMenuId,(16^4))
  317. local cloundMenuIdNH,cloundMenuIdNL = divided(cloundMenuIdNHHLeft,(16^2))
  318. tdlist[20] = cloundMenuIdNHH
  319. tdlist[21] = cloundMenuIdNH
  320. tdlist[22] = cloundMenuIdNL
  321. local stepDetail = t['WF']['extra']['Steps'][1]['Action']
  322. local nextSegStartMode = stepDetail['WorkStatus']
  323. local nextSegStartModeN = tonumber(nextSegStartMode)
  324. local workMode = stepDetail['WorkMode']
  325. tdlist[11]= getcommandfromworkmode(workMode)
  326. if workMode=="1" or workMode=="2" then
  327. local workTime = stepDetail['WorkTime']
  328. local workTimeNMinute, workTimeNSecond = divided(workTime,60)
  329. tdlist[12]= workTimeNMinute
  330. tdlist[13]= workTimeNSecond
  331. if workMode=="2" then
  332. local powerMode = stepDetail['PowerMode']
  333. tdlist[14] = getcommandfrompowermode(powerMode)
  334. end
  335. if workMode == "1" then
  336. tdlist[14] = 0x01
  337. end
  338. elseif workMode=="3" then
  339. local weight = stepDetail['Weight']
  340. local weightN = tonumber(weight)
  341. local weightN100 = weightN/100
  342. tdlist[15] = weightN100
  343. end
  344. tdlist[10] = 0x02
  345. tdlist[19] = nextSegStartModeN
  346. local mutiSegNextIndex = 23
  347. if totalStepN > 1 then
  348. for i=2,totalStepN do
  349. local stepDetail = t['WF']['extra']['Steps'][i]['Action']
  350. local nextSegStartMode = stepDetail['WorkStatus']
  351. local nextSegStartModeN = tonumber(nextSegStartMode)
  352. local workMode = stepDetail['WorkMode']
  353. tdlist[mutiSegNextIndex]= getcommandfromworkmode(workMode)
  354. mutiSegNextIndex = mutiSegNextIndex+1
  355. if workMode=="1" or workMode=="2" then
  356. local workTime = stepDetail['WorkTime']
  357. local workTimeN = tonumber(workTime);
  358. local workTimeNMinute, workTimeNSecond = divided(workTime,60)
  359. tdlist[mutiSegNextIndex]= workTimeNMinute
  360. mutiSegNextIndex = mutiSegNextIndex+1
  361. tdlist[mutiSegNextIndex]= workTimeNSecond
  362. mutiSegNextIndex = mutiSegNextIndex+1
  363. if workMode=="2" then
  364. local powerMode = stepDetail['PowerMode']
  365. tdlist[mutiSegNextIndex] = getcommandfrompowermode(powerMode)
  366. mutiSegNextIndex = mutiSegNextIndex+1
  367. tdlist[mutiSegNextIndex] =0
  368. mutiSegNextIndex = mutiSegNextIndex+1
  369. end
  370. if workMode=="1" then
  371. tdlist[mutiSegNextIndex] =0x01
  372. mutiSegNextIndex = mutiSegNextIndex+1
  373. tdlist[mutiSegNextIndex] =0
  374. mutiSegNextIndex = mutiSegNextIndex+1
  375. end
  376. elseif workMode=="3" then
  377. tdlist[mutiSegNextIndex] =0
  378. mutiSegNextIndex = mutiSegNextIndex+1
  379. tdlist[mutiSegNextIndex] =0
  380. mutiSegNextIndex = mutiSegNextIndex+1
  381. tdlist[mutiSegNextIndex] =0
  382. mutiSegNextIndex = mutiSegNextIndex+1
  383. local weight = stepDetail['Weight']
  384. local weightN = tonumber(weight)
  385. local weightN100 = weightN/100
  386. tdlist[mutiSegNextIndex] = weightN100
  387. mutiSegNextIndex = mutiSegNextIndex+1
  388. end
  389. tdlist[mutiSegNextIndex] = nextSegStartModeN
  390. mutiSegNextIndex= mutiSegNextIndex+1
  391. end
  392. end
  393. msgLenByteNumber = mutiSegNextIndex
  394. tdlist[1] = msgLenByteNumber
  395. end
  396. end
  397. end
  398. end
  399. --CRC8
  400. local crc = 0;
  401. for i=1, (msgLenByteNumber-1) do
  402. crc = crc+tdlist[i];
  403. end
  404. crc=~(crc);
  405. crc=(crc&0x000000FF);
  406. crc=crc+1;
  407. tdlist[msgLenByteNumber] = crc;
  408. local res = arraytostring(tdlist,0,msgLenByteNumber)
  409. return res
  410. end
  411. --将十六进制指令解释成json
  412. function convert_from_private(s)
  413. local str = s
  414. local result = {
  415. KG_Start = { value = '-1', when = ''},
  416. KG_Cancel = { value = '-1', when = ''},
  417. KG_Lock = { value = '0', when = ''},
  418. WorkStatus = { value = '0', when = ''},
  419. WorkMode = { value = '-1', when = ''},
  420. Weight = { value = '0', when = ''},
  421. WorkTime = { value = '-1', when = ''},
  422. PowerMode = { value = '-1', when = ''},
  423. WF_CurrentStep = { value = '0', when = ''},
  424. WF_TimeLeft = { value = '0', when = ''},
  425. EC = { value = '0', when = ''},
  426. WF = {
  427. value = '1',
  428. when = '',
  429. extra = {
  430. WF_ID = '0',
  431. StepNum = '0',
  432. TimeTotal = '0',
  433. Type = '0'
  434. }
  435. }
  436. }
  437. local b9Temp = getbyte(str,9)
  438. local strLen = string.len(str)
  439. if strLen > (28*2) then
  440. if strLen==(28*2*2) and ( b9Temp == "02" or b9Temp=="04" ) then
  441. str = subbyte(str,29,56)
  442. end
  443. end
  444. --[[
  445. local b4 = getbyte(str,4)
  446. if b4 ~= 'EE' then
  447. return nil
  448. end
  449. ]]--
  450. local b10 = getbyte(str,10)
  451. local b10N = tonumber(b10,16)
  452. local b10NH,b10NL = divided(b10,16)
  453. --b10NH = tonumber(''..b10NH)
  454. if b10NH > 7 then
  455. result['EC']['value']='1';
  456. b10='0'..b10NL
  457. end
  458. local b16 = getbyte(str,16)
  459. if str=="0808080808080808" then
  460. result['EC']['value']= "0";
  461. elseif b16~="00" then
  462. if b16 == "01" then
  463. result['EC']['value']= '1'
  464. elseif b16 == "02" then
  465. result['EC']['value']= '2'
  466. end
  467. end
  468. local workStatus = 0
  469. if b10=="01" or b10=="07" then
  470. workStatus = 0
  471. elseif b10=="02" then
  472. workStatus = 1
  473. elseif b10=="03" or b10=="09" then
  474. workStatus = 2
  475. elseif b10=="05" then
  476. workStatus = 3
  477. elseif b10=="66" then
  478. workStatus = 4
  479. elseif b10=="04" then
  480. workStatus = 5
  481. end
  482. local workMode = 9
  483. if workStatus == 1 or workStatus == 2 or workStatus==4 or workStatus==5 then
  484. local b11 = getbyte(str,11)
  485. workMode = getworkmodefromcommand(b11);
  486. if workMode == 1 or workMode == 2 or workMode == 3 or workMode==4 then
  487. local b12 = tonumber(getbyte(str,12), 16)
  488. local b13 = tonumber(getbyte(str,13), 16)
  489. local workTime = (b12*60) + b13
  490. result['WF_TimeLeft'] = {value = tostring(workTime), when=''}
  491. local powerMode = 0
  492. if workMode ==2 then
  493. local b24 = getbyte(str,24)
  494. powerMode = getpowermodefromcommand(b24)
  495. result['PowerMode']['value'] = tostring(powerMode)
  496. end
  497. if workMode ==4 then
  498. result['PowerMode']['value'] = '0'
  499. end
  500. if workMode == 3 then
  501. local weight = 0
  502. local b25 = getbyte(str,25)
  503. weight = b25
  504. local weightN100 = tonumber(weight,16)*100
  505. result['Weight']['value'] = tostring(weightN100)
  506. end
  507. result['WorkMode']['value'] = tostring(workMode)
  508. result['WF']['extra']['WF_ID'] = tostring(workMode)
  509. end
  510. local b20 = getbyte(str,20)
  511. local b21 = getbyte(str,21)
  512. local b22 = getbyte(str,22)
  513. local b23 = getbyte(str,23)
  514. local cloundMenuId = tonumber(b21,16)*(16^4)+ tonumber(b22,16)*(16^2)+ tonumber(b23,16);
  515. local b20N = tonumber(b20,16);
  516. local totalStep,currentStep = divided(b20N,16)
  517. if cloundMenuId ~= 0 then
  518. result['WF']['extra']['WF_ID'] = tostring(cloundMenuId)
  519. result['WF']['extra']['Type'] = "1"
  520. result['WF_CurrentStep']['value'] = tostring(currentStep)
  521. result['WF']['extra']['StepNum'] = tostring(totalStep)
  522. end
  523. end
  524. result['WorkStatus'] = {value = tostring(workStatus), when=''}
  525. return result
  526. end
  527. function is_increment()
  528. return true;
  529. end
  530. --查询指令
  531. function private_cmd_query(t)
  532. local restring="AA17B0000000000000030000000000000000000000000036"
  533. return restring
  534. end
  535. --print(control('1','1','61',''))
  536. function control(workMode,powerMode,workTime,weight,min_time,sec_time)
  537. local t = { WF = { extra = {
  538. Type = '0',
  539. WF_ID = '0',
  540. PreSet = { PowerMode=nil,WorkTime=nil,Weight=nil,Min=min_time,Sec=sec_time}
  541. }
  542. }
  543. }
  544. t['WF']['extra']['Type'] = '0'
  545. if workMode== '1' then
  546. workMode = '2'
  547. elseif workMode == '2' then
  548. workMode = '1'
  549. end
  550. t['WF']['extra']['WF_ID'] = workMode
  551. t['WF']['extra']['PreSet'] = {
  552. ['PowerMode'] = powerMode,
  553. ['WorkTime'] = workTime,
  554. ['Weight'] = weight,
  555. ['Min']=min_time,
  556. ['Sec']=sec_time
  557. }
  558. return convert_to_private(t)
  559. end
  560. --多段工作
  561. function cloundControl(MenuId, StepNum, Steps)
  562. local t = { WF = { extra = {
  563. Type = nil,
  564. StepNum = nil,
  565. WF_ID = nil,
  566. Steps = {
  567. { Action = { WorkStatus = nil, WorkMode = nil, PowerMode = nil,WorkTime = nil,Weight = nil} },
  568. { Action = { WorkStatus = nil, WorkMode = nil, PowerMode = nil,WorkTime = nil,Weight = nil} }
  569. }
  570. }
  571. }
  572. }
  573. t['WF']['extra']['Type'] = '1'
  574. t['WF']['extra']['WF_ID'] = MenuId
  575. t['WF']['extra']['StepNum'] = StepNum
  576. t['WF']['extra']['Steps'] = Steps
  577. return convert_to_private(t)
  578. end
  579. --暂停
  580. function pause()
  581. local t = {
  582. KG_Start = {
  583. value = '0'
  584. }
  585. }
  586. local res = convert_to_private(t)
  587. return res
  588. end
  589. --继续
  590. function continue()
  591. local t = {
  592. KG_Start = {
  593. value = '1'
  594. }
  595. }
  596. local res = convert_to_private(t)
  597. return res
  598. end
  599. --取消
  600. function cancel()
  601. local t = {
  602. KG_Cancel = {
  603. value = '1'
  604. }
  605. }
  606. local res = convert_to_private(t)
  607. return res
  608. end
  609. --童锁
  610. function lock()
  611. local t = {
  612. KG_Lock = {
  613. value = '1'
  614. }
  615. }
  616. local res = convert_to_private(t)
  617. return res
  618. end
  619. --解锁
  620. function unlock()
  621. local t = {
  622. KG_Lock = {
  623. value = '0'
  624. }
  625. }
  626. local res = convert_to_private(t)
  627. return res
  628. end
  629. --查询
  630. function query()
  631. local res = private_cmd_query(t)
  632. return res
  633. end
  634. function decodeStatus(r)
  635. if r==nil then
  636. return nil
  637. end
  638. local result = {
  639. Error='0',
  640. Lock='0',
  641. Type='-1',
  642. WorkStatus = r.WorkStatus.value,--0待机 1工作中 2暂停中 3童锁锁定 4完成
  643. WorkMode='-1',
  644. PowerMode='-1',
  645. Weight='-1',
  646. TimeLeft='-1',
  647. StepNum='-1',
  648. CurrentStep=nil,
  649. MenuId = nil
  650. }
  651. if r.EC~=nil and r.EC.value=='1' then
  652. result.Error = '1'
  653. return result
  654. end
  655. if r.Lock~=nil and r.Lock.value=='1' then
  656. result.Lock = '1'
  657. return result
  658. end
  659. if r.WF.extra~=nil and r.WF.extra.Type=='0' then
  660. result.Type='0'
  661. result.WorkStatus = r.WorkStatus.value
  662. result.WorkMode=r.WorkMode.value
  663. result.PowerMode=r.PowerMode.value
  664. result.Weight=r.Weight.value
  665. result.TimeLeft=r.WF_TimeLeft.value
  666. return result
  667. elseif r.WF.extra~=nil and r.WF.extra.Type=='1' then
  668. result.Type='1'
  669. result.WorkStatus = r.WorkStatus.value
  670. result.WorkMode=r.WorkMode.value
  671. result.PowerMode=r.PowerMode.value
  672. result.Weight=r.Weight.value
  673. result.TimeLeft=r.WF_TimeLeft.value
  674. result.StepNum=r.WF.extra.StepNum
  675. result.CurrentStep=r.WF_CurrentStep.value
  676. result.MenuId=r.WF.extra.WF_ID
  677. return result
  678. end
  679. return result
  680. end
  681. function uploadStatus(strCmd)
  682. return decodeStatus(convert_from_private(strCmd))
  683. end
  684. --------------------------------京东分隔线--------------------------------------------------------------
  685. function getJD_KVTable(t)
  686. return getKVTable(t,'stream_id','current_value')
  687. end
  688. function makeJD_KVArray(t)
  689. return makeKVArray(t,'stream_id','current_value')
  690. end
  691. function decode(cmd, model)
  692. local tb = {}
  693. if cjson == nil then
  694. cjson = (require 'JSON')
  695. tb = cjson:decode(cmd)
  696. cjson = nil
  697. else
  698. tb = cjson.decode(cmd)
  699. end
  700. return tb
  701. end
  702. function jds2pri(code,cmd,model)
  703. print("翻译器收到的指令类型为"..code)
  704. local strCmd = ""
  705. if code==1002 then
  706. print("翻译器收到的json指令为"..cmd)
  707. local t = decode(cmd, model)
  708. local kv = getJD_KVTable(t.streams)
  709. if kv.switch == '1' then
  710. strCmd = cancel();
  711. elseif kv.state=='1' then
  712. strCmd = continue();
  713. elseif kv.state=='2' then
  714. strCmd = pause()
  715. elseif kv.lock=='1' then
  716. strCmd = lock();
  717. elseif kv.lock=='0' then
  718. strCmd = unlock();
  719. elseif kv.switch=='0' then
  720. local workTime = nil
  721. if kv.min_time ~= nil and kv.sec_time ~= nil then
  722. workTime = tonumber(kv.min_time)*60 + tonumber(kv.sec_time)
  723. end
  724. strCmd = control(kv.mode,kv.power,workTime,kv.weight,kv.min_time,kv.sec_time)
  725. end
  726. elseif code==1004 then
  727. strCmd = query()
  728. elseif code==1050 then
  729. print("翻译器收到的json指令为"..cmd)
  730. local t = decode(cmd, model)
  731. local i = 1
  732. local StepNum = #t['cmd']['subTask']
  733. local Steps = {}
  734. local MenuId = t['cmd']['task_id']
  735. while i <= StepNum do
  736. local step = getJD_KVTable(t['cmd']['subTask'][i]['streams'])
  737. --mode power min_time sec_time start_type weight
  738. local workMode = ''..step.mode -- 1 微波 2 烧烤 3 解冻
  739. if workMode== '1' then
  740. workMode = '2'
  741. elseif workMode == '2' then
  742. workMode = '1'
  743. end
  744. local workTime = tonumber(''..step.min_time) *60 + tonumber(''..step.sec_time)
  745. local powerMode = ''..step.power
  746. local weight = ''..step.weight
  747. local workStatus = ''..step.start_type
  748. if workStatus == '2' then
  749. workStatus = '1'
  750. end
  751. Steps[i]['Action'] = {
  752. WorkStatus = workStatus, WorkMode = workMode, PowerMode = powerMode,WorkTime = workTime,Weight = weight
  753. }
  754. end
  755. strCmd = cloundControl(MenuId,StepNum,Steps)
  756. end
  757. if strCmd~="" then
  758. print("翻译器将json指令理解为:"..strCmd)
  759. local byteArray,byteArrayLen = toArray(strCmd)
  760. return 0,byteArrayLen,arraytostringinByte(byteArray)
  761. else
  762. print("翻译器无法识别此json指令:"..cmd)
  763. return 0,0,serialize(kv)
  764. end
  765. end
  766. function pri2jds( code, length, bin, model )
  767. local byteArray = stringtoByteArray(bin)
  768. local strCmd = arraytostring(byteArray,0,#byteArray)
  769. print("翻译器收到的机器码为:"..strCmd)
  770. local resultCode = 0;
  771. local b9 = getbyte(strCmd,9)
  772. if b9=='02' then
  773. resultCode = 102
  774. elseif b9=='03' then
  775. resultCode = 104
  776. else
  777. resultCode = 104
  778. end
  779. local status = uploadStatus(strCmd)
  780. if status == nil then
  781. return nil
  782. end
  783. --cloudy_menu info lock min_dctime mode power sec_dctime state step total_step weight switch
  784. local result = {
  785. info=status.Error,
  786. lock=status.Lock,
  787. switch = '0',
  788. cloudy_menu='0',
  789. state=nil,
  790. mode = nil,
  791. power=''..status.PowerMode,
  792. min_dctime=nil,
  793. sec_dctime=nil,
  794. weight=''..status.Weight,
  795. total_step = status.StepNum,
  796. step=status.CurrentStep,
  797. menu_id = status.MenuId
  798. }
  799. if status.WorkStatus == '0' then
  800. result.switch = '1'
  801. result.state = '0'
  802. elseif status.WorkStatus == '1' then
  803. result.state = '1'
  804. elseif status.WorkStatus == '2' then
  805. result.state = '2'
  806. elseif status.WorkStatus == '3' then
  807. result.lock = '1'
  808. elseif status.WorkStatus == '4' then
  809. result.state = '3'
  810. elseif status.WorkStatus == '5' then
  811. result.state = '4'
  812. end
  813. if status.Type == '1' then
  814. result.cloudy_menu = '1'
  815. end
  816. if status.WorkMode == '1' then
  817. result.mode = '2'
  818. elseif status.WorkMode == '2' then
  819. result.mode = '1'
  820. elseif status.WorkMode == '3' then
  821. result.mode = '3'
  822. end
  823. local leftTimeMin,lettTimeSec = divided(status.TimeLeft,60)
  824. result.min_dctime = ''..leftTimeMin
  825. result.sec_dctime = ''..lettTimeSec
  826. result = makeJD_KVArray(result)
  827. result = serializeArray(result)
  828. local resultStr = '{"code":0,"streams":'..result..'}'
  829. print("翻译器将收到的机器码翻译为:["..resultCode..']类型请求'..resultStr)
  830. return 0, resultStr, resultCode
  831. end
  832. print("Load OK")