Exemple #1
0
        public bool Clear(bool wipe)
        {
            bool failed = false;

            // ???
            //failed |= OmApi.OM_FAILED(OmApi.OmCommit(deviceId));

            failed |= OmApi.OM_FAILED(OmApi.OmSetSessionId(deviceId, 0));                                                                                           // Clear the session id
            failed |= OmApi.OM_FAILED(OmApi.OmSetMetadata(deviceId, "", 0));                                                                                        // No metadata
            failed |= OmApi.OM_FAILED(OmApi.OmSetDelays(deviceId, OmApi.OM_DATETIME_INFINITE, OmApi.OM_DATETIME_INFINITE));                                         // Never log
            failed |= OmApi.OM_FAILED(OmApi.OmSetAccelConfig(deviceId, OmApi.OM_ACCEL_DEFAULT_RATE, OmApi.OM_ACCEL_DEFAULT_RANGE));                                 // Default configuration
            failed |= OmApi.OM_FAILED(OmApi.OmEraseDataAndCommit(deviceId, wipe ? OmApi.OM_ERASE_LEVEL.OM_ERASE_WIPE : OmApi.OM_ERASE_LEVEL.OM_ERASE_QUICKFORMAT)); // Erase data and commit
            //failed |= OmApi.OM_FAILED(OmApi.OmClearDataAndCommit(deviceId));                                                   // Clear data and commit

//validData = false;
            if (!failed)
            {
                this.sessionId = 0;
                this.startTime = OmApi.OmDateTimeUnpack(OmApi.OM_DATETIME_INFINITE);
                this.stopTime  = OmApi.OmDateTimeUnpack(OmApi.OM_DATETIME_INFINITE);
            }

            hasChanged = true;
            om.OnChanged(new OmDeviceEventArgs(this));
            return(!failed);
        }
Exemple #2
0
 /** Macro to assert an OmApi value was successful, throws an exception otherwise */
 public static void OmAssert(int result, string message = null)
 {
     if (OmApi.OM_FAILED(result))
     {
         throw new OmException(result, message);
     }
     return;
 }
Exemple #3
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);
        }
Exemple #4
0
 public bool SetLed(OmApi.OM_LED_STATE state)
 {
     ledColor = state;
     if (OmApi.OM_FAILED(OmApi.OmSetLed(deviceId, (int)ledColor)))
     {
         return(false);
     }
     hasChanged = true;
     om.OnChanged(new OmDeviceEventArgs(this));
     return(true);
 }
Exemple #5
0
        public bool SetSessionId(uint sessionId, bool commit)
        {
            bool failed = false;

            failed |= OmApi.OM_FAILED(OmApi.OmSetSessionId(deviceId, sessionId));
            if (commit)
            {
                failed |= OmApi.OM_FAILED(OmApi.OmCommit(deviceId));
            }

//validData = false;
            if (!failed)
            {
                this.sessionId = sessionId;
            }

            hasChanged = true;
            om.OnChanged(new OmDeviceEventArgs(this));
            return(!failed);
        }
Exemple #6
0
        public bool SyncTime()
        {
            DateTime time;
            DateTime previous = DateTime.Now;

            while ((time = DateTime.Now) == previous)
            {
                volatileTemp = 0;
            }                                                                // Spin until the second rollover (up to 1 second)
            // ...now set the time.

            if (OmApi.OM_FAILED(OmApi.OmSetTime(deviceId, OmApi.OmDateTimePack(time))))
            {
                return(false);
            }
            timeDifference = TimeSpan.Zero;
            hasChanged     = true;
            om.OnChanged(new OmDeviceEventArgs(this));
            return(true);
        }
Exemple #7
0
        public bool SetInterval(DateTime start, DateTime stop)
        {
            bool failed = false;

            failed |= OmApi.OM_FAILED(OmApi.OmSetDelays(deviceId, OmApi.OmDateTimePack(start), OmApi.OmDateTimePack(stop)));
            if (!failed)
            {
                failed |= OmApi.OM_FAILED(OmApi.OmCommit(deviceId));
            }

//validData = false;
            if (!failed)
            {
                this.startTime = start;
                this.stopTime  = stop;
            }

            hasChanged = true;
            om.OnChanged(new OmDeviceEventArgs(this));
            return(!failed);
        }
Exemple #8
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);
        }
Exemple #9
0
        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);
        }
Exemple #10
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);
        }