/// <remarks>This method is thread-safe.</remarks>
        public static async Task AddControllerAsync(int ID, string orgId, string name, int type, string version, string model, string IPAddress, bool enabled = true, double?geo_latitude = null, double?geo_longitude = null)
        {
            var ctrl = new Controller()
            {
                ID    = ID,
                OrgId = !string.IsNullOrWhiteSpace(orgId) ? orgId.Trim() : throw new ArgumentNullException(nameof(orgId)),
                              IsEnabled                      = enabled,
                              Name                           = !string.IsNullOrWhiteSpace(name) ? name.Trim() : throw new ArgumentNullException(nameof(name)),
                                                        Type = (type >= 0) ? type : throw new ArgumentOutOfRangeException(nameof(type)),
                                                                     Version = !string.IsNullOrWhiteSpace(version) ? version.Trim() : throw new ArgumentNullException(nameof(version)),
                                                                                     Model                       = !string.IsNullOrWhiteSpace(model) ? model.Trim() : throw new ArgumentNullException(nameof(model)),
                                                                                                              IP = ProcessIPAddress(IPAddress) ?? throw new ArgumentOutOfRangeException(nameof(IPAddress)),
                                                                                                                         GeoLatitude  = geo_latitude,
                                                                                                                         GeoLongitude = geo_longitude
            };

            using (var db = new ConfigDB(m_Schema, m_Version)) {
                var cx = await db.Controllers.AsNoTracking().FirstOrDefaultAsync(c => c.ID == ID).ConfigureAwait(false);

                if (cx != null)
                {
                    throw new ApplicationException("ID already exists: " + ID);
                }

                db.Controllers.Add(ctrl);
                await db.SaveChangesAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 2
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <User> DeleteUserAsync(string orgId, string password)
        {
            if (string.IsNullOrWhiteSpace(orgId))
            {
                throw new ArgumentNullException(nameof(orgId));
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException(nameof(password));
            }

            orgId    = orgId.Trim();
            password = password.Trim();

            using (var db = new ConfigDB(m_Schema)) {
                var user = await db.Users
                           .Where(x => x.OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase))
                           .FirstOrDefaultAsync(x => x.Password.Equals(password, StringComparison.OrdinalIgnoreCase))
                           .ConfigureAwait(false);

                if (user == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(password));
                }

                db.Users.Remove(user);
                await db.SaveChangesAsync().ConfigureAwait(false);

                return(user);
            }
        }
Ejemplo n.º 3
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <int> AddMoldAsync(string name, int?controller, bool enabled = true, IReadOnlyDictionary <ushort, ushort> data = null)
        {
            var mold = new Mold()
            {
                IsEnabled = enabled,
                Name      = !string.IsNullOrWhiteSpace(name) ? name.Trim() : throw new ArgumentNullException(nameof(name)),
                                  ControllerId = (!controller.HasValue || controller.Value > 0) ? controller : throw new ArgumentOutOfRangeException(nameof(controller)),
                                                       Guid = Guid.NewGuid()
            };

            using (var db = new ConfigDB(m_Schema)) {
                var cx = await db.Molds.AsNoTracking().FirstOrDefaultAsync(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && x.ControllerId == controller).ConfigureAwait(false);

                if (cx != null)
                {
                    throw new ApplicationException($"Controller/Name already exists: {controller}/{name}");
                }

                db.Molds.Add(mold);

                if (data != null)
                {
                    db.MoldSettings.AddRange(data.Select(s => new MoldSetting()
                    {
                        Mold    = mold,
                        Offset  = (short)s.Key,
                        RawData = s.Value
                    }));
                }

                await db.SaveChangesAsync().ConfigureAwait(false);

                return(mold.ID);
            }
        }
