Ejemplo n.º 1
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.º 2
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 = 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);
            }
        }