Esempio n. 1
0
 public Node(string hwid, IServiceProvider services, IConfiguration configuration) : base(services)
 {
     this.mqtt          = GetService <mqtt.Service>();
     this.configuration = configuration;
     using (var db = new SensorCloudContext(configuration))
         node = db.nodes.Include(node => node.Room).Include(node => node.sensors).First(n => n.hwid == hwid);
     mqtt.On("(" + node.Room.topic + "/" + node.topic + ")/(.*)", onData);
 }
Esempio n. 2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            mqtt = GetService <mqtt.Service>();
            db   = new SensorCloudContext(configuration);

            foreach (var item in db.dashboardItems.ToList())
            {
                if (item.type == "mqtt")
                {
                    StartMqtt(item);
                }
            }

            await update();
        }
Esempio n. 3
0
        public override async void InstallMqttHandlers(mqtt.Service mqtt)
        {
            this.mqtt = mqtt;
            await mqtt.IsStarted();

            await mqtt.Publish("onkyo/status/title", "", retain : true);

            await mqtt.Publish("onkyo/status/album", "", retain : true);

            await mqtt.Publish("onkyo/status/artist", "", retain : true);


            mqtt.On("onkyo/volume/set", (match, payload) => Volume = int.Parse(payload));
            mqtt.On("onkyo/power/set", (match, payload) => Power   = (payload == "on"));
            mqtt.On("onkyo/action", (match, payload) =>
            {
                if (payload == "next")
                {
                    Log("Skipping to next song");
                    receiver.Next();
                }
            });
            mqtt.On("onkyo/.*", (m, p) => { });
        }
Esempio n. 4
0
 public override void InstallMqttHandlers(mqtt.Service mqtt)
 {
     this.mqtt = mqtt;
 }
Esempio n. 5
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            mqtt = GetService <mqtt.Service>();
            db   = new SensorCloudContext(configuration); //TODO: move to a construction using 'using' keyword

            var ruleManager = GetService <rulemanager.Service>();

            ruleManager.AddFunction(new Function()
            {
                Module       = this.moduleNameFirstCap,
                FunctionName = "Activate",
                Parameters   = new List <Tuple <string, rules.Socket> >()
                {
                    new Tuple <string, rules.Socket>("nodeid", new rules.NumberSocket())
                },
                Callback = this.Activate
            });


            mqtt.On("boot/whoami$", async(match, message) =>
            {
                dynamic data = JObject.Parse(message);
                String hwid  = data.hwid;

                var ret = db.nodes.Where(n => n.hwid == hwid).Select(n => new
                {
                    id        = n.id,
                    hwid      = n.hwid,
                    name      = n.name,
                    topic     = n.topic,
                    room      = n.roomId,
                    config    = JsonConvert.DeserializeObject(n.config),
                    roomtopic = n.Room.topic,
                    sensors   = n.sensors.Select(s => new
                    {
                        id     = s.id,
                        nodeid = s.nodeid,
                        type   = s.type,
                        config = JsonConvert.DeserializeObject(s.config)
                    })
                }).First();
                await mqtt.Publish("boot/whoami/" + hwid, JsonConvert.SerializeObject(ret));
                Log($"Sensor {ret.name}  (id {ret.id}) in {ret.roomtopic} is booting");
                var newNode = new Node(hwid, services, configuration);
                if (nodes.Any(n => n.hwid == hwid))
                {
                    Log($"Sensor already connected before");
                    nodes.RemoveAll(n => n.hwid == hwid);
                }
                nodes.Add(newNode);
            });

            mqtt.On("boot/whoami/(.+)", (match, message) =>
            {
            });

            var watchDog = Task.Run(WatchDog);
            var backlog  = Task.Run(BackLog);

            await Task.WhenAll(new Task[] { watchDog, backlog });
        }