data.js 20 KB

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