public void SetRace(string code)
        {
            if (!_raceDictionary.ContainsKey(code))
            {
                return;
            }

            AutoUpdating = false;
            _race        = _raceDictionary[code];
            DBError error;

            if (_raceStore != null)
            {
                _raceStore.Close();
            }
            var manager = DBDatastoreManager.Manager(DBAccountManager.SharedManager.LinkedAccount);

            _raceStore = manager.OpenDatastore(_race.DataStoreID, out error);
            _raceStore.Sync(out error);

            UpdateBoatInformation();
            UpdateEventData(true);

            _raceStore.AddObserver(_raceStore, () => {
                UpdateEventData(false);                 // true will curtail the write each time
            });
            AutoUpdating = true;
        }
Beispiel #2
0
        public static NSDictionary ToDictionary(this DropboxRace race)
        {
            var keys = new NSString[] {
                new NSString("Code"),
                new NSString("FullName"),
                new NSString("RaceDate"),
                new NSString("BoatsUpdated"),
                new NSString("DetailsUpdated"),
                new NSString("DatastoreID"),
                new NSString("IntermediateLocations"),
            };
            NSDate d1     = DateTime.SpecifyKind(race.Date, DateTimeKind.Utc);
            NSDate d2     = DateTime.SpecifyKind(race.BoatsUpdated, DateTimeKind.Utc);
            NSDate d3     = DateTime.SpecifyKind(race.DetailsUpdated, DateTimeKind.Utc);
            var    values = new NSObject[] {
                new NSString(race.Code),
                new NSString(race.Name),
                d1, d2, d3,
                new NSString(race.DataStoreID),
                new NSString(race
                             .Locations
                             .Select(l => l.Name)
                             .Aggregate((h, t) => string.Format("{0},{1}", h, t)))
            };

            return(NSDictionary.FromObjectsAndKeys(values, keys));
        }
