using BPASmartClient.Helper;
using BPASmartClient.Message;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace BPASmartClient.JakaRobot
{
public class JaKaHelper
{
#region 果汁机设备
/*public const string SENCE_取咖啡杯 = "10000";
public const string SENCE_取果汁杯 = "10001";
public const string SENCE_取茶水杯 = "10002";
public const string SENCE_取水杯 = "10003";
public const string SENCE_取咖啡杯检测 = "11000";
public const string SENCE_取果汁杯检测 = "11001";
public const string SENCE_取茶水杯检测 = "11002";
public const string SENCE_取水杯检测 = "11003";
public const string SENCE_接咖啡 = "12000";
public const string SENCE_接果汁1 = "12001";
public const string SENCE_接果汁2 = "12002";
public const string SENCE_接果汁3 = "12003";
public const string SENCE_接果汁4 = "12004";
public const string SENCE_接茶 = "12005";
public const string SENCE_接水 = "12006";
public const string SENCE_放咖啡杯 = "13000";
public const string SENCE_放果汁杯1 = "13001";
public const string SENCE_放果汁杯2 = "13002";
public const string SENCE_放果汁杯3 = "13003";
public const string SENCE_放果汁杯4 = "13004";
public const string SENCE_放茶水杯 = "13005";
public const string SENCE_放水杯 = "13006";*/
//public const string SENCE_取杯 = "10000";
//public const string SENCE_放杯位检测 = "10100";
//public const string SENCE_取杯检测 = "11000";
//public const string SENCE_接咖啡 = "12000";
//public const string SENCE_接果汁1 = "12001";
//public const string SENCE_接果汁2 = "12002";
//public const string SENCE_接果汁3 = "12003";
//public const string SENCE_接果汁4 = "12004";
//public const string SENCE_接茶 = "12005";
//public const string SENCE_接水 = "12006";
//public const string SENCE_接茶_接水 = "12007";
//public const string SENCE_放咖啡杯 = "13000";//接饮料位---放饮料过渡位
//public const string SENCE_放果汁杯1 = "13001";
//public const string SENCE_放果汁杯2 = "13002";
//public const string SENCE_放果汁杯3 = "13003";
//public const string SENCE_放果汁杯4 = "13004";
//public const string SENCE_放茶水杯 = "13005";
//public const string SENCE_放杯 = "14000";//放杯过渡位到-放杯
//public const string SENCE_放杯检测 = "15000";
//public const string SENCE_初始位 = "20000";
#endregion
public int rshd = -1;
private bool login = false;
///
/// 机器人的状态
///
private JKTYPE.RobotStatus robot_Status;
private JKTYPE.ProgramState program_status;
//private volatile static JaKaHelper _Instance;
//public static JaKaHelper GetInstance => _Instance ?? (_Instance = new JaKaHelper());
//private JaKaHelper() { }
public bool IsIdle { get; set; } = false;
public bool IsConnected { get { return login; } }
public bool IsDeviceFirstInit { get; set; }
private string IpAddress { get; set; }
private bool IsPingOk { get; set; }
public void Conn(string ip)
{
jakaAPI.create_handler(ip.ToCharArray(), ref rshd);
login = true;
}
public void Connect(string ip)
{
MessageLog.GetInstance.Show("节卡机器人连接中。。。。。");
IpAddress = ip;
Task.Factory.StartNew(() =>
{
while (!IsPingOk)
{
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
myPing.SendAsync(IpAddress, 1000, null);
Thread.Sleep(3000);
}
});
//bool ErrorFlag = false;
//ThreadManage.GetInstance().StartLong(new Action(() =>
//{
// if (rshd == -1 || login == false)
// {
// try
// {
// jakaAPI.create_handler(ip.ToCharArray(), ref rshd);
// login = true;
// }
// catch (Exception ex)
// {
// if (!ErrorFlag)
// {
// MessageLog.GetInstance.ShowEx(ex.ToString());
// ErrorFlag = true;
// login = false;
// }
// Thread.Sleep(3000);
// }
// }
// if (login && IsDeviceFirstInit == false)
// {
// try
// {
// Power_On();//打开机器人电源
// Thread.Sleep(1000);
// Enable_robot();//机器人上使能
// IsDeviceFirstInit = true;
// MessageLog.GetInstance.Show("机器人已上电使能");
// }
// catch (Exception ex)
// {
// MessageLog.GetInstance.ShowEx("机器人未完成上电和使能");
// }
// }
// Thread.Sleep(5000);
//}), $"节卡机器人连接", true);
}
private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
{
if (e.Reply != null && e.Reply.Status == IPStatus.Success)
{
IsPingOk = true;
MessageLog.GetInstance.Show("节卡机器人物理网络准备就绪");
Thread.Sleep(3000);
bool ErrorFlag = false;
ThreadManage.GetInstance().StartLong(new Action(() =>
{
if (rshd == -1 || login == false)
{
try
{
int result = jakaAPI.create_handler(IpAddress.ToCharArray(), ref rshd);
login = true;
MessageLog.GetInstance.Show($"节卡机器人创建连接成功,句柄:{rshd},返回值:{result}");
}
catch (Exception ex)
{
if (!ErrorFlag)
{
MessageLog.GetInstance.ShowEx(ex.ToString());
ErrorFlag = true;
login = false;
}
Thread.Sleep(3000);
}
}
if (login && IsDeviceFirstInit == false)
{
try
{
MessageLog.GetInstance.Show("节卡机器人打开电源准备");
Thread.Sleep(2000);
Power_On();//打开机器人电源
Thread.Sleep(1000);
MessageLog.GetInstance.Show("节卡机器人上使能准备");
Enable_robot();//机器人上使能
IsDeviceFirstInit = true;
MessageLog.GetInstance.Show("机器人已上电使能");
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx("机器人未完成上电和使能");
}
}
Thread.Sleep(5000);
}), $"节卡机器人连接", true);
}
}
public void Power_On()
{
if (login)
{
try
{
jakaAPI.power_on(ref rshd);
}
catch (System.AccessViolationException ave)
{
MessageLog.GetInstance.ShowEx(ave.ToString());
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx(ex.ToString());
}
}
else
{
MessageLog.GetInstance.Show("jaka机器人未连接成功!");
}
}
public void Power_Off()
{
if (login)
{
try
{
jakaAPI.power_off(ref rshd);
}
catch (Exception ex)
{
}
}
else
{
MessageLog.GetInstance.Show("jaka机器人未连接成功!");
}
}
public void Enable_robot()
{
if (!(rshd == -1))
{
try
{
jakaAPI.enable_robot(ref rshd);
}
catch (Exception ex)
{
}
}
else
{
MessageLog.GetInstance.Show("jaka机器人未连接成功!");
}
}
public void disEnable_robot()
{
if (!(rshd == -1))
{
try
{
jakaAPI.disable_robot(ref rshd);
}
catch (Exception ex)
{
}
}
else
{
MessageLog.GetInstance.Show("jaka机器人未连接成功!");
}
}
///
/// 设置机器人的DO值
///
///
///
///
public bool Set_RobotDO(int DOIndex, bool value)
{
try
{
jakaAPI.set_digital_output(ref rshd, JKTYPE.IOType.IO_CABINET, DOIndex, value);
return value;
}
catch (Exception ex)
{
return false;
}
}
///
/// 设置机器人的AO值
///
/// 设置值
///
public int Set_RobotAO1(int Value)
{
try
{
return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, Value);
}
catch (Exception ex)
{
return 0;
}
}
///
/// 设置机器人的AO值
///
/// 索引值
/// 设置值
///
public int Set_RobotAO(int Index, int Value)
{
try
{
return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, Value);
}
catch (Exception ex)
{
return 0;
}
}
///
/// 获取机器人的AO值
///
/// 索引值
/// 设置值
///
public float Get_RobotAO(int Index, int value)
{
try
{
float bResult = 0;
jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, ref bResult);
return bResult;
}
catch (Exception ex)
{
return 0;
}
}
///
/// 获取机器人的AO值
///
/// 索引值
/// 设置值
///
public float Get_RobotAO1()
{
try
{
float bResult = 0;
jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, ref bResult);
return bResult;
}
catch (Exception ex)
{
return 0;
}
}
///
/// 获取机器人的DI输入
///
///
///
public bool Get_RobotDI(int index)
{
bool bResult = false;
jakaAPI.get_digital_input(ref rshd, JKTYPE.IOType.IO_CABINET, index, ref bResult);
return bResult;
}
///
/// 获取机器人的状态
///
public void GetRobotStatus()
{
jakaAPI.get_robot_status(ref rshd, ref robot_Status);
}
public JKTYPE.ProgramState GetProgramStatus()
{
jakaAPI.get_program_state(ref rshd, ref program_status);
return program_status;
}
StringBuilder jakafile;
JKTYPE.ProgramState status;
char[] file;
public void JaKaProgramName(String Programname)
{
try
{
//加载
p: file = Programname.ToCharArray();
MessageLog.GetInstance.Show($"调用文件名:{Programname}");
status = new JKTYPE.ProgramState();
jakaAPI.get_program_state(ref rshd, ref status);
if (status != JKTYPE.ProgramState.PROGRAM_IDLE)
{
MessageLog.GetInstance.Show($"程序运行中,无法加载程序!!!");
goto p;
}
else
{
if (jakaAPI.program_load(ref rshd, file) == 0)
{
MessageLog.GetInstance.Show($"加载程序完成");
}
else
{
MessageLog.GetInstance.Show($"加载程序失败!!!");
goto p;
}
}
if (status == JKTYPE.ProgramState.PROGRAM_RUNNING)
{
MessageLog.GetInstance.Show($"程序处于已处于启动状态!!!");
goto p;
}
else if (status == JKTYPE.ProgramState.PROGRAM_IDLE)
{
jakafile = new StringBuilder();
jakaAPI.get_loaded_program(ref rshd, jakafile);
if (jakafile.Length == 0)
{
MessageLog.GetInstance.Show($"未加载程序,无法运行!!!");
goto p;
}
else
{
MessageLog.GetInstance.Show($"当前加载程序为:" + jakafile);
jakaAPI.program_run(ref rshd);
MessageLog.GetInstance.Show($"程序启动");
}
}
else
{
MessageLog.GetInstance.Show($"程序处于暂停状态,无法重新启动程序!!!");
}
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx(ex.ToString());
}
}
}
}