public Status RequestChangeDeviceID(DeviceDescrip dd, byte newID, Action.CallBack callback)
        {
            Action ac = new Action(callback, dd, ActionType.SetID);

            ac.newID = (uint)newID;
            return(PushAction(ac));
        }
        public Status RequestFieldUpgrade(DeviceDescrip dd, string crfPath, Action.CallBack callback)
        {
            Action ac = new Action(callback, dd, ActionType.FieldUpgradeDevice);

            ac.filePath = crfPath;
            return(PushAction(ac));
        }
        public Status RequestChangeName(DeviceDescrip dd, String newName, Action.CallBack callback)
        {
            Action ac = new Action(callback, dd, ActionType.SetDeviceName);

            ac.stringParam = newName;
            return(PushAction(ac));
        }
 public Action(CallBack callback, DeviceDescrip dd, ActionType type)
 {
     this.callback = callback;
     this.model    = dd.model;
     this.deviceID = dd.deviceID;
     this.type     = type;
 }
Пример #5
0
        bool FillListViewItem(ListViewItem item, DeviceDescrip descriptor)
        {
            bool ValueChanged = false; /* this can be used to count the tree changes */
            int  i            = 0;

            ValueChanged |= AssignIfDiff(item.SubItems[i++], descriptor.jsonStrings.Name);                    // Name
            ValueChanged |= AssignIfDiff(item.SubItems[i++], descriptor.jsonStrings.SoftStatus);              // Status
            ValueChanged |= AssignIfDiff(item.SubItems[i++], descriptor.jsonStrings.Model);                   // Model
            ValueChanged |= AssignIfDiff(item.SubItems[i++], descriptor.deviceID.ToString());                 // devID
            ValueChanged |= AssignIfDiff(item.SubItems[i++], descriptor.jsonStrings.CurrentVers);             // Firm
            ValueChanged |= AssignIfDiff(item.SubItems[i++], descriptor.jsonStrings.ManDate);                 // Man Date
            ValueChanged |= AssignIfDiff(item.SubItems[i++], descriptor.jsonStrings.BootloaderRev);           // Btld
            ValueChanged |= AssignIfDiff(item.SubItems[i++], descriptor.jsonStrings.HardwareRev);             // Man Rev

            /* icon index, same as model */
            int imageIndex = ModelToInt(descriptor.model);

            if (item.ImageIndex != imageIndex)
            {
                item.ImageIndex = imageIndex;
                ValueChanged    = true;
            }

            /* brk pt here to catch changes in tree*/
            if (ValueChanged)
            {
                return(true);
            }
            return(false);
        }
        public Status SaveConfigs(DeviceDescrip dd, string serializedData, Action.CallBack callback)
        {
            Action ac = new Action(callback, dd, ActionType.SetConfig);

            ac.stringParam = serializedData;
            return(PushAction(ac));
        }
        private Status UpdateConfigs(DeviceDescrip dd)
        {
            /* attempt a fresh copy of configs */
            GetConfigsReturn configs;
            Status           innerLoopErr = ExecuteGetConfigs(dd, out configs);

            /* if transactoin was successful, update the cache */
            if (innerLoopErr == Status.Ok)
            {
                /* form will get this on next poll */
                dd.configCache = configs;
            }
            else if (innerLoopErr == Status.CTRE_DI_NoConfigs)
            {
                /* Device does not support configs, this is not a problem, we just won't set configCache to new configs */
                innerLoopErr = Status.Ok;
            }
            else
            {
                /* leave the last cache alone, just because
                 * the connectoin was lost, don't drop anything */
            }

            return(innerLoopErr);
        }
        /// <summary>
        /// Convert jd into the strongly-typed device descriptor type.
        /// </summary>
        /// <param name="jd"></param>
        /// <returns></returns>
        public static DeviceDescrip Convert(JsonDevice jd)
        {
            DeviceDescrip dd = new DeviceDescrip();

            dd.jsonStrings = jd;
            dd.deviceID    = (byte)(jd.ID & 0x3F);
            dd.model       = jd.Model;
            return(dd);;
        }