Ejemplo n.º 4
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task ReplaceMoldSettingsAsync(int moldId, IReadOnlyDictionary <ushort, ulong> data)
        {
            if (moldId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(moldId));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            using (var db = new ConfigDB(m_Schema)) {
                var existing = await db.MoldSettings.Where(mx => mx.MoldId == moldId).ToListAsync().ConfigureAwait(false);

                db.MoldSettings.RemoveRange(existing);
                await db.SaveChangesAsync().ConfigureAwait(false);

                foreach (var kv in data)
                {
                    (var value, var variable) = UnpackMoldSettingValue(kv.Value);

                    db.MoldSettings.Add(new MoldSetting()
                    {
                        MoldId   = moldId,
                        Offset   = (short)kv.Key,
                        RawData  = value,
                        Variable = variable
                    });
                }

                await db.SaveChangesAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 5
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <ushort> GetMoldSettingValueAsync(int moldId, int offset)
        {
            using (var db = new ConfigDB(m_Schema)) {
                var setting = await db.MoldSettings.AsNoTracking().FirstOrDefaultAsync(ms => ms.MoldId == moldId && ms.Offset == offset).ConfigureAwait(false);

                return(setting?.RawData ?? 0);
            }
        }
Ejemplo n.º 6
0
        public TextMapCache()
        {
            using (var db = new ConfigDB()) {
                m_IdToText = new ConcurrentDictionary <int, string>(db.TextMaps.AsNoTracking().AsEnumerable().Select(tm => new KeyValuePair <int, string>(tm.ID, tm.Text)));
            }

            m_TextToId = new ConcurrentDictionary <string, int>(m_IdToText.Select(kv => new KeyValuePair <string, int>(kv.Value, kv.Key)), StringComparer.OrdinalIgnoreCase);
        }
Ejemplo n.º 7
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <ICollection <Mold> > GetAllMoldsAsync(int?controller)
        {
            if (controller.HasValue && controller.Value <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(controller));
            }

            using (var db = new ConfigDB(m_Schema)) {
                return(await db.Molds.AsNoTracking().Where(x => x.ControllerId == controller).ToListAsync().ConfigureAwait(false));
            }
        }
        public static async Task <ICollection <Organization> > GetAllOrgsAsync()
        {
            if (m_Version < ConfigDB.Version_Organization)
            {
                return(new Organization[0]);
            }

            using (var db = new ConfigDB(m_Schema, m_Version)) {
                return(await db.Organizations.AsNoTracking().ToListAsync().ConfigureAwait(false));
            }
        }
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <ICollection <Controller> > GetAllControllersAsync(string orgId)
        {
            if (orgId != null && string.IsNullOrWhiteSpace(orgId))
            {
                throw new ArgumentNullException(nameof(orgId));
            }

            using (var db = new ConfigDB(m_Schema, m_Version)) {
                return((orgId == null)
                                        ? await db.Controllers.AsNoTracking().ToListAsync().ConfigureAwait(false)
                                        : await db.Controllers.AsNoTracking().Where(c => c.OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase)).ToListAsync().ConfigureAwait(false));
            }
        }
Ejemplo n.º 10
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <ICollection <Mold> > SearchMoldsByControllerAndKeywordAsync(int controller, string keyword)
        {
            if (controller <= 0)
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(keyword))
            {
                return(null);
            }

            using (var db = new ConfigDB(m_Schema)) {
                return(await db.Molds.AsNoTracking().Where(x => x.ControllerId == controller && x.Name.Contains(keyword)).ToListAsync().ConfigureAwait(false));
            }
        }
Ejemplo n.º 11
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <Mold> GetMoldByControllerAndNameAsync(int controller, string name)
        {
            if (controller <= 0)
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                return(null);
            }

            using (var db = new ConfigDB(m_Schema)) {
                return(await db.Molds.AsNoTracking().FirstOrDefaultAsync(x => x.ControllerId == controller && x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).ConfigureAwait(false));
            }
        }
