data.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. const MAGIC=0xdead;
  2. const PKT_TYPE=function(){
  3. var len = 0;
  4. return {
  5. PKT_TYPE_ACK:len++,
  6. PKT_TYPE_PARAS:len++,
  7. PKT_TYPE_POWER_CH:len++, //power_ch_t
  8. PKT_TYPE_POWER_TOTAL:len++, //power_total_t
  9. PKT_TYPE_POWER_RESET:len++,
  10. PKT_TYPE_CH_ALL:len++,
  11. PKT_TYPE_SCAN:len++,
  12. PKT_TYPE_FACTORY:len++,
  13. PKT_TYPE_ALARM:len++,
  14. PKT_TYPE_SENSOR:len++,
  15. PKT_TYPE_BREAKER:len++,
  16. PKT_TYPE_STAT:len++,
  17. PKT_TYPE_MESG:len++,
  18. PKT_TYPE_FILE:len++,
  19. PKT_TYPE_SLAVE_LIST:len++,
  20. }
  21. }();
  22. const SUB_TYPE=function(){
  23. var len = 0;
  24. return {
  25. SUB_TYPE_POWER_SWITCH:len++,
  26. SUB_TYPE_POWER_ALARM:len++,
  27. SUB_TYPE_POWER_THRESHOLD:len++, //power_ch_t
  28. SUB_TYPE_POWER_START_DELAY:len++, //power_total_t
  29. SUB_TYPE_POWER_STOP_DELAY:len++,
  30. SUB_TYPE_PARAS_MODBUS:len++,
  31. SUB_TYPE_PARAS_MISC:len++,
  32. SUB_TYPE_PARAS_PROD:len++,
  33. SUB_TYPE_PARAS_SERV:len++,
  34. SUB_TYPE_PARAS_NETWORK:len++,
  35. }
  36. }();
  37. const PDU_TYPE=function(){
  38. var len = 0;
  39. return {
  40. PDU_AC_I1O1:len++,
  41. PDU_DC_I1O1:len+=2,
  42. PDU_AC_I3O3:len++,
  43. PDU_AC_I3O1:len++,
  44. PDU_AC_I3O1_H:len++,
  45. }
  46. }();
  47. const ALARM_ACT=function(){
  48. var len = 1;
  49. return {
  50. ALARM_ACT_CLOSE_CH:len++,
  51. ALARM_ACT_MESG:len++,
  52. ALARM_ACT_CLOSE_CHALL:len++,
  53. }
  54. }();
  55. const IP_MODE=function(){
  56. var len = 0;
  57. return {
  58. IP_MODE_DHCP:len++,
  59. IP_MODE_STATIC:len++,
  60. }
  61. }();
  62. const IP_VER=function(){
  63. var len = 0;
  64. return {
  65. IP_VER_ERR:len++,
  66. IP_VER_V4:len++,
  67. IP_VER_V6:len++,
  68. }
  69. }();
  70. const MODBUS_MODE=function(){
  71. var len = 0;
  72. return {
  73. MODBUS_MASTER:len++,
  74. MODBUS_SLAVE:len++,
  75. }
  76. }();
  77. const LANG_ID=function(){
  78. var len = 0;
  79. return {
  80. LANG_ID_CHINESE:len++,
  81. LANG_ID_ENGLISH:len++,
  82. LANG_ID_RUSSIAN:len++,
  83. LANG_ID_SPANISH:len++,
  84. LANG_ID_PORTYGYESE:len++,
  85. LANG_ID_GERMAN:len++,
  86. LANG_ID_FRENCH:len++,
  87. LANG_ID_ITALIAN:len++,
  88. LANG_ID_JAPANESE:len++,
  89. }
  90. }();
  91. ///////////////////////////////////////////////////////
  92. function array_to_string(data){
  93. const decoder = new TextDecoder();
  94. return decoder.decode(new Uint8Array(data));
  95. }
  96. function array_set(array,str,offset){
  97. const encoder = new TextEncoder(); // ĬÈÏ'utf-8'±àÂë
  98. const buffer = encoder.encode(str);
  99. array.set(buffer);
  100. array.fill(offset, buffer.length);
  101. }
  102. function string_to_array(data,len){
  103. const encoder = new TextEncoder(); // ĬÈÏ'utf-8'±àÂë
  104. const buffer = encoder.encode(data);
  105. const fixedArray = new Uint8Array(len);
  106. fixedArray.set(buffer);
  107. fixedArray.fill(0, buffer.length);
  108. return fixedArray.buffer;
  109. }
  110. function array_cat(a1,a2){
  111. var tlen=a1.byteLength+a2.byteLength;
  112. var buffer = new ArrayBuffer(tlen);
  113. var vw = new Uint8Array(buffer);
  114. var vw1 = new Uint8Array(a1);
  115. for(var i=0; i<a1.byteLength; i++) {
  116. vw[i] = vw1[i];
  117. }
  118. var vw2 = new Uint8Array(a2);
  119. for(var i=0; i<a2.byteLength; i++) {
  120. vw[i+a1.byteLength] = vw2[i];
  121. }
  122. return buffer;
  123. }
  124. function mk_hdr_bin(addr,type,subtype,nack,flag,dlen)
  125. {
  126. var tmp,length=0;
  127. var buffer = new ArrayBuffer(14);
  128. tmp = new DataView(buffer, length);
  129. var t = MAGIC;
  130. tmp.setUint16(0,t,true); //magic
  131. length += 2;
  132. tmp = new DataView(buffer, length);
  133. tmp.setUint8(0,addr,true); //addr
  134. length += 1;
  135. tmp = new DataView(buffer, length);
  136. tmp.setUint8(0,type,true); //type
  137. length += 1;
  138. tmp = new DataView(buffer, length);
  139. tmp.setUint8(0,0,true); //subtype
  140. length += 1;
  141. tmp = new DataView(buffer, length);
  142. tmp.setUint8(0,0,true); //nack
  143. length += 1;
  144. tmp = new DataView(buffer, length);
  145. tmp.setUint32(0,flag,true); //flag
  146. length += 4;
  147. tmp = new DataView(buffer, length);
  148. tmp.setUint32(0,dlen,true); //dlen
  149. length += 4;
  150. return buffer;
  151. }
  152. function mk_hdr_obj(addr,type,subtype,nack,flag,dlen)
  153. {
  154. this.magic = MAGIC;
  155. this.addr = addr;
  156. this.type = type;
  157. this.subtype = subtype;
  158. this.flag = flag;
  159. this.dlen = dlen;
  160. }
  161. function pkt_to_bin(addr,type,subtype,nack,flag,js)
  162. {
  163. var tmp;
  164. var buffer = new mk_hdr_bin(addr,type,subtype,nack,flag,js.len);
  165. switch(type){
  166. case PKT_TYPE.PKT_TYPE_POWER_CH:
  167. tmp = new powerc_to_bin(js);
  168. break;
  169. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  170. tmp = new powert_to_bin(js);
  171. break;
  172. case PKT_TYPE.PKT_TYPE_ALARM:
  173. tmp = new alarm_to_bin(js);
  174. break;
  175. default:
  176. return tmp;
  177. }
  178. return array_cat(buffer,tmp);
  179. }
  180. function bin_to_pkt(bin)
  181. {
  182. var hdr = new bin_to_hdr(bin);
  183. var data = bin.slice(hdr.len);
  184. if(data.byteLength>0) {
  185. switch(hdr.type) {
  186. case PKT_TYPE.PKT_TYPE_POWER_CH:
  187. hdr.powerc = new bin_to_powerc(data);
  188. break;
  189. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  190. hdr.powert = BIN.powert.b2j(data);
  191. break;
  192. case PKT_TYPE.PKT_TYPE_ALARM:
  193. hdr.alarmd = BIN.alarmd.b2j(data);
  194. break;
  195. case PKT_TYPE.PKT_TYPE_ALARM:
  196. hdr.alarmd = BIN.alarmd.b2j(data);
  197. break;
  198. case PKT_TYPE.PKT_TYPE_SLAVE_LIST:
  199. hdr.slaves = BIN.alarmd.b2j(data);
  200. break;
  201. default:
  202. console.log("wrong hdr type: "+hdr.type);
  203. }
  204. }
  205. return hdr;
  206. }
  207. ///////////////////////////////////////////////////
  208. function bin_to_hdr(bin) {
  209. var tmp, length=0;
  210. tmp = new DataView(bin, length);
  211. this.magic = tmp.getUint16(0,true);
  212. length += 2;
  213. tmp = new DataView(bin, length);
  214. this.addr = tmp.getUint8(0,true);
  215. length += 1;
  216. tmp = new DataView(bin, length);
  217. this.type = tmp.getUint8(0,true);
  218. length += 1;
  219. tmp = new DataView(bin, length);
  220. this.subtype = tmp.getUint8(0,true);
  221. length += 1;
  222. tmp = new DataView(bin, length);
  223. this.nack = tmp.getUint8(0,true);
  224. length += 1;
  225. tmp = new DataView(bin, length);
  226. this.flag = tmp.getUint8(0,true);
  227. length += 4;
  228. tmp = new DataView(bin, length);
  229. this.dlen = tmp.getUint32(0,true);
  230. length += 4;
  231. this.len = length;
  232. }
  233. function bin_to_info(bin) {
  234. var tmp, length=0;
  235. this.name = array_to_string(bin.slice(0,32));
  236. length += 32;
  237. tmp = new DataView(bin, length);
  238. this.type = tmp.getUint8(0,true);
  239. length += 1;
  240. tmp = new DataView(bin, length);
  241. this.addr = tmp.getUint8(0,true);
  242. length += 1;
  243. tmp = new DataView(bin, length);
  244. this.ch = tmp.getUint8(0,true);
  245. length += 1;
  246. tmp = new DataView(bin, length);
  247. this.sch = tmp.getUint8(0,true);
  248. length += 1;
  249. tmp = new DataView(bin, length);
  250. this.ph_id = tmp.getUint8(0,true);
  251. length += 1;
  252. tmp = new DataView(bin, length);
  253. this.ph_val = tmp.getUint8(0,true);
  254. length += 1;
  255. tmp = new DataView(bin, length);
  256. this.start_delay = tmp.getUint32(0,true);
  257. length += 4;
  258. tmp = new DataView(bin, length);
  259. this.stop_delay = tmp.getUint32(0,true);
  260. length += 4;
  261. this.len = length;
  262. }
  263. function bin_to_power(bin) {
  264. var tmp, length=0;
  265. tmp = new DataView(bin, length);
  266. this.voltage = tmp.getFloat32(0,true);
  267. length += 4;
  268. tmp = new DataView(bin, length);
  269. this.current = tmp.getFloat32(0,true);
  270. length += 4;
  271. tmp = new DataView(bin, length);
  272. this.power = tmp.getFloat32(0,true);
  273. length += 4;
  274. tmp = new DataView(bin, length);
  275. this.freq = tmp.getFloat32(0,true);
  276. length += 4;
  277. tmp = new DataView(bin, length);
  278. this.factor = tmp.getFloat32(0,true);
  279. length += 4;
  280. tmp = new DataView(bin, length);
  281. this.consump = tmp.getFloat32(0,true);
  282. length += 4;
  283. tmp = new DataView(bin, length);
  284. this.active = tmp.getFloat32(0,true);
  285. length += 4;
  286. tmp = new DataView(bin, length);
  287. this.reactive = tmp.getFloat32(0,true);
  288. length += 4;
  289. tmp = new DataView(bin, length);
  290. this.ph_out = tmp.getUint8(0,true);
  291. length += 1;
  292. tmp = new DataView(bin, length);
  293. this.status = tmp.getUint8(0,true);
  294. length += 1;
  295. tmp = new DataView(bin, length);
  296. this.nwire = tmp.getUint8(0,true);
  297. length += 1;
  298. this.len = length;
  299. }
  300. function bin_to_alarm(bin) {
  301. var tmp, length=0;
  302. tmp = new DataView(bin, length);
  303. this.v_upper = tmp.getUint8(0,true);
  304. length += 1;
  305. tmp = new DataView(bin, length);
  306. this.v_lower = tmp.getUint8(0,true);
  307. length += 1;
  308. tmp = new DataView(bin, length);
  309. this.c_upper = tmp.getUint8(0,true);
  310. length += 1;
  311. tmp = new DataView(bin, length);
  312. this.p_upper = tmp.getUint8(0,true);
  313. length += 1;
  314. tmp = new DataView(bin, length);
  315. this.w_upper = tmp.getUint8(0,true);
  316. length += 1;
  317. tmp = new DataView(bin, length);
  318. this.ph_loss = tmp.getUint8(0,true);
  319. length += 1;
  320. this.len = length;
  321. }
  322. function bin_to_thrval(bin) {
  323. var tmp, length=0;
  324. tmp = new DataView(bin, length);
  325. this.en = tmp.getUint8(0,true);
  326. length += 1;
  327. tmp = new DataView(bin, length);
  328. this.val = tmp.getFloat32(0,true);
  329. length += 4;
  330. tmp = new DataView(bin, length);
  331. this.act = tmp.getUint8(0,true);
  332. length += 1;
  333. this.act_para = [];
  334. tmp = new DataView(bin, length);
  335. for(var i=0; i<14; i++) {
  336. this.act_para[i] = tmp.getUint8(i,true);
  337. }
  338. length += 14;
  339. this.len = length;
  340. }
  341. function bin_to_thr(bin) {
  342. var tmp, length=0;
  343. this.v_upper = new bin_to_thrval(bin.slice(length));
  344. length += this.v_upper.len;
  345. this.v_lower = new bin_to_thrval(bin.slice(length));
  346. length += this.v_lower.len;
  347. this.c_upper = new bin_to_thrval(bin.slice(length));
  348. length += this.c_upper.len;
  349. this.p_upper = new bin_to_thrval(bin.slice(length));
  350. length += this.p_upper.len;
  351. this.w_upper = new bin_to_thrval(bin.slice(length));
  352. length += this.w_upper.len;
  353. this.len = length;
  354. }
  355. ////////////////////////////////
  356. function bin_to_modbus(bin) {
  357. var tmp, length=0;
  358. tmp = new DataView(bin, length);
  359. this.mode = tmp.getUint8(0,true);
  360. length += 1;
  361. tmp = new DataView(bin, length);
  362. this.addr = tmp.getUint8(0,true);
  363. length += 1;
  364. tmp = new DataView(bin, length);
  365. this.baud = tmp.getUint32(0,true);
  366. length += 4;
  367. this.len = length;
  368. }
  369. function bin_to_misc(bin) {
  370. var tmp, length=0;
  371. tmp = new DataView(bin, length);
  372. this.watchdog = tmp.getUint8(0,true);
  373. length += 1;
  374. tmp = new DataView(bin, length);
  375. this.fliptime = tmp.getUint32(0,true);
  376. length += 4;
  377. tmp = new DataView(bin, length);
  378. this.led = tmp.getUint8(0,true);
  379. length += 1;
  380. tmp = new DataView(bin, length);
  381. this.beep = tmp.getUint8(0,true);
  382. length += 1;
  383. tmp = new DataView(bin, length);
  384. this.rotate = tmp.getUint8(0,true);
  385. length += 1;
  386. this.len = length;
  387. }
  388. function bin_to_prod(bin) {
  389. var tmp, length=0;
  390. tmp = new DataView(bin, length);
  391. this.id = tmp.getUint8(0,true);
  392. length += 1;
  393. this.pname = array_to_string(bin.slice(length,32));
  394. length += 32;
  395. this.pnumber = array_to_string(bin.slice(length,32));
  396. length += 32;
  397. this.status = array_to_string(bin.slice(length,32));
  398. length += 32;
  399. this.name = array_to_string(bin.slice(length,32));
  400. length += 32;
  401. this.number = array_to_string(bin.slice(length,32));
  402. length += 32;
  403. this.date = array_to_string(bin.slice(length,32));
  404. length += 32;
  405. this.batch = array_to_string(bin.slice(length,32));
  406. length += 32;
  407. tmp = new DataView(bin, length);
  408. this.type = tmp.getUint8(0,true);
  409. length += 1;
  410. tmp = new DataView(bin, length);
  411. this.pwr_type = tmp.getUint8(0,true);
  412. length += 1;
  413. this.version = array_to_string(bin.slice(length,8));
  414. length += 8;
  415. tmp = new DataView(bin, length);
  416. this.language = tmp.getUint8(0,true);
  417. length += 1;
  418. tmp = new DataView(bin, length);
  419. this.samptime = tmp.getUint32(0,true);
  420. length += 4;
  421. tmp = new DataView(bin, length);
  422. this.savetime = tmp.getUint32(0,true);
  423. length += 4;
  424. tmp = new DataView(bin, length);
  425. this.ph_group = tmp.getUint16(0,true);
  426. length += 2;
  427. tmp = new DataView(bin, length);
  428. this.ch_delay = tmp.getUint16(0,true);
  429. length += 2;
  430. this.len = length;
  431. }
  432. function bin_to_serv(bin) {
  433. var tmp, length=0;
  434. this.name = array_to_string(bin.slice(length,256));
  435. length += 256;
  436. this.site = array_to_string(bin.slice(length,256));
  437. length += 256;
  438. this.mail = array_to_string(bin.slice(length,256));
  439. length += 256;
  440. this.addr = array_to_string(bin.slice(length,256));
  441. length += 256;
  442. this.tele = array_to_string(bin.slice(length,32));
  443. length += 32;
  444. this.len = length;
  445. }
  446. function bin_to_network(bin) {
  447. var tmp, length=0;
  448. tmp = new DataView(bin, length);
  449. this.mode = tmp.getUint8(0,true);
  450. length += 1;
  451. this.ipaddr = array_to_string(bin.slice(length,48));
  452. length += 48;
  453. this.mask = array_to_string(bin.slice(length,48));
  454. length += 48;
  455. this.gateway = array_to_string(bin.slice(length,48));
  456. length += 48;
  457. this.dns1 = array_to_string(bin.slice(length,48));
  458. length += 48;
  459. this.dns2 = array_to_string(bin.slice(length,48));
  460. length += 48;
  461. this.len = length;
  462. }
  463. ////////////////////////////////
  464. function bin_to_powerc(bin) {
  465. var tmp, bin2, length=0;
  466. this.info = new bin_to_info(bin);
  467. length += this.info.len;
  468. this.power = [];
  469. for(var i=0; i<3; i++) {
  470. this.power[i] = new bin_to_power(bin.slice(length));
  471. length += this.power[i].len;
  472. }
  473. this.thr = new bin_to_thr(bin.slice(length));
  474. length += this.thr.len;
  475. this.alarm = new bin_to_alarm(bin.slice(length));
  476. length += this.alarm.len;
  477. tmp = new DataView(bin, length);
  478. this.time = tmp.getUint32(0,true);
  479. length += 4;
  480. tmp = new DataView(bin, length);
  481. this.pbrd = tmp.getUint32(0,true);
  482. length += 4;
  483. this.len = length;
  484. }
  485. function bin_to_total(bin) {
  486. var tmp, length=0;
  487. tmp = new DataView(bin, length);
  488. this.voltage = tmp.getFloat32 (0,true);
  489. length += 4;
  490. tmp = new DataView(bin, length);
  491. this.current = tmp.getFloat32(0,true);
  492. length += 4;
  493. tmp = new DataView(bin, length);
  494. this.power = tmp.getFloat32(0,true);
  495. length += 4;
  496. tmp = new DataView(bin, length);
  497. this.freq = tmp.getFloat32(0,true);
  498. length += 4;
  499. tmp = new DataView(bin, length);
  500. this.factor = tmp.getFloat32(0,true);
  501. length += 4;
  502. tmp = new DataView(bin, length);
  503. this.consump = tmp.getFloat32(0,true);
  504. length += 4;
  505. tmp = new DataView(bin, length);
  506. this.active = tmp.getFloat32(0,true);
  507. length += 4;
  508. tmp = new DataView(bin, length);
  509. this.reactive = tmp.getFloat32(0,true);
  510. length += 4;
  511. this.len = length;
  512. }
  513. function bin_to_powert(bin) {
  514. var tmp, length=0;
  515. tmp = new DataView(bin, length);
  516. this.type = tmp.getUint8(0,true);
  517. length += 1;
  518. this.total = [];
  519. for(var i=0; i<3; i++) {
  520. this.total[i] = new bin_to_total(bin.slice(length));
  521. length += this.total[i].len;
  522. }
  523. this.len = length;
  524. }
  525. function bin_to_alarmd(bin) {
  526. var tmp, length=0;
  527. tmp = new DataView(bin, length);
  528. this.ch = tmp.getUint8(0,true);
  529. length += 1;
  530. this.alarm = new bin_to_alarm(bin.slice(length));
  531. length += this.alarm.len;
  532. tmp = new DataView(bin, length);
  533. this.time = tmp.getUint32(0,true);
  534. length += 4;
  535. this.len = length;
  536. }
  537. function bin_to_paras(bin) {
  538. var tmp, length=0;
  539. this.cas = new bin_to_modbus(bin.slice(length));
  540. length += this.cas.len;
  541. this.misc = new bin_to_misc(bin.slice(length));
  542. length += this.misc.len;
  543. this.prod = new bin_to_prod(bin.slice(length));
  544. length += this.prod.len;
  545. this.serv = new bin_to_serv(bin.slice(length));
  546. length += this.serv.len;
  547. this.network = new bin_to_network(bin.slice(length));
  548. length += this.network.len;
  549. this.len = length;
  550. }
  551. function bin_to_slist(bin) {
  552. var tmp, length=0;
  553. tmp = new DataView(bin, length);
  554. this.cnt = tmp.getUint8(0,true);
  555. length += 1;
  556. this.addr = [];
  557. tmp = new DataView(bin, length);
  558. for(var i=0; i<this.cnt; i++) {
  559. this.addr[i] = tmp.getUint8(i,true);
  560. }
  561. length += this.cnt;
  562. this.len = length;
  563. }
  564. ///////////////////////////////////////////////////
  565. function hdr_to_bin(js) {
  566. var tmp, length=0;
  567. var buffer = new ArrayBuffer(js.len);
  568. tmp = new DataView(buffer, length);
  569. tmp.setUint16(0,js.magic,true);
  570. length += 2;
  571. tmp = new DataView(buffer, length);
  572. tmp.setUint8(0,js.addr,true);
  573. length += 1;
  574. tmp = new DataView(buffer, length);
  575. tmp.setUint8(0,js.type,true);
  576. length += 1;
  577. tmp = new DataView(buffer, length);
  578. tmp.setUint8(0,js.subtype,true);
  579. length += 1;
  580. tmp = new DataView(buffer, length);
  581. tmp.setUint8(0,js.nack,true);
  582. length += 1;
  583. tmp = new DataView(buffer, length);
  584. tmp.setUint32(0,js.flag,true);
  585. length += 4;
  586. tmp = new DataView(buffer, length);
  587. tmp.setUint32(0,js.dlen,true);
  588. length += 4;
  589. return buffer;
  590. }
  591. function info_to_bin(js) {
  592. var tmp, length=0;
  593. var name = string_to_array(js.name, 32);
  594. var buffer = new ArrayBuffer(js.len-name.byteLength);
  595. tmp = new DataView(buffer, length);
  596. tmp.setUint8(0,js.type,true);
  597. length += 1;
  598. tmp = new DataView(buffer, length);
  599. tmp.setUint8(0,js.addr,true);
  600. length += 1;
  601. tmp = new DataView(buffer, length);
  602. tmp.setUint8(0,js.ch,true);
  603. length += 1;
  604. tmp = new DataView(buffer, length);
  605. tmp.setUint8(0,js.sch,true);
  606. length += 1;
  607. tmp = new DataView(buffer, length);
  608. tmp.setUint8(0,js.ph_id,true);
  609. length += 1;
  610. tmp = new DataView(buffer, length);
  611. tmp.setUint8(0,js.ph_val,true);
  612. length += 1;
  613. tmp = new DataView(buffer, length);
  614. tmp.setUint32(0,js.start_delay,true);
  615. length += 4;
  616. tmp = new DataView(buffer, length);
  617. tmp.setUint32(0,js.stop_delay,true);
  618. length += 4;
  619. return array_cat(name,buffer);
  620. }
  621. function power_to_bin(js) {
  622. var tmp, length=0;
  623. var buffer = new ArrayBuffer(js.len);
  624. tmp = new DataView(buffer, length);
  625. tmp.setFloat32(0,js.voltage,true);
  626. length += 4;
  627. tmp = new DataView(buffer, length);
  628. tmp.setFloat32(0,js.current,true);
  629. length += 4;
  630. tmp = new DataView(buffer, length);
  631. tmp.setFloat32(0,js.power,true);
  632. length += 4;
  633. tmp = new DataView(buffer, length);
  634. tmp.setFloat32(0,js.freq,true);
  635. length += 4;
  636. tmp = new DataView(buffer, length);
  637. tmp.setFloat32(0,js.factor,true);
  638. length += 4;
  639. tmp = new DataView(buffer, length);
  640. tmp.setFloat32(0,js.consump,true);
  641. length += 4;
  642. tmp = new DataView(buffer, length);
  643. tmp.setFloat32(0,js.active,true);
  644. length += 4;
  645. tmp = new DataView(buffer, length);
  646. tmp.setFloat32(0,js.reactive,true);
  647. length += 4;
  648. tmp = new DataView(buffer, length);
  649. tmp.setUint8(0,js.ph_put,true);
  650. length += 1;
  651. tmp = new DataView(buffer, length);
  652. tmp.setUint8(0,js.status,true);
  653. length += 1;
  654. tmp = new DataView(buffer, length);
  655. tmp.setUint8(0,js.nwire,true);
  656. length += 1;
  657. return buffer;
  658. }
  659. function alarm_to_bin(js) {
  660. var tmp, length=0;
  661. var buffer = new ArrayBuffer(js.len);
  662. tmp = new DataView(buffer, length);
  663. tmp.setUint8(0,js.v_upper,true);
  664. length += 1;
  665. tmp = new DataView(buffer, length);
  666. tmp.setUint8(0,js.v_lower,true);
  667. length += 1;
  668. tmp = new DataView(buffer, length);
  669. tmp.setUint8(0,js.c_upper,true);
  670. length += 1;
  671. tmp = new DataView(buffer, length);
  672. tmp.setUint8(0,js.p_upper,true);
  673. length += 1;
  674. tmp = new DataView(buffer, length);
  675. tmp.setUint8(0,js.w_upper,true);
  676. length += 1;
  677. tmp = new DataView(buffer, length);
  678. tmp.setUint8(0,js.ph_loss,true);
  679. length += 1;
  680. return buffer;
  681. }
  682. function thrval_to_bin(js) {
  683. var tmp, length=0;
  684. var buffer = new ArrayBuffer(js.len);
  685. tmp = new DataView(buffer, length);
  686. tmp.setUint8(0,js.en,true);
  687. length += 1;
  688. tmp = new DataView(buffer, length);
  689. tmp.setFloat32(0,js.val,true);
  690. length += 4;
  691. tmp = new DataView(buffer, length);
  692. tmp.getUint8(0,js.act,true);
  693. length += 1;
  694. tmp = new DataView(buffer, length);
  695. for(var i=0; i<14; i++) {
  696. tmp.setUint8(i,js.act_para[i],true);
  697. }
  698. length += 14;
  699. return buffer;
  700. }
  701. function thr_to_bin(js) {
  702. var bin,tmp, length=0;
  703. bin = thrval_to_bin(js.v_upper);
  704. tmp = thrval_to_bin(js.v_lower);
  705. bin = array_cat(bin, tmp);
  706. tmp = thrval_to_bin(js.c_upper);
  707. bin = array_cat(bin, tmp);
  708. tmp = thrval_to_bin(js.p_upper);
  709. bin = array_cat(bin, tmp);
  710. tmp = thrval_to_bin(js.w_upper);
  711. bin = array_cat(bin, tmp);
  712. return bin;
  713. }
  714. ////////////////////////////////
  715. function modbus_to_bin(js) {
  716. var tmp, length=0;
  717. var buffer = new ArrayBuffer(js.len);
  718. tmp = new DataView(buffer, length);
  719. tmp.setUint8(0,js.mode,true);
  720. length += 1;
  721. tmp = new DataView(buffer, length);
  722. tmp.setUint8(0,js.addr,true);
  723. length += 1;
  724. tmp = new DataView(buffer, length);
  725. tmp.setUint32(0,js.baud,true);
  726. length += 4;
  727. return buffer;
  728. }
  729. function misc_to_bin(js) {
  730. var tmp, length=0;
  731. var buffer = new ArrayBuffer(js.len);
  732. tmp = new DataView(buffer, length);
  733. tmp.setUint8(0,js.watchdog,true);
  734. length += 1;
  735. tmp = new DataView(buffer, length);
  736. tmp.setUint8(0,js.fliptime,true);
  737. length += 1;
  738. tmp = new DataView(buffer, length);
  739. tmp.setUint8(0,js.led,true);
  740. length += 1;
  741. tmp = new DataView(buffer, length);
  742. tmp.setUint8(0,js.beep,true);
  743. length += 1;
  744. tmp = new DataView(buffer, length);
  745. tmp.setUint8(0,js.rotate,true);
  746. length += 1;
  747. return buffer;
  748. }
  749. function prod_to_bin(js) {
  750. var tmp, length=0;
  751. var buffer = new ArrayBuffer(js.len);
  752. tmp = new DataView(buffer, length);
  753. tmp.setUint32(0,js.id,true);
  754. length += 4;
  755. array_set(buffer,js.pname,length);
  756. length += 32;
  757. array_set(buffer,js.pnumber,length);
  758. length += 32;
  759. array_set(buffer,js.status,length);
  760. length += 32;
  761. array_set(buffer,js.name,length);
  762. length += 32;
  763. array_set(buffer,js.number,length);
  764. length += 32;
  765. array_set(buffer,js.date,length);
  766. length += 32;
  767. array_set(buffer,js.batch,length);
  768. length += 32;
  769. tmp = new DataView(buffer, length);
  770. tmp.setUint8(0,js.type,true);
  771. length += 1;
  772. tmp = new DataView(buffer, length);
  773. tmp.setUint8(0,js.pwrtype,true);
  774. length += 1;
  775. array_set(buffer,js.version,length);
  776. length += 8;
  777. tmp = new DataView(buffer, length);
  778. tmp.setUint8(0,js.language,true);
  779. length += 1;
  780. tmp = new DataView(buffer, length);
  781. tmp.setUint8(0,js.count,true);
  782. length += 1;
  783. tmp = new DataView(buffer, length);
  784. tmp.setUint32(0,js.samptime,true);
  785. length += 4;
  786. tmp = new DataView(buffer, length);
  787. tmp.setUint32(0,js.savetime,true);
  788. length += 4;
  789. tmp = new DataView(buffer, length);
  790. tmp.setUint16(0,js.ph_group,true);
  791. length += 2;
  792. tmp = new DataView(buffer, length);
  793. tmp.setUint16(0,js.ch_delay,true);
  794. length += 2;
  795. return buffer;
  796. }
  797. function serv_to_bin(js) {
  798. var tmp, length=0;
  799. var buffer = new ArrayBuffer(js.len);
  800. array_set(buffer,js.name,length);
  801. length += 256;
  802. array_set(buffer,js.site,length);
  803. length += 256;
  804. array_set(buffer,js.mail,length);
  805. length += 256;
  806. array_set(buffer,js.addr,length);
  807. length += 256;
  808. array_set(buffer,js.tele,length);
  809. length += 32;
  810. return buffer;
  811. }
  812. function network_to_bin(js) {
  813. var tmp, length=0;
  814. var buffer = new ArrayBuffer(js.len);
  815. tmp = new DataView(buffer, length);
  816. tmp.setUint8(0,js.mode,true);
  817. length += 1;
  818. array_set(buffer,js.ipaddr,length);
  819. length += 48;
  820. array_set(buffer,js.mask,length);
  821. length += 48;
  822. array_set(buffer,js.gateway,length);
  823. length += 48;
  824. array_set(buffer,js.dns1,length);
  825. length += 48;
  826. array_set(buffer,js.dns2,length);
  827. length += 48;
  828. return buffer;
  829. }
  830. ////////////////////////////////
  831. function powerc_to_bin(js) {
  832. var bin,tmp, length=0;
  833. bin = new info_to_bin(js.info);
  834. for(var i=0; i<3; i++) {
  835. tmp = new power_to_bin(js.power[i]);
  836. bin = array_cat(bin, tmp);
  837. }
  838. tmp = new thr_to_bin(js.thr);
  839. bin = array_cat(bin, tmp);
  840. tmp = new alarm_to_bin(js.alarm);
  841. bin = array_cat(bin, tmp);
  842. var buf = new ArrayBuffer(8);
  843. tmp = new DataView(buf, 0);
  844. tmp.setUint32(0,js.time,true);
  845. tmp.setUint32(4,js.pbrd,true);
  846. bin = array_cat(bin, buf);
  847. return bin;
  848. }
  849. function total_to_bin(js) {
  850. var tmp, length=0;
  851. var buffer = new ArrayBuffer(js.len);
  852. tmp = new DataView(buffer, length);
  853. tmp.setFloat32(0,js.voltage,true);
  854. length += 4;
  855. tmp = new DataView(buffer, length);
  856. tmp.setFloat32(0,js.current,true);
  857. length += 4;
  858. tmp = new DataView(buffer, length);
  859. tmp.setFloat32(0,js.power,true);
  860. length += 4;
  861. tmp = new DataView(buffer, length);
  862. tmp.setFloat32(0,js.freq,true);
  863. length += 4;
  864. tmp = new DataView(buffer, length);
  865. tmp.setFloat32(0,js.factor,true);
  866. length += 4;
  867. tmp = new DataView(buffer, length);
  868. tmp.setFloat32(0,js.consump,true);
  869. length += 4;
  870. tmp = new DataView(buffer, length);
  871. tmp.setFloat32(0,js.factor,true);
  872. length += 4;
  873. tmp = new DataView(buffer, length);
  874. tmp.setFloat32(0,js.active,true);
  875. length += 4;
  876. tmp = new DataView(buffer, length);
  877. tmp.setFloat32(0,js.reactive,true);
  878. length += 4;
  879. return buffer;
  880. }
  881. function powert_to_bin(js) {
  882. var tmp,bin2,length=0;
  883. var bin = new ArrayBuffer(1);
  884. tmp = new DataView(bin, length);
  885. tmp.setUint8(0,js.type,true);
  886. length += 4;
  887. for(var i=0; i<3; i++) {
  888. bin2 = total_to_bin(js.total[i]);
  889. bin = array_cat(bin, bin2);
  890. }
  891. return bin;
  892. }
  893. function alarmd_to_bin(js) {
  894. var tmp, length=0;
  895. var bin,buffer = new ArrayBuffer(1);
  896. tmp = new DataView(buffer, 0);
  897. tmp.setUint8(0,js.ch,true);
  898. var alarm = alarm_to_bin(js.alarm);
  899. bin = array_cat(buffer, alarm);
  900. buffer = new ArrayBuffer(4);
  901. tmp = new DataView(buffer, 0);
  902. tmp.setUint32(0,js.time,true);
  903. return array_cat(bin, buffer);
  904. }
  905. //////////////////////////////////////////////////////////////
  906. function bin_parse(bin)
  907. {
  908. var tmp,h = new bin_to_hdr(bin);
  909. if(h.nack == 1){
  910. //tmp = myBinary.ack.bin2js(bin,this.h.len,true);
  911. //myLog('log',tmp);
  912. //var data = tmp.data;
  913. }
  914. var data = bin.slice(h.len);
  915. if(data.byteLength>0 && h.dlen==data.byteLength) {
  916. switch(h.type) {
  917. case PKT_TYPE.PKT_TYPE_POWER_CH:
  918. {
  919. var tmp=[],length=0;
  920. for(var i=0;; i++) {
  921. tmp[i] = new bin_to_powerc(data.slice(length));
  922. length += tmp[i].len;
  923. if(length >= data.byteLength) {
  924. break;
  925. }
  926. }
  927. h.powerc = tmp;
  928. }
  929. break;
  930. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  931. h.powert = new bin_to_powert(data);
  932. break;
  933. case PKT_TYPE.PKT_TYPE_ALARM:
  934. h.alarmd = new bin_to_alarm(data);
  935. break;
  936. case PKT_TYPE.PKT_TYPE_SLAVE_LIST:
  937. h.alarmd = new bin_to_slist(data);
  938. break;
  939. case PKT_TYPE.PKT_TYPE_PARAS:
  940. h.alarmd = new bin_to_paras(data);
  941. break;
  942. default :
  943. console.log("wrong hdr type: "+h.type);
  944. break;
  945. }
  946. }
  947. return h;
  948. };
  949. //////////////////////////////////////////
  950. function LOG(){
  951. var data = [];
  952. for(var i= 1,len=arguments.length;i<len;i++){
  953. data.push(arguments[i])
  954. }
  955. console.log(data);
  956. }
  957. function WS(url,fn) {
  958. this.url = url;
  959. this.name = 'myWebs';
  960. this.open = _open;
  961. this.reopen = _reopen;
  962. this.close = _close;
  963. this.send = _send;
  964. this.state = _state;
  965. this.setcb = _setcb;
  966. this.closeCount = 0;
  967. this.maxConnect = 100;
  968. var his = this;
  969. this.open();
  970. function _open() {
  971. console.log("____url: ", his.url);
  972. his.ws = new WebSocket(his.url);
  973. his.ws.binaryType = 'arraybuffer';
  974. his.ws.onopen = _onopen;
  975. his.ws.onclose = _onclose;
  976. his.ws.onmessage = _onmsg;
  977. his.ws.onerror = _onerror;
  978. }
  979. function _reopen() {
  980. _close();
  981. _open(his.url);
  982. }
  983. function _close() {
  984. his.ws.close();
  985. }
  986. function _send(msg) {
  987. if (his.ws.readyState != WebSocket.OPEN)
  988. return;
  989. his.ws.send(msg);
  990. }
  991. function _state() {
  992. return his.ws.readyState;
  993. }
  994. function _setcb(tp, f) {
  995. switch (tp) {
  996. case "open":
  997. his.ws.onopen = f;
  998. break;
  999. case "close":
  1000. his.ws.onclose = f;
  1001. break;
  1002. case "msg":
  1003. his.ws.onmessage = f;
  1004. break;
  1005. case "err":
  1006. his.ws.onerror = f;
  1007. break;
  1008. }
  1009. }
  1010. function loop_send() {
  1011. console.log("PKT_TYPE_POWER_CH request");
  1012. _send(mk_hdr(PKT_TYPE.PKT_TYPE_POWER_CH));
  1013. setTimeout(loop_send, 2000);
  1014. }
  1015. function _onopen(evt) {
  1016. his.closeCount = 0;
  1017. console.log("___ ws.onopen");
  1018. //setTimeout(loop_send, 2000);
  1019. }
  1020. function _onclose(evt) {
  1021. LOG("ws.onclose");
  1022. setTimeout(_reopen, 3000);
  1023. his.closeCount += 1;
  1024. }
  1025. function _onmsg(evt) {
  1026. console.log("___ ws.onmsg");
  1027. if (evt.data instanceof ArrayBuffer) {
  1028. var obj=bin_parse(evt.data);
  1029. fn(obj);
  1030. }
  1031. }
  1032. function _onerror(evt) {
  1033. console.log("___ ws.onerror");
  1034. //write_scr('<span style="color: red;">ERROR:</span> '+ evt.data);
  1035. }
  1036. }