/// <summary> /// Creates a node that represents the specified command. /// </summary> private TreeNode CreateCmdNode(CmdConfig cmd) { TreeNode cmdNode = new(GetCmdNodeText(cmd)) { Tag = cmd }; cmdNode.SetImageKey("cmd.png"); return(cmdNode); }
/// <summary> /// Shows the command configuration. /// </summary> private void ShowCmdConfig(CmdConfig cmd) { numCmdAddress.Value = 1; numCmdAddress.Minimum = AddrShift; numCmdAddress.Maximum = ushort.MaxValue + AddrShift; numCmdAddress.Hexadecimal = !DecAddr; ShowFuncCode(cmd); ShowByteOrder(cmd); if (cmd == null) { txtCmdName.Text = ""; txtCmdCode.Text = ""; pnlCmdCodeWarn.Visible = false; numCmdNum.Value = 0; cbCmdDataBlock.SelectedIndex = 0; chkCmdMultiple.Checked = false; numCmdAddress.Value = AddrShift; lblCmdAddressHint.Text = ""; cbCmdElemType.SelectedIndex = 0; numCmdElemCnt.Value = 1; pnlCmdElem.Visible = true; gbCmd.Enabled = false; } else { txtCmdName.Text = cmd.Name; txtCmdCode.Text = cmd.CmdCode; pnlCmdCodeWarn.Visible = string.IsNullOrEmpty(cmd.CmdCode); numCmdNum.SetValue(cmd.CmdNum); cbCmdDataBlock.SelectedIndex = cmd.DataBlock switch { DataBlock.Coils => 0, DataBlock.HoldingRegisters => 1, _ => 2 }; if (cmd.DataBlock == DataBlock.Custom) { chkCmdMultiple.Checked = false; chkCmdMultiple.Enabled = false; pnlCmdElem.Visible = false; } else { chkCmdMultiple.Checked = cmd.Multiple; chkCmdMultiple.Enabled = true; numCmdAddress.SetValue(cmd.Address + AddrShift); lblCmdAddressHint.Text = string.Format(ModbusDriverPhrases.AddressHint, AddrNotation, AddrShift); cbCmdElemType.SelectedIndex = (int)cmd.ElemType; cbCmdElemType.Enabled = cmd.ElemTypeEnabled; numCmdElemCnt.Maximum = cmd.MaxElemCnt; numCmdElemCnt.SetValue(cmd.ElemCnt); numCmdElemCnt.Enabled = cmd.Multiple; pnlCmdElem.Visible = true; } gbCmd.Enabled = true; } }
/// <summary> /// Shows the command configuration. /// </summary> private void ShowCmdConfig(CmdConfig cmd) { ctrlElemGroup.Visible = false; ctrlElem.Visible = false; ctrlCmd.Visible = true; ctrlCmd.TemplateOptions = template.Options; ctrlCmd.Cmd = cmd; }
// CTOR public RunSheduler(ILoggerFactory loggerFactory , IOptions <CmdConfig> confCmd , IOptions <WorkersConf> confWorkers , IShedulerFactory shedulerFactory) { _logger = loggerFactory.CreateLogger <NLogLoggerProvider>(); _cmdConfig = confCmd.Value; _workers = confWorkers.Value.Workers; _shedulerFactory = shedulerFactory; }
private void Clear() { m_AirConfig = null; m_PlayerConfig = null; m_CNSConfig = null; m_CmdConfig = null; if (m_LuaConfig != null) { m_LuaConfig.Dispose(); m_LuaConfig = null; } }
/// <summary> /// 设置指令的缓存 /// </summary> /// <param name="cmdCode"></param> /// <param name="model"></param> /// <returns></returns> public static CmdConfig GetCachCmdConfig(string cmdCode, int model) { string key = "cmd_" + cmdCode + model; CmdConfig cmd = GetCache(key) as CmdConfig; if (cmd == null) { cmd = CmdLogic.GetCmdConfigByCode(cmdCode, model); } SetCache(key, cmd, 3600);//一个小时过期 return(cmd); }
/// <summary> /// 启动指令队列 /// </summary> /// <param name="second">执行时间间隔,默认10分钟</param> public static void StartCmdQueue(int second = 600) { //最少一分钟跑一次 if (second < 60) { second = 60; } ThreadPool.QueueUserWorkItem(p => { while (true) { try { List <CmdQueue> list = CmdLogic.GetCmdQueues(); InLogQueue("获取到待执行指令数量:" + list.Count(), LogLevel.Info, "local"); if (list.Count > 0) { foreach (var l in list) { //多个指令以_区分 string[] cmdCodes = l.CmdCode.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries); foreach (string code in cmdCodes) { CmdConfig cmd = CacheHelper.GetCachCmdConfig(code, l.Model); CmdLog log = new CmdLog(); log.CmdCode = code; log.CmdName = l.ExceTime + "分钟:" + cmd.CmdName; log.Imei = l.IMEI; log.Param = l.Param; log.IsSucess = false; log.SendLoginName = l.Notice; log.SendDate = DateTime.Now; SendCmdModel model = DeviceData.SendCmd(code, l.APIDeviceId ?? 0, l.Model, CacheHelper.GetTokenByDeviceId(l.IMEI), l.Param); log.IsSucess = model.State == 0; log.Notice = "每隔" + l.ExceTime + "分钟下发主动监测。"; log.Response = model.Content + ":" + model.Message; log.ResponseTime = DateTime.Now; CmdLogic.SaveCmdLog(log);//保存日志 } CmdLogic.UpdateQueueTime(l); } } } catch (Exception ex) { InLogQueue_Error(ex, "local"); } Thread.Sleep(second * 1000); } }); }
/// <summary> /// Shows the byte order of the command. /// </summary> private void ShowByteOrder(CmdConfig cmd) { if (cmd != null && cmd.ByteOrderEnabled) { txtCmdByteOrder.Text = cmd.ByteOrder; txtCmdByteOrder.Enabled = true; } else { txtCmdByteOrder.Text = ""; txtCmdByteOrder.Enabled = false; } }
/// <summary> /// Gets the command node text. /// </summary> private string GetCmdNodeText(CmdConfig cmd) { string cmdName = string.IsNullOrEmpty(cmd.Name) ? ModbusDriverPhrases.UnnamedCommand : cmd.Name; string blockName = ModbusUtils.GetDataBlockName(cmd.DataBlock); if (cmd.DataBlock == DataBlock.Custom) { return(string.Format("{0} ({1})", cmdName, blockName)); } else { string addrRange = ModbusUtils.GetAddressRange(cmd.Address, cmd.ElemCnt * ModbusUtils.GetQuantity(cmd.ElemType), template.Options.ZeroAddr, template.Options.DecAddr); return(string.Format("{0} ({1}, {2})", cmdName, blockName, addrRange)); } }
/// <summary> /// Shows the function code of the command. /// </summary> private void ShowFuncCode(CmdConfig cmd) { if (cmd == null) { numCmdFuncCode.Value = 0; numCmdFuncCode.ReadOnly = true; } else if (cmd.DataBlock == DataBlock.Custom) { numCmdFuncCode.Value = (byte)cmd.CustomFuncCode; numCmdFuncCode.ReadOnly = false; } else { numCmdFuncCode.Value = ModbusUtils.GetWriteFuncCode(cmd.DataBlock, cmd.Multiple); numCmdFuncCode.ReadOnly = true; } }
/// <summary> /// Initializes a new instance of the class. /// </summary> public FrmDeviceTemplate(AppDirs appDirs, CustomUi customUi) : this() { this.appDirs = appDirs ?? throw new ArgumentNullException(nameof(appDirs)); this.customUi = customUi ?? throw new ArgumentNullException(nameof(customUi)); elemGroupsNode = treeView.Nodes["elemGroupsNode"]; commandsNode = treeView.Nodes["commandsNode"]; template = null; modified = false; selectedNode = null; selectedElemGroup = null; selectedElemTag = null; selectedCmd = null; SaveOnly = false; FileName = ""; }
/// <summary> /// Creates a new Modbus command based on the command configuration. /// </summary> private ModbusCmd CreateModbusCmd(DeviceTemplateOptions options, CmdConfig cmdConfig) { ModbusCmd modbusCmd = deviceModel.CreateModbusCmd(cmdConfig.DataBlock, cmdConfig.Multiple); modbusCmd.Name = cmdConfig.Name; modbusCmd.Address = (ushort)cmdConfig.Address; modbusCmd.ElemType = cmdConfig.ElemType; modbusCmd.ElemCnt = cmdConfig.ElemCnt; modbusCmd.ByteOrder = ModbusUtils.ParseByteOrder(cmdConfig.ByteOrder) ?? options.GetDefaultByteOrder(ModbusUtils.GetDataLength(cmdConfig.ElemType) * cmdConfig.ElemCnt); modbusCmd.CmdNum = cmdConfig.CmdNum; modbusCmd.CmdCode = cmdConfig.CmdCode; if (cmdConfig.DataBlock == DataBlock.Custom) { modbusCmd.SetFuncCode((byte)cmdConfig.CustomFuncCode); } modbusCmd.InitReqPDU(); modbusCmd.InitReqADU(deviceModel.Addr, transMode); return(modbusCmd); }
/// <summary> /// 获取设备指令列表 /// </summary> /// <param name="type">1用户id,2设备id</param> /// <param name="objId"></param> /// <returns></returns> public JsonResult AjaxGetCmdListAndLog(int type, int objId) { Device d = null; if (type == 1) { d = DeviceLogic.GetDeviceByUserId(objId); } else if (type == 2) { d = DeviceLogic.GetDeviceById(objId); } if (d == null) { return(Json(new BaseResult { State = State.NotFind, Message = "未找到绑定设备" })); } List <CmdConfig> ccList = CmdLogic.GetCmdList(d.APIDeviceModel ?? 0); List <CmdLog> clList = CmdLogic.GetCmdLogs(d.Imei); List <CmdQueue> qList = CmdLogic.GetQueueByDeviceId(d.APIDeviceId ?? 0); foreach (var q in qList) { CmdConfig _c = ccList.FirstOrDefault(p => p.CmdCode == q.CmdCode); if (_c != null) { _c.CmdValue = (q.ExceTime ?? 0).ToString(); } } return(Json(new { State = State.Success, UserName = AuthUser.LoginName, CmdList = ccList, LogList = clList, Imei = d.Imei, DeviceId = d.Id })); }
/// <summary> /// 获取命令请求分发服务器信息命令执行 /// </summary> /// <param name="context"></param> public override void Execute(DataContext context) { byte[] cmdData = context.CmdData; if (cmdData.Length != 4) { context.Flush(RespondCode.CmdDataLack); return; } int lastVer = BitConverter.ToInt32(cmdData.Reverse(), 0); if (Compiled.Debug) { cmdData.Debug("=== Support.CmdDispatch 上行数据==="); lastVer.Debug("=== Support.CmdDispatch 上行数据 ==="); } CmdConfig config = CmdConfigs.GetCmdConfigFromStorage(); var cmdWithServers = config.CmdList.Where(c => c.VCode > lastVer); if (cmdWithServers.Count() > 0) { CmdServer result = new CmdServer { LastVer = cmdWithServers.Max(c => c.VCode), DataList = cmdWithServers.Select(c => new KvPair { Key = c.Cmd, Value = c.Server }).ToList() }; context.Flush <CmdServer>(result); } else { context.Flush(); } }
/// <summary> /// Fills the tree view according to the device template. /// </summary> private void FillTree() { // reset selected objects selectedNode = null; selectedElemGroup = null; selectedElemTag = null; selectedCmd = null; ShowElemGroupConfig(null); try { treeView.BeginUpdate(); elemGroupsNode.Nodes.Clear(); commandsNode.Nodes.Clear(); // fill element groups foreach (ElemGroupConfig elemGroup in template.ElemGroups) { elemGroupsNode.Nodes.Add(CreateElemGroupNode(elemGroup)); } // fill commands foreach (CmdConfig modbusCmd in template.Cmds) { commandsNode.Nodes.Add(CreateCmdNode(modbusCmd)); } elemGroupsNode.Expand(); commandsNode.Expand(); treeView.SelectedNode = elemGroupsNode; } finally { treeView.EndUpdate(); } }
/// <summary> /// 下发指令 /// </summary> /// <param name="cmdCode">指令code</param> /// <param name="deviceId">设备id</param> /// <returns></returns> public JsonResult AjaxSendCmd(string cmdCode, int deviceId, string param, int min) { BaseResult res = new BaseResult(); Device d = DeviceLogic.GetDeviceById(deviceId); CmdConfig cmd = CmdLogic.GetCmdConfigByCode(cmdCode, d.APIDeviceModel.Value); if ((d.APIDeviceModel ?? 0) <= 0) { res.Message = "指令下发失败,设备型号为空!"; res.State = State.Falid; return(Json(res)); } CmdLog log = new CmdLog(); log.CmdCode = cmdCode; log.CmdName = cmd.CmdName; log.Imei = d.Imei; log.Param = param; log.IsSucess = false; log.SendLoginName = AuthUser.LoginName; //是否立即下发,不是的加入队列 if ((cmd.IsNowSend ?? true) == false)//设置主动上传间隔 { log.SendDate = DateTime.Now; if (min > 0) { CmdQueue queue = new CmdQueue(); queue.APIDeviceId = d.APIDeviceId; queue.Model = d.APIDeviceModel.Value; queue.CmdCode = cmdCode; queue.Created = DateTime.Now; queue.LastExecTime = DateTime.Now; queue.Param = param; queue.ExceTime = min; queue.IMEI = d.Imei; queue.Notice = AuthUser.LoginName; CmdLogic.SaveCmdQueue(queue); SaveUserLog(AuthUser.LoginName + "设置了设备" + d.Imei + cmd.CmdName + ":" + min + "分钟", LogLevel.Info, AuthUser.LoginName, "AjaxSendCmd", "下发指令"); } else { CmdLogic.DelCmdQueue(cmdCode, d.APIDeviceId.Value); } log.CmdName = cmd.CmdName + ":" + min + "分钟"; res.State = State.Success; res.Message = "设置成功," + min + "分钟"; //return Json(res); } else if (d != null) { log.SendDate = DateTime.Now; SendCmdModel model = DeviceData.SendCmd(cmdCode, d.APIDeviceId ?? 0, d.APIDeviceModel ?? 0, CacheHelper.GetToken(d.UserId), param); if (model.State != 0) { res.State = State.Falid; res.Message = model.Content + ":" + model.Message; SaveUserLog(AuthUser.LoginName + "下发设备" + d.Imei + "的" + cmd.CmdName + "指令失败:" + res.Message, LogLevel.Sensitive, AuthUser.LoginName, "AjaxSendCmd", "下发指令"); } else { log.IsSucess = true; res.State = State.Success; res.Message = model.Content; SaveUserLog(AuthUser.LoginName + "下发设备" + d.Imei + "的" + cmd.CmdName + "指令:" + res.Message, LogLevel.Info, AuthUser.LoginName, "AjaxSendCmd", "下发指令"); //WriteLog( + res.Message); } log.Response = model.State + "|" + res.Message; log.ResponseTime = DateTime.Now; } else { res.State = State.NotFind; res.Message = "设备不存在"; WriteLog(AuthUser.LoginName + "下发设备" + d.Imei + "的" + cmd + "指令失败:" + res.Message); } //保存指令下发记录 CmdLogic.SaveCmdLog(log); if (res.Message.StartsWith("Off")) { res.Message = "设备不线,指令离线下发"; } return(Json(res)); }
private bool Init(string playerName, out GlobalPlayerLoaderResult result, string cnsName = "") { Clear(); result = GlobalPlayerLoaderResult.None; m_PlayerName = playerName; if (string.IsNullOrEmpty(playerName)) { result = GlobalPlayerLoaderResult.ParamError; return(false); } try { m_PlayerConfig = new PlayerConfig(); m_PlayerConfig.LoadPlayer(playerName); } catch (Exception e) { #if DEBUG Debug.LogError(e.ToString()); #endif Clear(); result = GlobalPlayerLoaderResult.PlayerConfigError; return(false); } if (!m_PlayerConfig.IsVaild) { Clear(); result = GlobalPlayerLoaderResult.PlayerConfigError; return(false); } try { string airName = string.Empty; if (m_PlayerConfig != null && m_PlayerConfig.Files != null) { airName = m_PlayerConfig.Files.anim; airName = GlobalConfigMgr.GetConfigFileNameNoExt(airName); } m_AirConfig = new AirConfig(playerName, airName); if (!m_AirConfig.IsVaild) { Clear(); result = GlobalPlayerLoaderResult.AirConfigError; return(false); } } catch (Exception e) { #if DEBUG Debug.LogError(e.ToString()); #endif Clear(); result = GlobalPlayerLoaderResult.AirConfigError; return(false); } // 判断LUA bool isLua = false; try { if (m_PlayerConfig != null && m_PlayerConfig.Files != null && m_PlayerConfig.Files.HasLuaFile) { isLua = true; m_LuaConfig = new LuaCnsConfig(); if (!m_LuaConfig.LoadFromFile(m_PlayerConfig.Files.lua)) { result = (GlobalPlayerLoaderResult)((int)result | (int)GlobalPlayerLoaderResult.LUAConfigError); } } } catch (Exception e) { #if DEBUG Debug.LogError(e.ToString()); #endif result = (GlobalPlayerLoaderResult)((int)result | (int)GlobalPlayerLoaderResult.LUAConfigError); } //---------------------------- 加载Cmd if (!isLua) { try { if (m_PlayerConfig != null && m_PlayerConfig.Files != null) { string cmdName = m_PlayerConfig.Files.cmd; if (string.IsNullOrEmpty(cmdName)) { cmdName = playerName; } else { cmdName = GlobalConfigMgr.GetConfigFileNameNoExt(cmdName); } string fileName = string.Format("{0}@{1}/{2}.cmd.txt", AppConfig.GetInstance().PlayerRootDir, playerName, cmdName); m_CmdConfig = new CmdConfig(); if (!m_CmdConfig.LoadFromFile(fileName)) { result = GlobalPlayerLoaderResult.CmdConfigError; } } } catch (Exception e) { #if DEBUG Debug.LogError(e.ToString()); #endif result = GlobalPlayerLoaderResult.CmdConfigError; } } //--------------------------- 最后加载cns if (!isLua) { try { if (string.IsNullOrEmpty(cnsName)) { if (m_PlayerConfig == null || m_PlayerConfig.Files == null) { cnsName = playerName; } else { cnsName = m_PlayerConfig.Files.cns; cnsName = GlobalConfigMgr.GetConfigFileNameNoExt(cnsName); } } string fileName = string.Format("{0}@{1}/{2}.cns.txt", AppConfig.GetInstance().PlayerRootDir, playerName, cnsName); m_CNSConfig = new CNSConfig(); if (!m_CNSConfig.LoadFromFile(fileName)) { //Clear(); result = (GlobalPlayerLoaderResult)((int)result | (int)GlobalPlayerLoaderResult.CNSConfigError); //return false; } } catch (Exception e) { #if DEBUG Debug.LogError(e.ToString()); #endif //Clear (); result = (GlobalPlayerLoaderResult)((int)result | (int)GlobalPlayerLoaderResult.CNSConfigError); //return false; } } if (result == GlobalPlayerLoaderResult.None) { result = GlobalPlayerLoaderResult.Ok; } return(true); }
private CmdConfig cmd; // the command configuration /// <summary> /// Initializes a new instance of the class. /// </summary> public CtrlCmd() { InitializeComponent(); cmd = null; TemplateOptions = null; }