Skip to content

Commit

Permalink
v1.4.0-beta.51
Browse files Browse the repository at this point in the history
- Fixes concurrency issue when loading modules causing duplicates: might fix #454
  • Loading branch information
genemars committed Sep 20, 2023
1 parent 99880d8 commit d2d680d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 59 deletions.
2 changes: 1 addition & 1 deletion assets/build/all/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@

</div>

<script src="runtime.b92e2ca178ebbf4a.js" type="module"></script><script src="polyfills.a360d6e45b80c065.js" type="module"></script><script src="scripts.0dcb4f0a61221c18.js" defer></script><script src="main.f7ab6393eaa70953.js" type="module"></script>
<script src="runtime.b92e2ca178ebbf4a.js" type="module"></script><script src="polyfills.a360d6e45b80c065.js" type="module"></script><script src="scripts.0dcb4f0a61221c18.js" defer></script><script src="main.4a684fa4cd2d53a0.js" type="module"></script>

</body></html>

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions assets/build/all/app/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1695218756180,
"timestamp": 1695234020211,
"index": "/app/index.html",
"assetGroups": [
{
Expand All @@ -15,7 +15,7 @@
"/app/613.16b9c52c0ae74833.js",
"/app/859.0f150be9c99a6b5a.js",
"/app/index.html",
"/app/main.f7ab6393eaa70953.js",
"/app/main.4a684fa4cd2d53a0.js",
"/app/manifest.webmanifest",
"/app/polyfills.a360d6e45b80c065.js",
"/app/runtime.b92e2ca178ebbf4a.js",
Expand Down Expand Up @@ -729,8 +729,8 @@
"/app/assets/widgets/weather/images_alt/thunder.svg": "2ace7038dfd663330cecc350d680ef08206d62ff",
"/app/assets/widgets/weather/images_alt/weather.svg": "c719c09048977fd40688272316b35ddf1bdf89e8",
"/app/assets/workers/typings-worker.js": "8df10d867c12d36cdebfb7e16caacd0020db301f",
"/app/index.html": "9bfc822bbe4c1ca81c8c731dbff2f5cf586fca2e",
"/app/main.f7ab6393eaa70953.js": "6e30395d9a1d8db7ca061c611130244d3a5ff6ca",
"/app/index.html": "2c349c0961352dc1f8ceda176b38be8550cf0dcf",
"/app/main.4a684fa4cd2d53a0.js": "80705373200803194b9bdae46f51817d268a1c38",
"/app/manifest.webmanifest": "d47bb07a495e27ddf072f8702df373f79b4eb183",
"/app/polyfills.a360d6e45b80c065.js": "f317e1a3e4bb6a84068c7e1845a2ce8b2f956ee7",
"/app/runtime.b92e2ca178ebbf4a.js": "b50d841a4c50d466c34aab3267d3fb4afe87dede",
Expand Down
134 changes: 81 additions & 53 deletions src/HomeGenie/Service/HomeGenieService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ internal void modules_RefreshVirtualModules()
internal void modules_RefreshPrograms()
{
// Refresh ProgramEngine program modules
lock (systemModules.LockObject)
if (masterControlProgram != null)
{
var modules = new List<Module>(systemModules);
Expand All @@ -1117,8 +1118,7 @@ internal void modules_RefreshPrograms()
// only removes program's module if the program kind is "scenario",
// while keep it for other types in order to hold program's configuration
// that is stored as module's parameters
lock (systemModules.LockObject)
systemModules.Remove(module);
systemModules.Remove(module);
continue;
}
if (module == null && !program.IsEnabled)
Expand All @@ -1143,8 +1143,7 @@ internal void modules_RefreshPrograms()
"homegenie/generic/program"
);
}
lock (systemModules.LockObject)
systemModules.Add(module);
systemModules.Add(module);
}
module.Address = program.Address.ToString();
module.DeviceType = ModuleTypes.Program;
Expand Down Expand Up @@ -1230,7 +1229,11 @@ internal void modules_Sort()

