data.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. SUB_TYPE_PARAS_MODBUS:len++,
  31. SUB_TYPE_PARAS_MISC:len++,
  32. SUB_TYPE_PARAS_PROD:len++,
  33. SUB_TYPE_PARAS_SERV:len++,
  34. SUB_TYPE_PARAS_NETWORK:len++,
  35. }
  36. }();
  37. const PDU_TYPE=function(){
  38. var len = 0;
  39. return {
  40. PDU_AC_I1O1:len++,
  41. PDU_DC_I1O1:len+=2,
  42. PDU_AC_I3O3:len++,
  43. PDU_AC_I3O1:len++,
  44. PDU_AC_I3O1_H:len++,
  45. }
  46. }();
  47. const ALARM_ACT=function(){
  48. var len = 1;
  49. return {
  50. ALARM_ACT_CLOSE_CH:len++,
  51. ALARM_ACT_MESG:len++,
  52. ALARM_ACT_CLOSE_CHALL:len++,
  53. }
  54. }();
  55. const IP_MODE=function(){
  56. var len = 0;
  57. return {
  58. IP_MODE_DHCP:len++,
  59. IP_MODE_STATIC:len++,
  60. }
  61. }();
  62. const IP_VER=function(){
  63. var len = 0;
  64. return {
  65. IP_VER_ERR:len++,
  66. IP_VER_V4:len++,
  67. IP_VER_V6:len++,
  68. }
  69. }();
  70. const MODBUS_MODE=function(){
  71. var len = 0;
  72. return {
  73. MODBUS_MASTER:len++,
  74. MODBUS_SLAVE:len++,
  75. }
  76. }();
  77. const LANG_ID=function(){
  78. var len = 0;
  79. return {
  80. LANG_ID_CHINESE:len++,
  81. LANG_ID_ENGLISH:len++,
  82. LANG_ID_RUSSIAN:len++,
  83. LANG_ID_SPANISH:len++,
  84. LANG_ID_PORTYGYESE:len++,
  85. LANG_ID_GERMAN:len++,
  86. LANG_ID_FRENCH:len++,
  87. LANG_ID_ITALIAN:len++,
  88. LANG_ID_JAPANESE:len++,
  89. LANG_ID_MAX:len++,
  90. }
  91. }();
  92. ///////////////////////////////////////////////////////
  93. function array_to_string(data){
  94. const decoder = new TextDecoder();
  95. return decoder.decode(new Uint8Array(data));
  96. }
  97. function array_set(array,str,offset){
  98. const encoder = new TextEncoder(); // ĬÈÏ'utf-8'±àÂë
  99. const buffer = encoder.encode(str);
  100. array.set(buffer);
  101. array.fill(offset, buffer.length);
  102. }
  103. function string_to_array(data,len){
  104. const encoder = new TextEncoder(); // ĬÈÏ'utf-8'±àÂë
  105. const buffer = encoder.encode(data);
  106. const fixedArray = new Uint8Array(len);
  107. fixedArray.set(buffer);
  108. fixedArray.fill(0, buffer.length);
  109. return fixedArray.buffer;
  110. }
  111. function array_cat(a1,a2){
  112. var tlen=a1.byteLength+a2.byteLength;
  113. var buffer = new ArrayBuffer(tlen);
  114. var vw = new Uint8Array(buffer);
  115. var vw1 = new Uint8Array(a1);
  116. for(var i=0; i<a1.byteLength; i++) {
  117. vw[i] = vw1[i];
  118. }
  119. var vw2 = new Uint8Array(a2);
  120. for(var i=0; i<a2.byteLength; i++) {
  121. vw[i+a1.byteLength] = vw2[i];
  122. }
  123. return buffer;
  124. }
  125. function mk_hdr_bin(addr,type,subtype,nack,flag,dlen)
  126. {
  127. var tmp,length=0;
  128. var buffer = new ArrayBuffer(14);
  129. tmp = new DataView(buffer, length);
  130. var t = MAGIC;
  131. tmp.setUint16(0,t,true); //magic
  132. length += 2;
  133. tmp = new DataView(buffer, length);
  134. tmp.setUint8(0,addr,true); //addr
  135. length += 1;
  136. tmp = new DataView(buffer, length);
  137. tmp.setUint8(0,type,true); //type
  138. length += 1;
  139. tmp = new DataView(buffer, length);
  140. tmp.setUint8(0,0,true); //subtype
  141. length += 1;
  142. tmp = new DataView(buffer, length);
  143. tmp.setUint8(0,0,true); //nack
  144. length += 1;
  145. tmp = new DataView(buffer, length);
  146. tmp.setUint32(0,flag,true); //flag
  147. length += 4;
  148. tmp = new DataView(buffer, length);
  149. tmp.setUint32(0,dlen,true); //dlen
  150. length += 4;
  151. return buffer;
  152. }
  153. function mk_hdr_obj(addr,type,subtype,nack,flag,dlen)
  154. {
  155. this.magic = MAGIC;
  156. this.addr = addr;
  157. this.type = type;
  158. this.subtype = subtype;
  159. this.flag = flag;
  160. this.dlen = dlen;
  161. }
  162. function pkt_to_bin(addr,type,subtype,nack,flag,js)
  163. {
  164. var tmp;
  165. var buffer = new mk_hdr_bin(addr,type,subtype,nack,flag,js.len);
  166. switch(type){
  167. case PKT_TYPE.PKT_TYPE_POWER_CH:
  168. tmp = new powerc_to_bin(js);
  169. break;
  170. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  171. tmp = new powert_to_bin(js);
  172. break;
  173. case PKT_TYPE.PKT_TYPE_ALARM:
  174. tmp = new alarm_to_bin(js);
  175. break;
  176. default:
  177. return tmp;
  178. }
  179. return array_cat(buffer,tmp);
  180. }
  181. function bin_to_pkt(bin)
  182. {
  183. var hdr = new bin_to_hdr(bin);
  184. var data = bin.slice(hdr.len);
  185. if(data.byteLength>0) {
  186. switch(hdr.type) {
  187. case PKT_TYPE.PKT_TYPE_POWER_CH:
  188. hdr.powerc = new bin_to_powerc(data);
  189. break;
  190. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  191. hdr.powert = BIN.powert.b2j(data);
  192. break;
  193. case PKT_TYPE.PKT_TYPE_ALARM:
  194. hdr.alarmd = BIN.alarmd.b2j(data);
  195. break;
  196. case PKT_TYPE.PKT_TYPE_ALARM:
  197. hdr.alarmd = BIN.alarmd.b2j(data);
  198. break;
  199. case PKT_TYPE.PKT_TYPE_SLAVE_LIST:
  200. hdr.slaves = BIN.alarmd.b2j(data);
  201. break;
  202. default:
  203. console.log("wrong hdr type: "+hdr.type);
  204. }
  205. }
  206. return hdr;
  207. }
  208. ///////////////////////////////////////////////////
  209. function bin_to_hdr(bin) {
  210. var tmp, length=0;
  211. tmp = new DataView(bin, length);
  212. this.magic = tmp.getUint16(0,true);
  213. length += 2;
  214. tmp = new DataView(bin, length);
  215. this.addr = tmp.getUint8(0,true);
  216. length += 1;
  217. tmp = new DataView(bin, length);
  218. this.type = tmp.getUint8(0,true);
  219. length += 1;
  220. tmp = new DataView(bin, length);
  221. this.subtype = tmp.getUint8(0,true);
  222. length += 1;
  223. tmp = new DataView(bin, length);
  224. this.nack = tmp.getUint8(0,true);
  225. length += 1;
  226. tmp = new DataView(bin, length);
  227. this.flag = tmp.getUint8(0,true);
  228. length += 4;
  229. tmp = new DataView(bin, length);
  230. this.dlen = tmp.getUint32(0,true);
  231. length += 4;
  232. this.len = length;
  233. }
  234. function bin_to_info(bin) {
  235. var tmp, length=0;
  236. this.name = array_to_string(bin.slice(length,32));
  237. length += 32;
  238. tmp = new DataView(bin, length);
  239. this.type = tmp.getUint8(0,true);
  240. length += 1;
  241. tmp = new DataView(bin, length);
  242. this.addr = tmp.getUint8(0,true);
  243. length += 1;
  244. tmp = new DataView(bin, length);
  245. this.ch = tmp.getUint8(0,true);
  246. length += 1;
  247. tmp = new DataView(bin, length);
  248. this.sch = tmp.getUint8(0,true);
  249. length += 1;
  250. tmp = new DataView(bin, length);
  251. this.ph_id = tmp.getUint8(0,true);
  252. length += 1;
  253. tmp = new DataView(bin, length);
  254. this.ph_val = tmp.getUint8(0,true);
  255. length += 1;
  256. tmp = new DataView(bin, length);
  257. this.start_delay = tmp.getUint32(0,true);
  258. length += 4;
  259. tmp = new DataView(bin, length);
  260. this.stop_delay = tmp.getUint32(0,true);
  261. length += 4;
  262. this.len = length;
  263. }
  264. function bin_to_power(bin) {
  265. var tmp, length=0;
  266. tmp = new DataView(bin, length);
  267. this.voltage = tmp.getFloat32(0,true);
  268. length += 4;
  269. tmp = new DataView(bin, length);
  270. this.current = tmp.getFloat32(0,true);
  271. length += 4;
  272. tmp = new DataView(bin, length);
  273. this.power = tmp.getFloat32(0,true);
  274. length += 4;
  275. tmp = new DataView(bin, length);
  276. this.freq = tmp.getFloat32(0,true);
  277. length += 4;
  278. tmp = new DataView(bin, length);
  279. this.factor = tmp.getFloat32(0,true);
  280. length += 4;
  281. tmp = new DataView(bin, length);
  282. this.consump = tmp.getFloat32(0,true);
  283. length += 4;
  284. tmp = new DataView(bin, length);
  285. this.active = tmp.getFloat32(0,true);
  286. length += 4;
  287. tmp = new DataView(bin, length);
  288. this.reactive = tmp.getFloat32(0,true);
  289. length += 4;
  290. tmp = new DataView(bin, length);
  291. this.ph_out = tmp.getUint8(0,true);
  292. length += 1;
  293. tmp = new DataView(bin, length);
  294. this.status = tmp.getUint8(0,true);
  295. length += 1;
  296. tmp = new DataView(bin, length);
  297. this.nwire = tmp.getUint8(0,true);
  298. length += 1;
  299. this.len = length;
  300. }
  301. function bin_to_alarm(bin) {
  302. var tmp, length=0;
  303. tmp = new DataView(bin, length);
  304. this.v_upper = tmp.getUint8(0,true);
  305. length += 1;
  306. tmp = new DataView(bin, length);
  307. this.v_lower = tmp.getUint8(0,true);
  308. length += 1;
  309. tmp = new DataView(bin, length);
  310. this.c_upper = tmp.getUint8(0,true);
  311. length += 1;
  312. tmp = new DataView(bin, length);
  313. this.p_upper = tmp.getUint8(0,true);
  314. length += 1;
  315. tmp = new DataView(bin, length);
  316. this.w_upper = tmp.getUint8(0,true);
  317. length += 1;
  318. tmp = new DataView(bin, length);
  319. this.ph_loss = tmp.getUint8(0,true);
  320. length += 1;
  321. this.len = length;
  322. }
  323. function bin_to_thrval(bin) {
  324. var tmp, length=0;
  325. tmp = new DataView(bin, length);
  326. this.en = tmp.getUint8(0,true);
  327. length += 1;
  328. tmp = new DataView(bin, length);
  329. this.val = tmp.getFloat32(0,true);
  330. length += 4;
  331. tmp = new DataView(bin, length);
  332. this.act = tmp.getUint8(0,true);
  333. length += 1;
  334. this.act_para = [];
  335. tmp = new DataView(bin, length);
  336. for(var i=0; i<14; i++) {
  337. this.act_para[i] = tmp.getUint8(i,true);
  338. }
  339. length += 14;
  340. this.len = length;
  341. }
  342. function bin_to_thr(bin) {
  343. var tmp, length=0;
  344. this.v_upper = new bin_to_thrval(bin.slice(length));
  345. length += this.v_upper.len;
  346. this.v_lower = new bin_to_thrval(bin.slice(length));
  347. length += this.v_lower.len;
  348. this.c_upper = new bin_to_thrval(bin.slice(length));
  349. length += this.c_upper.len;
  350. this.p_upper = new bin_to_thrval(bin.slice(length));
  351. length += this.p_upper.len;
  352. this.w_upper = new bin_to_thrval(bin.slice(length));
  353. length += this.w_upper.len;
  354. this.len = length;
  355. }
  356. ////////////////////////////////
  357. function bin_to_ver(bin) {
  358. var tmp, length=0;
  359. this.ver = array_to_string(bin.slice(length,16));
  360. length += 16;
  361. this.addr = array_to_string(bin.slice(length,32));
  362. length += 32;
  363. this.len = length;
  364. }
  365. function bin_to_modbus(bin) {
  366. var tmp, length=0;
  367. tmp = new DataView(bin, length);
  368. this.mode = tmp.getUint8(0,true);
  369. length += 1;
  370. tmp = new DataView(bin, length);
  371. this.addr = tmp.getUint8(0,true);
  372. length += 1;
  373. tmp = new DataView(bin, length);
  374. this.baud = tmp.getUint32(0,true);
  375. length += 4;
  376. this.len = length;
  377. }
  378. function bin_to_misc(bin) {
  379. var tmp, length=0;
  380. tmp = new DataView(bin, length);
  381. this.watchdog = tmp.getUint8(0,true);
  382. length += 1;
  383. tmp = new DataView(bin, length);
  384. this.fliptime = tmp.getUint32(0,true);
  385. length += 4;
  386. tmp = new DataView(bin, length);
  387. this.led = tmp.getUint8(0,true);
  388. length += 1;
  389. tmp = new DataView(bin, length);
  390. this.beep = tmp.getUint8(0,true);
  391. length += 1;
  392. tmp = new DataView(bin, length);
  393. this.rotate = tmp.getUint8(0,true);
  394. length += 1;
  395. this.len = length;
  396. }
  397. function bin_to_prod(bin) {
  398. var tmp, length=0;
  399. tmp = new DataView(bin, length);
  400. this.id = tmp.getUint8(0,true);
  401. length += 1;
  402. this.pname = array_to_string(bin.slice(length,32));
  403. length += 32;
  404. this.pnumber = array_to_string(bin.slice(length,32));
  405. length += 32;
  406. this.status = array_to_string(bin.slice(length,32));
  407. length += 32;
  408. this.name = array_to_string(bin.slice(length,32));
  409. length += 32;
  410. this.number = array_to_string(bin.slice(length,32));
  411. length += 32;
  412. this.date = array_to_string(bin.slice(length,32));
  413. length += 32;
  414. this.batch = array_to_string(bin.slice(length,32));
  415. length += 32;
  416. tmp = new DataView(bin, length);
  417. this.type = tmp.getUint8(0,true);
  418. length += 1;
  419. tmp = new DataView(bin, length);
  420. this.pwr_type = tmp.getUint8(0,true);
  421. length += 1;
  422. this.version = array_to_string(bin.slice(length,8));
  423. length += 8;
  424. tmp = new DataView(bin, length);
  425. this.language = tmp.getUint8(0,true);
  426. length += 1;
  427. tmp = new DataView(bin, length);
  428. this.samptime = tmp.getUint32(0,true);
  429. length += 4;
  430. tmp = new DataView(bin, length);
  431. this.savetime = tmp.getUint32(0,true);
  432. length += 4;
  433. tmp = new DataView(bin, length);
  434. this.ph_group = tmp.getUint16(0,true);
  435. length += 2;
  436. tmp = new DataView(bin, length);
  437. this.ch_delay = tmp.getUint16(0,true);
  438. length += 2;
  439. this.len = length;
  440. }
  441. function bin_to_serv(bin) {
  442. var tmp, length=0;
  443. this.name = array_to_string(bin.slice(length,256));
  444. length += 256;
  445. this.site = array_to_string(bin.slice(length,256));
  446. length += 256;
  447. this.mail = array_to_string(bin.slice(length,256));
  448. length += 256;
  449. this.addr = array_to_string(bin.slice(length,256));
  450. length += 256;
  451. this.tele = array_to_string(bin.slice(length,32));
  452. length += 32;
  453. this.len = length;
  454. }
  455. function bin_to_network(bin) {
  456. var tmp, length=0;
  457. tmp = new DataView(bin, length);
  458. this.mode = tmp.getUint8(0,true);
  459. length += 1;
  460. this.ipaddr = array_to_string(bin.slice(length,48));
  461. length += 48;
  462. this.mask = array_to_string(bin.slice(length,48));
  463. length += 48;
  464. this.gateway = array_to_string(bin.slice(length,48));
  465. length += 48;
  466. this.dns1 = array_to_string(bin.slice(length,48));
  467. length += 48;
  468. this.dns2 = array_to_string(bin.slice(length,48));
  469. length += 48;
  470. this.len = length;
  471. }
  472. ////////////////////////////////
  473. function bin_to_powerc(bin) {
  474. var tmp, bin2, length=0;
  475. this.info = new bin_to_info(bin);
  476. length += this.info.len;
  477. this.power = [];
  478. for(var i=0; i<3; i++) {
  479. this.power[i] = new bin_to_power(bin.slice(length));
  480. length += this.power[i].len;
  481. }
  482. this.thr = new bin_to_thr(bin.slice(length));
  483. length += this.thr.len;
  484. this.alarm = new bin_to_alarm(bin.slice(length));
  485. length += this.alarm.len;
  486. tmp = new DataView(bin, length);
  487. this.time = tmp.getUint32(0,true);
  488. length += 4;
  489. tmp = new DataView(bin, length);
  490. this.pbrd = tmp.getUint32(0,true);
  491. length += 4;
  492. this.len = length;
  493. }
  494. function bin_to_total(bin) {
  495. var tmp, length=0;
  496. tmp = new DataView(bin, length);
  497. this.voltage = tmp.getFloat32 (0,true);
  498. length += 4;
  499. tmp = new DataView(bin, length);
  500. this.current = tmp.getFloat32(0,true);
  501. length += 4;
  502. tmp = new DataView(bin, length);
  503. this.power = tmp.getFloat32(0,true);
  504. length += 4;
  505. tmp = new DataView(bin, length);
  506. this.freq = tmp.getFloat32(0,true);
  507. length += 4;
  508. tmp = new DataView(bin, length);
  509. this.factor = tmp.getFloat32(0,true);
  510. length += 4;
  511. tmp = new DataView(bin, length);
  512. this.consump = tmp.getFloat32(0,true);
  513. length += 4;
  514. tmp = new DataView(bin, length);
  515. this.active = tmp.getFloat32(0,true);
  516. length += 4;
  517. tmp = new DataView(bin, length);
  518. this.reactive = tmp.getFloat32(0,true);
  519. length += 4;
  520. this.len = length;
  521. }
  522. function bin_to_powert(bin) {
  523. var tmp, length=0;
  524. tmp = new DataView(bin, length);
  525. this.type = tmp.getUint8(0,true);
  526. length += 1;
  527. this.total = [];
  528. for(var i=0; i<3; i++) {
  529. this.total[i] = new bin_to_total(bin.slice(length));
  530. length += this.total[i].len;
  531. }
  532. this.len = length;
  533. }
  534. function bin_to_alarmd(bin) {
  535. var tmp, length=0;
  536. tmp = new DataView(bin, length);
  537. this.ch = tmp.getUint8(0,true);
  538. length += 1;
  539. this.alarm = new bin_to_alarm(bin.slice(length));
  540. length += this.alarm.len;
  541. tmp = new DataView(bin, length);
  542. this.time = tmp.getUint32(0,true);
  543. length += 4;
  544. this.len = length;
  545. }
  546. function bin_to_paras(bin) {
  547. var tmp, length=0;
  548. tmp = new DataView(bin, length);
  549. this.lang = tmp.getUint8(0,true);
  550. length += 1;
  551. this.ver = new bin_to_ver(bin.slice(length));
  552. length += this.ver.len;
  553. this.cas = new bin_to_modbus(bin.slice(length));
  554. length += this.cas.len;
  555. this.misc = new bin_to_misc(bin.slice(length));
  556. length += this.misc.len;
  557. this.prod = new bin_to_prod(bin.slice(length));
  558. length += this.prod.len;
  559. this.serv = new bin_to_serv(bin.slice(length));
  560. length += this.serv.len;
  561. this.network = new bin_to_network(bin.slice(length));
  562. length += this.network.len;
  563. this.len = length;
  564. }
  565. function bin_to_slist(bin) {
  566. var tmp, length=0;
  567. tmp = new DataView(bin, length);
  568. this.cnt = tmp.getUint8(0,true);
  569. length += 1;
  570. this.addr = [];
  571. tmp = new DataView(bin, length);
  572. for(var i=0; i<this.cnt; i++) {
  573. this.addr[i] = tmp.getUint8(i,true);
  574. }
  575. length += this.cnt;
  576. this.len = length;
  577. }
  578. ///////////////////////////////////////////////////
  579. function hdr_to_bin(js) {
  580. var tmp, length=0;
  581. var buffer = new ArrayBuffer(js.len);
  582. tmp = new DataView(buffer, length);
  583. tmp.setUint16(0,js.magic,true);
  584. length += 2;
  585. tmp = new DataView(buffer, length);
  586. tmp.setUint8(0,js.addr,true);
  587. length += 1;
  588. tmp = new DataView(buffer, length);
  589. tmp.setUint8(0,js.type,true);
  590. length += 1;
  591. tmp = new DataView(buffer, length);
  592. tmp.setUint8(0,js.subtype,true);
  593. length += 1;
  594. tmp = new DataView(buffer, length);
  595. tmp.setUint8(0,js.nack,true);
  596. length += 1;
  597. tmp = new DataView(buffer, length);
  598. tmp.setUint32(0,js.flag,true);
  599. length += 4;
  600. tmp = new DataView(buffer, length);
  601. tmp.setUint32(0,js.dlen,true);
  602. length += 4;
  603. return buffer;
  604. }
  605. function info_to_bin(js) {
  606. var tmp, length=0;
  607. var name = string_to_array(js.name, 32);
  608. var buffer = new ArrayBuffer(js.len-name.byteLength);
  609. tmp = new DataView(buffer, length);
  610. tmp.setUint8(0,js.type,true);
  611. length += 1;
  612. tmp = new DataView(buffer, length);
  613. tmp.setUint8(0,js.addr,true);
  614. length += 1;
  615. tmp = new DataView(buffer, length);
  616. tmp.setUint8(0,js.ch,true);
  617. length += 1;
  618. tmp = new DataView(buffer, length);
  619. tmp.setUint8(0,js.sch,true);
  620. length += 1;
  621. tmp = new DataView(buffer, length);
  622. tmp.setUint8(0,js.ph_id,true);
  623. length += 1;
  624. tmp = new DataView(buffer, length);
  625. tmp.setUint8(0,js.ph_val,true);
  626. length += 1;
  627. tmp = new DataView(buffer, length);
  628. tmp.setUint32(0,js.start_delay,true);
  629. length += 4;
  630. tmp = new DataView(buffer, length);
  631. tmp.setUint32(0,js.stop_delay,true);
  632. length += 4;
  633. return array_cat(name,buffer);
  634. }
  635. function power_to_bin(js) {
  636. var tmp, length=0;
  637. var buffer = new ArrayBuffer(js.len);
  638. tmp = new DataView(buffer, length);
  639. tmp.setFloat32(0,js.voltage,true);
  640. length += 4;
  641. tmp = new DataView(buffer, length);
  642. tmp.setFloat32(0,js.current,true);
  643. length += 4;
  644. tmp = new DataView(buffer, length);
  645. tmp.setFloat32(0,js.power,true);
  646. length += 4;
  647. tmp = new DataView(buffer, length);
  648. tmp.setFloat32(0,js.freq,true);
  649. length += 4;
  650. tmp = new DataView(buffer, length);
  651. tmp.setFloat32(0,js.factor,true);
  652. length += 4;
  653. tmp = new DataView(buffer, length);
  654. tmp.setFloat32(0,js.consump,true);
  655. length += 4;
  656. tmp = new DataView(buffer, length);
  657. tmp.setFloat32(0,js.active,true);
  658. length += 4;
  659. tmp = new DataView(buffer, length);
  660. tmp.setFloat32(0,js.reactive,true);
  661. length += 4;
  662. tmp = new DataView(buffer, length);
  663. tmp.setUint8(0,js.ph_put,true);
  664. length += 1;
  665. tmp = new DataView(buffer, length);
  666. tmp.setUint8(0,js.status,true);
  667. length += 1;
  668. tmp = new DataView(buffer, length);
  669. tmp.setUint8(0,js.nwire,true);
  670. length += 1;
  671. return buffer;
  672. }
  673. function alarm_to_bin(js) {
  674. var tmp, length=0;
  675. var buffer = new ArrayBuffer(js.len);
  676. tmp = new DataView(buffer, length);
  677. tmp.setUint8(0,js.v_upper,true);
  678. length += 1;
  679. tmp = new DataView(buffer, length);
  680. tmp.setUint8(0,js.v_lower,true);
  681. length += 1;
  682. tmp = new DataView(buffer, length);
  683. tmp.setUint8(0,js.c_upper,true);
  684. length += 1;
  685. tmp = new DataView(buffer, length);
  686. tmp.setUint8(0,js.p_upper,true);
  687. length += 1;
  688. tmp = new DataView(buffer, length);
  689. tmp.setUint8(0,js.w_upper,true);
  690. length += 1;
  691. tmp = new DataView(buffer, length);
  692. tmp.setUint8(0,js.ph_loss,true);
  693. length += 1;
  694. return buffer;
  695. }
  696. function thrval_to_bin(js) {
  697. var tmp, length=0;
  698. var buffer = new ArrayBuffer(js.len);
  699. tmp = new DataView(buffer, length);
  700. tmp.setUint8(0,js.en,true);
  701. length += 1;
  702. tmp = new DataView(buffer, length);
  703. tmp.setFloat32(0,js.val,true);
  704. length += 4;
  705. tmp = new DataView(buffer, length);
  706. tmp.getUint8(0,js.act,true);
  707. length += 1;
  708. tmp = new DataView(buffer, length);
  709. for(var i=0; i<14; i++) {
  710. tmp.setUint8(i,js.act_para[i],true);
  711. }
  712. length += 14;
  713. return buffer;
  714. }
  715. function thr_to_bin(js) {
  716. var bin,tmp, length=0;
  717. bin = thrval_to_bin(js.v_upper);
  718. tmp = thrval_to_bin(js.v_lower);
  719. bin = array_cat(bin, tmp);
  720. tmp = thrval_to_bin(js.c_upper);
  721. bin = array_cat(bin, tmp);
  722. tmp = thrval_to_bin(js.p_upper);
  723. bin = array_cat(bin, tmp);
  724. tmp = thrval_to_bin(js.w_upper);
  725. bin = array_cat(bin, tmp);
  726. return bin;
  727. }
  728. ////////////////////////////////
  729. function modbus_to_bin(js) {
  730. var tmp, length=0;
  731. var buffer = new ArrayBuffer(js.len);
  732. tmp = new DataView(buffer, length);
  733. tmp.setUint8(0,js.mode,true);
  734. length += 1;
  735. tmp = new DataView(buffer, length);
  736. tmp.setUint8(0,js.addr,true);
  737. length += 1;
  738. tmp = new DataView(buffer, length);
  739. tmp.setUint32(0,js.baud,true);
  740. length += 4;
  741. return buffer;
  742. }
  743. function misc_to_bin(js) {
  744. var tmp, length=0;
  745. var buffer = new ArrayBuffer(js.len);
  746. tmp = new DataView(buffer, length);
  747. tmp.setUint8(0,js.watchdog,true);
  748. length += 1;
  749. tmp = new DataView(buffer, length);
  750. tmp.setUint8(0,js.fliptime,true);
  751. length += 1;
  752. tmp = new DataView(buffer, length);
  753. tmp.setUint8(0,js.led,true);
  754. length += 1;
  755. tmp = new DataView(buffer, length);
  756. tmp.setUint8(0,js.beep,true);
  757. length += 1;
  758. tmp = new DataView(buffer, length);
  759. tmp.setUint8(0,js.rotate,true);
  760. length += 1;
  761. return buffer;
  762. }
  763. function prod_to_bin(js) {
  764. var tmp, length=0;
  765. var buffer = new ArrayBuffer(js.len);
  766. tmp = new DataView(buffer, length);
  767. tmp.setUint32(0,js.id,true);
  768. length += 4;
  769. array_set(buffer,js.pname,length);
  770. length += 32;
  771. array_set(buffer,js.pnumber,length);
  772. length += 32;
  773. array_set(buffer,js.status,length);
  774. length += 32;
  775. array_set(buffer,js.name,length);
  776. length += 32;
  777. array_set(buffer,js.number,length);
  778. length += 32;
  779. array_set(buffer,js.date,length);
  780. length += 32;
  781. array_set(buffer,js.batch,length);
  782. length += 32;
  783. tmp = new DataView(buffer, length);
  784. tmp.setUint8(0,js.type,true);
  785. length += 1;
  786. tmp = new DataView(buffer, length);
  787. tmp.setUint8(0,js.pwrtype,true);
  788. length += 1;
  789. array_set(buffer,js.version,length);
  790. length += 8;
  791. tmp = new DataView(buffer, length);
  792. tmp.setUint8(0,js.language,true);
  793. length += 1;
  794. tmp = new DataView(buffer, length);
  795. tmp.setUint8(0,js.count,true);
  796. length += 1;
  797. tmp = new DataView(buffer, length);
  798. tmp.setUint32(0,js.samptime,true);
  799. length += 4;
  800. tmp = new DataView(buffer, length);
  801. tmp.setUint32(0,js.savetime,true);
  802. length += 4;
  803. tmp = new DataView(buffer, length);
  804. tmp.setUint16(0,js.ph_group,true);
  805. length += 2;
  806. tmp = new DataView(buffer, length);
  807. tmp.setUint16(0,js.ch_delay,true);
  808. length += 2;
  809. return buffer;
  810. }
  811. function serv_to_bin(js) {
  812. var tmp, length=0;
  813. var buffer = new ArrayBuffer(js.len);
  814. array_set(buffer,js.name,length);
  815. length += 256;
  816. array_set(buffer,js.site,length);
  817. length += 256;
  818. array_set(buffer,js.mail,length);
  819. length += 256;
  820. array_set(buffer,js.addr,length);
  821. length += 256;
  822. array_set(buffer,js.tele,length);
  823. length += 32;
  824. return buffer;
  825. }
  826. function network_to_bin(js) {
  827. var tmp, length=0;
  828. var buffer = new ArrayBuffer(js.len);
  829. tmp = new DataView(buffer, length);
  830. tmp.setUint8(0,js.mode,true);
  831. length += 1;
  832. array_set(buffer,js.ipaddr,length);
  833. length += 48;
  834. array_set(buffer,js.mask,length);
  835. length += 48;
  836. array_set(buffer,js.gateway,length);
  837. length += 48;
  838. array_set(buffer,js.dns1,length);
  839. length += 48;
  840. array_set(buffer,js.dns2,length);
  841. length += 48;
  842. return buffer;
  843. }
  844. ////////////////////////////////
  845. function powerc_to_bin(js) {
  846. var bin,tmp, length=0;
  847. bin = new info_to_bin(js.info);
  848. for(var i=0; i<3; i++) {
  849. tmp = new power_to_bin(js.power[i]);
  850. bin = array_cat(bin, tmp);
  851. }
  852. tmp = new thr_to_bin(js.thr);
  853. bin = array_cat(bin, tmp);
  854. tmp = new alarm_to_bin(js.alarm);
  855. bin = array_cat(bin, tmp);
  856. var buf = new ArrayBuffer(8);
  857. tmp = new DataView(buf, 0);
  858. tmp.setUint32(0,js.time,true);
  859. tmp.setUint32(4,js.pbrd,true);
  860. bin = array_cat(bin, buf);
  861. return bin;
  862. }
  863. function total_to_bin(js) {
  864. var tmp, length=0;
  865. var buffer = new ArrayBuffer(js.len);
  866. tmp = new DataView(buffer, length);
  867. tmp.setFloat32(0,js.voltage,true);
  868. length += 4;
  869. tmp = new DataView(buffer, length);
  870. tmp.setFloat32(0,js.current,true);
  871. length += 4;
  872. tmp = new DataView(buffer, length);
  873. tmp.setFloat32(0,js.power,true);
  874. length += 4;
  875. tmp = new DataView(buffer, length);
  876. tmp.setFloat32(0,js.freq,true);
  877. length += 4;
  878. tmp = new DataView(buffer, length);
  879. tmp.setFloat32(0,js.factor,true);
  880. length += 4;
  881. tmp = new DataView(buffer, length);
  882. tmp.setFloat32(0,js.consump,true);
  883. length += 4;
  884. tmp = new DataView(buffer, length);
  885. tmp.setFloat32(0,js.factor,true);
  886. length += 4;
  887. tmp = new DataView(buffer, length);
  888. tmp.setFloat32(0,js.active,true);
  889. length += 4;
  890. tmp = new DataView(buffer, length);
  891. tmp.setFloat32(0,js.reactive,true);
  892. length += 4;
  893. return buffer;
  894. }
  895. function powert_to_bin(js) {
  896. var tmp,bin2,length=0;
  897. var bin = new ArrayBuffer(1);
  898. tmp = new DataView(bin, length);
  899. tmp.setUint8(0,js.type,true);
  900. length += 4;
  901. for(var i=0; i<3; i++) {
  902. bin2 = total_to_bin(js.total[i]);
  903. bin = array_cat(bin, bin2);
  904. }
  905. return bin;
  906. }
  907. function alarmd_to_bin(js) {
  908. var tmp, length=0;
  909. var bin,buffer = new ArrayBuffer(1);
  910. tmp = new DataView(buffer, 0);
  911. tmp.setUint8(0,js.ch,true);
  912. var alarm = alarm_to_bin(js.alarm);
  913. bin = array_cat(buffer, alarm);
  914. buffer = new ArrayBuffer(4);
  915. tmp = new DataView(buffer, 0);
  916. tmp.setUint32(0,js.time,true);
  917. return array_cat(bin, buffer);
  918. }
  919. //////////////////////////////////////////////////////////////
  920. function bin_parse(bin)
  921. {
  922. var tmp,h = new bin_to_hdr(bin);
  923. if(h.nack == 1){
  924. //tmp = myBinary.ack.bin2js(bin,this.h.len,true);
  925. //myLog('log',tmp);
  926. //var data = tmp.data;
  927. }
  928. var data = bin.slice(h.len);
  929. if(data.byteLength>0 && h.dlen==data.byteLength) {
  930. switch(h.type) {
  931. case PKT_TYPE.PKT_TYPE_POWER_CH:
  932. {
  933. var tmp=[],length=0;
  934. for(var i=0;; i++) {
  935. tmp[i] = new bin_to_powerc(data.slice(length));
  936. length += tmp[i].len;
  937. if(length >= data.byteLength) {
  938. break;
  939. }
  940. }
  941. h.powerc = tmp;
  942. }
  943. break;
  944. case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
  945. h.powert = new bin_to_powert(data);
  946. break;
  947. case PKT_TYPE.PKT_TYPE_ALARM:
  948. h.alarmd = new bin_to_alarm(data);
  949. break;
  950. case PKT_TYPE.PKT_TYPE_SLAVE_LIST:
  951. h.alarmd = new bin_to_slist(data);
  952. break;
  953. case PKT_TYPE.PKT_TYPE_PARAS:
  954. h.paras = new bin_to_paras(data);
  955. break;
  956. default :
  957. console.log("wrong hdr type: "+h.type);
  958. break;
  959. }
  960. }
  961. return h;
  962. };
  963. //////////////////////////////////////////
  964. function LOG(){
  965. var data = [];
  966. for(var i= 1,len=arguments.length;i<len;i++){
  967. data.push(arguments[i])
  968. }
  969. console.log(data);
  970. }
  971. function WS(url,fn) {
  972. this.url = url;
  973. this.name = 'myWebs';
  974. this.open = _open;
  975. this.reopen = _reopen;
  976. this.close = _close;
  977. this.send = _send;
  978. this.state = _state;
  979. this.setcb = _setcb;
  980. this.closeCount = 0;
  981. this.maxConnect = 100;
  982. var his = this;
  983. this.open();
  984. function _open() {
  985. console.log("____url: ", his.url);
  986. his.ws = new WebSocket(his.url);
  987. his.ws.binaryType = 'arraybuffer';
  988. his.ws.onopen = _onopen;
  989. his.ws.onclose = _onclose;
  990. his.ws.onmessage = _onmsg;
  991. his.ws.onerror = _onerror;
  992. }
  993. function _reopen() {
  994. _close();
  995. _open(his.url);
  996. }
  997. function _close() {
  998. his.ws.close();
  999. }
  1000. function _send(msg) {
  1001. if (his.ws.readyState != WebSocket.OPEN)
  1002. return;
  1003. his.ws.send(msg);
  1004. }
  1005. function _state() {
  1006. return his.ws.readyState;
  1007. }
  1008. function _setcb(tp, f) {
  1009. switch (tp) {
  1010. case "open":
  1011. his.ws.onopen = f;
  1012. break;
  1013. case "close":
  1014. his.ws.onclose = f;
  1015. break;
  1016. case "msg":
  1017. his.ws.onmessage = f;
  1018. break;
  1019. case "err":
  1020. his.ws.onerror = f;
  1021. break;
  1022. }
  1023. }
  1024. function loop_send() {
  1025. console.log("PKT_TYPE_POWER_CH request");
  1026. _send(mk_hdr(PKT_TYPE.PKT_TYPE_POWER_CH));
  1027. setTimeout(loop_send, 2000);
  1028. }
  1029. function _onopen(evt) {
  1030. his.closeCount = 0;
  1031. console.log("___ ws.onopen");
  1032. //setTimeout(loop_send, 2000);
  1033. }
  1034. function _onclose(evt) {
  1035. LOG("ws.onclose");
  1036. setTimeout(_reopen, 3000);
  1037. his.closeCount += 1;
  1038. }
  1039. function _onmsg(evt) {
  1040. console.log("___ ws.onmsg");
  1041. if (evt.data instanceof ArrayBuffer) {
  1042. var obj=bin_parse(evt.data);
  1043. fn(obj);
  1044. }
  1045. }
  1046. function _onerror(evt) {
  1047. console.log("___ ws.onerror");
  1048. //write_scr('<span style="color: red;">ERROR:</span> '+ evt.data);
  1049. }
  1050. }