Blame view

web/WebMvc/Controllers/PingToolController.cs 2.35 KB
赖素文 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;using System.Collections.Generic;using System.Net.NetworkInformation;
using System.Text;using Hh.Mes.Common.Json;using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service;using Hh.Mes.Service.SystemAuth;
using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebMvc{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public  class PingToolController : BaseController
    {

        public PingToolController(IAuth authUtil,IHttpContextAccessor accessor):base(authUtil)
        {
            this.context = accessor.HttpContext;
        }

        /// <summary>
        /// Ping数据 http://127.0.0.1:6001/api/PingTool/PingIP
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("PingIP")]
        public string PingIP(string ip)
        {            
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                string host = ip;
                Ping p1 = new Ping();
                PingReply reply = null;
                try
                {
                    if (Program.PingTimeout > 0)
                    {
                        reply = p1.Send(host, Program.PingTimeout);
                    }
                    else
                    {
                        reply = p1.Send(host);
                    }
                }
                catch (Exception ex)
                {
                    response.Result = "异常";
                    return Serialize(response);
                }

                string result = "未知";
                if (reply.Status == IPStatus.Success)
                {
                    result = "成功";
                }
                else if (reply.Status == IPStatus.TimedOut)
                {
                    result = "超时";
                }
                else
                {
                    result = "失败";
                }
                response.Result = result;
                return Serialize(response);
            });
        }
    }}