예제 #1
0
        private void SendDeviceStatus(IPEndPoint src)
        {
            var dss     = GetDeviceData();
            var payload = dss.EncodeState();

            DreamSender.SendUdpWrite(0x01, 0x0A, payload, 0x60, group, src);
        }
예제 #2
0
        // This broadcasts the subscribe message that other devices reply to to get color data
        private async void SubscribeBroadcast(CancellationToken ct)
        {
            _subscribers = new Dictionary <string, int>();
            // Loop until canceled
            try {
                while (!ct.IsCancellationRequested)
                {
                    // Send our subscribe multicast
                    DreamSender.SendUdpWrite(0x01, 0x0C, new byte[] { 0x01 }, 0x30, (byte)_dev.GroupNumber, null, true);
                    // Enumerate all subscribers, check to see that they are still valid
                    var keys = new List <string>(_subscribers.Keys);
                    foreach (var key in keys)
                    {
                        // If the subscribers haven't replied in three messages, remove them, otherwise, count down one
                        if (_subscribers[key] <= 0)
                        {
                            _subscribers.Remove(key);
                        }
                        else
                        {
                            _subscribers[key] -= 1;
                        }
                    }

                    // Sleep for 5s
                    await Task.Delay(5000, ct);
                }
            } catch (TaskCanceledException) {
                _subscribers = new Dictionary <string, int>();
            }
        }
예제 #3
0
 private void Subscribe(bool log = false)
 {
     if (log)
     {
         LogUtil.Write(@"Sending subscribe message.");
     }
     DreamSender.SendUdpWrite(0x01, 0x0C, new byte[] { 0x01 }, 0x10, group, targetEndpoint);
 }
예제 #4
0
        public async Task <List <BaseDevice> > FindDevices()
        {
            _searching = true;
            Devices    = new List <BaseDevice>();
            var msg = new byte[] { 0xFC, 0x05, 0xFF, 0x30, 0x01, 0x0A, 0x2A };

            DreamSender.SendUdpBroadcast(msg);
            var s = new Stopwatch();

            s.Start();
            await Task.Delay(3000).ConfigureAwait(true);

            Devices = Devices.Distinct().ToList();
            s.Stop();
            _searching = false;
            DreamData.SetItem("devices", Devices);
            return(Devices);
        }
예제 #5
0
        // If we pass in a third set of sectors, use that info instead.
        public void SendColors(List <Color> colors, List <Color> sectors, List <Color> sectorsV2, Dictionary <string, List <Color> > wledSectors, double fadeTime = 0)
        {
            _sendTokenSource ??= new CancellationTokenSource();
            if (wledSectors == null)
            {
                throw new ArgumentNullException(nameof(wledSectors));
            }
            if (_sendTokenSource.IsCancellationRequested)
            {
                LogUtil.Write("Canceled.");
                return;
            }

            if (!_streamStarted)
            {
                LogUtil.Write("Stream not started.");
                return;
            }

            foreach (var sd in _sDevices)
            {
                sd.SetColor(sectorsV2, fadeTime);
            }

            foreach (var wl in _wlStrips)
            {
                wl.SetColor(wledSectors[wl.Id], fadeTime);
            }

            if (CaptureMode != 0)
            {
                // If we have subscribers and we're capturing
                if (_subscribers.Count > 0 && CaptureMode != 0)
                {
                    var keys = new List <string>(_subscribers.Keys);
                    foreach (var ip in keys)
                    {
                        DreamSender.SendSectors(sectors, ip, _dev.GroupNumber);
                    }
                }
                _strip?.UpdateAll(colors);
                //LogUtil.Write("Colorsend complete");
            }
        }
예제 #6
0
        public static async Task <List <BaseDevice> > Discover()
        {
            LogUtil.Write("Discovery started..");
            // Send a custom internal message to self to store discovery results
            var selfEp = new IPEndPoint(IPAddress.Loopback, 8888);

            DreamSender.SendUdpWrite(0x01, 0x0D, new byte[] { 0x01 }, 0x30, 0x00, selfEp);
            // Send our notification to actually discover
            var msg = new byte[] { 0xFC, 0x05, 0xFF, 0x30, 0x01, 0x0A, 0x2A };

            DreamSender.SendUdpBroadcast(msg);
            await Task.Delay(3000).ConfigureAwait(false);

            DreamSender.SendUdpWrite(0x01, 0x0E, new byte[] { 0x01 }, 0x30, 0x00, selfEp);
            await Task.Delay(500).ConfigureAwait(false);

            var devices = DataUtil.GetDreamDevices();

            return(devices);
        }
