public virtual void Update() { CommStates processedState; do { processedState = this.commState; switch (this.commState) { case CommStates.idle: { lock (this) { if (this.actionQueue.Count != 0) { this.action = (CommAction)this.actionQueue.Dequeue(); } } if (null != this.action) { this.commAttemptCount = 0; this.commState = CommStates.tx; //Tracer.WriteHigh(TraceGroup.CANBUS, "", "action start"); } break; } case CommStates.tx: { this.commAttemptCount++; if (this.commAttemptCount <= this.action.RetryAttemptLimit) { int cobId = 0; byte[] frameData = this.action.GetTransmitFrame(ref cobId, this.NodeId); if (null != frameData) { bool txResult = this.Transmit(cobId, frameData); if (false != txResult) { this.action.TransmitComplete(); bool responseNeeded = this.action.ResponseNeeded(); if (false != responseNeeded) { //Tracer.WriteHigh(TraceGroup.CANBUS, "", "tx pending"); this.commTimeLimit = DateTime.Now.AddMilliseconds(this.action.RetryTime); this.commState = CommStates.rxWait; } else { this.EvaluateAction(this.action); this.action = null; this.commState = CommStates.idle; } } else { Tracer.WriteHigh(TraceGroup.CANBUS, "", "tx fail"); this.commTimeLimit = DateTime.Now.AddMilliseconds(this.TransmitFailureHoldoffTime); this.commState = CommStates.txWait; } } else { this.action = null; this.commState = CommStates.idle; } } else { this.Fault("comm failure"); this.action = null; } break; } case CommStates.txWait: { if (DateTime.Now > this.commTimeLimit) { this.commState = CommStates.tx; } break; } case CommStates.rxWait: { if (false != this.action.Done) { this.EvaluateAction(this.action); if (false != this.action.Aborted) { this.Fault("comm abort"); } this.action = null; this.commState = CommStates.idle; } else if (false != this.action.Transmit) { this.commAttemptCount = 0; this.commState = CommStates.tx; } else if (DateTime.Now > this.commTimeLimit) { this.commState = CommStates.tx; Tracer.WriteHigh(TraceGroup.CANBUS, "", "response timeout"); } break; } case CommStates.error: { break; } } } while (processedState != this.commState); if (null == this.FaultReason) { if ((false != this.consumerHeartbeatActive) && (DateTime.Now > this.consumerHeartbeatTimeLimit)) { this.Fault("heartbeat missing"); } } #region Download Process if (false != this.downloadActive) { string downloadResult = null; bool downloadComplete = false; if (null != this.FaultReason) { downloadComplete = true; downloadResult = "Device faulted: " + this.FaultReason; } else if (false != this.downloadCancel) { downloadComplete = true; downloadResult = "Cancelled"; } else if (DownloadSteps.start == this.downloadStep) { if (File.Exists(this.downloadFile) != false) { FileStream fs = File.Open(this.downloadFile, FileMode.Open); BinaryReader br = new BinaryReader(fs); this.downloadImagePosition = 0; this.downloadImageSize = (UInt32)fs.Length; this.downloadData = br.ReadBytes((int)this.downloadImageSize); br.Close(); br.Dispose(); fs.Close(); fs.Dispose(); this.receiveBootupHeartbeart = false; this.downloadAction = new NetworkRequest(0x80, this.NodeId); bool result = this.ScheduleAction(this.downloadAction); if (false != result) { this.downloadTimeLimit = DateTime.Now.AddMilliseconds(700); this.downloadStep = DownloadSteps.waitReset; } else { downloadResult = "Unable to schedule reset."; downloadComplete = true; } } else { downloadResult = "Unable to open file."; downloadComplete = true; } } else if (DownloadSteps.waitReset == this.downloadStep) { if ((null == this.downloadAction) && (false != this.receiveBootupHeartbeart)) { this.downloadTimeLimit = DateTime.Now.AddMilliseconds(500); this.downloadStep = DownloadSteps.waitBootStart; } else if (DateTime.Now > this.downloadTimeLimit) { this.downloadTimeLimit = DateTime.Now.AddMilliseconds(500); this.downloadStep = DownloadSteps.waitBootStart; } } else if (DownloadSteps.waitBootStart == this.downloadStep) { if (DateTime.Now > this.downloadTimeLimit) { this.downloadAction = new SDODownload(0x1F50, 1, this.downloadData, 0, this.downloadData.Length); bool result = this.ScheduleAction(this.downloadAction, 5000, 1); if (false != result) { this.downloadStep = DownloadSteps.waitFirmwareDownload; } else { downloadResult = "Unable to schedule download."; downloadComplete = true; } } } else if (DownloadSteps.waitFirmwareDownload == this.downloadStep) { if (null != this.downloadAction) { this.downloadImagePosition = ((SDODownload)this.downloadAction).SentCount; } else { this.downloadImagePosition = this.downloadImageSize; this.downloadAction = new SDODownload(0x1F51, 1, 1, 1); bool result = this.ScheduleAction(this.downloadAction); if (false != result) { this.downloadTimeLimit = DateTime.Now.AddSeconds(2); this.downloadStep = DownloadSteps.waitStartRequest; } else { downloadResult = "Unable to schedule start."; downloadComplete = true; } } } else if (DownloadSteps.waitStartRequest == this.downloadStep) { if (null == this.downloadAction) { downloadResult = null; downloadComplete = true; } } else { downloadResult = "Unknown step."; downloadComplete = true; } if (false != downloadComplete) { this.downloadData = null; this.downloadActive = false; this.downloadCompleteHandler(downloadResult); } } #endregion }
public bool DownloadImage(string file, ImageDownloadCompleteHandler onComplete) { bool result = false; if ((null == this.FaultReason) && (false == this.downloadActive)) { this.downloadFile = file; this.downloadCompleteHandler = onComplete; this.downloadCancel = false; this.downloadActive = true; this.downloadStep = DownloadSteps.start; result = true; } return (result); }