public ViewAddressTable(string deviceName, DeviceALDB deviceALDB, Dictionary<string,Device> allDevices) { _aldb = deviceALDB; _deviceName = deviceName; _allDevices = allDevices; InitializeComponent(); }
public void GetALDBForAllDevices() { if (_aldbLibrary.LastSynchronized > DateTime.Now.AddDays(-7)) { return; } _aldbLibrary.LastSynchronized = DateTime.Now; foreach (string key in _allDevices.Keys) { // the PLM will not respond normally to this command, there are special commands for it // so do not get it in this fashion, also battery devices are asleep so ignore them if ((_allDevices[key] is PLMDevice)|| (_allDevices[key] is SensorDevice)) continue; DeviceALDB targetALDB = _aldbLibrary.Devices.FirstOrDefault(a => a.DeviceAddress == key); if (null != targetALDB && targetALDB.Delta == _allDevices[key].Delta && targetALDB.Delta != 0x00 && (targetALDB.ALDBRecords != null && targetALDB.ALDBRecords.Count > 0)) { targetALDB.Name = _allDevices[key].Name; log.DebugFormat("Device {0}'s Delta matched the stored delta ({1})", _allDevices[key].Name, _allDevices[key].Delta.ToString("X")); continue; } else if (null != targetALDB) { targetALDB.Name = _allDevices[key].Name; log.DebugFormat("Device {0}'s Delta ({1}) did not match the stored delta ({2})", _allDevices[key].Name, _allDevices[key].Delta.ToString("X"), targetALDB.Delta.ToString("X")); targetALDB.Delta = _allDevices[key].Delta; } else { log.DebugFormat("Device {0} was not in the ALDB Library. Added with Delta {1}", _allDevices[key].Name, _allDevices[key].Delta.ToString("X")); targetALDB = new DeviceALDB(); targetALDB.Name = _allDevices[key].Name; targetALDB.Delta = _allDevices[key].Delta; targetALDB.DeviceAddress = _allDevices[key].AddressString; _aldbLibrary.Devices.Add(targetALDB); } log.Info(string.Format("Getting ALDB records for {0}", _allDevices[key].Name)); // clear out the existing data from the dictionary so it doesn't get duplicated targetALDB.ALDBRecords.Clear(); GetAddressRecords(_allDevices[key].Address); log.Info("Waiting on ALDB Event Handle"); // todo: make a better way of detecting a timeout..as my database sizes grow i keep incrementing // this timeout to prevent quitting before we're done _aldbFinishedForDevice = false; _lastALDBRecordTime = DateTime.Now; while (true) { _aldbEventWaitHandle.WaitOne(5000); if (_aldbFinishedForDevice || DateTime.Now.Subtract(_lastALDBRecordTime).TotalSeconds > 20) { log.DebugFormat("Exiting ALDB Loop. Last Record Found: {0}, Last Successful Record Time: {1}", _aldbFinishedForDevice, _lastALDBRecordTime); break; } } log.Info("Finished Waiting on ALDB entry."); log.Info(string.Format("Found {0} ALDB entries for device: {1}", targetALDB.ALDBRecords.Count, _allDevices[key].Name)); } }
private void ProcessALDBResponse(byte[] message) { try { _lastALDBRecordTime = DateTime.Now; byte deviceAddress1 = message[2]; byte deviceAddress2 = message[3]; byte deviceAddress3 = message[4]; DeviceAddress deviceAddress = new DeviceAddress(deviceAddress1, deviceAddress2, deviceAddress3); Device device = _allDevices[deviceAddress.ToString()]; DeviceALDB deviceALDB = null; // record isnt in out ALDB Library yet if (!_aldbLibrary.Devices.Exists(a => a.DeviceAddress == deviceAddress.ToString())) { deviceALDB = new DeviceALDB(); deviceALDB.DeviceAddress = deviceAddress.ToString(); deviceALDB.Delta = device.Delta; _aldbLibrary.Devices.Add(deviceALDB); } else deviceALDB = _aldbLibrary.Devices.First(a => a.DeviceAddress == deviceAddress.ToString()); // redundant...we should be doing this before we even pull the ALDB //if (device.Delta != device.DeviceALDB.Delta) // log.DebugFormat("Device Delta {0} did not match the stored version {1}", device.Delta.ToString("X"), device.DeviceALDB.Delta.ToString("X")); ALDBRecord record = new ALDBRecord(); record.AddressMSB = message[13]; record.AddressLSB = message[14]; record.Flags = message[16]; record.Group = message[17]; record.Address1 = message[18]; record.Address2 = message[19]; record.Address3 = message[20]; record.LocalData1 = message[21]; record.LocalData2 = message[22]; record.LocalData3 = message[23]; if ((record.Flags == 0x00) && ((record.Address1 == 0x00) && (record.Address2 == 0x00) && (record.Address3 == 0x00))) { _aldbFinishedForDevice = true; log.Info("Reached last address record."); _aldbEventWaitHandle.Set(); return; } deviceALDB.ALDBRecords.Add(record); } catch (Exception ex) { log.ErrorFormat("Error processing ALDB Response"); } }