public bool SendMessage(string sendMessage) { if (!sendMessage.Equals(lastMessage)) { AGVLog.WriteSendInfo(sendMessage, new StackFrame(true)); } else { } lastMessage = sendMessage; Socket msock; try { msock = getTcpClient().Client; byte[] data = Encoding.ASCII.GetBytes(sendMessage); DBDao.getDao().InsertConnectMsg(sendMessage, "SendMessage"); msock.Send(data); if (!"发送消息成功".Equals(lastMsgAboutSend)) { lastMsgAboutSend = "发送消息成功"; AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true)); } return(true); } catch (Exception se) { lastMsgAboutSend = "发送消息错误" + se.Message + ",系统稍后将重新连接AGV"; AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true)); Closeclient(); return(false); } }
/// <summary> /// 发送任务 /// </summary> public int sendTask(TaskRecord tr) { int result = 0; Console.WriteLine("ready to send task: " + tr.singleTask.taskName + "forklist stat:" + getForkLift().taskStep + "forklift finished:" + getForkLift().finishStatus); string cmd = "cmd=set task by name;name=" + tr.taskRecordName; //发送命令格式,如果有多个对应值用;隔开,如果后面没有命令了,不需要再加;号 Console.WriteLine("send msg :" + cmd + "to " + getForkLift().forklift_number); lock (getAGVSocketClient().clientLock) { try { getAGVSocketClient().SendMessage(cmd); //确保发送成功 tr.taskRecordStat = TASKSTAT_T.TASK_SEND; tr.singleTask.taskStat = TASKSTAT_T.TASK_SEND; FormController.getFormController().getMainFrm().updateFrm(); //设置更新界面 tr.forkLiftWrapper = this; getForkLift().taskStep = TASK_STEP.TASK_SENDED; getForkLift().currentTask = tr.singleTask.taskText; DBDao.getDao().UpdateTaskRecord(tr); DBDao.getDao().updateForkLift(this); //更新车子状态 } catch (Exception ex) { Console.WriteLine(ex.ToString()); AGVLog.WriteError("发送" + tr.singleTask.taskText + " 任务到" + getForkLift().forklift_number + "号车 失败", new StackFrame(true)); result = -1; } AGVLog.WriteError("发送" + tr.singleTask.taskText + " 任务到" + getForkLift().forklift_number + "号车 成功", new StackFrame(true)); return(result); } }
private void ServerService() { try { checkClientSocket(); while (true) { Console.WriteLine("read to send"); ForkLiftWrapper forklift = AGVCacheData.getForkLiftByID(3); StringBuilder sb = new StringBuilder(); sb.Append("battery_soc="); sb.Append(forklift.getBatteryInfo().getBatterySoc() + ";"); sb.Append("agvMessage="); sb.Append((int)AGVMessageHandler.getMessageHandler().getMessage().getMessageType()); Console.WriteLine(" send data = " + sb.ToString()); AGVLog.WriteError(" send data = " + sb.ToString(), new StackFrame(true)); byte[] byteData = Encoding.ASCII.GetBytes(sb.ToString()); DBDao.getDao().InsertConnectMsg(sb.ToString(), "ServerService"); clientSocket.Send(byteData); Thread.Sleep(10000); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } serverFuncOK = true; turnToMainThread(); }
private void ClientService() { checkClientSocket(); string data = null; byte[] bytes = new byte[4096]; Console.WriteLine("new user"); try { Console.WriteLine("read to receive"); while ((i = clientSocket.Receive(bytes)) != 0) { if (i < 0) { break; } Console.WriteLine(i); data = Encoding.ASCII.GetString(bytes, 0, i); DBDao.getDao().InsertConnectMsg(data, "ClientService"); if (data.IndexOf("<AGV>") > -1) { handleMessage(data); } } Thread.Sleep(10); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } clientFuncOK = true; turnToMainThread(); }
/// <summary> /// 添加任务记录 /// </summary> public void addTaskRecord(TASKSTAT_T stat, SingleTask st) { /*TaskRecord tr = new TaskRecord(); * tr.taskRecordStat = taskRecordStat; * tr.singleTask = st; * tr.taskRecordName = st.taskName;*/ DBDao.getDao().InsertTaskRecord(stat, st); }
public void topTaskRecord(TaskRecord tr) { int count = 0; count = DBDao.getDao().selectMaxBySql("select max(taskLevel) from taskrecord"); //查询所有被置过顶的任务 tr.taskLevel = count + 1; DBDao.getDao().UpdateTaskRecord(tr); }
public void setSingleTaskByTaskName(string taskName) { SingleTask st = DBDao.getDao().SelectSingleTaskByName(taskName); Console.WriteLine("TaskRecord taskName = " + taskName); if (st != null) { this.singleTask = st; } else { Console.WriteLine("TaskRecord Set Name error"); } }
private void receive(object fl) { //ForkLiftWrapper forklift = new ForkLiftWrapper(); //forklift.setForkLift((ForkLiftItem)fl); byte[] buffer = new byte[512]; Socket msock; TcpClient vClient = null; Console.WriteLine("receive ConnectStatus: " + connectStatus); if (connectStatus == false) //检查连接状态 { return; } while (isRecvMsgFlag) { try { vClient = getTcpClient(); msock = tcpClient.Client; Array.Clear(buffer, 0, buffer.Length); tcpClient.GetStream(); int bytes = msock.Receive(buffer); string receiveStr = Encoding.ASCII.GetString(buffer).Trim(); CommandService.getInstance().setLatestMsgFromClient(receiveStr); DBDao.getDao().InsertConnectMsg(receiveStr, "receive"); readTimeOutTimes = 0; //读取超时次数清零 if (hrmCallback != null) { hrmCallback(forkLiftWrapper.getForkLift().id, buffer, bytes); } } catch (SocketException ex) { if (ex.ErrorCode == 10060 && readTimeOutTimes < 10) //超时次数超过10次,关闭socket进行重连 { AGVLog.WriteWarn("read msg timeout", new StackFrame(true)); Console.WriteLine("read msg timeout"); readTimeOutTimes++; continue; } AGVLog.WriteError("读取消息错误" + ex.ErrorCode, new StackFrame(true)); Console.WriteLine("recv msg client close" + ex.ErrorCode); Closeclient(); } catch (Exception ex) { Closeclient(); } } }
public void Sendbuffer(byte[] buffer) { if (tcpClient == null || connectStatus == false) //检查连接状态 { return; } Socket msock; try { msock = tcpClient.Client; DBDao.getDao().InsertConnectMsg(Encoding.ASCII.GetString(buffer), "Sendbuffer"); msock.Send(buffer); } catch (Exception ex) { AGVLog.WriteError("发送消息错误" + ex.Message, new StackFrame(true)); Console.WriteLine("send message error"); Closeclient(); } }
public void SendMessage(string sendMessage) { AGVLog.WriteSendInfo(sendMessage, new StackFrame(true)); Socket msock; try { if (tcpClient == null || connectStatus == false) { Exception ex = new Exception("connect err"); throw (ex); } msock = tcpClient.Client; byte[] data = Encoding.ASCII.GetBytes(sendMessage); DBDao.getDao().InsertConnectMsg(sendMessage, "SendMessage"); msock.Send(data); } catch (Exception se) { AGVLog.WriteError("发送消息错误" + se.Message, new StackFrame(true)); Console.WriteLine("send message error" + se.Message); Closeclient(); } }
/*处理车子反馈报文 msg格式cmd=position;battery=%d;error=%d;x=%d;y=%d;a=%f;z=%d; * speed=%d;task=%s;veer_angle=%f; * task_step=%d;task_isfinished=%d;task_error=%d;walk_path_id=%d */ /// <summary> /// /// </summary> /// <param name="id">表示车子</param> /// <param name="msg"></param> /// <returns></returns> private void handleForkLiftMsg(int id, byte[] buffer, int length) { int pos = -1; int pos_e = -1; int pos_t = -1; string taskName = ""; int x = 0; //车子横坐标 int y = 0; //车子纵坐标 int pause_stat = -1; //默认是错误状态 int battery_soc = -1; int finish_stat = -1; int gAlarm = 1; //AGV防撞信号 默认1 表示没有报警 string msg = parseForkLiftMsg(buffer, length); if (string.IsNullOrEmpty(msg)) { AGVLog.WriteError("msg is null", new StackFrame(true)); return; } if (!(msg.StartsWith("cmd=position;") && msg.EndsWith("?"))) { AGVLog.WriteError("msg is error patten", new StackFrame(true)); return; } lastMsg = msg; CommandService.getInstance().setLatestMsgFromClient(msg); DBDao.getDao().InsertConnectMsg(msg, "receive"); //Console.WriteLine("msg = " + msg); //解析taskName try { //if (id == 2) // AGVLog.WriteError(msg, new StackFrame(true)); pos_t = msg.IndexOf("task="); if (pos_t != -1) { pos_e = msg.Substring(pos_t, msg.Length - pos_t).IndexOf(";"); if (pos_e != -1) { taskName = msg.Substring(pos_t + 5, pos_e - 5); //AGVLog.WriteInfo("forklift taskName = " + taskName, new StackFrame(true)); //Console.WriteLine("forklift taskName = " + taskName); } } if (string.IsNullOrEmpty(taskName)) { //AGVLog.WriteError("forklift taskName is null", new StackFrame(true)); //Console.WriteLine("msg format err: taskName is null"); //return ; //主要判断车的finished状态 } //解析坐标位置 x,y pos_t = msg.IndexOf(";x="); if (pos_t != -1) { pos_e = msg.Substring(pos_t + 1, msg.Length - pos_t - 1).IndexOf(";"); if (pos_e != -1) { // Console.WriteLine("x = " + msg.Substring(pos_t + 3, pos_e - 2) + " id = " + id); x = int.Parse(msg.Substring(pos_t + 3, pos_e - 2)); } } pos_t = msg.IndexOf(";y="); if (pos_t != -1) { pos_e = msg.Substring(pos_t + 1, msg.Length - pos_t - 1).IndexOf(";"); if (pos_e != -1) { // Console.WriteLine("y = " + msg.Substring(pos_t + 3, pos_e - 2)); y = int.Parse(msg.Substring(pos_t + 3, pos_e - 2)); } } pos_t = msg.IndexOf("pause_stat="); if (pos_t != -1) { pos_e = msg.Substring(pos_t, msg.Length - pos_t).IndexOf(";"); pause_stat = int.Parse(msg.Substring(pos_t + 11, pos_e - 11)); } pos_t = msg.IndexOf("gAlarm="); if (pos_t != -1) { pos_e = msg.Substring(pos_t, msg.Length - pos_t).IndexOf(";"); gAlarm = int.Parse(msg.Substring(pos_t + 7, pos_e - 7)); } pos_t = msg.IndexOf("battery="); if (pos_t != -1) //获取电池数据 { pos_e = msg.Substring(pos_t, msg.Length - pos_t).IndexOf(";"); battery_soc = int.Parse(msg.Substring(pos_t + 8, pos_e - 8)); //Console.WriteLine("battery = " + battery_soc); } pos = msg.IndexOf("task_isfinished="); finish_stat = Convert.ToInt16(msg[pos + 16]) - 48; //转的对象是单个字符 0会转成48 } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine("接收 数据异常"); return; } if (!checkForkLiftMsg(battery_soc, pause_stat, finish_stat, gAlarm)) { //Console.WriteLine("接收数据异常"); return; } lock (LockController.getLockController().getLockTask()) { if (pos != -1) //成功匹配到状态 { foreach (ForkLiftWrapper fl in forkLiftWrapperList) { if (fl.getForkLift().id == id) { if (x != 0 && y != 0) { fl.setPosition(x, y); } if (id == 1 && pauseSetTime_f1 == 0) { fl.getForkLift().shedulePause = pause_stat; //只在启动的时候设置一次 pauseSetTime_f1 = 1; fl.getPosition().calcPositionArea(); } else if (id == 2 && pauseSetTime_f2 == 0) { fl.getForkLift().shedulePause = pause_stat; //只在启动的时候设置一次 pauseSetTime_f2 = 1; fl.getPosition().calcPositionArea(); } fl.getForkLift().pauseStat = pause_stat; if (pause_stat >= 0) { checkForkliftPauseStat(fl, pause_stat); } fl.getForkLift().finishStatus = finish_stat; fl.getBatteryInfo().setBatterySoc(battery_soc); fl.updateAlarm(gAlarm); AGVLog.WriteInfo("forklift id " + id + "taskName = " + taskName + "forklift stat = " + fl.getForkLift().finishStatus, new StackFrame(true)); { bool stat = checkTaskSendStat(fl, taskName); if (stat == false) { AGVLog.WriteError("任务列表中不能匹配正确状态的任务", new StackFrame(true)); } } } } } } return; }
/// <summary> /// 检查任务状态 /// </summary> private bool checkTaskSendStat(ForkLiftWrapper fl, string taskName) { bool stat = true; if (fl.getForkLift().finishStatus == 1) { foreach (TaskRecord tr in TaskReordService.getInstance().getTaskRecordList()) { if (tr.forkLiftWrapper != null && tr.forkLiftWrapper.getForkLift().id == fl.getForkLift().id) { if (tr.taskRecordStat == TASKSTAT_T.TASK_SEND) { if (fl.getForkLift().waitTimes > 15) { Console.WriteLine("send task: " + taskName + "to " + fl.getForkLift().forklift_number + "fail"); fl.getForkLift().waitTimes = 0; tr.forkLiftWrapper = null; fl.getForkLift().taskStep = TASK_STEP.TASK_IDLE; fl.getForkLift().currentTask = ""; DBDao.getDao().updateForkLift(fl); if (tr.singleTask.taskType == TASKTYPE_T.TASK_TYPE_UP_PICK) { DBDao.getDao().RemoveTaskRecord(tr); AGVLog.WriteWarn("forklift number: " + fl.getForkLift().forklift_number + " taskName: " + taskName + " 发送失败 移除任务", new StackFrame(true)); } else { tr.taskRecordStat = TASKSTAT_T.TASK_READY_SEND; tr.singleTask.taskStat = TASKSTAT_T.TASK_READY_SEND; DBDao.getDao().UpdateTaskRecord(tr); AGVLog.WriteWarn("forklift number: " + fl.getForkLift().forklift_number + " taskName: " + taskName + " 发送失败 更新任务状态,等待重新发送", new StackFrame(true)); } FormController.getFormController().getMainFrm().updateFrm(); } else { fl.getForkLift().waitTimes++; Console.WriteLine("fl: " + fl.getForkLift().forklift_number + "taskName: " + taskName + "waittimes: " + fl.getForkLift().waitTimes); AGVLog.WriteWarn("forklift number: " + fl.getForkLift().forklift_number + " taskName: " + taskName + " waittimes: " + fl.getForkLift().waitTimes, new StackFrame(true)); } break; } else if (tr.taskRecordStat == TASKSTAT_T.TASK_SEND_SUCCESS) { Console.WriteLine("task: " + taskName + "in " + fl.getForkLift().forklift_number + "finished"); AGVLog.WriteInfo("taskName: " + taskName + "in " + fl.getForkLift().forklift_number + " finished", new StackFrame(true)); DBDao.getDao().RemoveTaskRecord(tr); DBDao.getDao().InsertTaskRecordBak(tr); tr.singleTask.taskStat = TASKSTAT_T.TASK_END; tr.taskRecordStat = TASKSTAT_T.TASK_END; FormController.getFormController().getMainFrm().updateFrm(); fl.getForkLift().taskStep = TASK_STEP.TASK_IDLE; fl.getForkLift().currentTask = ""; DBDao.getDao().updateForkLift(fl); break; } else if (tr.taskRecordStat == TASKSTAT_T.TASK_END) { //break; 继续任务状态没及时删除,继续循环 } break; //每次只匹配一条记录,可能存在两条记录,对应singleTask一样,一条正在运行,一条待发送,适应一键添加功能 } } } else if (fl.getForkLift().finishStatus == 0) { //bool storeTask = true; //是否需要缓存该任务 foreach (TaskRecord tr in TaskReordService.getInstance().getTaskRecordList()) { //Console.WriteLine(" tr stat = " + tr.taskRecordStat + " taskName = " + tr.taskRecordName); if (tr.forkLiftWrapper != null && tr.forkLiftWrapper.getForkLift().id == fl.getForkLift().id) //任务列表中匹配到非待发送的该任务则不缓存 { if (tr.taskRecordStat == TASKSTAT_T.TASK_SEND) { tr.singleTask.taskStat = TASKSTAT_T.TASK_SEND_SUCCESS; tr.taskRecordStat = TASKSTAT_T.TASK_SEND_SUCCESS; DBDao.getDao().UpdateTaskRecord(tr); stat = true; } else if (tr.taskRecordStat == TASKSTAT_T.TASK_SEND_SUCCESS && tr.forkLiftWrapper.getForkLift().id == fl.getForkLift().id) { } fl.getForkLift().waitTimes = 0; //发送任务,等待确认是否发送成功 fl.getForkLift().taskStep = TASK_STEP.TASK_EXCUTE; fl.getForkLift().currentTask = tr.singleTask.taskText; break; //每次只匹配一条记录,可能存在两条记录,对应singleTask一样,一条正在运行,一条待发送,适应一键添加功能 } } } else { Console.WriteLine("fork status err"); AGVLog.WriteError("fork lift staus: " + fl.getForkLift().finishStatus + "err", new StackFrame(true)); } return(stat); }
/// <summary> /// 根据相关条件移除任务 包括更新任务列表 删除数据库 /// </summary> public void removeTaskRecord(SingleTask st, TASKSTAT_T taskRecordStat) { DBDao.getDao().RemoveTaskRecord(st, taskRecordStat); }
/// <summary> /// 添加任务记录,包括两部1、更新taskRecordList 2、更新数据库 /// </summary> public void addTaskRecord(TaskRecord tr) { DBDao.getDao().InsertTaskRecord(tr); }
/// <summary> /// 检查任务状态 /// </summary> /// <param name="fl">车子实例</param> /// <param name="taskName"></param> /// <returns></returns> private bool checkTaskSendStat(ForkLiftWrapper fl, string taskName) { bool stat = true; //Console.WriteLine(" fl id " + fl.getForkLift().id + " finishStatus = " + fl.finishStatus); if (fl.getForkLift().finishStatus == 1) //车子状态空闲有两种可能1:车子任务执行完成 2:任务在执行中 报文没有及时反馈 { foreach (TaskRecord tr in TaskReordService.getInstance().getTaskRecordList()) { //if (tr.forkLift != null && tr.taskRecordName.Equals(taskName)) if (tr.forkLiftWrapper != null && tr.forkLiftWrapper.getForkLift().id == fl.getForkLift().id) //可能存在任务没发送成功,反馈的taskName与现在taskrecord的名称不一样 { if (tr.taskRecordStat == TASKSTAT_T.TASK_SEND) { if (fl.getForkLift().waitTimes > 15) //等待次数超过15次,后面重新发送该任务 { Console.WriteLine("send task: " + taskName + "to " + fl.getForkLift().forklift_number + "fail"); fl.getForkLift().waitTimes = 0; tr.forkLiftWrapper = null; fl.getForkLift().taskStep = TASK_STEP.TASK_IDLE; //车子状态改为空闲 fl.getForkLift().currentTask = ""; DBDao.getDao().updateForkLift(fl); //赋值空字符串 if (tr.singleTask.taskType == TASKTYPE_T.TASK_TYPE_UP_PICK) { DBDao.getDao().RemoveTaskRecord(tr); AGVLog.WriteWarn("forklift number: " + fl.getForkLift().forklift_number + " taskName: " + taskName + " 发送失败 移除任务", new StackFrame(true)); } else { tr.taskRecordStat = TASKSTAT_T.TASK_READY_SEND; //改变任务的状态,后面重新发送 tr.singleTask.taskStat = TASKSTAT_T.TASK_READY_SEND; DBDao.getDao().UpdateTaskRecord(tr); AGVLog.WriteWarn("forklift number: " + fl.getForkLift().forklift_number + " taskName: " + taskName + " 发送失败 更新任务状态,等待重新发送", new StackFrame(true)); } FormController.getFormController().getMainFrm().updateFrm(); //设置更新界面 } else { fl.getForkLift().waitTimes++; Console.WriteLine("fl: " + fl.getForkLift().forklift_number + "taskName: " + taskName + "waittimes: " + fl.getForkLift().waitTimes); AGVLog.WriteWarn("forklift number: " + fl.getForkLift().forklift_number + " taskName: " + taskName + " waittimes: " + fl.getForkLift().waitTimes, new StackFrame(true)); } break; } else if (tr.taskRecordStat == TASKSTAT_T.TASK_SEND_SUCCESS) //确保没有重复进入,否则会插入多条备份记录 { Console.WriteLine("task: " + taskName + "in " + fl.getForkLift().forklift_number + "finished"); AGVLog.WriteInfo("taskName: " + taskName + "in " + fl.getForkLift().forklift_number + " finished", new StackFrame(true)); DBDao.getDao().RemoveTaskRecord(tr); //移除record是3的记录 DBDao.getDao().InsertTaskRecordBak(tr); tr.singleTask.taskStat = TASKSTAT_T.TASK_END; tr.taskRecordStat = TASKSTAT_T.TASK_END; FormController.getFormController().getMainFrm().updateFrm(); //设置更新界面 //设置更新界面 fl.getForkLift().taskStep = TASK_STEP.TASK_IDLE; fl.getForkLift().currentTask = ""; DBDao.getDao().updateForkLift(fl); break; } else if (tr.taskRecordStat == TASKSTAT_T.TASK_END) { //break; 继续任务状态没及时删除,继续循环 } break; //每次只匹配一条记录,可能存在两条记录,对应singleTask一样,一条正在运行,一条待发送,适应一键添加功能 } } } else if (fl.getForkLift().finishStatus == 0) { //bool storeTask = true; //是否需要缓存该任务 foreach (TaskRecord tr in TaskReordService.getInstance().getTaskRecordList()) { //Console.WriteLine(" tr stat = " + tr.taskRecordStat + " taskName = " + tr.taskRecordName); if (tr.forkLiftWrapper != null && tr.forkLiftWrapper.getForkLift().id == fl.getForkLift().id) //任务列表中匹配到非待发送的该任务则不缓存 { if (tr.taskRecordStat == TASKSTAT_T.TASK_SEND) { tr.singleTask.taskStat = TASKSTAT_T.TASK_SEND_SUCCESS; tr.taskRecordStat = TASKSTAT_T.TASK_SEND_SUCCESS; DBDao.getDao().UpdateTaskRecord(tr); stat = true; } else if (tr.taskRecordStat == TASKSTAT_T.TASK_SEND_SUCCESS && tr.forkLiftWrapper.getForkLift().id == fl.getForkLift().id) { } fl.getForkLift().waitTimes = 0; //发送任务,等待确认是否发送成功 fl.getForkLift().taskStep = TASK_STEP.TASK_EXCUTE; fl.getForkLift().currentTask = tr.singleTask.taskText; break; //每次只匹配一条记录,可能存在两条记录,对应singleTask一样,一条正在运行,一条待发送,适应一键添加功能 } } /* * if (storeTask) //系统启动后,车子可能正在执行任务起来,将正在执行的任务缓存 * { * TaskRecord tr = new TaskRecord(); * tr.taskRecordName = taskName; * tr.forkLift = fl; * tr.taskRecordStat = TASKSTAT_T.TASK_SEND_SUCCESS; //状态已经发送成功 * tr.setSingleTaskByTaskName(AGVUtil.parseTaskRecordName(taskName)); * fl.getForkLift().taskStep = TASK_STEP.TASK_EXCUTE; * addTs = tr; * //AGVInitialize.getInitialize().getMainFrm().updateCurrentTask(st.taskName); //更新界面上的当前任务 * Console.WriteLine("store task: " + tr.taskRecordName + "taskNumber:" + taskRecordList.Count); * AGVLog.WriteInfo("store task: " + tr.taskRecordName + "at boot task count: " + taskRecordList.Count, new StackFrame(true)); * } */ } else { Console.WriteLine("fork status err"); AGVLog.WriteError("fork lift staus: " + fl.getForkLift().finishStatus + "err", new StackFrame(true)); } return(stat); }