private static async void Device_TemperaturaMeasurement(object sender, DeviceMeasurement @event) { Interlocked.Increment(ref _events); Console.WriteLine($"A temperature of {@event.Temperature} has been recorded" + $" {@event.Date:hh:mm:ss}" + $" for the device id : {@event.DeviceId}"); await _buffer.Push(@event); }
private void AcceptMeasurement(string entry) { _timer.Stop(); float value; if (float.TryParse(entry, out value)) { int nextIndex = MeasurementsTaken.Count + 1; var measurement = DeviceMeasurement.CreateMeasurement(nextIndex, value); MeasurementsTaken.Add(measurement); } _timer.Start(); }
public async Task Push(DeviceMeasurement measurement) { _measurements.Add(measurement); if (_measurements.Count >= _bufferSize) { try { // todo : this solution is not safe _semaphore.Wait(); var newCollection = new List <DeviceMeasurement>(_measurements); _measurements.Clear(); // clear the buffer _semaphore.Release(); await SendMessages(newCollection); } finally { _semaphore.Release(); } } }
public VivoAlarmMonitor(Product product, DeviceMeasurement alarmMask0_31, DeviceMeasurement alarmMask32_63, DeviceMeasurement alarmMask64_95, DeviceMeasurement alarmMask96_127, DeviceMeasurement alarmMask128_159) { this.product = product; this.alarms = VivoAlarms.Alarms[product]; this.alarmMeasurements[0] = alarmMask0_31; this.alarmMeasurements[1] = alarmMask32_63; this.alarmMeasurements[2] = alarmMask64_95; this.alarmMeasurements[3] = alarmMask96_127; this.alarmMeasurements[4] = alarmMask128_159; int bitIndex = 0; //we could just hardcode the bitarrays size, but this way //is more maintainable for (int i = 0; i < alarmMeasurements.Length; i++) { bitIndex += alarmMeasurements[i].StorageType.size * 8; } currentBits = new BitArray(bitIndex); }
/// <summary> /// Gets a bulk list of measurement points from the device. /// This is more efficient than calling GetMeasurementValue separately. /// /// The max packet size over USB for Vivo is 30 bytes. The method will split /// up the given list into blocks of 30 bytes each /// </summary> /// <param name="values">The dictionary to fill with the values from the device</param> /// <returns>True if the read was successfull</returns> public bool GetMeasurementPointValues(IDictionary <DeviceMeasurement, int> values) { lock (bcpLock) { DeviceMeasurement[] keys = new DeviceMeasurement[values.Count]; int[] ids = new int[values.Count]; int[] sizes = new int[values.Count]; int idx = 0; int totalSize = 0; foreach (var keyValPair in values) { //Id's here need to be + 1 ids[idx] = MeasurePointDefinitions[keyValPair.Key.Key].NativeId + 1; keys[idx] = keyValPair.Key; sizes[idx] = keyValPair.Key.StorageType.size; totalSize += sizes[idx]; idx++; } //an array of the end index of each block int[] blockIndexes = new int[(int)Math.Ceiling((double)totalSize / MaxPacketDataSize)]; totalSize = 0; idx = 0; //set the last block index's end index to the amount of measurements since //the last index has to end with the amount of measurements blockIndexes[blockIndexes.Length - 1] = sizes.Length; for (int i = 0; i < sizes.Length; i++) { //check if this blocks size is over, or is going over, the max packet size if (totalSize > MaxPacketDataSize || totalSize + sizes[i] > MaxPacketDataSize) { //set this blocks end index blockIndexes[idx++] = i; totalSize = 0; } totalSize += sizes[i]; } int[] results = new int[ids.Length]; int lastBlockIndex = 0; //split the measurements into blocks 15 bytes each for (int block = 0; block < blockIndexes.Length; block++) { int blockEndIndex = blockIndexes[block]; //determine this blocks length int subLength = blockEndIndex - lastBlockIndex; int[] subIds = new int[subLength]; int[] subSizes = new int[subLength]; int[] subResults = new int[subLength]; //not needed? int resultsLen = 0; //fill the sub ids and sizes for (int i = 0; i < subLength; i++) { subIds[i] = ids[lastBlockIndex + i]; subSizes[i] = sizes[lastBlockIndex + i]; } //get the measurement values try { communication.FillMeasurementPointValues(subLength, subIds, subSizes, subResults, out resultsLen); } catch (Exception e) { Log.Error(e.Message, e); } //set the results in the map for (int i = 0; i < subLength; i++) { int index = lastBlockIndex + i; values[keys[index]] = subResults[i]; } lastBlockIndex = blockEndIndex; } return(true); } }