public static void Main(string[] args) { _Lease = new Lease(); _outputDatum = new InterfaceDatum(); _datumLock = new ReaderWriterLockSlim(); uint ticks = 0; uint pedalRotations = 0; _userName = args[1]; _password = args[2]; if (args[0] == @"Test") { _pollServer = new Timer(SendDataToServer, null, 0, POLL_INTERVAL); Random rnd = new Random(); ushort pwrCount = 0; while (1 == 1) { int delay = 500 + rnd.Next(40); Thread.Sleep(delay); ticks += (uint)delay; SensorPayload p = new SensorPayload(); p.Type = "hrm"; p.Data.Add("bpm", (ushort)(70 + rnd.Next(20))); p.Data.Add("ticks", (ushort)(ticks % 65536)); SensorMessageReceived(null, p); pedalRotations += 1; p = new SensorPayload(); p.Type = "spcd"; p.Data.Add("wheelTicks", (ushort)((uint)(0.6 * ticks) % 65536)); p.Data.Add("wheelRotations", (ushort)(pedalRotations % 65536)); p.Data.Add("pedalTicks", (ushort)(ticks % 65536)); p.Data.Add("pedalRotations", (ushort)(pedalRotations % 65536)); SensorMessageReceived(null, p); p = new SensorPayload(); pwrCount++; p.Type = "power"; p.Data.Add("power", (ushort)(170+rnd.Next(20))); p.Data.Add("powerCount",1); SensorMessageReceived(null, p); } } else { Device UsbStick = new Device(); UsbStick.Initialise(); Sensor hrm = new HRMSensor(UsbStick); Sensor speedCadence = new SpeedCadenceSensor(UsbStick); hrm.SensorMessage += new SensorMessageHandler(SensorMessageReceived); speedCadence.SensorMessage += new SensorMessageHandler(SensorMessageReceived); _pollServer = new Timer(SendDataToServer, null, 2000, POLL_INTERVAL); UsbStick.Start(); } // }
public override void Decode(byte[] payload) { if (payload[6] != _lastCount) { // It's new data SensorPayload p = new SensorPayload(); // Decode the time p.Type = "hrm"; p.Data.Add("ticks",BitConverter.ToUInt16(payload, 4)); p.Data.Add("bpm", payload[7]); OnSensorMessage(p); _lastCount = payload[6]; } }
public override void Decode(byte[] payload) { if (payload[0] == 0x10 && payload[1] != _lastCount) { // It's new data SensorPayload p = new SensorPayload(); // Decode the time p.Type = "pwr"; p.Data.Add("count", payload[1]); p.Data.Add("pwr", BitConverter.ToUInt16(payload, 4)); OnSensorMessage(p); _lastCount = payload[1]; } }
public override void Decode(byte[] payload) { SensorPayload pSpCd = new SensorPayload(); pSpCd.Type = "spcd"; ushort pedalTicks = BitConverter.ToUInt16(payload, 0); ushort pedalRotations = BitConverter.ToUInt16(payload, 2); ushort wheelTicks = BitConverter.ToUInt16(payload, 4); ushort wheelRotations = BitConverter.ToUInt16(payload, 6); pSpCd.Data.Add("pedalTicks", pedalTicks); pSpCd.Data.Add("pedalRotations", pedalRotations); pSpCd.Data.Add("wheelTicks", wheelTicks); pSpCd.Data.Add("wheelRotations", wheelRotations); OnSensorMessage(pSpCd); }
protected void OnSensorMessage(SensorPayload p) { SensorMessageHandler handler = SensorMessage; if (handler != null) handler(this, p); }
private static void SensorMessageReceived(object sender, SensorPayload p) { // what is it? _datumLock.EnterWriteLock(); try { // Set the lease _outputDatum.ls = _Lease.LeaseId; _outputDatum.userId = _Lease.UserId; switch (p.Type) { case "hrm": _outputDatum.h = p.Data["bpm"]; _outputDatum.ht = OverflowSubtract(p.Data["ticks"], _lastBpmTick); _lastBpmTick = p.Data["ticks"]; break; case "spcd": _outputDatum.p = OverflowSubtract(p.Data["pedalRotations"], _lastPedalRotation); _outputDatum.pt = OverflowSubtract(p.Data["pedalTicks"], _lastPedalTick); _outputDatum.w = OverflowSubtract(p.Data["wheelRotations"], _lastWheelRotation); _outputDatum.wt = OverflowSubtract(p.Data["wheelTicks"], _lastWheelTick); _lastPedalRotation = p.Data["pedalRotations"]; _lastPedalTick = p.Data["pedalTicks"]; _lastWheelRotation = p.Data["wheelRotations"]; _lastWheelTick = p.Data["wheelTicks"]; break; case "power": _outputDatum.e = p.Data["power"]; _outputDatum.ec = p.Data["powerCount"]; break; default: break; } } finally { _datumLock.ExitWriteLock(); } }