data.js 22 KB

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