public bool Update() { bool changed = false; DateTime now = DateTime.Now; if (!validData) { bool error = false; int res; // TODO: Error checking res = OmApi.OmGetVersion(deviceId, out firmwareVersion, out hardwareVersion); error |= OmApi.OM_FAILED(res); uint time; res = OmApi.OmGetTime(deviceId, out time); error |= OmApi.OM_FAILED(res); timeDifference = (OmApi.OmDateTimeUnpack(time) - now); uint startTimeValue, stopTimeValue; res = OmApi.OmGetDelays(deviceId, out startTimeValue, out stopTimeValue); error |= OmApi.OM_FAILED(res); startTime = OmApi.OmDateTimeUnpack(startTimeValue); stopTime = OmApi.OmDateTimeUnpack(stopTimeValue); res = OmApi.OmGetSessionId(deviceId, out sessionId); error |= OmApi.OM_FAILED(res); error = false; // Ignore error here as retrying won't help up (log to console) Console.WriteLine("ERROR: Problem fetching data for device: " + deviceId); changed = true; if (!error) { validData = true; } } if (lastBatteryUpdate == DateTime.MinValue || (now - lastBatteryUpdate) > TimeSpan.FromSeconds(60.0f)) { int newBatteryLevel = OmApi.OmGetBatteryLevel(deviceId); lastBatteryUpdate = now; if (newBatteryLevel != batteryLevel) { batteryLevel = newBatteryLevel; changed = true; } } changed |= hasChanged; hasChanged = false; return(changed); }
public static volatile int volatileTemp = 0; // Junk to prevent code elimination public bool SyncTime() { uint newTime = 0; // last received timestamp (packed) int retries = 12; do { DateTime previous = DateTime.Now; DateTime time; Console.WriteLine("TIMESYNC-DEBUG: Waiting for roll-over from: " + previous); while ((time = DateTime.Now).Second == previous.Second) { volatileTemp++; } // Busy spin until the second rollover (up to 1 second) // ...now set the time (always to the nearest second) DateTime setTime = new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second); Console.WriteLine("TIMESYNC-DEBUG: Setting time: " + setTime); if (OmApi.OM_FAILED(OmApi.OmSetTime((int)deviceId, OmApi.OmDateTimePack(setTime)))) { Console.WriteLine("TIMESYNC: Failed to write time."); continue; } // Verify that the clock was set as expected if (OmApi.OM_FAILED(OmApi.OmGetTime((int)deviceId, out newTime))) { Console.WriteLine("TIMESYNC: Failed to read time."); continue; } DateTime newDateTime = OmApi.OmDateTimeUnpack(newTime); timeDifference = newDateTime - setTime; Console.WriteLine("TIMESYNC-DEBUG: Received time: " + newDateTime + ", delta: " + timeDifference.TotalMilliseconds + " ms"); if (Math.Abs(timeDifference.TotalMilliseconds) > 3000) { Console.WriteLine("TIMESYNC: Time was not within range: " + (int)timeDifference.TotalSeconds); continue; } Console.WriteLine("TIMESYNC-DEBUG: Set time ok, checking for ticks..."); break; // everything ok } while (--retries > 0); if (retries <= 0) { Console.WriteLine("TIMESYNC: Failed to set time successfully"); return(false); } hasChanged = true; om.OnChanged(new OmDeviceEventArgs(this)); // Verify that the clock is ticking DateTime checkStart = DateTime.Now; for (; ;) { if (OmApi.OM_FAILED(OmApi.OmGetTime((int)deviceId, out uint currentTime))) { Console.WriteLine("TIMESYNC: Failed to read time while checking change."); return(false); // Failed to read time } if (currentTime > newTime && ((DateTime.Now - OmApi.OmDateTimeUnpack(currentTime)).TotalMilliseconds < 5000)) { break; // The clock is ticking and within a few seconds of now } var td = DateTime.Now - checkStart; if (Math.Abs(td.TotalMilliseconds) > 4000) { Console.WriteLine("TIMESYNC: Time is not ticking on device after " + (int)td.TotalSeconds + " sec."); return(false); // The clock is not ticking } } return(true); }
public bool Update(int resetIfUnresponsive, bool force = false) { bool changed = false; DateTime now = DateTime.Now; double updateInterval = 30.0; // Usually a 30 second update interval updateInterval = updateInterval + (failedCount * 10.0); updateInterval = Math.Min(updateInterval, 120.0); // At most, 2 minute interval if not communicating if (force || lastUpdate == DateTime.MinValue || (now - lastUpdate) > TimeSpan.FromSeconds(updateInterval)) { int error = 0; //Console.WriteLine("backgroundWorkerUpdate - checking battery for " + this.deviceId + "..."); int newBatteryLevel = OmApi.OmGetBatteryLevel(deviceId); lastUpdate = now; if (OmApi.OM_FAILED(newBatteryLevel)) { error |= 0x10; } // Battery level has changed, or first error reading battery level if (newBatteryLevel != batteryLevel || (error != 0 && failedCount == 0)) { batteryLevel = newBatteryLevel; changed = true; } if (error == 0 && !validData) { //Console.WriteLine("backgroundWorkerUpdate - first check for " + this.deviceId + "..."); int res; // TODO: Error checking res = OmApi.OmGetVersion(deviceId, out firmwareVersion, out hardwareVersion); error |= (OmApi.OM_FAILED(res) ? 0x01 : 0); uint time; res = OmApi.OmGetTime(deviceId, out time); error |= (OmApi.OM_FAILED(res) ? 0x02 : 0); timeDifference = (OmApi.OmDateTimeUnpack(time) - now); uint startTimeValue, stopTimeValue; res = OmApi.OmGetDelays(deviceId, out startTimeValue, out stopTimeValue); error |= (OmApi.OM_FAILED(res) ? 0x04 : 0); startTime = OmApi.OmDateTimeUnpack(startTimeValue); stopTime = OmApi.OmDateTimeUnpack(stopTimeValue); res = OmApi.OmGetSessionId(deviceId, out sessionId); error |= (OmApi.OM_FAILED(res) ? 0x08 : 0); changed = true; if (error == 0) { validData = true; } } if (error != 0) { if (error != 0) { Console.WriteLine("ERROR: Problem fetching data for device: " + deviceId + " (code " + error + ")"); } failedCount++; // Every odd failure, try to reset the device if (resetIfUnresponsive > 0 && (failedCount % resetIfUnresponsive) == 0 && !this.IsDownloading) { Console.WriteLine("NOTE: Resetting device " + deviceId + " (failed " + failedCount + " times)..."); Reset(); } } } changed |= hasChanged; hasChanged = false; return(changed); }
public bool Update(int resetIfUnresponsive, bool force = false) { bool changed = false; DateTime now = DateTime.Now; double updateInterval = 30.0; // Usually a 30 second update interval updateInterval = updateInterval + (failedCount * 10.0); updateInterval = Math.Min(updateInterval, 120.0); // At most, 2 minute interval if not communicating if (force || lastUpdate == DateTime.MinValue || (now - lastUpdate) > TimeSpan.FromSeconds(updateInterval)) { int error = 0; //Console.WriteLine("backgroundWorkerUpdate - checking battery for " + this.deviceId + "..."); int newBatteryLevel = OmApi.OmGetBatteryLevel((int)deviceId); lastUpdate = now; if (OmApi.OM_FAILED(newBatteryLevel)) { error |= 0x10; } // Battery level has changed, or first error reading battery level if (newBatteryLevel != batteryLevel || (error != 0 && failedCount == 0)) { batteryLevel = newBatteryLevel; changed = true; } if (error == 0 && !validData) { //Console.WriteLine("backgroundWorkerUpdate - first check for " + this.deviceId + "..."); int res; // TODO: Error checking res = OmApi.OmGetVersion((int)deviceId, out firmwareVersion, out hardwareVersion); error |= (OmApi.OM_FAILED(res) ? 0x01 : 0); uint time; res = OmApi.OmGetTime((int)deviceId, out time); error |= (OmApi.OM_FAILED(res) ? 0x02 : 0); DateTime deviceTime = OmApi.OmDateTimeUnpack(time); timeDifference = (deviceTime - now); // DateTime deviceTime = DateTime.Now + omDevice.TimeDifference; // Caution the user if the battery was allowed to reset // (the RTC clock became reset) DateTime cautionDate = new DateTime(2008, 1, 1, 0, 0, 0); if (deviceWarning < 1 && deviceTime < cautionDate) { deviceWarning = 1; // Completely flattening battery may damage it changed = true; } // Warn the user of likely damaged device if RTC reset less than // 15 minutes ago, yet the device reports >= 70% charge DateTime warningDate = new DateTime(2000, 1, 1, 0, 15, 0); int warningPercent = 70; if (deviceWarning < 2 && deviceTime < warningDate && batteryLevel >= warningPercent) { deviceWarning = 2; // Device battery or RTC may be damaged changed = true; } uint startTimeValue, stopTimeValue; res = OmApi.OmGetDelays((int)deviceId, out startTimeValue, out stopTimeValue); error |= (OmApi.OM_FAILED(res) ? 0x04 : 0); startTime = OmApi.OmDateTimeUnpack(startTimeValue); stopTime = OmApi.OmDateTimeUnpack(stopTimeValue); res = OmApi.OmGetSessionId((int)deviceId, out sessionId); error |= (OmApi.OM_FAILED(res) ? 0x08 : 0); changed = true; if (error == 0) { validData = true; } } if (error != 0) { if (error != 0) { Console.WriteLine("ERROR: Problem fetching data for device: " + deviceId + " (code " + error + ")"); } failedCount++; // Every odd failure, try to reset the device if (resetIfUnresponsive > 0 && (failedCount % resetIfUnresponsive) == 0 && !this.IsDownloading) { Console.WriteLine("NOTE: Resetting device " + deviceId + " (failed " + failedCount + " times)..."); Reset(); } } } changed |= hasChanged; hasChanged = false; return(changed); }