Ejemplo n.º 12
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <ushort[]> GetMoldSettingsAsync(int moldId)
        {
            IList <MoldSetting> data;

            using (var db = new ConfigDB(m_Schema)) {
                data = await db.MoldSettings.AsNoTracking().Where(s => s.MoldId == moldId).OrderBy(s => s.Offset).ToListAsync().ConfigureAwait(false);
            }

            var list = new ushort[(data.Count <= 0) ? 0 : data[data.Count - 1].Offset + 1];

            foreach (var s in data)
            {
                list[s.Offset] = s.RawData;
            }
            return(list);
        }
        public static async Task <Organization> GetOrgAsync(string orgId)
        {
            if (string.IsNullOrWhiteSpace(orgId))
            {
                throw new ArgumentNullException(nameof(orgId));
            }

            if (m_Version < ConfigDB.Version_Organization)
            {
                return(null);
            }

            using (var db = new ConfigDB(m_Schema, m_Version)) {
                return(await db.Organizations.AsNoTracking().FirstOrDefaultAsync(o => o.ID.Equals(orgId, StringComparison.OrdinalIgnoreCase)).ConfigureAwait(false));
            }
        }
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <Controller> DeleteControllerAsync(int ID)
        {
            using (var db = new ConfigDB(m_Schema, m_Version)) {
                var ctrl = await db.Controllers.FirstOrDefaultAsync(c => c.ID == ID).ConfigureAwait(false);

                if (ctrl == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(ID));
                }

                db.Controllers.Remove(ctrl);
                await db.SaveChangesAsync().ConfigureAwait(false);

                return(ctrl);
            }
        }
Ejemplo n.º 15
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <int> AddMoldAsync(string name, int?controller, bool enabled = true, IList <ulong> data = null)
        {
            var mold = new Mold()
            {
                IsEnabled = enabled,
                Name      = !string.IsNullOrWhiteSpace(name) ? name.Trim() : throw new ArgumentNullException(nameof(name)),
                                  ControllerId = (!controller.HasValue || controller.Value > 0) ? controller : throw new ArgumentOutOfRangeException(nameof(controller)),
                                                       Guid = Guid.NewGuid()
            };

            using (var db = new ConfigDB(m_Schema)) {
                var cx = await db.Molds.AsNoTracking().FirstOrDefaultAsync(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && x.ControllerId == controller).ConfigureAwait(false);

                if (cx != null)
                {
                    throw new ApplicationException($"Controller/Name already exists: {controller}/{name}");
                }

                db.Molds.Add(mold);

                if (data != null)
                {
                    for (var x = 0; x < data.Count; x++)
                    {
                        (var value, var variable) = UnpackMoldSettingValue(data[x]);

                        // Make sure the last item is always stored to keep the accurate length of the whole data set
                        if (value == 0 && x < data.Count - 1)
                        {
                            continue;
                        }

                        db.MoldSettings.Add(new MoldSetting()
                        {
                            Mold     = mold,
                            Offset   = (short)x,
                            RawData  = value,
                            Variable = variable
                        });
                    }
                }

                await db.SaveChangesAsync().ConfigureAwait(false);

                return(mold.ID);
            }
        }
