public bool UpdateTask(CanMsg msg) { if (CanDb.GetNodeTypeId(msg.ArbId) == CanDb.Instance.Nodes.FirstOrDefault(n => n.Name == NodeCollection.NODE_MCEL).NodeTypeId) { byte node = CanDb.GetNodeAddress(msg.ArbId); byte msgId = CanDb.GetMsgId(msg.ArbId); if (Devices.FirstOrDefault(n => n.Address == node) is IDevice item) { item.Update(msgId, msg.Data); DeviceUpdated?.Invoke(this, item); } else { var newitem = new MCEL181123DeviceItem(node, msgId, msg.Data); Devices.Add(newitem); NewDeviceArrived?.Invoke(this, newitem); } return(true); } else { return(false); } }
/*** TcpService ***/ private string TcpServiceParser(string line) { line = Regex.Replace(line, @"\s+", " "); var array = line.Trim().Split(' '); var addrStr = array[0].Trim(); var command = array[1].Trim(); byte address = 0; if (!addrStr.Contains("#")) { return("Emulator address format is invalid. Use this: '#A1 MEAS:VOLT?' (-???)"); } if (!byte.TryParse(addrStr.Substring(1), out address)) { return("Data Type Error (-104)"); } if (_explorer.Devices.Count != 0) { switch (command) { case "MEAS:CURR?": return(_explorer.Devices.FirstOrDefault(n => n.Address == address).SIG_MCEL_C_MEAS.ToString()); case "MEAS:VOLT?": return(_explorer.Devices.FirstOrDefault(n => n.Address == address).SIG_MCEL_V_MEAS.ToString()); case "VOLT": { double par; if (string.IsNullOrEmpty(array[2])) { return("Missing Paramtert (-109)"); } else if (!double.TryParse(array[2], out par)) { return("Data Type Error (-104)"); } var msg = CanDb.MakeMessage ( nodeTypeId: CanDb.Instance.Nodes.FirstOrDefault(n => n.Name == NodeCollection.NODE_PC).NodeTypeId, nodeAddress: address, broadcast: false, signal: CanDb.Instance.Signals.FirstOrDefault(n => n.Name == "SIG_PC_CV_SET"), value: par.ToString() ); _ioService.TxQueue.Enqueue(msg); return("OK"); } } } else { return("Hardware Missing (-241)"); } return("UNKNOWN"); }
public App() { /*** Application Settings Upgrade ***/ if (Settings.Default.ApplictionSettingsSaveCounter == 0) { Settings.Default.Upgrade(); Settings.Default.ApplictionSettingsUpgradeCounter++; } Settings.Default.ApplictionSettingsSaveCounter++; Settings.Default.PropertyChanged += new PropertyChangedEventHandler(Settings_PropertyChanged); /*** Main Form ***/ _mainForm = new MainForm(); _mainForm.Text = AppConstants.SoftwareTitle + " - " + Application.ProductVersion; _mainForm.Shown += MainForm_Shown; _mainForm.FormClosing += MainForm_FormClosing; _mainForm.FormClosed += new FormClosedEventHandler(MainForm_FormClosed); /*** Explorer ***/ _explorer = new Explorer(); /*** IoService ***/ _ioService = new IoService(_explorer); _ioService.Started += IoService_Started; _ioService.Stopped += IoService_Stopped; /*** TimerService ***/ TimerService.Instance.Interval = Settings.Default.GuiRefreshRateMs; /*** TcpService ***/ _tcpService = new TcpService(); _tcpService.ParserCallback = TcpServiceParser; _tcpService.Begin(null); _tcpService.Completed += TcpService_Completed; /*** Menu Bar ***/ #region MenuBar var configMenu = new ToolStripMenuItem("Config"); configMenu.DropDown.Items.AddRange( new ToolStripItem[] { new Commands.OptionsCommand(this) }); var helpMenu = new ToolStripMenuItem("Help"); helpMenu.DropDown.Items.AddRange( new ToolStripItem[] { new Commands.HowIsWorkingCommand(), // new Commands.UpdatesCommands(), }); var runMenu = new ToolStripMenuItem("Run"); runMenu.DropDown.Items.AddRange( new ToolStripItem[] { new Commands.PlayCommand(_ioService), new Commands.StopCommand(_ioService), new Commands.ResetCommand() }); _mainForm.MenuBar = new ToolStripItem[] { // configMenu, runMenu, //viewMenu, helpMenu, }; #endregion /*** SendView ***/ #region SendView var sendView = _mainForm.SendView; sendView.Signals = CanDb.Instance.Signals.Where(n => n.Message.NodeType.Name == NodeCollection.NODE_PC).Select(n => n.Name).ToArray(); sendView.SelectedSignalChanged += (o, s) => { sendView.Value = CanDb.Instance.Signals.FirstOrDefault(n => n.Name == sendView.SelectedSignal).DefaultValue; }; sendView.Send += (o, s) => { var msg = CanDb.MakeMessage ( nodeTypeId: CanDb.Instance.Nodes.FirstOrDefault(n => n.Name == NodeCollection.NODE_PC).NodeTypeId, nodeAddress: sendView.Address, broadcast: sendView.Broadcast, signal: CanDb.Instance.Signals.FirstOrDefault(n => n.Name == sendView.SelectedSignal), value: sendView.Value ); _ioService.TxQueue.Enqueue(msg); }; #endregion /*** Tree ***/ #region Tree _mainForm.Tree.AfterSelect += Tree_AfterSelect; _mainForm.Tree.Nodes.AddRange( new TreeNode[] { new View.TreeNodes.RacksTreeNode(_explorer), new View.TreeNodes.ModulsTree(_explorer), new View.TreeNodes.WaitForParseTreeNode(_ioService), new View.TreeNodes.DropFrameTreeNode(_ioService), new View.TreeNodes.ParsedFrameTreeNode(_ioService), new View.TreeNodes.RxFramesTreeNode(_ioService), new View.TreeNodes.TxFramesTreeNode(_ioService), new View.TreeNodes.WaitForTxTreeNode(_ioService), new View.TreeNodes.IoLogTreeNode() }); _mainForm.Tree.ContextMenuStrip = new ContextMenuStrip(); _mainForm.Tree.ContextMenuStrip.Items.AddRange( new ToolStripItem[] { new View.Commands.OpenCanIOLogFileCommand(), new View.Commands.DeleteCanIOLogFileCommand(), new View.Commands.OpenExplorerCanIOLogFileCommand(), }); #endregion /*** DataGrid ***/ var grid = _mainForm.DataGrid; grid.DataSource = _explorer.Devices; /*** StatusBar ***/ #region StatusBar _mainForm.StatusBar = new ToolStripItem[] { new StatusBar.AppLogStatus(), new StatusBar.WaitForParseFramesStatus(_ioService), new StatusBar.ParsedFramesStatus(_ioService), new StatusBar.DroppedFramesStatus(_ioService), new StatusBar.EmptyStatus(), new StatusBar.VersionStatus(), new StatusBar.LogoStatus(), }; #endregion /*** Run ***/ Application.Run((MainForm)_mainForm); }
public void Update(byte msgId, byte[] data) { LastRxTimeStamp = DateTime.Now; var mcelSingals = CanDb.Instance.Signals.Where(n => n.Message.NodeType.Name == NodeCollection.NODE_MCEL).Select(n => n); //Debug.WriteLine(string.Join("\r\n", mcelSingals.Select(n => n.Name))); switch (msgId) { case MessageCollection.MSG_MCEL_V_MEAS_ID: { var signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_V_MEAS)); SIG_MCEL_V_MEAS = CanDb.GetSingle(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_V_MEAS)); break; } case MessageCollection.MSG_MCEL_C_MEAS_ID: { var signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_C_RANGE)); SIG_MCEL_C_RANGE = CanDb.GetUInt8(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_C_RANGE)); signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_C_MEAS)); SIG_MCEL_C_MEAS = CanDb.GetSingle(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_C_MEAS)); break; } case MessageCollection.MSG_MCEL_STATUS_ID: { var signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_CC_STATUS)); SIG_MCEL_CC_STATUS = CanDb.GetBool(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_CC_STATUS)); signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_CV_STATUS)); SIG_MCEL_CV_STATUS = CanDb.GetBool(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_CV_STATUS)); signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_OE_STATUS)); SIG_MCEL_OE_STATUS = CanDb.GetBool(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_OE_STATUS)); break; } case MessageCollection.MSG_MCEL_TEMPS_ID: { var signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_UC_TEMP)); SIG_MCEL_UC_TEMP = CanDb.GetSingle(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_UC_TEMP)); signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_TR_TEMP)); SIG_MCEL_TR_TEMP = CanDb.GetSingle(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_TR_TEMP)); break; } case MessageCollection.MSG_MCEL_LIVE_ID: { var signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_RUN_TIME_TICK)); SIG_MCEL_RUN_TIME_TICK = CanDb.GetUInt32(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_RUN_TIME_TICK)); signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_VERSION)); SIG_MCEL_VERSION = CanDb.GetUInt32(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_VERSION)); break; } case MessageCollection.MSG_MCEL_NET_MON_ID: { var signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_RXERRCNT)); SIG_MCEL_RXERRCNT = CanDb.GetUInt8(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_RXERRCNT)); signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_TXERRCNT)); SIG_MCEL_TXERRCNT = CanDb.GetUInt8(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_TXERRCNT)); signal = mcelSingals.FirstOrDefault(n => n.Name == Tools.GetPropertyName(() => SIG_MCEL_RXTESTCNT)); SIG_MCEL_RXTESTCNT = CanDb.GetUInt8(signal, data); OnProppertyChanged(Tools.GetPropertyName(() => SIG_MCEL_RXTESTCNT)); break; } default: { //throw new ApplicationException("Unknown message Id"); break; }; } }