You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.5 KiB

import { Control, Sensors, Core } from "./capabilities/index.js";
import { deepFreeze } from "../utils/index.js";
const DeviceType = Object.freeze({
HEART_RATE: "heart_rate",
CADENCE: "cadence",
TRAINER: "trainer",
UNKNOWN: "unknown",
});
class Device {
constructor({
id,
name,
type,
core,
sensors,
control,
connected = false,
lastSeen,
} = {}) {
if (!id) throw new Error("Device requires id");
this.id = id;
this.name = name || id;
this.type = type;
this.core = core instanceof Core ? core : core ? new Core(core) : undefined;
this.sensors =
sensors instanceof Sensors
? sensors
: sensors
? new Sensors(sensors)
: undefined;
this.control =
control instanceof Control
? control
: control
? new Control(control)
: undefined;
this.connected = Boolean(connected);
this.lastSeen = lastSeen;
deepFreeze(this);
}
withCore = (partial) =>
new Device(
Object.assign({}, this, {
core: this.core ? this.core.merge(partial) : new Core(partial),
}),
);
withSensors = (partial) =>
new Device(
Object.assign({}, this, {
sensors: this.sensors
? this.sensors.merge(partial)
: new Sensors(partial),
}),
);
withControl = (partial) =>
new Device(
Object.assign({}, this, {
control: this.control
? this.control.merge(partial)
: new Control(partial),
}),
);
}
export { Device, Core };