Ejemplo n.º 16
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task UpdateMoldAsync(int ID, bool enabled, string name, int?controller)
        {
            using (var db = new ConfigDB(m_Schema)) {
                var mold = await db.Molds.FirstOrDefaultAsync(m => m.ID == ID).ConfigureAwait(false);

                if (mold == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(ID));
                }

                mold.IsEnabled    = enabled;
                mold.Name         = !string.IsNullOrWhiteSpace(name) ? name.Trim() : throw new ArgumentNullException(nameof(name));
                mold.ControllerId = (!controller.HasValue || controller.Value > 0) ? controller : throw new ArgumentOutOfRangeException(nameof(controller));
                mold.Modified     = DateTime.Now;

                await db.SaveChangesAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 17
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task UpdateUserAsync(string orgId, string password, bool enabled, string name, Filters filters, byte level)
        {
            if (string.IsNullOrWhiteSpace(orgId))
            {
                throw new ArgumentNullException(nameof(orgId));
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException(nameof(password));
            }

            orgId    = orgId.Trim();
            password = password.Trim();

            using (var db = new ConfigDB(m_Schema)) {
                var user = await db.Users
                           .Where(x => x.OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase))
                           .FirstOrDefaultAsync(x => x.Password.Equals(password, StringComparison.OrdinalIgnoreCase))
                           .ConfigureAwait(false);

                if (user == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(password));
                }

                var ux = await db.Users.AsNoTracking()
                         .Where(x => x.OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase))
                         .FirstOrDefaultAsync(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                         .ConfigureAwait(false);

                if (ux != null)
                {
                    throw new ApplicationException($"User name already exists in org {orgId}: {name}");
                }

                user.Name        = !string.IsNullOrWhiteSpace(name) ? name.Trim() : throw new ArgumentNullException(nameof(name));
                user.IsEnabled   = enabled;
                user.Filters     = filters;
                user.AccessLevel = level;
                user.Modified    = DateTime.Now;

                await db.SaveChangesAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 18
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task AddMoldSettingsAsync(int moldId, IReadOnlyDictionary <ushort, ulong> data)
        {
            if (moldId <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(moldId));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            using (var db = new ConfigDB(m_Schema)) {
                var existing = await db.MoldSettings.AsNoTracking().Where(mx => mx.MoldId == moldId).ToListAsync().ConfigureAwait(false);

                if (existing.Any(s => data.ContainsKey((ushort)s.Offset)))
                {
                    throw new ApplicationException($"Some MoldId/Offset already exist.");
                }

                int maxkey = (data.Count > 0) ? data.Keys.Max() : 0;

                foreach (var kv in data)
                {
                    (var value, var variable) = UnpackMoldSettingValue(kv.Value);

                    // Skip zeros but always keep the last item
                    if (value == 0 && kv.Key != maxkey)
                    {
                        continue;
                    }

                    db.MoldSettings.Add(new MoldSetting()
                    {
                        MoldId   = moldId,
                        Offset   = (short)kv.Key,
                        RawData  = value,
                        Variable = variable
                    });
                }

                await db.SaveChangesAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 19
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <Mold> DeleteMoldAsync(int ID)
        {
            using (var db = new ConfigDB(m_Schema)) {
                var mold = await db.Molds.FirstOrDefaultAsync(m => m.ID == ID).ConfigureAwait(false);

                if (mold == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(ID));
                }

                await db.Entry(mold).Collection(m => m.MoldSettings).LoadAsync().ConfigureAwait(false);

                db.MoldSettings.RemoveRange(mold.MoldSettings);
                db.Molds.Remove(mold);

                await db.SaveChangesAsync().ConfigureAwait(false);

                return(mold);
            }
        }
Ejemplo n.º 20
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task <User> GetUserAsync(string orgId, string password)
        {
            if (string.IsNullOrWhiteSpace(orgId))
            {
                throw new ArgumentNullException(nameof(orgId));
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException(nameof(password));
            }

            password = password.Trim();

            using (var db = new ConfigDB(m_Schema)) {
                return(await db.Users.AsNoTracking()
                       .Where(x => x.OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase))
                       .FirstOrDefaultAsync(x => x.Password.Equals(password, StringComparison.OrdinalIgnoreCase))
                       .ConfigureAwait(false));
            }
        }
Ejemplo n.º 21
0
        public async Task <int> GetTextIdAsync(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (m_TextToId.TryGetValue(text, out var id))
            {
                return(id);
            }

            // Not found in cache, add it

            using (var db = new ConfigDB()) {
                var map = await db.TextMaps.SingleOrDefaultAsync(tm => tm.Text.Equals(text, StringComparison.OrdinalIgnoreCase));

                if (map == null)
                {
                    try {
                        db.TextMaps.Add(new TextMap()
                        {
                            Text = text
                        });
                        await db.SaveChangesAsync();
                    } catch (DbUpdateException) {
                        // Probably already added by some other process
                    }
                }

                map = db.TextMaps.Single(tm => tm.Text.Equals(text, StringComparison.OrdinalIgnoreCase));

                m_TextToId.AddOrUpdate(map.Text, map.ID, (x, v) => v);
                m_IdToText.AddOrUpdate(map.ID, map.Text, (x, v) => v);

                return(map.ID);
            }
        }
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task UpdateControllerAsync(int ID, bool enabled, string name, int type, string version, string model, string IPAddress, double?geo_latitude, double?geo_longitude)
        {
            using (var db = new ConfigDB(m_Schema, m_Version)) {
                var ctrl = await db.Controllers.FirstOrDefaultAsync(c => c.ID == ID).ConfigureAwait(false);

                if (ctrl == null)
                {
                    throw new ArgumentOutOfRangeException(nameof(ID));
                }

                ctrl.IsEnabled    = enabled;
                ctrl.Name         = !string.IsNullOrWhiteSpace(name) ? name.Trim() : throw new ArgumentNullException(nameof(name));
                ctrl.Type         = (type >= 0) ? type : throw new ArgumentOutOfRangeException(nameof(type));
                ctrl.Version      = !string.IsNullOrWhiteSpace(version) ? version.Trim() : throw new ArgumentNullException(nameof(version));
                ctrl.Model        = !string.IsNullOrWhiteSpace(model) ? model.Trim() : throw new ArgumentNullException(nameof(model));
                ctrl.IP           = ProcessIPAddress(IPAddress) ?? throw new ArgumentOutOfRangeException(nameof(IPAddress));
                ctrl.GeoLatitude  = geo_latitude;
                ctrl.GeoLongitude = geo_longitude;
                ctrl.Modified     = DateTime.Now;

                await db.SaveChangesAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 23
0
        /// <remarks>This method is thread-safe.</remarks>
        public static async Task AddUserAsync(string orgId, string password, string name, Filters filters, byte level, bool enabled)
        {
            var user = new User()
            {
                OrgId = !string.IsNullOrWhiteSpace(orgId) ? orgId.Trim() : throw new ArgumentNullException(nameof(orgId)),
                              Password = !string.IsNullOrWhiteSpace(password) ? password.Trim() : throw new ArgumentNullException(nameof(password)),
                                               IsEnabled                                    = enabled,
                                               Name                                         = !string.IsNullOrWhiteSpace(name) ? name.Trim() : throw new ArgumentNullException(nameof(name)),
                                                                                Filters     = filters,
                                                                                AccessLevel = level
            };

            using (var db = new ConfigDB(m_Schema)) {
                var ux = await db.Users.AsNoTracking()
                         .Where(x => x.OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase))
                         .FirstOrDefaultAsync(x => x.Password.Equals(password, StringComparison.OrdinalIgnoreCase))
                         .ConfigureAwait(false);

                if (ux != null)
                {
                    throw new ApplicationException($"Password already exists in org {orgId}: {password}");
                }

                ux = await db.Users.AsNoTracking()
                     .Where(x => x.OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase))
                     .FirstOrDefaultAsync(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                     .ConfigureAwait(false);

                if (ux != null)
                {
                    throw new ApplicationException($"User name already exists in org {orgId}: {name}");
                }

                db.Users.Add(user);
                await db.SaveChangesAsync().ConfigureAwait(false);
            }
        }
 /// <remarks>This method is thread-safe.</remarks>
 public static async Task <Controller> GetControllerAsync(int ID)
 {
     using (var db = new ConfigDB(m_Schema, m_Version)) {
         return(await db.Controllers.AsNoTracking().FirstOrDefaultAsync(c => c.ID == ID).ConfigureAwait(false));
     }
 }
Ejemplo n.º 25
0
 /// <remarks>This method is thread-safe.</remarks>
 public static async Task <ICollection <Mold> > GetAllMoldsAsync()
 {
     using (var db = new ConfigDB(m_Schema)) {
         return(await db.Molds.AsNoTracking().ToListAsync().ConfigureAwait(false));
     }
 }
Ejemplo n.º 26
0
 /// <remarks>This method is thread-safe.</remarks>
 public static async Task <Mold> GetMoldAsync(int id)
 {
     using (var db = new ConfigDB(m_Schema)) {
         return(await db.Molds.AsNoTracking().FirstOrDefaultAsync(m => m.ID == id).ConfigureAwait(false));
     }
 }
Ejemplo n.º 27
0
 /// <remarks>This method is thread-safe.</remarks>
 public static async Task <MoldSetting> GetMoldSettingAsync(int moldId, int offset)
 {
     using (var db = new ConfigDB(m_Schema)) {
         return(await db.MoldSettings.AsNoTracking().FirstOrDefaultAsync(ms => ms.MoldId == moldId && ms.Offset == offset).ConfigureAwait(false));
     }
 }
Ejemplo n.º 28
0
 /// <remarks>This method is thread-safe.</remarks>
 public static async Task <ICollection <MoldSetting> > GetMoldSettingsDataAsync(int moldId)
 {
     using (var db = new ConfigDB(m_Schema)) {
         return(await db.MoldSettings.AsNoTracking().Where(s => s.MoldId == moldId).OrderBy(s => s.Offset).ToListAsync().ConfigureAwait(false));
     }
 }