Exemple #1
0
        public FirmwareUpgradeWindow(string deviceName, string ip, CTRProductStuff.Devices device, uint deviceID) : base()
        {
            Width  = 500;
            Height = 100;
            Text   = deviceName + " Firmware Update Status";

            ProgressBar      pr          = new ProgressBar();
            TableLayoutPanel layoutPanel = new TableLayoutPanel();
            Label            label       = new Label();

            label.Text = "Progress";
            label.Dock = DockStyle.Fill;

            pr.Dock    = DockStyle.Fill;
            pr.Minimum = 0;
            pr.Maximum = 100;

            layoutPanel.ColumnCount = 1;
            layoutPanel.RowCount    = 2;
            layoutPanel.Dock        = DockStyle.Fill;
            layoutPanel.Controls.Add(label);
            layoutPanel.Controls.Add(pr);

            Controls.Add(layoutPanel);

            Show();

            FormClosing += formClosingEvent;

            Thread t = new Thread(() => updateThread(ip, device, deviceID, pr));

            t.IsBackground = true;
            t.Start();
        }
        private void updateDeviceButton_Click(object sender, EventArgs e)
        {
            if (deviceView.SelectedItems.Count == 1)
            {
                var descriptor = _deviceStatus.DeviceArray[deviceView.SelectedIndices[0]];
                CTRProductStuff.Devices dev = CTRProductStuff.DeviceIDMap[descriptor.ID & 0xFFFFFFC0];
                uint id = (uint)descriptor.ID & 0x3F;

                new FirmwareUpgradeWindow(descriptor.Name, _connectedIp, dev, id);
                Thread t = new Thread(() => updateFirmwareThread(_connectedIp, dev, id));
                t.IsBackground = false; //Make sure firmware update finished before closing thread
                t.Start();              //Make a thread for firmware update because it blocks for too long otherwise
            }
        }
        private void saveConfigButton_Click(object sender, EventArgs e)
        {
            if (deviceView.SelectedItems.Count == 1)
            {
                var descriptor = _deviceStatus.DeviceArray[deviceView.SelectedIndices[0]];
                CTRProductStuff.Devices dev = CTRProductStuff.DeviceIDMap[descriptor.ID & 0xFFFFFFC0];
                uint id = (uint)descriptor.ID & 0x3F;


                DeviceConfigs allConfigs = new DeviceConfigs();
                System.Collections.Generic.List <ConfigGroup> listOfConfigs = new System.Collections.Generic.List <ConfigGroup>();
                foreach (TabPage tab in groupedControls.TabPages)
                {
                    IControlGroup group    = ((GroupTabPage)tab).group.GetFromValues((GroupTabPage)tab);
                    ConfigGroup   newGroup = new ConfigGroup();
                    newGroup.Name    = tab.Text;
                    newGroup.Type    = group.GetType().Name;
                    newGroup.Ordinal = 0;
                    if (group is SlotGroup)
                    {
                        newGroup.Ordinal = (int)((SlotGroup)group).SlotNumber;
                    }
                    newGroup.Values = group;

                    listOfConfigs.Add(newGroup);
                }
                allConfigs.Configs = listOfConfigs.ToArray();
                string jsonToWrite = JsonConvert.SerializeObject(allConfigs);

                string builtIP = WebServerScripts.buildIP(_connectedIp, dev, id, CTRProductStuff.Action.SetConfig);
                using (MemoryStream outputStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonToWrite)))
                {
                    string ret = WebServerScripts.HttpPost(builtIP, outputStream);
                    outputStream.Close();
                    if (ret == "Failed")
                    {
                        updateReturnTextBox();
                        return;
                    }
                    SetConfigReturn retClass = JsonConvert.DeserializeObject <SetConfigReturn>(ret);
                    if (retClass == null)
                    {
                        updateReturnTextBox();
                        return;
                    }
                    updateReturnTextBox(retClass.GeneralReturn.Error, retClass.GeneralReturn.ErrorMessage);
                }
            }
        }
        public static string HttpGet(string ip, CTRProductStuff.Devices device, uint deviceID, CTRProductStuff.Action action, string extraOptions = "", int timeout = 500)
        {
            string address = buildIP(ip, device, deviceID, action, extraOptions);

            Console.WriteLine(address);
            string retval = "";

            Form1.GetInstance().Invoke(new Action(() =>
            {
                Form1.GetInstance().returnList.Items.Add("GET: " + address);
            }));

            //Request a GET at specified address
            WebRequest request = WebRequest.Create(address);

            request.Timeout = timeout;
            try
            {
                //Stream malarky to get response from GET request
                using (WebResponse response = request.GetResponse())
                {
                    Stream       dataStream = response.GetResponseStream();
                    StreamReader reader     = new StreamReader(dataStream);
                    //Got the string resposne
                    retval = reader.ReadToEnd();

                    //Clean up afterwards
                    reader.Close();
                    dataStream.Close();
                    response.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                retval = "Failed";
            }

            Form1.GetInstance().Invoke(new Action(() =>
            {
                Form1.GetInstance().returnList.Items.Add(retval);
            }));
            //Return that string
            return(retval);
        }
        private void blinkButton_Click(object sender, EventArgs e)
        {
            if (deviceView.SelectedItems.Count == 1)
            {
                var descriptor = _deviceStatus.DeviceArray[deviceView.SelectedIndices[0]];
                CTRProductStuff.Devices dev = CTRProductStuff.DeviceIDMap[descriptor.ID & 0xFFFFFFC0];
                uint id = (uint)descriptor.ID & 0x3F;

                string ret = WebServerScripts.HttpGet(_connectedIp, dev, id, CTRProductStuff.Action.Blink);
                if (ret == "Failed")
                {
                    updateReturnTextBox(-999, "Failed to transmit message");
                    return;
                }
                BlinkReturn tmp = JsonConvert.DeserializeObject <BlinkReturn>(ret);

                updateReturnTextBox(tmp.GeneralReturn.Error, tmp.GeneralReturn.ErrorMessage);
            }
        }
        private void selftTestButton_Click(object sender, EventArgs e)
        {
            if (deviceView.SelectedItems.Count == 1)
            {
                var descriptor = _deviceStatus.DeviceArray[deviceView.SelectedIndices[0]];
                CTRProductStuff.Devices dev = CTRProductStuff.DeviceIDMap[descriptor.ID & 0xFFFFFFC0];
                uint id = (uint)descriptor.ID & 0x3F;

                //Self test holds a "lot" of data, so increase the timeout for it
                string ret = WebServerScripts.HttpGet(_connectedIp, dev, id, CTRProductStuff.Action.SelfTest, "", 1000);
                if (ret == "Failed")
                {
                    updateReturnTextBox();
                    return;
                }
                SelfTestReturn retClass = JsonConvert.DeserializeObject <SelfTestReturn>(ret);
                updateReturnTextBox(retClass.GeneralReturn.Error, retClass.GeneralReturn.ErrorMessage);
                selfTestBox.Text = retClass.SelfTest;
            }
        }
        private void nameChangeButton_Click(object sender, EventArgs e)
        {
            if (deviceView.SelectedItems.Count == 1)
            {
                var descriptor = _deviceStatus.DeviceArray[deviceView.SelectedIndices[0]];
                CTRProductStuff.Devices dev = CTRProductStuff.DeviceIDMap[descriptor.ID & 0xFFFFFFC0];
                uint   id = (uint)descriptor.ID & 0x3F;
                string extraParameters = "&newname=" + nameChanger.Text;

                string ret = WebServerScripts.HttpGet(_connectedIp, dev, id, CTRProductStuff.Action.SetDeviceName, extraParameters);
                if (ret == "Failed")
                {
                    updateReturnTextBox();
                    refreshButton_Click(null, null); //Update GUI
                    return;
                }
                NameReturn retJson = JsonConvert.DeserializeObject <NameReturn>(ret);
                updateReturnTextBox(retJson.GeneralReturn.Error, retJson.GeneralReturn.ErrorMessage);
                refreshButton_Click(null, null); //Update GUI
            }
        }
        private void updateFirmwareThread(string ip, CTRProductStuff.Devices device, uint id)
        {
            string ret = WebServerScripts.HttpGet(ip, device, id, CTRProductStuff.Action.UpdateFirmware, "", 60000);//Give it a minute to update

            if (ret == "Failed")
            {
                Invoke(new Action(() =>
                {
                    updateReturnTextBox(-999, "Firmware update failed");
                }));
                return;
            }

            FirmwareUpdateReturn returnClass = JsonConvert.DeserializeObject <FirmwareUpdateReturn>(ret);

            Invoke(new Action(() =>
            {
                updateReturnTextBox(returnClass.GeneralReturn.Error, returnClass.UpdateMessage);
            }));

            return;
        }
Exemple #9
0
        private void updateThread(string ip, CTRProductStuff.Devices device, uint deviceID, ProgressBar updateBar)
        {
            ProgressReturn returnClass = null;
            bool           starting    = true;

            do
            {
                if (closeThread)
                {
                    break;
                }

                string ret = WebServerScripts.HttpGet(ip, device, deviceID, CTRProductStuff.Action.CheckUpdateProgress);
                if (ret == "Failed")
                {
                    continue;
                }
                returnClass = JsonConvert.DeserializeObject <ProgressReturn>(ret);
                if (returnClass != null)
                {
                    if (returnClass.progress != 0)
                    {
                        starting = false;
                    }
                    Invoke(new Action(() =>
                    {
                        updateBar.Value = returnClass.progress; //Update in UI Thread
                    }));
                }
                Thread.Sleep(100);
            } while (returnClass == null || returnClass.progress != 0 || starting);

            closeThread = true;
            Invoke(new Action(() =>
            {
                Close();
            }));
        }
        public static string buildIP(string baseIP, CTRProductStuff.Devices device, uint deviceID, CTRProductStuff.Action action, string extraOptions = "")
        {
            string address = baseIP;

            address += "?";
            switch (device)
            {
            case CTRProductStuff.Devices.None:
                break;     //No device selected, fall through

            default:
                address += "device=" + CTRProductStuff.DeviceMap[device];
                break;
            }
            address += "&";

            address += "id=" + deviceID;

            address += "&";

            switch (action)
            {
            case CTRProductStuff.Action.None:
                break;     //Just calling the address for a ping

            case CTRProductStuff.Action.SetID:
            case CTRProductStuff.Action.SetDeviceName:
            case CTRProductStuff.Action.UpdateFirmware:
                address += CTRProductStuff.ActionMap[action] + extraOptions;
                break;

            default:
                address += CTRProductStuff.ActionMap[action];
                break;
            }
            address = Uri.EscapeUriString(address);
            return(address);
        }
        private void refreshButton_Click(object sender, EventArgs e)
        {
            //On refresh poll for possible IP's and populate combobox with them
            _ipAddrItems.Clear();
            foreach (string addr in WebServerScripts.GetIPAddrs())
            {
                _ipAddrItems.Add(addr);
            }
            deviceView.Items.Clear();
            refreshConfigs();
            if (_connectedIp == "")
            {
                return;
            }
            string devices = WebServerScripts.HttpGet(_connectedIp, CTRProductStuff.Devices.None, 0, CTRProductStuff.Action.GetDeviceList, "", 1000);

            if (devices == "Failed")
            {
                updateReturnTextBox();
                return;
            }

            _deviceStatus = JsonConvert.DeserializeObject <GetDevicesReturn>(devices);
            if (_deviceStatus != null)
            {
                foreach (DeviceDescriptor d in _deviceStatus.DeviceArray)
                {
                    if ((d.ID & 0xFFFFFFC0) == 0x0204f400)
                    {
                        d.Model = "Pigeon Over Ribbon";
                    }

                    CTRProductStuff.Devices dev = CTRProductStuff.DeviceIDMap[(d.ID & 0xFFFFFFC0)];
                    string[] array = new string[7];
                    array[0] = d.Name;
                    array[1] = d.Model;
                    array[2] = (d.ID & 0x3F).ToString();
                    array[3] = d.CurrentVers;
                    array[4] = d.ManDate;
                    array[5] = d.BootloaderRev;
                    array[6] = d.SoftStatus;

                    int imageKey = 0;
                    switch (dev)
                    {
                    case CTRProductStuff.Devices.TalonSRX:
                        imageKey = 0;
                        break;

                    case CTRProductStuff.Devices.VictorSPX:
                        imageKey = 1;
                        break;

                    case CTRProductStuff.Devices.PigeonIMURibbon:
                    case CTRProductStuff.Devices.PigeonIMU:
                        imageKey = 2;
                        break;

                    case CTRProductStuff.Devices.CANifier:
                        imageKey = 3;
                        break;

                    case CTRProductStuff.Devices.PCM:
                        imageKey = 4;
                        break;

                    case CTRProductStuff.Devices.PDP:
                        imageKey = 5;
                        break;
                    }

                    deviceView.Items.Add(new ListViewItem(array, imageKey));
                }
            }
            updateReturnTextBox(_deviceStatus.GeneralReturn.Error, _deviceStatus.GeneralReturn.ErrorMessage);
            foreach (Control c in deviceSpecificControls.Controls)
            {
                c.Enabled = false;
            }
        }
        private void refreshConfigs()
        {
            groupedControls.TabPages.Clear();
            if (deviceView.SelectedItems.Count == 1)
            {
                var descriptor = _deviceStatus.DeviceArray[deviceView.SelectedIndices[0]];
                CTRProductStuff.Devices dev = CTRProductStuff.DeviceIDMap[descriptor.ID & 0xFFFFFFC0];
                uint id = (uint)descriptor.ID & 0x3F;

                //Populate Configs with TalonSRX.json
                FileStream configParams;
                switch (dev)
                {
                case CTRProductStuff.Devices.TalonSRX:
                    configParams = File.Open("TalonSRX.json", FileMode.Open);
                    break;

                case CTRProductStuff.Devices.VictorSPX:
                    configParams = File.Open("VictorSPX.json", FileMode.Open);
                    break;

                default:
                    configParams = File.Open("NotRecognized.json", FileMode.Open);
                    break;
                }
                string builtIP = WebServerScripts.buildIP(_connectedIp, dev, id, CTRProductStuff.Action.GetConfig);
                string txt     = WebServerScripts.HttpPost(builtIP, configParams);
                configParams.Close();

                if (txt == "Failed")
                {
                    updateReturnTextBox();
                    return;
                }

                GetConfigsReturn configs = JsonConvert.DeserializeObject <GetConfigsReturn>(txt);

                if (configs == null)//Should never happen
                {
                    updateReturnTextBox();
                    return;
                }
                //Update return text box before doing anything else
                updateReturnTextBox(configs.GeneralReturn.Error, configs.GeneralReturn.ErrorMessage);
                if (configs.Device == null || configs.Device.Configs == null)
                {
                    return;
                }

                foreach (ConfigGroup group in configs.Device.Configs)
                {
                    Type t = Type.GetType("DiagServerAccessor." + group.Type);

                    IControlGroup newGroup = (IControlGroup)Activator.CreateInstance(t);

                    GroupTabPage newTab = new GroupTabPage(newGroup);
                    newTab.Text = group.Name;

                    newGroup.SetFromValues(group.Values, group.Ordinal);

                    newTab.Controls.Add(newGroup.CreateLayout());

                    groupedControls.TabPages.Add(newTab);
                }
            }
        }