@idm-plugin/vessel 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -511,7 +511,7 @@ class ot extends C {
511
511
  return r;
512
512
  }
513
513
  }
514
- const A = O.getLogger(__filename);
514
+ const A = O.getLogger();
515
515
  var U = /* @__PURE__ */ ((p) => (p.NOTICE = "NOTICE", p.WARN = "WARN", p.HEAVY = "HEAVY", p.SEVERE = "SEVERE", p.ERROR = "ERROR", p.FATAL = "FATAL", p))(U || {});
516
516
  class J {
517
517
  /**
@@ -1 +1 @@
1
- (function(M,y){typeof exports=="object"&&typeof module<"u"?y(exports,require("got"),require("moment")):typeof define=="function"&&define.amd?define(["exports","got","moment"],y):(M=typeof globalThis<"u"?globalThis:M||self,y(M["idm-plugin-rabbitmq"]={},M.got,M.moment))})(this,function(M,y,l){"use strict";var Z=Object.defineProperty;var tt=(M,y,l)=>y in M?Z(M,y,{enumerable:!0,configurable:!0,writable:!0,value:l}):M[y]=l;var _=(M,y,l)=>(tt(M,typeof y!="symbol"?y+"":y,l),l);function z(k){return k&&k.__esModule&&Object.prototype.hasOwnProperty.call(k,"default")?k.default:k}class W{log(){}isLevelEnabled(){return!1}addContext(){}removeContext(){}clearContext(){}}["Trace","Debug","Info","Warn","Error","Fatal","Mark"].forEach(k=>{W.prototype[k.toLowerCase()]=()=>{},W.prototype[`is${k}Enabled`]=()=>!1});const O=(()=>{try{return require("log4js")}catch{return null}})();var G={getLogger:O?O.getLogger:()=>new W};const L=z(G),I=L.getLogger();class R{parseStatus(p){let e,a;switch(p){case 0:e="在航(主机推动)",a="The engine is in use";break;case 1:e="锚泊",a="Anchored";break;case 2:e="失控",a="Not operated";break;case 3:e="操纵受限",a="Limited airworthiness";break;case 4:e="吃水受限",a="Limited by ship's draft";break;case 5:e="靠泊",a="Mooring";break;case 6:e="搁浅",a="Stranded";break;case 7:e="捕捞作业",a="Engaged in fishing";break;case 8:e="靠帆船提供动力",a="Sailing";break;default:e="未定义",a="Undefined"}return{labelCn:e,labelEn:a}}}class B extends R{constructor(e,a){super();_(this,"clientId");_(this,"clientSecret");_(this,"token");this.clientId=e,this.clientSecret=a}async authToken(e={}){const a="https://svc.data.myvessel.cn/ada/oauth/token",n={searchParams:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},o=await y.post(a,n).json();I.info("[%s] fetch access token from: %s - %j",e.requestId,a,o),o.error||(this.token={accessToken:o.access_token,tokenType:o.token_type,expiresIn:o.expires_in,scope:o.scope,jti:o.jti,issuedAt:l().utc().format()})}async realTimePosition(e,a={}){var d,c,f;(!this.token||l().diff(l(this.token.issuedAt),"seconds")>((d=this.token)==null?void 0:d.expiresIn)-300)&&await this.authToken(a);const n="https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit",o={headers:{Authorization:`${(c=this.token)==null?void 0:c.tokenType} ${(f=this.token)==null?void 0:f.accessToken}`},searchParams:{mmsi:e}};I.info("[%s] fetch realtime position from: %s - %j",a.requestId,n,o);const s=await y.get(n,o).json();if(s.code)return I.warn("[%s] fetch realtime position failed: %j",a.requestId,n,{message:s.message,status:s.status,code:s.code}),s;const t=s.data;for(const u in t)!isNaN(t[u])&&Number(t[u])!==1/0&&(t[u]=Number(t[u]));const r=l(`${t.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:t.mmsi,name:t.vesselName,imo:t.imo,callSign:t.callsign,lat:t.lat,lng:t.lon,length:t.length,width:t.width,draught:t.currDraught,sog:t.sog,cog:t.cog,hdg:t.hdg,rot:t.rot,eta:t.eta,destination:t.dest,positionTime:r.unix(),status:t.status,labelCn:t.statusNameCn,labelEn:t.statusNameEn,method:"position",vendor:"myVessel",utc:r.utc().format()}}async trajectory(e,a,n,o,s=!0,t={}){(!this.token||l().diff(l(this.token.issuedAt),"seconds")>this.token.expiresIn-300)&&await this.authToken(t);const r=await this.realTimePosition(e,t),i=l(a),d=l(n),c=[];for(;d.diff(i,"day",!0)>30;)await this.trajectoryIn30Day(e,i,i.clone().add(30,"day"),r,o,c,t),i.add(30,"day");return await this.trajectoryIn30Day(e,i,d,r,o,c,t),c}async trajectoryIn30Day(e,a,n,o,s,t,r={}){var h,w,Y,j,S;const i="https://svc.data.myvessel.cn/sdc/v1/vessels/status/track",d={headers:{Authorization:`${(h=this.token)==null?void 0:h.tokenType} ${(w=this.token)==null?void 0:w.accessToken}`},json:{mmsi:e,startTime:a.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),endTime:n.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")}};I.info("[%s] fetch trajectory from: %s - %j",r.requestId,i,d);const c=await y.post(i,d).json();if(c.code)return I.warn("[%s] fetch trajectory failed: %j",r.requestId,i,{message:c.message,status:c.status,code:c.code}),c;let f=-1;const u=l(`${(j=(Y=c.data)==null?void 0:Y[0])==null?void 0:j.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return(S=c.data)==null||S.forEach(g=>{for(const N in g)!isNaN(g[N])&&Number(g[N])!==1/0&&(g[N]=Number(g[N]));const v=l(`${g.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),T=g.eta?l(`${g.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"):void 0,m=g.status,{labelCn:D,labelEn:A}=this.parseStatus(m),P={mmsi:g.mmsi,imo:o==null?void 0:o.imo,lat:g.lat,lng:g.lon,sog:g.sog,cog:g.cog,hdg:g.hdg,draught:g.draught,status:m,eta:T==null?void 0:T.unix(),destination:g.dest,positionTime:v.unix(),labelCn:D,labelEn:A,method:"trajectory",vendor:"myVessel",utc:v.utc().format()},q=Math.floor(v.diff(u,"minute",!0)/(s||1));q!==f&&(f=q,t.push(P))}),t}}class U extends R{constructor(e){super();_(this,"token");this.token=e}async realTimePosition(e,a={}){const n="https://api.hifleet.com/position/position/get/token",o={searchParams:{mmsi:e,usertoken:this.token}},s=await y.post(n,o).json();I.info("[%s] fetch realtime position from: %s - %j",a.requestId,n,o);const t=s==null?void 0:s.list;if(!t)return I.warn("[%s] fetch realtime position failed: %j",a.requestId,n,s),s;for(const u in t)!isNaN(t[u])&&Number(t[u])!==1/0&&(t[u]=Number(t[u]));t.status=t.sp>3?0:1;const r=t.status,{labelCn:i,labelEn:d}=this.parseStatus(r),c=l(`${t.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:t.m,name:t.n,imo:t.imonumber,callSign:t.callsign,lat:Math.round(t.la/60*1e5)/1e5,lng:Math.round(t.lo/60*1e5)/1e5,length:t.l,width:t.w,draught:t.draught,sog:t.sp,cog:t.co,hdg:t.h,rot:isNaN(t.rot)?0:t.rot,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(t.eta)?l(`${t.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00").unix():void 0,destination:t.destination,positionTime:c.unix(),utc:c.utc().format(),status:r,labelCn:i,labelEn:d,method:"position",vendor:"hifleet"}}async search(e,a={}){let n="https://www.hifleet.com/hifleetapi/searchVesselOL.do";const o={searchParams:{keyword:e},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}};let s=await y.post(n,o).json();I.info("[%s] fetch vessel props from: %s - %j",a.requestId,n,o),s instanceof Array&&(s=s[0]);for(const r in s)!isNaN(s[r])&&Number(s[r])!==1/0&&(s[r]=Number(s[r]));const t={mmsi:s.m,name:s.n,imo:s.i,callSign:s.c,length:s.l,breadth:s.b,draught:s.dr};return n="https://www.hifleet.com/hifleetapi/sameShipSearch.do",s=await y.post(n,o).json(),I.info("[%s] fetch vessel dead weight from: %s - %j",a.requestId,n,o),s instanceof Array&&(s=s[0]),s&&(t.deadweight=Number(s.dwt)),t}async trajectory(e,a,n,o,s=!0,t={}){var g,v,T;const r=await this.realTimePosition(e,t);let i=l(a);const d=l(n),c=l();if(s){let m=d.diff(i,"d",!0);m<0?i=d.clone().subtract(40,"d"):m<30?i.subtract(10,"d"):m<60?i.subtract(5,"d"):i=d.clone().subtract(80,"d"),m=c.diff(d,"d",!0),d.add(m>10?240:m*24,"h")}const f={searchParams:{endtime:d.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),starttime:i.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),mmsi:e,usertoken:this.token}},u="https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token",h=await y.get(u,f).json();I.info("[%s] fetch trajectory from: %s - %j",t.requestId,u,f);let w;h&&(w=((v=(g=h.ships)==null?void 0:g.offors)==null?void 0:v.ship)||[],w.length||I.warn("[%s] fetch trajectory failed: %j",t.requestId,h));const Y=[];let j=-1;const S=l(`${(T=w==null?void 0:w[0])==null?void 0:T.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");for(const m of w){for(const x in m)!isNaN(m[x])&&Number(m[x])!==1/0&&(m[x]=Number(m[x]));const D=l(`${m.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");m.status=m.sp>4?0:1;const{labelEn:A,labelCn:P}=this.parseStatus(m.status),q={mmsi:m.m,name:m.n,imo:r==null?void 0:r.imo,lat:m.la,lng:m.lo,draught:m.draught,sog:m.sp,cog:m.co,hdg:m.hdg,positionTime:D.unix(),utc:D.utc().format(),status:m.status,labelCn:P,labelEn:A,method:"trajectory",vendor:"hifleet"},N=Math.floor(D.diff(S,"minute",!0)/(o||1));N!==j&&(j=N,Y.push(q))}return Y}}class J extends R{constructor(e){super();_(this,"token");this.token=e}async realTimePosition(e,a={}){const n={searchParams:{id:e,k:this.token,enc:1}},o="https://api.shipxy.com/apicall/GetSingleShip",s=await y.get(o,n).json();if(I.info("[%s] fetch realtime position from: %s - %j",a.requestId,o,n),(s==null?void 0:s.status)!==0)return s;const t=s.data[0];for(const f in t)!isNaN(t[f])&&Number(t[f])!==1/0&&(t[f]=Number(t[f]));const{labelCn:r,labelEn:i}=await this.parseStatus(t.navistat),d=l.unix(t.lasttime);return{mmsi:t.ShipID,name:t.name,imo:t.imo,callSign:t.callsign,lat:Math.round(t.lat/1e6*1e5)/1e5,lng:Math.round(t.lon/1e6*1e5)/1e5,length:Math.round(t.length/10*100)/100,width:Math.round(t.width/10*100)/100,draught:Math.round(t.draught/1e3*100)/100,sog:Math.round(t.sog*3600/1e3/1852*100)/100,cog:Math.round(t.cog/100*100)/100,hdg:Math.round(t.hdg/100*100)/100,rot:Math.round(t.rot/100*100)/100,positionTime:t.lasttime,utc:d.utc().format(),status:t.navistat,labelEn:i,labelCn:r,method:"position",vendor:"shipxy"}}async trajectory(e,a,n,o,s=!0,t={}){var S;const r=await this.realTimePosition(e,t),i=l(a),d=l(n),c="https://api.shipxy.com/apicall/GetShipTrack",f={searchParams:{id:e,k:this.token,enc:1,cut:0,btm:i.unix(),etm:d.unix()}},u=await y.get(c,f).json();if(I.info("[%s] fetch trajectory from: %s - %j",t.requestId,c,f),(u==null?void 0:u.status)!==0)return u;const h=u==null?void 0:u.points,w=[],Y=l.unix((S=h[0])==null?void 0:S.utc);let j=-1;for(const g of h){const v=l.unix(g.utc),T={imo:r==null?void 0:r.imo,mmsi:e,sog:Math.round(g.sog*3600/1e3/1852*100)/100,cog:Math.round(g.cog/100*100)/100,lat:Math.round(g.lat/1e6*1e5)/1e5,lng:Math.round(g.lon/1e6*1e5)/1e5,positionTime:v.unix(),utc:v.utc().format(),method:"trajectory",vendor:"shipxy"},m=Math.floor(v.diff(Y,"minute",!0)/(o||1));m!==j&&(j=m,w.push(T))}return w}}class Q extends R{constructor(e){super();_(this,"token");this.token=e}async getShipId(e,a={}){const n={headers:{appKey:this.token},json:{mmsiList:e}},o="https://api3.myships.com/sp/ships/getShipIdByMMSI",s=await y.post(o,n).json();return I.info("[%s] fetch ship id from: %s - %j",a.requestId,o,n),s.code!=="0"?s:s.data[0].shipId}async getShipInfo(e,a={}){const n={headers:{appKey:this.token},json:{shipId:e}},o="https://api3.myships.com/sp/ships/aissta",s=await y.post(o,n).json();if(I.info("[%s] fetch ship info from: %s - %j",a.requestId,o,n),s.code!=="0")return s;const t=s.data;let r=t.imo;return e==="407170"&&(r="9198379",I.warn("[%s] ship(%s) imo error: %s, should be %s",a.requestId,e,t.imo,r)),{mmsi:t.mmsi,name:t.shipnameEn,imo:r,callSign:t.callSign,length:t.length,width:t.breadth,draught:(t.draught||100)/10}}async realTimePosition(e,a={}){const n=await this.getShipId(e,a),o=await this.getShipInfo(n,a),s={headers:{appKey:this.token},json:{shipId:n}},t="https://api3.myships.com/sp/ships/position/latest",r=await y.post(t,s).json();I.info("[%s] fetch realtime position from: %s - %j",a.requestId,t,s);const i=r.data[0];for(const h in i)!isNaN(i[h])&&Number(i[h])!==1/0&&(i[h]=Number(i[h]));const{labelCn:d,labelEn:c}=await this.parseStatus(i.aisNavStatus),f=l.unix(i.posTime);return{...o,mmsi:e,lat:Math.round(i.lat/1e4/60*1e5)/1e5,lng:Math.round(i.lon/1e4/60*1e5)/1e5,sog:Math.round(i.sog/10*100)/100,cog:Math.round(i.cog/10*100)/100,hdg:Math.round(i.heading*100)/100,rot:Math.round(i.rot*100)/100,positionTime:i.posTime,utc:f.utc().format(),status:i.aisNavStatus,labelEn:c,labelCn:d,method:"position",vendor:"myship"}}async trajectory(e,a,n,o,s=!0,t={}){const r=l(a),i=l(n),d=await this.getShipId(e),c=await this.getShipInfo(d),f=[];for(;i.diff(r,"day",!0)>30;)await this.trajectoryIn30Day(d,r.unix(),r.add(30,"day").unix(),c,e,o,f);return await this.trajectoryIn30Day(d,r.unix(),i.unix(),c,e,o,f),f}async trajectoryIn30Day(e,a,n,o,s,t,r,i={}){var Y;const d={headers:{appKey:this.token},json:{shipId:e,startTime:a,endTime:n}},c="https://api3.myships.com/sp/ships/position/history",f=await y.post(c,d).json();if(I.info("[%s] fetch trajectory from: %s - %j",i.requestId,c,d),f.code!=="0")return I.warn("[%s] invoke myship trajectory failed: %j",i.requestId,f),f;const u=f.data;for(const j in u)!isNaN(u[j])&&Number(u[j])!==1/0&&(u[j]=Number(u[j]));const h=l.unix((Y=u[0])==null?void 0:Y.posTime);let w=-1;for(const j of u){const S=l.unix(j.posTime),g={imo:o==null?void 0:o.imo,mmsi:s,lat:Math.round(j.lat/1e4/60*1e5)/1e5,lng:Math.round(j.lon/1e4/60*1e5)/1e5,sog:Math.round(j.sog/10*100)/100,cog:Math.round(j.cog/10*100)/100,hdg:Math.round(j.heading*100)/100,rot:Math.round(j.rot*100)/100,positionTime:S.unix(),utc:S.utc().format(),method:"trajectory",vendor:"myship"},v=Math.floor(S.diff(h,"minute",!0)/(t||1));v!==w&&(w=v,r.push(g))}return r}}const V=L.getLogger(__filename);var F=(k=>(k.NOTICE="NOTICE",k.WARN="WARN",k.HEAVY="HEAVY",k.SEVERE="SEVERE",k.ERROR="ERROR",k.FATAL="FATAL",k))(F||{});class K{parsePrinciple(p,e={}){var t,r,i;V.info("[%s] parse rule: %s",e.requestId,p);const a=new RegExp("(?<=\\[)(.+)(?=\\])","g"),n=p.match(a)?(t=p.match(a))==null?void 0:t[0]:void 0,o=n==null?void 0:n.split(";");if(!o)return;const s={};for(let d=0;d<(o==null?void 0:o.length);d++){const c=(i=(r=o[d].match(a))==null?void 0:r[0])==null?void 0:i.split("],");if(d===0&&!c)s.scope=o[0];else if(c)for(let f=0,u=c.length;f<u;f++){const h=this.parseRule(c[f]);h&&(s[h.level]?h.key?s[h.level][h==null?void 0:h.key]=h:s[h.level]=h:h.key?s[h.level]={[h==null?void 0:h.key]:h}:s[h.level]=h)}}return s}parseRule(p,e={}){var s;V.info("[%s] parse rule: %s",e.requestId,p),p=p.startsWith("[")?p:`[${p}`,p=p.endsWith("]")?p:`${p}]`;const a=new RegExp("(?<=\\[)(.+?)(?=\\])","g"),n=(s=p==null?void 0:p.match(a))==null?void 0:s[0],o=n==null?void 0:n.split(",");if(o)return{operator:o[0],number:Number(o[1]),level:o[2],time:Number(o[3]),key:o[4]}}checkWeather(p,e,a={}){var h,w,Y,j,S,g,v,T,m,D,A,P,q,N,x;let n=0,o=0,s=0,t=0;const r=Math.round(((w=(h=e==null?void 0:e.SEVERE)==null?void 0:h.sigWave)==null?void 0:w.number)*1.6*100)/100,i=(j=(Y=e==null?void 0:e.SEVERE)==null?void 0:Y.sigWave)==null?void 0:j.number,d=(g=(S=e==null?void 0:e.HEAVY)==null?void 0:S.sigWave)==null?void 0:g.number,c=Math.round((((T=(v=e==null?void 0:e.SEVERE)==null?void 0:v.wind)==null?void 0:T.number)+2)*100)/100,f=(D=(m=e==null?void 0:e.SEVERE)==null?void 0:m.wind)==null?void 0:D.number,u=(P=(A=e==null?void 0:e.HEAVY)==null?void 0:A.wind)==null?void 0:P.number;for(let $=0;$<(p==null?void 0:p.length);$++){const b=p[$],E=(N=(q=b==null?void 0:b.meteo)==null?void 0:q.wave)==null?void 0:N.sig,H=(x=b==null?void 0:b.meteo)==null?void 0:x.wind,C=$?l(b.eta).diff(l(p[$-1].eta),"hour",!0):0;t=C>t?C:t,V.info("[%s] check sig.wave: %j",a.requestId,{...E,dgThd4Wv:r,svThd4Wv:i,hvThd4Wv:d}),(E==null?void 0:E.height)>=r?b.isDangerous=!0:(E==null?void 0:E.height)>=i?b.isSevere=!0:(E==null?void 0:E.height)>=d&&(b.isHeavy=!0),V.info("[%s] check wind: %j",a.requestId,{...H,dgThd4Wd:c,svThd4Wd:f,hvThd4Wd:u}),(H==null?void 0:H.scale)>=c?(b.isDangerous=!0,delete b.isSevere,delete b.isHeavy):(H==null?void 0:H.scale)>f?(b.isDangerous||(b.isSevere=!0),delete b.isHeavy):(H==null?void 0:H.scale)===u&&!b.isDangerous&&!b.isSevere&&(b.isHeavy=!0),n+=b.isDangerous?C:0,o+=b.isSevere?C:0,s+=b.isHeavy?C:0}return n=Math.round(n*100)/100,o=Math.round(o*100)/100,s=Math.round(s*100)/100,t=Math.round(t),{sample:p,dangerous:n,severe:o,heavy:s,step:t<3?3:t,wind:{dgThd4Wd:c,svThd4Wd:f,hvThd4Wd:u},sig:{dgThd4Wv:r,svThd4Wv:i,hvThd4Wv:d}}}}const X=new K;M.AISImpl=R,M.AlertHelper=K,M.AlertLevel=F,M.HifleetImpl=U,M.MyShipImpl=Q,M.MyVesselImpl=B,M.ShipxyImpl=J,M.alertHelper=X,Object.defineProperty(M,Symbol.toStringTag,{value:"Module"})});
1
+ (function(M,y){typeof exports=="object"&&typeof module<"u"?y(exports,require("got"),require("moment")):typeof define=="function"&&define.amd?define(["exports","got","moment"],y):(M=typeof globalThis<"u"?globalThis:M||self,y(M["idm-plugin-rabbitmq"]={},M.got,M.moment))})(this,function(M,y,l){"use strict";var Z=Object.defineProperty;var tt=(M,y,l)=>y in M?Z(M,y,{enumerable:!0,configurable:!0,writable:!0,value:l}):M[y]=l;var _=(M,y,l)=>(tt(M,typeof y!="symbol"?y+"":y,l),l);function z(k){return k&&k.__esModule&&Object.prototype.hasOwnProperty.call(k,"default")?k.default:k}class W{log(){}isLevelEnabled(){return!1}addContext(){}removeContext(){}clearContext(){}}["Trace","Debug","Info","Warn","Error","Fatal","Mark"].forEach(k=>{W.prototype[k.toLowerCase()]=()=>{},W.prototype[`is${k}Enabled`]=()=>!1});const O=(()=>{try{return require("log4js")}catch{return null}})();var G={getLogger:O?O.getLogger:()=>new W};const L=z(G),I=L.getLogger();class R{parseStatus(p){let e,a;switch(p){case 0:e="在航(主机推动)",a="The engine is in use";break;case 1:e="锚泊",a="Anchored";break;case 2:e="失控",a="Not operated";break;case 3:e="操纵受限",a="Limited airworthiness";break;case 4:e="吃水受限",a="Limited by ship's draft";break;case 5:e="靠泊",a="Mooring";break;case 6:e="搁浅",a="Stranded";break;case 7:e="捕捞作业",a="Engaged in fishing";break;case 8:e="靠帆船提供动力",a="Sailing";break;default:e="未定义",a="Undefined"}return{labelCn:e,labelEn:a}}}class B extends R{constructor(e,a){super();_(this,"clientId");_(this,"clientSecret");_(this,"token");this.clientId=e,this.clientSecret=a}async authToken(e={}){const a="https://svc.data.myvessel.cn/ada/oauth/token",n={searchParams:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},o=await y.post(a,n).json();I.info("[%s] fetch access token from: %s - %j",e.requestId,a,o),o.error||(this.token={accessToken:o.access_token,tokenType:o.token_type,expiresIn:o.expires_in,scope:o.scope,jti:o.jti,issuedAt:l().utc().format()})}async realTimePosition(e,a={}){var d,c,f;(!this.token||l().diff(l(this.token.issuedAt),"seconds")>((d=this.token)==null?void 0:d.expiresIn)-300)&&await this.authToken(a);const n="https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit",o={headers:{Authorization:`${(c=this.token)==null?void 0:c.tokenType} ${(f=this.token)==null?void 0:f.accessToken}`},searchParams:{mmsi:e}};I.info("[%s] fetch realtime position from: %s - %j",a.requestId,n,o);const s=await y.get(n,o).json();if(s.code)return I.warn("[%s] fetch realtime position failed: %j",a.requestId,n,{message:s.message,status:s.status,code:s.code}),s;const t=s.data;for(const u in t)!isNaN(t[u])&&Number(t[u])!==1/0&&(t[u]=Number(t[u]));const r=l(`${t.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:t.mmsi,name:t.vesselName,imo:t.imo,callSign:t.callsign,lat:t.lat,lng:t.lon,length:t.length,width:t.width,draught:t.currDraught,sog:t.sog,cog:t.cog,hdg:t.hdg,rot:t.rot,eta:t.eta,destination:t.dest,positionTime:r.unix(),status:t.status,labelCn:t.statusNameCn,labelEn:t.statusNameEn,method:"position",vendor:"myVessel",utc:r.utc().format()}}async trajectory(e,a,n,o,s=!0,t={}){(!this.token||l().diff(l(this.token.issuedAt),"seconds")>this.token.expiresIn-300)&&await this.authToken(t);const r=await this.realTimePosition(e,t),i=l(a),d=l(n),c=[];for(;d.diff(i,"day",!0)>30;)await this.trajectoryIn30Day(e,i,i.clone().add(30,"day"),r,o,c,t),i.add(30,"day");return await this.trajectoryIn30Day(e,i,d,r,o,c,t),c}async trajectoryIn30Day(e,a,n,o,s,t,r={}){var h,w,Y,j,S;const i="https://svc.data.myvessel.cn/sdc/v1/vessels/status/track",d={headers:{Authorization:`${(h=this.token)==null?void 0:h.tokenType} ${(w=this.token)==null?void 0:w.accessToken}`},json:{mmsi:e,startTime:a.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),endTime:n.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")}};I.info("[%s] fetch trajectory from: %s - %j",r.requestId,i,d);const c=await y.post(i,d).json();if(c.code)return I.warn("[%s] fetch trajectory failed: %j",r.requestId,i,{message:c.message,status:c.status,code:c.code}),c;let f=-1;const u=l(`${(j=(Y=c.data)==null?void 0:Y[0])==null?void 0:j.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return(S=c.data)==null||S.forEach(g=>{for(const N in g)!isNaN(g[N])&&Number(g[N])!==1/0&&(g[N]=Number(g[N]));const v=l(`${g.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),T=g.eta?l(`${g.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"):void 0,m=g.status,{labelCn:D,labelEn:A}=this.parseStatus(m),P={mmsi:g.mmsi,imo:o==null?void 0:o.imo,lat:g.lat,lng:g.lon,sog:g.sog,cog:g.cog,hdg:g.hdg,draught:g.draught,status:m,eta:T==null?void 0:T.unix(),destination:g.dest,positionTime:v.unix(),labelCn:D,labelEn:A,method:"trajectory",vendor:"myVessel",utc:v.utc().format()},q=Math.floor(v.diff(u,"minute",!0)/(s||1));q!==f&&(f=q,t.push(P))}),t}}class U extends R{constructor(e){super();_(this,"token");this.token=e}async realTimePosition(e,a={}){const n="https://api.hifleet.com/position/position/get/token",o={searchParams:{mmsi:e,usertoken:this.token}},s=await y.post(n,o).json();I.info("[%s] fetch realtime position from: %s - %j",a.requestId,n,o);const t=s==null?void 0:s.list;if(!t)return I.warn("[%s] fetch realtime position failed: %j",a.requestId,n,s),s;for(const u in t)!isNaN(t[u])&&Number(t[u])!==1/0&&(t[u]=Number(t[u]));t.status=t.sp>3?0:1;const r=t.status,{labelCn:i,labelEn:d}=this.parseStatus(r),c=l(`${t.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:t.m,name:t.n,imo:t.imonumber,callSign:t.callsign,lat:Math.round(t.la/60*1e5)/1e5,lng:Math.round(t.lo/60*1e5)/1e5,length:t.l,width:t.w,draught:t.draught,sog:t.sp,cog:t.co,hdg:t.h,rot:isNaN(t.rot)?0:t.rot,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(t.eta)?l(`${t.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00").unix():void 0,destination:t.destination,positionTime:c.unix(),utc:c.utc().format(),status:r,labelCn:i,labelEn:d,method:"position",vendor:"hifleet"}}async search(e,a={}){let n="https://www.hifleet.com/hifleetapi/searchVesselOL.do";const o={searchParams:{keyword:e},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}};let s=await y.post(n,o).json();I.info("[%s] fetch vessel props from: %s - %j",a.requestId,n,o),s instanceof Array&&(s=s[0]);for(const r in s)!isNaN(s[r])&&Number(s[r])!==1/0&&(s[r]=Number(s[r]));const t={mmsi:s.m,name:s.n,imo:s.i,callSign:s.c,length:s.l,breadth:s.b,draught:s.dr};return n="https://www.hifleet.com/hifleetapi/sameShipSearch.do",s=await y.post(n,o).json(),I.info("[%s] fetch vessel dead weight from: %s - %j",a.requestId,n,o),s instanceof Array&&(s=s[0]),s&&(t.deadweight=Number(s.dwt)),t}async trajectory(e,a,n,o,s=!0,t={}){var g,v,T;const r=await this.realTimePosition(e,t);let i=l(a);const d=l(n),c=l();if(s){let m=d.diff(i,"d",!0);m<0?i=d.clone().subtract(40,"d"):m<30?i.subtract(10,"d"):m<60?i.subtract(5,"d"):i=d.clone().subtract(80,"d"),m=c.diff(d,"d",!0),d.add(m>10?240:m*24,"h")}const f={searchParams:{endtime:d.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),starttime:i.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),mmsi:e,usertoken:this.token}},u="https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token",h=await y.get(u,f).json();I.info("[%s] fetch trajectory from: %s - %j",t.requestId,u,f);let w;h&&(w=((v=(g=h.ships)==null?void 0:g.offors)==null?void 0:v.ship)||[],w.length||I.warn("[%s] fetch trajectory failed: %j",t.requestId,h));const Y=[];let j=-1;const S=l(`${(T=w==null?void 0:w[0])==null?void 0:T.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");for(const m of w){for(const x in m)!isNaN(m[x])&&Number(m[x])!==1/0&&(m[x]=Number(m[x]));const D=l(`${m.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");m.status=m.sp>4?0:1;const{labelEn:A,labelCn:P}=this.parseStatus(m.status),q={mmsi:m.m,name:m.n,imo:r==null?void 0:r.imo,lat:m.la,lng:m.lo,draught:m.draught,sog:m.sp,cog:m.co,hdg:m.hdg,positionTime:D.unix(),utc:D.utc().format(),status:m.status,labelCn:P,labelEn:A,method:"trajectory",vendor:"hifleet"},N=Math.floor(D.diff(S,"minute",!0)/(o||1));N!==j&&(j=N,Y.push(q))}return Y}}class J extends R{constructor(e){super();_(this,"token");this.token=e}async realTimePosition(e,a={}){const n={searchParams:{id:e,k:this.token,enc:1}},o="https://api.shipxy.com/apicall/GetSingleShip",s=await y.get(o,n).json();if(I.info("[%s] fetch realtime position from: %s - %j",a.requestId,o,n),(s==null?void 0:s.status)!==0)return s;const t=s.data[0];for(const f in t)!isNaN(t[f])&&Number(t[f])!==1/0&&(t[f]=Number(t[f]));const{labelCn:r,labelEn:i}=await this.parseStatus(t.navistat),d=l.unix(t.lasttime);return{mmsi:t.ShipID,name:t.name,imo:t.imo,callSign:t.callsign,lat:Math.round(t.lat/1e6*1e5)/1e5,lng:Math.round(t.lon/1e6*1e5)/1e5,length:Math.round(t.length/10*100)/100,width:Math.round(t.width/10*100)/100,draught:Math.round(t.draught/1e3*100)/100,sog:Math.round(t.sog*3600/1e3/1852*100)/100,cog:Math.round(t.cog/100*100)/100,hdg:Math.round(t.hdg/100*100)/100,rot:Math.round(t.rot/100*100)/100,positionTime:t.lasttime,utc:d.utc().format(),status:t.navistat,labelEn:i,labelCn:r,method:"position",vendor:"shipxy"}}async trajectory(e,a,n,o,s=!0,t={}){var S;const r=await this.realTimePosition(e,t),i=l(a),d=l(n),c="https://api.shipxy.com/apicall/GetShipTrack",f={searchParams:{id:e,k:this.token,enc:1,cut:0,btm:i.unix(),etm:d.unix()}},u=await y.get(c,f).json();if(I.info("[%s] fetch trajectory from: %s - %j",t.requestId,c,f),(u==null?void 0:u.status)!==0)return u;const h=u==null?void 0:u.points,w=[],Y=l.unix((S=h[0])==null?void 0:S.utc);let j=-1;for(const g of h){const v=l.unix(g.utc),T={imo:r==null?void 0:r.imo,mmsi:e,sog:Math.round(g.sog*3600/1e3/1852*100)/100,cog:Math.round(g.cog/100*100)/100,lat:Math.round(g.lat/1e6*1e5)/1e5,lng:Math.round(g.lon/1e6*1e5)/1e5,positionTime:v.unix(),utc:v.utc().format(),method:"trajectory",vendor:"shipxy"},m=Math.floor(v.diff(Y,"minute",!0)/(o||1));m!==j&&(j=m,w.push(T))}return w}}class Q extends R{constructor(e){super();_(this,"token");this.token=e}async getShipId(e,a={}){const n={headers:{appKey:this.token},json:{mmsiList:e}},o="https://api3.myships.com/sp/ships/getShipIdByMMSI",s=await y.post(o,n).json();return I.info("[%s] fetch ship id from: %s - %j",a.requestId,o,n),s.code!=="0"?s:s.data[0].shipId}async getShipInfo(e,a={}){const n={headers:{appKey:this.token},json:{shipId:e}},o="https://api3.myships.com/sp/ships/aissta",s=await y.post(o,n).json();if(I.info("[%s] fetch ship info from: %s - %j",a.requestId,o,n),s.code!=="0")return s;const t=s.data;let r=t.imo;return e==="407170"&&(r="9198379",I.warn("[%s] ship(%s) imo error: %s, should be %s",a.requestId,e,t.imo,r)),{mmsi:t.mmsi,name:t.shipnameEn,imo:r,callSign:t.callSign,length:t.length,width:t.breadth,draught:(t.draught||100)/10}}async realTimePosition(e,a={}){const n=await this.getShipId(e,a),o=await this.getShipInfo(n,a),s={headers:{appKey:this.token},json:{shipId:n}},t="https://api3.myships.com/sp/ships/position/latest",r=await y.post(t,s).json();I.info("[%s] fetch realtime position from: %s - %j",a.requestId,t,s);const i=r.data[0];for(const h in i)!isNaN(i[h])&&Number(i[h])!==1/0&&(i[h]=Number(i[h]));const{labelCn:d,labelEn:c}=await this.parseStatus(i.aisNavStatus),f=l.unix(i.posTime);return{...o,mmsi:e,lat:Math.round(i.lat/1e4/60*1e5)/1e5,lng:Math.round(i.lon/1e4/60*1e5)/1e5,sog:Math.round(i.sog/10*100)/100,cog:Math.round(i.cog/10*100)/100,hdg:Math.round(i.heading*100)/100,rot:Math.round(i.rot*100)/100,positionTime:i.posTime,utc:f.utc().format(),status:i.aisNavStatus,labelEn:c,labelCn:d,method:"position",vendor:"myship"}}async trajectory(e,a,n,o,s=!0,t={}){const r=l(a),i=l(n),d=await this.getShipId(e),c=await this.getShipInfo(d),f=[];for(;i.diff(r,"day",!0)>30;)await this.trajectoryIn30Day(d,r.unix(),r.add(30,"day").unix(),c,e,o,f);return await this.trajectoryIn30Day(d,r.unix(),i.unix(),c,e,o,f),f}async trajectoryIn30Day(e,a,n,o,s,t,r,i={}){var Y;const d={headers:{appKey:this.token},json:{shipId:e,startTime:a,endTime:n}},c="https://api3.myships.com/sp/ships/position/history",f=await y.post(c,d).json();if(I.info("[%s] fetch trajectory from: %s - %j",i.requestId,c,d),f.code!=="0")return I.warn("[%s] invoke myship trajectory failed: %j",i.requestId,f),f;const u=f.data;for(const j in u)!isNaN(u[j])&&Number(u[j])!==1/0&&(u[j]=Number(u[j]));const h=l.unix((Y=u[0])==null?void 0:Y.posTime);let w=-1;for(const j of u){const S=l.unix(j.posTime),g={imo:o==null?void 0:o.imo,mmsi:s,lat:Math.round(j.lat/1e4/60*1e5)/1e5,lng:Math.round(j.lon/1e4/60*1e5)/1e5,sog:Math.round(j.sog/10*100)/100,cog:Math.round(j.cog/10*100)/100,hdg:Math.round(j.heading*100)/100,rot:Math.round(j.rot*100)/100,positionTime:S.unix(),utc:S.utc().format(),method:"trajectory",vendor:"myship"},v=Math.floor(S.diff(h,"minute",!0)/(t||1));v!==w&&(w=v,r.push(g))}return r}}const V=L.getLogger();var F=(k=>(k.NOTICE="NOTICE",k.WARN="WARN",k.HEAVY="HEAVY",k.SEVERE="SEVERE",k.ERROR="ERROR",k.FATAL="FATAL",k))(F||{});class K{parsePrinciple(p,e={}){var t,r,i;V.info("[%s] parse rule: %s",e.requestId,p);const a=new RegExp("(?<=\\[)(.+)(?=\\])","g"),n=p.match(a)?(t=p.match(a))==null?void 0:t[0]:void 0,o=n==null?void 0:n.split(";");if(!o)return;const s={};for(let d=0;d<(o==null?void 0:o.length);d++){const c=(i=(r=o[d].match(a))==null?void 0:r[0])==null?void 0:i.split("],");if(d===0&&!c)s.scope=o[0];else if(c)for(let f=0,u=c.length;f<u;f++){const h=this.parseRule(c[f]);h&&(s[h.level]?h.key?s[h.level][h==null?void 0:h.key]=h:s[h.level]=h:h.key?s[h.level]={[h==null?void 0:h.key]:h}:s[h.level]=h)}}return s}parseRule(p,e={}){var s;V.info("[%s] parse rule: %s",e.requestId,p),p=p.startsWith("[")?p:`[${p}`,p=p.endsWith("]")?p:`${p}]`;const a=new RegExp("(?<=\\[)(.+?)(?=\\])","g"),n=(s=p==null?void 0:p.match(a))==null?void 0:s[0],o=n==null?void 0:n.split(",");if(o)return{operator:o[0],number:Number(o[1]),level:o[2],time:Number(o[3]),key:o[4]}}checkWeather(p,e,a={}){var h,w,Y,j,S,g,v,T,m,D,A,P,q,N,x;let n=0,o=0,s=0,t=0;const r=Math.round(((w=(h=e==null?void 0:e.SEVERE)==null?void 0:h.sigWave)==null?void 0:w.number)*1.6*100)/100,i=(j=(Y=e==null?void 0:e.SEVERE)==null?void 0:Y.sigWave)==null?void 0:j.number,d=(g=(S=e==null?void 0:e.HEAVY)==null?void 0:S.sigWave)==null?void 0:g.number,c=Math.round((((T=(v=e==null?void 0:e.SEVERE)==null?void 0:v.wind)==null?void 0:T.number)+2)*100)/100,f=(D=(m=e==null?void 0:e.SEVERE)==null?void 0:m.wind)==null?void 0:D.number,u=(P=(A=e==null?void 0:e.HEAVY)==null?void 0:A.wind)==null?void 0:P.number;for(let $=0;$<(p==null?void 0:p.length);$++){const b=p[$],E=(N=(q=b==null?void 0:b.meteo)==null?void 0:q.wave)==null?void 0:N.sig,H=(x=b==null?void 0:b.meteo)==null?void 0:x.wind,C=$?l(b.eta).diff(l(p[$-1].eta),"hour",!0):0;t=C>t?C:t,V.info("[%s] check sig.wave: %j",a.requestId,{...E,dgThd4Wv:r,svThd4Wv:i,hvThd4Wv:d}),(E==null?void 0:E.height)>=r?b.isDangerous=!0:(E==null?void 0:E.height)>=i?b.isSevere=!0:(E==null?void 0:E.height)>=d&&(b.isHeavy=!0),V.info("[%s] check wind: %j",a.requestId,{...H,dgThd4Wd:c,svThd4Wd:f,hvThd4Wd:u}),(H==null?void 0:H.scale)>=c?(b.isDangerous=!0,delete b.isSevere,delete b.isHeavy):(H==null?void 0:H.scale)>f?(b.isDangerous||(b.isSevere=!0),delete b.isHeavy):(H==null?void 0:H.scale)===u&&!b.isDangerous&&!b.isSevere&&(b.isHeavy=!0),n+=b.isDangerous?C:0,o+=b.isSevere?C:0,s+=b.isHeavy?C:0}return n=Math.round(n*100)/100,o=Math.round(o*100)/100,s=Math.round(s*100)/100,t=Math.round(t),{sample:p,dangerous:n,severe:o,heavy:s,step:t<3?3:t,wind:{dgThd4Wd:c,svThd4Wd:f,hvThd4Wd:u},sig:{dgThd4Wv:r,svThd4Wv:i,hvThd4Wv:d}}}}const X=new K;M.AISImpl=R,M.AlertHelper=K,M.AlertLevel=F,M.HifleetImpl=U,M.MyShipImpl=Q,M.MyVesselImpl=B,M.ShipxyImpl=J,M.alertHelper=X,Object.defineProperty(M,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@idm-plugin/vessel",
3
3
  "private": false,
4
- "version": "1.1.1",
4
+ "version": "1.1.3",
5
5
  "description": "idm plugin for vessel",
6
6
  "type": "module",
7
7
  "keywords": [