data.js 23 KB

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