/// <summary> /// 上报状态 /// </summary> /// <param name="msg"></param> /// <param name="er"></param> /// <returns></returns> public bool Report_Status(List <CData_Status> msg, out string er) { er = string.Empty; try { CMessage <CData_Status> message = new CMessage <CData_Status>(); message.Header = new CHeader() { ID = _pc_idNo, Name = _pc_name, Type = (int)ENodeType.客户端, Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; message.Data = msg; string topic = _publishTopics[EMessageType.状态消息]; string cmd = CJSon.Serializer <CMessage <CData_Status> >(message); if (!_mqtt.Publish(topic, cmd, out er)) { return(false); } return(true); } catch (Exception ex) { er = ex.ToString(); return(false); } }
/// <summary> /// 发布点对点消息 /// </summary> /// <returns></returns> public bool Publish_Device(EMessageType messageType, ECmdType cmdType, string cmdName, string cmdInfo, List <CDevList> devList, out string er) { try { CMessage <CData_Cmd> message = new CMessage <CData_Cmd>(); CHeader header = new CHeader() { ID = _pc_idNo, Name = _pc_name, Type = (int)ENodeType.主控端, Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; message.Header = header; message.Data = new List <CData_Cmd>(); for (int i = 0; i < devList.Count; i++) { message.Data.Add(new CData_Cmd() { ID = devList[i].idNo, Name = devList[i].Name, CmdType = (int)cmdType, CmdName = cmdName, CmdInfo = cmdInfo, Remark1 = string.Empty, Remark2 = string.Empty }); } string topic = _publishTopics[messageType]; string cmd = CJSon.Serializer <CMessage <CData_Cmd> >(message); if (!_mqtt.Publish(topic, cmd, out er)) { return(false); } return(true); } catch (Exception ex) { er = ex.ToString(); return(false); } }
/// <summary> /// 应答指令请求 /// </summary> /// <param name="er"></param> /// <returns></returns> public bool Reponse_Command(string status, out string er) { er = string.Empty; try { CMessage <CData_Cmd> message = new CMessage <CData_Cmd>(); message.Header = new CHeader() { ID = _pc_idNo, Name = _pc_name, Type = (int)ENodeType.客户端, Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; message.Data = new List <CData_Cmd>(); for (int i = 0; i < _devices.Count; i++) { message.Data.Add(new CData_Cmd() { ID = _devices[i].idNo, Name = _devices[i].Name, CmdType = (int)ECmdType.应答指令, CmdName = (ECmdType.应答指令).ToString(), CmdInfo = status, Remark1 = string.Empty, Remark2 = string.Empty }); } string topic = _publishTopics[EMessageType.应答指令]; string cmd = CJSon.Serializer <CMessage <CData_Cmd> >(message); if (!_mqtt.Publish(topic, cmd, out er)) { return(false); } return(true); } catch (Exception ex) { er = ex.ToString(); return(false); } }
/// <summary> /// 测试与服务端连接 /// </summary> /// <param name="er"></param> /// <returns></returns> public bool Start(out EMesState status, out string er) { er = string.Empty; status = EMesState.正常; try { string url = CIniFile.ReadFromIni("Parameter", "url", iniFile, "http://192.168.50.250:8080/biz/api/") + "TestConnect"; CTestConnectReponse reponse = null; string requestData = string.Empty; string reponseData = string.Empty; if (!CNet.HttpGet(url, requestData, out reponseData, out er)) { status = EMesState.网络异常; return(false); } reponse = CJSon.Deserialize <CTestConnectReponse>(reponseData); if (reponse.status != "200") { status = EMesState.网络异常; er = reponseData; return(false); } if (reponse.result.ToUpper() != "SUCCESS") { status = EMesState.异常错误; er = reponse.msg; return(false); } er = requestData; return(true); } catch (Exception ex) { er = ex.ToString(); return(false); } }
/// <summary> /// 接收消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnMessageRecv(object sender, CMQTT.CMessageArgs e) { try { if (!_recieveTopics.ContainsKey(e.topic)) { OnMessageArgs.OnEvented(new CCMessageArgs(e.idNo, e.name, e.topic, e.message, true, "主题不存在")); return; } EMessageType msgType = _recieveTopics[e.topic]; switch (msgType) { case EMessageType.广播指令: //请求指示 CMessage <CData_Cmd> cmd_RPT = CJSon.Deserialize <CMessage <CData_Cmd> >(e.message); OnRPTCmdArgs.OnEvented(new CCmdArgs(e.topic, e.message, cmd_RPT)); break; case EMessageType.应答指令: //应答指令 CMessage <CData_Cmd> cmd_REQ = CJSon.Deserialize <CMessage <CData_Cmd> >(e.message); OnREQCmdArgs.OnEvented(new CCmdArgs(e.topic, e.message, cmd_REQ)); break; case EMessageType.状态消息: CMessage <CData_Status> status_data = CJSon.Deserialize <CMessage <CData_Status> >(e.message); OnStatusArgs.OnEvented(new CStatusArgs(e.topic, e.message, status_data)); break; case EMessageType.报警消息: break; case EMessageType.抽样消息: break; default: break; } OnMessageArgs.OnEvented(new CCMessageArgs(e.idNo, e.name, e.topic, e.message, false, msgType.ToString())); } catch (Exception ex) { OnMessageArgs.OnEvented(new CCMessageArgs(e.idNo, e.name, e.topic, e.message, true, ex.ToString())); } }
/// <summary> /// 上传数据 /// </summary> /// <param name="data"></param> /// <param name="status"></param> /// <param name="er"></param> /// <returns></returns> public bool TranSn(CSFCS.CSnData data, out EMesState status, out string er) { er = string.Empty; status = EMesState.正常; try { string url = CIniFile.ReadFromIni("Parameter", "url", iniFile, "http://192.168.50.250:8080/biz/api/") + "tongda/WriteATEProcess02?"; string processCode = CIniFile.ReadFromIni(data.StatName, "ProcessCode", iniFile); string clientCode = CIniFile.ReadFromIni(data.StatName, "ClientCode", iniFile); string createBy = CIniFile.ReadFromIni(data.StatName, "CreateBy", iniFile); CSnResultRequest request = new CSnResultRequest() { ClientCode = clientCode, ProcessCode = processCode, CreateBy = createBy, BarCode = data.SerialNo, DeviceId = data.DeviceId, Flag = (data.Result == 0?1:0), SoftwareVer = "V1.0.0", HardwareVer = "V1.0.0", Ext1 = string.Empty, Ext2 = string.Empty, Ext3 = string.Empty, SIM = string.Empty, ResultDetail = string.Empty }; //测试内容_测试项_值范围_测试值_测试结果(测试结果为PASS或者NG) for (int i = 0; i < data.Item.Count; i++) { if (i > 0) { request.ResultDetail += ","; } request.ResultDetail += data.Item[i].Name + "_"; request.ResultDetail += data.Item[i].Desc + "_"; request.ResultDetail += data.Item[i].LowLimit + data.Item[i].Unit + "~" + data.Item[i].UpLimit + data.Item[i].Unit + "_"; request.ResultDetail += data.Item[i].Value + "_"; request.ResultDetail += data.Item[i].Result == 0 ? "PASS" : "NG"; } CSnResultReponse reponse = null; url += string.Format("ProcessCode={0}" + "&BarCode={1}" + "&HardwareVer={2}" + "&SoftwareVer={3}" + "&DeviceId={4}" + "&SIM={5}" + "&Ext1={6}" + "&Ext2={7}" + "&Ext3={8}" + "&Flag={9}" + "&ResultDetail={10}" + "&CreateBy={11}" + "&ClientCode={12}", request.ProcessCode, request.BarCode, request.HardwareVer, request.SoftwareVer, request.DeviceId, request.SIM, request.Ext1, request.Ext2, request.Ext3, request.Flag, request.ResultDetail, request.CreateBy, request.ClientCode); string requestData = string.Empty; string reponseData = string.Empty; if (!CNet.HttpPost(url, requestData, out reponseData, out er)) { status = EMesState.网络异常; return(false); } reponse = CJSon.Deserialize <CSnResultReponse>(reponseData); if (reponse.status != "200") { status = EMesState.异常错误; er = reponse.msg; return(false); } if (reponse.msg != "SUCCESS") { status = EMesState.异常错误; er = reponse.msg; return(false); } er = requestData; return(true); } catch (Exception ex) { er = ex.ToString(); return(false); } }
/// <summary> /// 检查条码 /// </summary> /// <param name="sn"></param> /// <param name="status"></param> /// <param name="er"></param> /// <returns></returns> public bool CheckSn(CSFCS.CSnInfo sn, out EMesState status, out string er) { er = string.Empty; status = EMesState.正常; try { string url = CIniFile.ReadFromIni("Parameter", "url", iniFile, "http://192.168.50.250:8080/biz/api/") + "tongda/GetATEProcess?"; string processCode = CIniFile.ReadFromIni(sn.StatName, "ProcessCode", iniFile); string clientCode = CIniFile.ReadFromIni(sn.StatName, "ClientCode", iniFile); string createBy = CIniFile.ReadFromIni(sn.StatName, "CreateBy", iniFile); CSnValidationRequest request = new CSnValidationRequest() { ProcessCode = processCode, BarCode = sn.SerialNo, ClientCode = clientCode, CreateBy = createBy }; CSnValidationReponse reponse = null; url += string.Format("ProcessCode={0}&BarCode={1}&CreateBy={2}&ClientCode={3}", request.ProcessCode, request.BarCode, request.CreateBy, request.ClientCode); string requestData = string.Empty; string reponseData = string.Empty; if (!CNet.HttpPost(url, requestData, out reponseData, out er)) { status = EMesState.网络异常; return(false); } reponse = CJSon.Deserialize <CSnValidationReponse>(reponseData); if (reponse.status != "200") { status = EMesState.异常错误; er = reponse.msg; return(false); } if (reponse.msg != "SUCCESS") { status = EMesState.异常错误; er = reponse.msg; return(false); } er = requestData; return(true); } catch (Exception ex) { er = ex.ToString(); return(false); } }
/// <summary> /// 接收消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnMessageRecv(object sender, CMQTT.CMessageArgs e) { try { if (!_recieveTopics.ContainsKey(e.topic)) { OnMessageArgs.OnEvented(new CCMessageArgs(e.idNo, e.name, e.topic, e.message, true, "主题不存在")); return; } CMessage <CData_Cmd> message = CJSon.Deserialize <CMessage <CData_Cmd> >(e.message); EMessageType msgType = _recieveTopics[e.topic]; //解析设备编号 List <CData_Cmd> data = new List <CData_Cmd>(); for (int i = 0; i < message.Data.Count; i++) { if (message.Data[i].ID == "-1") //广播设备编号 { for (int z = 0; z < _devices.Count; z++) { data.Add(new CData_Cmd() { ID = _devices[i].idNo, Name = _devices[i].Name, CmdName = message.Data[i].CmdName, CmdType = message.Data[i].CmdType, CmdInfo = message.Data[i].CmdInfo, Remark1 = message.Data[i].Remark1, Remark2 = message.Data[i].Remark2 }); } break; } else if (IsDeviceId(message.Data[i].ID)) //指定设备编号 { data.Add(new CData_Cmd() { ID = _devices[i].idNo, Name = _devices[i].Name, CmdName = message.Data[i].CmdName, CmdType = message.Data[i].CmdType, CmdInfo = message.Data[i].CmdInfo, Remark1 = message.Data[i].Remark1, Remark2 = message.Data[i].Remark2 }); } } if (data.Count == 0) { OnMessageArgs.OnEvented(new CCMessageArgs(e.idNo, e.name, e.topic, e.message, false, "NULL")); return; } CMessage <CData_Cmd> data_cmd = new CMessage <CData_Cmd>(); data_cmd.Header = message.Header; data_cmd.Data = data; string msg = CJSon.Serializer <List <CData_Cmd> >(data); if (msgType == EMessageType.广播指令) //接收主控端指令指示 { OnCmdRPTArgs.OnEvented(new CCmdArgs(e.topic, msg, data_cmd)); } else if (msgType == EMessageType.应答指令) //接收主端控制指令 { OnCmdREQArgs.OnEvented(new CCmdArgs(e.topic, msg, data_cmd)); } OnMessageArgs.OnEvented(new CCMessageArgs(e.idNo, e.name, e.topic, e.message, false, msgType.ToString())); } catch (Exception ex) { OnMessageArgs.OnEvented(new CCMessageArgs(e.idNo, e.name, e.topic, e.message, true, ex.ToString())); } }
/// <summary> /// 自定义JSON应答客户端数据 /// </summary> /// <param name="recvData"></param> /// <returns></returns> public string ReponseJSON(string recvData) { try { string reposeData = string.Empty; CJSON_REQUEST obj = CJSon.Deserialize <CJSON_REQUEST>(recvData); if (obj == null) { json_reponse.CmdNo = ESOCKET_CMD.NG; json_reponse.ErrCode = ESOCKET_ERROR.COMMAND_IS_NOT_DEFINE; json_reponse.Ready = 0; return(CJSon.Serializer <CJSON_REQUEST>(json_reponse)); } //站别名称 string statName = obj.Name; if (!StatIdList.ContainsKey(statName)) { json_reponse.CmdNo = ESOCKET_CMD.NG; json_reponse.Name = obj.Name; json_reponse.ErrCode = ESOCKET_ERROR.STATION_IS_NOT_EXIST; json_reponse.Ready = 0; return(CJSon.Serializer <CJSON_REQUEST>(json_reponse)); } int statId = StatIdList[statName]; if (obj.UUT.Count != StatRunList[statId].SerialNos.Count) { json_reponse.CmdNo = ESOCKET_CMD.NG; json_reponse.Name = obj.Name; json_reponse.ErrCode = ESOCKET_ERROR.COMMAND_LENTH_ERROR; json_reponse.Ready = 0; return(CJSon.Serializer <CJSON_REQUEST>(json_reponse)); } //命令类型 switch (obj.CmdNo) { case ESOCKET_CMD.QUERY_STATE: json_reponse.CmdNo = ESOCKET_CMD.STATE_OK; json_reponse.Name = obj.Name; json_reponse.IdCard = StatRunList[statId].IdCard; json_reponse.Model = StatRunList[statId].ModelName; json_reponse.MesFlag = StatRunList[statId].MesFlag; json_reponse.UUT.Clear(); if (StatRunList[statId].DoRun == ERUN.空闲 || StatRunList[statId].DoRun == ERUN.测试结束) { json_reponse.ErrCode = ESOCKET_ERROR.OK; json_reponse.Ready = 0; for (int i = 0; i < StatRunList[statId].SerialNos.Count; i++) { json_reponse.UUT.Add(new CJSON_UUT()); json_reponse.UUT[i].SerialNo = string.Empty; json_reponse.UUT[i].Result = 0; } } else { int ready = StatRunList[statId].SubNo; json_reponse.ErrCode = ESOCKET_ERROR.OK; json_reponse.Ready = ready; json_reponse.UUT.Clear(); for (int i = 0; i < StatRunList[statId].SerialNos.Count; i++) { json_reponse.UUT.Add(new CJSON_UUT()); json_reponse.UUT[i].SerialNo = StatRunList[statId].SerialNos[i]; json_reponse.UUT[i].Result = 0; } } break; case ESOCKET_CMD.START_TEST: if (StatRunList[statId].DoRun == ERUN.空闲) { json_reponse.CmdNo = ESOCKET_CMD.NG; json_reponse.Name = obj.Name; json_reponse.ErrCode = ESOCKET_ERROR.STATION_IS_NOT_READY; json_reponse.Ready = 0; json_reponse.UUT.Clear(); for (int i = 0; i < StatRunList[statId].SerialNos.Count; i++) { json_reponse.UUT.Add(new CJSON_UUT()); json_reponse.UUT[i].SerialNo = string.Empty; json_reponse.UUT[i].Result = 0; } return(CJSon.Serializer <CJSON_REQUEST>(json_reponse)); } else { json_reponse.ErrCode = ESOCKET_ERROR.OK; json_reponse.CmdNo = ESOCKET_CMD.START_OK; json_reponse.Name = obj.Name; json_reponse.IdCard = StatRunList[statId].IdCard; json_reponse.Model = StatRunList[statId].ModelName; json_reponse.MesFlag = StatRunList[statId].MesFlag; json_reponse.Ready = StatRunList[statId].SubNo; json_reponse.UUT.Clear(); for (int i = 0; i < StatRunList[statId].SerialNos.Count; i++) { json_reponse.UUT.Add(new CJSON_UUT()); json_reponse.UUT[i].SerialNo = StatRunList[statId].SerialNos[i]; json_reponse.UUT[i].Result = 0; } StatRunList[statId].DoRun = ERUN.测试中; } break; case ESOCKET_CMD.END_TEST: if (StatRunList[statId].DoRun == ERUN.空闲) { json_reponse.CmdNo = ESOCKET_CMD.NG; json_reponse.Name = obj.Name; json_reponse.ErrCode = ESOCKET_ERROR.STATION_IS_NOT_READY; json_reponse.Ready = 0; return(CJSon.Serializer <CJSON_REQUEST>(json_reponse)); } else { json_reponse.ErrCode = ESOCKET_ERROR.OK; json_reponse.CmdNo = ESOCKET_CMD.END_OK; json_reponse.Name = obj.Name; json_reponse.IdCard = StatRunList[statId].IdCard; json_reponse.Model = StatRunList[statId].ModelName; json_reponse.MesFlag = StatRunList[statId].MesFlag; json_reponse.Ready = StatRunList[statId].SubNo; json_reponse.UUT.Clear(); for (int i = 0; i < StatRunList[statId].SerialNos.Count; i++) { json_reponse.UUT.Add(new CJSON_UUT()); socket_reponse.UUT[i].SerialNo = StatRunList[statId].SerialNos[i]; StatRunList[statId].Result[i] = obj.UUT[i].Result; } StatRunList[statId].DoRun = ERUN.测试结束; } break; default: json_reponse.CmdNo = ESOCKET_CMD.NG; json_reponse.ErrCode = ESOCKET_ERROR.COMMAND_IS_NOT_DEFINE; json_reponse.Ready = 0; break; } return(CJSon.Serializer <CJSON_REQUEST>(json_reponse)); } catch (Exception) { json_reponse.CmdNo = ESOCKET_CMD.NG; json_reponse.ErrCode = ESOCKET_ERROR.SYSTEM_ERROR; json_reponse.Ready = 0; return(CJSon.Serializer <CJSON_REQUEST>(json_reponse)); } finally { } }