コード例 #1
0
        public static void StartEventListener()
        {
            if (eventCleanLoop == null)
            {
                eventCleanLoop = new Thread(() => {
                    eventCleanLoopThreadRunning = true;
                    while (eventCleanLoopThreadRunning)
                    {
                        Dictionary <string, TouchEvent> outgoingEvents = new Dictionary <string, TouchEvent>();
                        Thread.Sleep(100);
                        lock (lastTouchEvent)
                        {
                            foreach (var last in lastTouchEvent)
                            {
                                var hovering = last.Value.TouchPanelEvents.Where(p => p.Type == ETouch.Hover).ToArray();
                                if (hovering.Length > 0)
                                {
                                    long timestamp = DateTime.Now.Ticks;
                                    if (timestamp - last.Value.Timestamp >= 5000000)
                                    {
                                        outgoingEvents[last.Key] = new TouchEvent(last.Value.TouchedPanelsNumber - hovering.Length, hovering.Select(h => new TouchPanelEvent(h.PanelId, ETouch.Up)).ToArray());
                                    }
                                }
                            }
                        }
                        foreach (var _event in outgoingEvents)
                        {
                            lastTouchEvent[_event.Key] = _event.Value;
                            StaticOnTouchEvent?.Invoke(null, new TouchEventArgs(_event.Key, _event.Value));
                        }
                    }
                });
                eventCleanLoop.Name         = "Nanoleaf Event-Cleaner";
                eventCleanLoop.Priority     = ThreadPriority.Lowest;
                eventCleanLoop.IsBackground = true;
                eventCleanLoop.Start();
            }

            if (eventListenerThread != null)
            {
                return;
            }

            eventListenerThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(async(o) =>
            {
                eventListenerThreadRunning = true;
                try
                {
                    using (var client = new UdpClient(_touchEventsPort))
                    {
                        do
                        {
                            try
                            {
                                UdpReceiveResult result = await client.ReceiveAsync();
                                string ip       = result.RemoteEndPoint.Address.ToString();
                                byte[] datagram = result.Buffer;
                                var touchEvent  = TouchEvent.FromArray(datagram);
                                lock (lastTouchEvent)
                                {
                                    lastTouchEvent[ip] = touchEvent;
                                    StaticOnTouchEvent?.Invoke(null, new TouchEventArgs(ip, touchEvent));
                                }
                            }
                            catch (Exception e)
                            {
                            }
                        } while (eventListenerThreadRunning);
                    }
                }
                catch (Exception e)
                {
                }
            }));
            eventListenerThread.Name         = $"Nanoleaf EventListener";
            eventListenerThread.Priority     = ThreadPriority.BelowNormal;
            eventListenerThread.IsBackground = true;
            eventListenerThread.Start();
        }
コード例 #2
0
 public TouchEventArgs(string ip, TouchEvent touchEvent)
 {
     IP         = ip;
     TouchEvent = touchEvent;
 }