void StartButton_Activated(object sender, EventArgs e)
        {
            // handle button state
            if (running)
            {
                if (artnet != null)
                {
                    artnet.Stop();
                }

                running               = false;
                startButton.Title     = "Start";
                interfaceText.Enabled = true;
            }
            else
            {
                running               = true;
                startButton.Title     = "Stop";
                interfaceText.Enabled = false;
            }

            // Artnet
            artnet.Address = IPAddress.Parse(interfaceText.StringValue);
            artnet.Start();
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            var subUni  = 0;
            var running = true;

            Console.WriteLine("ArtDotNet Client");
            var controller = new ArtNetController();

            controller.Address = IPAddress.Loopback;

            controller.DmxPacketReceived += (s, p) =>
            {
                if (p.SubUni != subUni)
                {
                    return;
                }

                Console.Clear();
                Console.WriteLine("ArtNet Universe " + subUni);

                for (int i = 0; i < p.Length; i++)
                {
                    if (i % 24 == 0)
                    {
                        Console.WriteLine();
                    }

                    Console.Write(string.Format("{000:00} ", p.Data[i]));
                }
            };

            controller.Start();

            while (running)
            {
                var key = Console.ReadKey(true);

                if (key.Key == ConsoleKey.UpArrow)
                {
                    subUni++;
                }

                if (key.Key == ConsoleKey.DownArrow)
                {
                    subUni--;
                }

                if (key.Key == ConsoleKey.Escape)
                {
                    running = false;
                }

                Console.Title = ("ArtNet Universe " + subUni);
            }

            controller.Stop();
        }
Exemple #3
0
        public static async Task RunLightControl(Configuration configuration, HueClient hueClient)
        {
            var timeBetweenChecks = new TimeSpan(Math.Max(configuration.TransitionTime.Ticks, configuration.PollingFrequency.Ticks));

            await AsyncUtils.ContinuallyExecuteReportingExceptionsOnlyOnce(async() =>
            {
                var dmxUniverse = configuration.DMXConfiguration.Universe;

                RunningState runningState = RunningState.HueShift;
                using (var controller = new ArtNetController())
                {
                    controller.Address = IPAddress.Parse(configuration.DMXConfiguration.ListeningIPAddress);

                    var groups = await hueClient.GetGroupsAsync();

                    var dmxControlGroupName = "DmxControlGroup";
                    foreach (var item in groups)
                    {
                        if (item.Name == dmxControlGroupName)
                        {
                            await hueClient.DeleteGroupAsync(item.Id);
                            Console.WriteLine("Deleting existing control group: " + item.Id);
                        }
                    }

                    // var groupId = await hueClient.CreateGroupAsync(
                    //     configuration.DMXConfiguration.LightIdsInDmxGroup.Select(x => x.ToString()).ToList(),
                    //     dmxControlGroupName,
                    //     null,
                    //     Q42.HueApi.Models.Groups.GroupType.LightGroup);

                    var stateQueue = new ConcurrentQueue <LightState>();

                    var startingChannel           = configuration.DMXConfiguration.StartingChannel;
                    controller.DmxPacketReceived += (sender, packet) =>
                    {
                        if (packet.SubUniverse != dmxUniverse)
                        {
                            return;
                        }

                        LightState state = new LightState(
                            packet.Data[startingChannel] > 0,
                            packet.Data[startingChannel + 1],
                            packet.Data[startingChannel + 2]);

                        if (state.IsOn)
                        {
                            //Sanity check to prevent runawayqueu
                            const int maxQueueSize = 10000;
                            if (stateQueue.Count < maxQueueSize)
                            {
                                stateQueue.Enqueue(state);
                            }
                        }
                        else
                        {
                            //Console.Write("D");
                        }
                    };

                    controller.Start();

                    var lastApiSentTime           = DateTimeOffset.MinValue;
                    var lastTimeDmxPacketDetected = DateTimeOffset.MinValue;

                    // LightState lastSentLightState = new LightState();
                    Console.WriteLine("_------------------STARTING ");

                    var controlState = new Dictionary <string, LightControlStatus>();

                    var sunriseSunsetState = new AppState();
                    while (true)
                    {
                        switch (runningState)
                        {
                        case RunningState.HueShift:
                            var currentTime = DateTimeOffset.Now;

                            int colorTemperature = GetTargetColorTemperature(currentTime, configuration);

                            await PerformDayNightCycling(hueClient, colorTemperature, currentTime, sunriseSunsetState, configuration);

                            var timeStartedWaiting = DateTimeOffset.Now;
                            do
                            {
                                if (!stateQueue.IsEmpty)
                                {
                                    lastTimeDmxPacketDetected = DateTimeOffset.Now;
                                    runningState = RunningState.DMX;
                                    Console.WriteLine("DMX Mode detected");
                                    break;
                                }
                                else
                                {
                                    Thread.Sleep(configuration.DMXConfiguration.MillisToSleepBetweenQueueChecks);

                                    //await Task.Delay(.0).ConfigureAwait(false);
                                }
                            } while ((DateTimeOffset.Now - timeStartedWaiting) < timeBetweenChecks);

                            break;

                        default:
                            break;
                            // case RunningState.DMX:

                            //     var transitionDuration = configuration.DMXConfiguration.TransitionTime;
                            //     var minWaitDurationBetweenApiCalls = configuration.DMXConfiguration.MinWaitDurationBetweenApiCalls;

                            //     var timeSinceLastSend = DateTimeOffset.Now - lastApiSentTime;

                            //     var timeSinceLastReceivedDMXPacket = DateTimeOffset.Now - lastTimeDmxPacketDetected;

                            //     LightState mostCurrentLightState = new LightState();

                            //     bool haveReceivedNextDMXPacket = false;
                            //     while (timeSinceLastSend < minWaitDurationBetweenApiCalls)
                            //     {
                            //         Thread.Sleep(configuration.DMXConfiguration.MillisToSleepBetweenQueueChecks);

                            //         LightState latestLightState;
                            //         while (stateQueue.TryDequeue(out latestLightState))
                            //         {
                            //             mostCurrentLightState = latestLightState;
                            //             haveReceivedNextDMXPacket = true;
                            //             lastTimeDmxPacketDetected = DateTime.Now;
                            //         }

                            //         timeSinceLastSend = DateTimeOffset.Now - lastApiSentTime;
                            //     }

                            //     if (!haveReceivedNextDMXPacket)
                            //     {
                            //         LightState latestLightState;
                            //         while (!stateQueue.TryDequeue(out latestLightState))
                            //         {
                            //             Thread.Sleep(configuration.DMXConfiguration.MillisToSleepBetweenQueueChecks);

                            //             if ((DateTimeOffset.Now - lastTimeDmxPacketDetected) > configuration.DMXConfiguration.TimeAfterLastDmxPacketToReturnToShifting)
                            //             {
                            //                 runningState = RunningState.HueShift;
                            //                 break;
                            //             }
                            //         }

                            //         mostCurrentLightState = latestLightState;
                            //         haveReceivedNextDMXPacket = true;
                            //         lastTimeDmxPacketDetected = DateTime.Now;
                            //     }

                            //     if (runningState == RunningState.DMX)
                            //     {
                            //         if (!mostCurrentLightState.Equals(lastSentLightState))
                            //         {
                            //             Console.WriteLine();
                            //             var command = new LightCommand();

                            //             if (mostCurrentLightState.Brightness != lastSentLightState.Brightness)
                            //                 command.Brightness = mostCurrentLightState.Brightness;

                            //             //if (latest.ColorTemperature != last.ColorTemperature)
                            //             //    command.ColorTemperature = (int)map(latest.ColorTemperature, 0, 255, (float)ColorTemperature.Blue, (float)ColorTemperature.Red);

                            //             command.TransitionTime = transitionDuration;

                            //             Console.Write($"Setting: {mostCurrentLightState.IsOn} {mostCurrentLightState.Brightness} {mostCurrentLightState.ColorTemperature}");

                            //             var hueResults = await hueClient.SendGroupCommandAsync(command, groupId);

                            //             foreach (var error in hueResults.Errors)
                            //             {
                            //                 Console.WriteLine($"Error: {error}");
                            //             }

                            //             lastApiSentTime = DateTimeOffset.Now;
                            //             lastSentLightState = mostCurrentLightState;
                            //         }
                            //     }

                            //     break;
                        }
                    }
                }
            });
        }