Пример #9
0
 public bool GetDescriptor(ListViewItem item, out DeviceDescrip descriptor)
 {
     if (item != null)
     {
         if (_mapDescriptors.ContainsKey(item))
         {
             descriptor = _mapDescriptors[item];
             return(true);
         }
     }
     descriptor = null;
     return(false);
 }
Пример #10
0
 bool GetListViewItem(DeviceDescrip descriptor, out ListViewItem item)
 {
     foreach (KeyValuePair <ListViewItem, DeviceDescrip> entry in _mapDescriptors)
     {
         if (entry.Value.deviceID == descriptor.deviceID && entry.Value.model == descriptor.model)
         {
             item = entry.Key;
             return(true);
         }
     }
     item = null;
     return(false);
 }
Пример #11
0
        /**
         * Create a row in the device tree
         */
        ListViewItem CreateListViewItem(DeviceDescrip descriptor)
        {
            ListViewItem item = new ListViewItem();

            item.SubItems.Clear();
            item.Text = descriptor.jsonStrings.Name;                   // Name
            item.SubItems.Add((descriptor.jsonStrings.SoftStatus));    // Status
            item.SubItems.Add((descriptor.jsonStrings.Model));         // Model
            item.SubItems.Add((descriptor.deviceID.ToString()));       // devID
            item.SubItems.Add((descriptor.jsonStrings.CurrentVers));   // Firm
            item.SubItems.Add((descriptor.jsonStrings.ManDate));       // Man Date
            item.SubItems.Add((descriptor.jsonStrings.BootloaderRev)); // Btld
            item.SubItems.Add(descriptor.jsonStrings.HardwareRev);     // Hard Rev
            item.ImageIndex = ModelToInt(descriptor.model);            /* icon index, same as model */

            return(item);
        }
