Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }