コード例 #1
0
 private void DevList_ItemTapped(object sender, ItemTappedEventArgs e)
 {
     if (e.Item is DeviceWithValue dev)
     {
         Console.WriteLine(DeviceLogics.Get(dev.Dev.ProductID));
     }
 }
コード例 #2
0
ファイル: App.xaml.cs プロジェクト: SparkVial/sparkvial_app
        public App(SparkVial sv)
        {
            this.sv = sv;
            Console.WriteLine($"--- SparkVial App v0.1.0 / libsv v{SparkVial.MajorVersion}.{SparkVial.MinorVersion}.{SparkVial.PatchVersion} ---");

            // When an interface is connected enable it, scan it, and create an entry for it in the UI.
            sv.OnInterfaceAdded += (Interface inf) => {
                Console.WriteLine($"- Interface of type {inf.type} added: {inf.id}");

                inf.Enable().ContinueWith((Task <bool> success) => {
                    if (success.Result)
                    {
                        new Thread(() => {
                            sv.ScanInterface(inf);
                        }).Start();
                    }
                });

                Xamarin.Forms.Device.BeginInvokeOnMainThread(() => {
                    var newInf         = new InterfaceWithStatus(inf);
                    infMap[inf]        = newInf;
                    infMap[inf].Status = "Enabling...";
                    infs.Add(newInf);
                });
            };

            // When an interface starts/stops scanning update it on the UI.
            sv.OnInterfaceScanning += async(inf, tsk) => {
                Xamarin.Forms.Device.BeginInvokeOnMainThread(() => {
                    infMap[inf].Status = "Scanning...";
                });
                await tsk;
                Xamarin.Forms.Device.BeginInvokeOnMainThread(() => {
                    infMap[inf].Status = "Active";
                });
            };

            // When an interface gets removed update it on the UI.
            sv.OnInterfaceRemoved += (inf) => {
                Console.WriteLine($"- Interface of type {inf.type} removed: {inf.id}");
                infs.Remove(infMap[inf]);
                infMap.Remove(inf);
            };

            // When a device gets added spawn a graph node for it and start sampling it at 10hz.
            sv.OnDeviceAdded += (inf, dev) => {
                Console.WriteLine($"- Device '{dev.name}' added with serial number {dev.uniqueID:X8}");
                if (dev is PeripheralDevice pdev)
                {
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() => {
                        devMap[pdev] = new DeviceWithValue(pdev);
                        infMap[inf].Add(devMap[pdev]);
                        var randX         = pseudoRandomGen.Next(-200, 200);
                        var randY         = pseudoRandomGen.Next(-200, 200);
                        devNodesMap[pdev] = new SensorNode(pdev.Name, "Number", "Units", devMap[pdev].Tape, graphEditor.Graph)
                        {
                            pos = new SKPoint(300 + randX, 200 + randY)
                        };

                        graphEditor.Graph.nodes.Add(devNodesMap[pdev]);
                    });
                }
                dev.AdjustInterval(100 /*ms*/);
            };

            // When a device gets removed remove it's graph node.
            sv.OnDeviceRemoved += (inf, dev) => {
                Console.WriteLine($"- Device '{dev.name}' with serial number {dev.uniqueID:X8} removed");
                if (dev is PeripheralDevice pdev)
                {
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() => {
                        graphEditor.Graph.connections.RemoveAll(c => c.sink == devNodesMap[pdev] || c.source == devNodesMap[pdev]);
                        graphEditor.Graph.nodes.Remove(devNodesMap[pdev]);
                        devNodesMap.Remove(pdev);
                        infMap[inf].Remove(devMap[pdev]);
                        devMap.Remove(pdev);
                    });
                }
            };

            // When a sample occurs update it's value on the UI and in the sensor node.
            sv.OnSample += (inf, dev, smp) => {
                //Console.WriteLine($"- Value from {dev.name} ({dev.uniqueID:X8}) with {smp.values.Count} fields at {smp.timestamp}ms:");
                string formattedValue = DeviceLogics.Get(dev.productID).FormatValue(smp);

                if (devMap.ContainsKey(dev))
                {
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() => {
                        devMap[dev].Value = formattedValue;
                    });
                    devMap[dev].Tape.Add(smp);
                }
            };

            InitializeComponent();

            MainPage            = new MainPage();
            devList             = MainPage.FindByName <DevicesPage>("DevPage").FindByName <ListView>("DevList");
            devList.ItemsSource = infs;
            graphEditor         = MainPage.FindByName <GraphPage>("GraphPage").FindByName <GraphEditor>("GraphEditor");

            graphEditor.Graph.nodes.Add(new AddNode(graphEditor.Graph)
            {
                pos = new SKPoint(600, 200)
            });

            graphEditor.Graph.nodes.Add(new ChartNode(graphEditor.Graph)
            {
                pos = new SKPoint(750, 300)
            });

            graphEditor.Graph.connections.CollectionChanged += ConnectionsChanged;

            new Thread(UIRefresherThread).Start();
        }