| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321 |
- const MAGIC=0xdead;
- const PKT_TYPE=function(){
- var len = 0;
- return {
- PKT_TYPE_ACK:len++,
- PKT_TYPE_PARAS:len++,
- PKT_TYPE_POWER_CH:len++, //power_ch_t
- PKT_TYPE_POWER_TOTAL:len++, //power_total_t
- PKT_TYPE_POWER_RESET:len++,
- PKT_TYPE_CH_ALL:len++,
- PKT_TYPE_SCAN:len++,
- PKT_TYPE_FACTORY:len++,
- PKT_TYPE_ALARM:len++,
- PKT_TYPE_SENSOR:len++,
- PKT_TYPE_BREAKER:len++,
- PKT_TYPE_STAT:len++,
- PKT_TYPE_MESG:len++,
- PKT_TYPE_FILE:len++,
- PKT_TYPE_SLAVE_LIST:len++,
- }
- }();
- const SUB_TYPE=function(){
- var len = 0;
- return {
- SUB_TYPE_POWER_SWITCH:len++,
- SUB_TYPE_POWER_ALARM:len++,
- SUB_TYPE_POWER_THRESHOLD:len++, //power_ch_t
- SUB_TYPE_POWER_START_DELAY:len++, //power_total_t
- SUB_TYPE_POWER_STOP_DELAY:len++,
-
- SUB_TYPE_PARAS_MODBUS:len++,
- SUB_TYPE_PARAS_MISC:len++,
- SUB_TYPE_PARAS_PROD:len++,
- SUB_TYPE_PARAS_SERV:len++,
- SUB_TYPE_PARAS_NETWORK:len++,
- }
- }();
- const PDU_TYPE=function(){
- var len = 0;
- return {
- PDU_AC_I1O1:len++,
- PDU_DC_I1O1:len+=2,
- PDU_AC_I3O3:len++,
- PDU_AC_I3O1:len++,
- PDU_AC_I3O1_H:len++,
- }
- }();
- const ALARM_ACT=function(){
- var len = 1;
- return {
- ALARM_ACT_CLOSE_CH:len++,
- ALARM_ACT_MESG:len++,
- ALARM_ACT_CLOSE_CHALL:len++,
- }
- }();
- const IP_MODE=function(){
- var len = 0;
- return {
- IP_MODE_DHCP:len++,
- IP_MODE_STATIC:len++,
- }
- }();
- const IP_VER=function(){
- var len = 0;
- return {
- IP_VER_ERR:len++,
- IP_VER_V4:len++,
- IP_VER_V6:len++,
- }
- }();
- const MODBUS_MODE=function(){
- var len = 0;
- return {
- MODBUS_MASTER:len++,
- MODBUS_SLAVE:len++,
- }
- }();
- const LANG_ID=function(){
- var len = 0;
- return {
- LANG_ID_CHINESE:len++,
- LANG_ID_ENGLISH:len++,
- LANG_ID_RUSSIAN:len++,
- LANG_ID_SPANISH:len++,
- LANG_ID_PORTYGYESE:len++,
- LANG_ID_GERMAN:len++,
- LANG_ID_FRENCH:len++,
- LANG_ID_ITALIAN:len++,
- LANG_ID_JAPANESE:len++,
- }
- }();
- ///////////////////////////////////////////////////////
- function array_to_string(data){
- const decoder = new TextDecoder();
- return decoder.decode(new Uint8Array(data));
- }
- function array_set(array,str,offset){
- const encoder = new TextEncoder(); // ĬÈÏ'utf-8'±àÂë
- const buffer = encoder.encode(str);
- array.set(buffer);
- array.fill(offset, buffer.length);
- }
- function string_to_array(data,len){
- const encoder = new TextEncoder(); // ĬÈÏ'utf-8'±àÂë
- const buffer = encoder.encode(data);
- const fixedArray = new Uint8Array(len);
- fixedArray.set(buffer);
- fixedArray.fill(0, buffer.length);
- return fixedArray.buffer;
- }
- function array_cat(a1,a2){
- var tlen=a1.byteLength+a2.byteLength;
- var buffer = new ArrayBuffer(tlen);
- var vw = new Uint8Array(buffer);
-
- var vw1 = new Uint8Array(a1);
- for(var i=0; i<a1.byteLength; i++) {
- vw[i] = vw1[i];
- }
-
- var vw2 = new Uint8Array(a2);
- for(var i=0; i<a2.byteLength; i++) {
- vw[i+a1.byteLength] = vw2[i];
- }
- return buffer;
- }
- function mk_hdr_bin(addr,type,subtype,nack,flag,dlen)
- {
- var tmp,length=0;
- var buffer = new ArrayBuffer(14);
-
- tmp = new DataView(buffer, length);
- var t = MAGIC;
- tmp.setUint16(0,t,true); //magic
- length += 2;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,addr,true); //addr
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,type,true); //type
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,0,true); //subtype
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,0,true); //nack
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,flag,true); //flag
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,dlen,true); //dlen
- length += 4;
-
- return buffer;
- }
- function mk_hdr_obj(addr,type,subtype,nack,flag,dlen)
- {
- this.magic = MAGIC;
- this.addr = addr;
- this.type = type;
- this.subtype = subtype;
- this.flag = flag;
- this.dlen = dlen;
- }
- function pkt_to_bin(addr,type,subtype,nack,flag,js)
- {
- var tmp;
- var buffer = new mk_hdr_bin(addr,type,subtype,nack,flag,js.len);
-
- switch(type){
- case PKT_TYPE.PKT_TYPE_POWER_CH:
- tmp = new powerc_to_bin(js);
- break;
- case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
- tmp = new powert_to_bin(js);
- break;
-
- case PKT_TYPE.PKT_TYPE_ALARM:
- tmp = new alarm_to_bin(js);
- break;
-
- default:
- return tmp;
- }
- return array_cat(buffer,tmp);
- }
- function bin_to_pkt(bin)
- {
- var hdr = new bin_to_hdr(bin);
-
- var data = bin.slice(hdr.len);
- if(data.byteLength>0) {
- switch(hdr.type) {
- case PKT_TYPE.PKT_TYPE_POWER_CH:
- hdr.powerc = new bin_to_powerc(data);
- break;
- case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
- hdr.powert = BIN.powert.b2j(data);
- break;
-
- case PKT_TYPE.PKT_TYPE_ALARM:
- hdr.alarmd = BIN.alarmd.b2j(data);
- break;
-
- case PKT_TYPE.PKT_TYPE_ALARM:
- hdr.alarmd = BIN.alarmd.b2j(data);
- break;
-
- case PKT_TYPE.PKT_TYPE_SLAVE_LIST:
- hdr.slaves = BIN.alarmd.b2j(data);
- break;
-
- default:
- console.log("wrong hdr type: "+hdr.type);
- }
- }
- return hdr;
- }
- ///////////////////////////////////////////////////
- function bin_to_hdr(bin) {
- var tmp, length=0;
- tmp = new DataView(bin, length);
- this.magic = tmp.getUint16(0,true);
- length += 2;
- tmp = new DataView(bin, length);
- this.addr = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.type = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.subtype = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.nack = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.flag = tmp.getUint8(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.dlen = tmp.getUint32(0,true);
- length += 4;
- this.len = length;
- }
- function bin_to_info(bin) {
- var tmp, length=0;
- this.name = array_to_string(bin.slice(0,32));
- length += 32;
- tmp = new DataView(bin, length);
- this.type = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.addr = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.ch = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.sch = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.ph_id = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.ph_val = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.start_delay = tmp.getUint32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.stop_delay = tmp.getUint32(0,true);
- length += 4;
- this.len = length;
- }
- function bin_to_power(bin) {
- var tmp, length=0;
- tmp = new DataView(bin, length);
- this.voltage = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.current = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.power = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.freq = tmp.getFloat32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.factor = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.consump = tmp.getFloat32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.active = tmp.getFloat32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.reactive = tmp.getFloat32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.ph_out = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.status = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.nwire = tmp.getUint8(0,true);
- length += 1;
- this.len = length;
- }
- function bin_to_alarm(bin) {
- var tmp, length=0;
- tmp = new DataView(bin, length);
- this.v_upper = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.v_lower = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.c_upper = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.p_upper = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.w_upper = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.ph_loss = tmp.getUint8(0,true);
- length += 1;
- this.len = length;
- }
- function bin_to_thrval(bin) {
- var tmp, length=0;
-
- tmp = new DataView(bin, length);
- this.en = tmp.getUint8(0,true);
- length += 1;
- tmp = new DataView(bin, length);
- this.val = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.act = tmp.getUint8(0,true);
- length += 1;
- this.act_para = [];
- tmp = new DataView(bin, length);
- for(var i=0; i<14; i++) {
- this.act_para[i] = tmp.getUint8(i,true);
- }
- length += 14;
- this.len = length;
- }
- function bin_to_thr(bin) {
- var tmp, length=0;
-
- this.v_upper = new bin_to_thrval(bin.slice(length));
- length += this.v_upper.len;
-
- this.v_lower = new bin_to_thrval(bin.slice(length));
- length += this.v_lower.len;
-
- this.c_upper = new bin_to_thrval(bin.slice(length));
- length += this.c_upper.len;
-
- this.p_upper = new bin_to_thrval(bin.slice(length));
- length += this.p_upper.len;
-
- this.w_upper = new bin_to_thrval(bin.slice(length));
- length += this.w_upper.len;
- this.len = length;
- }
- ////////////////////////////////
- function bin_to_modbus(bin) {
- var tmp, length=0;
-
- tmp = new DataView(bin, length);
- this.mode = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.addr = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.baud = tmp.getUint32(0,true);
- length += 4;
- this.len = length;
- }
- function bin_to_misc(bin) {
- var tmp, length=0;
-
- tmp = new DataView(bin, length);
- this.watchdog = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.fliptime = tmp.getUint32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.led = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.beep = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.rotate = tmp.getUint8(0,true);
- length += 1;
- this.len = length;
- }
- function bin_to_prod(bin) {
- var tmp, length=0;
-
- tmp = new DataView(bin, length);
- this.id = tmp.getUint8(0,true);
- length += 1;
-
- this.pname = array_to_string(bin.slice(length,32));
- length += 32;
-
- this.pnumber = array_to_string(bin.slice(length,32));
- length += 32;
-
- this.status = array_to_string(bin.slice(length,32));
- length += 32;
-
- this.name = array_to_string(bin.slice(length,32));
- length += 32;
-
- this.number = array_to_string(bin.slice(length,32));
- length += 32;
-
- this.date = array_to_string(bin.slice(length,32));
- length += 32;
-
- this.batch = array_to_string(bin.slice(length,32));
- length += 32;
-
- tmp = new DataView(bin, length);
- this.type = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.pwr_type = tmp.getUint8(0,true);
- length += 1;
-
- this.version = array_to_string(bin.slice(length,8));
- length += 8;
-
- tmp = new DataView(bin, length);
- this.language = tmp.getUint8(0,true);
- length += 1;
-
- tmp = new DataView(bin, length);
- this.samptime = tmp.getUint32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.savetime = tmp.getUint32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.ph_group = tmp.getUint16(0,true);
- length += 2;
-
- tmp = new DataView(bin, length);
- this.ch_delay = tmp.getUint16(0,true);
- length += 2;
- this.len = length;
- }
- function bin_to_serv(bin) {
- var tmp, length=0;
-
- this.name = array_to_string(bin.slice(length,256));
- length += 256;
-
- this.site = array_to_string(bin.slice(length,256));
- length += 256;
-
- this.mail = array_to_string(bin.slice(length,256));
- length += 256;
-
- this.addr = array_to_string(bin.slice(length,256));
- length += 256;
-
- this.tele = array_to_string(bin.slice(length,32));
- length += 32;
- this.len = length;
- }
- function bin_to_network(bin) {
- var tmp, length=0;
-
- tmp = new DataView(bin, length);
- this.mode = tmp.getUint8(0,true);
- length += 1;
-
- this.ipaddr = array_to_string(bin.slice(length,48));
- length += 48;
-
- this.mask = array_to_string(bin.slice(length,48));
- length += 48;
-
- this.gateway = array_to_string(bin.slice(length,48));
- length += 48;
-
- this.dns1 = array_to_string(bin.slice(length,48));
- length += 48;
-
- this.dns2 = array_to_string(bin.slice(length,48));
- length += 48;
- this.len = length;
- }
- ////////////////////////////////
- function bin_to_powerc(bin) {
- var tmp, bin2, length=0;
- this.info = new bin_to_info(bin);
- length += this.info.len;
-
- this.power = [];
- for(var i=0; i<3; i++) {
- this.power[i] = new bin_to_power(bin.slice(length));
- length += this.power[i].len;
- }
-
- this.thr = new bin_to_thr(bin.slice(length));
- length += this.thr.len;
-
- this.alarm = new bin_to_alarm(bin.slice(length));
- length += this.alarm.len;
-
- tmp = new DataView(bin, length);
- this.time = tmp.getUint32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.pbrd = tmp.getUint32(0,true);
- length += 4;
- this.len = length;
- }
- function bin_to_total(bin) {
- var tmp, length=0;
- tmp = new DataView(bin, length);
- this.voltage = tmp.getFloat32 (0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.current = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.power = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.freq = tmp.getFloat32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.factor = tmp.getFloat32(0,true);
- length += 4;
-
- tmp = new DataView(bin, length);
- this.consump = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.active = tmp.getFloat32(0,true);
- length += 4;
- tmp = new DataView(bin, length);
- this.reactive = tmp.getFloat32(0,true);
- length += 4;
-
- this.len = length;
- }
- function bin_to_powert(bin) {
- var tmp, length=0;
- tmp = new DataView(bin, length);
- this.type = tmp.getUint8(0,true);
- length += 1;
- this.total = [];
- for(var i=0; i<3; i++) {
- this.total[i] = new bin_to_total(bin.slice(length));
- length += this.total[i].len;
- }
-
- this.len = length;
- }
- function bin_to_alarmd(bin) {
- var tmp, length=0;
-
- tmp = new DataView(bin, length);
- this.ch = tmp.getUint8(0,true);
- length += 1;
-
- this.alarm = new bin_to_alarm(bin.slice(length));
- length += this.alarm.len;
-
- tmp = new DataView(bin, length);
- this.time = tmp.getUint32(0,true);
- length += 4;
- this.len = length;
- }
- function bin_to_paras(bin) {
- var tmp, length=0;
-
- this.cas = new bin_to_modbus(bin.slice(length));
- length += this.cas.len;
-
- this.misc = new bin_to_misc(bin.slice(length));
- length += this.misc.len;
-
- this.prod = new bin_to_prod(bin.slice(length));
- length += this.prod.len;
-
- this.serv = new bin_to_serv(bin.slice(length));
- length += this.serv.len;
-
- this.network = new bin_to_network(bin.slice(length));
- length += this.network.len;
- this.len = length;
- }
- function bin_to_slist(bin) {
- var tmp, length=0;
-
- tmp = new DataView(bin, length);
- this.cnt = tmp.getUint8(0,true);
- length += 1;
-
- this.addr = [];
- tmp = new DataView(bin, length);
- for(var i=0; i<this.cnt; i++) {
- this.addr[i] = tmp.getUint8(i,true);
- }
- length += this.cnt;
- this.len = length;
- }
- ///////////////////////////////////////////////////
- function hdr_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- tmp = new DataView(buffer, length);
- tmp.setUint16(0,js.magic,true);
- length += 2;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.addr,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.type,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.subtype,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.nack,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,js.flag,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,js.dlen,true);
- length += 4;
- return buffer;
- }
- function info_to_bin(js) {
- var tmp, length=0;
- var name = string_to_array(js.name, 32);
- var buffer = new ArrayBuffer(js.len-name.byteLength);
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.type,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.addr,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.ch,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.sch,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.ph_id,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.ph_val,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,js.start_delay,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,js.stop_delay,true);
- length += 4;
- return array_cat(name,buffer);
- }
- function power_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.voltage,true);
- length += 4;
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.current,true);
- length += 4;
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.power,true);
- length += 4;
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.freq,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.factor,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.consump,true);
- length += 4;
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.active,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.reactive,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.ph_put,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.status,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.nwire,true);
- length += 1;
- return buffer;
- }
- function alarm_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.v_upper,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.v_lower,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.c_upper,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.p_upper,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.w_upper,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.ph_loss,true);
- length += 1;
- return buffer;
- }
- function thrval_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.en,true);
- length += 1;
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.val,true);
- length += 4;
- tmp = new DataView(buffer, length);
- tmp.getUint8(0,js.act,true);
- length += 1;
- tmp = new DataView(buffer, length);
- for(var i=0; i<14; i++) {
- tmp.setUint8(i,js.act_para[i],true);
- }
- length += 14;
- return buffer;
- }
- function thr_to_bin(js) {
- var bin,tmp, length=0;
-
- bin = thrval_to_bin(js.v_upper);
- tmp = thrval_to_bin(js.v_lower);
-
- bin = array_cat(bin, tmp);
- tmp = thrval_to_bin(js.c_upper);
- bin = array_cat(bin, tmp);
-
- tmp = thrval_to_bin(js.p_upper);
- bin = array_cat(bin, tmp);
-
- tmp = thrval_to_bin(js.w_upper);
- bin = array_cat(bin, tmp);
- return bin;
- }
- ////////////////////////////////
- function modbus_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.mode,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.addr,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,js.baud,true);
- length += 4;
-
- return buffer;
- }
- function misc_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.watchdog,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.fliptime,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.led,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.beep,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.rotate,true);
- length += 1;
-
- return buffer;
- }
- function prod_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,js.id,true);
- length += 4;
-
- array_set(buffer,js.pname,length);
- length += 32;
-
- array_set(buffer,js.pnumber,length);
- length += 32;
-
- array_set(buffer,js.status,length);
- length += 32;
-
- array_set(buffer,js.name,length);
- length += 32;
-
- array_set(buffer,js.number,length);
- length += 32;
-
- array_set(buffer,js.date,length);
- length += 32;
-
- array_set(buffer,js.batch,length);
- length += 32;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.type,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.pwrtype,true);
- length += 1;
-
- array_set(buffer,js.version,length);
- length += 8;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.language,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.count,true);
- length += 1;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,js.samptime,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setUint32(0,js.savetime,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setUint16(0,js.ph_group,true);
- length += 2;
-
- tmp = new DataView(buffer, length);
- tmp.setUint16(0,js.ch_delay,true);
- length += 2;
-
- return buffer;
- }
- function serv_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- array_set(buffer,js.name,length);
- length += 256;
-
- array_set(buffer,js.site,length);
- length += 256;
-
- array_set(buffer,js.mail,length);
- length += 256;
-
- array_set(buffer,js.addr,length);
- length += 256;
-
- array_set(buffer,js.tele,length);
- length += 32;
-
- return buffer;
- }
- function network_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- tmp = new DataView(buffer, length);
- tmp.setUint8(0,js.mode,true);
- length += 1;
-
- array_set(buffer,js.ipaddr,length);
- length += 48;
-
- array_set(buffer,js.mask,length);
- length += 48;
-
- array_set(buffer,js.gateway,length);
- length += 48;
-
- array_set(buffer,js.dns1,length);
- length += 48;
-
- array_set(buffer,js.dns2,length);
- length += 48;
-
- return buffer;
- }
- ////////////////////////////////
- function powerc_to_bin(js) {
- var bin,tmp, length=0;
-
- bin = new info_to_bin(js.info);
-
- for(var i=0; i<3; i++) {
- tmp = new power_to_bin(js.power[i]);
- bin = array_cat(bin, tmp);
- }
-
- tmp = new thr_to_bin(js.thr);
- bin = array_cat(bin, tmp);
- tmp = new alarm_to_bin(js.alarm);
- bin = array_cat(bin, tmp);
-
- var buf = new ArrayBuffer(8);
- tmp = new DataView(buf, 0);
- tmp.setUint32(0,js.time,true);
- tmp.setUint32(4,js.pbrd,true);
-
- bin = array_cat(bin, buf);
- return bin;
- }
- function total_to_bin(js) {
- var tmp, length=0;
- var buffer = new ArrayBuffer(js.len);
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.voltage,true);
- length += 4;
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.current,true);
- length += 4;
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.power,true);
- length += 4;
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.freq,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.factor,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.consump,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.factor,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.active,true);
- length += 4;
-
- tmp = new DataView(buffer, length);
- tmp.setFloat32(0,js.reactive,true);
- length += 4;
- return buffer;
- }
- function powert_to_bin(js) {
- var tmp,bin2,length=0;
- var bin = new ArrayBuffer(1);
- tmp = new DataView(bin, length);
- tmp.setUint8(0,js.type,true);
- length += 4;
- for(var i=0; i<3; i++) {
- bin2 = total_to_bin(js.total[i]);
- bin = array_cat(bin, bin2);
- }
- return bin;
- }
- function alarmd_to_bin(js) {
- var tmp, length=0;
- var bin,buffer = new ArrayBuffer(1);
- tmp = new DataView(buffer, 0);
- tmp.setUint8(0,js.ch,true);
-
- var alarm = alarm_to_bin(js.alarm);
- bin = array_cat(buffer, alarm);
- buffer = new ArrayBuffer(4);
- tmp = new DataView(buffer, 0);
- tmp.setUint32(0,js.time,true);
- return array_cat(bin, buffer);
- }
- //////////////////////////////////////////////////////////////
- function bin_parse(bin)
- {
- var tmp,h = new bin_to_hdr(bin);
- if(h.nack == 1){
- //tmp = myBinary.ack.bin2js(bin,this.h.len,true);
- //myLog('log',tmp);
- //var data = tmp.data;
- }
-
- var data = bin.slice(h.len);
- if(data.byteLength>0 && h.dlen==data.byteLength) {
- switch(h.type) {
- case PKT_TYPE.PKT_TYPE_POWER_CH:
- {
- var tmp=[],length=0;
- for(var i=0;; i++) {
- tmp[i] = new bin_to_powerc(data.slice(length));
- length += tmp[i].len;
- if(length >= data.byteLength) {
- break;
- }
- }
- h.powerc = tmp;
- }
- break;
- case PKT_TYPE.PKT_TYPE_POWER_TOTAL:
- h.powert = new bin_to_powert(data);
- break;
-
- case PKT_TYPE.PKT_TYPE_ALARM:
- h.alarmd = new bin_to_alarm(data);
- break;
-
- case PKT_TYPE.PKT_TYPE_SLAVE_LIST:
- h.alarmd = new bin_to_slist(data);
- break;
-
- case PKT_TYPE.PKT_TYPE_PARAS:
- h.alarmd = new bin_to_paras(data);
- break;
-
- default :
- console.log("wrong hdr type: "+h.type);
- break;
- }
- }
- return h;
- };
- //////////////////////////////////////////
- function LOG(){
- var data = [];
- for(var i= 1,len=arguments.length;i<len;i++){
- data.push(arguments[i])
- }
- console.log(data);
- }
- function WS(url,fn) {
- this.url = url;
- this.name = 'myWebs';
- this.open = _open;
- this.reopen = _reopen;
- this.close = _close;
- this.send = _send;
- this.state = _state;
- this.setcb = _setcb;
- this.closeCount = 0;
- this.maxConnect = 100;
- var his = this;
- this.open();
- function _open() {
- console.log("____url: ", his.url);
- his.ws = new WebSocket(his.url);
- his.ws.binaryType = 'arraybuffer';
- his.ws.onopen = _onopen;
- his.ws.onclose = _onclose;
- his.ws.onmessage = _onmsg;
- his.ws.onerror = _onerror;
- }
- function _reopen() {
- _close();
- _open(his.url);
- }
- function _close() {
- his.ws.close();
- }
- function _send(msg) {
- if (his.ws.readyState != WebSocket.OPEN)
- return;
- his.ws.send(msg);
- }
- function _state() {
- return his.ws.readyState;
- }
- function _setcb(tp, f) {
- switch (tp) {
- case "open":
- his.ws.onopen = f;
- break;
- case "close":
- his.ws.onclose = f;
- break;
- case "msg":
- his.ws.onmessage = f;
- break;
- case "err":
- his.ws.onerror = f;
- break;
- }
- }
- function loop_send() {
- console.log("PKT_TYPE_POWER_CH request");
- _send(mk_hdr(PKT_TYPE.PKT_TYPE_POWER_CH));
- setTimeout(loop_send, 2000);
- }
- function _onopen(evt) {
- his.closeCount = 0;
- console.log("___ ws.onopen");
- //setTimeout(loop_send, 2000);
- }
- function _onclose(evt) {
- LOG("ws.onclose");
- setTimeout(_reopen, 3000);
- his.closeCount += 1;
- }
- function _onmsg(evt) {
- console.log("___ ws.onmsg");
- if (evt.data instanceof ArrayBuffer) {
- var obj=bin_parse(evt.data);
- fn(obj);
- }
- }
- function _onerror(evt) {
- console.log("___ ws.onerror");
- //write_scr('<span style="color: red;">ERROR:</span> '+ evt.data);
- }
- }
|