void DeviceDetailsWindow_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<Device>.EntityAddedArgs e) { Dispatcher.Invoke(new Action(async () => { if (e.AddedEntity.Id == DeviceId) await LoadDeviceAsync(); })); }
private async void ChangeNotificationsOnOnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValue>.EntityUpdatedArgs entityUpdatedArgs) { //TODO: listen to more changes and get device name if (entityUpdatedArgs.NewEntity.Value != entityUpdatedArgs.OldEntity.Value) await Log.ReportInfoFormatAsync(Ct, "Value changed from {0} to {1} on device id {2}", entityUpdatedArgs.OldEntity.Value, entityUpdatedArgs.NewEntity.Value, entityUpdatedArgs.NewEntity.DeviceId); }
void SceneCreator_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<JavaScriptCommand>.EntityAddedArgs e) { if (_context == null) return; Dispatcher.Invoke(new Action(async () => { await _context.JavaScriptCommands.ToListAsync(); })); }
void ScheduledTaskCreator_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<ScheduledTask>.EntityAddedArgs e) { if (_context == null) return; Dispatcher.Invoke(new Action(async () => { await _context.ScheduledTasks.ToListAsync(); })); }
void TriggerGridUC_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValueTrigger>.EntityAddedArgs e) { if (_context == null) return; Dispatcher.Invoke(new Action(async () => { await _context.DeviceValueTriggers.ToListAsync(); })); }
void TriggerGridUC_onEntityDeleted(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValueTrigger>.EntityDeletedArgs e) { if (_context == null) return; Dispatcher.Invoke(new Action(async () => { foreach (var ent in _context.ChangeTracker.Entries<DeviceValueTrigger>()) await ent.ReloadAsync(); })); }
private void GroupEditor_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<Device>.EntityAddedArgs e) { if (Context == null) return; Dispatcher.Invoke(new Action(async () => { foreach (var ent in Context.ChangeTracker.Entries<Device>()) await ent.ReloadAsync(); })); }
void ScheduledTaskCreator_onEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<ScheduledTask>.EntityUpdatedArgs e) { if (_context == null) return; Dispatcher.Invoke(new Action(async () => { //Reloads context from DB when modifications happen foreach (var ent in _context.ChangeTracker.Entries<ScheduledTask>()) await ent.ReloadAsync(); })); }
void DeviceValues_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValue>.EntityAddedArgs e) { if (_context == null || Device == null) return; Dispatcher.Invoke(new Action(async () => { await _context.DeviceValues .Where(o => o.DeviceId == Device.Id) .ToListAsync(); })); }
void SceneCreator_onEntityDeleted(object sender, NotifyEntityChangeContext.ChangeNotifications<JavaScriptCommand>.EntityDeletedArgs e) { if (_context == null) return; Dispatcher.Invoke(new Action(async () => { //Reloads context from DB when modifications happen foreach (var ent in _context.ChangeTracker.Entries<JavaScriptCommand>()) await ent.ReloadAsync(); })); }
void DeviceDataGridUC_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<Group>.EntityAddedArgs e) { if (Context == null) return; if (!MinimalistDisplay) return; Dispatcher.Invoke(new Action(async () => { await Context.Groups.ToListAsync(); })); }
void DeviceDataGridUC_onEntityDeleted(object sender, NotifyEntityChangeContext.ChangeNotifications<Group>.EntityDeletedArgs e) { if (Context == null) return; if (!MinimalistDisplay) return; Dispatcher.Invoke(new Action(async () => { //Reloads context from DB when modifications happen foreach (var ent in Context.ChangeTracker.Entries<Group>()) await ent.ReloadAsync(); })); }
void DeviceDataGridUC_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<Device>.EntityAddedArgs e) { if (Context == null) return; Dispatcher.Invoke(() => { Context.Devices.Local.Add(e.AddedEntity); Context.Entry(e.AddedEntity).State = EntityState.Unchanged; }); }
void DeviceDataGridUC_onEntityDeleted(object sender, NotifyEntityChangeContext.ChangeNotifications<Device>.EntityDeletedArgs e) { if (Context == null) return; Dispatcher.Invoke(new Action(async () => { Context.Devices.Local.Remove(e.DeletedEntity); //Context.Entry(e.DeletedEntity).State = EntityState.Unchanged; //Reloads context from DB when modifications happen foreach (var ent in Context.ChangeTracker.Entries<Device>()) await ent.ReloadAsync(); })); }
private async void Plugin_OnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValue>.EntityUpdatedArgs e) { if (enabled) { try { var newId = e.NewEntity.Id; using (var context = new ZvsContext(EntityContextConnection)) { var dv = await context.DeviceValues .Include(o => o.Device.Type) .FirstOrDefaultAsync(v => v.Id == newId, CancellationToken); if (dv == null) return; var device = (from d in context.Devices where d.Id == dv.DeviceId select d).FirstOrDefault(); if (device != null) { var topic = this.TopicFormat; topic = topic.Replace("{NodeNumber}", device.NodeNumber.ToString()); var message = new DeviceMessage() { Action = "Updated", DeviceId = device.Id, DeviceName = device.Name, NodeNumber = device.NodeNumber, PropertyName = dv.Name, PropertyType = dv.ValueType.ToString(), PropertyValue = dv.Value }; var msg = Newtonsoft.Json.JsonConvert.SerializeObject(message); await Publish(topic, msg); } } } catch (Exception exc) { Log.ReportInfoAsync(exc.Message, CancellationToken); } } else { Log.ReportInfoAsync("MQTT Message was not published, the plugin in turned on, but the service state is not connected.", CancellationToken); } }
void AdapterManagerWindow_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<Adapter>.EntityAddedArgs e) { UpdateAdapterList(); }
async void PluginManager_OnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<PluginSetting>.EntityUpdatedArgs e) { var plugin = FindZvsPlugin(e.NewEntity.PluginId); if (plugin == null) return; await SetPluginProperty(plugin, e.NewEntity.UniqueIdentifier, e.NewEntity.Value, CancellationToken.None); }
void PluginManagerWindow_onEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<Plugin>.EntityUpdatedArgs e) { UpdatePluginList(); }
async void TriggerManager_OnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValue>.EntityUpdatedArgs e) { if (e.NewEntity.Value == e.OldEntity.Value) return; using (var context = new ZvsContext(EntityContextConnection)) { var sendingContext = sender as ZvsContext; if (sendingContext != null && sendingContext.Database.Connection.ConnectionString != context.Database.Connection.ConnectionString) return; //Get triggers for this value var triggers = await context.DeviceValueTriggers .Include(o => o.DeviceValue) .Where(o => o.DeviceValueId == e.NewEntity.Id && o.IsEnabled) .ToListAsync(Ct); foreach (var trigger in triggers) { var changedToValue = e.NewEntity.Value; switch (trigger.Operator) { case TriggerOperator.EqualTo: { Console.WriteLine("--"); Console.WriteLine("Device Value: " + changedToValue); Console.WriteLine("Trigger Value: " + trigger.Value); if (changedToValue.Equals(trigger.Value)) { await ActivateTriggerAsync(trigger.Id, Ct); } break; } case TriggerOperator.GreaterThan: { double deviceValue; double triggerValue; if (double.TryParse(changedToValue, out deviceValue) && double.TryParse(trigger.Value, out triggerValue)) { if (deviceValue > triggerValue) { await ActivateTriggerAsync(trigger.Id, Ct); } } else { await Log.ReportWarningFormatAsync(Ct, "Trigger '{0}' failed to evaluate. Make sure the trigger value and device value is numeric.", trigger.Name); } break; } case TriggerOperator.LessThan: { double deviceValue; double triggerValue; if (double.TryParse(changedToValue, out deviceValue) && double.TryParse(trigger.Value, out triggerValue)) { if (deviceValue < triggerValue) await ActivateTriggerAsync(trigger.Id, Ct); } else { await Log.ReportWarningFormatAsync(Ct, "Trigger '{0}' failed to evaluate. Make sure the trigger value and device value is numeric.", trigger.Name); } break; } case TriggerOperator.NotEqualTo: { if (!changedToValue.Equals(trigger.Value)) { await ActivateTriggerAsync(trigger.Id, Ct); } break; } } } } }
async void SpeechPlugin_OnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValue>.EntityUpdatedArgs e) { using (var context = new ZvsContext(EntityContextConnection)) { var dv = await context.DeviceValues .Include(o => o.Device.Type) .FirstOrDefaultAsync(v => v.Id == e.NewEntity.Id, CancellationToken); if (dv == null) return; var deviceTypeUId = dv.Device.Type.UniqueIdentifier; if (AnnounceOptionSetting == "Switch Level" || AnnounceOptionSetting == "All of the above") { if (deviceTypeUId == "SWITCH" && dv.Name == "Basic") { _synth.SpeakAsync(dv.Device.Name + " switched " + (dv.Value == "255" ? "On" : "Off") + "."); } } if (AnnounceOptionSetting == "Dimmer Level" || AnnounceOptionSetting == "All of the above") { if (deviceTypeUId == "DIMMER" && dv.Name == "Level") { _synth.SpeakAsync(dv.Device.Name + " " + dv.Name + " changed to " + dv.Value + "."); } } if (AnnounceOptionSetting == "Thermostat Operating State and Temp" || AnnounceOptionSetting == "All of the above") { if (deviceTypeUId == "THERMOSTAT" && dv.Name == "Temperature") { _synth.SpeakAsync(dv.Device.Name + " " + dv.Name + " changed to " + dv.Value + "."); } if (deviceTypeUId == "THERMOSTAT" && dv.Name == "Operating State") { _synth.SpeakAsync(dv.Device.Name + " " + dv.Name + " changed to " + dv.Value + "."); } } if (AnnounceOptionSetting != "Custom") return; var objTypeValuespairs = CustomAnnounceSetting.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var objTypeValuespair in objTypeValuespairs) { var thisEvent = $"{deviceTypeUId}:{dv.Name}"; if (thisEvent.Equals(objTypeValuespair.Trim())) _synth.SpeakAsync($"{dv.Device.Name} {dv.Name} changed to {dv.Value}."); } } }
void DeviceDataGridUC_onEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<Device>.EntityUpdatedArgs e) { if (Context == null) return; Dispatcher.Invoke(async () => { //Update the primitives used in this user control var device = Context.Devices.Local.FirstOrDefault(o => o.Id == e.NewEntity.Id); if (device == null) return; device.DeviceTypeId = e.NewEntity.DeviceTypeId; device.CurrentLevelInt = e.NewEntity.CurrentLevelInt; device.CurrentLevelText = e.NewEntity.CurrentLevelText; device.NodeNumber = e.NewEntity.NodeNumber; device.Name = e.NewEntity.Name; device.Location = e.NewEntity.Location; // var entry = context.Entry(e.NewEntity); // entry.State = EntityState.Unchanged; if (e.NewEntity.DeviceTypeId != e.OldEntity.DeviceTypeId) await Context.DeviceTypes.FirstOrDefaultAsync(o => o.Id == e.NewEntity.DeviceTypeId); }); }
async void SpeechPlugin_OnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValue>.EntityUpdatedArgs e) { using (var context = new ZvsContext(EntityContextConnection)) { var dv = await context.DeviceValues .Include(o => o.Device) .Include(o => o.Device.Type) .FirstOrDefaultAsync(v => v.Id == e.NewEntity.Id, CancellationToken); var newValue = e.NewEntity.Value; if (dv == null) return; if (dv.Name != "Basic") return; if (!_zvsTypeToLsType.ContainsKey(dv.Device.Type.UniqueIdentifier)) return; var level = newValue; var type = _zvsTypeToLsType[dv.Device.Type.UniqueIdentifier]; int l; int.TryParse(newValue, out l); if (dv.Device.Type.UniqueIdentifier == "SWITCH") level = (l > 0 ? "255" : "0"); await BroadcastCommandAsync(LightSwitchProtocol.CreateUpdateCmd(dv.Device.Name, dv.Device.Id.ToString(), level, type)); await BroadcastCommandAsync(LightSwitchProtocol.CreateEndListCmd()); await BroadcastCommandAsync(LightSwitchProtocol.CreateMsgCmdFormat("'{0}' {1} changed to {2}", dv.Device.Name, dv.Name, newValue)); } }
private async void ChangeNotificationsOnOnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<AdapterSetting>.EntityUpdatedArgs entityUpdatedArgs) { var adapter = FindZvsAdapter(entityUpdatedArgs.NewEntity.AdapterId); if (adapter == null) return; await SetAdapterProperty(adapter, entityUpdatedArgs.NewEntity.UniqueIdentifier, entityUpdatedArgs.NewEntity.Value, CancellationToken.None); }
void GroupEditorUserControl_OnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<Group>.EntityUpdatedArgs e) { if (Context == null) return; Dispatcher.Invoke(() => { //Update the primitives used in this user control var group = Context.Groups.Local.FirstOrDefault(o => o.Id == e.NewEntity.Id); if (group == null) return; group.Name = e.NewEntity.Name; group.Description = e.NewEntity.Description; }); }
private void ChangeNotificationsOnOnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications<DeviceValue>.EntityUpdatedArgs entityUpdatedArgs) { var bw = new BackgroundWorker(); bw.DoWork += async (s, a) => { try { await Log.ReportInfoFormatAsync(CancellationToken, "{0} working!", Name); using (var context = new ZvsContext(EntityContextConnection)) { var dv = await context.DeviceValues .FirstOrDefaultAsync(v => v.Id == entityUpdatedArgs.NewEntity.Id, CancellationToken); await Log.ReportInfoFormatAsync(CancellationToken, "DeviceValueId : {1}, new:{2}, old:{3}, dv.Value:{4}", (dv != null && dv.Value != null), entityUpdatedArgs.NewEntity.Id, entityUpdatedArgs.NewEntity.Value, entityUpdatedArgs.OldEntity.Value); if (dv == null || dv.Value == null) return; var name = dv.Device.Name; await Log.ReportInfoFormatAsync(CancellationToken, "[{0}] value name: [{1}], Value: [{2}]", Name, name, dv.Value); if (name == Field1) { await Log.ReportInfoFormatAsync(CancellationToken, "Sending {0} to ThingSpeak as Field1, Value={1}", name, dv.Value); short response; var success = ThingSpeakClient.SendDataToThingSpeak(out response, dv.Value); await Log.ReportInfoFormatAsync(CancellationToken, "ThingSpeak results ({0}): success:{1}, response:{2}", name, success, response); } if (name == Field2) { await Log.ReportInfoFormatAsync(CancellationToken, "Sending {0} to ThingSpeak as Field2, Value={1}", name, dv.Value); short response; var success = ThingSpeakClient.SendDataToThingSpeak(out response, null, dv.Value); await Log.ReportInfoFormatAsync(CancellationToken, "ThingSpeak results ({0}): success:{1}, response:{2}", name, success, response); } if (name == Field3) { await Log.ReportInfoFormatAsync(CancellationToken, "Sending {0} to ThingSpeak as Field3, Value={1}", name, dv.Value); short response; var success = ThingSpeakClient.SendDataToThingSpeak(out response, null, null, dv.Value); await Log.ReportInfoFormatAsync(CancellationToken, "ThingSpeak results ({0}): success:{1}, response:{2}", name, success, response); } if (name == Field4) { await Log.ReportInfoFormatAsync(CancellationToken, "Sending {0} to ThingSpeak as Field4, Value={1}", name, dv.Value); short response; var success = ThingSpeakClient.SendDataToThingSpeak(out response, null, null, null, dv.Value); await Log.ReportInfoFormatAsync(CancellationToken, "ThingSpeak results ({0}): success:{1}, response:{2}", name, success, response); } if (name == Field5) { await Log.ReportInfoFormatAsync(CancellationToken, "Sending {0} to ThingSpeak as Field5, Value={1}", name, dv.Value); short response; var success = ThingSpeakClient.SendDataToThingSpeak(out response, null, null, null, null, dv.Value); await Log.ReportInfoFormatAsync(CancellationToken, "ThingSpeak results ({0}): success:{1}, response:{2}", name, success, response); } if (name == Field6) { await Log.ReportInfoFormatAsync(CancellationToken, "Sending {0} to ThingSpeak as Field6, Value={1}", name, dv.Value); short response; var success = ThingSpeakClient.SendDataToThingSpeak(out response, null, null, null, null, null, dv.Value); await Log.ReportInfoFormatAsync(CancellationToken, "ThingSpeak results ({0}): success:{1}, response:{2}", name, success, response); } if (name == Field7) { await Log.ReportInfoFormatAsync(CancellationToken, "Sending {0} to ThingSpeak as Field7, Value={1}", name, dv.Value); short response; var success = ThingSpeakClient.SendDataToThingSpeak(out response, null, null, null, null, null, null, dv.Value); await Log.ReportInfoFormatAsync(CancellationToken, "ThingSpeak results ({0}): success:{1}, response:{2}", name, success, response); } if (name == Field8) { await Log.ReportInfoFormatAsync(CancellationToken, "Sending {0} to ThingSpeak as Field8, Value={1}", name, dv.Value); short response; var success = ThingSpeakClient.SendDataToThingSpeak(out response, null, null, null, null, null, null, null, dv.Value); await Log.ReportInfoFormatAsync(CancellationToken, "ThingSpeak results ({0}): success:{1}, response:{2}", name, success, response); } } } catch (Exception e) { Log.ReportErrorAsync(e.Message, CancellationToken).Wait(); } }; bw.RunWorkerAsync(); }
void LogUserControl_OnEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications<LogEntry>.EntityAddedArgs e) { Dispatcher.Invoke(() => { //Make room for the next entry if (LogEntries.Count >= MaxEntriesToDisplay) { var lastEntry = LogEntries.OrderBy(o => o.Datetime).FirstOrDefault(); if (lastEntry != null) LogEntries.Remove(lastEntry); } LogEntries.Insert(0, e.AddedEntity); }); }