예제 #7
0
        private void ProcessData(byte[] receivedBytes, IPEndPoint receivedIpEndPoint)
        {
            // Convert data to ASCII and print in console
            if (!MsgUtils.CheckCrc(receivedBytes))
            {
                return;
            }
            string     command       = null;
            string     flag          = null;
            var        from          = receivedIpEndPoint.Address.ToString();
            var        replyPoint    = new IPEndPoint(receivedIpEndPoint.Address, 8888);
            var        payloadString = string.Empty;
            var        payload       = Array.Empty <byte>();
            BaseDevice msgDevice     = null;
            var        writeState    = false;
            var        msg           = new DreamScreenMessage(receivedBytes, from);

            if (msg.IsValid)
            {
                payload       = msg.GetPayload();
                payloadString = msg.PayloadString;
                command       = msg.Command;
                msgDevice     = msg.Device;
                string[] ignore = { "SUBSCRIBE", "READ_CONNECT_VERSION?", "COLOR_DATA", "DEVICE_DISCOVERY" };
                if (!ignore.Contains(command))
                {
                    Console.WriteLine($@"{from} -> {JsonConvert.SerializeObject(msg)}.");
                }
                flag = msg.Flags;
                var groupMatch = msg.Group == dev.GroupNumber || msg.Group == 255;
                if ((flag == "11" || flag == "21") && groupMatch)
                {
                    dev        = GetDeviceData();
                    writeState = true;
                }
            }

            switch (command)
            {
            case "SUBSCRIBE":
                if (devMode == 1 || devMode == 2)
                {
                    DreamSender.SendUdpWrite(0x01, 0x0C, new byte[] { 0x01 }, 0x10, group, replyPoint);
                }
                break;

            case "COLOR_DATA":
                if (devMode == 1 || devMode == 2)
                {
                    var colorData  = ByteUtils.SplitHex(payloadString, 6);    // Swap this with payload
                    var lightCount = 0;
                    var colors     = new string[12];
                    foreach (var colorValue in colorData)
                    {
                        colors[lightCount] = colorValue;
                        if (lightCount > 11)
                        {
                            break;
                        }
                        lightCount++;
                    }

                    SendColors(colors);
                }

                break;

            case "DEVICE_DISCOVERY":
                if (flag == "30" && from != "0.0.0.0")
                {
                    SendDeviceStatus(replyPoint);
                }
                else if (flag == "60")
                {
                    if (msgDevice != null)
                    {
                        string dsIpCheck = DreamData.GetItem("dsIp");
                        if (dsIpCheck == "0.0.0.0" &&
                            msgDevice.Tag.Contains("DreamScreen", StringComparison.CurrentCulture))
                        {
                            Console.WriteLine(@"No DS IP Set, setting.");
                            DreamData.SetItem("dsIp", from);
                            targetEndpoint = replyPoint;
                        }

                        if (_searching)
                        {
                            Devices.Add(msgDevice);
                        }
                    }
                }

                break;

            case "GROUP_NAME":
                var gName = Encoding.ASCII.GetString(payload);
                if (writeState)
                {
                    dev.GroupName = gName;
                }

                break;

            case "GROUP_NUMBER":
                int gNum = payload[0];
                if (writeState)
                {
                    dev.GroupNumber = gNum;
                }

                break;

            case "NAME":
                var dName = Encoding.ASCII.GetString(payload);
                if (writeState)
                {
                    dev.Name = dName;
                }

                break;

            case "BRIGHTNESS":
                brightness = payload[0];
                if (writeState)
                {
                    Console.WriteLine($@"Setting brightness to {brightness}.");
                    dev.Brightness = payload[0];
                    UpdateBrightness(payload[0]);
                }

                break;

            case "SATURATION":
                if (writeState)
                {
                    dev.Saturation = ByteUtils.ByteString(payload);
                }

                break;

            case "MODE":
                if (writeState)
                {
                    dev.Mode = payload[0];
                    Console.WriteLine($@"Updating mode: {dev.Mode}.");
                    UpdateMode(dev.Mode);
                }

                break;

            case "AMBIENT_MODE_TYPE":
                if (writeState)
                {
                    dev.AmbientModeType = payload[0];
                    UpdateAmbientMode(dev.Mode);
                }

                break;

            case "AMBIENT_SCENE":
                if (writeState)
                {
                    ambientShow         = payload[0];
                    dev.AmbientShowType = ambientShow;
                    UpdateAmbientShow(ambientShow);
                    Console.WriteLine($@"Scene updated: {ambientShow}.");
                }

                break;

            case "AMBIENT_COLOR":
                if (writeState)
                {
                    dev.AmbientColor = ByteUtils.ByteString(payload);
                    UpdateAmbientColor(dev.AmbientColor);
                }

                break;
            }

            if (writeState)
            {
                DreamData.SetItem <BaseDevice>("myDevice", dev);
            }
        }
