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
{
///
/// 创建控制句柄
///
public class ControlIntPtr
{
private static ILog logger = LogManager.GetLogger("AppInfoLog");
///
/// 句柄集合
///
public static Dictionary dic = new Dictionary();
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;
}
///
/// 创建句柄
///
///
public bool init()
{
result = null;
resultEvent = new AutoResetEvent(false);
receive = new CmdSendService.GycpReceive(ReceiveCallback); ;
using (wisdom_cs_entity ctx = new wisdom_cs_entity())
{
IList 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;
}
}
///
/// 销毁句柄
///
public void Destroy()
{
foreach (GycpServer server in this.servers)
{
CmdSendService.gycp_destroy(server.Gycp);
}
}
///
/// 回调函数
///
/// 指令类型
/// 指令地址
/// 错误码
/// 指令长度
/// 句柄
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 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 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;
}
}