Пример #12
0
        public void SelectedIndexChanged(object sender, EventArgs e)
        {
            DeviceDescrip dd = null;

            if (lstDevices.SelectedItems.Count > 0)
            {
                ListViewItem sel = lstDevices.SelectedItems[0];
                /* update any gui elements that depend on selection */
                dd = Lookup(sel);
            }


            if (SelectionChanged != null)
            {
                /* update config panels */
                SelectionChanged(this, e, dd);
            }
        }
 public bool NewIdConflicts(DeviceDescrip dd, uint newID)
 {
     foreach (DeviceDescrip dev in GetDeviceDescriptors())
     {
         if (dev == dd)
         {
             continue;            //We're looking at our own device
         }
         if (dev.model == dd.model)
         {
             if (dev.deviceID == newID)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        private Status ExecutePollDevices()
        {
            /* attempt requesting an update */
            string response;
            Status retval = _WebServerScripts.HttpGet(_hostName, "", 0, ActionType.GetDeviceList, out response, String.Empty, 2000);

            /* attempt parsing */
            GetDevicesReturn deviceStatus = null;

            if (retval == Status.Ok)
            {
                try
                {
                    deviceStatus = Newtonsoft.Json.JsonConvert.DeserializeObject <GetDevicesReturn>(response);

                    if (deviceStatus != null)
                    {
                        /* decoded okay */
                        retval = (Status)deviceStatus.GeneralReturn.Error;
                    }
                    else
                    {
                        retval = Status.CouldNotParseJson;
                    }
                }
                catch (Exception)
                {
                    retval = Status.CouldNotParseJson;
                }
            }

            /* roll thru decoded json objects */
            if (retval == Status.Ok)
            {
                foreach (JsonDevice jd in deviceStatus.DeviceArray)
                {
                    DeviceDescrip dd = CTRE.Phoenix.Diagnostics.JSON.Adapter.Convert(jd);

                    _descriptors.Insert(dd);
                }
            }
            _lastPoll = DateTime.UtcNow;
            return(retval);
        }
        private Status ConfirmIfFieldUpgradeIsOccuring(DeviceDescrip ddRef, bool bExpectToBeFlashing)
        {
            string response = string.Empty;
            Status retval   = Status.Ok;

            /* get status update */
            if (retval == Status.Ok)
            {
                retval = _WebServerScripts.HttpGet(_hostName, ddRef.model, ddRef.deviceID, ActionType.CheckUpdateProgress, out response, "", 200);
            }
            if (retval != Status.Ok)
            {
                return(retval);
            }


            /* get the error code in JSON */
            ProgressReturn jsonResp = JsonConvert.DeserializeObject <ProgressReturn>(response);

            retval = (Status)jsonResp.GeneralReturn.Error;

            if (bExpectToBeFlashing == false)
            {
                /* caller expects the server to be not be flashing anything -  we should get CTRE_DI_NotUpdating */

                if (retval == Status.CTRE_DI_NotUpdating)
                {
                    retval = Status.Ok;
                }
                else if (retval == Status.Ok)
                {
                    retval = Status.ServerIsFlashingAlready;
                }
            }
            else
            {
                /* server should be flashing - leave retval alone */
            }

            return(retval);
        }
        private Status ExecuteGetConfigs(DeviceDescrip dd, out GetConfigsReturn configs)
        {
            /* init the outputs */
            configs = null;
            Status retval = Status.Ok;

            /* all state info to track */
            string response = string.Empty;

            /* do the http exchange */
            if (retval == Status.Ok)
            {
                retval = _WebServerScripts.HttpGet(_hostName, dd.model, dd.deviceID, ActionType.GetConfig, out response);
            }

            /* parse the served response */
            if (retval == Status.Ok)
            {
                configs = JsonConvert.DeserializeObject <GetConfigsReturn>(response);

                if (configs == null) /* what does this mean */
                {
                    retval = Status.GeneralError;
                }
                else if (configs.Device == null) /* what does this mean? */
                {
                    retval = (Status)configs.GeneralReturn.Error;
                }
                else if (configs.Device.Configs == null) /* what does this mean? */
                {
                    retval = Status.GeneralError;
                }
            }

            return(retval);
        }
        private Status ExecuteFieldUpgrade(DeviceDescrip ddRef, AsyncWebExchange asyncWebExchange, string fileName, bool usingSftp)
        {
            Status retval   = Status.Ok;
            string response = string.Empty;

            /* let user know action is being processed */
            SetFieldUpgradeStatus("Reading CRF...", 0);


            byte[] fileContents = null;
            /* If we're using POST, we need to make sure the file has contents */
            if (!usingSftp)
            {
                /* copy out CRF */
                fileContents = File.Read(_action.filePath);

                /* check file read */
                if (retval == Status.Ok)
                {
                    if (fileContents == null)
                    {
                        retval = Status.CouldNotOpenFile;
                    }
                }
            }

            /* check firmUpdate progress first */
            if (retval == Status.Ok)
            {
                SetFieldUpgradeStatus("Confirm server is ready", 0); /* we are confirming if field upgrade is already occuring */
                /* Make double sure that we aren't field upgrading. Server already does this but there's no harm in checking again */
                retval = ConfirmIfFieldUpgradeIsOccuring(ddRef, false);
                SetFieldUpgradeStatus("Confirm server is ready : " + retval, 0); /* display the result of this check */
            }

            /* request firmware-update of device */
            if (retval == Status.Ok)
            {
                SetFieldUpgradeStatus("Starting field-upgrade", 0); /* now we will request to start a new FU session */

                /* If we're using sftp, use Get */
                if (usingSftp)
                {
                    /* start firmware-update */
                    retval = asyncWebExchange.StartHttpGet(_hostName, ddRef.model, ddRef.deviceID, ActionType.FieldUpgradeDevice, 60000, "&file=" + fileName);
                }
                else /* Otherwise use Post */
                {
                    /* start firmware-update */
                    retval = asyncWebExchange.StartHttpPost(_hostName, ddRef.model, ddRef.deviceID, ActionType.FieldUpgradeDevice, fileContents, 60000);
                }
            }
            /* confirm FU started okay */
            if (retval == Status.Ok)
            {
                /* wait until we get the rising edge of the flash event - a percent update with healthy error code, or a failed response from async web request. */
                int       i         = 0;
                const int kMaxLoops = 50;
                for (; i < kMaxLoops; ++i)
                {
                    const int kTimePerLoopMs = 100;

                    /* check if flashing has started */
                    retval = ConfirmIfFieldUpgradeIsOccuring(ddRef, true);

                    /* leave for-loop if flashing started */
                    if (retval == Status.Ok)
                    {
                        break;
                    }

                    /* wait for kTimePerLoopMs to see if firmUpdate finishes early */
                    bool bIsDone = false;
                    var  respErr = CheckAsyncWebServerResp(asyncWebExchange, kTimePerLoopMs, out bIsDone);

                    /* leave for-loop if FU request completed */
                    if (bIsDone)
                    {
                        /* since we are leaving the loop because of this, save the error code */
                        retval = respErr;
                        break;
                    }
                }
                /* uncomment to help better debug where errors are sourced */
                //SetFieldUpgradeStatus("Starting field-upgrade : " + retval, 0);
            }
            /* poll status - loop while status is ok and firmUpdate is not complete*/
            int notUpdatingCnt = 0;

            while (retval == Status.Ok)
            {
                /* request an update */
                if (retval == Status.Ok)
                {
                    retval = _WebServerScripts.HttpGet(_hostName, ddRef.model, ddRef.deviceID, ActionType.CheckUpdateProgress, out response, "", 200);
                }
                /* proces the update response */
                if (retval == Status.Ok)
                {
                    /* parse the response that has the field-upgrade status */
                    ProgressReturn jsonResp = JsonConvert.DeserializeObject <ProgressReturn>(response);
                    retval = (Status)jsonResp.GeneralReturn.Error;

                    if (retval == Status.CTRE_DI_NotUpdating)
                    {
                        ++notUpdatingCnt; /* most likely flash has finished */
                        retval = Status.Ok;
                    }
                    else
                    {
                        /* save it so calling application can poll it */
                        SetFieldUpgradeStatus(jsonResp);
                    }
                }
                /* check on the first async web exchange */
                if (retval == Status.Ok)
                {
                    bool bIsDone = false;
                    retval = CheckAsyncWebServerResp(asyncWebExchange, 100, out bIsDone);

                    /* leave while-loop if FU request completed */
                    if (bIsDone)
                    {
                        break;
                    }
                }
                /* check on our state in case GUI is shutting down */
                if (GetState_NoLock() != State.ExecAction)
                {
                    retval = Status.Aborted;
                }
            }
            return(retval);
        }
Пример #18
0
 bool GetSelectedDescriptor(out DeviceDescrip descriptor)
 {
     return(GetDescriptor(GetSelectedDevice(), out descriptor));
 }
        public Status RequestBlink(DeviceDescrip dd, Action.CallBack callback)
        {
            Action ac = new Action(callback, dd, ActionType.Blink);

            return(PushAction(ac));
        }
        public Status RequestSelfTest(DeviceDescrip dd, Action.CallBack callback)
        {
            Action ac = new Action(callback, dd, ActionType.SelfTest);

            return(PushAction(ac));
        }
        public Status RequestFactoryDefault(DeviceDescrip dd, Action.CallBack callback)
        {
            Action ac = new Action(callback, dd, ActionType.FactoryDefault);

            return(PushAction(ac));
        }