internal bool ConnectIot() { try { if (client.Connected) { client.Close(); } client.Connect(iotConfig.IotIp, iotConfig.IotPort); client.RegisterAll(); client.Identify(iotConfig.IotId, iotConfig.IotGroupId); return(true); } catch (SocketException ex) { NLogHelper.DefalutError(" ConnectIot SocketException:{0}", ex.Message); NLogHelper.ExceptionInfo(ex, " ConnectIot SocketException:{0}", ex.Message); return(false); } catch (Exception ex) { NLogHelper.DefalutError(" ConnectIot Exception:{0}", ex.Message); NLogHelper.ExceptionInfo(ex, " ConnectIot Exception:{0}", ex.Message); return(false); } }
bool MockDeviceData(ref bool receiveDataFlag) { NLogHelper.DefalutInfo(" MockDeviceData "); try { if (null == transmitDA) { transmitDA = new TransmitDAClient(); if (-1 != iotConfig.IotMockDAId) { transmitDA.ID = iotConfig.IotMockDAId; } } if (transmitDA.Connected) { transmitDA.Close(); } transmitDA.Connect(iotConfig.IotIp, iotConfig.IotDataAdapterPort); NLogHelper.DefalutInfo(" MockDeviceData connect success "); for (int i = 0; i < 3; i++) { if (receiveDataFlag) { break; } transmitDA.DataPackager.Device = "test_device";//deviceId; transmitDA.DataPackager.Add(i + 1, (i + 1).ToString()); transmitDA.Send(); NLogHelper.DefalutInfo(" MockDeviceData send '{0}:{0}'", (i + 1).ToString()); Thread.Sleep(3000); } transmitDA.Close(); NLogHelper.DefalutInfo(" MockDeviceData close success "); return(true); } catch (Exception ex) { NLogHelper.DefalutError(" MockDeviceData exception :{0}", ex.Message); NLogHelper.ExceptionInfo(ex, " MockDeviceData exception :{0}", ex.Message); return(false); } }
//private static string name = "DBAnalyzer"; //private static string path = Application.ExecutablePath; /// <summary> /// 注册表括操作将程序添加到启动项 /// </summary> public static void SetRegistryKey(string name, string startPath, bool Started) { try { RegistryKey HKCU = Registry.CurrentUser; RegistryKey Run = HKCU.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\"); if (Started == true) { try { Run.SetValue(name, startPath); Run.Close(); HKCU.Close(); NLogHelper.DefalutInfo("注册表修改成功,name:{0},value:{1}", name, startPath); } catch (Exception ex) { NLogHelper.DefalutError("注册表修改失败,name:{0},value:{1},errmsg:{2}", name, startPath, ex.Message); NLogHelper.ExceptionInfo(ex, "注册表修改失败,name:{0},value:{1},errmsg:{2}", name, startPath, ex.Message); } } else { if (Run.GetValue(name) != null) { Run.DeleteValue(name); Run.Close(); HKCU.Close(); } else { return; } } } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "注册表修改失败,name:{0},value:{1},errmsg:{2}", name, startPath, ex.Message); } }
internal void Init(ConfigEntity config) { try { if (null == client) { client = new DataClient(ushort.MaxValue); client.DataReceived += new DataReceived(client_DataReceived); client.MessageReceived += new MessageReceived(client_MessageReceived); client.Disconnected += new EventHandler(client_Disconnected); } if (client.Connected) { client.Close(); } iotConfig = config; } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "InitClientException:{0}", ex.Message); } }
/// <summary> /// 获取是否开机启动 /// </summary> /// <returns></returns> public static bool IsRegeditExit(string name, string startPath) { try { RegistryKey HKCU = Registry.CurrentUser; RegistryKey software = HKCU.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.OpenSubKey(@"Microsoft\Windows\CurrentVersion\Run\", true); object runObj = aimdir.GetValue(name); if (runObj == null || !startPath.Equals(runObj.ToString())) { return(false); } else { return(true); } } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "获取注册表失败,name:{0},value:{1},errmsg:{2}", name, startPath, ex.Message); } return(false); }
internal static bool StartExe(string processFileName, string processName, out string errMsg) { errMsg = string.Empty; FileInfo file = null; try { NLogHelper.DefalutInfo(" StartExe 开始运行外部程序**********"); file = new FileInfo(processFileName); if (!file.Exists) { NLogHelper.DefalutError(" StartExe not exists . file:{0}", processFileName); errMsg = "启动程序文件不存在。"; return(false); } Process[] arrayProcess = Process.GetProcessesByName(processName); foreach (Process p in arrayProcess) { p.Kill(); //if (!p.Responding) //{ // p.Kill(); //} } //启动程序休眠3秒等待资源回收 Thread.Sleep(3000); } catch (InvalidOperationException) { //程序已经退出 } catch (Exception ex) { NLogHelper.DefalutError(" StartExe 程序 '{0}' 异常", processName); NLogHelper.ExceptionInfo(ex, " StartExe 程序 '{0}' 异常{1} ", processName, ex.Message); errMsg = "启动程序异常:" + ex.Message; return(false); } try { if (file.Exists) { NLogHelper.DefalutInfo(" StartExe 启动程序:{0}", processFileName); // WinAPI_Interop.CreateProcess(file.FullName, file.DirectoryName); Process process = new Process(); process.StartInfo.FileName = processFileName; //process.StartInfo.UseShellExecute = true; process.StartInfo.CreateNoWindow = false; //process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; process.StartInfo.WorkingDirectory = file.DirectoryName; process.Start(); } NLogHelper.DefalutInfo(" StartExe 成功运行外部程序**********"); return(true); } catch (Exception ex) { NLogHelper.DefalutError(" StartExe 程序 '{0}' 异常", processName); NLogHelper.ExceptionInfo(ex, " StartExe 程序 '{0}' 异常{1} ", processName, ex.Message); errMsg = "启动程序异常:" + ex.Message; return(false); } }