data.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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.factor = tmp.getFloat32(0,true);
  334. length += 4;
  335. tmp = new DataView(bin, length);
  336. this.consump = tmp.getFloat32(0,true);
  337. length += 4;
  338. tmp = new DataView(bin, length);
  339. this.active = tmp.getFloat32(0,true);
  340. length += 4;
  341. tmp = new DataView(bin, length);
  342. this.reactive = tmp.getFloat32(0,true);
  343. length += 4;
  344. this.len = length;
  345. }
  346. function bin_to_powert(bin) {
  347. var tmp, length=0;
  348. tmp = new DataView(bin, length);
  349. this.type = tmp.getUint8(0,true);
  350. length += 1;
  351. this.total = [];
  352. for(var i=0; i<3; i++) {
  353. this.total[i] = new bin_to_total(bin.slice(length));
  354. length += this.total[i].len;
  355. }
  356. this.len = length;
  357. }
  358. function bin_to_alarmd(bin) {
  359. var tmp, length=0;
  360. tmp = new DataView(bin, length);
  361. this.ch = tmp.getUint8(0,true);
  362. length += 1;
  363. this.alarm = new bin_to_alarm(bin.slice(length));
  364. length += this.alarm.len;
  365. tmp = new DataView(bin, length);
  366. this.time = tmp.getUint32(0,true);
  367. length += 1;
  368. this.len = length;
  369. }
  370. ///////////////////////////////////////////////////
  371. function hdr_to_bin(js) {
  372. var tmp, length=0;
  373. var buffer = new ArrayBuffer(js.len);
  374. tmp = new DataView(buffer, length);
  375. tmp.setUint16(0,js.magic,true);
  376. length += 2;
  377. tmp = new DataView(buffer, length);
  378. tmp.setUint8(0,js.type,true);
  379. length += 1;
  380. tmp = new DataView(buffer, length);
  381. tmp.setUint8(0,js.subtype,true);
  382. length += 1;
  383. tmp = new DataView(buffer, length);
  384. tmp.setUint8(0,js.nack,true);
  385. length += 1;
  386. tmp = new DataView(buffer, length);
  387. tmp.setUint32(0,js.flag,true);
  388. length += 4;
  389. tmp = new DataView(buffer, length);
  390. tmp.setUint32(0,js.dlen,true);
  391. length += 4;
  392. return buffer;
  393. }
  394. function info_to_bin(js) {
  395. var tmp, length=0;
  396. var name = string_to_array(js.name, 32);
  397. var buffer = new ArrayBuffer(js.len-name.byteLength);
  398. tmp = new DataView(buffer, length);
  399. tmp.setUint8(0,js.type,true);
  400. length += 1;
  401. tmp = new DataView(buffer, length);
  402. tmp.setUint8(0,js.addr,true);
  403. length += 1;
  404. tmp = new DataView(buffer, length);
  405. tmp.setUint8(0,js.ch,true);
  406. length += 1;
  407. tmp = new DataView(buffer, length);
  408. tmp.setUint8(0,js.sch,true);
  409. length += 1;
  410. tmp = new DataView(buffer, length);
  411. tmp.setUint8(0,js.ph_id,true);
  412. length += 1;
  413. tmp = new DataView(buffer, length);
  414. tmp.setUint8(0,js.ph_val,true);
  415. length += 1;
  416. tmp = new DataView(buffer, length);
  417. tmp.setUint32(0,js.start_delay,true);
  418. length += 4;
  419. tmp = new DataView(buffer, length);
  420. tmp.setUint32(0,js.stop_delay,true);
  421. length += 4;
  422. return array_cat(name,buffer);
  423. }
  424. function power_to_bin(js) {
  425. var tmp, length=0;
  426. var buffer = new ArrayBuffer(js.len);
  427. tmp = new DataView(buffer, length);
  428. tmp.setFloat32(0,js.voltage,true);
  429. length += 4;
  430. tmp = new DataView(buffer, length);
  431. tmp.setFloat32(0,js.current,true);
  432. length += 4;
  433. tmp = new DataView(buffer, length);
  434. tmp.setFloat32(0,js.power,true);
  435. length += 4;
  436. tmp = new DataView(buffer, length);
  437. tmp.setFloat32(0,js.freq,true);
  438. length += 4;
  439. tmp = new DataView(buffer, length);
  440. tmp.setFloat32(0,js.factor,true);
  441. length += 4;
  442. tmp = new DataView(buffer, length);
  443. tmp.setFloat32(0,js.consump,true);
  444. length += 4;
  445. tmp = new DataView(buffer, length);
  446. tmp.setFloat32(0,js.active,true);
  447. length += 4;
  448. tmp = new DataView(buffer, length);
  449. tmp.setFloat32(0,js.reactive,true);
  450. length += 4;
  451. tmp = new DataView(buffer, length);
  452. tmp.setUint8(0,js.ph_put,true);
  453. length += 1;
  454. tmp = new DataView(buffer, length);
  455. tmp.setUint8(0,js.status,true);
  456. length += 1;
  457. tmp = new DataView(buffer, length);
  458. tmp.setUint8(0,js.nwire,true);
  459. length += 1;
  460. return buffer;
  461. }
  462. function alarm_to_bin(js) {
  463. var tmp, length=0;
  464. var buffer = new ArrayBuffer(js.len);
  465. tmp = new DataView(buffer, length);
  466. tmp.setUint8(0,js.v_upper,true);
  467. length += 1;
  468. tmp = new DataView(buffer, length);
  469. tmp.setUint8(0,js.v_lower,true);
  470. length += 1;
  471. tmp = new DataView(buffer, length);
  472. tmp.setUint8(0,js.c_upper,true);
  473. length += 1;
  474. tmp = new DataView(buffer, length);
  475. tmp.setUint8(0,js.p_upper,true);
  476. length += 1;
  477. tmp = new DataView(buffer, length);
  478. tmp.setUint8(0,js.w_upper,true);
  479. length += 1;
  480. tmp = new DataView(buffer, length);
  481. tmp.setUint8(0,js.ph_loss,true);
  482. length += 1;
  483. return buffer;
  484. }
  485. function thrval_to_js(js) {
  486. var tmp, length=0;
  487. var buffer = new ArrayBuffer(js.len);
  488. tmp = new DataView(buffer, length);
  489. tmp.setUint8(0,js.en,true);
  490. length += 1;
  491. tmp = new DataView(buffer, length);
  492. tmp.setFloat32(0,js.val,true);
  493. length += 4;
  494. tmp = new DataView(buffer, length);
  495. tmp.getUint8(0,js.act,true);
  496. length += 1;
  497. tmp = new DataView(buffer, length);
  498. for(var i=0; i<14; i++) {
  499. tmp.setUint8(i,js.act_para[i],true);
  500. }
  501. length += 14;
  502. return buffer;
  503. }
  504. function thr_to_bin(js) {
  505. var bin,tmp, length=0;
  506. bin = thrval_to_js(js.v_upper);
  507. tmp = thrval_to_js(js.v_lower);
  508. bin = array_cat(bin, tmp);
  509. tmp = thrval_to_js(js.c_upper);
  510. bin = array_cat(bin, tmp);
  511. tmp = thrval_to_js(js.p_upper);
  512. bin = array_cat(bin, tmp);
  513. tmp = thrval_to_js(js.w_upper);
  514. bin = array_cat(bin, tmp);
  515. return bin;
  516. }
  517. function powerc_to_bin(js) {
  518. var bin,tmp, length=0;
  519. bin = new info_to_bin(js.info);
  520. for(var i=0; i<3; i++) {
  521. tmp = new power_to_bin(js.power[i]);
  522. bin = array_cat(bin, tmp);
  523. }
  524. tmp = new thr_to_bin(js.thr);
  525. bin = array_cat(bin, tmp);
  526. tmp = new alarm_to_bin(js.alarm);
  527. bin = array_cat(bin, tmp);
  528. var buf = new ArrayBuffer(8);
  529. tmp = new DataView(buf, 0);
  530. tmp.setUint32(0,js.time,true);
  531. tmp.setUint32(4,js.pbrd,true);
  532. bin = array_cat(bin, buf);
  533. return bin;
  534. }
  535. function total_to_bin(js) {
  536. var tmp, length=0;
  537. var buffer = new ArrayBuffer(js.len);
  538. tmp = new DataView(buffer, length);
  539. tmp.setFloat32(0,js.voltage,true);
  540. length += 4;
  541. tmp = new DataView(buffer, length);
  542. tmp.setFloat32(0,js.current,true);
  543. length += 4;
  544. tmp = new DataView(buffer, length);
  545. tmp.setFloat32(0,js.power,true);
  546. length += 4;
  547. tmp = new DataView(buffer, length);
  548. tmp.setFloat32(0,js.freq,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.consump,true);
  555. length += 4;
  556. tmp = new DataView(buffer, length);
  557. tmp.setFloat32(0,js.factor,true);
  558. length += 4;
  559. tmp = new DataView(buffer, length);
  560. tmp.setFloat32(0,js.active,true);
  561. length += 4;
  562. tmp = new DataView(buffer, length);
  563. tmp.setFloat32(0,js.reactive,true);
  564. length += 4;
  565. return buffer;
  566. }
  567. function powert_to_bin(js) {
  568. var tmp,bin2,length=0;
  569. var bin = new ArrayBuffer(1);
  570. tmp = new DataView(bin, length);
  571. tmp.setUint8(0,js.type,true);
  572. length += 4;
  573. for(var i=0; i<3; i++) {
  574. bin2 = total_to_bin(js.total[i]);
  575. bin = array_cat(bin, bin2);
  576. }
  577. return bin;
  578. }
  579. function alarmd_to_bin(js) {
  580. var tmp, length=0;
  581. var bin,buffer = new ArrayBuffer(1);
  582. tmp = new DataView(buffer, 0);
  583. tmp.setUint8(0,js.ch,true);
  584. var alarm = alarm_to_bin(js.alarm);
  585. bin = array_cat(buffer, alarm);
  586. buffer = new ArrayBuffer(4);
  587. tmp = new DataView(buffer, 0);
  588. tmp.setUint32(0,js.time,true);
  589. return array_cat(bin, buffer);
  590. }
  591. //////////////////////////////////////////////////////////////
  592. var XXX ={};
  593. XXX.hdr = function() {
  594. this.j2b = function (js) {
  595. var tmp;
  596. switch(js.type){
  597. case PKT_TYPE.PKT_TYPE_POWER_CH:
  598. tmp = BIN.powerc.j2b(js.powerc);
  599. break;
  600. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  601. tmp = BIN.powert.j2b(js.powert);
  602. break;
  603. case PKT_TYPE.PKT_TYPE_ALARM:
  604. tmp = BIN.alarm.j2b(js.alarmd);
  605. break;
  606. default:
  607. return tmp;
  608. }
  609. js.dlen = tmp.byteLength;
  610. var hdr = new hdr_to_bin(js);
  611. return array_cat(hdr,tmp);
  612. };
  613. this.b2j = function (bin) {
  614. var tmp;
  615. var hdr = new bin_to_hdr(bin);
  616. if(hdr.nack == 1){
  617. //tmp = myBinary.ack.bin2js(bin,this.hdr.len,true);
  618. //myLog('log',tmp);
  619. //var data = tmp.data;
  620. }
  621. var data = bin.slice(hdr.len);
  622. if(data.byteLength>0 && hdr.dlen==data.byteLength) {
  623. switch(hdr.type) {
  624. case PKT_TYPE.PKT_TYPE_POWER_CH:
  625. hdr.powerc = BIN.powerc.b2j(data);
  626. break;
  627. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  628. hdr.powert = BIN.powert.b2j(data);
  629. break;
  630. case PKT_TYPE.PKT_TYPE_ALARM:
  631. hdr.alarmd = BIN.alarmd.b2j(data);
  632. break;
  633. default :
  634. console.log("wrong hdr type: "+hdr.type);
  635. }
  636. }
  637. return hdr;
  638. }
  639. };
  640. XXX.paras=function() {
  641. this.j2b = function (js) {
  642. }
  643. this.b2j = function (bin) {
  644. }
  645. };
  646. XXX.powerc=function() {
  647. this.j2b = function (js) {
  648. return new powerc_to_bin(js);
  649. }
  650. this.b2j = function (bin) {
  651. var tmp=[],length=0;
  652. for(var i=0;; i++) {
  653. tmp[i] = new bin_to_powerc(bin.slice(length));
  654. length += tmp[i].len;
  655. if(length >= bin.byteLength) {
  656. break;
  657. }
  658. }
  659. return tmp;
  660. }
  661. };
  662. XXX.powert=function() {
  663. this.j2b = function (js) {
  664. return new powert_to_bin(js);
  665. }
  666. this.b2j = function (bin) {
  667. return new bin_to_powert(bin);
  668. }
  669. };
  670. XXX.alarmd=function() {
  671. this.j2b = function (js) {
  672. return new alarmd_to_bin(js);
  673. }
  674. this.b2j = function (bin) {
  675. return new bin_to_alarmd(bin);
  676. }
  677. };
  678. var BIN=new function() {
  679. this.hdr = new XXX.hdr();
  680. this.paras = new XXX.paras();
  681. this.powerc = new XXX.powerc();
  682. this.powert = new XXX.powert();
  683. this.alarmd = new XXX.alarmd();
  684. }();
  685. //////////////////////////////////////////
  686. function LOG(){
  687. var data = [];
  688. for(var i= 1,len=arguments.length;i<len;i++){
  689. data.push(arguments[i])
  690. }
  691. console.log(data);
  692. }
  693. function WS(url,fn) {
  694. this.url = url;
  695. this.name = 'myWebs';
  696. this.open = _open;
  697. this.reopen = _reopen;
  698. this.close = _close;
  699. this.send = _send;
  700. this.state = _state;
  701. this.setcb = _setcb;
  702. this.closeCount = 0;
  703. this.maxConnect = 100;
  704. var his = this;
  705. this.open();
  706. function _open() {
  707. console.log("____url: ", his.url);
  708. his.ws = new WebSocket(his.url);
  709. his.ws.binaryType = 'arraybuffer';
  710. his.ws.onopen = _onopen;
  711. his.ws.onclose = _onclose;
  712. his.ws.onmessage = _onmsg;
  713. his.ws.onerror = _onerror;
  714. }
  715. function _reopen() {
  716. _close();
  717. _open(his.url);
  718. }
  719. function _close() {
  720. his.ws.close();
  721. }
  722. function _send(msg) {
  723. if (his.ws.readyState != WebSocket.OPEN)
  724. return;
  725. his.ws.send(msg);
  726. }
  727. function _state() {
  728. return his.ws.readyState;
  729. }
  730. function _setcb(tp, f) {
  731. switch (tp) {
  732. case "open":
  733. his.ws.onopen = f;
  734. break;
  735. case "close":
  736. his.ws.onclose = f;
  737. break;
  738. case "msg":
  739. his.ws.onmessage = f;
  740. break;
  741. case "err":
  742. his.ws.onerror = f;
  743. break;
  744. }
  745. }
  746. function loop_send() {
  747. console.log("PKT_TYPE_POWER_CH request");
  748. _send(mk_hdr(PKT_TYPE.PKT_TYPE_POWER_CH));
  749. setTimeout(loop_send, 2000);
  750. }
  751. function _onopen(evt) {
  752. his.closeCount = 0;
  753. console.log("___ ws.onopen");
  754. setTimeout(loop_send, 2000);
  755. }
  756. function _onclose(evt) {
  757. LOG("ws.onclose");
  758. setTimeout(_reopen, 3000);
  759. his.closeCount += 1;
  760. }
  761. function _onmsg(evt) {
  762. console.log("___ ws.onmsg");
  763. if (evt.data instanceof ArrayBuffer) {
  764. fn(evt.data);
  765. }
  766. }
  767. function _onerror(evt) {
  768. console.log("___ ws.onerror");
  769. //write_scr('<span style="color: red;">ERROR:</span> '+ evt.data);
  770. }
  771. }