@idm-plugin/vessel 2.1.1 → 2.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
@@ -2,18 +2,18 @@ var ht = Object.defineProperty;
2
2
  var lt = (E, t, e) => t in E ? ht(E, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : E[t] = e;
3
3
  var K = (E, t, e) => (lt(E, typeof t != "symbol" ? t + "" : t, e), e);
4
4
  import B from "got";
5
- import rt from "@log4js-node/log4js-api";
5
+ import ct from "@log4js-node/log4js-api";
6
6
  import b from "moment";
7
7
  import { LngLatHelper as J, LaneHelper as W } from "@idm-plugin/geo2";
8
8
  import { MeteoHelper2 as ft } from "@idm-plugin/meteo2";
9
- import { Meteo2Assist as it } from "@idm-plugin/meteo";
9
+ import { Meteo2Assist as rt } from "@idm-plugin/meteo";
10
10
  let v;
11
11
  try {
12
- v = rt.getLogger("vessel");
12
+ v = ct.getLogger("vessel");
13
13
  } catch {
14
14
  } finally {
15
15
  }
16
- class et {
16
+ class st {
17
17
  /**
18
18
  * 解析AIS状态码
19
19
  * @param status
@@ -54,7 +54,7 @@ class et {
54
54
  return { labelCn: e, labelEn: a };
55
55
  }
56
56
  }
57
- class xt extends et {
57
+ class xt extends st {
58
58
  constructor(e, a) {
59
59
  super();
60
60
  K(this, "clientId");
@@ -182,7 +182,7 @@ class xt extends et {
182
182
  }), s;
183
183
  }
184
184
  }
185
- class Et extends et {
185
+ class Et extends st {
186
186
  constructor(e) {
187
187
  super();
188
188
  K(this, "token");
@@ -334,7 +334,7 @@ class Et extends et {
334
334
  return S;
335
335
  }
336
336
  }
337
- class jt extends et {
337
+ class jt extends st {
338
338
  constructor(e) {
339
339
  super();
340
340
  K(this, "token");
@@ -411,7 +411,7 @@ class jt extends et {
411
411
  return C;
412
412
  }
413
413
  }
414
- class Nt extends et {
414
+ class Nt extends st {
415
415
  constructor(e) {
416
416
  super();
417
417
  K(this, "token");
@@ -530,7 +530,7 @@ class Nt extends et {
530
530
  }
531
531
  let _;
532
532
  try {
533
- _ = rt.getLogger("vessel");
533
+ _ = ct.getLogger("vessel");
534
534
  } catch {
535
535
  } finally {
536
536
  }
@@ -602,7 +602,7 @@ class yt {
602
602
  const Tt = new yt();
603
603
  let F;
604
604
  try {
605
- F = rt.getLogger("vessel");
605
+ F = ct.getLogger("vessel");
606
606
  } catch {
607
607
  } finally {
608
608
  }
@@ -752,14 +752,14 @@ class O {
752
752
  let m;
753
753
  try {
754
754
  i = (i == null ? void 0 : i.toUpperCase()) === "CMEMS" ? "ECMWF" : i, i = (i == null ? void 0 : i.toUpperCase()) === "METEO2" ? "best_match" : i;
755
- const { weatherModels: f, marineModels: C } = await it.autoPickMeteoModel(i), S = await Mt.spotForecast(e.lat, e.lng, a.utc().format(), !1, !1, !0, {
755
+ const { weatherModels: f, marineModels: C } = await rt.autoPickMeteoModel(i), S = await Mt.spotForecast(e.lat, e.lng, a.utc().format(), !1, !1, !0, {
756
756
  ...d,
757
757
  pastDays: 1,
758
758
  forecastDays: 1,
759
759
  weatherModels: f,
760
760
  marineModels: C
761
- }), [p] = it.pickHourly(S, a);
762
- m = it.toLegacy(p);
761
+ }), [p] = rt.pickHourly(S, a);
762
+ m = rt.toLegacy(p);
763
763
  } catch (f) {
764
764
  F.warn("[%s] meteo2 spot(%j) forecast failed: %s", d.requestId, { ...e, eta: a.utc().format(), source: i }, f);
765
765
  }
@@ -801,7 +801,7 @@ class O {
801
801
  let f = 0, C = 0, S, p;
802
802
  for (let w = 0; w < o.length - 1; w++) {
803
803
  let c = o[w];
804
- c.distanceFromStart = Math.round((n + C) * 1e4) / 1e4;
804
+ c.distanceFromStart = Math.round((n + C) * 1e3) / 1e3;
805
805
  const y = o[w + 1];
806
806
  if (t.bearing = W.calculateBearing(c, y, !y.gcToPrevious), c.bearing = t.bearing, c.suspend && r) {
807
807
  c.eta = c.eta || e.utc().format(), c.elapsed = c.elapsed ?? 0;
@@ -943,21 +943,21 @@ class O {
943
943
  if (x) {
944
944
  const L = q.distanceFromStart - x.distanceFromStart, H = b(q.eta || q.etd).diff(b(x.etd || x.eta), "h", !0);
945
945
  q.avgSpd = Math.round(L / H * 100) / 100;
946
- const nt = W.calculateBearing(x, q);
947
- x.bearing = nt;
946
+ const it = W.calculateBearing(x, q);
947
+ x.bearing = it;
948
948
  }
949
949
  }), y.wps = (Z = y.wps) == null ? void 0 : Z.reduce((q, V) => (q.some((x) => Math.round(x.positionTime / 60) === Math.round(V.positionTime / 60)) || q.push(V), q), []), c.sample = y;
950
950
  const P = y.hours.at(0), T = y.hours.at(-1);
951
- c.distance = Math.round(T.distanceFromStart * 1e4) / 1e4, c.etd = b(P.eta).utc().format(), c.eta = b(T.eta).utc().format(), c.wxFactor = Math.round(I / D * 1e4) / 1e4, c.cFactor = Math.round(k / D * 1e4) / 1e4, c.avgSpeed = Math.round(T.distanceFromStart / D * 1e4) / 1e4, c.totalHrs = Math.round(D * 1e4) / 1e4;
952
- const { distanceInECA: N, hoursInECA: A, totalDgoConsInECA: R, eca: U } = await this.calculateECA(c, i, m), st = J.roundPrecision(i.fo / 24 * (D - A), 3), at = J.roundPrecision(i.dgo / 24 * D, 3);
951
+ c.distance = Math.round(T.distanceFromStart * 1e3) / 1e3, c.etd = b(P.eta).utc().format(), c.eta = b(T.eta).utc().format(), c.wxFactor = Math.round(I / D * 1e3) / 1e3, c.cFactor = Math.round(k / D * 1e3) / 1e3, c.avgSpeed = Math.round(T.distanceFromStart / D * 1e3) / 1e3, c.totalHrs = Math.round(D * 1e3) / 1e3;
952
+ const { distanceInECA: N, hoursInECA: A, totalDgoConsInECA: R, eca: U } = await this.calculateECA(c, i, m), tt = J.roundPrecision(i.fo / 24 * (D - A), 3), at = J.roundPrecision(i.dgo / 24 * D, 3);
953
953
  c.extend = {
954
954
  eca: U,
955
955
  distanceInECA: N,
956
956
  hoursInECA: A,
957
957
  totalDgoConsInECA: R
958
- }, c.totalFoCons = st, c.totalDgoCons = at;
959
- const tt = b().valueOf() - M, ot = (($ = y == null ? void 0 : y.hours) == null ? void 0 : $.length) || 1;
960
- return F == null || F.info("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms", m == null ? void 0 : m.requestId, tt, ot, Math.round(tt / ot * 1e3) / 1e3), c;
958
+ }, c.totalFoCons = tt < 0 ? 0 : tt, c.totalDgoCons = at;
959
+ const et = b().valueOf() - M, nt = (($ = y == null ? void 0 : y.hours) == null ? void 0 : $.length) || 1;
960
+ return F == null || F.info("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms", m == null ? void 0 : m.requestId, et, nt, Math.round(et / nt * 1e3) / 1e3), c;
961
961
  }
962
962
  /**
963
963
  * 分段失速分析(最多走hours 小时)
@@ -1008,8 +1008,8 @@ class O {
1008
1008
  k.wps = ($ = k.wps) == null ? void 0 : $.reduce((x, L) => (x.some((H) => Math.round(b(H.etd).unix() / 60) === Math.round(b(L.etd).unix() / 60)) || x.push(L), x), []), (q = k.wps) == null || q.forEach((x, L) => {
1009
1009
  const H = k.wps[L - 1];
1010
1010
  if (H) {
1011
- const nt = x.distanceFromStart - H.distanceFromStart, dt = b(x.eta || x.etd).diff(b(H.etd || H.eta), "h", !0);
1012
- x.avgSpd = Math.round(nt / dt * 100) / 100;
1011
+ const it = x.distanceFromStart - H.distanceFromStart, dt = b(x.eta || x.etd).diff(b(H.etd || H.eta), "h", !0);
1012
+ x.avgSpd = Math.round(it / dt * 100) / 100;
1013
1013
  const ut = W.calculateBearing(H, x);
1014
1014
  H.bearing = ut;
1015
1015
  }
@@ -1025,23 +1025,23 @@ class O {
1025
1025
  // 注意,可能会在first节点Drift,所有采用eta做为初始出发时间
1026
1026
  etd: b(j.eta).utc().format(),
1027
1027
  eta: b(P == null ? void 0 : P.eta).utc().format(),
1028
- wxFactor: Math.round(h / I * 1e4) / 1e4,
1029
- cFactor: Math.round(u / I * 1e4) / 1e4,
1030
- avgSpeed: Math.round(((P == null ? void 0 : P.distanceFromStart) || 0) / I * 1e4) / 1e4,
1031
- totalHrs: Math.round(I * 1e4) / 1e4,
1028
+ wxFactor: Math.round(h / I * 1e3) / 1e3,
1029
+ cFactor: Math.round(u / I * 1e3) / 1e3,
1030
+ avgSpeed: Math.round(((P == null ? void 0 : P.distanceFromStart) || 0) / I * 1e3) / 1e3,
1031
+ totalHrs: Math.round(I * 1e3) / 1e3,
1032
1032
  from: j,
1033
1033
  to: P,
1034
1034
  route: T,
1035
1035
  waypoints: N,
1036
1036
  v0: f,
1037
1037
  label: C
1038
- }, { distanceInECA: R, hoursInECA: U, totalDgoConsInECA: st, eca: at } = await this.calculateECA(A, n, l), ct = J.roundPrecision(n.fo / 24 * (I - U), 3), tt = J.roundPrecision(n.dgo / 24 * I, 3);
1038
+ }, { distanceInECA: R, hoursInECA: U, totalDgoConsInECA: tt, eca: at } = await this.calculateECA(A, n, l), ot = J.roundPrecision(n.fo / 24 * (I - U), 3), et = J.roundPrecision(n.dgo / 24 * I, 3);
1039
1039
  A.extend = {
1040
1040
  eca: at,
1041
1041
  distanceInECA: R,
1042
1042
  hoursInECA: U,
1043
- totalDgoConsInECA: st
1044
- }, A.totalDgoCons = tt, A.totalFoCons = ct;
1043
+ totalDgoConsInECA: tt
1044
+ }, A.totalDgoCons = et, A.totalFoCons = ot < 0 ? 0 : ot;
1045
1045
  const z = b().valueOf() - g, G = ((V = k == null ? void 0 : k.hours) == null ? void 0 : V.length) || 1;
1046
1046
  return F == null || F.debug("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms", l == null ? void 0 : l.requestId, z, G, Math.round(z / G * 1e3) / 1e3), A;
1047
1047
  }
@@ -1230,7 +1230,7 @@ class O {
1230
1230
  for (const l of i) {
1231
1231
  n += l.distance;
1232
1232
  const g = await W.deadReckoningTime((m = l.waypoints) == null ? void 0 : m.at(0), t.sample.wps), f = await W.deadReckoningTime((M = l.waypoints) == null ? void 0 : M.at(-1), t.sample.wps);
1233
- l.in = g, l.out = f, l.totalHrs = J.roundPrecision((f.positionTime - g.positionTime) / 3600, 2), l.totalDgoCons = J.roundPrecision(e.fo / 24 * l.totalHrs, 3), o += l.totalHrs, s += l.totalDgoCons;
1233
+ l.in = g, l.out = f, l.totalHrs = J.roundPrecision((f.positionTime - g.positionTime) / 3600, 3), l.totalDgoCons = J.roundPrecision(e.fo / 24 * l.totalHrs, 3), o += l.totalHrs, s += l.totalDgoCons;
1234
1234
  }
1235
1235
  return n = J.roundPrecision(n, 3), o = J.roundPrecision(o, 3), s = J.roundPrecision(s, 3), {
1236
1236
  distanceInECA: n,
@@ -1303,7 +1303,7 @@ class O {
1303
1303
  to: t.at(-1).to,
1304
1304
  v0: t.at(0).v0,
1305
1305
  label: "Combined",
1306
- distance: Math.round(i * 1e4) / 1e4,
1306
+ distance: Math.round(i * 1e3) / 1e3,
1307
1307
  totalHrs: Math.round(o * 1e3) / 1e3,
1308
1308
  avgSpeed: Math.round(i / o * 1e3) / 1e3,
1309
1309
  wxFactor: Math.round(r * 1e3) / 1e3,
@@ -1318,7 +1318,7 @@ class O {
1318
1318
  extend: {
1319
1319
  cps: S,
1320
1320
  eca: p,
1321
- distanceInECA: Math.round(n * 1e4) / 1e4,
1321
+ distanceInECA: Math.round(n * 1e3) / 1e3,
1322
1322
  hoursInECA: Math.round(s * 1e3) / 1e3,
1323
1323
  totalDgoConsInECA: Math.round(d * 1e3) / 1e3,
1324
1324
  speeds: t
@@ -1327,7 +1327,7 @@ class O {
1327
1327
  }
1328
1328
  }
1329
1329
  export {
1330
- et as AISImpl,
1330
+ st as AISImpl,
1331
1331
  yt as AlertHelper,
1332
1332
  mt as AlertLevel,
1333
1333
  Et as HifleetImpl,
@@ -1 +1 @@
1
- (function(A,R){typeof exports=="object"&&typeof module<"u"?R(exports,require("got"),require("@log4js-node/log4js-api"),require("moment"),require("@idm-plugin/geo2"),require("@idm-plugin/meteo2"),require("@idm-plugin/meteo")):typeof define=="function"&&define.amd?define(["exports","got","@log4js-node/log4js-api","moment","@idm-plugin/geo2","@idm-plugin/meteo2","@idm-plugin/meteo"],R):(A=typeof globalThis<"u"?globalThis:A||self,R(A["idm-plugin-rabbitmq"]={},A.got,A["@log4js-node/log4js-api"],A.moment,A["@idm-plugin/geo2"],A["@idm-plugin/meteo2"],A["@idm-plugin/meteo"]))})(this,function(A,R,K,p,T,mt,at){"use strict";var St=Object.defineProperty;var kt=(A,R,K)=>R in A?St(A,R,{enumerable:!0,configurable:!0,writable:!0,value:K}):A[R]=K;var z=(A,R,K)=>(kt(A,typeof R!="symbol"?R+"":R,K),K);let g;try{g=K.getLogger("vessel")}catch{}finally{}class G{parseStatus(e){let t,a;switch(e){case 0:t="在航(主机推动)",a="Underway Using Engine";break;case 1:t="锚泊",a="Anchored";break;case 2:t="失控",a="Not operated";break;case 3:t="操纵受限",a="Limited airworthiness";break;case 4:t="吃水受限",a="Limited by ship's draft";break;case 5:t="靠泊",a="Mooring";break;case 6:t="搁浅",a="Stranded";break;case 7:t="捕捞作业",a="Engaged in fishing";break;case 8:t="靠帆船提供动力",a="Sailing";break;default:t="未定义",a="Undefined"}return{labelCn:t,labelEn:a}}}class yt extends G{constructor(t,a){super();z(this,"clientId");z(this,"clientSecret");z(this,"token");this.clientId=t,this.clientSecret=a}async authToken(t={}){const a="https://svc.data.myvessel.cn/ada/oauth/token",i={searchParams:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},o=await R.post(a,i).json();g==null||g.info("[%s] fetch access token from: %s - %j",t.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:p().utc().format()})}async realTimePosition(t,a={}){var d,r,m;(!this.token||p().diff(p(this.token.issuedAt),"seconds")>((d=this.token)==null?void 0:d.expiresIn)-300)&&await this.authToken(a);const i="https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit",o={headers:{Authorization:`${(r=this.token)==null?void 0:r.tokenType} ${(m=this.token)==null?void 0:m.accessToken}`},searchParams:{mmsi:t}};g==null||g.info("[%s] fetch realtime position from: %s - %j",a.requestId,i,o);const n=await R.get(i,o).json();if(n.code)return g==null||g.warn("[%s] fetch realtime position failed: %j",a.requestId,i,{message:n.message,status:n.status,code:n.code}),n;const s=n.data;for(const M in s)!isNaN(s[M])&&Number(s[M])!==1/0&&(s[M]=Number(s[M]));if(s){const M=p(`${s.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:s.mmsi,name:s.vesselName||s.aisVesselName,imo:s.imo,callSign:s.callsign||s.aisCallSign,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:M.unix(),status:s.status,labelCn:s.statusNameCn,labelEn:s.statusNameEn,vesselType:s.vesselTypeNameEn,flag:s.flagCtryNameEn,clasz:s.classSociety,build:s.buildYear,dwt:s.dwt,grt:s.grt,net:s.net,method:"position",vendor:"myVessel",utc:M.utc().format()}}else return{}}async trajectory(t,a,i,o,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(a),m=p(i),M=[];for(;m.diff(r,"day",!0)>30;)await this.trajectoryIn30Day(t,r,r.clone().add(30,"day"),d,o,M,s),r.add(30,"day");return await this.trajectoryIn30Day(t,r,m,d,o,M,s),M}async trajectoryIn30Day(t,a,i,o,n,s,d={}){var f,C,k,w,v;const r="https://svc.data.myvessel.cn/sdc/v1/vessels/status/track",m={headers:{Authorization:`${(f=this.token)==null?void 0:f.tokenType} ${(C=this.token)==null?void 0:C.accessToken}`},json:{mmsi:t,startTime:a.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),endTime:i.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")}};g==null||g.info("[%s] fetch trajectory from: %s - %j",d.requestId,r,m);const M=await R.post(r,m).json();if(M.code)return g==null||g.warn("[%s] fetch trajectory failed: %j",d.requestId,r,{message:M.message,status:M.status,code:M.code}),M;let h=-1;const b=p(`${(w=(k=M.data)==null?void 0:k[0])==null?void 0:w.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return(v=M.data)==null||v.forEach(c=>{for(const P in c)!isNaN(c[P])&&Number(c[P])!==1/0&&(c[P]=Number(c[P]));const y=p(`${c.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),l=c.status,{labelCn:u,labelEn:I}=this.parseStatus(l),F={mmsi:c.mmsi,imo:o==null?void 0:o.imo,lat:c.lat,lng:c.lon,sog:c.sog,cog:c.cog,hdg:c.hdg,draught:c.draught,status:l,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:y.unix(),labelCn:u,labelEn:I,method:"trajectory",vendor:"myVessel",utc:y.utc().format()},N=Math.floor(y.diff(b,"minute",!0)/(n||1));N!==h&&(h=N,s.push(F))}),s}}class Mt extends G{constructor(t){super();z(this,"token");this.token=t}async realTimePosition(t,a={}){const i="https://api.hifleet.com/position/position/get/token",o={searchParams:{mmsi:t,usertoken:this.token}},n=await R.post(i,o).json();g==null||g.info("[%s] fetch realtime position from: %s - %j",a.requestId,i,o);const s=n==null?void 0:n.list;if(!s)return g==null||g.warn("[%s] fetch realtime position failed: %j",a.requestId,i,n),n;for(const b in s)!isNaN(s[b])&&Number(s[b])!==1/0&&(s[b]=Number(s[b]));s.status=s.sp>3?0:1;const d=s.status,{labelCn:r,labelEn:m}=this.parseStatus(d),M=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,vesselType:s.type,dwt:s.dwt,build:s.buildyear,flag:s.fn,positionTime:M.unix(),utc:M.utc().format(),status:d,labelCn:r,labelEn:m,method:"position",vendor:"hifleet"}}async search(t,a={}){let i="https://www.hifleet.com/hifleetapi/searchVesselOL.do";const o={searchParams:{keyword:t},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}};let n=await R.post(i,o).json();g==null||g.info("[%s] fetch vessel props from: %s - %j",a.requestId,i,o),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 R.post(i,o).json(),g==null||g.info("[%s] search vessel dead weight from: %s - %j",a.requestId,i,o),n instanceof Array&&(n=n[0]),n&&(s.deadweight=Number(n.dwt)),s}async suggest(t,a={}){const i="https://www.hifleet.com/hifleetapi/getShipSuggest.do",o={searchParams:{q:t},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}},n=await R.post(i,o).json();g==null||g.info("[%s] suggest vessel props from: %s - %j",a.requestId,i,o);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,a,i,o,n=!0,s={}){var c,y,l;const d=await this.realTimePosition(t,s);let r=p(a);const m=p(i),M=p();if(n){let u=m.diff(r,"d",!0);u<0?r=m.clone().subtract(40,"d"):u<30?r.subtract(10,"d"):u<60?r.subtract(5,"d"):r=m.clone().subtract(80,"d"),u=M.diff(m,"d",!0),m.add(u>10?240:u*24,"h")}const h={searchParams:{endtime:m.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}},b="https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token",f=await R.get(b,h).json();g==null||g.info("[%s] fetch trajectory from: %s - %j",s.requestId,b,h);let C;f&&(C=((y=(c=f.ships)==null?void 0:c.offors)==null?void 0:y.ship)||[],C.length||g==null||g.warn("[%s] fetch trajectory failed: %j",s.requestId,f));const k=[];let w=-1;const v=p(`${(l=C==null?void 0:C[0])==null?void 0:l.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");for(const u of C){for(const L in u)!isNaN(u[L])&&Number(u[L])!==1/0&&(u[L]=Number(u[L]));const I=p(`${u.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");u.status=u.sp>4?0:1;const{labelEn:F,labelCn:N}=this.parseStatus(u.status),P={mmsi:u.m,name:u.n,imo:d==null?void 0:d.imo,lat:u.la,lng:u.lo,draught:u.draught,sog:u.sp,cog:u.co,hdg:u.hdg,positionTime:I.unix(),utc:I.utc().format(),status:u.status,labelCn:N,labelEn:F,method:"trajectory",vendor:"hifleet"},E=Math.floor(I.diff(v,"minute",!0)/(o||1));E!==w&&(w=E,k.push(P))}return k}}class pt extends G{constructor(t){super();z(this,"token");this.token=t}async realTimePosition(t,a={}){const i={searchParams:{id:t,k:this.token,enc:1}},o="https://api.shipxy.com/apicall/GetSingleShip",n=await R.get(o,i).json();if(g==null||g.info("[%s] fetch realtime position from: %s - %j",a.requestId,o,i),(n==null?void 0:n.status)!==0)return n;const s=n.data[0];for(const h in s)!isNaN(s[h])&&Number(s[h])!==1/0&&(s[h]=Number(s[h]));const{labelCn:d,labelEn:r}=await this.parseStatus(s.navistat),m=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:m.utc().format(),status:s.navistat,labelEn:r,labelCn:d,method:"position",vendor:"shipxy"}}async trajectory(t,a,i,o,n=!0,s={}){var v;const d=await this.realTimePosition(t,s),r=p(a),m=p(i),M="https://api.shipxy.com/apicall/GetShipTrack",h={searchParams:{id:t,k:this.token,enc:1,cut:0,btm:r.unix(),etm:m.unix()}},b=await R.get(M,h).json();if(g==null||g.info("[%s] fetch trajectory from: %s - %j",s.requestId,M,h),(b==null?void 0:b.status)!==0)return b;const f=b==null?void 0:b.points,C=[],k=p.unix((v=f[0])==null?void 0:v.utc);let w=-1;for(const c of f){const y=p.unix(c.utc),l={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:y.unix(),utc:y.utc().format(),method:"trajectory",vendor:"shipxy"},u=Math.floor(y.diff(k,"minute",!0)/(o||1));u!==w&&(w=u,C.push(l))}return C}}class bt extends G{constructor(t){super();z(this,"token");this.token=t}async getShipId(t,a={}){const i={headers:{appKey:this.token},json:{mmsiList:t}},o="https://api3.myships.com/sp/ships/getShipIdByMMSI",n=await R.post(o,i).json();return g==null||g.info("[%s] fetch ship id from: %s - %j",a.requestId,o,i),n.code!=="0"?n:n.data[0].shipId}async getShipInfo(t,a={}){const i={headers:{appKey:this.token},json:{shipId:t}},o="https://api3.myships.com/sp/ships/aissta",n=await R.post(o,i).json();if(g==null||g.info("[%s] fetch ship info from: %s - %j",a.requestId,o,i),n.code!=="0")return n;const s=n.data;let d=s.imo;return t==="407170"&&(d="9198379",g==null||g.warn("[%s] ship(%s) imo error: %s, should be %s",a.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,a={}){const i=await this.getShipId(t,a),o=await this.getShipInfo(i,a),n={headers:{appKey:this.token},json:{shipId:i}},s="https://api3.myships.com/sp/ships/position/latest",d=await R.post(s,n).json();g==null||g.info("[%s] fetch realtime position from: %s - %j",a.requestId,s,n);const r=d.data[0];for(const f in r)!isNaN(r[f])&&Number(r[f])!==1/0&&(r[f]=Number(r[f]));const{labelCn:m,labelEn:M}=await this.parseStatus(r.aisNavStatus),h=p.unix(r.posTime);return{...o,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:h.utc().format(),status:r.aisNavStatus,labelEn:M,labelCn:m,method:"position",vendor:"myship"}}async trajectory(t,a,i,o,n=!0,s={}){const d=p(a),r=p(i),m=await this.getShipId(t),M=await this.getShipInfo(m),h=[];for(;r.diff(d,"day",!0)>30;)await this.trajectoryIn30Day(m,d.unix(),d.add(30,"day").unix(),M,t,o,h);return await this.trajectoryIn30Day(m,d.unix(),r.unix(),M,t,o,h),h}async trajectoryIn30Day(t,a,i,o,n,s,d,r={}){var k;const m={headers:{appKey:this.token},json:{shipId:t,startTime:a,endTime:i}},M="https://api3.myships.com/sp/ships/position/history",h=await R.post(M,m).json();if(g==null||g.info("[%s] fetch trajectory from: %s - %j",r.requestId,M,m),h.code!=="0")return g==null||g.warn("[%s] invoke myship trajectory failed: %j",r.requestId,h),h;const b=h.data;for(const w in b)!isNaN(b[w])&&Number(b[w])!==1/0&&(b[w]=Number(b[w]));const f=p.unix((k=b[0])==null?void 0:k.posTime);let C=-1;for(const w of b){const v=p.unix(w.posTime),c={imo:o==null?void 0:o.imo,mmsi:n,lat:Math.round(w.lat/1e4/60*1e5)/1e5,lng:Math.round(w.lon/1e4/60*1e5)/1e5,sog:Math.round(w.sog/10*100)/100,cog:Math.round(w.cog/10*100)/100,hdg:Math.round(w.heading*100)/100,rot:Math.round(w.rot*100)/100,positionTime:v.unix(),utc:v.utc().format(),method:"trajectory",vendor:"myship"},y=Math.floor(v.diff(f,"minute",!0)/(s||1));y!==C&&(C=y,d.push(c))}return d}}let _;try{_=K.getLogger("vessel")}catch{}finally{}var ct=(D=>(D.NOTICE="NOTICE",D.WARN="WARN",D.HEAVY="HEAVY",D.SEVERE="SEVERE",D.ERROR="ERROR",D.FATAL="FATAL",D))(ct||{});class dt{parsePrinciple(e,t={}){var s,d,r;_==null||_.info("[%s] parse rule: %s",t.requestId,e);const a=new RegExp("(?<=\\[)(.+)(?=])","g"),i=e.match(a)?(s=e.match(a))==null?void 0:s[0]:void 0,o=i==null?void 0:i.split(";");if(!o)return;const n={};for(let m=0;m<(o==null?void 0:o.length);m++){const M=(r=(d=o[m].match(a))==null?void 0:d[0])==null?void 0:r.split("],");if(m===0&&!M)n.scope=o[0];else if(M)for(let h=0,b=M.length;h<b;h++){const f=this.parseRule(M[h]);f&&(n[f.level]?f.key?n[f.level][f==null?void 0:f.key]=f:n[f.level]=f:f.key?n[f.level]={[f==null?void 0:f.key]:f}:n[f.level]=f)}}return n}parseRule(e,t={}){var n;_==null||_.debug("[%s] parse rule: %s",t.requestId,e),e=e.startsWith("[")?e:`[${e}`,e=e.endsWith("]")?e:`${e}]`;const a=new RegExp("(?<=\\[)(.+?)(?=])","g"),i=(n=e==null?void 0:e.match(a))==null?void 0:n[0],o=i==null?void 0:i.split(",");if(o)return{operator:o[0],number:Number.isNaN(Number(o[1]))?o[1]:Number(o[1]),level:o[2],time:Number(o[3]),key:o[4]}}checkWeather(e,t,a={}){var f,C,k,w,v,c,y,l,u,I,F,N,P,E,L;let i=0,o=0,n=0,s=0;const d=Math.round(((C=(f=t==null?void 0:t.SEVERE)==null?void 0:f.sigWave)==null?void 0:C.number)*1.6*100)/100,r=(w=(k=t==null?void 0:t.SEVERE)==null?void 0:k.sigWave)==null?void 0:w.number,m=(c=(v=t==null?void 0:t.HEAVY)==null?void 0:v.sigWave)==null?void 0:c.number,M=Math.round((((l=(y=t==null?void 0:t.SEVERE)==null?void 0:y.wind)==null?void 0:l.number)+2)*100)/100,h=(I=(u=t==null?void 0:t.SEVERE)==null?void 0:u.wind)==null?void 0:I.number,b=(N=(F=t==null?void 0:t.HEAVY)==null?void 0:F.wind)==null?void 0:N.number;for(let x=0;x<(e==null?void 0:e.length);x++){const H=e[x],q=(E=(P=H==null?void 0:H.meteo)==null?void 0:P.wave)==null?void 0:E.sig,V=(L=H==null?void 0:H.meteo)==null?void 0:L.wind,U=x?p(H.eta).diff(p(e[x-1].eta),"hour",!0):0;s=U>s?U:s,_==null||_.debug("[%s] check sig.wave: %j",a.requestId,{...q,dgThd4Wv:d,svThd4Wv:r,hvThd4Wv:m}),(q==null?void 0:q.height)>=d?H.isDangerous=!0:(q==null?void 0:q.height)>=r?H.isSevere=!0:(q==null?void 0:q.height)>=m&&(H.isHeavy=!0),_==null||_.debug("[%s] check wind: %j",a.requestId,{...V,dgThd4Wd:M,svThd4Wd:h,hvThd4Wd:b}),(V==null?void 0:V.scale)>=M?(H.isDangerous=!0,delete H.isSevere,delete H.isHeavy):(V==null?void 0:V.scale)>h?(H.isDangerous||(H.isSevere=!0),delete H.isHeavy):(V==null?void 0:V.scale)===b&&!H.isDangerous&&!H.isSevere&&(H.isHeavy=!0),i+=H.isDangerous?U:0,o+=H.isSevere?U:0,n+=H.isHeavy?U:0}return i=Math.round(i*100)/100,o=Math.round(o*100)/100,n=Math.round(n*100)/100,s=Math.round(s),{sample:e,dangerous:i,severe:o,heavy:n,step:s<3?3:s,wind:{dgThd4Wd:M,svThd4Wd:h,hvThd4Wd:b},sig:{dgThd4Wv:d,svThd4Wv:r,hvThd4Wv:m}}}}const gt=new dt;let S;try{S=K.getLogger("vessel")}catch{}finally{}const vt=new mt.MeteoHelper2("",!0);var ut=(D=>(D.common="common",D.container="container",D.tugs="tugs",D))(ut||{}),lt=(D=>(D.Ballast="Ballast",D.Laden="Laden",D))(lt||{}),ht=(D=>(D.Cp="CP",D.Perf="Basis",D.Instruct="Other",D))(ht||{});class W{static blockCoefficient(e,t,a,i){let o=Math.round(e/(t*a*i)*100)/100;o=o<.55?.55:o>.85?.85:o;const n=[.55,.6,.65,.7,.75,.8,.85],s=n.map(d=>Math.abs(d-o));return n[s.indexOf(Math.min(...s))]}static froudeNumber(e,t,a=9.8){let i=Math.round(Math.sqrt(e*e/(a*t))*100)/100;return i=i<.05?.05:i>.3?.3:i,i}static amendFactor(e,t,a){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 a==="Laden"&&(n=i[e]),n[0]+n[1]*t+n[2]*Math.pow(t,2)}static directionFactor(e,t=0){let a;return e>30&&e<=60?a=(1.7-.03*Math.pow(t-4,2))/2:e>60&&e<=150?a=(.9-.06*Math.pow(t-6,2))/2:e>150&&e<=180?a=(.4-.03*Math.pow(t-8,2))/2:a=1,Math.round(a*1e5)/1e5}static vesselTagFactor(e,t,a,i=0){i=i>6?i-.9*(i-6):i;let o;return a==="container"?o=.7*i+Math.pow(i,6.5)/(22*Math.pow(e,2/3)):t==="Ballast"?o=.7*i+Math.pow(i,6.5)/(2.7*Math.pow(e,2/3)):o=.5*i+Math.pow(i,6.5)/(2.7*Math.pow(e,2/3)),o}static waveHeightFactor(e,t){e=e<0?.2:e,e=e>6?e-.9*(e-6):e,e=e>9?9:e;let a;return t>30&&t<=60?a=-.6:t>60&&t<=90?a=-.4:t>90&&t<=120?a=e<3?.4:-.3:t>120&&t<=150?a=e<3?.6:-.5:t>150&&t<=180?a=e<3?.7:-.6:a=-.7,Math.round(a*(.144*Math.pow(e,2)+.178*e)*1e4)/1e4}static assembleProperties(e,t,a,i){var h;const o=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=((h=e==null?void 0:e.type)==null?void 0:h.toLowerCase())||"common";return{tag:r.indexOf("container")>-1?"container":r.indexOf("tugs")>-1?"tugs":"common",lbp:o,loadCondition:t,draught:n,breadthMoulded:s,displacement:Math.round((d/1.025+n*s*o*.7)*1e4)/1e4,speed:Math.round((a??14.1382)*1852/3600*1e4)/1e4,bearing:i||90}}static async speedLoseAt(e,t,a,i="",o=2,n=!0,s=!1,d={}){let r;if(t.velocity&&s&&(e.speed=T.LngLatHelper.roundPrecision(t.velocity*1852/3600,6)),n){let m;try{i=(i==null?void 0:i.toUpperCase())==="CMEMS"?"ECMWF":i,i=(i==null?void 0:i.toUpperCase())==="METEO2"?"best_match":i;const{weatherModels:f,marineModels:C}=await at.Meteo2Assist.autoPickMeteoModel(i),k=await vt.spotForecast(t.lat,t.lng,a.utc().format(),!1,!1,!0,{...d,pastDays:1,forecastDays:1,weatherModels:f,marineModels:C}),[w]=at.Meteo2Assist.pickHourly(k,a);m=at.Meteo2Assist.toLegacy(w)}catch(f){S.warn("[%s] meteo2 spot(%j) forecast failed: %s",d.requestId,{...t,eta:a.utc().format(),source:i},f)}const M=W.weatherFactor(e,m),h=W.currentFactor(e.bearing,m==null?void 0:m.current,o),b=Math.round((e.speed*1.943844+M+h)*100)/100;r={meteo:{...m},wxFactor:M,cFactor:h,speed:t.velocity&&s?t.velocity:b<0?1:b,eta:a.utc().format(),etd:a.utc().format()}}else r={wxFactor:0,cFactor:0,speed:t.velocity&&s?t.velocity:Math.round((e.speed*1.943844+0+0)*100)/100,eta:a.utc().format(),etd:a.utc().format()};return delete t.meteo,delete t.wxFactor,delete t.cFactor,delete t.speed,delete t.etd,{...r,...t}}static async speedLoseInHoursStep(e,t,a,i,o,n,s="",d=!0,r=!1,m={}){t.utc();const M=t.clone().add(14,"days"),h=[],b=[];let f=0,C=0,k,w;for(let v=0;v<n.length-1;v++){let c=n[v];c.distanceFromStart=Math.round((o+C)*1e4)/1e4;const y=n[v+1];if(e.bearing=T.LaneHelper.calculateBearing(c,y,!y.gcToPrevious),c.bearing=e.bearing,c.suspend&&r){c.eta=c.eta||t.utc().format(),c.elapsed=c.elapsed??0;const I=c.suspend-c.elapsed;if(i-f>I)i=i-f-I,t.add(I,"hour"),c.elapsed=c.suspend;else{const F=i-f;c.elapsed+=F,t.add(F,"hour"),i=0}if(S==null||S.info(`[%s] suspend ${c.elapsed} hours at %j, and remain ${i} hours need to go...`,m.requestId,c),i===0)return c.distanceFromPrevious=C,{etd:t,from:w||c,to:c,next:n.filter(F=>F),wps:h,days:b}}else c.suspend=0;d=t.isAfter(M)?!1:d,c=await W.speedLoseAt(e,c,t,s,0,d,r,m),w=w||c,c.important&&h.push(c),t.isSameOrAfter(a)&&(b.push(c),a.add(24,"hour"));const l=T.LaneHelper.calculateDistance(c,y,!y.gcToPrevious);let u=Math.round(l/w.speed*1e5)/1e5;if(f+u<i){if(f+=u,t.add(u,"hour"),delete n[v],S==null||S.debug(`[%s] go to %j from %j with ${l}nm, and cost ${u} hours`,m.requestId,{lat:y.lat,lng:y.lng},{lat:w.lat,lng:w.lng,etd:w.etd}),C+=l,n.filter(I=>I).length<=1){k=y,k.eta=t.utc().format(),k.distanceFromPrevious=l,k.distanceFromStart=Math.round((o+C)*1e4)/1e4,h.push(k),delete n[v+1];break}}else{u=i-f,t.add(u,"hour");const I=T.LngLatHelper.roundPrecision(w.speed*u,5);k=T.LaneHelper.calculateCoordinate(c,e.bearing,I,"nauticalmiles",!y.gcToPrevious),k.eta=t.utc().format(),n[v]=k,S==null||S.debug(`[%s] go to %j from %j with ${I}nm, and cost ${u} hours`,m.requestId,{lat:k.lat,lng:k.lng},{lat:c.lat,lng:c.lng,etd:c.etd}),C+=I,k.distanceFromPrevious=Math.round(C*1e4)/1e4,k.distanceFromStart=Math.round((o+C)*1e4)/1e4;break}}return{etd:t,from:w,to:k,next:n.filter(v=>v),wps:h,days:b}}static currentFactor(e,t,a=0){const i=(e-(t==null?void 0:t.degree)||0)/180*Math.PI;if(Math.abs(i)===Math.PI/2)return 0;let o=((t==null?void 0:t.kts)||0)*Math.cos(i);return a&2?o=Math.ceil(o*100)/100:a&1?o=Math.floor(o*100)/100:o=Math.round(o*100)/100,Math.abs(o)>5?0:o}static weatherFactor(e,t){var M,h,b,f,C,k,w;S==null||S.debug("calculate weather factor via: %j",{...e,...t});const a=W.blockCoefficient(e.displacement,e.lbp,e.breadthMoulded,e.draught),i=W.froudeNumber(e.speed,e.lbp),o=W.amendFactor(a,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=W.directionFactor(n,(h=t==null?void 0:t.wind)==null?void 0:h.scale),d=W.vesselTagFactor(e.displacement,e.loadCondition,e.tag,(b=t==null?void 0:t.wind)==null?void 0:b.scale);let r=s*o*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)),S==null||S.debug("wind wx factor = %d",r),n=Math.abs(e.bearing%360-(((C=(f=t==null?void 0:t.wave)==null?void 0:f.sig)==null?void 0:C.degree)%360||0));const m=W.waveHeightFactor(((w=(k=t==null?void 0:t.wave)==null?void 0:k.sig)==null?void 0:w.height)??1,n);return S==null||S.debug("wave wx factor = %d",m),r=r*.4+m,S==null||S.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,a,i,o,n="",s=0,d=!0,r=!1,m={}){var Q,X,Z,$,tt,et;const M=p().valueOf();e.lng=T.LngLatHelper.convertToStdLng(e.lng);const{route:h,waypoints:b}=o.points,f=T.LaneHelper.calculateSubRoute(e,h);if(((Q=f[0])==null?void 0:Q.length)<=1)return;const{v0:C,label:k}=e.sog?{v0:e.sog,label:e.label||"Other"}:{v0:i.speed,label:"CP"},w=W.assembleProperties(a,i.loadCondition,C,0),v=b.length?T.LaneHelper.calculateSubWaypoints(e,b):[];v.forEach(Y=>Y.important=!0);const c={from:{...e},route:f,waypoints:v,v0:C,label:k},y={hours:[],days:[],wps:[]};s||(T.LaneHelper.calculateRouteDistance(f)/i.speed<=72?s=3:s=6);let l=T.LaneHelper.simplifyRouteToCoordinates(f,v,0),u=0,I=0,F=0,N=0;t=p(t).utc();const P=t.clone();for(;l.length>0;){const Y=s-t.hour()%s,J=Math.ceil(t.clone().add(Y,"h").set({minute:0,second:0,millisecond:0}).diff(t,"h",!0)*1e4)/1e4,j=await W.speedLoseInHoursStep(w,t,P,J,u,l,n,d,r,m);(X=j.from)!=null&&X.speed&&(y.hours.push(j.from),y.wps.push(...j.wps),y.days.push(...j.days)),l=j==null?void 0:j.next,l.length||y.hours.push(j==null?void 0:j.to),u+=Math.round((((Z=j==null?void 0:j.to)==null?void 0:Z.distanceFromPrevious)??0)*1e4)/1e4}const E=y.hours;for(let Y=0;Y<E.length-1;Y++){const J=p(E[Y+1].eta).diff(E[Y].etd,"hour",!0)||1;I+=(E[Y].wxFactor||0)*J,F+=(E[Y].cFactor||0)*J,N+=J}($=y.wps)==null||$.forEach((Y,J)=>{Y.positionTime=p.utc(Y.etd||Y.eta).unix();const j=y.wps[J-1];if(j){const B=Y.distanceFromStart-j.distanceFromStart,O=p(Y.eta||Y.etd).diff(p(j.etd||j.eta),"h",!0);Y.avgSpd=Math.round(B/O*100)/100;const rt=T.LaneHelper.calculateBearing(j,Y);j.bearing=rt}}),y.wps=(tt=y.wps)==null?void 0:tt.reduce((Y,J)=>(Y.some(j=>Math.round(j.positionTime/60)===Math.round(J.positionTime/60))||Y.push(J),Y),[]),c.sample=y;const L=y.hours.at(0),x=y.hours.at(-1);c.distance=Math.round(x.distanceFromStart*1e4)/1e4,c.etd=p(L.eta).utc().format(),c.eta=p(x.eta).utc().format(),c.wxFactor=Math.round(I/N*1e4)/1e4,c.cFactor=Math.round(F/N*1e4)/1e4,c.avgSpeed=Math.round(x.distanceFromStart/N*1e4)/1e4,c.totalHrs=Math.round(N*1e4)/1e4;const{distanceInECA:H,hoursInECA:q,totalDgoConsInECA:V,eca:U}=await this.calculateECA(c,i,m),nt=T.LngLatHelper.roundPrecision(i.fo/24*(N-q),3),ot=T.LngLatHelper.roundPrecision(i.dgo/24*N,3);c.extend={eca:U,distanceInECA:H,hoursInECA:q,totalDgoConsInECA:V},c.totalFoCons=nt,c.totalDgoCons=ot;const st=p().valueOf()-M,it=((et=y==null?void 0:y.hours)==null?void 0:et.length)||1;return S==null||S.info("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms",m==null?void 0:m.requestId,st,it,Math.round(st/it*1e3)/1e3),c}static async analyseInstantWithThreshed(e,t,a,i,o,n,s,d="",r=3,m=!0,M=!1,h={}){var Z,$,tt,et,Y,J;const b=p().valueOf();e.lng=T.LngLatHelper.convertToStdLng(e.lng);const{v0:f,label:C}=e.sog?{v0:e.sog,label:e.label||"Other"}:{v0:o.speed,label:"CP"},k=W.assembleProperties(i,o.loadCondition,f,0),w=T.LaneHelper.calculateSubRoute(e,n);if(((Z=w[0])==null?void 0:Z.length)<=1)return;const v=s.length?T.LaneHelper.calculateSubWaypoints(e,s):[];v.forEach(j=>j.important=!0);let c=T.LaneHelper.simplifyRouteToCoordinates(w,v,0),y=0,l=0,u=0,I=0;const F={hours:[],wps:[],days:[]};t=p(t).utc();const N=t.clone();for(;c.length>0;){const j=r-t.hour()%r;let B=Math.ceil(t.clone().add(j,"h").set({minute:0,second:0,millisecond:0}).diff(t,"h",!0)*1e4)/1e4;B=t.clone().add(B,"h").isSameOrAfter(a)?a.diff(t,"h",!0)*1e4/1e4:B;const O=await W.speedLoseInHoursStep(k,t,N,B,y,c,d,m,M,h);if(($=O.from)!=null&&$.speed&&(F.hours.push(O.from),O!=null&&O.wps&&F.wps.push(...O.wps),F.days.push(...O.days)),c=O==null?void 0:O.next,c.length||F.hours.push(O==null?void 0:O.to),y+=Math.round((((tt=O==null?void 0:O.to)==null?void 0:tt.distanceFromPrevious)??0)*1e4)/1e4,!B)break}F.wps=(et=F.wps)==null?void 0:et.reduce((j,B)=>(j.some(O=>Math.round(p(O.etd).unix()/60)===Math.round(p(B.etd).unix()/60))||j.push(B),j),[]),(Y=F.wps)==null||Y.forEach((j,B)=>{const O=F.wps[B-1];if(O){const rt=j.distanceFromStart-O.distanceFromStart,wt=p(j.eta||j.etd).diff(p(O.etd||O.eta),"h",!0);j.avgSpd=Math.round(rt/wt*100)/100;const It=T.LaneHelper.calculateBearing(O,j);O.bearing=It}});const P=F.hours;for(let j=0;j<P.length-1;j++){const B=p(P[j+1].eta).diff(P[j].etd,"hour",!0);l+=P[j].wxFactor*B,u+=P[j].cFactor*B,I+=B}const E=F.hours.at(0),L=F.hours.at(-1),x=await T.LaneHelper.calculateRangeRoute(E,L,w),H=await T.LaneHelper.calculateRangeWaypoints(E,L,w,v),q={sample:F,distance:Math.round(((L==null?void 0:L.distanceFromStart)||0)*1e4)/1e4,etd:p(E.eta).utc().format(),eta:p(L==null?void 0:L.eta).utc().format(),wxFactor:Math.round(l/I*1e4)/1e4,cFactor:Math.round(u/I*1e4)/1e4,avgSpeed:Math.round(((L==null?void 0:L.distanceFromStart)||0)/I*1e4)/1e4,totalHrs:Math.round(I*1e4)/1e4,from:E,to:L,route:x,waypoints:H,v0:f,label:C},{distanceInECA:V,hoursInECA:U,totalDgoConsInECA:nt,eca:ot}=await this.calculateECA(q,o,h),ft=T.LngLatHelper.roundPrecision(o.fo/24*(I-U),3),st=T.LngLatHelper.roundPrecision(o.dgo/24*I,3);q.extend={eca:ot,distanceInECA:V,hoursInECA:U,totalDgoConsInECA:nt},q.totalDgoCons=st,q.totalFoCons=ft;const Q=p().valueOf()-b,X=((J=F==null?void 0:F.hours)==null?void 0:J.length)||1;return S==null||S.debug("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms",h==null?void 0:h.requestId,Q,X,Math.round(Q/X*1e3)/1e3),q}static async analyseCost(e,t,a,i,o={}){var w,v;const n=p().valueOf(),s=[];e.speedStep=e.speedStep||3,e.alterStep=e.alterStep??1;const d=T.LaneHelper.calculateRouteDistance(i.route);let r=0;a.forEach(c=>{const y=Math.ceil(d/c.speed/24);r=r<y?y:r}),r=r*1.3;const m=p.utc(e.etd).add(r??14,"day");let M=1;for(const c of a){const y=JSON.parse(JSON.stringify(i.route)),l=JSON.parse(JSON.stringify(i.waypoints)),u=await W.analyseInstantWithThreshed({lat:e.lat,lng:e.lng},e.etd,m,t,c,y,l,e.meteoVendor,e.speedStep,e.useMeteo,e.useRouteParam,o);u&&(await W.calculateCost(u,c,e,o),s.push(u),S==null||S.info("[%s][L%d-%d] analyse from %s to %s cost: %j",o.requestId,1,M,e.etd,m.format(),{cost:u.cost.total,hire:u.cost.hire,bunker:u.cost.bunker,distance:u.distance,hours:u.totalHrs,cp:`${c.speed}/${c.fo}/${c.dgo}`})),M++}s.sort((c,y)=>c.cost.total-y.cost.total);const h=s.at(0),b=s.at(1),f=[];if(f.push({combined:!1,speeds:[h],cost:(w=h.cost)==null?void 0:w.total}),b){const c=h.cost.cp,y=b.cost.cp,l=p(h.eta),u=p(h.etd),I=l.diff(u,"days",!0);let F=Math.ceil(I/2);F=F>7?7:F<e.alterStep?e.alterStep:F;let N=2,P={combined:!1,speeds:[b],cost:(v=b.cost)==null?void 0:v.total},E;for(;F>=e.alterStep;){const L=await W.combinedAnalyse(e,t,m,[c,y],i,F,{...o,level:N});if(P.cost>L.cost?E?(E==null?void 0:E.cost)>L.cost&&(E=L):(E=P,P=L):(!E||(E==null?void 0:E.cost)>L.cost)&&(E=L),F<=e.alterStep)break;F=Math.ceil(F/2),N+=1}f.push(P),E&&f.push(E)}const k=p().valueOf()-n;return S==null||S.info("[%s] analyse elapsed: %d ms",o==null?void 0:o.requestId,k),f.sort((c,y)=>c.cost-y.cost)}static async combinedAnalyse(e,t,a,i,o,n,s={}){s.counter=1,S==null||S.info("[%s][L%d] analyse with alternate cp in every %d days",s.requestId,s.level,n);const d=await W.alternateAnalyse(e,t,a,i,0,o,n,s),r=d.reduce((y,l)=>y+l.cost.total,0),m=d.reduce((y,l)=>y+l.cost.hire,0),M=d.reduce((y,l)=>y+l.cost.bunker,0),h=d.reduce((y,l)=>y+l.distance,0),b=d.reduce((y,l)=>y+l.totalHrs,0);S==null||S.info("[%s][L%d] cost with cpa/cpb turn: %j",s.requestId,s.level,{cost:r,hire:m,bunker:M,distance:h,hours:b});const f=await W.alternateAnalyse(e,t,a,i,1,o,n,s),C=f.reduce((y,l)=>y+l.cost.total,0),k=f.reduce((y,l)=>y+l.cost.hire,0),w=f.reduce((y,l)=>y+l.cost.bunker,0),v=f.reduce((y,l)=>y+l.distance,0),c=f.reduce((y,l)=>y+l.totalHrs,0);return S==null||S.info("[%s][L%d] cost with cpb/cpa turn: %j",s.requestId,s.level,{cost:C,hire:k,bunker:w,distance:v,hours:c}),r<C?{combined:!0,cost:Math.round(r*1e3)/1e3,speeds:d,step:n}:{combined:!0,cost:Math.round(C*1e3)/1e3,speeds:f,step:n}}static async alternateAnalyse(e,t,a,i,o,n,s,d={}){var h,b;let r=p.utc(e.etd);const m={lat:e.lat,lng:e.lng},M=[];for(;r.isBefore(a);){const f=r.clone().utc().add(s,"day"),C=JSON.parse(JSON.stringify(n.route)),k=JSON.parse(JSON.stringify(n.waypoints)),w=i[o],v=await W.analyseInstantWithThreshed(m,r.utc().format(),f,t,w,C,k,e.meteoVendor,e.speedStep,e.useMeteo,e.useRouteParam,d);v&&(await W.calculateCost(v,w,e,d),S==null||S.info("[%s][L%d-%d] analyse from %s to %s cost: %j",d.requestId,d.level,d.counter,r.utc().format(),f.utc().format(),{cost:v.cost.total,hire:v.cost.hire,bunker:v.cost.bunker,distance:v.distance,hours:v.totalHrs,cp:`${w.speed}/${w.fo}/${w.dgo}`})),d.counter=d.counter+1;const c=(b=(h=v==null?void 0:v.sample)==null?void 0:h.hours)==null?void 0:b.at(-1);if(c)m.lat=c.lat,m.lng=c.lng,r=p(c.eta),M.push(v),o=o?0:1;else break}return M}static async calculateCost(e,t,a,i={}){var o;if(e){const n=(a.addComm||0)>=1?(a.addComm||0)/100:a.addComm||0,s=Math.round(e.totalHrs/24*(a.dailyHire||0)*(1-n)*1e3)/1e3,d=Math.round(e.totalFoCons*(a.priceFO||0)*1e3)/1e3,r=Math.round((e.totalDgoCons+(((o=e.extend)==null?void 0:o.totalDgoConsInECA)||0))*(a.priceDGO||0)*1e3)/1e3;e.cost={total:Math.round((s+d+r)*1e3)/1e3,hire:s,bunker:Math.round((d+r)*1e3)/1e3,cp:t}}return e}static async calculateECA(e,t,a={}){var d,r,m,M;const i=await T.LaneHelper.intersectInECA((e==null?void 0:e.route)||[]);let o=0,n=0,s=0;(r=(d=e==null?void 0:e.sample)==null?void 0:d.wps)==null||r.forEach(h=>{h.positionTime=p.utc(h.etd||h.eta).unix()});for(const h of i){o+=h.distance;const b=await T.LaneHelper.deadReckoningTime((m=h.waypoints)==null?void 0:m.at(0),e.sample.wps),f=await T.LaneHelper.deadReckoningTime((M=h.waypoints)==null?void 0:M.at(-1),e.sample.wps);h.in=b,h.out=f,h.totalHrs=T.LngLatHelper.roundPrecision((f.positionTime-b.positionTime)/3600,2),h.totalDgoCons=T.LngLatHelper.roundPrecision(t.fo/24*h.totalHrs,3),n+=h.totalHrs,s+=h.totalDgoCons}return o=T.LngLatHelper.roundPrecision(o,3),n=T.LngLatHelper.roundPrecision(n,3),s=T.LngLatHelper.roundPrecision(s,3),{distanceInECA:o,hoursInECA:n,totalDgoConsInECA:s,eca:i}}static async mergeSpeeds(e,t={}){var c,y;const a={hours:[],wps:[],days:[]},i=e.reduce((l,u)=>l+u.distance,0),o=e.reduce((l,u)=>{var I;return l+(((I=u.extend)==null?void 0:I.distanceInECA)||0)},0),n=e.reduce((l,u)=>l+u.totalHrs,0),s=e.reduce((l,u)=>{var I;return l+(((I=u.extend)==null?void 0:I.hoursInECA)||0)},0),d=e.reduce((l,u)=>{var I;return l+(((I=u.extend)==null?void 0:I.totalDgoConsInECA)||0)},0),r=e.reduce((l,u)=>l+u.wxFactor*u.totalHrs/n,0),m=e.reduce((l,u)=>l+u.cFactor*u.totalHrs/n,0),M=e.reduce((l,u)=>l+u.totalFoCons,0),h=e.reduce((l,u)=>l+u.totalDgoCons,0),b=e.reduce((l,u)=>l+u.cost.total,0),f=e.reduce((l,u)=>l+u.cost.hire,0),C=e.reduce((l,u)=>l+u.cost.bunker,0),k=[],w=[];let v;for(const l of e){w.push(...((c=l.extend)==null?void 0:c.eca)||[]);const u=l.sample.hours,I=l.sample.wps,F=l.sample.days,N=u.at(0);v&&(N.distanceFromPrevious=v.distanceFromPrevious,N.distanceFromStart=v.distanceFromStart,u.forEach((x,H)=>{H&&(x.distanceFromStart=x.distanceFromStart+v.distanceFromStart)}),I.at(0).distanceFromPrevious=v.distanceFromPrevious,I.at(0).distanceFromStart=v.distanceFromStart,I.forEach((x,H)=>{H&&(x.distanceFromStart=x.distanceFromStart+v.distanceFromStart)}),F.at(0).distanceFromPrevious=v.distanceFromPrevious,F.at(0).distanceFromStart=v.distanceFromStart,F.forEach((x,H)=>{H&&(x.distanceFromStart=x.distanceFromStart+v.distanceFromStart)})),N.cp=l.cost.cp;const P=[l.etd,l.eta],E=k.findIndex(x=>x.id===N.cp.id);E===-1?(N.cp.segment=[P],k.push(N.cp)):k[E].segment.push(P),u.forEach(x=>{var q;((q=a.hours)==null?void 0:q.findIndex(V=>V.eta===x.eta))===-1&&a.hours.push(x)}),I.forEach(x=>{var q;((q=a.wps)==null?void 0:q.findIndex(V=>V.eta===x.eta))===-1&&a.wps.push(x)}),F.forEach(x=>{var q;((q=a==null?void 0:a.days)==null?void 0:q.findIndex(V=>V.eta===x.eta))===-1&&a.days.push(x)});const L=(y=a.wps)==null?void 0:y.findIndex(x=>x.eta===N.eta);L===-1?a.wps.push(N):a.wps[L]=N,v=u.at(-1)}return a.wps.sort((l,u)=>{p(l.etd).unix()-p(u.etd).unix()}),a.wps.forEach((l,u)=>{const I=a.wps[u-1];if(I){const F=l.distanceFromStart-(I.distanceFromStart||0),N=p(l.eta||l.etd).diff(p(I.etd||I.eta),"hour",!0),P=Math.round(F/N*100)/100;l.avgSpd=P;const E=T.LaneHelper.calculateBearing(I,l);I.bearing=E}}),{sample:a,etd:e.at(0).etd,eta:e.at(-1).eta,from:e.at(0).from,to:e.at(-1).to,v0:e.at(0).v0,label:"Combined",distance:Math.round(i*1e4)/1e4,totalHrs:Math.round(n*1e3)/1e3,avgSpeed:Math.round(i/n*1e3)/1e3,wxFactor:Math.round(r*1e3)/1e3,cFactor:Math.round(m*1e3)/1e3,totalFoCons:Math.round(M*1e3)/1e3,totalDgoCons:Math.round(h*1e3)/1e3,cost:{total:Math.round(b*1e3)/1e3,hire:Math.round(f*1e3)/1e3,bunker:Math.round(C*1e3)/1e3},extend:{cps:k,eca:w,distanceInECA:Math.round(o*1e4)/1e4,hoursInECA:Math.round(s*1e3)/1e3,totalDgoConsInECA:Math.round(d*1e3)/1e3,speeds:e}}}}A.AISImpl=G,A.AlertHelper=dt,A.AlertLevel=ct,A.HifleetImpl=Mt,A.LoadCondition=lt,A.MyShipImpl=bt,A.MyVesselImpl=yt,A.ShipxyImpl=pt,A.SpeedHelper=W,A.SpeedLabel=ht,A.VesselTag=ut,A.alertHelper=gt,Object.defineProperty(A,Symbol.toStringTag,{value:"Module"})});
1
+ (function(A,R){typeof exports=="object"&&typeof module<"u"?R(exports,require("got"),require("@log4js-node/log4js-api"),require("moment"),require("@idm-plugin/geo2"),require("@idm-plugin/meteo2"),require("@idm-plugin/meteo")):typeof define=="function"&&define.amd?define(["exports","got","@log4js-node/log4js-api","moment","@idm-plugin/geo2","@idm-plugin/meteo2","@idm-plugin/meteo"],R):(A=typeof globalThis<"u"?globalThis:A||self,R(A["idm-plugin-rabbitmq"]={},A.got,A["@log4js-node/log4js-api"],A.moment,A["@idm-plugin/geo2"],A["@idm-plugin/meteo2"],A["@idm-plugin/meteo"]))})(this,function(A,R,K,p,T,mt,nt){"use strict";var St=Object.defineProperty;var kt=(A,R,K)=>R in A?St(A,R,{enumerable:!0,configurable:!0,writable:!0,value:K}):A[R]=K;var z=(A,R,K)=>(kt(A,typeof R!="symbol"?R+"":R,K),K);let g;try{g=K.getLogger("vessel")}catch{}finally{}class G{parseStatus(e){let t,a;switch(e){case 0:t="在航(主机推动)",a="Underway Using Engine";break;case 1:t="锚泊",a="Anchored";break;case 2:t="失控",a="Not operated";break;case 3:t="操纵受限",a="Limited airworthiness";break;case 4:t="吃水受限",a="Limited by ship's draft";break;case 5:t="靠泊",a="Mooring";break;case 6:t="搁浅",a="Stranded";break;case 7:t="捕捞作业",a="Engaged in fishing";break;case 8:t="靠帆船提供动力",a="Sailing";break;default:t="未定义",a="Undefined"}return{labelCn:t,labelEn:a}}}class yt extends G{constructor(t,a){super();z(this,"clientId");z(this,"clientSecret");z(this,"token");this.clientId=t,this.clientSecret=a}async authToken(t={}){const a="https://svc.data.myvessel.cn/ada/oauth/token",i={searchParams:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},o=await R.post(a,i).json();g==null||g.info("[%s] fetch access token from: %s - %j",t.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:p().utc().format()})}async realTimePosition(t,a={}){var d,r,m;(!this.token||p().diff(p(this.token.issuedAt),"seconds")>((d=this.token)==null?void 0:d.expiresIn)-300)&&await this.authToken(a);const i="https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit",o={headers:{Authorization:`${(r=this.token)==null?void 0:r.tokenType} ${(m=this.token)==null?void 0:m.accessToken}`},searchParams:{mmsi:t}};g==null||g.info("[%s] fetch realtime position from: %s - %j",a.requestId,i,o);const n=await R.get(i,o).json();if(n.code)return g==null||g.warn("[%s] fetch realtime position failed: %j",a.requestId,i,{message:n.message,status:n.status,code:n.code}),n;const s=n.data;for(const M in s)!isNaN(s[M])&&Number(s[M])!==1/0&&(s[M]=Number(s[M]));if(s){const M=p(`${s.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:s.mmsi,name:s.vesselName||s.aisVesselName,imo:s.imo,callSign:s.callsign||s.aisCallSign,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:M.unix(),status:s.status,labelCn:s.statusNameCn,labelEn:s.statusNameEn,vesselType:s.vesselTypeNameEn,flag:s.flagCtryNameEn,clasz:s.classSociety,build:s.buildYear,dwt:s.dwt,grt:s.grt,net:s.net,method:"position",vendor:"myVessel",utc:M.utc().format()}}else return{}}async trajectory(t,a,i,o,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(a),m=p(i),M=[];for(;m.diff(r,"day",!0)>30;)await this.trajectoryIn30Day(t,r,r.clone().add(30,"day"),d,o,M,s),r.add(30,"day");return await this.trajectoryIn30Day(t,r,m,d,o,M,s),M}async trajectoryIn30Day(t,a,i,o,n,s,d={}){var f,C,k,w,v;const r="https://svc.data.myvessel.cn/sdc/v1/vessels/status/track",m={headers:{Authorization:`${(f=this.token)==null?void 0:f.tokenType} ${(C=this.token)==null?void 0:C.accessToken}`},json:{mmsi:t,startTime:a.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),endTime:i.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")}};g==null||g.info("[%s] fetch trajectory from: %s - %j",d.requestId,r,m);const M=await R.post(r,m).json();if(M.code)return g==null||g.warn("[%s] fetch trajectory failed: %j",d.requestId,r,{message:M.message,status:M.status,code:M.code}),M;let h=-1;const b=p(`${(w=(k=M.data)==null?void 0:k[0])==null?void 0:w.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return(v=M.data)==null||v.forEach(c=>{for(const P in c)!isNaN(c[P])&&Number(c[P])!==1/0&&(c[P]=Number(c[P]));const y=p(`${c.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),l=c.status,{labelCn:u,labelEn:I}=this.parseStatus(l),F={mmsi:c.mmsi,imo:o==null?void 0:o.imo,lat:c.lat,lng:c.lon,sog:c.sog,cog:c.cog,hdg:c.hdg,draught:c.draught,status:l,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:y.unix(),labelCn:u,labelEn:I,method:"trajectory",vendor:"myVessel",utc:y.utc().format()},N=Math.floor(y.diff(b,"minute",!0)/(n||1));N!==h&&(h=N,s.push(F))}),s}}class Mt extends G{constructor(t){super();z(this,"token");this.token=t}async realTimePosition(t,a={}){const i="https://api.hifleet.com/position/position/get/token",o={searchParams:{mmsi:t,usertoken:this.token}},n=await R.post(i,o).json();g==null||g.info("[%s] fetch realtime position from: %s - %j",a.requestId,i,o);const s=n==null?void 0:n.list;if(!s)return g==null||g.warn("[%s] fetch realtime position failed: %j",a.requestId,i,n),n;for(const b in s)!isNaN(s[b])&&Number(s[b])!==1/0&&(s[b]=Number(s[b]));s.status=s.sp>3?0:1;const d=s.status,{labelCn:r,labelEn:m}=this.parseStatus(d),M=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,vesselType:s.type,dwt:s.dwt,build:s.buildyear,flag:s.fn,positionTime:M.unix(),utc:M.utc().format(),status:d,labelCn:r,labelEn:m,method:"position",vendor:"hifleet"}}async search(t,a={}){let i="https://www.hifleet.com/hifleetapi/searchVesselOL.do";const o={searchParams:{keyword:t},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}};let n=await R.post(i,o).json();g==null||g.info("[%s] fetch vessel props from: %s - %j",a.requestId,i,o),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 R.post(i,o).json(),g==null||g.info("[%s] search vessel dead weight from: %s - %j",a.requestId,i,o),n instanceof Array&&(n=n[0]),n&&(s.deadweight=Number(n.dwt)),s}async suggest(t,a={}){const i="https://www.hifleet.com/hifleetapi/getShipSuggest.do",o={searchParams:{q:t},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}},n=await R.post(i,o).json();g==null||g.info("[%s] suggest vessel props from: %s - %j",a.requestId,i,o);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,a,i,o,n=!0,s={}){var c,y,l;const d=await this.realTimePosition(t,s);let r=p(a);const m=p(i),M=p();if(n){let u=m.diff(r,"d",!0);u<0?r=m.clone().subtract(40,"d"):u<30?r.subtract(10,"d"):u<60?r.subtract(5,"d"):r=m.clone().subtract(80,"d"),u=M.diff(m,"d",!0),m.add(u>10?240:u*24,"h")}const h={searchParams:{endtime:m.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}},b="https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token",f=await R.get(b,h).json();g==null||g.info("[%s] fetch trajectory from: %s - %j",s.requestId,b,h);let C;f&&(C=((y=(c=f.ships)==null?void 0:c.offors)==null?void 0:y.ship)||[],C.length||g==null||g.warn("[%s] fetch trajectory failed: %j",s.requestId,f));const k=[];let w=-1;const v=p(`${(l=C==null?void 0:C[0])==null?void 0:l.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");for(const u of C){for(const L in u)!isNaN(u[L])&&Number(u[L])!==1/0&&(u[L]=Number(u[L]));const I=p(`${u.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");u.status=u.sp>4?0:1;const{labelEn:F,labelCn:N}=this.parseStatus(u.status),P={mmsi:u.m,name:u.n,imo:d==null?void 0:d.imo,lat:u.la,lng:u.lo,draught:u.draught,sog:u.sp,cog:u.co,hdg:u.hdg,positionTime:I.unix(),utc:I.utc().format(),status:u.status,labelCn:N,labelEn:F,method:"trajectory",vendor:"hifleet"},E=Math.floor(I.diff(v,"minute",!0)/(o||1));E!==w&&(w=E,k.push(P))}return k}}class pt extends G{constructor(t){super();z(this,"token");this.token=t}async realTimePosition(t,a={}){const i={searchParams:{id:t,k:this.token,enc:1}},o="https://api.shipxy.com/apicall/GetSingleShip",n=await R.get(o,i).json();if(g==null||g.info("[%s] fetch realtime position from: %s - %j",a.requestId,o,i),(n==null?void 0:n.status)!==0)return n;const s=n.data[0];for(const h in s)!isNaN(s[h])&&Number(s[h])!==1/0&&(s[h]=Number(s[h]));const{labelCn:d,labelEn:r}=await this.parseStatus(s.navistat),m=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:m.utc().format(),status:s.navistat,labelEn:r,labelCn:d,method:"position",vendor:"shipxy"}}async trajectory(t,a,i,o,n=!0,s={}){var v;const d=await this.realTimePosition(t,s),r=p(a),m=p(i),M="https://api.shipxy.com/apicall/GetShipTrack",h={searchParams:{id:t,k:this.token,enc:1,cut:0,btm:r.unix(),etm:m.unix()}},b=await R.get(M,h).json();if(g==null||g.info("[%s] fetch trajectory from: %s - %j",s.requestId,M,h),(b==null?void 0:b.status)!==0)return b;const f=b==null?void 0:b.points,C=[],k=p.unix((v=f[0])==null?void 0:v.utc);let w=-1;for(const c of f){const y=p.unix(c.utc),l={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:y.unix(),utc:y.utc().format(),method:"trajectory",vendor:"shipxy"},u=Math.floor(y.diff(k,"minute",!0)/(o||1));u!==w&&(w=u,C.push(l))}return C}}class bt extends G{constructor(t){super();z(this,"token");this.token=t}async getShipId(t,a={}){const i={headers:{appKey:this.token},json:{mmsiList:t}},o="https://api3.myships.com/sp/ships/getShipIdByMMSI",n=await R.post(o,i).json();return g==null||g.info("[%s] fetch ship id from: %s - %j",a.requestId,o,i),n.code!=="0"?n:n.data[0].shipId}async getShipInfo(t,a={}){const i={headers:{appKey:this.token},json:{shipId:t}},o="https://api3.myships.com/sp/ships/aissta",n=await R.post(o,i).json();if(g==null||g.info("[%s] fetch ship info from: %s - %j",a.requestId,o,i),n.code!=="0")return n;const s=n.data;let d=s.imo;return t==="407170"&&(d="9198379",g==null||g.warn("[%s] ship(%s) imo error: %s, should be %s",a.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,a={}){const i=await this.getShipId(t,a),o=await this.getShipInfo(i,a),n={headers:{appKey:this.token},json:{shipId:i}},s="https://api3.myships.com/sp/ships/position/latest",d=await R.post(s,n).json();g==null||g.info("[%s] fetch realtime position from: %s - %j",a.requestId,s,n);const r=d.data[0];for(const f in r)!isNaN(r[f])&&Number(r[f])!==1/0&&(r[f]=Number(r[f]));const{labelCn:m,labelEn:M}=await this.parseStatus(r.aisNavStatus),h=p.unix(r.posTime);return{...o,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:h.utc().format(),status:r.aisNavStatus,labelEn:M,labelCn:m,method:"position",vendor:"myship"}}async trajectory(t,a,i,o,n=!0,s={}){const d=p(a),r=p(i),m=await this.getShipId(t),M=await this.getShipInfo(m),h=[];for(;r.diff(d,"day",!0)>30;)await this.trajectoryIn30Day(m,d.unix(),d.add(30,"day").unix(),M,t,o,h);return await this.trajectoryIn30Day(m,d.unix(),r.unix(),M,t,o,h),h}async trajectoryIn30Day(t,a,i,o,n,s,d,r={}){var k;const m={headers:{appKey:this.token},json:{shipId:t,startTime:a,endTime:i}},M="https://api3.myships.com/sp/ships/position/history",h=await R.post(M,m).json();if(g==null||g.info("[%s] fetch trajectory from: %s - %j",r.requestId,M,m),h.code!=="0")return g==null||g.warn("[%s] invoke myship trajectory failed: %j",r.requestId,h),h;const b=h.data;for(const w in b)!isNaN(b[w])&&Number(b[w])!==1/0&&(b[w]=Number(b[w]));const f=p.unix((k=b[0])==null?void 0:k.posTime);let C=-1;for(const w of b){const v=p.unix(w.posTime),c={imo:o==null?void 0:o.imo,mmsi:n,lat:Math.round(w.lat/1e4/60*1e5)/1e5,lng:Math.round(w.lon/1e4/60*1e5)/1e5,sog:Math.round(w.sog/10*100)/100,cog:Math.round(w.cog/10*100)/100,hdg:Math.round(w.heading*100)/100,rot:Math.round(w.rot*100)/100,positionTime:v.unix(),utc:v.utc().format(),method:"trajectory",vendor:"myship"},y=Math.floor(v.diff(f,"minute",!0)/(s||1));y!==C&&(C=y,d.push(c))}return d}}let _;try{_=K.getLogger("vessel")}catch{}finally{}var dt=(D=>(D.NOTICE="NOTICE",D.WARN="WARN",D.HEAVY="HEAVY",D.SEVERE="SEVERE",D.ERROR="ERROR",D.FATAL="FATAL",D))(dt||{});class ut{parsePrinciple(e,t={}){var s,d,r;_==null||_.info("[%s] parse rule: %s",t.requestId,e);const a=new RegExp("(?<=\\[)(.+)(?=])","g"),i=e.match(a)?(s=e.match(a))==null?void 0:s[0]:void 0,o=i==null?void 0:i.split(";");if(!o)return;const n={};for(let m=0;m<(o==null?void 0:o.length);m++){const M=(r=(d=o[m].match(a))==null?void 0:d[0])==null?void 0:r.split("],");if(m===0&&!M)n.scope=o[0];else if(M)for(let h=0,b=M.length;h<b;h++){const f=this.parseRule(M[h]);f&&(n[f.level]?f.key?n[f.level][f==null?void 0:f.key]=f:n[f.level]=f:f.key?n[f.level]={[f==null?void 0:f.key]:f}:n[f.level]=f)}}return n}parseRule(e,t={}){var n;_==null||_.debug("[%s] parse rule: %s",t.requestId,e),e=e.startsWith("[")?e:`[${e}`,e=e.endsWith("]")?e:`${e}]`;const a=new RegExp("(?<=\\[)(.+?)(?=])","g"),i=(n=e==null?void 0:e.match(a))==null?void 0:n[0],o=i==null?void 0:i.split(",");if(o)return{operator:o[0],number:Number.isNaN(Number(o[1]))?o[1]:Number(o[1]),level:o[2],time:Number(o[3]),key:o[4]}}checkWeather(e,t,a={}){var f,C,k,w,v,c,y,l,u,I,F,N,P,E,L;let i=0,o=0,n=0,s=0;const d=Math.round(((C=(f=t==null?void 0:t.SEVERE)==null?void 0:f.sigWave)==null?void 0:C.number)*1.6*100)/100,r=(w=(k=t==null?void 0:t.SEVERE)==null?void 0:k.sigWave)==null?void 0:w.number,m=(c=(v=t==null?void 0:t.HEAVY)==null?void 0:v.sigWave)==null?void 0:c.number,M=Math.round((((l=(y=t==null?void 0:t.SEVERE)==null?void 0:y.wind)==null?void 0:l.number)+2)*100)/100,h=(I=(u=t==null?void 0:t.SEVERE)==null?void 0:u.wind)==null?void 0:I.number,b=(N=(F=t==null?void 0:t.HEAVY)==null?void 0:F.wind)==null?void 0:N.number;for(let x=0;x<(e==null?void 0:e.length);x++){const H=e[x],q=(E=(P=H==null?void 0:H.meteo)==null?void 0:P.wave)==null?void 0:E.sig,V=(L=H==null?void 0:H.meteo)==null?void 0:L.wind,U=x?p(H.eta).diff(p(e[x-1].eta),"hour",!0):0;s=U>s?U:s,_==null||_.debug("[%s] check sig.wave: %j",a.requestId,{...q,dgThd4Wv:d,svThd4Wv:r,hvThd4Wv:m}),(q==null?void 0:q.height)>=d?H.isDangerous=!0:(q==null?void 0:q.height)>=r?H.isSevere=!0:(q==null?void 0:q.height)>=m&&(H.isHeavy=!0),_==null||_.debug("[%s] check wind: %j",a.requestId,{...V,dgThd4Wd:M,svThd4Wd:h,hvThd4Wd:b}),(V==null?void 0:V.scale)>=M?(H.isDangerous=!0,delete H.isSevere,delete H.isHeavy):(V==null?void 0:V.scale)>h?(H.isDangerous||(H.isSevere=!0),delete H.isHeavy):(V==null?void 0:V.scale)===b&&!H.isDangerous&&!H.isSevere&&(H.isHeavy=!0),i+=H.isDangerous?U:0,o+=H.isSevere?U:0,n+=H.isHeavy?U:0}return i=Math.round(i*100)/100,o=Math.round(o*100)/100,n=Math.round(n*100)/100,s=Math.round(s),{sample:e,dangerous:i,severe:o,heavy:n,step:s<3?3:s,wind:{dgThd4Wd:M,svThd4Wd:h,hvThd4Wd:b},sig:{dgThd4Wv:d,svThd4Wv:r,hvThd4Wv:m}}}}const gt=new ut;let S;try{S=K.getLogger("vessel")}catch{}finally{}const vt=new mt.MeteoHelper2("",!0);var lt=(D=>(D.common="common",D.container="container",D.tugs="tugs",D))(lt||{}),ht=(D=>(D.Ballast="Ballast",D.Laden="Laden",D))(ht||{}),ft=(D=>(D.Cp="CP",D.Perf="Basis",D.Instruct="Other",D))(ft||{});class W{static blockCoefficient(e,t,a,i){let o=Math.round(e/(t*a*i)*100)/100;o=o<.55?.55:o>.85?.85:o;const n=[.55,.6,.65,.7,.75,.8,.85],s=n.map(d=>Math.abs(d-o));return n[s.indexOf(Math.min(...s))]}static froudeNumber(e,t,a=9.8){let i=Math.round(Math.sqrt(e*e/(a*t))*100)/100;return i=i<.05?.05:i>.3?.3:i,i}static amendFactor(e,t,a){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 a==="Laden"&&(n=i[e]),n[0]+n[1]*t+n[2]*Math.pow(t,2)}static directionFactor(e,t=0){let a;return e>30&&e<=60?a=(1.7-.03*Math.pow(t-4,2))/2:e>60&&e<=150?a=(.9-.06*Math.pow(t-6,2))/2:e>150&&e<=180?a=(.4-.03*Math.pow(t-8,2))/2:a=1,Math.round(a*1e5)/1e5}static vesselTagFactor(e,t,a,i=0){i=i>6?i-.9*(i-6):i;let o;return a==="container"?o=.7*i+Math.pow(i,6.5)/(22*Math.pow(e,2/3)):t==="Ballast"?o=.7*i+Math.pow(i,6.5)/(2.7*Math.pow(e,2/3)):o=.5*i+Math.pow(i,6.5)/(2.7*Math.pow(e,2/3)),o}static waveHeightFactor(e,t){e=e<0?.2:e,e=e>6?e-.9*(e-6):e,e=e>9?9:e;let a;return t>30&&t<=60?a=-.6:t>60&&t<=90?a=-.4:t>90&&t<=120?a=e<3?.4:-.3:t>120&&t<=150?a=e<3?.6:-.5:t>150&&t<=180?a=e<3?.7:-.6:a=-.7,Math.round(a*(.144*Math.pow(e,2)+.178*e)*1e4)/1e4}static assembleProperties(e,t,a,i){var h;const o=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=((h=e==null?void 0:e.type)==null?void 0:h.toLowerCase())||"common";return{tag:r.indexOf("container")>-1?"container":r.indexOf("tugs")>-1?"tugs":"common",lbp:o,loadCondition:t,draught:n,breadthMoulded:s,displacement:Math.round((d/1.025+n*s*o*.7)*1e4)/1e4,speed:Math.round((a??14.1382)*1852/3600*1e4)/1e4,bearing:i||90}}static async speedLoseAt(e,t,a,i="",o=2,n=!0,s=!1,d={}){let r;if(t.velocity&&s&&(e.speed=T.LngLatHelper.roundPrecision(t.velocity*1852/3600,6)),n){let m;try{i=(i==null?void 0:i.toUpperCase())==="CMEMS"?"ECMWF":i,i=(i==null?void 0:i.toUpperCase())==="METEO2"?"best_match":i;const{weatherModels:f,marineModels:C}=await nt.Meteo2Assist.autoPickMeteoModel(i),k=await vt.spotForecast(t.lat,t.lng,a.utc().format(),!1,!1,!0,{...d,pastDays:1,forecastDays:1,weatherModels:f,marineModels:C}),[w]=nt.Meteo2Assist.pickHourly(k,a);m=nt.Meteo2Assist.toLegacy(w)}catch(f){S.warn("[%s] meteo2 spot(%j) forecast failed: %s",d.requestId,{...t,eta:a.utc().format(),source:i},f)}const M=W.weatherFactor(e,m),h=W.currentFactor(e.bearing,m==null?void 0:m.current,o),b=Math.round((e.speed*1.943844+M+h)*100)/100;r={meteo:{...m},wxFactor:M,cFactor:h,speed:t.velocity&&s?t.velocity:b<0?1:b,eta:a.utc().format(),etd:a.utc().format()}}else r={wxFactor:0,cFactor:0,speed:t.velocity&&s?t.velocity:Math.round((e.speed*1.943844+0+0)*100)/100,eta:a.utc().format(),etd:a.utc().format()};return delete t.meteo,delete t.wxFactor,delete t.cFactor,delete t.speed,delete t.etd,{...r,...t}}static async speedLoseInHoursStep(e,t,a,i,o,n,s="",d=!0,r=!1,m={}){t.utc();const M=t.clone().add(14,"days"),h=[],b=[];let f=0,C=0,k,w;for(let v=0;v<n.length-1;v++){let c=n[v];c.distanceFromStart=Math.round((o+C)*1e3)/1e3;const y=n[v+1];if(e.bearing=T.LaneHelper.calculateBearing(c,y,!y.gcToPrevious),c.bearing=e.bearing,c.suspend&&r){c.eta=c.eta||t.utc().format(),c.elapsed=c.elapsed??0;const I=c.suspend-c.elapsed;if(i-f>I)i=i-f-I,t.add(I,"hour"),c.elapsed=c.suspend;else{const F=i-f;c.elapsed+=F,t.add(F,"hour"),i=0}if(S==null||S.info(`[%s] suspend ${c.elapsed} hours at %j, and remain ${i} hours need to go...`,m.requestId,c),i===0)return c.distanceFromPrevious=C,{etd:t,from:w||c,to:c,next:n.filter(F=>F),wps:h,days:b}}else c.suspend=0;d=t.isAfter(M)?!1:d,c=await W.speedLoseAt(e,c,t,s,0,d,r,m),w=w||c,c.important&&h.push(c),t.isSameOrAfter(a)&&(b.push(c),a.add(24,"hour"));const l=T.LaneHelper.calculateDistance(c,y,!y.gcToPrevious);let u=Math.round(l/w.speed*1e5)/1e5;if(f+u<i){if(f+=u,t.add(u,"hour"),delete n[v],S==null||S.debug(`[%s] go to %j from %j with ${l}nm, and cost ${u} hours`,m.requestId,{lat:y.lat,lng:y.lng},{lat:w.lat,lng:w.lng,etd:w.etd}),C+=l,n.filter(I=>I).length<=1){k=y,k.eta=t.utc().format(),k.distanceFromPrevious=l,k.distanceFromStart=Math.round((o+C)*1e4)/1e4,h.push(k),delete n[v+1];break}}else{u=i-f,t.add(u,"hour");const I=T.LngLatHelper.roundPrecision(w.speed*u,5);k=T.LaneHelper.calculateCoordinate(c,e.bearing,I,"nauticalmiles",!y.gcToPrevious),k.eta=t.utc().format(),n[v]=k,S==null||S.debug(`[%s] go to %j from %j with ${I}nm, and cost ${u} hours`,m.requestId,{lat:k.lat,lng:k.lng},{lat:c.lat,lng:c.lng,etd:c.etd}),C+=I,k.distanceFromPrevious=Math.round(C*1e4)/1e4,k.distanceFromStart=Math.round((o+C)*1e4)/1e4;break}}return{etd:t,from:w,to:k,next:n.filter(v=>v),wps:h,days:b}}static currentFactor(e,t,a=0){const i=(e-(t==null?void 0:t.degree)||0)/180*Math.PI;if(Math.abs(i)===Math.PI/2)return 0;let o=((t==null?void 0:t.kts)||0)*Math.cos(i);return a&2?o=Math.ceil(o*100)/100:a&1?o=Math.floor(o*100)/100:o=Math.round(o*100)/100,Math.abs(o)>5?0:o}static weatherFactor(e,t){var M,h,b,f,C,k,w;S==null||S.debug("calculate weather factor via: %j",{...e,...t});const a=W.blockCoefficient(e.displacement,e.lbp,e.breadthMoulded,e.draught),i=W.froudeNumber(e.speed,e.lbp),o=W.amendFactor(a,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=W.directionFactor(n,(h=t==null?void 0:t.wind)==null?void 0:h.scale),d=W.vesselTagFactor(e.displacement,e.loadCondition,e.tag,(b=t==null?void 0:t.wind)==null?void 0:b.scale);let r=s*o*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)),S==null||S.debug("wind wx factor = %d",r),n=Math.abs(e.bearing%360-(((C=(f=t==null?void 0:t.wave)==null?void 0:f.sig)==null?void 0:C.degree)%360||0));const m=W.waveHeightFactor(((w=(k=t==null?void 0:t.wave)==null?void 0:k.sig)==null?void 0:w.height)??1,n);return S==null||S.debug("wave wx factor = %d",m),r=r*.4+m,S==null||S.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,a,i,o,n="",s=0,d=!0,r=!1,m={}){var Q,X,Z,$,tt,et;const M=p().valueOf();e.lng=T.LngLatHelper.convertToStdLng(e.lng);const{route:h,waypoints:b}=o.points,f=T.LaneHelper.calculateSubRoute(e,h);if(((Q=f[0])==null?void 0:Q.length)<=1)return;const{v0:C,label:k}=e.sog?{v0:e.sog,label:e.label||"Other"}:{v0:i.speed,label:"CP"},w=W.assembleProperties(a,i.loadCondition,C,0),v=b.length?T.LaneHelper.calculateSubWaypoints(e,b):[];v.forEach(Y=>Y.important=!0);const c={from:{...e},route:f,waypoints:v,v0:C,label:k},y={hours:[],days:[],wps:[]};s||(T.LaneHelper.calculateRouteDistance(f)/i.speed<=72?s=3:s=6);let l=T.LaneHelper.simplifyRouteToCoordinates(f,v,0),u=0,I=0,F=0,N=0;t=p(t).utc();const P=t.clone();for(;l.length>0;){const Y=s-t.hour()%s,J=Math.ceil(t.clone().add(Y,"h").set({minute:0,second:0,millisecond:0}).diff(t,"h",!0)*1e4)/1e4,j=await W.speedLoseInHoursStep(w,t,P,J,u,l,n,d,r,m);(X=j.from)!=null&&X.speed&&(y.hours.push(j.from),y.wps.push(...j.wps),y.days.push(...j.days)),l=j==null?void 0:j.next,l.length||y.hours.push(j==null?void 0:j.to),u+=Math.round((((Z=j==null?void 0:j.to)==null?void 0:Z.distanceFromPrevious)??0)*1e4)/1e4}const E=y.hours;for(let Y=0;Y<E.length-1;Y++){const J=p(E[Y+1].eta).diff(E[Y].etd,"hour",!0)||1;I+=(E[Y].wxFactor||0)*J,F+=(E[Y].cFactor||0)*J,N+=J}($=y.wps)==null||$.forEach((Y,J)=>{Y.positionTime=p.utc(Y.etd||Y.eta).unix();const j=y.wps[J-1];if(j){const B=Y.distanceFromStart-j.distanceFromStart,O=p(Y.eta||Y.etd).diff(p(j.etd||j.eta),"h",!0);Y.avgSpd=Math.round(B/O*100)/100;const ct=T.LaneHelper.calculateBearing(j,Y);j.bearing=ct}}),y.wps=(tt=y.wps)==null?void 0:tt.reduce((Y,J)=>(Y.some(j=>Math.round(j.positionTime/60)===Math.round(J.positionTime/60))||Y.push(J),Y),[]),c.sample=y;const L=y.hours.at(0),x=y.hours.at(-1);c.distance=Math.round(x.distanceFromStart*1e3)/1e3,c.etd=p(L.eta).utc().format(),c.eta=p(x.eta).utc().format(),c.wxFactor=Math.round(I/N*1e3)/1e3,c.cFactor=Math.round(F/N*1e3)/1e3,c.avgSpeed=Math.round(x.distanceFromStart/N*1e3)/1e3,c.totalHrs=Math.round(N*1e3)/1e3;const{distanceInECA:H,hoursInECA:q,totalDgoConsInECA:V,eca:U}=await this.calculateECA(c,i,m),st=T.LngLatHelper.roundPrecision(i.fo/24*(N-q),3),ot=T.LngLatHelper.roundPrecision(i.dgo/24*N,3);c.extend={eca:U,distanceInECA:H,hoursInECA:q,totalDgoConsInECA:V},c.totalFoCons=st<0?0:st,c.totalDgoCons=ot;const at=p().valueOf()-M,rt=((et=y==null?void 0:y.hours)==null?void 0:et.length)||1;return S==null||S.info("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms",m==null?void 0:m.requestId,at,rt,Math.round(at/rt*1e3)/1e3),c}static async analyseInstantWithThreshed(e,t,a,i,o,n,s,d="",r=3,m=!0,M=!1,h={}){var Z,$,tt,et,Y,J;const b=p().valueOf();e.lng=T.LngLatHelper.convertToStdLng(e.lng);const{v0:f,label:C}=e.sog?{v0:e.sog,label:e.label||"Other"}:{v0:o.speed,label:"CP"},k=W.assembleProperties(i,o.loadCondition,f,0),w=T.LaneHelper.calculateSubRoute(e,n);if(((Z=w[0])==null?void 0:Z.length)<=1)return;const v=s.length?T.LaneHelper.calculateSubWaypoints(e,s):[];v.forEach(j=>j.important=!0);let c=T.LaneHelper.simplifyRouteToCoordinates(w,v,0),y=0,l=0,u=0,I=0;const F={hours:[],wps:[],days:[]};t=p(t).utc();const N=t.clone();for(;c.length>0;){const j=r-t.hour()%r;let B=Math.ceil(t.clone().add(j,"h").set({minute:0,second:0,millisecond:0}).diff(t,"h",!0)*1e4)/1e4;B=t.clone().add(B,"h").isSameOrAfter(a)?a.diff(t,"h",!0)*1e4/1e4:B;const O=await W.speedLoseInHoursStep(k,t,N,B,y,c,d,m,M,h);if(($=O.from)!=null&&$.speed&&(F.hours.push(O.from),O!=null&&O.wps&&F.wps.push(...O.wps),F.days.push(...O.days)),c=O==null?void 0:O.next,c.length||F.hours.push(O==null?void 0:O.to),y+=Math.round((((tt=O==null?void 0:O.to)==null?void 0:tt.distanceFromPrevious)??0)*1e4)/1e4,!B)break}F.wps=(et=F.wps)==null?void 0:et.reduce((j,B)=>(j.some(O=>Math.round(p(O.etd).unix()/60)===Math.round(p(B.etd).unix()/60))||j.push(B),j),[]),(Y=F.wps)==null||Y.forEach((j,B)=>{const O=F.wps[B-1];if(O){const ct=j.distanceFromStart-O.distanceFromStart,wt=p(j.eta||j.etd).diff(p(O.etd||O.eta),"h",!0);j.avgSpd=Math.round(ct/wt*100)/100;const It=T.LaneHelper.calculateBearing(O,j);O.bearing=It}});const P=F.hours;for(let j=0;j<P.length-1;j++){const B=p(P[j+1].eta).diff(P[j].etd,"hour",!0);l+=P[j].wxFactor*B,u+=P[j].cFactor*B,I+=B}const E=F.hours.at(0),L=F.hours.at(-1),x=await T.LaneHelper.calculateRangeRoute(E,L,w),H=await T.LaneHelper.calculateRangeWaypoints(E,L,w,v),q={sample:F,distance:Math.round(((L==null?void 0:L.distanceFromStart)||0)*1e4)/1e4,etd:p(E.eta).utc().format(),eta:p(L==null?void 0:L.eta).utc().format(),wxFactor:Math.round(l/I*1e3)/1e3,cFactor:Math.round(u/I*1e3)/1e3,avgSpeed:Math.round(((L==null?void 0:L.distanceFromStart)||0)/I*1e3)/1e3,totalHrs:Math.round(I*1e3)/1e3,from:E,to:L,route:x,waypoints:H,v0:f,label:C},{distanceInECA:V,hoursInECA:U,totalDgoConsInECA:st,eca:ot}=await this.calculateECA(q,o,h),it=T.LngLatHelper.roundPrecision(o.fo/24*(I-U),3),at=T.LngLatHelper.roundPrecision(o.dgo/24*I,3);q.extend={eca:ot,distanceInECA:V,hoursInECA:U,totalDgoConsInECA:st},q.totalDgoCons=at,q.totalFoCons=it<0?0:it;const Q=p().valueOf()-b,X=((J=F==null?void 0:F.hours)==null?void 0:J.length)||1;return S==null||S.debug("[%s] each hour-sample speed analyse cost: (%d / %d = %d) ms",h==null?void 0:h.requestId,Q,X,Math.round(Q/X*1e3)/1e3),q}static async analyseCost(e,t,a,i,o={}){var w,v;const n=p().valueOf(),s=[];e.speedStep=e.speedStep||3,e.alterStep=e.alterStep??1;const d=T.LaneHelper.calculateRouteDistance(i.route);let r=0;a.forEach(c=>{const y=Math.ceil(d/c.speed/24);r=r<y?y:r}),r=r*1.3;const m=p.utc(e.etd).add(r??14,"day");let M=1;for(const c of a){const y=JSON.parse(JSON.stringify(i.route)),l=JSON.parse(JSON.stringify(i.waypoints)),u=await W.analyseInstantWithThreshed({lat:e.lat,lng:e.lng},e.etd,m,t,c,y,l,e.meteoVendor,e.speedStep,e.useMeteo,e.useRouteParam,o);u&&(await W.calculateCost(u,c,e,o),s.push(u),S==null||S.info("[%s][L%d-%d] analyse from %s to %s cost: %j",o.requestId,1,M,e.etd,m.format(),{cost:u.cost.total,hire:u.cost.hire,bunker:u.cost.bunker,distance:u.distance,hours:u.totalHrs,cp:`${c.speed}/${c.fo}/${c.dgo}`})),M++}s.sort((c,y)=>c.cost.total-y.cost.total);const h=s.at(0),b=s.at(1),f=[];if(f.push({combined:!1,speeds:[h],cost:(w=h.cost)==null?void 0:w.total}),b){const c=h.cost.cp,y=b.cost.cp,l=p(h.eta),u=p(h.etd),I=l.diff(u,"days",!0);let F=Math.ceil(I/2);F=F>7?7:F<e.alterStep?e.alterStep:F;let N=2,P={combined:!1,speeds:[b],cost:(v=b.cost)==null?void 0:v.total},E;for(;F>=e.alterStep;){const L=await W.combinedAnalyse(e,t,m,[c,y],i,F,{...o,level:N});if(P.cost>L.cost?E?(E==null?void 0:E.cost)>L.cost&&(E=L):(E=P,P=L):(!E||(E==null?void 0:E.cost)>L.cost)&&(E=L),F<=e.alterStep)break;F=Math.ceil(F/2),N+=1}f.push(P),E&&f.push(E)}const k=p().valueOf()-n;return S==null||S.info("[%s] analyse elapsed: %d ms",o==null?void 0:o.requestId,k),f.sort((c,y)=>c.cost-y.cost)}static async combinedAnalyse(e,t,a,i,o,n,s={}){s.counter=1,S==null||S.info("[%s][L%d] analyse with alternate cp in every %d days",s.requestId,s.level,n);const d=await W.alternateAnalyse(e,t,a,i,0,o,n,s),r=d.reduce((y,l)=>y+l.cost.total,0),m=d.reduce((y,l)=>y+l.cost.hire,0),M=d.reduce((y,l)=>y+l.cost.bunker,0),h=d.reduce((y,l)=>y+l.distance,0),b=d.reduce((y,l)=>y+l.totalHrs,0);S==null||S.info("[%s][L%d] cost with cpa/cpb turn: %j",s.requestId,s.level,{cost:r,hire:m,bunker:M,distance:h,hours:b});const f=await W.alternateAnalyse(e,t,a,i,1,o,n,s),C=f.reduce((y,l)=>y+l.cost.total,0),k=f.reduce((y,l)=>y+l.cost.hire,0),w=f.reduce((y,l)=>y+l.cost.bunker,0),v=f.reduce((y,l)=>y+l.distance,0),c=f.reduce((y,l)=>y+l.totalHrs,0);return S==null||S.info("[%s][L%d] cost with cpb/cpa turn: %j",s.requestId,s.level,{cost:C,hire:k,bunker:w,distance:v,hours:c}),r<C?{combined:!0,cost:Math.round(r*1e3)/1e3,speeds:d,step:n}:{combined:!0,cost:Math.round(C*1e3)/1e3,speeds:f,step:n}}static async alternateAnalyse(e,t,a,i,o,n,s,d={}){var h,b;let r=p.utc(e.etd);const m={lat:e.lat,lng:e.lng},M=[];for(;r.isBefore(a);){const f=r.clone().utc().add(s,"day"),C=JSON.parse(JSON.stringify(n.route)),k=JSON.parse(JSON.stringify(n.waypoints)),w=i[o],v=await W.analyseInstantWithThreshed(m,r.utc().format(),f,t,w,C,k,e.meteoVendor,e.speedStep,e.useMeteo,e.useRouteParam,d);v&&(await W.calculateCost(v,w,e,d),S==null||S.info("[%s][L%d-%d] analyse from %s to %s cost: %j",d.requestId,d.level,d.counter,r.utc().format(),f.utc().format(),{cost:v.cost.total,hire:v.cost.hire,bunker:v.cost.bunker,distance:v.distance,hours:v.totalHrs,cp:`${w.speed}/${w.fo}/${w.dgo}`})),d.counter=d.counter+1;const c=(b=(h=v==null?void 0:v.sample)==null?void 0:h.hours)==null?void 0:b.at(-1);if(c)m.lat=c.lat,m.lng=c.lng,r=p(c.eta),M.push(v),o=o?0:1;else break}return M}static async calculateCost(e,t,a,i={}){var o;if(e){const n=(a.addComm||0)>=1?(a.addComm||0)/100:a.addComm||0,s=Math.round(e.totalHrs/24*(a.dailyHire||0)*(1-n)*1e3)/1e3,d=Math.round(e.totalFoCons*(a.priceFO||0)*1e3)/1e3,r=Math.round((e.totalDgoCons+(((o=e.extend)==null?void 0:o.totalDgoConsInECA)||0))*(a.priceDGO||0)*1e3)/1e3;e.cost={total:Math.round((s+d+r)*1e3)/1e3,hire:s,bunker:Math.round((d+r)*1e3)/1e3,cp:t}}return e}static async calculateECA(e,t,a={}){var d,r,m,M;const i=await T.LaneHelper.intersectInECA((e==null?void 0:e.route)||[]);let o=0,n=0,s=0;(r=(d=e==null?void 0:e.sample)==null?void 0:d.wps)==null||r.forEach(h=>{h.positionTime=p.utc(h.etd||h.eta).unix()});for(const h of i){o+=h.distance;const b=await T.LaneHelper.deadReckoningTime((m=h.waypoints)==null?void 0:m.at(0),e.sample.wps),f=await T.LaneHelper.deadReckoningTime((M=h.waypoints)==null?void 0:M.at(-1),e.sample.wps);h.in=b,h.out=f,h.totalHrs=T.LngLatHelper.roundPrecision((f.positionTime-b.positionTime)/3600,3),h.totalDgoCons=T.LngLatHelper.roundPrecision(t.fo/24*h.totalHrs,3),n+=h.totalHrs,s+=h.totalDgoCons}return o=T.LngLatHelper.roundPrecision(o,3),n=T.LngLatHelper.roundPrecision(n,3),s=T.LngLatHelper.roundPrecision(s,3),{distanceInECA:o,hoursInECA:n,totalDgoConsInECA:s,eca:i}}static async mergeSpeeds(e,t={}){var c,y;const a={hours:[],wps:[],days:[]},i=e.reduce((l,u)=>l+u.distance,0),o=e.reduce((l,u)=>{var I;return l+(((I=u.extend)==null?void 0:I.distanceInECA)||0)},0),n=e.reduce((l,u)=>l+u.totalHrs,0),s=e.reduce((l,u)=>{var I;return l+(((I=u.extend)==null?void 0:I.hoursInECA)||0)},0),d=e.reduce((l,u)=>{var I;return l+(((I=u.extend)==null?void 0:I.totalDgoConsInECA)||0)},0),r=e.reduce((l,u)=>l+u.wxFactor*u.totalHrs/n,0),m=e.reduce((l,u)=>l+u.cFactor*u.totalHrs/n,0),M=e.reduce((l,u)=>l+u.totalFoCons,0),h=e.reduce((l,u)=>l+u.totalDgoCons,0),b=e.reduce((l,u)=>l+u.cost.total,0),f=e.reduce((l,u)=>l+u.cost.hire,0),C=e.reduce((l,u)=>l+u.cost.bunker,0),k=[],w=[];let v;for(const l of e){w.push(...((c=l.extend)==null?void 0:c.eca)||[]);const u=l.sample.hours,I=l.sample.wps,F=l.sample.days,N=u.at(0);v&&(N.distanceFromPrevious=v.distanceFromPrevious,N.distanceFromStart=v.distanceFromStart,u.forEach((x,H)=>{H&&(x.distanceFromStart=x.distanceFromStart+v.distanceFromStart)}),I.at(0).distanceFromPrevious=v.distanceFromPrevious,I.at(0).distanceFromStart=v.distanceFromStart,I.forEach((x,H)=>{H&&(x.distanceFromStart=x.distanceFromStart+v.distanceFromStart)}),F.at(0).distanceFromPrevious=v.distanceFromPrevious,F.at(0).distanceFromStart=v.distanceFromStart,F.forEach((x,H)=>{H&&(x.distanceFromStart=x.distanceFromStart+v.distanceFromStart)})),N.cp=l.cost.cp;const P=[l.etd,l.eta],E=k.findIndex(x=>x.id===N.cp.id);E===-1?(N.cp.segment=[P],k.push(N.cp)):k[E].segment.push(P),u.forEach(x=>{var q;((q=a.hours)==null?void 0:q.findIndex(V=>V.eta===x.eta))===-1&&a.hours.push(x)}),I.forEach(x=>{var q;((q=a.wps)==null?void 0:q.findIndex(V=>V.eta===x.eta))===-1&&a.wps.push(x)}),F.forEach(x=>{var q;((q=a==null?void 0:a.days)==null?void 0:q.findIndex(V=>V.eta===x.eta))===-1&&a.days.push(x)});const L=(y=a.wps)==null?void 0:y.findIndex(x=>x.eta===N.eta);L===-1?a.wps.push(N):a.wps[L]=N,v=u.at(-1)}return a.wps.sort((l,u)=>{p(l.etd).unix()-p(u.etd).unix()}),a.wps.forEach((l,u)=>{const I=a.wps[u-1];if(I){const F=l.distanceFromStart-(I.distanceFromStart||0),N=p(l.eta||l.etd).diff(p(I.etd||I.eta),"hour",!0),P=Math.round(F/N*100)/100;l.avgSpd=P;const E=T.LaneHelper.calculateBearing(I,l);I.bearing=E}}),{sample:a,etd:e.at(0).etd,eta:e.at(-1).eta,from:e.at(0).from,to:e.at(-1).to,v0:e.at(0).v0,label:"Combined",distance:Math.round(i*1e3)/1e3,totalHrs:Math.round(n*1e3)/1e3,avgSpeed:Math.round(i/n*1e3)/1e3,wxFactor:Math.round(r*1e3)/1e3,cFactor:Math.round(m*1e3)/1e3,totalFoCons:Math.round(M*1e3)/1e3,totalDgoCons:Math.round(h*1e3)/1e3,cost:{total:Math.round(b*1e3)/1e3,hire:Math.round(f*1e3)/1e3,bunker:Math.round(C*1e3)/1e3},extend:{cps:k,eca:w,distanceInECA:Math.round(o*1e3)/1e3,hoursInECA:Math.round(s*1e3)/1e3,totalDgoConsInECA:Math.round(d*1e3)/1e3,speeds:e}}}}A.AISImpl=G,A.AlertHelper=ut,A.AlertLevel=dt,A.HifleetImpl=Mt,A.LoadCondition=ht,A.MyShipImpl=bt,A.MyVesselImpl=yt,A.ShipxyImpl=pt,A.SpeedHelper=W,A.SpeedLabel=ft,A.VesselTag=lt,A.alertHelper=gt,Object.defineProperty(A,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": "2.1.1",
4
+ "version": "2.1.3",
5
5
  "description": "idm plugin for vessel",
6
6
  "type": "module",
7
7
  "keywords": [
@@ -27,7 +27,7 @@
27
27
  "release": "yarn build && yarn publish --access public"
28
28
  },
29
29
  "dependencies": {
30
- "@idm-plugin/geo2": "^1.4.5",
30
+ "@idm-plugin/geo2": "^1.4.7",
31
31
  "@idm-plugin/meteo": "^0.4.3",
32
32
  "@idm-plugin/meteo2": "^0.5.6",
33
33
  "@log4js-node/log4js-api": "^1.0.2",