-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
380 lines (325 loc) · 10.8 KB
/
Copy pathserver.js
File metadata and controls
380 lines (325 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import { Encoder, Profile } from "@garmin/fitsdk";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json({ limit: "5mb" }));
app.use(express.static(path.join(__dirname, "public")));
function toSemicircles(deg) {
return Math.round((deg * 2147483648) / 180);
}
function haversineDistance(lat1, lon1, lat2, lon2) {
const R = 6371000;
const toRad = (d) => (d * Math.PI) / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRad(lat1)) *
Math.cos(toRad(lat2)) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
function offsetPointMeters(point, offsetLatMeters, offsetLonMeters) {
const metersPerDegLat = 111320;
const metersPerDegLon =
111320 * Math.cos((point.lat * Math.PI) / 180);
return {
lat: point.lat + offsetLatMeters / metersPerDegLat,
lng: point.lng + offsetLonMeters / metersPerDegLon
};
}
function buildClosedBasePoints(points) {
if (!points || points.length < 2) return points || [];
const first = points[0];
const last = points[points.length - 1];
const d = haversineDistance(first.lat, first.lng, last.lat, last.lng);
if (d < 5) {
return points;
}
const closed = points.slice();
closed.push({ lat: first.lat, lng: first.lng });
return closed;
}
function computeSamples(allPoints, distances, totalDist, paceSecondsPerKm, hrRestVal, hrMaxVal) {
const totalDistanceKm = totalDist / 1000;
const targetDurationSec = totalDistanceKm * paceSecondsPerKm;
const avgSpeedTarget = totalDist / targetDurationSec;
const baseSpeedFactor = 0.98 + Math.random() * 0.06;
const phase1 = Math.random() * Math.PI * 2;
const phase2 = Math.random() * Math.PI * 2;
const n = allPoints.length;
const instSpeedRaw = new Array(n);
const hrValues = new Array(n);
let currentHr = hrRestVal;
for (let i = 0; i < n; i++) {
const frac = distances[i] / totalDist;
const longWave = 0.04 * Math.sin(frac * Math.PI * 2 + phase1);
const shortWave = 0.02 * Math.sin(frac * Math.PI * 6 + phase2);
const speedRaw =
avgSpeedTarget * baseSpeedFactor * (1 + longWave + shortWave);
instSpeedRaw[i] = speedRaw;
const effort = Math.min(
1,
Math.max(0, speedRaw / (avgSpeedTarget || 1e-6))
);
let intensityBase;
if (frac < 0.1) {
const f = frac / 0.1;
intensityBase = 0.4 + 0.4 * f;
} else if (frac < 0.8) {
const f = (frac - 0.1) / 0.7;
intensityBase = 0.8 + 0.05 * Math.sin(f * Math.PI * 2);
} else {
const f = (frac - 0.8) / 0.2;
intensityBase = 0.85 + 0.1 * f;
}
const intensity = Math.min(
1,
Math.max(0, 0.7 * intensityBase + 0.3 * effort)
);
const hrTarget = hrRestVal + (hrMaxVal - hrRestVal) * intensity;
currentHr += (hrTarget - currentHr) * 0.15;
const hrJitter = (Math.random() - 0.5) * 3;
const hrValue = Math.round(
Math.min(hrMaxVal, Math.max(hrRestVal, currentHr + hrJitter))
);
hrValues[i] = hrValue;
}
const segDurationsRaw = new Array(Math.max(0, n - 1));
let rawDuration = 0;
for (let i = 1; i < n; i++) {
const ds = distances[i] - distances[i - 1];
const v = instSpeedRaw[i] > 0 ? instSpeedRaw[i] : avgSpeedTarget;
const dt = ds / v;
segDurationsRaw[i - 1] = dt;
rawDuration += dt;
}
const scale = rawDuration > 0 ? targetDurationSec / rawDuration : 1;
const samples = [];
let t = 0;
samples.push({
timeSec: 0,
distance: distances[0],
speed: instSpeedRaw[0] / scale,
heartRate: hrValues[0],
lat: allPoints[0].lat,
lng: allPoints[0].lng
});
for (let i = 1; i < n; i++) {
const dt = segDurationsRaw[i - 1] * scale;
t += dt;
samples.push({
timeSec: t,
distance: distances[i],
speed: instSpeedRaw[i] / scale,
heartRate: hrValues[i],
lat: allPoints[i].lat,
lng: allPoints[i].lng
});
}
const totalDurationSec = samples.length
? samples[samples.length - 1].timeSec
: targetDurationSec;
return { samples, totalDurationSec };
}
app.post("/api/preview", (req, res) => {
try {
const {
startTime,
points,
paceSecondsPerKm,
hrRest,
hrMax,
lapCount
} = req.body || {};
if (!startTime || !points || !Array.isArray(points) || points.length < 2) {
return res.status(400).json({
error: "缺少参数:需要 startTime、至少两个轨迹点 points"
});
}
const startDate = new Date(startTime);
if (Number.isNaN(startDate.getTime())) {
return res.status(400).json({ error: "startTime 格式不正确" });
}
const pace = Number(paceSecondsPerKm) > 0 ? Number(paceSecondsPerKm) : 360;
const hrRestVal = Number.isFinite(Number(hrRest)) ? Number(hrRest) : 60;
const hrMaxVal = Number.isFinite(Number(hrMax)) ? Number(hrMax) : 180;
const lapsRaw = Number(lapCount);
const laps = Number.isFinite(lapsRaw) && lapsRaw > 0 ? Math.floor(lapsRaw) : 1;
const basePoints = buildClosedBasePoints(points);
const allPoints = [];
const usedLaps = laps > 0 ? laps : 1;
for (let lapIndex = 0; lapIndex < usedLaps; lapIndex++) {
for (let i = 0; i < basePoints.length; i++) {
const p = basePoints[i];
allPoints.push(p);
}
}
const distances = [0];
let totalDist = 0;
for (let i = 1; i < allPoints.length; i++) {
const d = haversineDistance(
allPoints[i - 1].lat,
allPoints[i - 1].lng,
allPoints[i].lat,
allPoints[i].lng
);
totalDist += d;
distances.push(totalDist);
}
if (totalDist === 0) {
return res.status(400).json({ error: "轨迹距离为 0,请绘制更长的路线" });
}
const { samples, totalDurationSec } = computeSamples(
allPoints,
distances,
totalDist,
pace,
hrRestVal,
hrMaxVal
);
return res.json({
totalDistanceMeters: totalDist,
totalDurationSec,
samples
});
} catch (e) {
console.error(e);
return res.status(500).json({ error: "生成预览失败" });
}
});
app.post("/api/generate-fit", (req, res) => {
try {
const {
startTime,
points,
paceSecondsPerKm,
hrRest,
hrMax,
lapCount,
variantIndex
} = req.body || {};
if (!startTime || !points || !Array.isArray(points) || points.length < 2) {
return res.status(400).json({
error: "缺少参数:需要 startTime、至少两个轨迹点 points"
});
}
const startDate = new Date(startTime);
if (Number.isNaN(startDate.getTime())) {
return res.status(400).json({ error: "startTime 格式不正确" });
}
const pace = Number(paceSecondsPerKm) > 0 ? Number(paceSecondsPerKm) : 360;
const hrRestVal = Number.isFinite(Number(hrRest)) ? Number(hrRest) : 60;
const hrMaxVal = Number.isFinite(Number(hrMax)) ? Number(hrMax) : 180;
const lapsRaw = Number(lapCount);
const laps = Number.isFinite(lapsRaw) && lapsRaw > 0 ? Math.floor(lapsRaw) : 1;
const variantRaw = Number(variantIndex);
const variant =
Number.isFinite(variantRaw) && variantRaw > 0
? Math.floor(variantRaw)
: 1;
const basePoints = buildClosedBasePoints(points);
const allPoints = [];
const usedLaps = laps > 0 ? laps : 1;
for (let lapIndex = 0; lapIndex < usedLaps; lapIndex++) {
const radiusMeters = 5 + Math.random() * 10;
const angle = Math.random() * Math.PI * 2;
const offsetLatMeters = radiusMeters * Math.cos(angle);
const offsetLonMeters = radiusMeters * Math.sin(angle);
for (let i = 0; i < basePoints.length; i++) {
const p = basePoints[i];
const noisyPoint =
usedLaps === 1
? p
: offsetPointMeters(p, offsetLatMeters, offsetLonMeters);
allPoints.push(noisyPoint);
}
}
const distances = [0];
let totalDist = 0;
for (let i = 1; i < allPoints.length; i++) {
const d = haversineDistance(
allPoints[i - 1].lat,
allPoints[i - 1].lng,
allPoints[i].lat,
allPoints[i].lng
);
totalDist += d;
distances.push(totalDist);
}
if (totalDist === 0) {
return res.status(400).json({ error: "轨迹距离为 0,请绘制更长的路线" });
}
const { samples, totalDurationSec } = computeSamples(
allPoints,
distances,
totalDist,
pace,
hrRestVal,
hrMaxVal
);
const encoder = new Encoder();
encoder.onMesg(Profile.MesgNum.FILE_ID, {
manufacturer: "development",
product: 1,
timeCreated: startDate,
type: "activity"
});
encoder.onMesg(Profile.MesgNum.DEVICE_INFO, {
timestamp: startDate,
manufacturer: "development",
product: 1,
serialNumber: 1
});
const avgSpeed = totalDist / totalDurationSec;
const sessionEnd = new Date(startDate.getTime() + totalDurationSec * 1000);
encoder.onMesg(Profile.MesgNum.SESSION, {
timestamp: sessionEnd,
startTime: startDate,
totalElapsedTime: totalDurationSec,
totalTimerTime: totalDurationSec,
totalDistance: totalDist,
sport: "running",
subSport: "generic",
avgSpeed
});
encoder.onMesg(Profile.MesgNum.ACTIVITY, {
timestamp: sessionEnd,
totalTimerTime: totalDurationSec,
numSessions: 1,
type: "manual"
});
for (let i = 0; i < samples.length; i++) {
const s = samples[i];
const timestamp = new Date(startDate.getTime() + s.timeSec * 1000);
encoder.onMesg(Profile.MesgNum.RECORD, {
timestamp,
positionLat: toSemicircles(allPoints[i].lat),
positionLong: toSemicircles(allPoints[i].lng),
distance: s.distance,
speed: s.speed,
heartRate: s.heartRate
});
}
const uint8Array = encoder.close();
const buffer = Buffer.from(uint8Array);
res.setHeader("Content-Type", "application/vnd.ant.fit");
res.setHeader(
"Content-Disposition",
`attachment; filename=run_${variant}.fit`
);
return res.send(buffer);
} catch (e) {
console.error(e);
return res.status(500).json({ error: "生成 FIT 文件失败" });
}
});
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});