internal void modules_RefreshAll()
{
systemModules.RemoveAll(m => m == null); // <-- dunno why but sometimes it happen to have null entries causing exceptions
lock (systemModules.LockObject)
{
systemModules.RemoveAll(m =>
m == null); // <-- dunno why but sometimes it happen to have null entries causing exceptions
}

// Refresh all MIG modules
foreach (var iface in migService.Interfaces)
Expand All @@ -1250,67 +1253,70 @@ internal void modules_RefreshAll()

private void modules_RefreshInterface(MigInterface iface)
{
if (migService.Configuration.GetInterface(iface.GetDomain()).IsEnabled)
lock (systemModules.LockObject)
{
var interfaceModules = iface.GetModules();
// delete removed modules
var deleted = systemModules.FindAll(m => m.Domain == iface.GetDomain() && (interfaceModules.Find(m1 => m1.Address == m.Address && m1.Domain == m.Domain) == null));
foreach (var mod in deleted)
if (migService.Configuration.GetInterface(iface.GetDomain()).IsEnabled)
{
// only "real" modules defined by mig interfaces are considered
var virtualParam = Utility.ModuleParameterGet(mod, Properties.VirtualModuleParentId);
if (virtualParam == null || virtualParam.DecimalValue == 0)
var interfaceModules = iface.GetModules();
// delete removed modules
var deleted = systemModules.FindAll(m => m.Domain == iface.GetDomain() && (interfaceModules.Find(m1 => m1.Address == m.Address && m1.Domain == m.Domain) == null));
foreach (var mod in deleted)
{
Module garbaged = modulesGarbage.Find(m => m.Domain == mod.Domain && m.Address == mod.Address);
if (garbaged != null) modulesGarbage.Remove(garbaged);
modulesGarbage.Add(mod);
systemModules.Remove(mod);
// only "real" modules defined by mig interfaces are considered
var virtualParam = Utility.ModuleParameterGet(mod, Properties.VirtualModuleParentId);
if (virtualParam == null || virtualParam.DecimalValue == 0)
{
Module garbaged = modulesGarbage.Find(m => m.Domain == mod.Domain && m.Address == mod.Address);
if (garbaged != null) modulesGarbage.Remove(garbaged);
modulesGarbage.Add(mod);
systemModules.Remove(mod);
}
}
}
if (interfaceModules.Count > 0)
{
foreach (var migModule in interfaceModules)
if (interfaceModules.Count > 0)
{
Module module = systemModules.Find(o => o.Domain == migModule.Domain && o.Address == migModule.Address);
if (module == null)
foreach (var migModule in interfaceModules)
{
// try restoring from garbage
module = modulesGarbage.Find(o => o.Domain == migModule.Domain && o.Address == migModule.Address);
if (module != null)
Module module = systemModules.Find(o => o.Domain == migModule.Domain && o.Address == migModule.Address);
if (module == null)
{
systemModules.Add(module);
// try restoring from garbage
module = modulesGarbage.Find(o => o.Domain == migModule.Domain && o.Address == migModule.Address);
if (module != null)
{
systemModules.Add(module);
}
else
{
module = new Module();
module.Domain = migModule.Domain;
module.Address = migModule.Address;
systemModules.Add(module);
}
}
else
if (String.IsNullOrEmpty(module.Description))
{
module = new Module();
module.Domain = migModule.Domain;
module.Address = migModule.Address;
systemModules.Add(module);
module.Description = migModule.Description;
}
if (module.DeviceType == ModuleTypes.Generic && migModule.CustomData != null && migModule.CustomData.Type != null)
{
module.DeviceType = migModule.CustomData.Type;
}
}
if (String.IsNullOrEmpty(module.Description))
{
module.Description = migModule.Description;
}
if (module.DeviceType == ModuleTypes.Generic && migModule.CustomData != null && migModule.CustomData.Type != null)
{
module.DeviceType = migModule.CustomData.Type;
}
}
}
}
else
{
var deleted = systemModules.FindAll(m => m.Domain == iface.GetDomain());
foreach (var mod in deleted)
else
{
var virtualParam = Utility.ModuleParameterGet(mod, Properties.VirtualModuleParentId);
if (virtualParam == null || virtualParam.DecimalValue == 0)
var deleted = systemModules.FindAll(m => m.Domain == iface.GetDomain());
foreach (var mod in deleted)
{
Module garbaged = modulesGarbage.Find(m => m.Domain == mod.Domain && m.Address == mod.Address);
if (garbaged != null) modulesGarbage.Remove(garbaged);
modulesGarbage.Add(mod);
systemModules.Remove(mod);
var virtualParam = Utility.ModuleParameterGet(mod, Properties.VirtualModuleParentId);
if (virtualParam == null || virtualParam.DecimalValue == 0)
{
Module garbaged = modulesGarbage.Find(m => m.Domain == mod.Domain && m.Address == mod.Address);
if (garbaged != null) modulesGarbage.Remove(garbaged);
modulesGarbage.Add(mod);
systemModules.Remove(mod);
}
}
}
}
Expand Down Expand Up @@ -1429,8 +1435,30 @@ private void LoadModules()
}
}
modulesGarbage.Clear();
systemModules.Clear();
systemModules = modules;
// prevent duplicates
lock (systemModules.LockObject)
{
systemModules.Clear();
foreach (var m in modules)
{
Module module = systemModules.Find(o => o.Domain == m.Domain && o.Address == m.Address);
if (module == null)
{
systemModules.Add(m);
}
else
{
var warningEvent = new MigEvent(
Domains.HomeAutomation_HomeGenie,
"LoadModules()",
"Ignoring module duplicate",
"SystemMessage.Warning",
m.Domain + ":" + m.Address
);
MigService.Log.Warn(warningEvent);
}
}
}
}
}
catch (Exception ex)
Expand Down

0 comments on commit d2d680d

Please sign in to comment.