/// <summary> /// Gets the most recent value from stat /// </summary> /// <param name="name">ENUM of the Stat</param> /// <returns> /// returns the value of the stat (most recent) /// </returns> public double getStat(Stats.StatName name) { double value = 0; dictionary.TryGetValue(name, out value); return(value); }
/// <summary> /// Write data to simulator /// </summary> /// <param name="name">stat name in ENUM</param> /// <param name="value">value of the stat</param> public void setData(Stats.StatName name, double value) { if (dictionary.ContainsKey(name)) { dictionary.Remove(name); } dictionary.Add(name, value); }
/// <summary> /// Returns current stat data as string /// </summary> /// <param name="name">The name.</param> /// <returns></returns> public string getData(Stats.StatName name) { if (name == Stats.StatName.TIME) { var min = (int)(getStat(name) % 60) + string.Empty; if (min.Length != 2) { min = "0" + min; } return((int)(getStat(name) / 60) + ":" + min); } if ((name == Stats.StatName.PROGRAMWATTAGE) || (name == Stats.StatName.WATTAGE)) { return((int)getStat(name) * 4 + string.Empty); } return((int)getStat(name) + string.Empty); }
/// <summary> /// Changes the data on the bike according to algorithm /// </summary> /// <param name="name">Stat ENUM</param> /// <param name="diff">target data</param> private void updateProgramData(Stats.StatName name, double diff) { if (name.Equals(focus)) { if (((getStat(name) <= 0) && (count == -1)) || ((getStat(name) == target) && (count == 0))) { if (count == -1) { count = 1; setData(name, target); } } } switch (name) { case Stats.StatName.HEARTBEAT: var rpm = getStat(Stats.StatName.RPM); var power = getStat(Stats.StatName.WATTAGE) / 400.0; var calcHeart = 60 + (rpm * (0.50 + new Random().NextDouble() / 4.0) + 10 * power * (0.5 + new Random().NextDouble() / 2.0) + new Random().NextDouble() * 10); setData(name, calcHeart); break; case Stats.StatName.DISTANCE: if (count == 0) { setData( Stats.StatName.DISTANCE, getStat(Stats.StatName.DISTANCE) + getStat(Stats.StatName.SPEED) / 3.6 * diff / 100); } else { setData( Stats.StatName.DISTANCE, getStat(Stats.StatName.DISTANCE) + count * (getStat(Stats.StatName.SPEED) / 3.6 * diff) / 100); } break; case Stats.StatName.SPEED: setData(Stats.StatName.SPEED, getStat(Stats.StatName.RPM) / 10 * 3.55); break; case Stats.StatName.RPM: if (getStat(Stats.StatName.RPM) > rpmTarget + 10) { setData( Stats.StatName.RPM, getStat(Stats.StatName.RPM) - 1 * (getStat(Stats.StatName.PROGRAMWATTAGE) / 40)); } else if (getStat(Stats.StatName.RPM) < rpmTarget - 10) { setData( Stats.StatName.RPM, getStat(Stats.StatName.RPM) + 1 * (getStat(Stats.StatName.PROGRAMWATTAGE) / 40)); } else { rpmTarget = 1 / getStat(Stats.StatName.PROGRAMWATTAGE) * (new Random().NextDouble() * 100) * 100; if (rpmTarget > 160) { rpmTarget = new Random().NextDouble() * 150; } } break; case Stats.StatName.WATTAGE: var RPM = getStat(Stats.StatName.RPM); var ProgramWatt = getStat(Stats.StatName.PROGRAMWATTAGE); if (RPM * ProgramWatt / 4 / 100 < 100) { setData(Stats.StatName.WATTAGE, RPM * ProgramWatt / 100); } else { setData(Stats.StatName.WATTAGE, ProgramWatt); } break; case Stats.StatName.ENERGY: var cwt = getStat(Stats.StatName.WATTAGE) * 4; if ((cwt > 0) && (cwt < 30)) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 2 * 4.184); } else if ((cwt > 30) && (cwt < 50)) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 5 * 4.184); } else if ((cwt > 50) && (cwt < 90)) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 7 * 4.184); } else if ((cwt > 90) && (cwt < 100)) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 10 * 4.184); } else if ((cwt > 100) && (cwt < 160)) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 13 * 4.184); } else if ((cwt > 160) && (cwt < 200)) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 16 * 4.184); } else if ((cwt > 200) && (cwt < 270)) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 21 * 4.184); } else if ((cwt > 270) && (cwt < 360)) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 26 * 4.184); } else if (cwt > 360) { setData( Stats.StatName.ENERGY, getStat(Stats.StatName.ENERGY) + updateInterval / 60000 * 30 * 4.184); } break; } }
/// <summary> /// Sends data /// possible values: /// ST : status request /// CD : computer modes countdown /// CM : computer modes countup /// RS : reset /// PD : get disctance data /// PE : get energy data /// PW : get programwattage data /// VS : get speed data /// </summary> /// <param name="data">The data.</param> public override void sendData(string data) { base.sendData(data); switch (data.Substring(0, 2)) { case "ST": // checkPrograms(); receivedData( string.Format( $"{getData(Stats.StatName.HEARTBEAT)}\t" + $"{getData(Stats.StatName.RPM)}\t" + $"{getData(Stats.StatName.SPEED)}\t" + $"{getData(Stats.StatName.DISTANCE)}\t" + $"{getData(Stats.StatName.PROGRAMWATTAGE)}\t" + $"{getData(Stats.StatName.ENERGY)}\t" + $"{getData(Stats.StatName.TIME)}\t" + $"{getData(Stats.StatName.WATTAGE)}")); break; case "CD": if (mode != BikeModes.CD) { if (count != 0) { receivedData("RUN\r"); } else { count = -1; } mode = BikeModes.CD; } break; case "CM": if (mode != BikeModes.CM) { if (count != 0) { receivedData("RUN\r"); } else { count = 1; } mode = BikeModes.CM; } break; case "RS": count = 0; dictionary.Clear(); // focus = null; target = 0.0; receivedData("ACK\r"); mode = BikeModes.NONE; break; case "ES1": // EXERCISE STATE 1 break; case "ID": // SEND ID #CANCELD# // See labels break; case "KI": // RETURN KETTLER ID #CANCELD# // See labels break; case "PD": focus = Stats.StatName.DISTANCE; setTarget(data); receivedData("ACK\r"); break; case "PE": focus = Stats.StatName.ENERGY; setTarget(data); receivedData("ACK\r"); break; case "PW": focus = Stats.StatName.PROGRAMWATTAGE; //this.setTarget(data); var value = 5; int.TryParse(data.Substring(3), out value); setData(Stats.StatName.PROGRAMWATTAGE, value); receivedData("ACK\r"); break; case "RP": // PROGRAM DATA break; case "SP": // SET PERSON break; case "VE": // return edition (Firmware) break; case "VS": receivedData(getData(Stats.StatName.SPEED)); break; } }