123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- using Newtonsoft.Json;
- using RequestServer.HttpServer;
- using ResponseServer.HttpServer;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Text;
- using System.Threading.Tasks;
- using static MaCls;
- namespace HttpServer.mazak
- {
- class MazakServer
- {
- private static string ON_LINE_STATE = "在线";
- private static Dictionary<short, string> runModeDict = new Dictionary<short, string>();
- public MazakServer()
- {
- runModeDict.Add(0, "No operation mode");
- runModeDict.Add(1, "Automatic operation mode");
- runModeDict.Add(2, "Tape operation mode");
- runModeDict.Add(3, "Rapid traverse mode");
- runModeDict.Add(4, "Home return mode");
- runModeDict.Add(5, "Cutting feed/manual pulse mode");
- runModeDict.Add(6, "MDI operation mode");
- }
-
- static ushort h;
- public static ResponseBody requestHttpServer(RequestBody requestBody)
- {
- string ip = requestBody.serverUrl;
- string port = requestBody.port;
- string fun = requestBody.type;
- YG.Log.Instance.WriteLogAdd($"mazak-请求参数-->> " + JsonConvert.SerializeObject(requestBody));
- ResponseBody responseBody = new ResponseBody();
- responseBody.deviceState = ON_LINE_STATE;
- Ping pingSender = new Ping();
- PingReply reply = pingSender.Send(ip);
- if (reply.Status != IPStatus.Success)
- {
- responseBody.deviceState = "离线";
- responseBody.msg = "网络不通";
- }
- else
- {
- // port = 50100;
- int ret = MaCls.MazConnect(out h, ip, 50100, 10);
- YG.Log.Instance.WriteLogAdd($"mazak-连接结果-->> " + ret);
- RunDatasInfo runDataInfo = new RunDatasInfo();
- if (fun == ActionTypeEnum.Collect.ToString())
- {
- ushort status0 = 0;
- short status = 1;
- ret = MaCls.MazGetRunningSts(h, status0, ref status);
- YG.Log.Instance.WriteLogAdd($"mazak-status-->> " + status);
- MAZ_NCONTIME powerTime = default(MAZ_NCONTIME);
- ret = MaCls.MazGetNcPowerOnTime(h, ref powerTime);
- if(ret == 0)
- {
- runDataInfo.powerOnTime = powerTime.uint_3.ToString();
- }
- MAZ_NCTIME runTime = default(MAZ_NCTIME);
- ret = MaCls.MazGetRunningTime(h, 0, ref runTime);
- YG.Log.Instance.WriteLogAdd($"mazak-powerTime-->> " + powerTime.uint_3);
- //获取轴名称
- MAZ_AXISNAME axisNameInfo = default(MAZ_AXISNAME);
- ret = MaCls.MazGetAxisName(h, ref axisNameInfo);
- if(ret == 0)
- {
- foreach(OneAxisName axisName in axisNameInfo.axis)
- {
- }
- YG.Log.Instance.WriteLogAdd($"mazak-轴名称-->> " + axisNameInfo);
- //当前位置
- MAZ_NCPOS currentNcPos = default(MAZ_NCPOS);
- ret = MaCls.MazGetCurrentPos(h, ref currentNcPos);
- if(ret == 0)
- {
- runDataInfo.absoluteCoordinate = string.Join(",", currentNcPos.data);
- }
- //相对位置
- MAZ_NCPOS relativeNcPos = default(MAZ_NCPOS);
- ret = MaCls.MazGetRemain(h, ref relativeNcPos);
- if (ret == 0)
- {
- runDataInfo.relativeCoordinate = string.Join(",", relativeNcPos.data);
- }
- //机械位置
- MAZ_NCPOS machineNcPos = default(MAZ_NCPOS);
- ret = MaCls.MazGetCurrentPos(h, ref machineNcPos);
- if (ret == 0)
- {
- runDataInfo.machineCoordinate = string.Join(",", machineNcPos.data);
- }
- }
- //主轴负载
- ushort spindleLoad = 0;
- ret = MaCls.MazGetSpindleLoad(h, 0, ref spindleLoad);
- if (ret == 0)
- {
- runDataInfo.spindleLoad = spindleLoad.ToString();
- }
- //进给轴负载
- ushort feedAxisLoad = 0;
- ret = MaCls.MazGetSpindleLoad(h, 0, ref feedAxisLoad);
- if (ret == 0)
- {
- runDataInfo.feedAxisLoad = feedAxisLoad.ToString();
- }
- //伺服轴负载
- MAZ_AXISLOAD axisLoadInfo = default(MAZ_AXISLOAD);
- ret = MaCls.MazGetAxisLoad(h, ref axisLoadInfo);
- if (ret == 0)
- {
- runDataInfo.servoLoad = axisLoadInfo.data[0].ToString();
- YG.Log.Instance.WriteLogAdd($"mazak-伺服轴负载-->> " + JsonConvert.SerializeObject(axisLoadInfo));
- }
- //主程序
- MAZ_PROINFO proInfo = default(MAZ_PROINFO);
- ret = MaCls.MazGetMainPro(h, status0, ref proInfo);
- if (ret == 0)
- {
- runDataInfo.mainProg = proInfo.comment;
- }
- //进给速度
- MAZ_FEED maz_feed = default(MAZ_FEED);
- ret = MaCls.MazGetFeed(h, status0, ref maz_feed);
- if (ret == 0)
- {
- runDataInfo.actFeedSpeed = maz_feed.fmin.ToString();
- }
- //进给倍率
- ushort feedOver = 0;
- ret = MaCls.MazGetFeedOverRide(h, 0, ref feedOver);
- if (ret == 0)
- {
- runDataInfo.feedRateOvr = feedOver.ToString();
- }
- //主轴速度
- int spindleSpeed = 0;
- ret = MaCls.MazGetCurrentSpindleRev(h, 0, ref spindleSpeed);
- if (ret == 0)
- {
- runDataInfo.actSpindleSpeed = spindleSpeed.ToString();
- }
- //主轴倍率
- ushort spindleRateOvr = 0;
- ret = MaCls.MazGetSpindleOverRide(h, 0, ref spindleRateOvr);
- if (ret == 0)
- {
- runDataInfo.spindleRateOvr = spindleRateOvr.ToString();
- }
- //NC版本信息
- MAZ_NC_VERINFO nc_version = default(MAZ_NC_VERINFO);
- ret = MaCls.MazGetNCVersionInfo(h, ref nc_version);
- if (ret == 0)
- {
- runDataInfo.ncModel = nc_version.szNCModel;
- }
- //运行模式
- short modeNum = 0;
- ret = MaCls.MazGetRunMode(h, 0, ref modeNum);
- if (ret == 0)
- {
- runDataInfo.runMode = runModeDict[modeNum];
- }
- //运行时间
- MAZ_NCTIME nc_time = default(MAZ_NCTIME);
- ret = MaCls.MazGetRunningTime(h, 0, ref nc_time);
- if (ret == 0)
- {
- runDataInfo.ncRunTime = nc_time.uint_2.ToString();
- }
- //工件计数
- int partsCount = 0;
- ret = MaCls.MazGetPartsCount(h, 0, ref partsCount);
- if (ret == 0)
- {
- runDataInfo.partsCount = partsCount;
- }
- //当前刀号
- MAZ_TOOLINFO currentToolInfo = default(MAZ_TOOLINFO);
- ret = MaCls.MazGetCurrentTool(h, 0, ref currentToolInfo);
- if (ret == 0)
- {
- runDataInfo.currentToolNo = currentToolInfo.ushort_0.ToString();
- }
- //报警
- MAZ_ALARMALL maz_ALARMALL = default(MAZ_ALARMALL);
- ret = MaCls.MazGetAlarm(h, ref maz_ALARMALL);
- if (ret == 0)
- {
- MAZ_ALARM[] alarms = maz_ALARMALL.alarm;
- if (alarms != null && alarms.Count() > 0)
- {
- List<AlmInfo> errors = new List<AlmInfo>();
- foreach (MAZ_ALARM alarm in alarms)
- {
- AlmInfo addAlarm = new AlmInfo();
- addAlarm.no = alarm.short_0.ToString();
- addAlarm.msg = alarm.message;
- errors.Add(addAlarm);
- }
- responseBody.errorsInfo = JsonConvert.SerializeObject(errors);
- YG.Log.Instance.WriteLogAdd($"mazak-报警-->> " + JsonConvert.SerializeObject(alarms));
- }
- }
- //获取刀具列表,先获取刀具数量
- int toolNum = 0;
- ret = MaCls.MazGetToolDataNum(h, 0, ref toolNum);
- MAZ_TD[] array = new MAZ_TD[toolNum - 1 + 1];
- ret = MaCls.MazGetAllToolData(h, 0, toolNum, array);
- if (ret == 0)
- {
- if (array != null && array.Count() > 0)
- {
- List<ToolsInfo> tools = new List<ToolsInfo>();
- foreach (MAZ_TD mazTd in array)
- {
- ToolsInfo addToolInfo = new ToolsInfo();
- addToolInfo.number = mazTd.td_common.byte_0.ToString();
- addToolInfo.position = mazTd.td_common.pkno.ToString();
- addToolInfo.rateLife = mazTd.td_common.lifetime.ToString();
- addToolInfo.useLife = mazTd.td_common.usetime.ToString();
- tools.Add(addToolInfo);
- }
- responseBody.toolsData = JsonConvert.SerializeObject(tools);
- }
-
- YG.Log.Instance.WriteLogAdd($"mazak-获取刀具列表-->> 成功" + JsonConvert.SerializeObject(array));
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-获取刀具列表-->> 失败");
- }
- responseBody.runDatasInfo = JsonConvert.SerializeObject(runDataInfo);
- //responseBody.runDatasInfo = "运行时间:" + powerTime.uint_2 + ",主程序:" + proInfo.string_0 + ",进给:" + maz_feed.fmin/1000 + ",主轴负载:" + spindleLoad + ",进给负载:" + feedOver
- // + ",主轴速度:" + spindleSpeed + ",刀具信息:" + JsonConvert.SerializeObject(currentToolInfo);
- }
- else if (fun == ActionTypeEnum.ToolNoData.ToString())
- {
- ushort toolno = 0;
- MAZ_TDALL toollist = default(MAZ_TDALL);
- ret = MaCls.MazGetToolData(h, 0, requestBody.toolNo, ref toollist);
- if (ret == 0)
- {
- responseBody.toolsData = JsonConvert.SerializeObject(toollist);
- YG.Log.Instance.WriteLogAdd($"mazak-获取刀具-->> 成功" + JsonConvert.SerializeObject(toollist));
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-获取刀具-->> 失败");
- }
- }
- else if (fun == ActionTypeEnum.ToolList.ToString())
- {
- int num = 0;
- ret = MaCls.MazGetToolDataNum(h, 0, ref num);
- responseBody.runDatasInfo = "刀具编号:" + num;
- MAZ_TD[] array = new MAZ_TD[num - 1 + 1];
- ret = MaCls.MazGetAllToolData(h, 0, num, array);
- if (ret == 0)
- {
- responseBody.toolsData = JsonConvert.SerializeObject(array);
- YG.Log.Instance.WriteLogAdd($"mazak-获取刀具列表-->> 成功" + JsonConvert.SerializeObject(array));
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-获取刀具-->> 失败");
- }
- }
- else if (fun == ActionTypeEnum.DeleteTool.ToString())
- {
- ret = MaCls.MazDeleteToolData(h, 0, requestBody.toolNo);
- if (ret == 0)
- {
- YG.Log.Instance.WriteLogAdd($"mazak-删除刀具-->> 成功");
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-删除刀具-->> 失败");
- }
- }
- else if (fun == ActionTypeEnum.SetTool.ToString())
- {
- MAZ_TDALL toollist = default(MAZ_TDALL);
- ret = MaCls.MazGetToolData(h, 0, requestBody.toolNo, ref toollist);
- MAZ_TDALL toolData = new MAZ_TDALL();
- MAZ_TD[] tool1 = new MAZ_TD[22];
- int count = 0;
- foreach (MAZ_TD aZ_TDALL in toollist.tool)
- {
- MAZ_TD td11 = new MAZ_TD();
- td11 = aZ_TDALL;
- if (aZ_TDALL.td_common.ushort_0 == requestBody.toolNo)
- {
- td11.td_common.pkno = 0; //刀位号
- td11.td_common.byte_1 = 1; //刀具ID编号对应key
- td11.td_common.lengthA = 2950000; //刀具长度
- td11.td_common.name = 15; //刀具名称对应key
- td11.td_common.int_0 = 20; //公称径
- td11.td_common.diameter = 12300; // 刀具径刀尖角
- td11.td_common.string_0 = "高速钢"; //材料,机床里要有
- td11.td_common.lifetime = 600; //刀具寿命
- td11.td_common.lifenumber = 7; //刀具次数
- td11.td_common.usetime = 480; //刀具使用时间
- td11.td_common.usenumber = 6; //刀具使用次数
- td11.td_common.int_1 = 2; // 组号
- td11.td_common.compno = 1; //补偿编号
- td11.td_common.wearcompX = 1000;
- td11.td_common.wearcompY = -1000;
- td11.td_common.wearcompZ = 2000;
- td11.td_common.maxwearX = 10000;
- td11.td_common.maxwearY = 20000;
- td11.td_common.maxwearZ = 30000;
- td11.td_various.td_general.td_general.angle = 1600;
- td11.td_various.td_general.td_general.easycompX = 6541000;
- td11.td_various.td_general.td_general.easycompY = 2310;
- td11.td_various.td_general.td_general.easycompZ = 2320;
- td11.td_various.td_general.td_general.conscompX = 2330;
- td11.td_various.td_general.td_general.conscompY = 2340;
- td11.td_various.td_general.td_general.conscompZ = -3120;
- // td11.td_common.lengthB = 2;
- //td11.td_common.dummy5 = "1";
- //td11.td_common.compno = 2;
- }
- tool1[count] = td11;
- count++;
- }
- toollist.tool = tool1;
- MAZ_TD td1 = new MAZ_TD();
- td1.td_common.ushort_0 = requestBody.toolNo;
- td1.td_common.name = 33;
- td1.td_common.pkno = 21;
- td1.td_common.int_0 = 1;
- // td1.td_common.lengthA = 10000+ requestBody.toolNo;
- //td1.td_common.lengthB = 20000+ requestBody.toolNo;
- MAZ_TD[] tool = new MAZ_TD[20];
- tool[0] = td1;
- //toolData.tool = tool;
- //MAZ_TD td2 = default(MAZ_TD);
- //td2.td_common.ushort_0 = 2;
- //td2.td_common.name = 22;
- //toolData.tool = td2;en
- MAZ_TD_ERRORALL errorData = default(MAZ_TD_ERRORALL);
- ret = MaCls.MazSetToolData(h, 0, requestBody.toolNo, ref toollist, ref errorData);
- if (ret == 0)
- {
- YG.Log.Instance.WriteLogAdd($"mazak-删除刀具-->> 成功");
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-删除刀具-->> 失败");
- }
- }
- else if (!string.IsNullOrEmpty(requestBody.path))
- {
- string fileName = Path.GetFileName(requestBody.path);
- string filepath = Path.GetDirectoryName(requestBody.path);
- YG.Log.Instance.WriteLogAdd($"mazak-fileName-->> " + fileName + ",filepath=" + filepath);
- try
- {
- if (fun == ActionTypeEnum.Upload.ToString())
- {
- ret = MaCls.MazSendProgram(h, fileName, filepath, 1);
- YG.Log.Instance.WriteLogAdd($"mazak-上传响应结果-->> " + ret);
- if (ret == 0)
- {
- YG.Log.Instance.WriteLogAdd($"mazak-上传-->> " + requestBody.path + "成功");
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-上传-->> " + requestBody.path + "失败");
- }
- }
- else if (fun == ActionTypeEnum.DownLoad.ToString())
- {
- ret = MaCls.MazReceiveProgram(h, fileName, filepath, 1);
- if (ret == 0)
- {
- YG.Log.Instance.WriteLogAdd($"mazak-下载-->> " + requestBody.path + "成功");
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-下载-->> " + requestBody.path + "失败");
- }
- }
- else if (fun == ActionTypeEnum.DeleteNc.ToString())
- {
- ret = MaCls.MazDeleteProgram(h, fileName);
- if (ret == 0)
- {
- YG.Log.Instance.WriteLogAdd($"mazak-删除-->> " + requestBody.path + "成功");
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-删除-->> " + requestBody.path + "失败");
- }
- }
- else if (fun == ActionTypeEnum.SelectNcProgram.ToString())
- {
- ret = MaCls.MazSetMainPro(h, 0, fileName);
- if (ret == 0)
- {
- YG.Log.Instance.WriteLogAdd($"mazak-选择-->> " + requestBody.path + "成功");
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"mazak-选择-->> " + requestBody.path + "失败");
- }
- }
- }
- catch (Exception e)
- {
- YG.Log.Instance.WriteLogAdd($"mazak-异常-->> " + e.Message);
- ret = MaCls.MazDisconnect(h);
- }
- ret = MaCls.MazDisconnect(h);
- }
- }
- return responseBody;
- }
- }
- }
|