Beispiel #3
0
        public static DropboxRace Update(this DropboxRace race, DBRecord record)
        {
            race.Code = record.Fields ["Code"].ToString();
            if (record.Fields.ContainsKey(new NSString("IntermediateLocations")))
            {
                var locations           = race.Locations.ToList();
                IList <ILocation> toAdd = new List <ILocation>();
                foreach (var location in
                         record
                         .Fields["IntermediateLocations"]
                         .ToString()
                         .Split(',')
                         .Union(new List <string> {
                    "start", "finish"
                }))
                {
                    if (!locations.Select(l => l.Name).Contains(location))
                    {
                        toAdd.Add(new LocationFactory().SetName(location).Create());
                    }
                }
                if (toAdd.Count > 0)
                {
                    race.Locations = locations.Union(toAdd);
                }
            }
            if (record.Fields.ContainsKey(new NSString("DatastoreID")))
            {
                race.DataStoreID = record.Fields ["DatastoreID"].ToString();
            }
            if (record.Fields.ContainsKey(new NSString("FullName")))
            {
                race.Name = record.Fields ["FullName"].ToString();
            }
            if (record.Fields.ContainsKey(new NSString("RaceDate")))
            {
                var rd  = record.Fields["RaceDate"];
                var nsd = (NSDate)rd;
                var dt  = DateTime.SpecifyKind(nsd, DateTimeKind.Unspecified);
                race.Date = dt;
            }
            if (record.Fields.ContainsKey(new NSString("BoatsUpdated")))
            {
                race.BoatsUpdated = DateTime.SpecifyKind(((NSDate)record.Fields["BoatsUpdated"]), DateTimeKind.Unspecified);
            }
            if (record.Fields.ContainsKey(new NSString("DetailsUpdated")))
            {
                race.DetailsUpdated = DateTime.SpecifyKind(((NSDate)record.Fields["DetailsUpdated"]), DateTimeKind.Unspecified);
            }

            return(race);
        }
        void UpdateRaceInformation(DropboxRace race)
        {
            var     path = DBPath.Root;
            DBError error;
            bool    updated = false;

            foreach (DBFileInfo i in DBFilesystem.SharedFilesystem.ListFolder(path, out error))
            {
                // todo - will need to consider here the dangers of updating the boats during the middle of a race

                // todo - KISS - we will only need the timing app to store boat number - location - token - timestamp
                if (i.Path.Name.EndsWith(race.Code + "-details.json") && i.ModifiedTime > race.DetailsUpdated)
                {
                    Debug.WriteLine("need to update details: " + i.Path.ToString());
                    string json = DBFilesystem.SharedFilesystem.OpenFile(i.Path, out error).ReadString(out error);

                    // todo - if this works, then abstract out the function and do it for the boats
                    try
                    {
                        var details = JsonConvert.DeserializeObject <List <JsonDetails> >(json).FirstOrDefault();
                        if (details != null)
                        {
                            race.Name = details.Name;
                            DateTime date;
                            if (DateTime.TryParse(details.Date, out date))
                            {
                                race.Date = date;
                            }
                            race.Locations =
                                details
                                .IntermediateLocations
                                .Split(',')
                                .Union(new List <string> {
                                "start", "finish"
                            })
                                .Select(l => l.ToLowerInvariant())
                                .Select(n => new LocationFactory().SetName(n).Create());
                            race.DetailsUpdated = DateTime.Now;
                            updated             = true;
                        }
                    } catch (Exception ex)
                    {
                        updated = false;
                    }
                }
            }
            if (updated)
            {
                Update(race);
            }
        }
        void Update(DropboxRace race)
        {
            DBRecord record;
            var      hasRecord = _raceRecords.TryGetValue(race.Code, out record);
            var      fields    = race.ToDictionary();
            var      inserted  = false;
            DBError  error;

            if (hasRecord)
            {
                record.Update(fields);
            }
            else
            {
                _generalStore.GetTable("races").GetOrInsertRecord(race.Code, fields, inserted, out error);
            }

            _generalStore.Sync(out error);
        }
        public void AddRaceCode(string code)
        {
            if (!_raceDictionary.ContainsKey(code))
            {
                AutoUpdating = false;
                var race = new DropboxRace {
                    Code = code
                };
                var     manager = DBDatastoreManager.Manager(DBAccountManager.SharedManager.LinkedAccount);
                DBError error;

                var racestore = manager.CreateDatastore(out error);
                racestore.SetRole("public", DBRole.Editor);
                racestore.SyncAsync();
                race.DataStoreID = racestore.DatastoreId;
                _raceStore       = racestore;
                _raceDictionary.Add(code, race);

                UpdateRaceInformation(race);
                SetRace(code);
                AutoUpdating = true;
            }
            LoadData();
        }
        void UpdateRaceInformation(DropboxRace race)
        {
            var path = DBPath.Root;
            DBError error;
            bool updated = false;

            foreach(DBFileInfo i in DBFilesystem.SharedFilesystem.ListFolder(path, out error))
            {
                // todo - will need to consider here the dangers of updating the boats during the middle of a race

                // todo - KISS - we will only need the timing app to store boat number - location - token - timestamp
                if(i.Path.Name.EndsWith(race.Code + "-details.json") && i.ModifiedTime > race.DetailsUpdated)
                {
                    Debug.WriteLine("need to update details: " + i.Path.ToString());
                    string json = DBFilesystem.SharedFilesystem.OpenFile(i.Path, out error).ReadString(out error);

                    // todo - if this works, then abstract out the function and do it for the boats
                    try
                    {
                        var details = JsonConvert.DeserializeObject<List<JsonDetails>>(json).FirstOrDefault();
                        if(details != null)
                        {
                            race.Name = details.Name;
                            DateTime date;
                            if(DateTime.TryParse(details.Date, out date))
                                race.Date = date;
                            race.Locations =
                                details
                                    .IntermediateLocations
                                    .Split(',')
                                    .Union(new List<string> { "start", "finish"})
                                    .Select(l => l.ToLowerInvariant())
                                    .Select(n => new LocationFactory().SetName(n).Create());
                            race.DetailsUpdated = DateTime.Now;
                            updated = true;
                        }

                    } catch(Exception ex)
                    {
                        updated = false;
                    }

                }
            }
            if(updated)
                Update(race);
        }
        void Update(DropboxRace race)
        {
            DBRecord record;
            var hasRecord = _raceRecords.TryGetValue (race.Code, out record);
            var fields = race.ToDictionary ();
            var inserted = false;
            DBError error;
            if (hasRecord)
                record.Update (fields);
            else
                _generalStore.GetTable("races").GetOrInsertRecord (race.Code, fields, inserted, out error);

            _generalStore.Sync (out error);
        }
        public void SetRace(string code)
        {
            if(!_raceDictionary.ContainsKey(code))
                return;

            AutoUpdating = false;
            _race = _raceDictionary[code];
            DBError error;
            if(_raceStore != null)
            {
                _raceStore.Close();
            }
            var manager = DBDatastoreManager.Manager(DBAccountManager.SharedManager.LinkedAccount);
            _raceStore = manager.OpenDatastore(_race.DataStoreID, out error);
            _raceStore.Sync(out error);

            UpdateBoatInformation();
            UpdateEventData(true);

            _raceStore.AddObserver (_raceStore, () => {
                UpdateEventData(false); // true will curtail the write each time
            });
            AutoUpdating = true;
        }
        public void AddRaceCode(string code)
        {
            if(!_raceDictionary.ContainsKey(code))
            {
                AutoUpdating = false;
                var race = new DropboxRace { Code = code };
                var manager = DBDatastoreManager.Manager(DBAccountManager.SharedManager.LinkedAccount);
                DBError error;

                var racestore = manager.CreateDatastore(out error);
                racestore.SetRole("public", DBRole.Editor);
                racestore.SyncAsync();
                race.DataStoreID = racestore.DatastoreId;
                _raceStore = racestore;
                _raceDictionary.Add (code, race);

                UpdateRaceInformation(race);
                SetRace(code);
                AutoUpdating = true;
            }
            LoadData();
        }