Beispiel #1
0
        public async Task Connect(LightsService lightsService, MQTTServer mQTTServer, SchedulePlanService schedulePlanService)
        {
            _connection = new HubConnectionBuilder()
                          .WithUrl("https://signalirserver20181021093049.azurewebsites.net/LightApp")
                          .WithConsoleLogger(LogLevel.Trace)
                          .Build();

            _connection.On <bool, int>("ChangeLightState", async(lightStatus, lightNumber) =>
            {
                Debug.WriteLine("You turn on light");

                var statusOffLight = lightStatus ? "on" : "off";
                var payLoad        = $"light/{lightNumber}/{statusOffLight}";
                await mQTTServer.PublishMessage(payLoad);
            });

            _connection.On <string>("SendInitialLightCollection", lightsCollection =>
            {
                Debug.WriteLine(lightsCollection);
            });

            _connection.On <string>("SendSchedulePlan", schedulePlanJson =>
            {
                Debug.WriteLine("SendSchedulePlan");
                if (false)
                {
                    schedulePlanService.SmartSwitchingLights(mQTTServer, schedulePlanJson);
                }
            });
            await _connection.StartAsync();
        }
Beispiel #2
0
        public void SmartSwitchingLights(MQTTServer mQTTServer, string schedulePlanJson)
        {
            var delay = TimeSpan.FromSeconds(60);
            var timer = 0;

            periodicTimer = ThreadPoolTimer.CreatePeriodicTimer(
                async(source) =>
            {
                timer++;
                var schedulePlan = JsonConvert.DeserializeObject <List <OneMinuteScheduleDTO> >(schedulePlanJson);
                foreach (var minute in schedulePlan)
                {
                    var payLoad = $"light/{minute.ID}/{minute.Status}";
                    await mQTTServer.PublishMessage(payLoad);
                }
                if (timer == 4)
                {
                    periodicTimer.Cancel();
                }
            }, delay);
        }