//删除任务的实现方法 public void taskDelete(Object id) { MySqlConnection conn = null; string taskId = (string)id; try { //获取本模拟器的编号并通过命令行删除 conn = DatabaseOpt.getDBConnection(); string emulatorId = DatabaseOpt.queryOne(conn, taskId, "emulatorId")[0]; if (!"".Equals(emulatorId)) { //说明模拟器已创建,通过命令行删除 EmulatiorOpt.removeEmu(emulatorId); } //删除该条任务信息 DatabaseOpt.deleteOne(conn, taskId); } catch { MessageBox.Show("删除任务出错"); } finally { DatabaseOpt.close(conn); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); } }
//停止任务的实现方法 public void taskStop(Object id) { MySqlConnection conn = null; string taskId = (string)id; try { conn = DatabaseOpt.getDBConnection(); //获取本模拟器的编号通过命令行停止,关闭appium和socket连接 string emulatorId = DatabaseOpt.queryOne(conn, taskId, "emulatorId")[0]; if (!"".Equals(emulatorId)) { //说明模拟器编号不为空,执行关闭模拟器操作 EmulatiorOpt.shutdownEmu(emulatorId); } string appiumPort = DatabaseOpt.queryOne(conn, taskId, "appiumPort")[0]; if (!"".Equals(appiumPort)) { //说明appium端口不为空 AppiumOpt.endAppium(int.Parse(appiumPort)); } string socketPort = DatabaseOpt.queryOne(conn, taskId, "socketPort")[0]; if (!"".Equals(socketPort)) { //说明socket端口不为空 AppiumOpt.endAppium(int.Parse(socketPort)); } //修改任务状态和任务标志 DatabaseOpt.updateOne(conn, taskId, "taskStatus", "停止"); DatabaseOpt.updateOne(conn, taskId, "taskTag", "普通"); } catch (Exception) { MessageBox.Show("任务停止出错"); } finally { DatabaseOpt.close(conn); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); } }
//是按钮 private void confirmBtn_Click(object sender, EventArgs e) { MySqlConnection conn = null; try { conn = DatabaseOpt.getDBConnection(); //将已激活任务置为普通 for (int i = 0; i < taskIdList.Count; i++) { string taskId = taskIdList[i]; //停止任务的代码 //查询并停止模拟器 string emualtorId = DatabaseOpt.queryOne(conn, taskId, "emulatorId")[0]; if (!"".Equals(emualtorId)) { //说明模拟器已创建,通过命令行停止 EmulatiorOpt.shutdownEmu(emualtorId); } string appiumPort = DatabaseOpt.queryOne(conn, taskId, "appiumPort")[0]; if (!"".Equals(appiumPort)) { //说明appium已经启动,通过命令行杀死 AppiumOpt.endAppium(int.Parse(appiumPort)); } string socketPort = DatabaseOpt.queryOne(conn, taskId, "socketPort")[0]; if (!"".Equals(socketPort)) { //说明socket已经开启,通过命令行杀死 AppiumOpt.endAppium(int.Parse(socketPort)); } DatabaseOpt.updateOne(conn, taskId, "taskTag", "普通"); DatabaseOpt.updateOne(conn, taskId, "taskStatus", "停止"); } } finally { DatabaseOpt.close(conn); this.Dispose(); } }
//重新吸附任务的实现方法 public void reabsorption(Object id) { MySqlConnection conn = null; string taskId = (string)id; try { conn = DatabaseOpt.getDBConnection(); //查询已激活的任务 string activeTaskId = DatabaseOpt.queryTaskTag(conn)[0]; if (!"".Equals(activeTaskId)) { //已有任务被激活 if (!taskId.Equals(activeTaskId)) { //说明当前激活的任务与重新吸附的任务不同,需要停止其模拟器 string activeTaskEmulatorId = DatabaseOpt.queryOne(conn, activeTaskId, "emulatorId")[0]; //停止模拟器 EmulatiorOpt.shutdownEmu(activeTaskId); } //关闭当前激活任务的appium string activeTaskAppiumPort = DatabaseOpt.queryOne(conn, activeTaskId, "appiumPort")[0]; if (!"".Equals(activeTaskAppiumPort)) { //关闭appium AppiumOpt.endAppium(int.Parse(activeTaskAppiumPort)); //将appium端口置空 DatabaseOpt.updateOne(conn, activeTaskId, "appiumPort", ""); } //关闭socket string activeTaskSocketPort = DatabaseOpt.queryOne(conn, activeTaskId, "socketPort")[0]; if (!"".Equals(activeTaskSocketPort)) { //关闭socket AppiumOpt.endAppium(int.Parse(activeTaskSocketPort)); //将socket端口置空 DatabaseOpt.updateOne(conn, activeTaskId, "socketPort", ""); } //置之前激活的任务标志为普通 DatabaseOpt.updateOne(conn, activeTaskId, "taskTag", "普通"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); } //获取重新吸附任务的appium端口 string appiumPort = DatabaseOpt.queryOne(conn, taskId, "appiumPort")[0]; if (!"".Equals(appiumPort)) { //关闭之前的appium AppiumOpt.endAppium(int.Parse(appiumPort)); //将appium端口置空 DatabaseOpt.updateOne(conn, taskId, "appiumPort", ""); } string socketPort = DatabaseOpt.queryOne(conn, taskId, "socketPort")[0]; if (!"".Equals(socketPort)) { //关闭之前的socket AppiumOpt.endAppium(int.Parse(socketPort)); //将socket端口置空 DatabaseOpt.updateOne(conn, taskId, "socketPort", ""); } //激活任务代码 //通知web端本条任务激活 string phoneNumber = DatabaseOpt.queryOne(conn, taskId, "phoneNumber")[0]; string IMSI = DatabaseOpt.queryOne(conn, taskId, "IMSI")[0]; string nationCode = DatabaseOpt.queryOne(conn, taskId, "nationCode")[0]; string url = "http://192.168.17.232:8989/ghost/getVerificationCode?imsi=" + IMSI + "&phone=" + phoneNumber + "&phone_nation_code=" + nationCode; string responseContent = WebServerCommunicate.httpGet(url); if (!"ok".Equals(responseContent)) { MessageBox.Show("激活失败"); return; } //修改任务标记为激活 DatabaseOpt.updateOne(conn, taskId, "taskTag", "激活"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); //启动任务函数 taskStart(taskId); } finally { DatabaseOpt.close(conn); } }
//启动任务的实现方法 public void taskStart(Object id) { MySqlConnection conn = null; int appiumPort = 0; //选中任务的id string taskId = (string)id; try { conn = DatabaseOpt.getDBConnection(); //汇报任务进度 DatabaseOpt.updateOne(conn, taskId, "taskProgress", "正在准备启动任务"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); List <string> emulatorIdList = DatabaseOpt.queryOne(conn, taskId, "emulatorId"); //说明该任务还没有创建模拟器 if ("".Equals(emulatorIdList[0])) { //创建模拟器 int emulatorId = EmulatiorOpt.createEmu(); //计算端口号 int emulatorPort = 21503 + emulatorId * 10; //将新建信息插入数据库 DatabaseOpt.updateOne(conn, taskId, "emulatorId", emulatorId.ToString()); DatabaseOpt.updateOne(conn, taskId, "emulatorPort", emulatorPort.ToString()); } //获取当前任务的模拟器号和端口 string emulatorIdStr = DatabaseOpt.queryOne(conn, taskId, "emulatorId")[0]; string emulatorPortStr = DatabaseOpt.queryOne(conn, taskId, "emulatorPort")[0]; //启动模拟器 EmulatiorOpt.startEmu(emulatorIdStr); //设置任务状态为已启动未控 DatabaseOpt.updateOne(conn, taskId, "taskStatus", "已启动未控"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); //睡眠10秒等待模拟器启动完成 Thread.Sleep(20000); //执行安装APP //汇报任务进度 DatabaseOpt.updateOne(conn, taskId, "taskProgress", "正在安装APP"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); string apkPath = DatabaseOpt.queryOne(conn, taskId, "apkPath")[0]; //APP安装函数 EmulatiorOpt.installApp(int.Parse(emulatorIdStr), apkPath); //执行启动appium,汇报任务进度 DatabaseOpt.updateOne(conn, taskId, "taskProgress", "正在初始化appium"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); appiumPort = AppiumOpt.getAvailablePort(4723, 65534); //说明appium无可用端口 if (appiumPort == 0) { //将该条任务在数据库中的appiumPort值置为"" DatabaseOpt.updateOne(conn, taskId, "appiumPort", ""); DatabaseOpt.updateOne(conn, taskId, "taskProgress", "appium无可用端口"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); return; } else { //开启appium服务 AppiumOpt.callCmd("appium -a 127.0.0.1 -p " + appiumPort); //睡眠5秒等待appium开启 Thread.Sleep(5000); //记录本次启动任务使用的appium端口号 DatabaseOpt.updateOne(conn, taskId, "appiumPort", appiumPort.ToString()); } int socketPort = AppiumOpt.getAvailablePort(50000, 65534); //说明socket无可用端口 if (socketPort == 0) { //将该条任务在数据库中的socketPort值置为"" DatabaseOpt.updateOne(conn, taskId, "socketPort", ""); DatabaseOpt.updateOne(conn, taskId, "taskProgress", "socket通信无可用端口"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); return; } //防止没有连上模拟器 AppiumOpt.callCmd("adb connect 127.0.0.1:" + emulatorPortStr); Thread.Sleep(3000); //准备调用jar包所需要传入的参数 string appName = DatabaseOpt.queryOne(conn, taskId, "appName")[0]; string methodName = MethodMapping(appName); string deviceName = "127.0.0.1:" + emulatorPortStr; string IMSI = DatabaseOpt.queryOne(conn, taskId, "IMSI")[0]; string phoneNumber = DatabaseOpt.queryOne(conn, taskId, "phoneNumber")[0]; string nationCode = DatabaseOpt.queryOne(conn, taskId, "nationCode")[0]; //开启线程运行jar包 Thread extrationThread = new Thread(new ParameterizedThreadStart(AppiumOpt.callCmd)); extrationThread.Start("java -cp" + " " + @appiumProject_jarPath + " " + methodName + " " + "127.0.0.1 " + socketPort + " " + deviceName + " " + appiumPort + " " + IMSI + " " + phoneNumber + " " + nationCode); //与客户端通信 AppiumOpt.communicate(socketPort, conn, taskId, this.taskInfoDataGridView); //查询任务执行到最后时的任务进度 List <string> taskEndProgress = DatabaseOpt.queryOne(conn, taskId, "taskProgress"); if (taskEndProgress[0].Contains("登录完成")) { //说明正常登录,将任务状态置为已启动已控 DatabaseOpt.updateOne(conn, taskId, "taskStatus", "已启动已控"); TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView); } } finally { //关闭appium端口,清除该任务在数据库中存放的端口信息 if (appiumPort != 0) { AppiumOpt.endAppium(appiumPort); } if (conn != null) { DatabaseOpt.updateOne(conn, taskId, "appiumPort", ""); DatabaseOpt.updateOne(conn, taskId, "socketPort", ""); } DatabaseOpt.close(conn); } }