Example #1
0
        void client_MessageReceived(object sender, Message message)
        {
            string content;

            if (message.Content is byte[])
            {
                content = "{" + ((byte[])message.Content).ToHexString() + "}" + "string:" + Encoding.UTF8.GetString((byte[])message.Content);
            }
            else
            {
                content = message.Content.ToString();
            }
            NLogHelper.DefalutInfo(string.Format("【消息】 来源:{0} 类别:{2} 消息:{1}", message.AddressID, content, message.Category));
        }
 void client_DataReceived(string device, int id, object value)
 {
     this.Dispatcher.Invoke(new Action(() =>
     {
         txb_iotStatus.Text       = "IOT正常运行";
         txb_iotStatus.Foreground = new SolidColorBrush(Colors.Green);
     }));
     if (timer.Enabled)
     {
         timer.Stop();
     }
     receiveDataFlag = true;
     daemonHelper.CloseIotConnected();
     NLogHelper.DefalutInfo("    IOT正常运行    client_DataReceived device:{0};id:{1};value:{2}", device, id.ToString(), value.ToString());
 }
        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 void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer.Stop();

            if (receiveDataFlag)
            {
                NLogHelper.DefalutInfo("IOT正常运行。");
                this.Dispatcher.Invoke(new Action(() =>
                {
                    txb_iotStatus.Text       = "IOT正常运行";
                    txb_iotStatus.Foreground = new SolidColorBrush(Colors.Green);
                }));
                return;
            }

            //没有接受到数据,重启iot
            StartIotProcess();
        }
        void DaemonThreadExecute()
        {
            while (true)
            {
                NLogHelper.DefalutInfo("开始检测IOT运行情况。");

                receiveDataFlag = false;

                this.Dispatcher.Invoke(new Action(() =>
                {
                    txb_testIotTime.Text     = DateTime.Now.ToString("G");
                    txb_iotStatus.Text       = "正在检测中……";
                    txb_iotStatus.Foreground = new SolidColorBrush(Colors.DarkBlue);
                }));



                bool bConnectIot = daemonHelper.ConnectIot();

                if (!bConnectIot)
                {
                    NLogHelper.DefalutInfo("DataClient连接IOT失败,重启IOT。");
                    //连接失败重启iot
                    StartIotProcess();
                    Thread.Sleep(10000);
                    continue;
                }

                bool bMockRet = MockDeviceData(ref receiveDataFlag);
                if (!bMockRet)
                {
                    NLogHelper.DefalutInfo("MockDeviceData 失败,重启IOT。");
                    StartIotProcess();
                    Thread.Sleep(10000);
                    continue;
                }

                if (!timer.Enabled && !receiveDataFlag)
                {
                    timer.Start();
                }
                Thread.Sleep(iotConfig.RateIotTime * 1000);
            }
        }
Example #6
0
        //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 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);
            }
        }