private void UpdateStatus() { if (owner == null) { return; } Device = null; if (string.IsNullOrWhiteSpace(SerialNumber)) { Error = null; } else { GaugeDevice availableArduinoGaugeDriver = ArduinoGaugeEnumerator.Singleton.AvailableArduinoGaugeDeviceList.FirstOrDefault(item => item.SerialNumber == SerialNumber); if (availableArduinoGaugeDriver == null) { Error = Translations.Main.ArduinoGaugeDriverNotFoundError; } else { try { Device = new GaugeDriver(availableArduinoGaugeDriver); Error = null; } catch (Exception e) { Error = e.Message; throw; } } } }
public RemoteGaugeDevice(GaugeDevice device) { serialNumber = device.serialNumber.ToString(); sensors = new RemoteGaugeDeviceSensor[device.sensorCount]; for (int i = 0; i < sensors.Length; i++) { sensors[i] = new RemoteGaugeDeviceSensor(device[i]); } connected = device.isConnected ? 1 : 0; battery = device.battery; }
private void InvalidateDevice(GaugeDevice gd) { foreach (var sensor in gd.sensors) { var index = IndexOfSensor(sensor); if (index >= 0) { NotifyItemChanged(index); } } }
private void InvalidateBattery(GaugeDevice device) { if (battery == null) { return; } if (device != null) { var bat = device.battery; if (device.isConnected && lastBattery != bat) { battery.Visibility = ViewStates.Visible; if (bat >= 100) { battery.SetImageBitmap(cache.GetBitmap(Resource.Drawable.ic_battery_horiz_100)); } else if (bat >= 75) { battery.SetImageBitmap(cache.GetBitmap(Resource.Drawable.ic_battery_horiz_75)); } else if (bat >= 50) { battery.SetImageBitmap(cache.GetBitmap(Resource.Drawable.ic_battery_horiz_50)); } else if (bat >= 25) { battery.SetImageBitmap(cache.GetBitmap(Resource.Drawable.ic_battery_horiz_25)); } else { battery.SetImageBitmap(cache.GetBitmap(Resource.Drawable.ic_battery_horiz_empty)); } lastBattery = bat; } else { battery.Visibility = ViewStates.Invisible; lastBattery = -1; } } else { battery.Visibility = ViewStates.Invisible; } }
private void RemoveDevice(GaugeDevice gd) { lock (locker) { if (!knownDevices.Contains(gd)) { return; } knownDevices.Remove(gd); var i = IndexOfDevice(gd); for (int j = 0; j < gd.sensorCount; j++) { sensors.RemoveAt(i); } NotifyItemRangeRemoved(i, gd.sensorCount); CrushContent(); } }
public int IndexOfDevice(GaugeDevice gd) { var ret = 0; for (int i = 0; i < sensors.Count; i++) { if (sensors[i] == null) { continue; } else if (!sensors[i].device.Equals(gd)) { i += sensors[i].device.sensorCount; } else { ret = i; break; } } return(ret); }
/// <summary> /// Inserts the device's sensor into the backing array. /// </summary> /// <returns>The insertion index.</returns> /// <param name="device">Device.</param> private void InsertGaugeDevice(GaugeDevice device) { lock (locker) { if (knownDevices.Contains(device)) { return; } else { knownDevices.Add(device); } var comparer = new GeneralSensorSorter(); var size = device.sensorCount; var index = 0; var k = 0; for (; index < sensors.Count; index += colSize) { if (sensors[index] == null) { // If the sensor is null, then we have a problem, the adapter was not crushed before a new device was added. // Try crushing the content in hopes that we can fix whatever happened. CrushContent(); if (sensors[index] == null) { // The sensor is still null. We have an issue with the algorithm and we need to resolve it. Hopefully this // will happen in test code, otherwise, we will just rape to adapter to prevent any bad things from happening. #if DEBUG throw new Exception("Found null index {" + index + "}: expected an item"); #else Log.E(this, "Unexpected issue with inserting sensor into DeviceGridAdapter: found a null item at first index"); // At this point, the adapter is broken, and we can't really fix it without a shit ton of effort. Just append // the sensor to the end of the list and wait until the user exits activity and comes back. return; #endif } } for (k = 0; k < colSize - device.sensorCount + 1 && index + k < sensors.Count; k++) { if (sensors[index + k] != null) { var dir = comparer.Compare(device.sensors[0], sensors[index + k]); if (dir >= 0) { continue; } // We found the insert index. Let's figure out what needs to happen here. goto __FOUND_INDEX__; } } } __FOUND_INDEX__: if (index + k > sensors.Count) { var oldCnt = sensors.Count; // If our insertion index is past the sensors count, then we can simply add the items to the end of the list // First though, check if we fit on the row. if (device.sensorCount + k > colSize) { // Fill out the row with empties, and then add the device for (int j = 0; j < colSize - k; j++) { sensors.Add(null); } } // Now we can add the gauges. var i = sensors.Count; foreach (var sensor in device.sensors) { sensors.Add(sensor); } NotifyItemRangeInserted(i, sensors.Count - oldCnt); return; } else { // Otherwise, we need to insert the item. if (index + k + device.sensorCount > index + colSize) { // Adding the sensor to the given index will split rows. So, add the sensors to the next row // However, we do need to fill this row with empties for (int i = colSize - 1; i >= k; i--) { sensors.Insert(index + k, null); } index += colSize; k = 0; } // Insert the device into the array. for (int i = device.sensorCount - 1; i >= 0; i--) { sensors.Insert(index + k, device.sensors[i]); } if (FixRows(index)) { NotifyDataSetChanged(); } else { NotifyItemRangeInserted(index + k, device.sensorCount); } } } }
public bool Contains(GaugeDevice gd) { return(knownDevices.Contains(gd)); }
public GaugeDeviceControlDialogFragment(GaugeDevice device) { this.device = device; }