data.js 23 KB

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