예제 #8
0
        private void ProcessData(byte[] receivedBytes, IPEndPoint receivedIpEndPoint)
        {
            // Convert data to ASCII and print in console
            if (!MsgUtils.CheckCrc(receivedBytes))
            {
                return;
            }
            string     command       = null;
            string     flag          = null;
            var        from          = receivedIpEndPoint.Address.ToString();
            var        replyPoint    = new IPEndPoint(receivedIpEndPoint.Address, 8888);
            var        payloadString = string.Empty;
            var        payload       = Array.Empty <byte>();
            BaseDevice msgDevice     = null;
            var        writeState    = false;
            var        writeDev      = false;
            var        msg           = new DreamScreenMessage(receivedBytes, from);
            var        tDevice       = _dev;

            if (msg.IsValid)
            {
                payload       = msg.GetPayload();
                payloadString = msg.PayloadString;
                command       = msg.Command;
                msgDevice     = msg.Device;
                flag          = msg.Flags;
                var groupMatch = msg.Group == _dev.GroupNumber || msg.Group == 255;
                if ((flag == "11" || flag == "17" || flag == "21") && groupMatch)
                {
                    writeState = true;
                    writeDev   = true;
                }
                if (flag == "41")
                {
                    LogUtil.Write($"Flag is 41, we should save settings for {from}.");
                    tDevice = DataUtil.GetDreamDevice(from);
                    if (tDevice != null)
                    {
                        writeDev = true;
                    }
                }
                if (from != null && command != null && command != "COLOR_DATA" && command != "SUBSCRIBE" && tDevice != null)
                {
                    LogUtil.Write($@"{from} -> {tDevice.IpAddress}::{command} {flag}-{msg.Group}.");
                }
            }
            else
            {
                LogUtil.Write($@"Invalid message from {from}");
            }
            switch (command)
            {
            case "SUBSCRIBE":

                if (_devMode == 1 || _devMode == 2)
                {
                    DreamSender.SendUdpWrite(0x01, 0x0C, new byte[] { 0x01 }, 0x10, _group, replyPoint);
                }

                // If the device is on and capture mode is not using DS data
                if (_devMode != 0 && CaptureMode != 0)
                {
                    // If the device is replying to our sub broadcast
                    if (flag == "60")
                    {
                        // Set our count to 3, which is how many tries we get before we stop sending data
                        if (!string.IsNullOrEmpty(from))
                        {
                            if (!_subscribers.ContainsKey(from))
                            {
                                LogUtil.Write("Adding new subscriber: " + from);
                            }
                            _subscribers[from] = 3;
                        }
                        else
                        {
                            LogUtil.Write("Can't add subscriber, from is empty...");
                        }
                    }
                }
                break;

            case "DISCOVERY_START":
                LogUtil.Write("DreamScreen: Starting discovery.");
                _devices     = new List <BaseDevice>();
                _discovering = true;
                break;

            case "DISCOVERY_STOP":
                LogUtil.Write($"DreamScreen: Discovery complete, found {_devices.Count} devices.");
                _discovering = false;
                foreach (var d in _devices)
                {
                    DataUtil.InsertCollection <BaseDevice>("devices", d);
                }

                break;

            case "REMOTE_REFRESH":
                var id   = Encoding.UTF8.GetString(payload.ToArray());
                var tDev = _sDevices.FirstOrDefault(b => b.Id == id);
                LogUtil.Write($"Triggering reload of device {id}.");
                tDev?.ReloadData();
                break;

            case "COLOR_DATA":
                if (_devMode == 1 || _devMode == 2)
                {
                    var colorData = ByteUtils.SplitHex(payloadString, 6);     // Swap this with payload
                    var colors    = new List <Color>();
                    foreach (var colorValue in colorData)
                    {
                        colors.Add(ColorFromString(colorValue));
                    }

                    colors = ShiftColors(colors);
                    SendColors(colors, colors);
                }

                break;

            case "DEVICE_DISCOVERY":
                if (flag == "30" && from != "0.0.0.0")
                {
                    SendDeviceStatus(replyPoint);
                }
                else if (flag == "60")
                {
                    if (msgDevice != null)
                    {
                        string dsIpCheck = DataUtil.GetItem("DsIp");
                        if (dsIpCheck == "0.0.0.0" &&
                            msgDevice.Tag.Contains("DreamScreen", StringComparison.CurrentCulture))
                        {
                            LogUtil.Write(@"Setting a target DS IP.");
                            DataUtil.SetItem("DsIp", from);
                            _targetEndpoint = replyPoint;
                        }

                        if (_discovering)
                        {
                            LogUtil.Write("Sending request for serial!");
                            DreamSender.SendUdpWrite(0x01, 0x03, new byte[] { 0 }, 0x60, 0, replyPoint);
                            _devices.Add(msgDevice);
                        }
                    }
                }

                break;

            case "GET_SERIAL":
                if (flag == "30")
                {
                    SendDeviceSerial(replyPoint);
                }
                else
                {
                    LogUtil.Write("DEVICE SERIAL RETRIEVED: " + JsonConvert.SerializeObject(msg));
                }

                break;

            case "GROUP_NAME":
                var gName = Encoding.ASCII.GetString(payload);
                if (writeState | writeDev)
                {
                    tDevice.GroupName = gName;
                }

                break;

            case "GROUP_NUMBER":
                int gNum = payload[0];
                if (writeState | writeDev)
                {
                    tDevice.GroupNumber = gNum;
                }

                break;

            case "NAME":
                var dName = Encoding.ASCII.GetString(payload);
                if (writeState | writeDev)
                {
                    tDevice.Name = dName;
                }

                break;

            case "BRIGHTNESS":
                _brightness = payload[0];
                if (writeState | writeDev)
                {
                    LogUtil.Write($@"Setting brightness to {_brightness}.");
                    tDevice.Brightness = payload[0];
                }
                if (writeState)
                {
                    UpdateBrightness(payload[0]);
                }

                break;

            case "SATURATION":
                if (writeState | writeDev)
                {
                    tDevice.Saturation = ByteUtils.ByteString(payload);
                }

                break;

            case "MODE":
                if (writeState | writeDev)
                {
                    tDevice.Mode = payload[0];
                    LogUtil.Write("UPDATING MODE FROM REMOTE");

                    LogUtil.Write($@"Updating mode: {tDevice.Mode}.");
                }
                else
                {
                    LogUtil.Write("Mode flag set, but we're not doing anything... " + flag);
                }

                if (writeState)
                {
                    UpdateMode(tDevice.Mode);
                }

                break;

            case "REFRESH_CLIENTS":
                LogUtil.Write("Triggering discovery.");
                StartRefreshTimer(true);
                break;

            case "AMBIENT_MODE_TYPE":
                if (writeState | writeDev)
                {
                    tDevice.AmbientModeType = payload[0];
                }

                if (writeState)
                {
                    UpdateAmbientMode(tDevice.AmbientModeType);
                }

                break;

            case "AMBIENT_SCENE":
                if (writeState | writeDev)
                {
                    _ambientShow            = payload[0];
                    tDevice.AmbientShowType = _ambientShow;
                }
                if (writeState)
                {
                    UpdateAmbientShow(_ambientShow);
                }
                break;

            case "AMBIENT_COLOR":
                if (writeDev | writeState)
                {
                    if (tDevice != null)
                    {
                        tDevice.AmbientColor = ByteUtils.ByteString(payload);
                    }
                }
                if (writeState && tDevice != null)
                {
                    UpdateAmbientColor(ColorFromString(tDevice.AmbientColor));
                }

                break;

            case "SKU_SETUP":
                if (writeState | writeDev)
                {
                    tDevice.SkuSetup = payload[0];
                }

                break;

            case "FLEX_SETUP":
                if (writeState | writeDev)
                {
                    int[] fSetup = payload.Select(x => (int)x).ToArray();
                    tDevice.flexSetup = fSetup;
                }

                break;

            case "RESET_PIC":
                break;
            }

            if (writeState)
            {
                DataUtil.SetObject("myDevice", tDevice);
                _dev = tDevice;
                DreamSender.SendUdpWrite(msg.C1, msg.C2, msg.GetPayload(), 0x41, (byte)msg.Group, receivedIpEndPoint);
            }

            if (!writeState && !writeDev)
            {
                return;
            }
            // Notify if the sender was not us
            if (from != _dev.IpAddress)
            {
                NotifyClients();
            }
            DataUtil.InsertDsDevice(tDevice);
        }
예제 #9
0
 private void Subscribe(bool log = false)
 {
     DreamSender.SendUdpWrite(0x01, 0x0C, new byte[] { 0x01 }, 0x10, _group, _targetEndpoint);
 }
예제 #10
0
        private void SendDeviceSerial(IPEndPoint src)
        {
            var serial = DataUtil.GetDeviceSerial();

            DreamSender.SendUdpWrite(0x01, 0x03, ByteUtils.StringBytes(serial), 0x60, _group, src);
        }