IPCameraHelper.cs 4.4 KB
using OnlineStore.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class IPCameraHelper
    {
        //"E:\\Codes\\CSharp-Workspace\\MyProject\\WindowsService\\IPCamService\\bin\\Debug\\IPCamService.exe"
        static string appPath = AppDomain.CurrentDomain.BaseDirectory;
        static string serviceFilePath = ConfigHelper.Config.Get("IPCamService_FilePath", $"{appPath}IPCamService\\IPCamService.exe");
        static string serviceName = ConfigHelper.Config.Get("IPCamService_ServiceName", "IPCamService");
        /// <summary>
        /// 安装服务
        /// </summary>
        public static void InstallService()
        {
            if (!IsServiceExisted(serviceName))
            {
                InstallService(serviceFilePath);
                LogUtil.info("安装监控相机服务");
                ServiceStart(serviceName);
                LogUtil.info("启动监控相机服务");
            }
            else
            {
                ServiceStart(serviceName);
                LogUtil.info("启动监控相机服务");
            }
        }
        static string baseDir = ConfigHelper.Config.Get("IPCameraService_HttpServer", "http://localhost:8088");
        public static void StartRecord(string camName, string fileName = "")
        {
            Task.Factory.StartNew(delegate {
                string url = $"{baseDir}/cam/startRecord?camName={camName}&filename={fileName}";
                string res = HttpHelper.Get(url);
                LogUtil.info($"开始记录视频:{fileName},{res}");
            });

        }
        public static void StopRecord(string camName)
        {
            Task.Factory.StartNew(delegate {
                string url = $"{baseDir}/cam/stopRecord?camName={camName}";
                string res = HttpHelper.Get(url);
                LogUtil.info($"停止记录视频:{res}");
            });

        }
        //判断服务是否存在
        static bool IsServiceExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        //安装服务
        static void InstallService(string serviceFilePath)
        {
            try
            {
                using (AssemblyInstaller installer = new AssemblyInstaller())
                {
                    installer.UseNewContext = true;
                    installer.Path = serviceFilePath;
                    IDictionary savedState = new Hashtable();
                    installer.Install(savedState);
                    installer.Commit(savedState);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("安装监控相机服务失败", ex);
            }

        }

        //卸载服务
        static void UninstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                installer.Uninstall(null);
            }
        }
        //启动服务
        static void ServiceStart(string serviceName)
        {
            try
            {
                using (ServiceController control = new ServiceController(serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Stopped)
                    {
                        control.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error($"启动监控相机服务失败", ex);
            }

        }

        //停止服务
        static void ServiceStop(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }
    }
}