diff --git a/DigitalSchoolApi/Controllers/HKAttendanceController.cs b/DigitalSchoolApi/Controllers/HKAttendanceController.cs index 68f39f3..769761e 100644 --- a/DigitalSchoolApi/Controllers/HKAttendanceController.cs +++ b/DigitalSchoolApi/Controllers/HKAttendanceController.cs @@ -6,10 +6,16 @@ using System.Data.SqlClient; using System.Diagnostics.Eventing.Reader; using System.IO; using System.Linq; +using System.Net; +using System.Net.Http; using System.Runtime.InteropServices; +using System.Text; using System.Web; using AlarmCSharpDemo; +using Antlr.Runtime.Misc; using Dapper; +using Org.BouncyCastle.Asn1.X509; +using Org.BouncyCastle.Ocsp; namespace DigitalSchoolApi.Controllers { @@ -1114,6 +1120,103 @@ namespace DigitalSchoolApi.Controllers } } + public static string OpenDoor(string id) + { + using (IDbConnection connmis = new SqlConnection(_misConnection)) + { + //获取设备 + var device = connmis.QueryFirstOrDefault($"select * from ADR_Device where F_EnabledMark=1 and Id='{id}'"); + if(device!=null) + { + string url = $"http://{device.IpAddress}/ISAPI/AccessControl/RemoteControl/door/1"; + string username = device.AdminAccount; + string password = device.AdminPwd; + string data = $"open {device.AdminPwd}11monitor11true101"; + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "PUT"; + request.ContentType = "application/xml"; + request.Credentials = new NetworkCredential(username, password); + byte[] byteArray = Encoding.UTF8.GetBytes(data); + request.ContentLength = byteArray.Length; + Stream dataStream = request.GetRequestStream(); + dataStream.Write(byteArray, 0, byteArray.Length); + dataStream.Close(); + HttpWebResponse response; + + try + { + response = (HttpWebResponse)request.GetResponse(); + } + catch (WebException ex) + { + response = (HttpWebResponse)ex.Response; + } + //Console.WriteLine("Response status: {0}", response.StatusDescription); + dataStream = response.GetResponseStream(); + StreamReader reader = new StreamReader(dataStream); + + string responseFromServer = reader.ReadToEnd(); + + //Console.WriteLine(responseFromServer); + reader.Close(); + dataStream.Close(); + + response.Close(); + return "开门成功"; + } + return "未找到设备"; + } + + + } + + + public static void CloseDoor(string id) + { + using (IDbConnection connmis = new SqlConnection(_misConnection)) + { + //获取设备 + var device = connmis.QueryFirstOrDefault($"select * from ADR_Device where F_EnabledMark=1 and Id='{id}'"); + if (device != null) + { + string url = $"http://{device.IpAddress}/ISAPI/AccessControl/RemoteControl/door/1"; + string username = device.AdminAccount; + string password = device.AdminPwd; + string data = $"close {device.AdminPwd}11monitor11true101"; + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "PUT"; + request.ContentType = "application/xml"; + request.Credentials = new NetworkCredential(username, password); + byte[] byteArray = Encoding.UTF8.GetBytes(data); + request.ContentLength = byteArray.Length; + Stream dataStream = request.GetRequestStream(); + dataStream.Write(byteArray, 0, byteArray.Length); + dataStream.Close(); + HttpWebResponse response; + + try + { + response = (HttpWebResponse)request.GetResponse(); + } + catch (WebException ex) + { + response = (HttpWebResponse)ex.Response; + } + //Console.WriteLine("Response status: {0}", response.StatusDescription); + dataStream = response.GetResponseStream(); + StreamReader reader = new StreamReader(dataStream); + + string responseFromServer = reader.ReadToEnd(); + + //Console.WriteLine(responseFromServer); + reader.Close(); + dataStream.Close(); + + response.Close(); + } + } + } + #region 考勤结构体 /// /// 考勤设备日志 diff --git a/DigitalSchoolApi/Controllers/YKTController.cs b/DigitalSchoolApi/Controllers/YKTController.cs index 7ef5251..5bc2f32 100644 --- a/DigitalSchoolApi/Controllers/YKTController.cs +++ b/DigitalSchoolApi/Controllers/YKTController.cs @@ -6,6 +6,8 @@ using System.Data.SqlClient; using System.Linq; using System.Net; using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; using System.Web.Http; using Dapper; using DigitalSchoolApi.Models; @@ -14,7 +16,7 @@ using Learun.Application.Organization; namespace DigitalSchoolApi.Controllers { - public class YKTController : BaseController + public class YKTController : BaseController { private readonly static string _admsConnection = ConfigurationManager.ConnectionStrings["CoreDBString"].ConnectionString; private readonly static string _misConnection = ConfigurationManager.ConnectionStrings["ConnectionPfcMisDBString"].ConnectionString; @@ -273,6 +275,25 @@ namespace DigitalSchoolApi.Controllers RecurringJob.RemoveIfExists("LeaderAttendance"); return Ok(); } + /// + /// 开门(延迟5秒后调用关门) + /// + /// + public IHttpActionResult OpenCloseDoor(string id,bool c) + { + var r = HKAttendanceController.OpenDoor(id); + + if (c) + { + Task.Factory.StartNew(() => + { + + Thread.Sleep(5000); + HKAttendanceController.CloseDoor(id); + }); + } + return Ok(r); + } #region 长阳报名\晚归预警 /// @@ -590,7 +611,7 @@ namespace DigitalSchoolApi.Controllers () => TLMSchoolController.AssignSendEmail(entityList), Cron.Daily(entityList[0].F_Hour.Value), TimeZoneInfo.Local); } - + } } }