@idm-plugin/vessel 1.0.1 → 1.0.2

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.
@@ -63,7 +63,8 @@ export declare class MyVesselImpl extends AISImpl {
63
63
  constructor(clientId: string, clientSecret: string);
64
64
  authToken(options?: RequestOptions): Promise<void>;
65
65
  realTimePosition(mmsi: number, options?: RequestOptions): Promise<AISRecord>;
66
- trajectory(mmsi: number, startTime: string, endTime: string, offset: number, scale?: boolean, options?: RequestOptions): Promise<any>;
66
+ trajectory(mmsi: number, startTime: string, endTime: string, offset: number, scale?: boolean, options?: RequestOptions): Promise<AISRecord[]>;
67
+ private trajectoryIn30Day;
67
68
  }
68
69
  export declare class HifleetImpl extends AISImpl {
69
70
  private readonly token;
package/dist/index.js CHANGED
@@ -1,18 +1,18 @@
1
1
  var P = Object.defineProperty;
2
- var $ = (I, M, s) => M in I ? P(I, M, { enumerable: !0, configurable: !0, writable: !0, value: s }) : I[M] = s;
3
- var x = (I, M, s) => ($(I, typeof M != "symbol" ? M + "" : M, s), s);
4
- import b from "got";
2
+ var $ = (w, j, s) => j in w ? P(w, j, { enumerable: !0, configurable: !0, writable: !0, value: s }) : w[j] = s;
3
+ var x = (w, j, s) => ($(w, typeof j != "symbol" ? j + "" : j, s), s);
4
+ import I from "got";
5
5
  import * as E from "log4js";
6
- import l from "moment";
6
+ import f from "moment";
7
7
  const g = E.getLogger();
8
- class v {
8
+ class q {
9
9
  /**
10
10
  * 解析AIS状态码
11
11
  * @param status
12
12
  */
13
- parseStatus(M) {
13
+ parseStatus(j) {
14
14
  let s, o;
15
- switch (M) {
15
+ switch (j) {
16
16
  case 0:
17
17
  s = "在航(主机推动)", o = "The engine is in use";
18
18
  break;
@@ -46,7 +46,7 @@ class v {
46
46
  return { labelCn: s, labelEn: o };
47
47
  }
48
48
  }
49
- class L extends v {
49
+ class L extends q {
50
50
  constructor(s, o) {
51
51
  super();
52
52
  x(this, "clientId");
@@ -61,33 +61,33 @@ class L extends v {
61
61
  client_secret: this.clientSecret,
62
62
  grant_type: "client_credentials"
63
63
  }
64
- }, i = await b.post(o, r).json();
64
+ }, i = await I.post(o, r).json();
65
65
  g.info("[%s] fetch access token from: %s - %j", s.requestId, o, i), i.error || (this.token = {
66
66
  accessToken: i.access_token,
67
67
  tokenType: i.token_type,
68
68
  expiresIn: i.expires_in,
69
69
  scope: i.scope,
70
70
  jti: i.jti,
71
- issuedAt: l().utc().format()
71
+ issuedAt: f().utc().format()
72
72
  });
73
73
  }
74
74
  async realTimePosition(s, o = {}) {
75
- var d, f, m;
76
- (!this.token || l().diff(l(this.token.issuedAt), "seconds") > ((d = this.token) == null ? void 0 : d.expiresIn) - 300) && await this.authToken(o);
75
+ var u, c, m;
76
+ (!this.token || f().diff(f(this.token.issuedAt), "seconds") > ((u = this.token) == null ? void 0 : u.expiresIn) - 300) && await this.authToken(o);
77
77
  const r = "https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit", i = {
78
78
  headers: {
79
- Authorization: `${(f = this.token) == null ? void 0 : f.tokenType} ${(m = this.token) == null ? void 0 : m.accessToken}`
79
+ Authorization: `${(c = this.token) == null ? void 0 : c.tokenType} ${(m = this.token) == null ? void 0 : m.accessToken}`
80
80
  },
81
81
  searchParams: { mmsi: s }
82
82
  };
83
83
  g.info("[%s] fetch realtime position from: %s - %j", o.requestId, r, i);
84
- const e = await b.get(r, i).json();
84
+ const e = await I.get(r, i).json();
85
85
  if (e.code)
86
86
  return g.warn("[%s] fetch realtime position failed: %j", o.requestId, r, { message: e.message, status: e.status, code: e.code }), e;
87
87
  const t = e.data;
88
- for (const c in t)
89
- !isNaN(t[c]) && Number(t[c]) !== 1 / 0 && (t[c] = Number(t[c]));
90
- const a = l(`${t.postime} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
88
+ for (const h in t)
89
+ !isNaN(t[h]) && Number(t[h]) !== 1 / 0 && (t[h] = Number(t[h]));
90
+ const a = f(`${t.postime} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
91
91
  return {
92
92
  mmsi: t.mmsi,
93
93
  name: t.vesselName,
@@ -114,58 +114,57 @@ class L extends v {
114
114
  };
115
115
  }
116
116
  async trajectory(s, o, r, i, e = !0, t = {}) {
117
- var p, w, j;
118
- (!this.token || l().diff(l(this.token.issuedAt), "seconds") > this.token.expiresIn - 300) && await this.authToken(t);
119
- const a = await this.realTimePosition(s, t), n = "https://svc.data.myvessel.cn/sdc/v1/vessels/status/track";
120
- let d = l(o);
121
- const f = l(r);
122
- if (e) {
123
- const u = f.diff(d, "d", !0);
124
- (u < 0 || u > 30) && (d = f.clone().subtract(30, "d"));
125
- }
126
- const m = {
117
+ (!this.token || f().diff(f(this.token.issuedAt), "seconds") > this.token.expiresIn - 300) && await this.authToken(t);
118
+ const a = await this.realTimePosition(s, t), n = f(o), u = f(r), c = [];
119
+ for (; u.diff(n, "day", !0) > 30; )
120
+ await this.trajectoryIn30Day(s, n, n.clone().add(30, "day"), a, i, c, t), n.add(30, "day");
121
+ return await this.trajectoryIn30Day(s, n, u, a, i, c, t), c;
122
+ }
123
+ async trajectoryIn30Day(s, o, r, i, e, t, a = {}) {
124
+ var y, k, Y, p, b;
125
+ const n = "https://svc.data.myvessel.cn/sdc/v1/vessels/status/track", u = {
127
126
  headers: {
128
- Authorization: `${(p = this.token) == null ? void 0 : p.tokenType} ${(w = this.token) == null ? void 0 : w.accessToken}`
127
+ Authorization: `${(y = this.token) == null ? void 0 : y.tokenType} ${(k = this.token) == null ? void 0 : k.accessToken}`
129
128
  },
130
129
  json: {
131
130
  mmsi: s,
132
- startTime: d.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),
133
- endTime: f.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")
131
+ startTime: o.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),
132
+ endTime: r.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")
134
133
  }
135
134
  };
136
- g.info("[%s] fetch trajectory from: %s - %j", t.requestId, n, m);
137
- const c = await b.post(n, m).json();
135
+ g.info("[%s] fetch trajectory from: %s - %j", a.requestId, n, u);
136
+ const c = await I.post(n, u).json();
138
137
  if (c.code)
139
- return g.warn("[%s] fetch trajectory failed: %j", t.requestId, n, { message: c.message, status: c.status, code: c.code }), c;
140
- let y = -1;
141
- const k = l(`${(j = c.data[0]) == null ? void 0 : j.postime} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00"), Y = [];
142
- return c.data.forEach((u) => {
143
- for (const N in u)
144
- !isNaN(u[N]) && Number(u[N]) !== 1 / 0 && (u[N] = Number(u[N]));
145
- const S = l(`${u.postime} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00"), h = u.eta ? l(`${u.eta} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00") : void 0, T = u.status, { labelCn: q, labelEn: H } = this.parseStatus(T), _ = {
146
- mmsi: u.mmsi,
147
- imo: a == null ? void 0 : a.imo,
148
- lat: u.lat,
149
- lng: u.lon,
150
- sog: u.sog,
151
- cog: u.cog,
152
- hdg: u.hdg,
153
- draught: u.draught,
154
- status: T,
155
- eta: h == null ? void 0 : h.unix(),
156
- destination: u.dest,
157
- positionTime: S.unix(),
158
- labelCn: q,
138
+ return g.warn("[%s] fetch trajectory failed: %j", a.requestId, n, { message: c.message, status: c.status, code: c.code }), c;
139
+ let m = -1;
140
+ const h = f(`${(p = (Y = c.data) == null ? void 0 : Y[0]) == null ? void 0 : p.postime} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
141
+ return (b = c.data) == null || b.forEach((l) => {
142
+ for (const S in l)
143
+ !isNaN(l[S]) && Number(l[S]) !== 1 / 0 && (l[S] = Number(l[S]));
144
+ const M = f(`${l.postime} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00"), N = l.eta ? f(`${l.eta} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00") : void 0, d = l.status, { labelCn: T, labelEn: H } = this.parseStatus(d), _ = {
145
+ mmsi: l.mmsi,
146
+ imo: i == null ? void 0 : i.imo,
147
+ lat: l.lat,
148
+ lng: l.lon,
149
+ sog: l.sog,
150
+ cog: l.cog,
151
+ hdg: l.hdg,
152
+ draught: l.draught,
153
+ status: d,
154
+ eta: N == null ? void 0 : N.unix(),
155
+ destination: l.dest,
156
+ positionTime: M.unix(),
157
+ labelCn: T,
159
158
  labelEn: H,
160
159
  method: "trajectory",
161
160
  vendor: "myVessel",
162
- utc: S.utc().format()
163
- }, D = Math.floor(S.diff(k, "minute", !0) / (i || 1));
164
- D !== y && (y = D, Y.push(_));
165
- }), Y;
161
+ utc: M.utc().format()
162
+ }, D = Math.floor(M.diff(h, "minute", !0) / (e || 1));
163
+ D !== m && (m = D, t.push(_));
164
+ }), t;
166
165
  }
167
166
  }
168
- class K extends v {
167
+ class K extends q {
169
168
  constructor(s) {
170
169
  super();
171
170
  x(this, "token");
@@ -177,15 +176,15 @@ class K extends v {
177
176
  mmsi: s,
178
177
  usertoken: this.token
179
178
  }
180
- }, e = await b.post(r, i).json();
179
+ }, e = await I.post(r, i).json();
181
180
  g.info("[%s] fetch realtime position from: %s - %j", o.requestId, r, i);
182
181
  const t = e == null ? void 0 : e.list;
183
182
  if (!t)
184
183
  return g.warn("[%s] fetch realtime position failed: %j", o.requestId, r, e), e;
185
- for (const c in t)
186
- !isNaN(t[c]) && Number(t[c]) !== 1 / 0 && (t[c] = Number(t[c]));
184
+ for (const h in t)
185
+ !isNaN(t[h]) && Number(t[h]) !== 1 / 0 && (t[h] = Number(t[h]));
187
186
  t.status = t.sp > 3 ? 0 : 1;
188
- const a = t.status, { labelCn: n, labelEn: d } = this.parseStatus(a), f = l(`${t.ti} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
187
+ const a = t.status, { labelCn: n, labelEn: u } = this.parseStatus(a), c = f(`${t.ti} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
189
188
  return {
190
189
  mmsi: t.m,
191
190
  name: t.n,
@@ -200,13 +199,13 @@ class K extends v {
200
199
  cog: t.co,
201
200
  hdg: t.h,
202
201
  rot: isNaN(t.rot) ? 0 : t.rot,
203
- eta: /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(t.eta) ? l(`${t.eta} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00").unix() : void 0,
202
+ eta: /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(t.eta) ? f(`${t.eta} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00").unix() : void 0,
204
203
  destination: t.destination,
205
- positionTime: f.unix(),
206
- utc: f.utc().format(),
204
+ positionTime: c.unix(),
205
+ utc: c.utc().format(),
207
206
  status: a,
208
207
  labelCn: n,
209
- labelEn: d,
208
+ labelEn: u,
210
209
  method: "position",
211
210
  vendor: "hifleet"
212
211
  };
@@ -223,7 +222,7 @@ class K extends v {
223
222
  Host: "www.hifleet.com"
224
223
  }
225
224
  };
226
- let e = await b.post(r, i).json();
225
+ let e = await I.post(r, i).json();
227
226
  g.info("[%s] fetch vessel props from: %s - %j", o.requestId, r, i), e instanceof Array && (e = e[0]);
228
227
  for (const a in e)
229
228
  !isNaN(e[a]) && Number(e[a]) !== 1 / 0 && (e[a] = Number(e[a]));
@@ -236,60 +235,60 @@ class K extends v {
236
235
  breadth: e.b,
237
236
  draught: e.dr
238
237
  };
239
- return r = "https://www.hifleet.com/hifleetapi/sameShipSearch.do", e = await b.post(r, i).json(), g.info("[%s] fetch vessel dead weight from: %s - %j", o.requestId, r, i), e instanceof Array && (e = e[0]), e && (t.deadweight = Number(e.dwt)), t;
238
+ return r = "https://www.hifleet.com/hifleetapi/sameShipSearch.do", e = await I.post(r, i).json(), g.info("[%s] fetch vessel dead weight from: %s - %j", o.requestId, r, i), e instanceof Array && (e = e[0]), e && (t.deadweight = Number(e.dwt)), t;
240
239
  }
241
240
  async trajectory(s, o, r, i, e = !0, t = {}) {
242
- var j, u, S;
241
+ var l, M, N;
243
242
  const a = await this.realTimePosition(s, t);
244
- let n = l(o);
245
- const d = l(r), f = l();
243
+ let n = f(o);
244
+ const u = f(r), c = f();
246
245
  if (e) {
247
- let h = d.diff(n, "d", !0);
248
- h < 0 ? n = d.clone().subtract(40, "d") : h < 30 ? n.subtract(10, "d") : h < 60 ? n.subtract(5, "d") : n = d.clone().subtract(80, "d"), h = f.diff(d, "d", !0), d.add(h > 10 ? 240 : h * 24, "h");
246
+ let d = u.diff(n, "d", !0);
247
+ d < 0 ? n = u.clone().subtract(40, "d") : d < 30 ? n.subtract(10, "d") : d < 60 ? n.subtract(5, "d") : n = u.clone().subtract(80, "d"), d = c.diff(u, "d", !0), u.add(d > 10 ? 240 : d * 24, "h");
249
248
  }
250
249
  const m = {
251
250
  searchParams: {
252
- endtime: d.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),
251
+ endtime: u.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),
253
252
  starttime: n.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),
254
253
  mmsi: s,
255
254
  usertoken: this.token
256
255
  }
257
- }, c = "https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token", y = await b.get(c, m).json();
258
- g.info("[%s] fetch trajectory from: %s - %j", t.requestId, c, m);
256
+ }, h = "https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token", y = await I.get(h, m).json();
257
+ g.info("[%s] fetch trajectory from: %s - %j", t.requestId, h, m);
259
258
  let k;
260
- y && (k = ((u = (j = y.ships) == null ? void 0 : j.offors) == null ? void 0 : u.ship) || [], k.length || g.warn("[%s] fetch trajectory failed: %j", t.requestId, y));
259
+ y && (k = ((M = (l = y.ships) == null ? void 0 : l.offors) == null ? void 0 : M.ship) || [], k.length || g.warn("[%s] fetch trajectory failed: %j", t.requestId, y));
261
260
  const Y = [];
262
261
  let p = -1;
263
- const w = l(`${(S = k == null ? void 0 : k[0]) == null ? void 0 : S.ti} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
264
- for (const h of k) {
265
- for (const N in h)
266
- !isNaN(h[N]) && Number(h[N]) !== 1 / 0 && (h[N] = Number(h[N]));
267
- const T = l(`${h.ti} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
268
- h.status = h.sp > 4 ? 0 : 1;
269
- const { labelEn: q, labelCn: H } = this.parseStatus(h.status), _ = {
270
- mmsi: h.m,
271
- name: h.n,
262
+ const b = f(`${(N = k == null ? void 0 : k[0]) == null ? void 0 : N.ti} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
263
+ for (const d of k) {
264
+ for (const v in d)
265
+ !isNaN(d[v]) && Number(d[v]) !== 1 / 0 && (d[v] = Number(d[v]));
266
+ const T = f(`${d.ti} +08:00`, "YYYY-MM-DD HH:mm:ss +08:00");
267
+ d.status = d.sp > 4 ? 0 : 1;
268
+ const { labelEn: H, labelCn: _ } = this.parseStatus(d.status), D = {
269
+ mmsi: d.m,
270
+ name: d.n,
272
271
  imo: a == null ? void 0 : a.imo,
273
- lat: h.la,
274
- lng: h.lo,
275
- draught: h.draught,
276
- sog: h.sp,
277
- cog: h.co,
278
- hdg: h.hdg,
272
+ lat: d.la,
273
+ lng: d.lo,
274
+ draught: d.draught,
275
+ sog: d.sp,
276
+ cog: d.co,
277
+ hdg: d.hdg,
279
278
  positionTime: T.unix(),
280
279
  utc: T.utc().format(),
281
- status: h.status,
282
- labelCn: H,
283
- labelEn: q,
280
+ status: d.status,
281
+ labelCn: _,
282
+ labelEn: H,
284
283
  method: "trajectory",
285
284
  vendor: "hifleet"
286
- }, D = Math.floor(T.diff(w, "minute", !0) / (i || 1));
287
- D !== p && (p = D, Y.push(_));
285
+ }, S = Math.floor(T.diff(b, "minute", !0) / (i || 1));
286
+ S !== p && (p = S, Y.push(D));
288
287
  }
289
288
  return Y;
290
289
  }
291
290
  }
292
- class V extends v {
291
+ class V extends q {
293
292
  constructor(s) {
294
293
  super();
295
294
  x(this, "token");
@@ -302,13 +301,13 @@ class V extends v {
302
301
  k: this.token,
303
302
  enc: 1
304
303
  }
305
- }, i = "https://api.shipxy.com/apicall/GetSingleShip", e = await b.get(i, r).json();
304
+ }, i = "https://api.shipxy.com/apicall/GetSingleShip", e = await I.get(i, r).json();
306
305
  if (g.info("[%s] fetch realtime position from: %s - %j", o.requestId, i, r), (e == null ? void 0 : e.status) !== 0)
307
306
  return e;
308
307
  const t = e.data[0];
309
308
  for (const m in t)
310
309
  !isNaN(t[m]) && Number(t[m]) !== 1 / 0 && (t[m] = Number(t[m]));
311
- const { labelCn: a, labelEn: n } = await this.parseStatus(t.navistat), d = l.unix(t.lasttime);
310
+ const { labelCn: a, labelEn: n } = await this.parseStatus(t.navistat), u = f.unix(t.lasttime);
312
311
  return {
313
312
  mmsi: t.ShipID,
314
313
  name: t.name,
@@ -324,7 +323,7 @@ class V extends v {
324
323
  hdg: Math.round(t.hdg / 100 * 100) / 100,
325
324
  rot: Math.round(t.rot / 100 * 100) / 100,
326
325
  positionTime: t.lasttime,
327
- utc: d.utc().format(),
326
+ utc: u.utc().format(),
328
327
  status: t.navistat,
329
328
  labelEn: n,
330
329
  labelCn: a,
@@ -333,40 +332,40 @@ class V extends v {
333
332
  };
334
333
  }
335
334
  async trajectory(s, o, r, i, e = !0, t = {}) {
336
- var w;
337
- const a = await this.realTimePosition(s, t), n = l(o), d = l(r), f = "https://api.shipxy.com/apicall/GetShipTrack", m = {
335
+ var b;
336
+ const a = await this.realTimePosition(s, t), n = f(o), u = f(r), c = "https://api.shipxy.com/apicall/GetShipTrack", m = {
338
337
  searchParams: {
339
338
  id: s,
340
339
  k: this.token,
341
340
  enc: 1,
342
341
  cut: 0,
343
342
  btm: n.unix(),
344
- etm: d.unix()
343
+ etm: u.unix()
345
344
  }
346
- }, c = await b.get(f, m).json();
347
- if (g.info("[%s] fetch trajectory from: %s - %j", t.requestId, f, m), (c == null ? void 0 : c.status) !== 0)
348
- return c;
349
- const y = c == null ? void 0 : c.points, k = [], Y = l.unix((w = y[0]) == null ? void 0 : w.utc);
345
+ }, h = await I.get(c, m).json();
346
+ if (g.info("[%s] fetch trajectory from: %s - %j", t.requestId, c, m), (h == null ? void 0 : h.status) !== 0)
347
+ return h;
348
+ const y = h == null ? void 0 : h.points, k = [], Y = f.unix((b = y[0]) == null ? void 0 : b.utc);
350
349
  let p = -1;
351
- for (const j of y) {
352
- const u = l.unix(j.utc), S = {
350
+ for (const l of y) {
351
+ const M = f.unix(l.utc), N = {
353
352
  imo: a == null ? void 0 : a.imo,
354
353
  mmsi: s,
355
- sog: Math.round(j.sog * 3600 / 1e3 / 1852 * 100) / 100,
356
- cog: Math.round(j.cog / 100 * 100) / 100,
357
- lat: Math.round(j.lat / 1e6 * 1e5) / 1e5,
358
- lng: Math.round(j.lon / 1e6 * 1e5) / 1e5,
359
- positionTime: u.unix(),
360
- utc: u.utc().format(),
354
+ sog: Math.round(l.sog * 3600 / 1e3 / 1852 * 100) / 100,
355
+ cog: Math.round(l.cog / 100 * 100) / 100,
356
+ lat: Math.round(l.lat / 1e6 * 1e5) / 1e5,
357
+ lng: Math.round(l.lon / 1e6 * 1e5) / 1e5,
358
+ positionTime: M.unix(),
359
+ utc: M.utc().format(),
361
360
  method: "trajectory",
362
361
  vendor: "shipxy"
363
- }, h = Math.floor(u.diff(Y, "minute", !0) / (i || 1));
364
- h !== p && (p = h, k.push(S));
362
+ }, d = Math.floor(M.diff(Y, "minute", !0) / (i || 1));
363
+ d !== p && (p = d, k.push(N));
365
364
  }
366
365
  return k;
367
366
  }
368
367
  }
369
- class z extends v {
368
+ class z extends q {
370
369
  constructor(s) {
371
370
  super();
372
371
  x(this, "token");
@@ -380,7 +379,7 @@ class z extends v {
380
379
  json: {
381
380
  mmsiList: s
382
381
  }
383
- }, i = "https://api3.myships.com/sp/ships/getShipIdByMMSI", e = await b.post(i, r).json();
382
+ }, i = "https://api3.myships.com/sp/ships/getShipIdByMMSI", e = await I.post(i, r).json();
384
383
  return g.info("[%s] fetch ship id from: %s - %j", o.requestId, i, r), e.code !== "0" ? e : e.data[0].shipId;
385
384
  }
386
385
  async getShipInfo(s, o = {}) {
@@ -391,7 +390,7 @@ class z extends v {
391
390
  json: {
392
391
  shipId: s
393
392
  }
394
- }, i = "https://api3.myships.com/sp/ships/aissta", e = await b.post(i, r).json();
393
+ }, i = "https://api3.myships.com/sp/ships/aissta", e = await I.post(i, r).json();
395
394
  if (g.info("[%s] fetch ship info from: %s - %j", o.requestId, i, r), e.code !== "0")
396
395
  return e;
397
396
  const t = e.data;
@@ -414,12 +413,12 @@ class z extends v {
414
413
  json: {
415
414
  shipId: r
416
415
  }
417
- }, t = "https://api3.myships.com/sp/ships/position/latest", a = await b.post(t, e).json();
416
+ }, t = "https://api3.myships.com/sp/ships/position/latest", a = await I.post(t, e).json();
418
417
  g.info("[%s] fetch realtime position from: %s - %j", o.requestId, t, e);
419
418
  const n = a.data[0];
420
419
  for (const y in n)
421
420
  !isNaN(n[y]) && Number(n[y]) !== 1 / 0 && (n[y] = Number(n[y]));
422
- const { labelCn: d, labelEn: f } = await this.parseStatus(n.aisNavStatus), m = l.unix(n.posTime);
421
+ const { labelCn: u, labelEn: c } = await this.parseStatus(n.aisNavStatus), m = f.unix(n.posTime);
423
422
  return {
424
423
  ...i,
425
424
  mmsi: s,
@@ -432,21 +431,21 @@ class z extends v {
432
431
  positionTime: n.posTime,
433
432
  utc: m.utc().format(),
434
433
  status: n.aisNavStatus,
435
- labelEn: f,
436
- labelCn: d,
434
+ labelEn: c,
435
+ labelCn: u,
437
436
  method: "position",
438
437
  vendor: "myship"
439
438
  };
440
439
  }
441
440
  async trajectory(s, o, r, i, e = !0, t = {}) {
442
- const a = l(o), n = l(r), d = await this.getShipId(s), f = await this.getShipInfo(d), m = [];
441
+ const a = f(o), n = f(r), u = await this.getShipId(s), c = await this.getShipInfo(u), m = [];
443
442
  for (; n.diff(a, "day", !0) > 30; )
444
- await this.trajectoryIn30Day(d, a.unix(), a.add(30, "day").unix(), f, s, i, m);
445
- return await this.trajectoryIn30Day(d, a.unix(), n.unix(), f, s, i, m), m;
443
+ await this.trajectoryIn30Day(u, a.unix(), a.add(30, "day").unix(), c, s, i, m);
444
+ return await this.trajectoryIn30Day(u, a.unix(), n.unix(), c, s, i, m), m;
446
445
  }
447
446
  async trajectoryIn30Day(s, o, r, i, e, t, a, n = {}) {
448
447
  var Y;
449
- const d = {
448
+ const u = {
450
449
  headers: {
451
450
  appKey: this.token
452
451
  },
@@ -455,16 +454,16 @@ class z extends v {
455
454
  startTime: o,
456
455
  endTime: r
457
456
  }
458
- }, f = "https://api3.myships.com/sp/ships/position/history", m = await b.post(f, d).json();
459
- if (g.info("[%s] fetch trajectory from: %s - %j", n.requestId, f, d), m.code !== "0")
457
+ }, c = "https://api3.myships.com/sp/ships/position/history", m = await I.post(c, u).json();
458
+ if (g.info("[%s] fetch trajectory from: %s - %j", n.requestId, c, u), m.code !== "0")
460
459
  return g.warn("[%s] invoke myship trajectory failed: %j", n.requestId, m), m;
461
- const c = m.data;
462
- for (const p in c)
463
- !isNaN(c[p]) && Number(c[p]) !== 1 / 0 && (c[p] = Number(c[p]));
464
- const y = l.unix((Y = c[0]) == null ? void 0 : Y.posTime);
460
+ const h = m.data;
461
+ for (const p in h)
462
+ !isNaN(h[p]) && Number(h[p]) !== 1 / 0 && (h[p] = Number(h[p]));
463
+ const y = f.unix((Y = h[0]) == null ? void 0 : Y.posTime);
465
464
  let k = -1;
466
- for (const p of c) {
467
- const w = l.unix(p.posTime), j = {
465
+ for (const p of h) {
466
+ const b = f.unix(p.posTime), l = {
468
467
  imo: i == null ? void 0 : i.imo,
469
468
  mmsi: e,
470
469
  lat: Math.round(p.lat / 1e4 / 60 * 1e5) / 1e5,
@@ -473,18 +472,18 @@ class z extends v {
473
472
  cog: Math.round(p.cog / 10 * 100) / 100,
474
473
  hdg: Math.round(p.heading * 100) / 100,
475
474
  rot: Math.round(p.rot * 100) / 100,
476
- positionTime: w.unix(),
477
- utc: w.utc().format(),
475
+ positionTime: b.unix(),
476
+ utc: b.utc().format(),
478
477
  method: "trajectory",
479
478
  vendor: "myship"
480
- }, u = Math.floor(w.diff(y, "minute", !0) / (t || 1));
481
- u !== k && (k = u, a.push(j));
479
+ }, M = Math.floor(b.diff(y, "minute", !0) / (t || 1));
480
+ M !== k && (k = M, a.push(l));
482
481
  }
483
482
  return a;
484
483
  }
485
484
  }
486
485
  export {
487
- v as AISImpl,
486
+ q as AISImpl,
488
487
  K as HifleetImpl,
489
488
  z as MyShipImpl,
490
489
  L as MyVesselImpl,
@@ -1 +1 @@
1
- (function(y,p){typeof exports=="object"&&typeof module<"u"?p(exports,require("got"),require("log4js"),require("moment")):typeof define=="function"&&define.amd?define(["exports","got","log4js","moment"],p):(y=typeof globalThis<"u"?globalThis:y||self,p(y["idm-plugin-rabbitmq"]={},y.got,y.log4js,y.moment))})(this,function(y,p,x,l){"use strict";var V=Object.defineProperty;var K=(y,p,x)=>p in y?V(y,p,{enumerable:!0,configurable:!0,writable:!0,value:x}):y[p]=x;var q=(y,p,x)=>(K(y,typeof p!="symbol"?p+"":p,x),x);function $(w){const S=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(w){for(const s in w)if(s!=="default"){const i=Object.getOwnPropertyDescriptor(w,s);Object.defineProperty(S,s,i.get?i:{enumerable:!0,get:()=>w[s]})}}return S.default=w,Object.freeze(S)}const j=$(x).getLogger();class v{parseStatus(S){let s,i;switch(S){case 0:s="在航(主机推动)",i="The engine is in use";break;case 1:s="锚泊",i="Anchored";break;case 2:s="失控",i="Not operated";break;case 3:s="操纵受限",i="Limited airworthiness";break;case 4:s="吃水受限",i="Limited by ship's draft";break;case 5:s="靠泊",i="Mooring";break;case 6:s="搁浅",i="Stranded";break;case 7:s="捕捞作业",i="Engaged in fishing";break;case 8:s="靠帆船提供动力",i="Sailing";break;default:s="未定义",i="Undefined"}return{labelCn:s,labelEn:i}}}class E extends v{constructor(s,i){super();q(this,"clientId");q(this,"clientSecret");q(this,"token");this.clientId=s,this.clientSecret=i}async authToken(s={}){const i="https://svc.data.myvessel.cn/ada/oauth/token",r={searchParams:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},o=await p.post(i,r).json();j.info("[%s] fetch access token from: %s - %j",s.requestId,i,o),o.error||(this.token={accessToken:o.access_token,tokenType:o.token_type,expiresIn:o.expires_in,scope:o.scope,jti:o.jti,issuedAt:l().utc().format()})}async realTimePosition(s,i={}){var h,m,f;(!this.token||l().diff(l(this.token.issuedAt),"seconds")>((h=this.token)==null?void 0:h.expiresIn)-300)&&await this.authToken(i);const r="https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit",o={headers:{Authorization:`${(m=this.token)==null?void 0:m.tokenType} ${(f=this.token)==null?void 0:f.accessToken}`},searchParams:{mmsi:s}};j.info("[%s] fetch realtime position from: %s - %j",i.requestId,r,o);const e=await p.get(r,o).json();if(e.code)return j.warn("[%s] fetch realtime position failed: %j",i.requestId,r,{message:e.message,status:e.status,code:e.code}),e;const t=e.data;for(const c in t)!isNaN(t[c])&&Number(t[c])!==1/0&&(t[c]=Number(t[c]));const a=l(`${t.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:t.mmsi,name:t.vesselName,imo:t.imo,callSign:t.callsign,lat:t.lat,lng:t.lon,length:t.length,width:t.width,draught:t.currDraught,sog:t.sog,cog:t.cog,hdg:t.hdg,rot:t.rot,eta:t.eta,destination:t.dest,positionTime:a.unix(),status:t.status,labelCn:t.statusNameCn,labelEn:t.statusNameEn,method:"position",vendor:"myVessel",utc:a.utc().format()}}async trajectory(s,i,r,o,e=!0,t={}){var g,I,M;(!this.token||l().diff(l(this.token.issuedAt),"seconds")>this.token.expiresIn-300)&&await this.authToken(t);const a=await this.realTimePosition(s,t),n="https://svc.data.myvessel.cn/sdc/v1/vessels/status/track";let h=l(i);const m=l(r);if(e){const d=m.diff(h,"d",!0);(d<0||d>30)&&(h=m.clone().subtract(30,"d"))}const f={headers:{Authorization:`${(g=this.token)==null?void 0:g.tokenType} ${(I=this.token)==null?void 0:I.accessToken}`},json:{mmsi:s,startTime:h.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),endTime:m.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")}};j.info("[%s] fetch trajectory from: %s - %j",t.requestId,n,f);const c=await p.post(n,f).json();if(c.code)return j.warn("[%s] fetch trajectory failed: %j",t.requestId,n,{message:c.message,status:c.status,code:c.code}),c;let k=-1;const b=l(`${(M=c.data[0])==null?void 0:M.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),Y=[];return c.data.forEach(d=>{for(const N in d)!isNaN(d[N])&&Number(d[N])!==1/0&&(d[N]=Number(d[N]));const T=l(`${d.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),u=d.eta?l(`${d.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"):void 0,D=d.status,{labelCn:_,labelEn:P}=this.parseStatus(D),O={mmsi:d.mmsi,imo:a==null?void 0:a.imo,lat:d.lat,lng:d.lon,sog:d.sog,cog:d.cog,hdg:d.hdg,draught:d.draught,status:D,eta:u==null?void 0:u.unix(),destination:d.dest,positionTime:T.unix(),labelCn:_,labelEn:P,method:"trajectory",vendor:"myVessel",utc:T.utc().format()},H=Math.floor(T.diff(b,"minute",!0)/(o||1));H!==k&&(k=H,Y.push(O))}),Y}}class A extends v{constructor(s){super();q(this,"token");this.token=s}async realTimePosition(s,i={}){const r="https://api.hifleet.com/position/position/get/token",o={searchParams:{mmsi:s,usertoken:this.token}},e=await p.post(r,o).json();j.info("[%s] fetch realtime position from: %s - %j",i.requestId,r,o);const t=e==null?void 0:e.list;if(!t)return j.warn("[%s] fetch realtime position failed: %j",i.requestId,r,e),e;for(const c in t)!isNaN(t[c])&&Number(t[c])!==1/0&&(t[c]=Number(t[c]));t.status=t.sp>3?0:1;const a=t.status,{labelCn:n,labelEn:h}=this.parseStatus(a),m=l(`${t.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:t.m,name:t.n,imo:t.imonumber,callSign:t.callsign,lat:Math.round(t.la/60*1e5)/1e5,lng:Math.round(t.lo/60*1e5)/1e5,length:t.l,width:t.w,draught:t.draught,sog:t.sp,cog:t.co,hdg:t.h,rot:isNaN(t.rot)?0:t.rot,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(t.eta)?l(`${t.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00").unix():void 0,destination:t.destination,positionTime:m.unix(),utc:m.utc().format(),status:a,labelCn:n,labelEn:h,method:"position",vendor:"hifleet"}}async search(s,i={}){let r="https://www.hifleet.com/hifleetapi/searchVesselOL.do";const o={searchParams:{keyword:s},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}};let e=await p.post(r,o).json();j.info("[%s] fetch vessel props from: %s - %j",i.requestId,r,o),e instanceof Array&&(e=e[0]);for(const a in e)!isNaN(e[a])&&Number(e[a])!==1/0&&(e[a]=Number(e[a]));const t={mmsi:e.m,name:e.n,imo:e.i,callSign:e.c,length:e.l,breadth:e.b,draught:e.dr};return r="https://www.hifleet.com/hifleetapi/sameShipSearch.do",e=await p.post(r,o).json(),j.info("[%s] fetch vessel dead weight from: %s - %j",i.requestId,r,o),e instanceof Array&&(e=e[0]),e&&(t.deadweight=Number(e.dwt)),t}async trajectory(s,i,r,o,e=!0,t={}){var M,d,T;const a=await this.realTimePosition(s,t);let n=l(i);const h=l(r),m=l();if(e){let u=h.diff(n,"d",!0);u<0?n=h.clone().subtract(40,"d"):u<30?n.subtract(10,"d"):u<60?n.subtract(5,"d"):n=h.clone().subtract(80,"d"),u=m.diff(h,"d",!0),h.add(u>10?240:u*24,"h")}const f={searchParams:{endtime:h.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),starttime:n.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),mmsi:s,usertoken:this.token}},c="https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token",k=await p.get(c,f).json();j.info("[%s] fetch trajectory from: %s - %j",t.requestId,c,f);let b;k&&(b=((d=(M=k.ships)==null?void 0:M.offors)==null?void 0:d.ship)||[],b.length||j.warn("[%s] fetch trajectory failed: %j",t.requestId,k));const Y=[];let g=-1;const I=l(`${(T=b==null?void 0:b[0])==null?void 0:T.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");for(const u of b){for(const N in u)!isNaN(u[N])&&Number(u[N])!==1/0&&(u[N]=Number(u[N]));const D=l(`${u.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");u.status=u.sp>4?0:1;const{labelEn:_,labelCn:P}=this.parseStatus(u.status),O={mmsi:u.m,name:u.n,imo:a==null?void 0:a.imo,lat:u.la,lng:u.lo,draught:u.draught,sog:u.sp,cog:u.co,hdg:u.hdg,positionTime:D.unix(),utc:D.utc().format(),status:u.status,labelCn:P,labelEn:_,method:"trajectory",vendor:"hifleet"},H=Math.floor(D.diff(I,"minute",!0)/(o||1));H!==g&&(g=H,Y.push(O))}return Y}}class C extends v{constructor(s){super();q(this,"token");this.token=s}async realTimePosition(s,i={}){const r={searchParams:{id:s,k:this.token,enc:1}},o="https://api.shipxy.com/apicall/GetSingleShip",e=await p.get(o,r).json();if(j.info("[%s] fetch realtime position from: %s - %j",i.requestId,o,r),(e==null?void 0:e.status)!==0)return e;const t=e.data[0];for(const f in t)!isNaN(t[f])&&Number(t[f])!==1/0&&(t[f]=Number(t[f]));const{labelCn:a,labelEn:n}=await this.parseStatus(t.navistat),h=l.unix(t.lasttime);return{mmsi:t.ShipID,name:t.name,imo:t.imo,callSign:t.callsign,lat:Math.round(t.lat/1e6*1e5)/1e5,lng:Math.round(t.lon/1e6*1e5)/1e5,length:Math.round(t.length/10*100)/100,width:Math.round(t.width/10*100)/100,draught:Math.round(t.draught/1e3*100)/100,sog:Math.round(t.sog*3600/1e3/1852*100)/100,cog:Math.round(t.cog/100*100)/100,hdg:Math.round(t.hdg/100*100)/100,rot:Math.round(t.rot/100*100)/100,positionTime:t.lasttime,utc:h.utc().format(),status:t.navistat,labelEn:n,labelCn:a,method:"position",vendor:"shipxy"}}async trajectory(s,i,r,o,e=!0,t={}){var I;const a=await this.realTimePosition(s,t),n=l(i),h=l(r),m="https://api.shipxy.com/apicall/GetShipTrack",f={searchParams:{id:s,k:this.token,enc:1,cut:0,btm:n.unix(),etm:h.unix()}},c=await p.get(m,f).json();if(j.info("[%s] fetch trajectory from: %s - %j",t.requestId,m,f),(c==null?void 0:c.status)!==0)return c;const k=c==null?void 0:c.points,b=[],Y=l.unix((I=k[0])==null?void 0:I.utc);let g=-1;for(const M of k){const d=l.unix(M.utc),T={imo:a==null?void 0:a.imo,mmsi:s,sog:Math.round(M.sog*3600/1e3/1852*100)/100,cog:Math.round(M.cog/100*100)/100,lat:Math.round(M.lat/1e6*1e5)/1e5,lng:Math.round(M.lon/1e6*1e5)/1e5,positionTime:d.unix(),utc:d.utc().format(),method:"trajectory",vendor:"shipxy"},u=Math.floor(d.diff(Y,"minute",!0)/(o||1));u!==g&&(g=u,b.push(T))}return b}}class L extends v{constructor(s){super();q(this,"token");this.token=s}async getShipId(s,i={}){const r={headers:{appKey:this.token},json:{mmsiList:s}},o="https://api3.myships.com/sp/ships/getShipIdByMMSI",e=await p.post(o,r).json();return j.info("[%s] fetch ship id from: %s - %j",i.requestId,o,r),e.code!=="0"?e:e.data[0].shipId}async getShipInfo(s,i={}){const r={headers:{appKey:this.token},json:{shipId:s}},o="https://api3.myships.com/sp/ships/aissta",e=await p.post(o,r).json();if(j.info("[%s] fetch ship info from: %s - %j",i.requestId,o,r),e.code!=="0")return e;const t=e.data;let a=t.imo;return s==="407170"&&(a="9198379",j.warn("[%s] ship(%s) imo error: %s, should be %s",i.requestId,s,t.imo,a)),{mmsi:t.mmsi,name:t.shipnameEn,imo:a,callSign:t.callSign,length:t.length,width:t.breadth,draught:(t.draught||100)/10}}async realTimePosition(s,i={}){const r=await this.getShipId(s,i),o=await this.getShipInfo(r,i),e={headers:{appKey:this.token},json:{shipId:r}},t="https://api3.myships.com/sp/ships/position/latest",a=await p.post(t,e).json();j.info("[%s] fetch realtime position from: %s - %j",i.requestId,t,e);const n=a.data[0];for(const k in n)!isNaN(n[k])&&Number(n[k])!==1/0&&(n[k]=Number(n[k]));const{labelCn:h,labelEn:m}=await this.parseStatus(n.aisNavStatus),f=l.unix(n.posTime);return{...o,mmsi:s,lat:Math.round(n.lat/1e4/60*1e5)/1e5,lng:Math.round(n.lon/1e4/60*1e5)/1e5,sog:Math.round(n.sog/10*100)/100,cog:Math.round(n.cog/10*100)/100,hdg:Math.round(n.heading*100)/100,rot:Math.round(n.rot*100)/100,positionTime:n.posTime,utc:f.utc().format(),status:n.aisNavStatus,labelEn:m,labelCn:h,method:"position",vendor:"myship"}}async trajectory(s,i,r,o,e=!0,t={}){const a=l(i),n=l(r),h=await this.getShipId(s),m=await this.getShipInfo(h),f=[];for(;n.diff(a,"day",!0)>30;)await this.trajectoryIn30Day(h,a.unix(),a.add(30,"day").unix(),m,s,o,f);return await this.trajectoryIn30Day(h,a.unix(),n.unix(),m,s,o,f),f}async trajectoryIn30Day(s,i,r,o,e,t,a,n={}){var Y;const h={headers:{appKey:this.token},json:{shipId:s,startTime:i,endTime:r}},m="https://api3.myships.com/sp/ships/position/history",f=await p.post(m,h).json();if(j.info("[%s] fetch trajectory from: %s - %j",n.requestId,m,h),f.code!=="0")return j.warn("[%s] invoke myship trajectory failed: %j",n.requestId,f),f;const c=f.data;for(const g in c)!isNaN(c[g])&&Number(c[g])!==1/0&&(c[g]=Number(c[g]));const k=l.unix((Y=c[0])==null?void 0:Y.posTime);let b=-1;for(const g of c){const I=l.unix(g.posTime),M={imo:o==null?void 0:o.imo,mmsi:e,lat:Math.round(g.lat/1e4/60*1e5)/1e5,lng:Math.round(g.lon/1e4/60*1e5)/1e5,sog:Math.round(g.sog/10*100)/100,cog:Math.round(g.cog/10*100)/100,hdg:Math.round(g.heading*100)/100,rot:Math.round(g.rot*100)/100,positionTime:I.unix(),utc:I.utc().format(),method:"trajectory",vendor:"myship"},d=Math.floor(I.diff(k,"minute",!0)/(t||1));d!==b&&(b=d,a.push(M))}return a}}y.AISImpl=v,y.HifleetImpl=A,y.MyShipImpl=L,y.MyVesselImpl=E,y.ShipxyImpl=C,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"})});
1
+ (function(y,m){typeof exports=="object"&&typeof module<"u"?m(exports,require("got"),require("log4js"),require("moment")):typeof define=="function"&&define.amd?define(["exports","got","log4js","moment"],m):(y=typeof globalThis<"u"?globalThis:y||self,m(y["idm-plugin-rabbitmq"]={},y.got,y.log4js,y.moment))})(this,function(y,m,T,l){"use strict";var V=Object.defineProperty;var K=(y,m,T)=>m in y?V(y,m,{enumerable:!0,configurable:!0,writable:!0,value:T}):y[m]=T;var x=(y,m,T)=>(K(y,typeof m!="symbol"?m+"":m,T),T);function $(w){const S=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(w){for(const s in w)if(s!=="default"){const i=Object.getOwnPropertyDescriptor(w,s);Object.defineProperty(S,s,i.get?i:{enumerable:!0,get:()=>w[s]})}}return S.default=w,Object.freeze(S)}const j=$(T).getLogger();class q{parseStatus(S){let s,i;switch(S){case 0:s="在航(主机推动)",i="The engine is in use";break;case 1:s="锚泊",i="Anchored";break;case 2:s="失控",i="Not operated";break;case 3:s="操纵受限",i="Limited airworthiness";break;case 4:s="吃水受限",i="Limited by ship's draft";break;case 5:s="靠泊",i="Mooring";break;case 6:s="搁浅",i="Stranded";break;case 7:s="捕捞作业",i="Engaged in fishing";break;case 8:s="靠帆船提供动力",i="Sailing";break;default:s="未定义",i="Undefined"}return{labelCn:s,labelEn:i}}}class E extends q{constructor(s,i){super();x(this,"clientId");x(this,"clientSecret");x(this,"token");this.clientId=s,this.clientSecret=i}async authToken(s={}){const i="https://svc.data.myvessel.cn/ada/oauth/token",r={searchParams:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},o=await m.post(i,r).json();j.info("[%s] fetch access token from: %s - %j",s.requestId,i,o),o.error||(this.token={accessToken:o.access_token,tokenType:o.token_type,expiresIn:o.expires_in,scope:o.scope,jti:o.jti,issuedAt:l().utc().format()})}async realTimePosition(s,i={}){var h,c,f;(!this.token||l().diff(l(this.token.issuedAt),"seconds")>((h=this.token)==null?void 0:h.expiresIn)-300)&&await this.authToken(i);const r="https://svc.data.myvessel.cn/sdc/v1/vessels/status/location/unit",o={headers:{Authorization:`${(c=this.token)==null?void 0:c.tokenType} ${(f=this.token)==null?void 0:f.accessToken}`},searchParams:{mmsi:s}};j.info("[%s] fetch realtime position from: %s - %j",i.requestId,r,o);const e=await m.get(r,o).json();if(e.code)return j.warn("[%s] fetch realtime position failed: %j",i.requestId,r,{message:e.message,status:e.status,code:e.code}),e;const t=e.data;for(const u in t)!isNaN(t[u])&&Number(t[u])!==1/0&&(t[u]=Number(t[u]));const a=l(`${t.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:t.mmsi,name:t.vesselName,imo:t.imo,callSign:t.callsign,lat:t.lat,lng:t.lon,length:t.length,width:t.width,draught:t.currDraught,sog:t.sog,cog:t.cog,hdg:t.hdg,rot:t.rot,eta:t.eta,destination:t.dest,positionTime:a.unix(),status:t.status,labelCn:t.statusNameCn,labelEn:t.statusNameEn,method:"position",vendor:"myVessel",utc:a.utc().format()}}async trajectory(s,i,r,o,e=!0,t={}){(!this.token||l().diff(l(this.token.issuedAt),"seconds")>this.token.expiresIn-300)&&await this.authToken(t);const a=await this.realTimePosition(s,t),n=l(i),h=l(r),c=[];for(;h.diff(n,"day",!0)>30;)await this.trajectoryIn30Day(s,n,n.clone().add(30,"day"),a,o,c,t),n.add(30,"day");return await this.trajectoryIn30Day(s,n,h,a,o,c,t),c}async trajectoryIn30Day(s,i,r,o,e,t,a={}){var k,M,Y,g,I;const n="https://svc.data.myvessel.cn/sdc/v1/vessels/status/track",h={headers:{Authorization:`${(k=this.token)==null?void 0:k.tokenType} ${(M=this.token)==null?void 0:M.accessToken}`},json:{mmsi:s,startTime:i.utcOffset(8).format("YYYY-MM-DD HH:mm:ss"),endTime:r.utcOffset(8).format("YYYY-MM-DD HH:mm:ss")}};j.info("[%s] fetch trajectory from: %s - %j",a.requestId,n,h);const c=await m.post(n,h).json();if(c.code)return j.warn("[%s] fetch trajectory failed: %j",a.requestId,n,{message:c.message,status:c.status,code:c.code}),c;let f=-1;const u=l(`${(g=(Y=c.data)==null?void 0:Y[0])==null?void 0:g.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return(I=c.data)==null||I.forEach(p=>{for(const D in p)!isNaN(p[D])&&Number(p[D])!==1/0&&(p[D]=Number(p[D]));const b=l(`${p.postime} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"),N=p.eta?l(`${p.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00"):void 0,d=p.status,{labelCn:v,labelEn:P}=this.parseStatus(d),O={mmsi:p.mmsi,imo:o==null?void 0:o.imo,lat:p.lat,lng:p.lon,sog:p.sog,cog:p.cog,hdg:p.hdg,draught:p.draught,status:d,eta:N==null?void 0:N.unix(),destination:p.dest,positionTime:b.unix(),labelCn:v,labelEn:P,method:"trajectory",vendor:"myVessel",utc:b.utc().format()},H=Math.floor(b.diff(u,"minute",!0)/(e||1));H!==f&&(f=H,t.push(O))}),t}}class A extends q{constructor(s){super();x(this,"token");this.token=s}async realTimePosition(s,i={}){const r="https://api.hifleet.com/position/position/get/token",o={searchParams:{mmsi:s,usertoken:this.token}},e=await m.post(r,o).json();j.info("[%s] fetch realtime position from: %s - %j",i.requestId,r,o);const t=e==null?void 0:e.list;if(!t)return j.warn("[%s] fetch realtime position failed: %j",i.requestId,r,e),e;for(const u in t)!isNaN(t[u])&&Number(t[u])!==1/0&&(t[u]=Number(t[u]));t.status=t.sp>3?0:1;const a=t.status,{labelCn:n,labelEn:h}=this.parseStatus(a),c=l(`${t.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");return{mmsi:t.m,name:t.n,imo:t.imonumber,callSign:t.callsign,lat:Math.round(t.la/60*1e5)/1e5,lng:Math.round(t.lo/60*1e5)/1e5,length:t.l,width:t.w,draught:t.draught,sog:t.sp,cog:t.co,hdg:t.h,rot:isNaN(t.rot)?0:t.rot,eta:/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(t.eta)?l(`${t.eta} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00").unix():void 0,destination:t.destination,positionTime:c.unix(),utc:c.utc().format(),status:a,labelCn:n,labelEn:h,method:"position",vendor:"hifleet"}}async search(s,i={}){let r="https://www.hifleet.com/hifleetapi/searchVesselOL.do";const o={searchParams:{keyword:s},headers:{Referer:"https://www.hifleet.com",Origin:"https://www.hifleet.com",Host:"www.hifleet.com"}};let e=await m.post(r,o).json();j.info("[%s] fetch vessel props from: %s - %j",i.requestId,r,o),e instanceof Array&&(e=e[0]);for(const a in e)!isNaN(e[a])&&Number(e[a])!==1/0&&(e[a]=Number(e[a]));const t={mmsi:e.m,name:e.n,imo:e.i,callSign:e.c,length:e.l,breadth:e.b,draught:e.dr};return r="https://www.hifleet.com/hifleetapi/sameShipSearch.do",e=await m.post(r,o).json(),j.info("[%s] fetch vessel dead weight from: %s - %j",i.requestId,r,o),e instanceof Array&&(e=e[0]),e&&(t.deadweight=Number(e.dwt)),t}async trajectory(s,i,r,o,e=!0,t={}){var p,b,N;const a=await this.realTimePosition(s,t);let n=l(i);const h=l(r),c=l();if(e){let d=h.diff(n,"d",!0);d<0?n=h.clone().subtract(40,"d"):d<30?n.subtract(10,"d"):d<60?n.subtract(5,"d"):n=h.clone().subtract(80,"d"),d=c.diff(h,"d",!0),h.add(d>10?240:d*24,"h")}const f={searchParams:{endtime:h.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),starttime:n.utcOffset("+8:00").format("YYYY-MM-DD HH:mm:ss"),mmsi:s,usertoken:this.token}},u="https://api.hifleet.com/position/trajectory/nocompressed/withstatic/token",k=await m.get(u,f).json();j.info("[%s] fetch trajectory from: %s - %j",t.requestId,u,f);let M;k&&(M=((b=(p=k.ships)==null?void 0:p.offors)==null?void 0:b.ship)||[],M.length||j.warn("[%s] fetch trajectory failed: %j",t.requestId,k));const Y=[];let g=-1;const I=l(`${(N=M==null?void 0:M[0])==null?void 0:N.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");for(const d of M){for(const _ in d)!isNaN(d[_])&&Number(d[_])!==1/0&&(d[_]=Number(d[_]));const v=l(`${d.ti} +08:00`,"YYYY-MM-DD HH:mm:ss +08:00");d.status=d.sp>4?0:1;const{labelEn:P,labelCn:O}=this.parseStatus(d.status),H={mmsi:d.m,name:d.n,imo:a==null?void 0:a.imo,lat:d.la,lng:d.lo,draught:d.draught,sog:d.sp,cog:d.co,hdg:d.hdg,positionTime:v.unix(),utc:v.utc().format(),status:d.status,labelCn:O,labelEn:P,method:"trajectory",vendor:"hifleet"},D=Math.floor(v.diff(I,"minute",!0)/(o||1));D!==g&&(g=D,Y.push(H))}return Y}}class C extends q{constructor(s){super();x(this,"token");this.token=s}async realTimePosition(s,i={}){const r={searchParams:{id:s,k:this.token,enc:1}},o="https://api.shipxy.com/apicall/GetSingleShip",e=await m.get(o,r).json();if(j.info("[%s] fetch realtime position from: %s - %j",i.requestId,o,r),(e==null?void 0:e.status)!==0)return e;const t=e.data[0];for(const f in t)!isNaN(t[f])&&Number(t[f])!==1/0&&(t[f]=Number(t[f]));const{labelCn:a,labelEn:n}=await this.parseStatus(t.navistat),h=l.unix(t.lasttime);return{mmsi:t.ShipID,name:t.name,imo:t.imo,callSign:t.callsign,lat:Math.round(t.lat/1e6*1e5)/1e5,lng:Math.round(t.lon/1e6*1e5)/1e5,length:Math.round(t.length/10*100)/100,width:Math.round(t.width/10*100)/100,draught:Math.round(t.draught/1e3*100)/100,sog:Math.round(t.sog*3600/1e3/1852*100)/100,cog:Math.round(t.cog/100*100)/100,hdg:Math.round(t.hdg/100*100)/100,rot:Math.round(t.rot/100*100)/100,positionTime:t.lasttime,utc:h.utc().format(),status:t.navistat,labelEn:n,labelCn:a,method:"position",vendor:"shipxy"}}async trajectory(s,i,r,o,e=!0,t={}){var I;const a=await this.realTimePosition(s,t),n=l(i),h=l(r),c="https://api.shipxy.com/apicall/GetShipTrack",f={searchParams:{id:s,k:this.token,enc:1,cut:0,btm:n.unix(),etm:h.unix()}},u=await m.get(c,f).json();if(j.info("[%s] fetch trajectory from: %s - %j",t.requestId,c,f),(u==null?void 0:u.status)!==0)return u;const k=u==null?void 0:u.points,M=[],Y=l.unix((I=k[0])==null?void 0:I.utc);let g=-1;for(const p of k){const b=l.unix(p.utc),N={imo:a==null?void 0:a.imo,mmsi:s,sog:Math.round(p.sog*3600/1e3/1852*100)/100,cog:Math.round(p.cog/100*100)/100,lat:Math.round(p.lat/1e6*1e5)/1e5,lng:Math.round(p.lon/1e6*1e5)/1e5,positionTime:b.unix(),utc:b.utc().format(),method:"trajectory",vendor:"shipxy"},d=Math.floor(b.diff(Y,"minute",!0)/(o||1));d!==g&&(g=d,M.push(N))}return M}}class L extends q{constructor(s){super();x(this,"token");this.token=s}async getShipId(s,i={}){const r={headers:{appKey:this.token},json:{mmsiList:s}},o="https://api3.myships.com/sp/ships/getShipIdByMMSI",e=await m.post(o,r).json();return j.info("[%s] fetch ship id from: %s - %j",i.requestId,o,r),e.code!=="0"?e:e.data[0].shipId}async getShipInfo(s,i={}){const r={headers:{appKey:this.token},json:{shipId:s}},o="https://api3.myships.com/sp/ships/aissta",e=await m.post(o,r).json();if(j.info("[%s] fetch ship info from: %s - %j",i.requestId,o,r),e.code!=="0")return e;const t=e.data;let a=t.imo;return s==="407170"&&(a="9198379",j.warn("[%s] ship(%s) imo error: %s, should be %s",i.requestId,s,t.imo,a)),{mmsi:t.mmsi,name:t.shipnameEn,imo:a,callSign:t.callSign,length:t.length,width:t.breadth,draught:(t.draught||100)/10}}async realTimePosition(s,i={}){const r=await this.getShipId(s,i),o=await this.getShipInfo(r,i),e={headers:{appKey:this.token},json:{shipId:r}},t="https://api3.myships.com/sp/ships/position/latest",a=await m.post(t,e).json();j.info("[%s] fetch realtime position from: %s - %j",i.requestId,t,e);const n=a.data[0];for(const k in n)!isNaN(n[k])&&Number(n[k])!==1/0&&(n[k]=Number(n[k]));const{labelCn:h,labelEn:c}=await this.parseStatus(n.aisNavStatus),f=l.unix(n.posTime);return{...o,mmsi:s,lat:Math.round(n.lat/1e4/60*1e5)/1e5,lng:Math.round(n.lon/1e4/60*1e5)/1e5,sog:Math.round(n.sog/10*100)/100,cog:Math.round(n.cog/10*100)/100,hdg:Math.round(n.heading*100)/100,rot:Math.round(n.rot*100)/100,positionTime:n.posTime,utc:f.utc().format(),status:n.aisNavStatus,labelEn:c,labelCn:h,method:"position",vendor:"myship"}}async trajectory(s,i,r,o,e=!0,t={}){const a=l(i),n=l(r),h=await this.getShipId(s),c=await this.getShipInfo(h),f=[];for(;n.diff(a,"day",!0)>30;)await this.trajectoryIn30Day(h,a.unix(),a.add(30,"day").unix(),c,s,o,f);return await this.trajectoryIn30Day(h,a.unix(),n.unix(),c,s,o,f),f}async trajectoryIn30Day(s,i,r,o,e,t,a,n={}){var Y;const h={headers:{appKey:this.token},json:{shipId:s,startTime:i,endTime:r}},c="https://api3.myships.com/sp/ships/position/history",f=await m.post(c,h).json();if(j.info("[%s] fetch trajectory from: %s - %j",n.requestId,c,h),f.code!=="0")return j.warn("[%s] invoke myship trajectory failed: %j",n.requestId,f),f;const u=f.data;for(const g in u)!isNaN(u[g])&&Number(u[g])!==1/0&&(u[g]=Number(u[g]));const k=l.unix((Y=u[0])==null?void 0:Y.posTime);let M=-1;for(const g of u){const I=l.unix(g.posTime),p={imo:o==null?void 0:o.imo,mmsi:e,lat:Math.round(g.lat/1e4/60*1e5)/1e5,lng:Math.round(g.lon/1e4/60*1e5)/1e5,sog:Math.round(g.sog/10*100)/100,cog:Math.round(g.cog/10*100)/100,hdg:Math.round(g.heading*100)/100,rot:Math.round(g.rot*100)/100,positionTime:I.unix(),utc:I.utc().format(),method:"trajectory",vendor:"myship"},b=Math.floor(I.diff(k,"minute",!0)/(t||1));b!==M&&(M=b,a.push(p))}return a}}y.AISImpl=q,y.HifleetImpl=A,y.MyShipImpl=L,y.MyVesselImpl=E,y.ShipxyImpl=C,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@idm-plugin/vessel",
3
3
  "private": false,
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "description": "idm plugin for vessel",
6
6
  "type": "module",
7
7
  "keywords": [