Ejemplo n.º 1
0
        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}.");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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);
                }
            });
        }
Ejemplo n.º 3
0
 void ZvsMainWindow_OnEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications <LogEntry> .EntityAddedArgs e)
 {
     //Dispatcher.Invoke(() =>
     //{
     //    StatusBarDescriptionTxt.Text = e.AddedEntity.Message;
     //    StatusBarSourceTxt.Text = e.AddedEntity.Source;
     //    StatusBarUrgencyTxt.Text = e.AddedEntity.Level.ToString();
     //});
 }
 void DeviceDetailsWindow_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications <Device> .EntityAddedArgs e)
 {
     Dispatcher.Invoke(new Action(async() =>
     {
         if (e.AddedEntity.Id == DeviceId)
         {
             await LoadDeviceAsync();
         }
     }));
 }
Ejemplo n.º 5
0
 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);
     }
 }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        void TriggerGridUC_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications <DeviceValueTrigger> .EntityAddedArgs e)
        {
            if (_context == null)
            {
                return;
            }

            Dispatcher.Invoke(new Action(async() =>
            {
                await _context.DeviceValueTriggers.ToListAsync();
            }));
        }
Ejemplo n.º 9
0
        void SceneCreator_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications <JavaScriptCommand> .EntityAddedArgs e)
        {
            if (_context == null)
            {
                return;
            }

            Dispatcher.Invoke(new Action(async() =>
            {
                await _context.JavaScriptCommands.ToListAsync();
            }));
        }
Ejemplo n.º 10
0
        void ScheduledTaskCreator_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications <ScheduledTask> .EntityAddedArgs e)
        {
            if (_context == null)
            {
                return;
            }

            Dispatcher.Invoke(new Action(async() =>
            {
                await _context.ScheduledTasks.ToListAsync();
            }));
        }
Ejemplo n.º 11
0
        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;
            });
        }
Ejemplo n.º 12
0
        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 = 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 = 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);
            }
        }
Ejemplo n.º 13
0
        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();
            }));
        }
Ejemplo n.º 14
0
        void DeviceValues_onEntityDeleted(object sender, NotifyEntityChangeContext.ChangeNotifications <DeviceValue> .EntityDeletedArgs e)
        {
            if (_context == null)
            {
                return;
            }

            Dispatcher.Invoke(new Action(async() =>
            {
                foreach (var ent in _context.ChangeTracker.Entries <DeviceValue>())
                {
                    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();
                }
            }));
        }
Ejemplo n.º 16
0
        void ScheduledTaskCreator_onEntityDeleted(object sender, NotifyEntityChangeContext.ChangeNotifications <ScheduledTask> .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 <ScheduledTask>())
                {
                    await ent.ReloadAsync();
                }
            }));
        }
Ejemplo n.º 17
0
        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();
            }));
        }
Ejemplo n.º 18
0
        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);
            });
        }
Ejemplo n.º 19
0
        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();
                }
            }));
        }
        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;
            });
        }
Ejemplo n.º 21
0
        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));
            }
        }
Ejemplo n.º 22
0
        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 AdapterManagerWindow_onEntityAdded(object sender, NotifyEntityChangeContext.ChangeNotifications <Adapter> .EntityAddedArgs e)
 {
     UpdateAdapterList();
 }
Ejemplo n.º 24
0
        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;
                    }
                    }
                }
            }
        }
 void PluginManagerWindow_onEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications <Plugin> .EntityUpdatedArgs e)
 {
     UpdatePluginList();
 }
Ejemplo n.º 26
0
        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();
        }