@idm-plugin/vessel 1.5.9 → 1.6.1

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.
@@ -1 +1 @@
1
- (function(N,F){typeof exports=="object"&&typeof module<"u"?F(exports,require("got"),require("@log4js-node/log4js-api"),require("moment"),require("@idm-plugin/geo"),require("@idm-plugin/meteo2"),require("@idm-plugin/meteo")):typeof define=="function"&&define.amd?define(["exports","got","@log4js-node/log4js-api","moment","@idm-plugin/geo","@idm-plugin/meteo2","@idm-plugin/meteo"],F):(N=typeof globalThis<"u"?globalThis:N||self,F(N["idm-plugin-rabbitmq"]={},N.got,N["@log4js-node/log4js-api"],N.moment,N["@idm-plugin/geo"],N["@idm-plugin/meteo2"],N["@idm-plugin/meteo"]))})(this,function(N,F,K,p,R,X,$){"use strict";var mt=Object.defineProperty;var yt=(N,F,K)=>F in N?mt(N,F,{enumerable:!0,configurable:!0,writable:!0,value:K}):N[F]=K;var _=(N,F,K)=>(yt(N,typeof F!="symbol"?F+"":F,K),K);let M;try{M=K.getLogger("vessel")}catch{}finally{}class G{parseStatus(e){let t,o;switch(e){case 0:t="在航(主机推动)",o="The engine is in use";break;case 1:t="锚泊",o="Anchored";break;case 2:t="失控",o="Not operated";break;case 3:t="操纵受限",o="Limited airworthiness";break;case 4:t="吃水受限",o="Limited by ship's draft";break;case 5:t="靠泊",o="Mooring";break;case 6:t="搁浅",o="Stranded";break;case 7:t="捕捞作业",o="Engaged in fishing";break;case 8:t="靠帆船提供动力",o="Sailing";break;default:t="未定义",o="Undefined"}return{labelCn:t,labelEn:o}}}class rt extends G{constructor(t,o){super();_(this,"clientId");_(this,"clientSecret");_(this,"token");this.clientId=t,this.clientSecret=o}async authToken(t={}){const o="https://svc.data.myvessel.cn/ada/oauth/token",i={searchParams:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},a=await F.post(o,i).json();M==null||M.info("[%s] fetch access token from: %s - %j",t.requestId,o,a),a.error||(this.token={accessToken:a.access_token,tokenType:a.token_type,expiresIn:a.expires_in,scope:a.scope,jti:a.jti,issuedAt:p().utc().format()})}async realTimePosition(t,o={}){var l,f,m;(!this.token||p().diff(p(this.token.issuedAt),"seconds")>((l=this.token)==null?void 0:l.expiresIn)-300)&&await this.authToken(o);const i="https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit",a={headers:{Authorization:`${(f=this.token)==null?void 0:f.tokenType} ${(m=this.token)==null?void 0:m.accessToken}`},searchParams:{mmsi:t}};M==null||M.info("[%s] fetch realtime position from: %s - %j",o.requestId,i,a);const n=await F.get(i,a).json();if(n.code)return M==null||M.warn("[%s] fetch realtime position failed: %j",o.requestId,i,{message:n.message,status:n.status,code:n.code}),n;const s=n.data;for(const h in s)!isNaN(s[h])&&Number(s[h])!==1/0&&(s[h]=Number(s[h]));const d=p(`${s.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:s.mmsi,name:s.vesselName,imo:s.imo,callSign:s.callsign,lat:s.lat,lng:s.lon,length:s.length,width:s.width,draught:s.currDraught,sog:s.sog,cog:s.cog,hdg:s.hdg,rot:s.rot,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(s.eta)?p.utc(s.eta).format():void 0,destination:s.dest,positionTime:d.unix(),status:s.status,labelCn:s.statusNameCn,labelEn:s.statusNameEn,method:"position",vendor:"myVessel",utc:d.utc().format()}}async trajectory(t,o,i,a,n=!0,s={}){(!this.token||p().diff(p(this.token.issuedAt),"seconds")>this.token.expiresIn-300)&&await this.authToken(s);const d=await this.realTimePosition(t,s),r=p(o),l=p(i),f=[];for(;l.diff(r,"day",!0)>30;)await this.trajectoryIn30Day(t,r,r.clone().add(30,"day"),d,a,f,s),r.add(30,"day");return await this.trajectoryIn30Day(t,r,l,d,a,f,s),f}async trajectoryIn30Day(t,o,i,a,n,s,d={}){var u,I,g,y,w;const r="https://svc.data.myvessel.cn/sdc/v1/vessels/status/track",l={headers:{Authorization:`${(u=this.token)==null?void 0:u.tokenType} ${(I=this.token)==null?void 0:I.accessToken}`},json:{mmsi:t,startTime:o.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),endTime:i.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")}};M==null||M.info("[%s] fetch trajectory from: %s - %j",d.requestId,r,l);const f=await F.post(r,l).json();if(f.code)return M==null||M.warn("[%s] fetch trajectory failed: %j",d.requestId,r,{message:f.message,status:f.status,code:f.code}),f;let m=-1;const h=p(`${(y=(g=f.data)==null?void 0:g[0])==null?void 0:y.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return(w=f.data)==null||w.forEach(c=>{for(const O in c)!isNaN(c[O])&&Number(c[O])!==1/0&&(c[O]=Number(c[O]));const v=p(`${c.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),Y=c.status,{labelCn:b,labelEn:j}=this.parseStatus(Y),S={mmsi:c.mmsi,imo:a==null?void 0:a.imo,lat:c.lat,lng:c.lon,sog:c.sog,cog:c.cog,hdg:c.hdg,draught:c.draught,status:Y,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(c.eta)?p(`${c.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00").utc().format():void 0,destination:c.dest,positionTime:v.unix(),labelCn:b,labelEn:j,method:"trajectory",vendor:"myVessel",utc:v.utc().format()},L=Math.floor(v.diff(h,"minute",!0)/(n||1));L!==m&&(m=L,s.push(S))}),s}}class ct extends G{constructor(t){super();_(this,"token");this.token=t}async realTimePosition(t,o={}){const i="https://api.hifleet.com/position/position/get/token",a={searchParams:{mmsi:t,usertoken:this.token}},n=await F.post(i,a).json();M==null||M.info("[%s] fetch realtime position from: %s - %j",o.requestId,i,a);const s=n==null?void 0:n.list;if(!s)return M==null||M.warn("[%s] fetch realtime position failed: %j",o.requestId,i,n),n;for(const h in s)!isNaN(s[h])&&Number(s[h])!==1/0&&(s[h]=Number(s[h]));s.status=s.sp>3?0:1;const d=s.status,{labelCn:r,labelEn:l}=this.parseStatus(d),f=p(`${s.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:s.m,name:s.n,imo:s.imonumber,callSign:s.callsign,lat:Math.round(s.la/60*1e5)/1e5,lng:Math.round(s.lo/60*1e5)/1e5,length:s.l,width:s.w,draught:s.draught,sog:s.sp,cog:s.co,hdg:s.h,rot:isNaN(s.rot)?0:s.rot,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(s.eta)?p.utc(s.eta).format():void 0,destination:s.destination,positionTime:f.unix(),utc:f.utc().format(),status:d,labelCn:r,labelEn:l,method:"position",vendor:"hifleet"}}async search(t,o={}){let i="https://www.hifleet.com/hifleetapi/searchVesselOL.do";const a={searchParams:{keyword:t},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}};let n=await F.post(i,a).json();M==null||M.info("[%s] fetch vessel props from: %s - %j",o.requestId,i,a),n instanceof Array&&(n=n[0]);for(const d in n)!isNaN(n[d])&&Number(n[d])!==1/0&&(n[d]=Number(n[d]));const s={mmsi:n.m,name:n.n,imo:n.i,callSign:n.c,length:n.l,breadth:n.b,draught:n.dr,type:n.t};return i="https://www.hifleet.com/hifleetapi/sameShipSearch.do",n=await F.post(i,a).json(),M==null||M.info("[%s] search vessel dead weight from: %s - %j",o.requestId,i,a),n instanceof Array&&(n=n[0]),n&&(s.deadweight=Number(n.dwt)),s}async suggest(t,o={}){const i="https://www.hifleet.com/hifleetapi/getShipSuggest.do",a={searchParams:{q:t},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}},n=await F.post(i,a).json();M==null||M.info("[%s] suggest vessel props from: %s - %j",o.requestId,i,a);const s=[];for(const d of n)s.push({mmsi:!d.mmsi||isNaN(d.mmsi)?null:Number(d.mmsi),name:d.name,callSign:d.callsign,imo:!d.imo||isNaN(d.imo)?null:Number(d.imo),score:d._score});return s.sort((d,r)=>r.score-d.score),s}async trajectory(t,o,i,a,n=!0,s={}){var c,v,Y;const d=await this.realTimePosition(t,s);let r=p(o);const l=p(i),f=p();if(n){let b=l.diff(r,"d",!0);b<0?r=l.clone().subtract(40,"d"):b<30?r.subtract(10,"d"):b<60?r.subtract(5,"d"):r=l.clone().subtract(80,"d"),b=f.diff(l,"d",!0),l.add(b>10?240:b*24,"h")}const m={searchParams:{endtime:l.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),starttime:r.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),mmsi:t,usertoken:this.token}},h="https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token",u=await F.get(h,m).json();M==null||M.info("[%s] fetch trajectory from: %s - %j",s.requestId,h,m);let I;u&&(I=((v=(c=u.ships)==null?void 0:c.offors)==null?void 0:v.ship)||[],I.length||M==null||M.warn("[%s] fetch trajectory failed: %j",s.requestId,u));const g=[];let y=-1;const w=p(`${(Y=I==null?void 0:I[0])==null?void 0:Y.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");for(const b of I){for(const E in b)!isNaN(b[E])&&Number(b[E])!==1/0&&(b[E]=Number(b[E]));const j=p(`${b.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");b.status=b.sp>4?0:1;const{labelEn:S,labelCn:L}=this.parseStatus(b.status),O={mmsi:b.m,name:b.n,imo:d==null?void 0:d.imo,lat:b.la,lng:b.lo,draught:b.draught,sog:b.sp,cog:b.co,hdg:b.hdg,positionTime:j.unix(),utc:j.utc().format(),status:b.status,labelCn:L,labelEn:S,method:"trajectory",vendor:"hifleet"},W=Math.floor(j.diff(w,"minute",!0)/(a||1));W!==y&&(y=W,g.push(O))}return g}}class dt extends G{constructor(t){super();_(this,"token");this.token=t}async realTimePosition(t,o={}){const i={searchParams:{id:t,k:this.token,enc:1}},a="https://api.shipxy.com/apicall/GetSingleShip",n=await F.get(a,i).json();if(M==null||M.info("[%s] fetch realtime position from: %s - %j",o.requestId,a,i),(n==null?void 0:n.status)!==0)return n;const s=n.data[0];for(const m in s)!isNaN(s[m])&&Number(s[m])!==1/0&&(s[m]=Number(s[m]));const{labelCn:d,labelEn:r}=await this.parseStatus(s.navistat),l=p.unix(s.lasttime);return{mmsi:s.ShipID,name:s.name,imo:s.imo,callSign:s.callsign,lat:Math.round(s.lat/1e6*1e5)/1e5,lng:Math.round(s.lon/1e6*1e5)/1e5,length:Math.round(s.length/10*100)/100,width:Math.round(s.width/10*100)/100,draught:Math.round(s.draught/1e3*100)/100,sog:Math.round(s.sog*3600/1e3/1852*100)/100,cog:Math.round(s.cog/100*100)/100,hdg:Math.round(s.hdg/100*100)/100,rot:Math.round(s.rot/100*100)/100,positionTime:s.lasttime,utc:l.utc().format(),status:s.navistat,labelEn:r,labelCn:d,method:"position",vendor:"shipxy"}}async trajectory(t,o,i,a,n=!0,s={}){var w;const d=await this.realTimePosition(t,s),r=p(o),l=p(i),f="https://api.shipxy.com/apicall/GetShipTrack",m={searchParams:{id:t,k:this.token,enc:1,cut:0,btm:r.unix(),etm:l.unix()}},h=await F.get(f,m).json();if(M==null||M.info("[%s] fetch trajectory from: %s - %j",s.requestId,f,m),(h==null?void 0:h.status)!==0)return h;const u=h==null?void 0:h.points,I=[],g=p.unix((w=u[0])==null?void 0:w.utc);let y=-1;for(const c of u){const v=p.unix(c.utc),Y={imo:d==null?void 0:d.imo,mmsi:t,sog:Math.round(c.sog*3600/1e3/1852*100)/100,cog:Math.round(c.cog/100*100)/100,lat:Math.round(c.lat/1e6*1e5)/1e5,lng:Math.round(c.lon/1e6*1e5)/1e5,positionTime:v.unix(),utc:v.utc().format(),method:"trajectory",vendor:"shipxy"},b=Math.floor(v.diff(g,"minute",!0)/(a||1));b!==y&&(y=b,I.push(Y))}return I}}class ut extends G{constructor(t){super();_(this,"token");this.token=t}async getShipId(t,o={}){const i={headers:{appKey:this.token},json:{mmsiList:t}},a="https://api3.myships.com/sp/ships/getShipIdByMMSI",n=await F.post(a,i).json();return M==null||M.info("[%s] fetch ship id from: %s - %j",o.requestId,a,i),n.code!=="0"?n:n.data[0].shipId}async getShipInfo(t,o={}){const i={headers:{appKey:this.token},json:{shipId:t}},a="https://api3.myships.com/sp/ships/aissta",n=await F.post(a,i).json();if(M==null||M.info("[%s] fetch ship info from: %s - %j",o.requestId,a,i),n.code!=="0")return n;const s=n.data;let d=s.imo;return t==="407170"&&(d="9198379",M==null||M.warn("[%s] ship(%s) imo error: %s, should be %s",o.requestId,t,s.imo,d)),{mmsi:s.mmsi,name:s.shipnameEn,imo:d,callSign:s.callSign,length:s.length,width:s.breadth,draught:(s.draught||100)/10}}async realTimePosition(t,o={}){const i=await this.getShipId(t,o),a=await this.getShipInfo(i,o),n={headers:{appKey:this.token},json:{shipId:i}},s="https://api3.myships.com/sp/ships/position/latest",d=await F.post(s,n).json();M==null||M.info("[%s] fetch realtime position from: %s - %j",o.requestId,s,n);const r=d.data[0];for(const u in r)!isNaN(r[u])&&Number(r[u])!==1/0&&(r[u]=Number(r[u]));const{labelCn:l,labelEn:f}=await this.parseStatus(r.aisNavStatus),m=p.unix(r.posTime);return{...a,mmsi:t,lat:Math.round(r.lat/1e4/60*1e5)/1e5,lng:Math.round(r.lon/1e4/60*1e5)/1e5,sog:Math.round(r.sog/10*100)/100,cog:Math.round(r.cog/10*100)/100,hdg:Math.round(r.heading*100)/100,rot:Math.round(r.rot*100)/100,positionTime:r.posTime,utc:m.utc().format(),status:r.aisNavStatus,labelEn:f,labelCn:l,method:"position",vendor:"myship"}}async trajectory(t,o,i,a,n=!0,s={}){const d=p(o),r=p(i),l=await this.getShipId(t),f=await this.getShipInfo(l),m=[];for(;r.diff(d,"day",!0)>30;)await this.trajectoryIn30Day(l,d.unix(),d.add(30,"day").unix(),f,t,a,m);return await this.trajectoryIn30Day(l,d.unix(),r.unix(),f,t,a,m),m}async trajectoryIn30Day(t,o,i,a,n,s,d,r={}){var g;const l={headers:{appKey:this.token},json:{shipId:t,startTime:o,endTime:i}},f="https://api3.myships.com/sp/ships/position/history",m=await F.post(f,l).json();if(M==null||M.info("[%s] fetch trajectory from: %s - %j",r.requestId,f,l),m.code!=="0")return M==null||M.warn("[%s] invoke myship trajectory failed: %j",r.requestId,m),m;const h=m.data;for(const y in h)!isNaN(h[y])&&Number(h[y])!==1/0&&(h[y]=Number(h[y]));const u=p.unix((g=h[0])==null?void 0:g.posTime);let I=-1;for(const y of h){const w=p.unix(y.posTime),c={imo:a==null?void 0:a.imo,mmsi:n,lat:Math.round(y.lat/1e4/60*1e5)/1e5,lng:Math.round(y.lon/1e4/60*1e5)/1e5,sog:Math.round(y.sog/10*100)/100,cog:Math.round(y.cog/10*100)/100,hdg:Math.round(y.heading*100)/100,rot:Math.round(y.rot*100)/100,positionTime:w.unix(),utc:w.utc().format(),method:"trajectory",vendor:"myship"},v=Math.floor(w.diff(u,"minute",!0)/(s||1));v!==I&&(I=v,d.push(c))}return d}}let V;try{V=K.getLogger("vessel")}catch{}finally{}var tt=(H=>(H.NOTICE="NOTICE",H.WARN="WARN",H.HEAVY="HEAVY",H.SEVERE="SEVERE",H.ERROR="ERROR",H.FATAL="FATAL",H))(tt||{});class et{parsePrinciple(e,t={}){var s,d,r;V==null||V.info("[%s] parse rule: %s",t.requestId,e);const o=new RegExp("(?<=\\[)(.+)(?=])","g"),i=e.match(o)?(s=e.match(o))==null?void 0:s[0]:void 0,a=i==null?void 0:i.split(";");if(!a)return;const n={};for(let l=0;l<(a==null?void 0:a.length);l++){const f=(r=(d=a[l].match(o))==null?void 0:d[0])==null?void 0:r.split("],");if(l===0&&!f)n.scope=a[0];else if(f)for(let m=0,h=f.length;m<h;m++){const u=this.parseRule(f[m]);u&&(n[u.level]?u.key?n[u.level][u==null?void 0:u.key]=u:n[u.level]=u:u.key?n[u.level]={[u==null?void 0:u.key]:u}:n[u.level]=u)}}return n}parseRule(e,t={}){var n;V==null||V.info("[%s] parse rule: %s",t.requestId,e),e=e.startsWith("[")?e:`[${e}`,e=e.endsWith("]")?e:`${e}]`;const o=new RegExp("(?<=\\[)(.+?)(?=])","g"),i=(n=e==null?void 0:e.match(o))==null?void 0:n[0],a=i==null?void 0:i.split(",");if(a)return{operator:a[0],number:Number.isNaN(Number(a[1]))?a[1]:Number(a[1]),level:a[2],time:Number(a[3]),key:a[4]}}checkWeather(e,t,o={}){var u,I,g,y,w,c,v,Y,b,j,S,L,O,W,E;let i=0,a=0,n=0,s=0;const d=Math.round(((I=(u=t==null?void 0:t.SEVERE)==null?void 0:u.sigWave)==null?void 0:I.number)*1.6*100)/100,r=(y=(g=t==null?void 0:t.SEVERE)==null?void 0:g.sigWave)==null?void 0:y.number,l=(c=(w=t==null?void 0:t.HEAVY)==null?void 0:w.sigWave)==null?void 0:c.number,f=Math.round((((Y=(v=t==null?void 0:t.SEVERE)==null?void 0:v.wind)==null?void 0:Y.number)+2)*100)/100,m=(j=(b=t==null?void 0:t.SEVERE)==null?void 0:b.wind)==null?void 0:j.number,h=(L=(S=t==null?void 0:t.HEAVY)==null?void 0:S.wind)==null?void 0:L.number;for(let B=0;B<(e==null?void 0:e.length);B++){const q=e[B],P=(W=(O=q==null?void 0:q.meteo)==null?void 0:O.wave)==null?void 0:W.sig,A=(E=q==null?void 0:q.meteo)==null?void 0:E.wind,Z=B?p(q.eta).diff(p(e[B-1].eta),"hour",!0):0;s=Z>s?Z:s,V==null||V.info("[%s] check sig.wave: %j",o.requestId,{...P,dgThd4Wv:d,svThd4Wv:r,hvThd4Wv:l}),(P==null?void 0:P.height)>=d?q.isDangerous=!0:(P==null?void 0:P.height)>=r?q.isSevere=!0:(P==null?void 0:P.height)>=l&&(q.isHeavy=!0),V==null||V.info("[%s] check wind: %j",o.requestId,{...A,dgThd4Wd:f,svThd4Wd:m,hvThd4Wd:h}),(A==null?void 0:A.scale)>=f?(q.isDangerous=!0,delete q.isSevere,delete q.isHeavy):(A==null?void 0:A.scale)>m?(q.isDangerous||(q.isSevere=!0),delete q.isHeavy):(A==null?void 0:A.scale)===h&&!q.isDangerous&&!q.isSevere&&(q.isHeavy=!0),i+=q.isDangerous?Z:0,a+=q.isSevere?Z:0,n+=q.isHeavy?Z:0}return i=Math.round(i*100)/100,a=Math.round(a*100)/100,n=Math.round(n*100)/100,s=Math.round(s),{sample:e,dangerous:i,severe:a,heavy:n,step:s<3?3:s,wind:{dgThd4Wd:f,svThd4Wd:m,hvThd4Wd:h},sig:{dgThd4Wv:d,svThd4Wv:r,hvThd4Wv:l}}}}const ht=new et;let k;try{k=K.getLogger("vessel")}catch{}finally{}const lt=new X.MeteoHelper2("",!0);var st=(H=>(H.common="common",H.container="container",H.tugs="tugs",H))(st||{}),at=(H=>(H.Ballast="Ballast",H.Laden="Laden",H))(at||{}),ot=(H=>(H.Cp="CP",H.Perf="Basis",H.Instruct="Other",H))(ot||{});class x{static blockCoefficient(e,t,o,i){let a=Math.round(e/(t*o*i)*100)/100;a=a<.55?.55:a>.85?.85:a;const n=[.55,.6,.65,.7,.75,.8,.85],s=n.map(d=>Math.abs(d-a));return n[s.indexOf(Math.min(...s))]}static froudeNumber(e,t,o=9.8){let i=Math.round(Math.sqrt(e*e/(o*t))*100)/100;return i=i<.05?.05:i>.3?.3:i,i}static amendFactor(e,t,o){const i={.55:[1.7,-1.4,-7.4],.6:[2.2,-2.5,-9.7],.65:[2.6,-3.7,-11.6],.7:[3.1,-5.3,-12.4],.75:[2.4,-10.6,-9.5],.8:[2.6,-13.1,-15.1],.85:[3.1,-18.7,28]};let n={.55:[1.7,-1.4,-7.4],.6:[2.2,-2.5,-9.7],.65:[2.6,-3.7,-11.6],.7:[3.1,-5.3,-12.4],.75:[2.6,-12.5,-13.5],.8:[3,-16.3,-21.6],.85:[3.4,-20.9,31.8]}[e];return o==="Laden"&&(n=i[e]),n[0]+n[1]*t+n[2]*Math.pow(t,2)}static directionFactor(e,t=0){let o;return e>30&&e<=60?o=(1.7-.03*Math.pow(t-4,2))/2:e>60&&e<=150?o=(.9-.06*Math.pow(t-6,2))/2:e>150&&e<=180?o=(.4-.03*Math.pow(t-8,2))/2:o=1,Math.round(o*1e5)/1e5}static vesselTagFactor(e,t,o,i=0){i=i>6?i-.9*(i-6):i;let a;return o==="container"?a=.7*i+Math.pow(i,6.5)/(22*Math.pow(e,2/3)):t==="Ballast"?a=.7*i+Math.pow(i,6.5)/(2.7*Math.pow(e,2/3)):a=.5*i+Math.pow(i,6.5)/(2.7*Math.pow(e,2/3)),a}static waveHeightFactor(e,t){e=e<0?.2:e,e=e>6?e-.9*(e-6):e,e=e>9?9:e;let o;return t>30&&t<=60?o=-.6:t>60&&t<=90?o=-.4:t>90&&t<=120?o=e<3?.4:-.3:t>120&&t<=150?o=e<3?.6:-.5:t>150&&t<=180?o=e<3?.7:-.6:o=-.7,Math.round(o*(.144*Math.pow(e,2)+.178*e)*1e4)/1e4}static assembleProperties(e,t,o,i){var m;const a=e.lbp??e.length??e.lengthOverall??198.9642,n=e.draught??8,s=e.breadthMoulded??e.breadth??e.breadthExtreme??32.4572,d=e.deadweight??67035.7773,r=((m=e==null?void 0:e.type)==null?void 0:m.toLowerCase())||"common";return{tag:r.indexOf("container")>-1?"container":r.indexOf("tugs")>-1?"tugs":"common",lbp:a,loadCondition:t,draught:n,breadthMoulded:s,displacement:Math.round((d/1.025+n*s*a*.7)*1e4)/1e4,speed:Math.round((o??14.1382)*1852/3600*1e4)/1e4,bearing:i||90}}static async speedLoseAt(e,t,o,i="",a=2,n=!0,s=!1,d={}){let r;if(t.velocity&&s&&(e.speed=R.LngLatHelper.roundPrecision(t.velocity*1852/3600,6)),n){let l;if(d.meteo2)try{const u=await lt.spotForecast(t.lat,t.lng,o.utc().format(),!1,!1,!0,d),[I]=$.Meteo2Assist.pickHourly(u,o);l=$.Meteo2Assist.toLegacy(I)}catch(u){k.warn("[%s] meteo2 spot(%j) forecast failed: %s",d.requestId,{...t,eta:o.utc().format()},u)}else l=await X.MeteoHelper.queryPointFactor(t.lng,t.lat,o.valueOf(),"wind,wave,current,watertemp",i,d);const f=x.weatherFactor(e,l),m=x.currentFactor(e.bearing,l==null?void 0:l.current,a),h=Math.round((e.speed*1.943844+f+m)*100)/100;r={meteo:{...l},wxFactor:f,cFactor:m,speed:t.velocity&&s?t.velocity:h<0?1:h,eta:o.utc().format("YYYY-MM-DDTHH:mm[Z]"),etd:o.utc().format("YYYY-MM-DDTHH:mm[Z]")}}else r={wxFactor:0,cFactor:0,speed:t.velocity&&s?t.velocity:Math.round((e.speed*1.943844+0+0)*100)/100,eta:o.utc().format("YYYY-MM-DDTHH:mm[Z]"),etd:o.utc().format("YYYY-MM-DDTHH:mm[Z]")};return delete t.meteo,delete t.wxFactor,delete t.cFactor,delete t.speed,delete t.etd,{...r,...t}}static async speedLoseInHoursStep(e,t,o,i,a,n,s="",d=!0,r=!1,l={}){t.utc();const f=t.clone().add(14,"days"),m=[],h=[];let u=0,I=0,g,y;for(let w=0;w<n.length-1;w++){let c=n[w];c.distanceFromStart=Math.round((a+I)*1e4)/1e4;const v=n[w+1];if(e.bearing=R.LaneHelper.calculateBearing(c,v,!v.gcToPrevious),c.bearing=e.bearing,c.suspend&&r){c.eta=c.eta||t.format("YYYY-MM-DDTHH:mm[Z]"),c.elapsed=c.elapsed??0;const j=c.suspend-c.elapsed;if(i-u>j)i=i-u-j,t.add(j,"hour"),c.elapsed=c.suspend;else{const S=i-u;c.elapsed+=S,t.add(S,"hour"),i=0}if(k==null||k.info(`[%s] suspend ${c.elapsed} hours at %j, and remain ${i} hours need to go...`,l.requestId,c),i===0)return c.distanceFromPrevious=I,{etd:t,from:y||c,to:c,next:n.filter(S=>S),wps:m,days:h}}else c.suspend=0;d=t.isAfter(f)?!1:d,c=await x.speedLoseAt(e,c,t,s,0,d,r,l),y=y||c,c.important&&m.push(c),t.isSameOrAfter(o)&&(h.push(c),o.add(24,"hour"));const Y=R.LaneHelper.calculateDistance(c,v,!v.gcToPrevious);let b=Math.ceil(Y/y.speed*1e4)/1e4;if(u+b<i){if(u+=b,t.add(b,"hour"),delete n[w],k==null||k.info(`[%s] go to %j from %j with ${Y}nm, and cost ${b} hours`,l.requestId,{lat:v.lat,lng:v.lng},{lat:y.lat,lng:y.lng,etd:y.etd}),I+=Y,n.filter(j=>j).length<=1){g=v,g.eta=t.format("YYYY-MM-DDTHH:mm[Z]"),g.distanceFromPrevious=Y,g.distanceFromStart=Math.round((a+I)*1e4)/1e4,m.push(g),delete n[w+1];break}}else{b=i-u,t.add(b,"hour");const j=R.LngLatHelper.roundPrecision(y.speed*b,4);g=R.LaneHelper.calculateCoordinate(c,e.bearing,j,"nauticalmiles",!v.gcToPrevious),g.eta=t.format("YYYY-MM-DDTHH:mm[Z]"),n[w]=g,k==null||k.info(`[%s] go to %j from %j with ${j}nm, and cost ${b} hours`,l.requestId,{lat:g.lat,lng:g.lng},{lat:c.lat,lng:c.lng,etd:c.etd}),I+=j,g.distanceFromPrevious=Math.round(I*1e4)/1e4,g.distanceFromStart=Math.round((a+I)*1e4)/1e4;break}}return{etd:t,from:y,to:g,next:n.filter(w=>w),wps:m,days:h}}static currentFactor(e,t,o=0){const i=(e-(t==null?void 0:t.degree)||0)/180*Math.PI;if(Math.abs(i)===Math.PI/2)return 0;let a=((t==null?void 0:t.kts)||0)*Math.cos(i);return o&2?a=Math.ceil(a*100)/100:o&1?a=Math.floor(a*100)/100:a=Math.round(a*100)/100,Math.abs(a)>5?0:a}static weatherFactor(e,t){var f,m,h,u,I,g,y;k==null||k.debug("calculate weather factor via: %j",{...e,...t});const o=x.blockCoefficient(e.displacement,e.lbp,e.breadthMoulded,e.draught),i=x.froudeNumber(e.speed,e.lbp),a=x.amendFactor(o,i,e.loadCondition);let n=Math.abs(e.bearing%360-(((f=t==null?void 0:t.wind)==null?void 0:f.degree)%360||0));n=n>180?360-n:n;const s=x.directionFactor(n,(m=t==null?void 0:t.wind)==null?void 0:m.scale),d=x.vesselTagFactor(e.displacement,e.loadCondition,e.tag,(h=t==null?void 0:t.wind)==null?void 0:h.scale);let r=s*a*d/100*e.speed;r=Math.round(r*1.943844*1e4)/1e4*-1,e.tag==="tugs"&&Math.abs(r)>1&&(r=r/(Math.abs(Math.round(r))+1)),k==null||k.debug("wind wx factor = %d",r),n=Math.abs(e.bearing%360-(((I=(u=t==null?void 0:t.wave)==null?void 0:u.sig)==null?void 0:I.degree)%360||0));const l=x.waveHeightFactor(((y=(g=t==null?void 0:t.wave)==null?void 0:g.sig)==null?void 0:y.height)??1,n);return k==null||k.debug("wave wx factor = %d",l),r=r*.4+l,k==null||k.debug("weather factor = %d",r),r=Math.abs(r)>4?4*(Math.abs(r)/r)+Math.abs(r)/r*(Math.abs(r)-4)*.1:r,Math.round((r||0)*100)/100}static async analyseInstant(e,t,o,i,a,n="",s=0,d=!0,r=!1,l={}){var Z,z,U,Q,J;const f=p().valueOf();e.lng=R.LngLatHelper.convertToStdLng(e.lng);const{route:m,waypoints:h}=a.points,u=R.LaneHelper.calculateSubRoute(e,m);if(((Z=u[0])==null?void 0:Z.length)<=1)return;const{v0:I,label:g}=e.sog?{v0:e.sog,label:"Other"}:{v0:i.speed,label:"CP"},y=x.assembleProperties(o,i.loadCondition,I,0),w=h.length?R.LaneHelper.calculateSubWaypoints(e,h):[];w.forEach(T=>T.important=!0);const c={from:{...e},route:u,waypoints:w,v0:I,label:g},v={hours:[],days:[],wps:[]};s||(R.LaneHelper.calculateRouteDistance(u)/i.speed<=72?s=3:s=6);let Y=R.LaneHelper.simplifyRouteToCoordinates(u,w,0),b=0,j=0,S=0,L=0;t=p(t).utc();const O=t.clone();for(;Y.length>0;){const T=s-t.hour()%s,D=Math.ceil(t.clone().add(T,"h").set({minute:0,second:0,millisecond:0}).diff(t,"h",!0)*1e4)/1e4,C=await x.speedLoseInHoursStep(y,t,O,D,b,Y,n,d,r,l);(z=C.from)!=null&&z.speed&&(v.hours.push(C.from),v.wps.push(...C.wps),v.days.push(...C.days)),Y=C==null?void 0:C.next,Y.length||v.hours.push(C==null?void 0:C.to),b+=Math.round((((U=C==null?void 0:C.to)==null?void 0:U.distanceFromPrevious)??0)*1e4)/1e4}const W=v.hours;for(let T=0;T<W.length-1;T++){const D=p(W[T+1].eta).diff(W[T].etd,"hour",!0)||1;j+=(W[T].wxFactor||0)*D,S+=(W[T].cFactor||0)*D,L+=D}(Q=v.wps)==null||Q.forEach((T,D)=>{if(D){const C=v.wps[D-1],ft=T.distanceFromStart-C.distanceFromStart,nt=p(T.eta).diff(p(C.etd),"h",!0);if(nt<1)T.avgSpd=C.speed;else{T.avgSpd=Math.round(ft/nt*100)/100;const it=Math.round((C.speed+T.speed)/2*100)/100;T.avgSpd=T.avgSpd>it?it:T.avgSpd}}}),c.sample=v;const E=v.hours.at(0),B=v.hours.at(-1);c.distance=Math.round(B.distanceFromStart*1e4)/1e4,c.etd=p(E.eta).utc().format(),c.eta=p(B.eta).utc().format(),c.wxFactor=Math.round(j/L*1e4)/1e4,c.cFactor=Math.round(S/L*1e4)/1e4,c.avgSpeed=Math.round(B.distanceFromStart/L*1e4)/1e4,c.totalHrs=Math.round(L*1e4)/1e4,c.totalFoCons=Math.round((i==null?void 0:i.fo)/24*c.totalHrs*1e3)/1e3,c.totalDgoCons=Math.round((i==null?void 0:i.dgo)/24*c.totalHrs*1e3)/1e3;const P=p().valueOf()-f,A=((J=v==null?void 0:v.hours)==null?void 0:J.length)||1;return k==null||k.info("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms",l==null?void 0:l.requestId,P,A,Math.round(P/A*1e3)/1e3),c}static async analyseInstantWithThreshed(e,t,o,i,a,n,s,d="",r=3,l=!0,f=!1,m={}){var Z,z,U,Q;const h=p().valueOf();e.lng=R.LngLatHelper.convertToStdLng(e.lng);const{v0:u,label:I}=e.sog?{v0:e.sog,label:"Other"}:{v0:a.speed,label:"CP"},g=x.assembleProperties(i,a.loadCondition,u,0),y=R.LaneHelper.calculateSubRoute(e,n);if(((Z=y[0])==null?void 0:Z.length)<=1)return;const w=s.length?R.LaneHelper.calculateSubWaypoints(e,s):[];w.forEach(J=>J.important=!0);let c=R.LaneHelper.simplifyRouteToCoordinates(y,w,0),v=0,Y=0,b=0,j=0;const S={hours:[],wps:[],days:[]};t=p(t).utc();const L=t.clone();for(;c.length>0;){const J=r-t.hour()%r;let T=Math.ceil(t.clone().add(J,"h").set({minute:0,second:0,millisecond:0}).diff(t,"h",!0)*1e4)/1e4;if(T=t.clone().add(T,"h").isAfter(o)?o.diff(t,"h",!0)*1e4/1e4:T,T){const D=await x.speedLoseInHoursStep(g,t,L,T,v,c,d,l,f,m);(z=D.from)!=null&&z.speed&&(S.hours.push(D.from),D!=null&&D.wps&&S.wps.push(...D.wps),S.days.push(...D.days)),c=D==null?void 0:D.next,c.length||S.hours.push(D==null?void 0:D.to),v+=Math.round((((U=D==null?void 0:D.to)==null?void 0:U.distanceFromPrevious)??0)*1e4)/1e4}else break}const O=S.hours;for(let J=0;J<O.length-1;J++){const T=p(O[J+1].eta).diff(O[J].etd,"hour",!0);Y+=O[J].wxFactor*T,b+=O[J].cFactor*T,j+=T}const W=S.hours.at(0),E=S.hours.at(-1),B={sample:S,distance:Math.round(((E==null?void 0:E.distanceFromStart)||0)*1e4)/1e4,etd:p(W.eta).utc().format(),eta:p(E==null?void 0:E.eta).utc().format(),wxFactor:Math.round(Y/j*1e4)/1e4,cFactor:Math.round(b/j*1e4)/1e4,avgSpeed:Math.round(((E==null?void 0:E.distanceFromStart)||0)/j*1e4)/1e4,totalHrs:Math.round(j*1e4)/1e4,totalFoCons:Math.round((a==null?void 0:a.fo)/24*j*1e3)/1e3,totalDgoCons:Math.round((a==null?void 0:a.dgo)/24*j*1e3)/1e3,from:W,to:E,route:y,waypoints:w,v0:u,label:I},P=p().valueOf()-h,A=((Q=S==null?void 0:S.hours)==null?void 0:Q.length)||1;return k==null||k.info("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms",m==null?void 0:m.requestId,P,A,Math.round(P/A*1e3)/1e3),B}static async analyseCost(e,t,o,i,a={}){var u,I;const n=p().valueOf(),s=[];a.meteo2=!0,e.speedStep=e.speedStep||3;const d=p(e.etd).add(14,"day");for(const g of o){k==null||k.info("[%s][L%d] speed analyse from %s to %s with cp: %j",a.requestId,1,e.etd,d.format(),g);const y=JSON.parse(JSON.stringify(i.route)),w=JSON.parse(JSON.stringify(i.waypoints)),c=await x.analyseInstantWithThreshed({lat:e.lat,lng:e.lng},e.etd,d,t,g,y,w,e.meteoVendor,e.speedStep,!1,e.useRouteParam,a);c&&(await x.calculateCost(c,g,e,a),s.push(c))}s.sort((g,y)=>g.cost.total-y.cost.total);const r=s.at(0),l=s.at(1),f=[];if(f.push({combined:!1,speeds:[r],cost:(u=r.cost)==null?void 0:u.total}),l){const g=r.cost.cp,y=l.cost.cp,w=p(r.eta),c=p(r.etd),v=w.diff(c,"days",!0);let Y=Math.ceil(v/2);Y=Y>7?7:Y;let b=2,j={combined:!1,speeds:[l],cost:(I=l.cost)==null?void 0:I.total},S;for(;Y>=1&&(S=await x.combinedAnalyse(e,t,d,[g,y],i,Y,{...a,level:b}),!(j.cost<S.cost));){const L=j;if(j=S,Y===1){S=L;break}S=void 0,Y=Math.ceil(Y/2),b+=1}f.push(j),S&&f.push(S)}const h=p().valueOf()-n;return k==null||k.info("[%s] speed analyse cost: %d ms",a==null?void 0:a.requestId,h),f.sort((g,y)=>g.cost-y.cost)}static async combinedAnalyse(e,t,o,i,a,n,s={}){k==null||k.info("[%s][L%d] speed analyse with step: %d",s.requestId,s.level,n);const d=await x.alternateAnalyse(e,t,o,i,0,a,n,s),r=await x.alternateAnalyse(e,t,o,i,1,a,n,s),l=d.reduce((m,h)=>m+h.cost.total,0),f=r.reduce((m,h)=>m+h.cost.total,0);return l<f?{combined:!0,cost:Math.round(l*1e3)/1e3,speeds:d}:{combined:!0,cost:Math.round(f*1e3)/1e3,speeds:r}}static async alternateAnalyse(e,t,o,i,a,n,s,d={}){var h,u;let r=p.utc(e.etd),l=1;const f={lat:e.lat,lng:e.lng},m=[];for(;r.isBefore(o);){const I=r.clone().add(s,"day");k==null||k.info("[%s][L%d-%d] speed analyse from %s to %s with cp: %j",d.requestId,d.level,l,r.format(),I.format(),i[a]);const g=JSON.parse(JSON.stringify(n.route)),y=JSON.parse(JSON.stringify(n.waypoints)),w=await x.analyseInstantWithThreshed(f,r.format(),I,t,i[a],g,y,e.meteoVendor,e.speedStep,!1,e.useRouteParam,d);await x.calculateCost(w,i[a],e,d);const c=(u=(h=w==null?void 0:w.sample)==null?void 0:h.hours)==null?void 0:u.at(-1);if(c)f.lat=c.lat,f.lng=c.lng,r=p(c.eta),m.push(w),l+=1,a=a?0:1;else break}return m}static async calculateCost(e,t,o,i={}){if(e){const a=Math.round(e.totalHrs/24*(o.dailyHire||0)*(1-(o.addComm||0))*1e3)/1e3,n=Math.round(e.totalFoCons*(o.priceFO||0)*1e3)/1e3,s=Math.round(e.totalDgoCons*(o.priceDGO||0)*1e3)/1e3;e.cost={total:Math.round((a+n+s)*1e3)/1e3,hire:a,bunker:Math.round((n+s)*1e3)/1e3,cp:t},k.info("[%s] calculate speed cost: %j",i.requestId,e.cost)}return e}static async mergeSpeeds(e,t={}){const o={hours:[],wps:[],days:[]},i=e.reduce((h,u)=>h+u.distance,0),a=e.reduce((h,u)=>h+u.totalHrs,0),n=e.reduce((h,u)=>h+u.wxFactor*u.totalHrs/a,0),s=e.reduce((h,u)=>h+u.cFactor*u.totalHrs/a,0),d=e.reduce((h,u)=>h+u.totalFoCons,0),r=e.reduce((h,u)=>h+u.totalDgoCons,0),l=e.reduce((h,u)=>h+u.cost.total,0),f=e.reduce((h,u)=>h+u.cost.hire,0),m=e.reduce((h,u)=>h+u.cost.bunker,0);for(const h of e){const u=h.sample.hours,I=h.sample.wps,g=h.sample.days;u.forEach(y=>{var c;((c=o.hours)==null?void 0:c.findIndex(v=>v.eta===y.eta))===-1&&o.hours.push(y)}),I.forEach(y=>{var c;((c=o.wps)==null?void 0:c.findIndex(v=>v.eta===y.eta))===-1&&o.wps.push(y)}),g.forEach(y=>{var c;((c=o==null?void 0:o.days)==null?void 0:c.findIndex(v=>v.eta===y.eta))===-1&&o.days.push(y)})}return{sample:o,etd:e.at(0).etd,eta:e.at(-1).eta,from:e.at(0).from,to:e.at(-1).to,label:"combined",distance:Math.round(i*1e4)/1e4,totalHrs:Math.round(a*1e3)/1e3,avgSpeed:Math.round(i/a*1e3)/1e3,wxFactor:Math.round(n/a*1e3)/1e3,cFactor:Math.round(s/a*1e3)/1e3,totalFoCons:Math.round(d*1e3)/1e3,totalDgoCons:Math.round(r*1e3)/1e3,cost:{total:Math.round(l*1e3)/1e3,hire:Math.round(f*1e3)/1e3,bunker:Math.round(m*1e3)/1e3}}}}N.AISImpl=G,N.AlertHelper=et,N.AlertLevel=tt,N.HifleetImpl=ct,N.LoadCondition=at,N.MyShipImpl=ut,N.MyVesselImpl=rt,N.ShipxyImpl=dt,N.SpeedHelper=x,N.SpeedLabel=ot,N.VesselTag=st,N.alertHelper=ht,Object.defineProperty(N,Symbol.toStringTag,{value:"Module"})});
1
+ (function(T,N){typeof exports=="object"&&typeof module<"u"?N(exports,require("got"),require("@log4js-node/log4js-api"),require("moment"),require("@idm-plugin/geo"),require("@idm-plugin/meteo2"),require("@idm-plugin/meteo")):typeof define=="function"&&define.amd?define(["exports","got","@log4js-node/log4js-api","moment","@idm-plugin/geo","@idm-plugin/meteo2","@idm-plugin/meteo"],N):(T=typeof globalThis<"u"?globalThis:T||self,N(T["idm-plugin-rabbitmq"]={},T.got,T["@log4js-node/log4js-api"],T.moment,T["@idm-plugin/geo"],T["@idm-plugin/meteo2"],T["@idm-plugin/meteo"]))})(this,function(T,N,K,I,R,X,$){"use strict";var mt=Object.defineProperty;var yt=(T,N,K)=>N in T?mt(T,N,{enumerable:!0,configurable:!0,writable:!0,value:K}):T[N]=K;var _=(T,N,K)=>(yt(T,typeof N!="symbol"?N+"":N,K),K);let M;try{M=K.getLogger("vessel")}catch{}finally{}class G{parseStatus(e){let t,o;switch(e){case 0:t="在航(主机推动)",o="The engine is in use";break;case 1:t="锚泊",o="Anchored";break;case 2:t="失控",o="Not operated";break;case 3:t="操纵受限",o="Limited airworthiness";break;case 4:t="吃水受限",o="Limited by ship's draft";break;case 5:t="靠泊",o="Mooring";break;case 6:t="搁浅",o="Stranded";break;case 7:t="捕捞作业",o="Engaged in fishing";break;case 8:t="靠帆船提供动力",o="Sailing";break;default:t="未定义",o="Undefined"}return{labelCn:t,labelEn:o}}}class rt extends G{constructor(t,o){super();_(this,"clientId");_(this,"clientSecret");_(this,"token");this.clientId=t,this.clientSecret=o}async authToken(t={}){const o="https://svc.data.myvessel.cn/ada/oauth/token",i={searchParams:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},a=await N.post(o,i).json();M==null||M.info("[%s] fetch access token from: %s - %j",t.requestId,o,a),a.error||(this.token={accessToken:a.access_token,tokenType:a.token_type,expiresIn:a.expires_in,scope:a.scope,jti:a.jti,issuedAt:I().utc().format()})}async realTimePosition(t,o={}){var l,m,f;(!this.token||I().diff(I(this.token.issuedAt),"seconds")>((l=this.token)==null?void 0:l.expiresIn)-300)&&await this.authToken(o);const i="https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit",a={headers:{Authorization:`${(m=this.token)==null?void 0:m.tokenType} ${(f=this.token)==null?void 0:f.accessToken}`},searchParams:{mmsi:t}};M==null||M.info("[%s] fetch realtime position from: %s - %j",o.requestId,i,a);const n=await N.get(i,a).json();if(n.code)return M==null||M.warn("[%s] fetch realtime position failed: %j",o.requestId,i,{message:n.message,status:n.status,code:n.code}),n;const s=n.data;for(const h in s)!isNaN(s[h])&&Number(s[h])!==1/0&&(s[h]=Number(s[h]));const d=I(`${s.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:s.mmsi,name:s.vesselName,imo:s.imo,callSign:s.callsign,lat:s.lat,lng:s.lon,length:s.length,width:s.width,draught:s.currDraught,sog:s.sog,cog:s.cog,hdg:s.hdg,rot:s.rot,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(s.eta)?I.utc(s.eta).format():void 0,destination:s.dest,positionTime:d.unix(),status:s.status,labelCn:s.statusNameCn,labelEn:s.statusNameEn,method:"position",vendor:"myVessel",utc:d.utc().format()}}async trajectory(t,o,i,a,n=!0,s={}){(!this.token||I().diff(I(this.token.issuedAt),"seconds")>this.token.expiresIn-300)&&await this.authToken(s);const d=await this.realTimePosition(t,s),r=I(o),l=I(i),m=[];for(;l.diff(r,"day",!0)>30;)await this.trajectoryIn30Day(t,r,r.clone().add(30,"day"),d,a,m,s),r.add(30,"day");return await this.trajectoryIn30Day(t,r,l,d,a,m,s),m}async trajectoryIn30Day(t,o,i,a,n,s,d={}){var u,g,j,y,p;const r="https://svc.data.myvessel.cn/sdc/v1/vessels/status/track",l={headers:{Authorization:`${(u=this.token)==null?void 0:u.tokenType} ${(g=this.token)==null?void 0:g.accessToken}`},json:{mmsi:t,startTime:o.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),endTime:i.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")}};M==null||M.info("[%s] fetch trajectory from: %s - %j",d.requestId,r,l);const m=await N.post(r,l).json();if(m.code)return M==null||M.warn("[%s] fetch trajectory failed: %j",d.requestId,r,{message:m.message,status:m.status,code:m.code}),m;let f=-1;const h=I(`${(y=(j=m.data)==null?void 0:j[0])==null?void 0:y.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return(p=m.data)==null||p.forEach(c=>{for(const L in c)!isNaN(c[L])&&Number(c[L])!==1/0&&(c[L]=Number(c[L]));const b=I(`${c.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),D=c.status,{labelCn:w,labelEn:k}=this.parseStatus(D),S={mmsi:c.mmsi,imo:a==null?void 0:a.imo,lat:c.lat,lng:c.lon,sog:c.sog,cog:c.cog,hdg:c.hdg,draught:c.draught,status:D,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(c.eta)?I(`${c.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00").utc().format():void 0,destination:c.dest,positionTime:b.unix(),labelCn:w,labelEn:k,method:"trajectory",vendor:"myVessel",utc:b.utc().format()},x=Math.floor(b.diff(h,"minute",!0)/(n||1));x!==f&&(f=x,s.push(S))}),s}}class ct extends G{constructor(t){super();_(this,"token");this.token=t}async realTimePosition(t,o={}){const i="https://api.hifleet.com/position/position/get/token",a={searchParams:{mmsi:t,usertoken:this.token}},n=await N.post(i,a).json();M==null||M.info("[%s] fetch realtime position from: %s - %j",o.requestId,i,a);const s=n==null?void 0:n.list;if(!s)return M==null||M.warn("[%s] fetch realtime position failed: %j",o.requestId,i,n),n;for(const h in s)!isNaN(s[h])&&Number(s[h])!==1/0&&(s[h]=Number(s[h]));s.status=s.sp>3?0:1;const d=s.status,{labelCn:r,labelEn:l}=this.parseStatus(d),m=I(`${s.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:s.m,name:s.n,imo:s.imonumber,callSign:s.callsign,lat:Math.round(s.la/60*1e5)/1e5,lng:Math.round(s.lo/60*1e5)/1e5,length:s.l,width:s.w,draught:s.draught,sog:s.sp,cog:s.co,hdg:s.h,rot:isNaN(s.rot)?0:s.rot,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(s.eta)?I.utc(s.eta).format():void 0,destination:s.destination,positionTime:m.unix(),utc:m.utc().format(),status:d,labelCn:r,labelEn:l,method:"position",vendor:"hifleet"}}async search(t,o={}){let i="https://www.hifleet.com/hifleetapi/searchVesselOL.do";const a={searchParams:{keyword:t},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}};let n=await N.post(i,a).json();M==null||M.info("[%s] fetch vessel props from: %s - %j",o.requestId,i,a),n instanceof Array&&(n=n[0]);for(const d in n)!isNaN(n[d])&&Number(n[d])!==1/0&&(n[d]=Number(n[d]));const s={mmsi:n.m,name:n.n,imo:n.i,callSign:n.c,length:n.l,breadth:n.b,draught:n.dr,type:n.t};return i="https://www.hifleet.com/hifleetapi/sameShipSearch.do",n=await N.post(i,a).json(),M==null||M.info("[%s] search vessel dead weight from: %s - %j",o.requestId,i,a),n instanceof Array&&(n=n[0]),n&&(s.deadweight=Number(n.dwt)),s}async suggest(t,o={}){const i="https://www.hifleet.com/hifleetapi/getShipSuggest.do",a={searchParams:{q:t},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}},n=await N.post(i,a).json();M==null||M.info("[%s] suggest vessel props from: %s - %j",o.requestId,i,a);const s=[];for(const d of n)s.push({mmsi:!d.mmsi||isNaN(d.mmsi)?null:Number(d.mmsi),name:d.name,callSign:d.callsign,imo:!d.imo||isNaN(d.imo)?null:Number(d.imo),score:d._score});return s.sort((d,r)=>r.score-d.score),s}async trajectory(t,o,i,a,n=!0,s={}){var c,b,D;const d=await this.realTimePosition(t,s);let r=I(o);const l=I(i),m=I();if(n){let w=l.diff(r,"d",!0);w<0?r=l.clone().subtract(40,"d"):w<30?r.subtract(10,"d"):w<60?r.subtract(5,"d"):r=l.clone().subtract(80,"d"),w=m.diff(l,"d",!0),l.add(w>10?240:w*24,"h")}const f={searchParams:{endtime:l.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),starttime:r.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),mmsi:t,usertoken:this.token}},h="https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token",u=await N.get(h,f).json();M==null||M.info("[%s] fetch trajectory from: %s - %j",s.requestId,h,f);let g;u&&(g=((b=(c=u.ships)==null?void 0:c.offors)==null?void 0:b.ship)||[],g.length||M==null||M.warn("[%s] fetch trajectory failed: %j",s.requestId,u));const j=[];let y=-1;const p=I(`${(D=g==null?void 0:g[0])==null?void 0:D.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");for(const w of g){for(const E in w)!isNaN(w[E])&&Number(w[E])!==1/0&&(w[E]=Number(w[E]));const k=I(`${w.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");w.status=w.sp>4?0:1;const{labelEn:S,labelCn:x}=this.parseStatus(w.status),L={mmsi:w.m,name:w.n,imo:d==null?void 0:d.imo,lat:w.la,lng:w.lo,draught:w.draught,sog:w.sp,cog:w.co,hdg:w.hdg,positionTime:k.unix(),utc:k.utc().format(),status:w.status,labelCn:x,labelEn:S,method:"trajectory",vendor:"hifleet"},W=Math.floor(k.diff(p,"minute",!0)/(a||1));W!==y&&(y=W,j.push(L))}return j}}class dt extends G{constructor(t){super();_(this,"token");this.token=t}async realTimePosition(t,o={}){const i={searchParams:{id:t,k:this.token,enc:1}},a="https://api.shipxy.com/apicall/GetSingleShip",n=await N.get(a,i).json();if(M==null||M.info("[%s] fetch realtime position from: %s - %j",o.requestId,a,i),(n==null?void 0:n.status)!==0)return n;const s=n.data[0];for(const f in s)!isNaN(s[f])&&Number(s[f])!==1/0&&(s[f]=Number(s[f]));const{labelCn:d,labelEn:r}=await this.parseStatus(s.navistat),l=I.unix(s.lasttime);return{mmsi:s.ShipID,name:s.name,imo:s.imo,callSign:s.callsign,lat:Math.round(s.lat/1e6*1e5)/1e5,lng:Math.round(s.lon/1e6*1e5)/1e5,length:Math.round(s.length/10*100)/100,width:Math.round(s.width/10*100)/100,draught:Math.round(s.draught/1e3*100)/100,sog:Math.round(s.sog*3600/1e3/1852*100)/100,cog:Math.round(s.cog/100*100)/100,hdg:Math.round(s.hdg/100*100)/100,rot:Math.round(s.rot/100*100)/100,positionTime:s.lasttime,utc:l.utc().format(),status:s.navistat,labelEn:r,labelCn:d,method:"position",vendor:"shipxy"}}async trajectory(t,o,i,a,n=!0,s={}){var p;const d=await this.realTimePosition(t,s),r=I(o),l=I(i),m="https://api.shipxy.com/apicall/GetShipTrack",f={searchParams:{id:t,k:this.token,enc:1,cut:0,btm:r.unix(),etm:l.unix()}},h=await N.get(m,f).json();if(M==null||M.info("[%s] fetch trajectory from: %s - %j",s.requestId,m,f),(h==null?void 0:h.status)!==0)return h;const u=h==null?void 0:h.points,g=[],j=I.unix((p=u[0])==null?void 0:p.utc);let y=-1;for(const c of u){const b=I.unix(c.utc),D={imo:d==null?void 0:d.imo,mmsi:t,sog:Math.round(c.sog*3600/1e3/1852*100)/100,cog:Math.round(c.cog/100*100)/100,lat:Math.round(c.lat/1e6*1e5)/1e5,lng:Math.round(c.lon/1e6*1e5)/1e5,positionTime:b.unix(),utc:b.utc().format(),method:"trajectory",vendor:"shipxy"},w=Math.floor(b.diff(j,"minute",!0)/(a||1));w!==y&&(y=w,g.push(D))}return g}}class ut extends G{constructor(t){super();_(this,"token");this.token=t}async getShipId(t,o={}){const i={headers:{appKey:this.token},json:{mmsiList:t}},a="https://api3.myships.com/sp/ships/getShipIdByMMSI",n=await N.post(a,i).json();return M==null||M.info("[%s] fetch ship id from: %s - %j",o.requestId,a,i),n.code!=="0"?n:n.data[0].shipId}async getShipInfo(t,o={}){const i={headers:{appKey:this.token},json:{shipId:t}},a="https://api3.myships.com/sp/ships/aissta",n=await N.post(a,i).json();if(M==null||M.info("[%s] fetch ship info from: %s - %j",o.requestId,a,i),n.code!=="0")return n;const s=n.data;let d=s.imo;return t==="407170"&&(d="9198379",M==null||M.warn("[%s] ship(%s) imo error: %s, should be %s",o.requestId,t,s.imo,d)),{mmsi:s.mmsi,name:s.shipnameEn,imo:d,callSign:s.callSign,length:s.length,width:s.breadth,draught:(s.draught||100)/10}}async realTimePosition(t,o={}){const i=await this.getShipId(t,o),a=await this.getShipInfo(i,o),n={headers:{appKey:this.token},json:{shipId:i}},s="https://api3.myships.com/sp/ships/position/latest",d=await N.post(s,n).json();M==null||M.info("[%s] fetch realtime position from: %s - %j",o.requestId,s,n);const r=d.data[0];for(const u in r)!isNaN(r[u])&&Number(r[u])!==1/0&&(r[u]=Number(r[u]));const{labelCn:l,labelEn:m}=await this.parseStatus(r.aisNavStatus),f=I.unix(r.posTime);return{...a,mmsi:t,lat:Math.round(r.lat/1e4/60*1e5)/1e5,lng:Math.round(r.lon/1e4/60*1e5)/1e5,sog:Math.round(r.sog/10*100)/100,cog:Math.round(r.cog/10*100)/100,hdg:Math.round(r.heading*100)/100,rot:Math.round(r.rot*100)/100,positionTime:r.posTime,utc:f.utc().format(),status:r.aisNavStatus,labelEn:m,labelCn:l,method:"position",vendor:"myship"}}async trajectory(t,o,i,a,n=!0,s={}){const d=I(o),r=I(i),l=await this.getShipId(t),m=await this.getShipInfo(l),f=[];for(;r.diff(d,"day",!0)>30;)await this.trajectoryIn30Day(l,d.unix(),d.add(30,"day").unix(),m,t,a,f);return await this.trajectoryIn30Day(l,d.unix(),r.unix(),m,t,a,f),f}async trajectoryIn30Day(t,o,i,a,n,s,d,r={}){var j;const l={headers:{appKey:this.token},json:{shipId:t,startTime:o,endTime:i}},m="https://api3.myships.com/sp/ships/position/history",f=await N.post(m,l).json();if(M==null||M.info("[%s] fetch trajectory from: %s - %j",r.requestId,m,l),f.code!=="0")return M==null||M.warn("[%s] invoke myship trajectory failed: %j",r.requestId,f),f;const h=f.data;for(const y in h)!isNaN(h[y])&&Number(h[y])!==1/0&&(h[y]=Number(h[y]));const u=I.unix((j=h[0])==null?void 0:j.posTime);let g=-1;for(const y of h){const p=I.unix(y.posTime),c={imo:a==null?void 0:a.imo,mmsi:n,lat:Math.round(y.lat/1e4/60*1e5)/1e5,lng:Math.round(y.lon/1e4/60*1e5)/1e5,sog:Math.round(y.sog/10*100)/100,cog:Math.round(y.cog/10*100)/100,hdg:Math.round(y.heading*100)/100,rot:Math.round(y.rot*100)/100,positionTime:p.unix(),utc:p.utc().format(),method:"trajectory",vendor:"myship"},b=Math.floor(p.diff(u,"minute",!0)/(s||1));b!==g&&(g=b,d.push(c))}return d}}let V;try{V=K.getLogger("vessel")}catch{}finally{}var tt=(Y=>(Y.NOTICE="NOTICE",Y.WARN="WARN",Y.HEAVY="HEAVY",Y.SEVERE="SEVERE",Y.ERROR="ERROR",Y.FATAL="FATAL",Y))(tt||{});class et{parsePrinciple(e,t={}){var s,d,r;V==null||V.info("[%s] parse rule: %s",t.requestId,e);const o=new RegExp("(?<=\\[)(.+)(?=])","g"),i=e.match(o)?(s=e.match(o))==null?void 0:s[0]:void 0,a=i==null?void 0:i.split(";");if(!a)return;const n={};for(let l=0;l<(a==null?void 0:a.length);l++){const m=(r=(d=a[l].match(o))==null?void 0:d[0])==null?void 0:r.split("],");if(l===0&&!m)n.scope=a[0];else if(m)for(let f=0,h=m.length;f<h;f++){const u=this.parseRule(m[f]);u&&(n[u.level]?u.key?n[u.level][u==null?void 0:u.key]=u:n[u.level]=u:u.key?n[u.level]={[u==null?void 0:u.key]:u}:n[u.level]=u)}}return n}parseRule(e,t={}){var n;V==null||V.info("[%s] parse rule: %s",t.requestId,e),e=e.startsWith("[")?e:`[${e}`,e=e.endsWith("]")?e:`${e}]`;const o=new RegExp("(?<=\\[)(.+?)(?=])","g"),i=(n=e==null?void 0:e.match(o))==null?void 0:n[0],a=i==null?void 0:i.split(",");if(a)return{operator:a[0],number:Number.isNaN(Number(a[1]))?a[1]:Number(a[1]),level:a[2],time:Number(a[3]),key:a[4]}}checkWeather(e,t,o={}){var u,g,j,y,p,c,b,D,w,k,S,x,L,W,E;let i=0,a=0,n=0,s=0;const d=Math.round(((g=(u=t==null?void 0:t.SEVERE)==null?void 0:u.sigWave)==null?void 0:g.number)*1.6*100)/100,r=(y=(j=t==null?void 0:t.SEVERE)==null?void 0:j.sigWave)==null?void 0:y.number,l=(c=(p=t==null?void 0:t.HEAVY)==null?void 0:p.sigWave)==null?void 0:c.number,m=Math.round((((D=(b=t==null?void 0:t.SEVERE)==null?void 0:b.wind)==null?void 0:D.number)+2)*100)/100,f=(k=(w=t==null?void 0:t.SEVERE)==null?void 0:w.wind)==null?void 0:k.number,h=(x=(S=t==null?void 0:t.HEAVY)==null?void 0:S.wind)==null?void 0:x.number;for(let B=0;B<(e==null?void 0:e.length);B++){const q=e[B],P=(W=(L=q==null?void 0:q.meteo)==null?void 0:L.wave)==null?void 0:W.sig,A=(E=q==null?void 0:q.meteo)==null?void 0:E.wind,Z=B?I(q.eta).diff(I(e[B-1].eta),"hour",!0):0;s=Z>s?Z:s,V==null||V.info("[%s] check sig.wave: %j",o.requestId,{...P,dgThd4Wv:d,svThd4Wv:r,hvThd4Wv:l}),(P==null?void 0:P.height)>=d?q.isDangerous=!0:(P==null?void 0:P.height)>=r?q.isSevere=!0:(P==null?void 0:P.height)>=l&&(q.isHeavy=!0),V==null||V.info("[%s] check wind: %j",o.requestId,{...A,dgThd4Wd:m,svThd4Wd:f,hvThd4Wd:h}),(A==null?void 0:A.scale)>=m?(q.isDangerous=!0,delete q.isSevere,delete q.isHeavy):(A==null?void 0:A.scale)>f?(q.isDangerous||(q.isSevere=!0),delete q.isHeavy):(A==null?void 0:A.scale)===h&&!q.isDangerous&&!q.isSevere&&(q.isHeavy=!0),i+=q.isDangerous?Z:0,a+=q.isSevere?Z:0,n+=q.isHeavy?Z:0}return i=Math.round(i*100)/100,a=Math.round(a*100)/100,n=Math.round(n*100)/100,s=Math.round(s),{sample:e,dangerous:i,severe:a,heavy:n,step:s<3?3:s,wind:{dgThd4Wd:m,svThd4Wd:f,hvThd4Wd:h},sig:{dgThd4Wv:d,svThd4Wv:r,hvThd4Wv:l}}}}const ht=new et;let v;try{v=K.getLogger("vessel")}catch{}finally{}const lt=new X.MeteoHelper2("",!0);var st=(Y=>(Y.common="common",Y.container="container",Y.tugs="tugs",Y))(st||{}),at=(Y=>(Y.Ballast="Ballast",Y.Laden="Laden",Y))(at||{}),ot=(Y=>(Y.Cp="CP",Y.Perf="Basis",Y.Instruct="Other",Y))(ot||{});class C{static blockCoefficient(e,t,o,i){let a=Math.round(e/(t*o*i)*100)/100;a=a<.55?.55:a>.85?.85:a;const n=[.55,.6,.65,.7,.75,.8,.85],s=n.map(d=>Math.abs(d-a));return n[s.indexOf(Math.min(...s))]}static froudeNumber(e,t,o=9.8){let i=Math.round(Math.sqrt(e*e/(o*t))*100)/100;return i=i<.05?.05:i>.3?.3:i,i}static amendFactor(e,t,o){const i={.55:[1.7,-1.4,-7.4],.6:[2.2,-2.5,-9.7],.65:[2.6,-3.7,-11.6],.7:[3.1,-5.3,-12.4],.75:[2.4,-10.6,-9.5],.8:[2.6,-13.1,-15.1],.85:[3.1,-18.7,28]};let n={.55:[1.7,-1.4,-7.4],.6:[2.2,-2.5,-9.7],.65:[2.6,-3.7,-11.6],.7:[3.1,-5.3,-12.4],.75:[2.6,-12.5,-13.5],.8:[3,-16.3,-21.6],.85:[3.4,-20.9,31.8]}[e];return o==="Laden"&&(n=i[e]),n[0]+n[1]*t+n[2]*Math.pow(t,2)}static directionFactor(e,t=0){let o;return e>30&&e<=60?o=(1.7-.03*Math.pow(t-4,2))/2:e>60&&e<=150?o=(.9-.06*Math.pow(t-6,2))/2:e>150&&e<=180?o=(.4-.03*Math.pow(t-8,2))/2:o=1,Math.round(o*1e5)/1e5}static vesselTagFactor(e,t,o,i=0){i=i>6?i-.9*(i-6):i;let a;return o==="container"?a=.7*i+Math.pow(i,6.5)/(22*Math.pow(e,2/3)):t==="Ballast"?a=.7*i+Math.pow(i,6.5)/(2.7*Math.pow(e,2/3)):a=.5*i+Math.pow(i,6.5)/(2.7*Math.pow(e,2/3)),a}static waveHeightFactor(e,t){e=e<0?.2:e,e=e>6?e-.9*(e-6):e,e=e>9?9:e;let o;return t>30&&t<=60?o=-.6:t>60&&t<=90?o=-.4:t>90&&t<=120?o=e<3?.4:-.3:t>120&&t<=150?o=e<3?.6:-.5:t>150&&t<=180?o=e<3?.7:-.6:o=-.7,Math.round(o*(.144*Math.pow(e,2)+.178*e)*1e4)/1e4}static assembleProperties(e,t,o,i){var f;const a=e.lbp??e.length??e.lengthOverall??198.9642,n=e.draught??8,s=e.breadthMoulded??e.breadth??e.breadthExtreme??32.4572,d=e.deadweight??67035.7773,r=((f=e==null?void 0:e.type)==null?void 0:f.toLowerCase())||"common";return{tag:r.indexOf("container")>-1?"container":r.indexOf("tugs")>-1?"tugs":"common",lbp:a,loadCondition:t,draught:n,breadthMoulded:s,displacement:Math.round((d/1.025+n*s*a*.7)*1e4)/1e4,speed:Math.round((o??14.1382)*1852/3600*1e4)/1e4,bearing:i||90}}static async speedLoseAt(e,t,o,i="",a=2,n=!0,s=!1,d={}){let r;if(t.velocity&&s&&(e.speed=R.LngLatHelper.roundPrecision(t.velocity*1852/3600,6)),n){let l;if(d.meteo2)try{const u=await lt.spotForecast(t.lat,t.lng,o.utc().format(),!1,!1,!0,d),[g]=$.Meteo2Assist.pickHourly(u,o);l=$.Meteo2Assist.toLegacy(g)}catch(u){v.warn("[%s] meteo2 spot(%j) forecast failed: %s",d.requestId,{...t,eta:o.utc().format()},u)}else l=await X.MeteoHelper.queryPointFactor(t.lng,t.lat,o.valueOf(),"wind,wave,current,watertemp",i,d);const m=C.weatherFactor(e,l),f=C.currentFactor(e.bearing,l==null?void 0:l.current,a),h=Math.round((e.speed*1.943844+m+f)*100)/100;r={meteo:{...l},wxFactor:m,cFactor:f,speed:t.velocity&&s?t.velocity:h<0?1:h,eta:o.utc().format("YYYY-MM-DDTHH:mm[Z]"),etd:o.utc().format("YYYY-MM-DDTHH:mm[Z]")}}else r={wxFactor:0,cFactor:0,speed:t.velocity&&s?t.velocity:Math.round((e.speed*1.943844+0+0)*100)/100,eta:o.utc().format("YYYY-MM-DDTHH:mm[Z]"),etd:o.utc().format("YYYY-MM-DDTHH:mm[Z]")};return delete t.meteo,delete t.wxFactor,delete t.cFactor,delete t.speed,delete t.etd,{...r,...t}}static async speedLoseInHoursStep(e,t,o,i,a,n,s="",d=!0,r=!1,l={}){t.utc();const m=t.clone().add(14,"days"),f=[],h=[];let u=0,g=0,j,y;for(let p=0;p<n.length-1;p++){let c=n[p];c.distanceFromStart=Math.round((a+g)*1e4)/1e4;const b=n[p+1];if(e.bearing=R.LaneHelper.calculateBearing(c,b,!b.gcToPrevious),c.bearing=e.bearing,c.suspend&&r){c.eta=c.eta||t.format("YYYY-MM-DDTHH:mm[Z]"),c.elapsed=c.elapsed??0;const k=c.suspend-c.elapsed;if(i-u>k)i=i-u-k,t.add(k,"hour"),c.elapsed=c.suspend;else{const S=i-u;c.elapsed+=S,t.add(S,"hour"),i=0}if(v==null||v.info(`[%s] suspend ${c.elapsed} hours at %j, and remain ${i} hours need to go...`,l.requestId,c),i===0)return c.distanceFromPrevious=g,{etd:t,from:y||c,to:c,next:n.filter(S=>S),wps:f,days:h}}else c.suspend=0;d=t.isAfter(m)?!1:d,c=await C.speedLoseAt(e,c,t,s,0,d,r,l),y=y||c,c.important&&f.push(c),t.isSameOrAfter(o)&&(h.push(c),o.add(24,"hour"));const D=R.LaneHelper.calculateDistance(c,b,!b.gcToPrevious);let w=Math.ceil(D/y.speed*1e4)/1e4;if(u+w<i){if(u+=w,t.add(w,"hour"),delete n[p],v==null||v.debug(`[%s] go to %j from %j with ${D}nm, and cost ${w} hours`,l.requestId,{lat:b.lat,lng:b.lng},{lat:y.lat,lng:y.lng,etd:y.etd}),g+=D,n.filter(k=>k).length<=1){j=b,j.eta=t.format("YYYY-MM-DDTHH:mm[Z]"),j.distanceFromPrevious=D,j.distanceFromStart=Math.round((a+g)*1e4)/1e4,f.push(j),delete n[p+1];break}}else{w=i-u,t.add(w,"hour");const k=R.LngLatHelper.roundPrecision(y.speed*w,4);j=R.LaneHelper.calculateCoordinate(c,e.bearing,k,"nauticalmiles",!b.gcToPrevious),j.eta=t.format("YYYY-MM-DDTHH:mm[Z]"),n[p]=j,v==null||v.debug(`[%s] go to %j from %j with ${k}nm, and cost ${w} hours`,l.requestId,{lat:j.lat,lng:j.lng},{lat:c.lat,lng:c.lng,etd:c.etd}),g+=k,j.distanceFromPrevious=Math.round(g*1e4)/1e4,j.distanceFromStart=Math.round((a+g)*1e4)/1e4;break}}return{etd:t,from:y,to:j,next:n.filter(p=>p),wps:f,days:h}}static currentFactor(e,t,o=0){const i=(e-(t==null?void 0:t.degree)||0)/180*Math.PI;if(Math.abs(i)===Math.PI/2)return 0;let a=((t==null?void 0:t.kts)||0)*Math.cos(i);return o&2?a=Math.ceil(a*100)/100:o&1?a=Math.floor(a*100)/100:a=Math.round(a*100)/100,Math.abs(a)>5?0:a}static weatherFactor(e,t){var m,f,h,u,g,j,y;v==null||v.debug("calculate weather factor via: %j",{...e,...t});const o=C.blockCoefficient(e.displacement,e.lbp,e.breadthMoulded,e.draught),i=C.froudeNumber(e.speed,e.lbp),a=C.amendFactor(o,i,e.loadCondition);let n=Math.abs(e.bearing%360-(((m=t==null?void 0:t.wind)==null?void 0:m.degree)%360||0));n=n>180?360-n:n;const s=C.directionFactor(n,(f=t==null?void 0:t.wind)==null?void 0:f.scale),d=C.vesselTagFactor(e.displacement,e.loadCondition,e.tag,(h=t==null?void 0:t.wind)==null?void 0:h.scale);let r=s*a*d/100*e.speed;r=Math.round(r*1.943844*1e4)/1e4*-1,e.tag==="tugs"&&Math.abs(r)>1&&(r=r/(Math.abs(Math.round(r))+1)),v==null||v.debug("wind wx factor = %d",r),n=Math.abs(e.bearing%360-(((g=(u=t==null?void 0:t.wave)==null?void 0:u.sig)==null?void 0:g.degree)%360||0));const l=C.waveHeightFactor(((y=(j=t==null?void 0:t.wave)==null?void 0:j.sig)==null?void 0:y.height)??1,n);return v==null||v.debug("wave wx factor = %d",l),r=r*.4+l,v==null||v.debug("weather factor = %d",r),r=Math.abs(r)>4?4*(Math.abs(r)/r)+Math.abs(r)/r*(Math.abs(r)-4)*.1:r,Math.round((r||0)*100)/100}static async analyseInstant(e,t,o,i,a,n="",s=0,d=!0,r=!1,l={}){var Z,z,U,Q,J;const m=I().valueOf();e.lng=R.LngLatHelper.convertToStdLng(e.lng);const{route:f,waypoints:h}=a.points,u=R.LaneHelper.calculateSubRoute(e,f);if(((Z=u[0])==null?void 0:Z.length)<=1)return;const{v0:g,label:j}=e.sog?{v0:e.sog,label:"Other"}:{v0:i.speed,label:"CP"},y=C.assembleProperties(o,i.loadCondition,g,0),p=h.length?R.LaneHelper.calculateSubWaypoints(e,h):[];p.forEach(H=>H.important=!0);const c={from:{...e},route:u,waypoints:p,v0:g,label:j},b={hours:[],days:[],wps:[]};s||(R.LaneHelper.calculateRouteDistance(u)/i.speed<=72?s=3:s=6);let D=R.LaneHelper.simplifyRouteToCoordinates(u,p,0),w=0,k=0,S=0,x=0;t=I(t).utc();const L=t.clone();for(;D.length>0;){const H=s-t.hour()%s,F=Math.ceil(t.clone().add(H,"h").set({minute:0,second:0,millisecond:0}).diff(t,"h",!0)*1e4)/1e4,O=await C.speedLoseInHoursStep(y,t,L,F,w,D,n,d,r,l);(z=O.from)!=null&&z.speed&&(b.hours.push(O.from),b.wps.push(...O.wps),b.days.push(...O.days)),D=O==null?void 0:O.next,D.length||b.hours.push(O==null?void 0:O.to),w+=Math.round((((U=O==null?void 0:O.to)==null?void 0:U.distanceFromPrevious)??0)*1e4)/1e4}const W=b.hours;for(let H=0;H<W.length-1;H++){const F=I(W[H+1].eta).diff(W[H].etd,"hour",!0)||1;k+=(W[H].wxFactor||0)*F,S+=(W[H].cFactor||0)*F,x+=F}(Q=b.wps)==null||Q.forEach((H,F)=>{if(F){const O=b.wps[F-1],ft=H.distanceFromStart-O.distanceFromStart,nt=I(H.eta).diff(I(O.etd),"h",!0);if(nt<1)H.avgSpd=O.speed;else{H.avgSpd=Math.round(ft/nt*100)/100;const it=Math.round((O.speed+H.speed)/2*100)/100;H.avgSpd=H.avgSpd>it?it:H.avgSpd}}}),c.sample=b;const E=b.hours.at(0),B=b.hours.at(-1);c.distance=Math.round(B.distanceFromStart*1e4)/1e4,c.etd=I(E.eta).utc().format(),c.eta=I(B.eta).utc().format(),c.wxFactor=Math.round(k/x*1e4)/1e4,c.cFactor=Math.round(S/x*1e4)/1e4,c.avgSpeed=Math.round(B.distanceFromStart/x*1e4)/1e4,c.totalHrs=Math.round(x*1e4)/1e4,c.totalFoCons=Math.round((i==null?void 0:i.fo)/24*c.totalHrs*1e3)/1e3,c.totalDgoCons=Math.round((i==null?void 0:i.dgo)/24*c.totalHrs*1e3)/1e3;const P=I().valueOf()-m,A=((J=b==null?void 0:b.hours)==null?void 0:J.length)||1;return v==null||v.info("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms",l==null?void 0:l.requestId,P,A,Math.round(P/A*1e3)/1e3),c}static async analyseInstantWithThreshed(e,t,o,i,a,n,s,d="",r=3,l=!0,m=!1,f={}){var Z,z,U,Q;const h=I().valueOf();e.lng=R.LngLatHelper.convertToStdLng(e.lng);const{v0:u,label:g}=e.sog?{v0:e.sog,label:"Other"}:{v0:a.speed,label:"CP"},j=C.assembleProperties(i,a.loadCondition,u,0),y=R.LaneHelper.calculateSubRoute(e,n);if(((Z=y[0])==null?void 0:Z.length)<=1)return;const p=s.length?R.LaneHelper.calculateSubWaypoints(e,s):[];p.forEach(J=>J.important=!0);let c=R.LaneHelper.simplifyRouteToCoordinates(y,p,0),b=0,D=0,w=0,k=0;const S={hours:[],wps:[],days:[]};t=I(t).utc();const x=t.clone();for(;c.length>0;){const J=r-t.hour()%r;let H=Math.ceil(t.clone().add(J,"h").set({minute:0,second:0,millisecond:0}).diff(t,"h",!0)*1e4)/1e4;if(H=t.clone().add(H,"h").isAfter(o)?o.diff(t,"h",!0)*1e4/1e4:H,H){const F=await C.speedLoseInHoursStep(j,t,x,H,b,c,d,l,m,f);(z=F.from)!=null&&z.speed&&(S.hours.push(F.from),F!=null&&F.wps&&S.wps.push(...F.wps),S.days.push(...F.days)),c=F==null?void 0:F.next,c.length||S.hours.push(F==null?void 0:F.to),b+=Math.round((((U=F==null?void 0:F.to)==null?void 0:U.distanceFromPrevious)??0)*1e4)/1e4}else break}const L=S.hours;for(let J=0;J<L.length-1;J++){const H=I(L[J+1].eta).diff(L[J].etd,"hour",!0);D+=L[J].wxFactor*H,w+=L[J].cFactor*H,k+=H}const W=S.hours.at(0),E=S.hours.at(-1),B={sample:S,distance:Math.round(((E==null?void 0:E.distanceFromStart)||0)*1e4)/1e4,etd:I(W.eta).utc().format(),eta:I(E==null?void 0:E.eta).utc().format(),wxFactor:Math.round(D/k*1e4)/1e4,cFactor:Math.round(w/k*1e4)/1e4,avgSpeed:Math.round(((E==null?void 0:E.distanceFromStart)||0)/k*1e4)/1e4,totalHrs:Math.round(k*1e4)/1e4,totalFoCons:Math.round((a==null?void 0:a.fo)/24*k*1e3)/1e3,totalDgoCons:Math.round((a==null?void 0:a.dgo)/24*k*1e3)/1e3,from:W,to:E,route:y,waypoints:p,v0:u,label:g},P=I().valueOf()-h,A=((Q=S==null?void 0:S.hours)==null?void 0:Q.length)||1;return v==null||v.info("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms",f==null?void 0:f.requestId,P,A,Math.round(P/A*1e3)/1e3),B}static async analyseCost(e,t,o,i,a={}){var g,j;const n=I().valueOf(),s=[];a.meteo2=!0,e.speedStep=e.speedStep||3;const d=I.utc(e.etd).add(14,"day");let r=1;for(const y of o){v==null||v.info("[%s][L%d-%d] analyse from %s to %s",a.requestId,1,r,e.etd,d.format()),v==null||v.info("[%s][L%d-%d] analyse with cp: %j",a.requestId,1,r,y);const p=JSON.parse(JSON.stringify(i.route)),c=JSON.parse(JSON.stringify(i.waypoints)),b=await C.analyseInstantWithThreshed({lat:e.lat,lng:e.lng},e.etd,d,t,y,p,c,e.meteoVendor,e.speedStep,!1,e.useRouteParam,a);b&&(await C.calculateCost(b,y,e,a),s.push(b),v==null||v.info("[%s][L%d-%d] cost: %j",a.requestId,1,r,{distance:b.distance,totalHrs:b.totalHrs,totalFoCons:b.totalFoCons,totalDgoCons:b.totalDgoCons,etd:b.etd,eta:b.eta,cost:b.cost})),r++}s.sort((y,p)=>y.cost.total-p.cost.total);const l=s.at(0),m=s.at(1),f=[];if(f.push({combined:!1,speeds:[l],cost:(g=l.cost)==null?void 0:g.total}),m){const y=l.cost.cp,p=m.cost.cp,c=I(l.eta),b=I(l.etd),D=c.diff(b,"days",!0);let w=Math.ceil(D/2);w=w>7?7:w;let k=2,S={combined:!1,speeds:[m],cost:(j=m.cost)==null?void 0:j.total},x;for(;w>=1&&(x=await C.combinedAnalyse(e,t,d,[y,p],i,w,{...a,level:k}),!(S.cost<x.cost));){const L=S;if(S=x,w===1){x=L;break}x=void 0,w=Math.ceil(w/2),k+=1}f.push(S),x&&f.push(x)}const u=I().valueOf()-n;return v==null||v.info("[%s] analyse elapsed: %d ms",a==null?void 0:a.requestId,u),f.sort((y,p)=>y.cost-p.cost)}static async combinedAnalyse(e,t,o,i,a,n,s={}){v==null||v.info("[%s][L%d] analyse with alternate cp in every %d days",s.requestId,s.level,n);const d=await C.alternateAnalyse(e,t,o,i,0,a,n,s),r=d.reduce((f,h)=>f+h.cost.total,0);v==null||v.info("[%s][L%d] cost with alternate cp a first: %j",s.requestId,s.level,{cost:r});const l=await C.alternateAnalyse(e,t,o,i,1,a,n,s),m=l.reduce((f,h)=>f+h.cost.total,0);return v==null||v.info("[%s][L%d] cost with alternate cp b first: %j",s.requestId,s.level,{cost:m}),r<m?{combined:!0,cost:Math.round(r*1e3)/1e3,speeds:d}:{combined:!0,cost:Math.round(m*1e3)/1e3,speeds:l}}static async alternateAnalyse(e,t,o,i,a,n,s,d={}){var h,u;let r=I.utc(e.etd),l=1;const m={lat:e.lat,lng:e.lng},f=[];for(;r.isBefore(o);){const g=r.clone().utc().add(s,"day");v==null||v.info("[%s][L%d-%d] analyse from %s to %s",d.requestId,d.level,l,e.etd,g.format()),v==null||v.info("[%s][L%d-%d] analyse with cp: %j",d.requestId,d.level,l,i[a]);const j=JSON.parse(JSON.stringify(n.route)),y=JSON.parse(JSON.stringify(n.waypoints)),p=await C.analyseInstantWithThreshed(m,r.format(),g,t,i[a],j,y,e.meteoVendor,e.speedStep,!1,e.useRouteParam,d);p&&(await C.calculateCost(p,i[a],e,d),v==null||v.info("[%s][L%d-%d] cost: %j",d.requestId,d.level,l,{distance:p.distance,totalHrs:p.totalHrs,totalFoCons:p.totalFoCons,totalDgoCons:p.totalDgoCons,etd:p.etd,eta:p.eta,cost:p.cost}));const c=(u=(h=p==null?void 0:p.sample)==null?void 0:h.hours)==null?void 0:u.at(-1);if(c)m.lat=c.lat,m.lng=c.lng,r=I(c.eta),f.push(p),l+=1,a=a?0:1;else break}return f}static async calculateCost(e,t,o,i={}){if(e){const a=Math.round(e.totalHrs/24*(o.dailyHire||0)*(1-(o.addComm||0))*1e3)/1e3,n=Math.round(e.totalFoCons*(o.priceFO||0)*1e3)/1e3,s=Math.round(e.totalDgoCons*(o.priceDGO||0)*1e3)/1e3;e.cost={total:Math.round((a+n+s)*1e3)/1e3,hire:a,bunker:Math.round((n+s)*1e3)/1e3,cp:t}}return e}static async mergeSpeeds(e,t={}){const o={hours:[],wps:[],days:[]},i=e.reduce((h,u)=>h+u.distance,0),a=e.reduce((h,u)=>h+u.totalHrs,0),n=e.reduce((h,u)=>h+u.wxFactor*u.totalHrs/a,0),s=e.reduce((h,u)=>h+u.cFactor*u.totalHrs/a,0),d=e.reduce((h,u)=>h+u.totalFoCons,0),r=e.reduce((h,u)=>h+u.totalDgoCons,0),l=e.reduce((h,u)=>h+u.cost.total,0),m=e.reduce((h,u)=>h+u.cost.hire,0),f=e.reduce((h,u)=>h+u.cost.bunker,0);for(const h of e){const u=h.sample.hours,g=h.sample.wps,j=h.sample.days;u.forEach(y=>{var c;((c=o.hours)==null?void 0:c.findIndex(b=>b.eta===y.eta))===-1&&o.hours.push(y)}),g.forEach(y=>{var c;((c=o.wps)==null?void 0:c.findIndex(b=>b.eta===y.eta))===-1&&o.wps.push(y)}),j.forEach(y=>{var c;((c=o==null?void 0:o.days)==null?void 0:c.findIndex(b=>b.eta===y.eta))===-1&&o.days.push(y)})}return{sample:o,etd:e.at(0).etd,eta:e.at(-1).eta,from:e.at(0).from,to:e.at(-1).to,label:"combined",distance:Math.round(i*1e4)/1e4,totalHrs:Math.round(a*1e3)/1e3,avgSpeed:Math.round(i/a*1e3)/1e3,wxFactor:Math.round(n/a*1e3)/1e3,cFactor:Math.round(s/a*1e3)/1e3,totalFoCons:Math.round(d*1e3)/1e3,totalDgoCons:Math.round(r*1e3)/1e3,cost:{total:Math.round(l*1e3)/1e3,hire:Math.round(m*1e3)/1e3,bunker:Math.round(f*1e3)/1e3}}}}T.AISImpl=G,T.AlertHelper=et,T.AlertLevel=tt,T.HifleetImpl=ct,T.LoadCondition=at,T.MyShipImpl=ut,T.MyVesselImpl=rt,T.ShipxyImpl=dt,T.SpeedHelper=C,T.SpeedLabel=ot,T.VesselTag=st,T.alertHelper=ht,Object.defineProperty(T,Symbol.toStringTag,{value:"Module"})});
@@ -275,7 +275,7 @@ export declare class SpeedHelper {
275
275
  }): Promise<any[]>;
276
276
  /**
277
277
  * 按步长多次减半,分别用7,4,2,1天步长及cpa,cpb交替计算各种组合下的成本
278
- * @param props 基础属性(位置,出发时间等)
278
+ * @param props 基础属性(位置,出发时间,日租金,FO/DO/GO单价等)
279
279
  * @param vessel 船舶档案(长、宽、吃水、船舶类型等)
280
280
  * @param cps CP条款(装载状态,速度、油耗等)
281
281
  * @param max 最长分析未来14天
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@idm-plugin/vessel",
3
3
  "private": false,
4
- "version": "1.5.9",
4
+ "version": "1.6.1",
5
5
  "description": "idm plugin for vessel",
6
6
  "type": "module",
7
7
  "keywords": [