import { Control, Sensors, Core } from "./capabilities"; import { deepFreeze } from "../utils"; const DeviceType = Object.freeze({ HEART_RATE: "heart_rate", CADENCE: "cadence", TRAINER: "trainer", UNKNOWN: "unknown", }); export 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), }), ); }