Example #1
0
        public NanoGroup(NanoData n, HttpClient hc, Socket hs)
        {
            _captureMode = DataUtil.GetItem <int>("captureMode");
            if (n != null)
            {
                SetData(n);
                _hc     = hc;
                _sender = hs;
            }

            _disposed = false;
        }
Example #2
0
        private void SetData(NanoData n)
        {
            _captureMode = DataUtil.GetItem <int>("captureMode");
            IpAddress    = n.IpAddress;
            _token       = n.Token;
            _layout      = n.Layout;
            Brightness   = n.Brightness;
            var nanoType = n.Type;

            _streamMode = nanoType == "NL29" ? 2 : 1;
            _basePath   = "http://" + IpAddress + ":16021/api/v1/" + _token;
            Id          = n.Id;
        }
Example #3
0
        public static async Task <List <NanoData> > Refresh(CancellationToken ct)
        {
            var foo       = Task.Run(() => Discover(), ct);
            var output    = new List <NanoData>();
            var newLeaves = await foo;

            foreach (var nl in newLeaves)
            {
                var cp = nl;
                var ex = DataUtil.GetCollectionItem <NanoData>("Dev_NanoLeaf", nl.Id);
                if (ex != null)
                {
                    cp = NanoData.CopyExisting(nl, ex);
                }
                output.Add(cp);
            }
            return(output);
        }
Example #4
0
        // Copy data from an existing leaf into this leaf...don't insert
        public static NanoData CopyExisting(NanoData newLeaf, NanoData existingLeaf)
        {
            if (existingLeaf == null || newLeaf == null)
            {
                throw new ArgumentException("Invalid nano data!");
            }
            newLeaf.Token    = existingLeaf.Token;
            newLeaf.X        = existingLeaf.X;
            newLeaf.Y        = existingLeaf.Y;
            newLeaf.Scale    = 1;
            newLeaf.Rotation = existingLeaf.Rotation;
            if (existingLeaf.Brightness != 0)
            {
                newLeaf.Brightness = existingLeaf.Brightness;
            }
            // Grab the new leaf layout
            newLeaf.RefreshLeaf();
            // Merge this data's layout with the existing leaf (copy sector)
            var newL = MergeLayouts(newLeaf.Layout, existingLeaf.Layout);

            newLeaf.Layout = newL;
            return(newLeaf);
        }
Example #5
0
        public static async Task <List <NanoData> > Discover(int timeout = 5)
        {
            var output = new List <NanoData>();
            var mDns   = new MulticastService();
            var sd     = new ServiceDiscovery(mDns);

            mDns.NetworkInterfaceDiscovered += (s, e) => {
                // Ask for the name of all services.
                sd.QueryServiceInstances("_nanoleafapi._tcp");
            };

            sd.ServiceDiscovered += (s, serviceName) => { mDns.SendQuery(serviceName, type: DnsType.PTR); };

            sd.ServiceInstanceDiscovered += (s, e) => {
                var name  = e.ServiceInstanceName.ToString();
                var nData = new NanoData {
                    IpAddress = string.Empty
                };
                if (!name.Contains("nanoleafapi", StringComparison.InvariantCulture))
                {
                    return;
                }
                foreach (var msg in e.Message.AdditionalRecords)
                {
                    switch (msg.Type)
                    {
                    case DnsType.A:
                        var aString = msg.ToString();
                        var aValues = aString.Split(" ");
                        nData.IpAddress = aValues[4];
                        nData.Name      = aValues[0].Split(".")[0];
                        break;

                    case DnsType.TXT:
                        var txtString = msg.ToString();
                        var txtValues = txtString.Split(" ");
                        nData.Version = txtValues[5]
                                        .Replace("srcvers=", string.Empty, StringComparison.InvariantCulture);
                        nData.Type = txtValues[4].Replace("md=", string.Empty, StringComparison.InvariantCulture);
                        nData.Id   = txtValues[3].Replace("id=", string.Empty, StringComparison.InvariantCulture);
                        break;

                    case DnsType.AAAA:
                        var mString = msg.ToString();
                        var mValues = mString.Split(" ");
                        nData.IpV6Address = mValues[4];
                        // Remove rest of FQDN
                        nData.Name = mValues[0].Split(".")[0];
                        break;

                    case DnsType.SRV:
                        var sString = msg.ToString();
                        var sValues = sString.Split(" ");
                        nData.Port     = int.Parse(sValues[6], CultureInfo.InvariantCulture);
                        nData.Hostname = sValues[7];
                        break;
                    }
                }

                if (string.IsNullOrEmpty(nData.IpAddress) && !string.IsNullOrEmpty(nData.Hostname))
                {
                    nData.IpAddress = nData.Hostname;
                }

                if (!string.IsNullOrEmpty(nData.IpAddress) && !string.IsNullOrEmpty(nData.Id))
                {
                    output.Add(nData);
                }
            };

            mDns.Start();
            LogUtil.Write("Nano: Discovery Started.");
            await Task.Delay(timeout * 1000).ConfigureAwait(false);

            mDns.Stop();
            sd.Dispose();
            mDns.Dispose();
            LogUtil.Write($"Nano: Discovery complete, found {output.Count} devices.");
            return(output);
        }