data.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. }
  20. }();
  21. const SUB_TYPE=function(){
  22. var len = 0;
  23. return {
  24. SUB_TYPE_POWER_SWITCH:len++,
  25. SUB_TYPE_POWER_ALARM:len++,
  26. SUB_TYPE_POWER_THRESHOLD:len++, //power_ch_t
  27. SUB_TYPE_POWER_START_DELAY:len++, //power_total_t
  28. SUB_TYPE_POWER_STOP_DELAY:len++,
  29. }
  30. }();
  31. const PDU_TYPE=function(){
  32. return {
  33. PDU_AC_I1O1:0,
  34. PDU_DC_I1O1:1,
  35. PDU_AC_I3O3:3,
  36. PDU_AC_I3O1:4,
  37. PDU_AC_I3O1_H:5,
  38. }
  39. }();
  40. ///////////////////////////////////////////////////////
  41. function array_to_string(data){
  42. const decoder = new TextDecoder();
  43. return decoder.decode(data);
  44. }
  45. function string_to_array(data,len){
  46. const encoder = new TextEncoder(); // ĬÈÏ'utf-8'±àÂë
  47. const buffer = encoder.encode(data);
  48. const fixedArray = new Uint8Array(len);
  49. fixedArray.set(encoder.subarray(0, len), 0);
  50. return fixedArray.buffer
  51. }
  52. function array_cat(a1,a2){
  53. var tlen=a1.byteLength+a2.byteLength;
  54. var buffer = new ArrayBuffer(tlen);
  55. var vw = new Uint8Array(buffer);
  56. var vw1 = new Uint8Array(a1);
  57. for(var i=0; i<a1.byteLength; i++) {
  58. vw[i] = vw1[i];
  59. }
  60. var vw2 = new Uint8Array(a2);
  61. for(var i=0; i<a2.byteLength; i++) {
  62. vw[i+a1.byteLength] = vw2[i];
  63. }
  64. return buffer;
  65. }
  66. function mk_hdr(type,flag)
  67. {
  68. var tmp,length=0;
  69. var buffer = new ArrayBuffer(13);
  70. tmp = new DataView(buffer, length);
  71. var t = MAGIC;
  72. tmp.setUint16(0,t,true);//magic
  73. length += 2;
  74. tmp = new DataView(buffer, length);
  75. tmp.setUint8(0,type,true); //type
  76. length += 1;
  77. tmp = new DataView(buffer, length);
  78. tmp.setUint8(0,0,true); //subtype
  79. length += 1;
  80. tmp = new DataView(buffer, length);
  81. tmp.setUint8(0,0,true); //nack
  82. length += 1;
  83. tmp = new DataView(buffer, length);
  84. tmp.setUint32(0,flag,true); //flag
  85. length += 4;
  86. tmp = new DataView(buffer, length);
  87. tmp.setUint32(0,0,true); //dlen
  88. length += 4;
  89. return buffer;
  90. }
  91. ///////////////////////////////////////////////////
  92. function bin_to_hdr(bin) {
  93. var tmp, length=0;
  94. tmp = new DataView(bin, length);
  95. this.magic = tmp.getUint16(0,true);
  96. length += 2;
  97. tmp = new DataView(bin, length);
  98. this.type = tmp.getUint8(0,true);
  99. length += 1;
  100. tmp = new DataView(bin, length);
  101. this.subtype = tmp.getUint8(0,true);
  102. length += 1;
  103. tmp = new DataView(bin, length);
  104. this.nack = tmp.getUint8(0,true);
  105. length += 1;
  106. tmp = new DataView(bin, length);
  107. this.flag = tmp.getUint8(0,true);
  108. length += 4;
  109. tmp = new DataView(bin, length);
  110. this.dlen = tmp.getUint32(0,true);
  111. length += 4;
  112. this.len = length;
  113. }
  114. function bin_to_info(bin) {
  115. var tmp, length=0;
  116. this.name = array_to_string(bin.slice(0,32));
  117. length += 32;
  118. tmp = new DataView(bin, length);
  119. this.type = tmp.getUint8(0,true);
  120. length += 1;
  121. tmp = new DataView(bin, length);
  122. this.addr = tmp.getUint8(0,true);
  123. length += 1;
  124. tmp = new DataView(bin, length);
  125. this.ch = tmp.getUint8(0,true);
  126. length += 1;
  127. tmp = new DataView(bin, length);
  128. this.sch = tmp.getUint8(0,true);
  129. length += 1;
  130. tmp = new DataView(bin, length);
  131. this.ph_id = tmp.getUint8(0,true);
  132. length += 1;
  133. tmp = new DataView(bin, length);
  134. this.ph_val = tmp.getUint8(0,true);
  135. length += 1;
  136. tmp = new DataView(bin, length);
  137. this.start_delay = tmp.getUint32(0,true);
  138. length += 4;
  139. tmp = new DataView(bin, length);
  140. this.stop_delay = tmp.getUint32(0,true);
  141. length += 4;
  142. this.len = length;
  143. }
  144. function bin_to_power(bin) {
  145. var tmp, length=0;
  146. tmp = new DataView(bin, length);
  147. this.voltage = tmp.getFloat32(0,true);
  148. length += 4;
  149. tmp = new DataView(bin, length);
  150. this.current = tmp.getFloat32(0,true);
  151. length += 4;
  152. tmp = new DataView(bin, length);
  153. this.power = tmp.getFloat32(0,true);
  154. length += 4;
  155. tmp = new DataView(bin, length);
  156. this.freq = tmp.getFloat32(0,true);
  157. length += 4;
  158. tmp = new DataView(bin, length);
  159. this.factor = tmp.getFloat32(0,true);
  160. length += 4;
  161. tmp = new DataView(bin, length);
  162. this.consump = tmp.getFloat32(0,true);
  163. length += 4;
  164. tmp = new DataView(bin, length);
  165. this.active = tmp.getFloat32(0,true);
  166. length += 4;
  167. tmp = new DataView(bin, length);
  168. this.reactive = tmp.getFloat32(0,true);
  169. length += 4;
  170. tmp = new DataView(bin, length);
  171. this.ph_out = tmp.getUint8(0,true);
  172. length += 1;
  173. tmp = new DataView(bin, length);
  174. this.status = tmp.getUint8(0,true);
  175. length += 1;
  176. tmp = new DataView(bin, length);
  177. this.nwire = tmp.getUint8(0,true);
  178. length += 1;
  179. this.len = length;
  180. }
  181. function bin_to_alarm(bin) {
  182. var tmp, length=0;
  183. tmp = new DataView(bin, length);
  184. this.v_upper = tmp.getUint8(0,true);
  185. length += 1;
  186. tmp = new DataView(bin, length);
  187. this.v_lower = tmp.getUint8(0,true);
  188. length += 1;
  189. tmp = new DataView(bin, length);
  190. this.c_upper = tmp.getUint8(0,true);
  191. length += 1;
  192. tmp = new DataView(bin, length);
  193. this.p_upper = tmp.getUint8(0,true);
  194. length += 1;
  195. tmp = new DataView(bin, length);
  196. this.w_upper = tmp.getUint8(0,true);
  197. length += 1;
  198. tmp = new DataView(bin, length);
  199. this.ph_loss = tmp.getUint8(0,true);
  200. length += 1;
  201. this.len = length;
  202. }
  203. function bin_to_thrval(bin) {
  204. var tmp, length=0;
  205. tmp = new DataView(bin, length);
  206. this.en = tmp.getUint8(0,true);
  207. length += 1;
  208. tmp = new DataView(bin, length);
  209. this.val = tmp.getFloat32(0,true);
  210. length += 4;
  211. tmp = new DataView(bin, length);
  212. this.act = tmp.getUint8(0,true);
  213. length += 1;
  214. this.act_para = [];
  215. tmp = new DataView(bin, length);
  216. for(var i=0; i<14; i++) {
  217. this.act_para[i] = tmp.getUint8(i,true);
  218. }
  219. length += 14;
  220. this.len = length;
  221. }
  222. function bin_to_thr(bin) {
  223. var tmp, length=0;
  224. this.v_upper = new bin_to_thrval(bin.slice(length));
  225. length += this.v_upper.len;
  226. this.v_lower = new bin_to_thrval(bin.slice(length));
  227. length += this.v_lower.len;
  228. this.c_upper = new bin_to_thrval(bin.slice(length));
  229. length += this.c_upper.len;
  230. this.p_upper = new bin_to_thrval(bin.slice(length));
  231. length += this.p_upper.len;
  232. this.w_upper = new bin_to_thrval(bin.slice(length));
  233. length += this.w_upper.len;
  234. this.len = length;
  235. }
  236. function bin_to_powerc(bin) {
  237. var tmp, bin2, length=0;
  238. this.info = new bin_to_info(bin);
  239. length += this.info.len;
  240. this.power = [];
  241. for(var i=0; i<3; i++) {
  242. this.power[i] = new bin_to_power(bin.slice(length));
  243. length += this.power[i].len;
  244. }
  245. this.thr = new bin_to_thr(bin.slice(length));
  246. length += this.thr.len;
  247. this.alarm = new bin_to_alarm(bin.slice(length));
  248. length += this.alarm.len;
  249. tmp = new DataView(bin, length);
  250. this.time = tmp.getUint32(0,true);
  251. length += 4;
  252. tmp = new DataView(bin, length);
  253. this.pbrd = tmp.getUint32(0,true);
  254. length += 4;
  255. this.len = length;
  256. }
  257. function bin_to_total(bin) {
  258. var tmp, length=0;
  259. tmp = new DataView(bin, length);
  260. this.voltage = tmp.getFloat32 (0,true);
  261. length += 4;
  262. tmp = new DataView(bin, length);
  263. this.current = tmp.getFloat32(0,true);
  264. length += 4;
  265. tmp = new DataView(bin, length);
  266. this.power = tmp.getFloat32(0,true);
  267. length += 4;
  268. tmp = new DataView(bin, length);
  269. this.freq = tmp.getFloat32(0,true);
  270. length += 4;
  271. tmp = new DataView(bin, length);
  272. this.consump = tmp.getFloat32 (0,true);
  273. length += 4;
  274. tmp = new DataView(bin, length);
  275. this.active = tmp.getFloat32(0,true);
  276. length += 4;
  277. tmp = new DataView(bin, length);
  278. this.reactive = tmp.getFloat32(0,true);
  279. length += 4;
  280. this.len = length;
  281. }
  282. function bin_to_powert(bin) {
  283. var tmp, length=0;
  284. tmp = new DataView(bin, length);
  285. this.type = tmp.getUint8(0,true);
  286. length += 1;
  287. this.total = [];
  288. for(var i=0; i<3; i++) {
  289. this.total[i] = new bin_to_total(bin.slice(length));
  290. length += this.total[i].len;
  291. }
  292. this.len = length;
  293. }
  294. function bin_to_alarmd(bin) {
  295. var tmp, length=0;
  296. tmp = new DataView(bin, length);
  297. this.ch = tmp.getUint8(0,true);
  298. length += 1;
  299. this.alarm = new bin_to_alarm(bin.slice(length));
  300. length += this.alarm.len;
  301. tmp = new DataView(bin, length);
  302. this.time = tmp.getUint32(0,true);
  303. length += 1;
  304. this.len = length;
  305. }
  306. ///////////////////////////////////////////////////
  307. function hdr_to_bin(js) {
  308. var tmp, length=0;
  309. var buffer = new ArrayBuffer(js.len);
  310. tmp = new DataView(buffer, length);
  311. tmp.setUint16(0,js.magic,true);
  312. length += 2;
  313. tmp = new DataView(buffer, length);
  314. tmp.setUint8(0,js.type,true);
  315. length += 1;
  316. tmp = new DataView(buffer, length);
  317. tmp.setUint8(0,js.subtype,true);
  318. length += 1;
  319. tmp = new DataView(buffer, length);
  320. tmp.setUint8(0,js.nack,true);
  321. length += 1;
  322. tmp = new DataView(buffer, length);
  323. tmp.setUint32(0,js.flag,true);
  324. length += 4;
  325. tmp = new DataView(buffer, length);
  326. tmp.setUint32(0,js.dlen,true);
  327. length += 4;
  328. return buffer;
  329. }
  330. function info_to_bin(js) {
  331. var tmp, length=0;
  332. var buffer = new ArrayBuffer(js.len);
  333. var name = string_to_array(js.name);
  334. tmp = new DataView(buffer, length);
  335. tmp.setUint8(0,js.type,true);
  336. length += 1;
  337. tmp = new DataView(buffer, length);
  338. tmp.setUint8(0,js.addr,true);
  339. length += 1;
  340. tmp = new DataView(buffer, length);
  341. tmp.setUint8(0,js.ch,true);
  342. length += 1;
  343. tmp = new DataView(buffer, length);
  344. tmp.setUint8(0,js.sch,true);
  345. length += 1;
  346. tmp = new DataView(buffer, length);
  347. tmp.setUint8(0,js.ph_id,true);
  348. length += 1;
  349. tmp = new DataView(buffer, length);
  350. tmp.setUint8(0,js.ph_val,true);
  351. length += 1;
  352. tmp = new DataView(buffer, length);
  353. tmp.setUint32(0,js.start_delay,true);
  354. length += 4;
  355. tmp = new DataView(buffer, length);
  356. tmp.setUint32(0,js.stop_delay,true);
  357. length += 4;
  358. return array_cat(name,buffer);
  359. }
  360. function power_to_bin(js) {
  361. var tmp, length=0;
  362. var buffer = new ArrayBuffer(js.len);
  363. tmp = new DataView(buffer, length);
  364. tmp.setFloat32(0,js.voltage,true);
  365. length += 4;
  366. tmp = new DataView(buffer, length);
  367. tmp.setFloat32(0,js.current,true);
  368. length += 4;
  369. tmp = new DataView(buffer, length);
  370. tmp.setFloat32(0,js.power,true);
  371. length += 4;
  372. tmp = new DataView(buffer, length);
  373. tmp.setFloat32(0,js.freq,true);
  374. length += 4;
  375. tmp = new DataView(buffer, length);
  376. tmp.setFloat32(0,js.factor,true);
  377. length += 4;
  378. tmp = new DataView(buffer, length);
  379. tmp.setFloat32(0,js.consump,true);
  380. length += 4;
  381. tmp = new DataView(buffer, length);
  382. tmp.setFloat32(0,js.active,true);
  383. length += 4;
  384. tmp = new DataView(buffer, length);
  385. tmp.setFloat32(0,js.reactive,true);
  386. length += 4;
  387. tmp = new DataView(buffer, length);
  388. tmp.setUint8(0,js.ph_put,true);
  389. length += 1;
  390. tmp = new DataView(buffer, length);
  391. tmp.setUint8(0,js.status,true);
  392. length += 1;
  393. tmp = new DataView(buffer, length);
  394. tmp.setUint8(0,js.nwire,true);
  395. length += 1;
  396. return buffer;
  397. }
  398. function alarm_to_bin(js) {
  399. var tmp, length=0;
  400. var buffer = new ArrayBuffer(js.len);
  401. tmp = new DataView(buffer, length);
  402. tmp.setUint8(0,js.v_upper,true);
  403. length += 1;
  404. tmp = new DataView(buffer, length);
  405. tmp.setUint8(0,js.v_lower,true);
  406. length += 1;
  407. tmp = new DataView(buffer, length);
  408. tmp.setUint8(0,js.c_upper,true);
  409. length += 1;
  410. tmp = new DataView(buffer, length);
  411. tmp.setUint8(0,js.p_upper,true);
  412. length += 1;
  413. tmp = new DataView(buffer, length);
  414. tmp.setUint8(0,js.w_upper,true);
  415. length += 1;
  416. tmp = new DataView(buffer, length);
  417. tmp.setUint8(0,js.ph_loss,true);
  418. length += 1;
  419. return buffer;
  420. }
  421. function thrval_to_js(js) {
  422. var tmp, length=0;
  423. var buffer = new ArrayBuffer(js.len);
  424. tmp = new DataView(buffer, length);
  425. tmp.setUint8(0,js.en,true);
  426. length += 1;
  427. tmp = new DataView(buffer, length);
  428. tmp.setFloat32(0,js.val,true);
  429. length += 4;
  430. tmp = new DataView(buffer, length);
  431. tmp.getUint8(0,js.act,true);
  432. length += 1;
  433. tmp = new DataView(buffer, length);
  434. for(var i=0; i<14; i++) {
  435. tmp.setUint8(i,js.act_para[i],true);
  436. }
  437. length += 14;
  438. return buffer;
  439. }
  440. function thr_to_bin(js) {
  441. var bin,tmp, length=0;
  442. bin = thrval_to_js(js.v_upper);
  443. tmp = thrval_to_js(js.v_lower);
  444. bin = array_cat(bin, tmp);
  445. tmp = thrval_to_js(js.c_upper);
  446. bin = array_cat(bin, tmp);
  447. tmp = thrval_to_js(js.p_upper);
  448. bin = array_cat(bin, tmp);
  449. tmp = thrval_to_js(js.w_upper);
  450. bin = array_cat(bin, tmp);
  451. return bin;
  452. }
  453. function powerc_to_bin(js) {
  454. var bin,tmp, length=0;
  455. bin = info_to_bin(js.info);
  456. for(var i=0; i<3; i++) {
  457. tmp = power_to_bin(js.power[i]);
  458. bin = array_cat(bin, tmp);
  459. }
  460. tmp = thr_to_bin(js.thr);
  461. bin = array_cat(bin, tmp);
  462. tmp = alarm_to_bin(js.alarm);
  463. bin = array_cat(bin, tmp);
  464. var buf = new ArrayBuffer(8);
  465. tmp = new DataView(buf, 0);
  466. tmp.setUint32(0,js.time,true);
  467. tmp.setUint32(4,js.pbrd,true);
  468. bin = array_cat(bin, buf);
  469. return bin;
  470. }
  471. function total_to_bin(js) {
  472. var tmp, length=0;
  473. var buffer = new ArrayBuffer(js.len);
  474. tmp = new DataView(buffer, length);
  475. tmp.setFloat32(0,js.voltage,true);
  476. length += 4;
  477. tmp = new DataView(buffer, length);
  478. tmp.setFloat32(0,js.current,true);
  479. length += 4;
  480. tmp = new DataView(buffer, length);
  481. tmp.setFloat32(0,js.power,true);
  482. length += 4;
  483. tmp = new DataView(buffer, length);
  484. tmp.setFloat32(0,js.freq,true);
  485. length += 4;
  486. tmp = new DataView(buffer, length);
  487. tmp.setFloat32(0,js.consump,true);
  488. length += 4;
  489. tmp = new DataView(buffer, length);
  490. tmp.setFloat32(0,js.factor,true);
  491. length += 4;
  492. tmp = new DataView(buffer, length);
  493. tmp.setFloat32(0,js.active,true);
  494. length += 4;
  495. tmp = new DataView(buffer, length);
  496. tmp.setFloat32(0,js.reactive,true);
  497. length += 4;
  498. return buffer;
  499. }
  500. function powert_to_bin(js) {
  501. var tmp,bin2,length=0;
  502. var bin = new ArrayBuffer(1);
  503. tmp = new DataView(bin, length);
  504. tmp.setUint8(0,js.type,true);
  505. length += 4;
  506. for(var i=0; i<3; i++) {
  507. bin2 = total_to_bin(js.total[i]);
  508. bin = array_cat(bin, bin2);
  509. }
  510. return bin;
  511. }
  512. function alarmd_to_bin(js) {
  513. var tmp, length=0;
  514. var bin,buffer = new ArrayBuffer(1);
  515. tmp = new DataView(buffer, 0);
  516. tmp.setUint8(0,js.ch,true);
  517. var alarm = alarm_to_bin(js.alarm);
  518. bin = array_cat(buffer, alarm);
  519. buffer = new ArrayBuffer(4);
  520. tmp = new DataView(buffer, 0);
  521. tmp.setUint32(0,js.time,true);
  522. return array_cat(bin, buffer);
  523. }
  524. //////////////////////////////////////////////////////////////
  525. var XXX ={};
  526. XXX.hdr = function() {
  527. this.j2b = function (js) {
  528. var tmp;
  529. var hdr = new hdr_to_bin(js);
  530. switch(js.type){
  531. case PKT_TYPE.PKT_TYPE_POWER_CH:
  532. tmp = BIN.powerc.j2b(js.powerc);
  533. break;
  534. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  535. tmp = BIN.powert.j2b(js.powert);
  536. break;
  537. case PKT_TYPE.PKT_TYPE_ALARM:
  538. tmp = BIN.alarm.j2b(js.alarmd);
  539. break;
  540. default:
  541. return tmp;
  542. }
  543. return array_cat(hdr,tmp);
  544. };
  545. this.b2j = function (bin) {
  546. var tmp;
  547. var hdr = new bin_to_hdr(bin);
  548. if(hdr.nack == 1){
  549. //tmp = myBinary.ack.bin2js(bin,this.hdr.len,true);
  550. //myLog('log',tmp);
  551. //var data = tmp.data;
  552. }
  553. var data = bin.slice(hdr.len);
  554. if(data.byteLength>0 && hdr.dlen==data.byteLength) {
  555. switch(hdr.type) {
  556. case PKT_TYPE.PKT_TYPE_POWER_CH:
  557. hdr.powerc = BIN.powerc.b2j(data);
  558. break;
  559. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  560. hdr.powert = BIN.powert.b2j(data);
  561. break;
  562. case PKT_TYPE.PKT_TYPE_ALARM:
  563. hdr.alarmd = BIN.alarmd.b2j(data);
  564. break;
  565. default :
  566. console.log("wrong hdr type: "+hdr.type);
  567. }
  568. }
  569. return hdr;
  570. }
  571. };
  572. XXX.paras=function() {
  573. this.j2b = function (js) {
  574. }
  575. this.b2j = function (bin) {
  576. }
  577. };
  578. XXX.powerc=function() {
  579. this.j2b = function (js) {
  580. return new powerc_to_bin(js);
  581. }
  582. this.b2j = function (bin) {
  583. var tmp=[],length=0;
  584. for(var i=0;; i++) {
  585. tmp[i] = new bin_to_powerc(bin.slice(length));
  586. length += tmp[i].len;
  587. if(length >= bin.byteLength) {
  588. break;
  589. }
  590. }
  591. return tmp;
  592. }
  593. };
  594. XXX.powert=function() {
  595. this.j2b = function (js) {
  596. return new powert_to_bin(js);
  597. }
  598. this.b2j = function (bin) {
  599. return new bin_to_powert(bin);
  600. }
  601. };
  602. XXX.alarmd=function() {
  603. this.j2b = function (js) {
  604. return new alarmd_to_bin(js);
  605. }
  606. this.b2j = function (bin) {
  607. return new bin_to_alarmd(bin);
  608. }
  609. };
  610. var BIN=new function() {
  611. this.hdr = new XXX.hdr();
  612. this.paras = new XXX.paras();
  613. this.powerc = new XXX.powerc();
  614. this.powert = new XXX.powert();
  615. this.alarmd = new XXX.alarmd();
  616. }();
  617. //////////////////////////////////////////
  618. function LOG(){
  619. var data = [];
  620. for(var i= 1,len=arguments.length;i<len;i++){
  621. data.push(arguments[i])
  622. }
  623. console.log(data);
  624. }
  625. function WS(url,fn) {
  626. this.url = url;
  627. this.name = 'myWebs';
  628. this.open = _open;
  629. this.reopen = _reopen;
  630. this.close = _close;
  631. this.send = _send;
  632. this.state = _state;
  633. this.setcb = _setcb;
  634. this.closeCount = 0;
  635. this.maxConnect = 100;
  636. var his = this;
  637. this.open();
  638. function _open() {
  639. console.log("____url: ", his.url);
  640. his.ws = new WebSocket(his.url);
  641. his.ws.binaryType = 'arraybuffer';
  642. his.ws.onopen = _onopen;
  643. his.ws.onclose = _onclose;
  644. his.ws.onmessage = _onmsg;
  645. his.ws.onerror = _onerror;
  646. }
  647. function _reopen() {
  648. _close();
  649. _open(his.url);
  650. }
  651. function _close() {
  652. his.ws.close();
  653. }
  654. function _send(msg) {
  655. if (his.ws.readyState != WebSocket.OPEN)
  656. return;
  657. his.ws.send(msg);
  658. }
  659. function _state() {
  660. return his.ws.readyState;
  661. }
  662. function _setcb(tp, f) {
  663. switch (tp) {
  664. case "open":
  665. his.ws.onopen = f;
  666. break;
  667. case "close":
  668. his.ws.onclose = f;
  669. break;
  670. case "msg":
  671. his.ws.onmessage = f;
  672. break;
  673. case "err":
  674. his.ws.onerror = f;
  675. break;
  676. }
  677. }
  678. function loop_send() {
  679. console.log("PKT_TYPE_POWER_CH request");
  680. _send(mk_hdr(PKT_TYPE.PKT_TYPE_POWER_CH));
  681. setTimeout(loop_send, 2000);
  682. }
  683. function _onopen(evt) {
  684. his.closeCount = 0;
  685. console.log("___ ws.onopen");
  686. setTimeout(loop_send, 2000);
  687. }
  688. function _onclose(evt) {
  689. LOG("ws.onclose");
  690. setTimeout(_reopen, 3000);
  691. his.closeCount += 1;
  692. }
  693. function _onmsg(evt) {
  694. console.log("___ ws.onmsg");
  695. if (evt.data instanceof ArrayBuffer) {
  696. fn(evt.data);
  697. }
  698. }
  699. function _onerror(evt) {
  700. console.log("___ ws.onerror");
  701. //write_scr('<span style="color: red;">ERROR:</span> '+ evt.data);
  702. }
  703. }