123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Metadata.Edm;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using EntityDataSet;
- using IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd;
- using log4net;
- namespace IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc
- {
- /// <summary>
- /// 创建控制句柄
- /// </summary>
- public class ControlIntPtr
- {
- private static ILog logger = LogManager.GetLogger("AppInfoLog");
- /// <summary>
- /// 句柄集合
- /// </summary>
- public static Dictionary<string, IntPtr> dic = new Dictionary<string, IntPtr>();
- private ArrayList servers = new ArrayList();
- private static ControlIntPtr controlIntPtr = new ControlIntPtr();
- public static ControlResult result = new ControlResult();
- private static CmdSendService.GycpReceive receive = null;
- private static AutoResetEvent resultEvent;
- private byte[] tmp_types;
- int[] tmp_addrs;
- private int[] tmp_errs;
- private ControlIntPtr()
- {
- }
- public static ControlIntPtr GetControlIntPtr()
- {
- return controlIntPtr;
- }
- /// <summary>
- /// 创建句柄
- /// </summary>
- /// <returns></returns>
- public bool init()
- {
- result = null;
- resultEvent = new AutoResetEvent(false);
- receive = new CmdSendService.GycpReceive(ReceiveCallback); ;
- using (wisdom_cs_entity ctx = new wisdom_cs_entity())
- {
- IList<gycp_address> list = ctx.gycp_address.Select(s => s).ToList();
- for (int i = 0; i < list.Count; i++)
- {
- GycpServer data = new GycpServer();
- data.Id = list[i].id;
- data.ProjectId = list[i].project_id;
- data.Ip = list[i].ip;
- data.Port = (ushort)list[i].port;
- servers.Add(data);
- }
- if (servers == null)
- {
- return false;
- }
- foreach (GycpServer server in this.servers)
- {
- try
- {
- server.Gycp = CmdSendService.gycp_create(server.Ip, server.Port, 30, 30, 20, receive, server.Gycp);
- // server.Gycp = CmdSendService.gycp_create("172.168.1.107", (ushort)9901, 30, 30, 20, receive, server.Gycp);
- //将控制句柄添加至字典
- dic.Add(server.ProjectId, server.Gycp);
- logger.Info("创建句柄成功:" + server.Ip + ":" + server.Port);
- }
- catch (Exception ex)
- {
- logger.Info("创建控制句柄失败" + server.Ip + "," + server.Port + ":" + ex);
- }
- }
- return true;
- }
- }
- /// <summary>
- /// 销毁句柄
- /// </summary>
- public void Destroy()
- {
- foreach (GycpServer server in this.servers)
- {
- CmdSendService.gycp_destroy(server.Gycp);
- }
- }
- /// <summary>
- /// 回调函数
- /// </summary>
- /// <param name="types">指令类型</param>
- /// <param name="addrs">指令地址</param>
- /// <param name="errors">错误码</param>
- /// <param name="length">指令长度</param>
- /// <param name="arg">句柄</param>
- public static void ReceiveCallback(IntPtr types, IntPtr addrs, IntPtr errors, uint length, IntPtr arg)
- {
- #region
- //控制命令错误码,0-发送成功,1--网络错误,2--modbus地址错误或不存在,-1无返回错误码,一般为服务错误,100为返回数据中命令地址编号找不到对应风机控制命令错误码
- //try
- //{
- // if (addrs != null && errors != null && length != 0)
- // {
- // byte[] tmp_types = new byte[length];
- // int[] tmp_addrs = new int[length];
- // int[] tmp_errs = new int[length];
- // Marshal.Copy(types, tmp_types, 0, tmp_types.Length);
- // Marshal.Copy(addrs, tmp_addrs, 0, tmp_addrs.Length);
- // Marshal.Copy(errors, tmp_errs, 0, tmp_errs.Length);
- // result = new ControlResult();
- // result.Types = tmp_types;
- // result.Addresses = tmp_addrs;
- // result.Errors = tmp_errs;
- // for (int i = 0; i < result.Addresses.Length; i++)
- // {
- // IList<string> keyList = TaskQueueSvc.dicTask.Keys.ToList();
- // for (int j = 0; j < keyList.Count; j++)
- // {
- // var task = TaskQueueSvc.dicTask[keyList[j]];
- // if (task.CmdId == result.Addresses[i])
- // {
- // //控制命令发送时间不为空
- // //重置命令发送时间为空
- // //控制命令错误码为空
- // //控制命令是否成功字段为空
- // //以上条件代表 控制命令 回调信息
- // if (task.SendControlTime.HasValue && !task.SendResetTime.HasValue && !task.SendControlErrorInfo.HasValue && !task.IsSendControlSuccess.HasValue)
- // {
- // task.SendControlErrorInfo = result.Errors[i];
- // if (result.Errors[i] == 0)
- // task.IsSendControlSuccess = true;
- // else
- // task.IsSendControlSuccess = false;
- // }
- // //控制命令发送时间不为空
- // //控制命令错误码不为空
- // //控制命令是否成功不为空
- // //重置命令发送时间不为空
- // //重置命令错误码为空
- // //重置命令是否成功为空
- // // 以上条件满足,则代表105机型重置命令 发送后的回调信息
- // else if (task.SendControlTime.HasValue && task.SendControlErrorInfo.HasValue && task.IsSendControlSuccess.HasValue && task.SendResetTime.HasValue && !task.SendResetlErrorInfo.HasValue && !task.IsSendResetSuccess.HasValue)
- // {
- // task.SendResetlErrorInfo = result.Errors[i];
- // if (result.Errors[i] == 0)
- // task.IsSendResetSuccess = true;
- // else
- // task.IsSendResetSuccess = false;
- // }
- // }
- // }
- // }
- // Console.WriteLine("回调成功" + tmp_addrs + "||" + tmp_errs + "||" + tmp_errs);
- // }
- //}
- //catch (Exception e)
- //{
- // Console.WriteLine("回调异常");
- //}
- #endregion
- #region
- //控制命令错误码,0-发送成功,1--网络错误,2--modbus地址错误或不存在,-1无返回错误码,一般为服务错误,100为返回数据中命令地址编号找不到对应风机控制命令错误码
- try
- {
- if (addrs != null && errors != null && length != 0)
- {
- byte[] tmp_types = new byte[length];
- int[] tmp_addrs = new int[length];
- int[] tmp_errs = new int[length];
- Marshal.Copy(types, tmp_types, 0, tmp_types.Length);
- Marshal.Copy(addrs, tmp_addrs, 0, tmp_addrs.Length);
- Marshal.Copy(errors, tmp_errs, 0, tmp_errs.Length);
- result = new ControlResult();
- result.Types = tmp_types;
- result.Addresses = tmp_addrs;
- result.Errors = tmp_errs;
- for (int i = 0; i < tmp_addrs.Length; i++)
- {
- Console.WriteLine("命令地址:"+tmp_addrs[i]+"错误码"+tmp_errs[i]);
- }
- for (int i = 0; i < result.Addresses.Length; i++)
- {
- IList<string> keyList = TaskQueueSvc.dicTask.Keys.ToList();
- for (int j = 0; j < keyList.Count; j++)
- {
- var task = TaskQueueSvc.dicTask[keyList[j]];
- if (task.CmdId == result.Addresses[i])
- {
- //控制命令发送时间不为空
- //重置命令发送时间为空
- //控制命令错误码为空
- //控制命令是否成功字段为空
- //以上条件代表 控制命令 回调信息
- if (task.SendControlTime.HasValue && !task.SendResetTime.HasValue && !task.SendControlErrorInfo.HasValue && !task.IsSendControlSuccess.HasValue)
- {
- task.SendControlErrorInfo = result.Errors[i];
- if (result.Errors[i] == 0)
- task.IsSendControlSuccess = true;
- else
- task.IsSendControlSuccess = false;
- using (wisdom_cs_entity ctx = new wisdom_cs_entity())
- {
- control_log log =
- ctx.control_log.Where(s => s.id == task.LogId).FirstOrDefault();
- if (log != null)
- {
- log.send_control_error_info = result.Errors[i].ToString();
- if (result.Errors[i] == 0)
- log.is_send_control_success = true;
- else
- log.is_send_control_success = false;
- ctx.Entry(log).State = EntityState.Modified;
- ctx.SaveChanges();
- }
- }
- }
- //控制命令发送时间不为空
- //控制命令错误码不为空
- //控制命令是否成功不为空
- //重置命令发送时间不为空
- //重置命令错误码为空
- //重置命令是否成功为空
- // 以上条件满足,则代表105机型重置命令 发送后的回调信息
- else if (task.SendControlTime.HasValue && task.SendControlErrorInfo.HasValue && task.IsSendControlSuccess.HasValue && task.SendResetTime.HasValue && !task.SendResetlErrorInfo.HasValue && !task.IsSendResetSuccess.HasValue)
- {
- task.SendResetlErrorInfo = result.Errors[i];
- if (result.Errors[i] == 0)
- task.IsSendResetSuccess = true;
- else
- task.IsSendResetSuccess = false;
- using (wisdom_cs_entity ctx = new wisdom_cs_entity())
- {
- control_log log =
- ctx.control_log.Where(s => s.id == task.LogId).FirstOrDefault();
- if (log != null)
- {
- log.send_reset_error_info = result.Errors[i].ToString();
- if (result.Errors[i] == 0)
- log.is_send_reset_success = true;
- else
- log.is_send_reset_success = false;
- ctx.Entry(log).State = EntityState.Modified;
- ctx.SaveChanges();
- }
- }
- }
- }
- }
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("回调异常");
- }
- #endregion
- }
- }
- public class GycpServer
- {
- public int Id { set; get; }
- public string ProjectId { set; get; }
- public string Ip { set; get; }
- public ushort Port { set; get; }
- public IntPtr Gycp;
- }
- }
|