private void SendDataToPlatform(string target_comm) { ReqHeader header = new ReqHeader(); string txData; if (target_comm == "oneM2M") { //header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1/csr-m2m_01222990847/cnt-TEMP"; header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1/csr-m2m_" + tbDeviceCTN.Text + "/cnt-" + tbContainer.Text; txData = tbData.Text; } else { header.Url = brkUrlL + "/" + tbLwM2MEntityID.Text + "/10250/0/1"; //header.Url = brkUrlL + "/IN_CSE-BASE-1/cb-1/" + deviceEntityId + "/10250/0/1"; txData = tbLwM2MData.Text; } header.Method = "POST"; header.X_M2M_Origin = svr.entityId; header.X_M2M_RI = DateTime.Now.ToString("yyyyMMddHHmmss") + "data_send"; header.X_MEF_TK = svr.token; header.X_MEF_EKI = svr.enrmtKeyId; header.X_M2M_NM = string.Empty; header.Accept = "application/vnd.onem2m-res+xml"; header.ContentType = "application/vnd.onem2m-res+xml;ty=4"; string packetStr = "<m2m:cin xmlns:m2m=\"http://www.onem2m.org/xml/protocols\">"; packetStr += "<cnf>text/plain</cnf>"; packetStr += "<con>" + txData + "</con>"; packetStr += "</m2m:cin>"; string retStr = SendHttpRequest(header, packetStr); //if (retStr != string.Empty) // LogWrite(retStr); }
// 1. MEF 인증 private void RequestMEF() { ReqHeader header = new ReqHeader(); header.Url = mefUrl + "/mef/server"; header.Method = "POST"; header.ContentType = "application/xml"; header.X_M2M_RI = string.Empty; header.X_M2M_Origin = string.Empty; header.X_MEF_TK = string.Empty; header.X_MEF_EKI = string.Empty; header.X_M2M_NM = string.Empty; string packetStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; packetStr += "<auth>"; packetStr += "<svcSvrCd>" + svr.svcSvrCd + "</svcSvrCd>"; packetStr += "<svcCd>" + svr.svcCd + "</svcCd>"; packetStr += "<svcSvrNum>" + svr.svcSvrNum + "</svcSvrNum>"; packetStr += "</auth>"; LogWrite("----------MEF 인증----------"); string retStr = SendHttpRequest(header, packetStr); // xml if (retStr != string.Empty) { ParsingXml(retStr); string nameCSR = svr.entityId.Replace("-", ""); lbremoteCSEName.Text = "csr-" + nameCSR; svr.remoteCSEName = lbremoteCSEName.Text; //LogWrite("svr.remoteCSEName = " + svr.remoteCSEName); } }
// 3. RemoteCSE-Update private void ReqRemoteCSEUpdate() { ReqHeader header = new ReqHeader(); header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1/" + svr.remoteCSEName; header.Method = "PUT"; header.Accept = "application/vnd.onem2m-res+json"; header.ContentType = "application/vnd.onem2m-res+json"; header.X_M2M_RI = DateTime.Now.ToString("yyyyMMddHHmmss") + "RemoteCSE_Update"; header.X_M2M_Origin = svr.entityId; header.X_MEF_TK = svr.token; header.X_MEF_EKI = svr.enrmtKeyId; header.X_M2M_NM = string.Empty; string packetStr = "<m2m:csr xmlns:m2m=\"http://www.onem2m.org/xml/protocols\">"; packetStr += "<cst>3</cst>"; packetStr += "<cb>/" + svr.entityId + "</cb>"; packetStr += "<csi>/" + svr.entityId + "/cb-1</csi>"; packetStr += "<rr>true</rr>"; packetStr += "<poa>" + tbSeverIP.Text + ":" + tbSeverPort.Text + "</poa>"; packetStr += "</m2m:csr>"; string retStr = SendHttpRequest(header, packetStr); //if (retStr != string.Empty) // LogWrite(retStr); }
// 3. RemoteCSE-Create private void ReqRemoteCSECreate() { ReqHeader header = new ReqHeader(); header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1"; header.Method = "POST"; header.Accept = "application/vnd.onem2m-res+json"; header.ContentType = "application/vnd.onem2m-res+json;ty=16"; header.X_M2M_RI = "RemoteCSE_Create_1"; header.X_M2M_Origin = svr.entityId; header.X_MEF_TK = svr.token; header.X_MEF_EKI = "p_YI1FZnZwyPajxwKYv15g"; header.X_M2M_NM = svr.remoteCSEName; var obj = new JObject(); obj.Add("cst", "3"); obj.Add("cb", "/" + svr.entityId + "/cb-1"); obj.Add("csi", "/" + svr.entityId); obj.Add("rr", "true"); var arr = new JArray(); arr.Add("http://" + httpServerIp + ":" + httpServerPort); obj.Add("poa", arr); //LogWriteobj.ToString()); string retStr = SendHttpRequest(header, obj.ToString()); if (retStr != string.Empty) { LogWrite(retStr); // 이미 같은 이름으로 생성되어 있다면 응답 : {"message": "CONFLICT_INVALID_RESOURCE_NAME"} } }
// 3. RemoteCSE-Delete private void ReqRemoteCSEDEL() { ReqHeader header = new ReqHeader(); header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1/" + svr.remoteCSEName; header.Method = "DELETE"; header.Accept = "application/vnd.onem2m-res+xml"; header.X_M2M_RI = DateTime.Now.ToString("yyyyMMddHHmmss") + "RemoteCSE_Delete"; header.X_M2M_Origin = svr.entityId; header.X_MEF_TK = svr.token; header.X_MEF_EKI = svr.enrmtKeyId; header.X_M2M_NM = string.Empty; string retStr = SendHttpRequest(header, string.Empty); //if (retStr != string.Empty) // LogWrite(retStr); }
// 2. CSEBase-GET : oneM2M 접속 확인 private void ReqCSEBaseGET() { ReqHeader header = new ReqHeader(); header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1"; header.Method = "GET"; header.Accept = "application/xml"; header.X_M2M_RI = DateTime.Now.ToString("yyyyMMddHHmmss") + "CSEBase"; header.X_M2M_Origin = svr.entityId; header.X_MEF_TK = svr.token; header.X_MEF_EKI = svr.enrmtKeyId; header.X_M2M_NM = string.Empty; string retStr = SendHttpRequest(header, string.Empty); //if (retStr != string.Empty) // LogWrite(retStr); }
private void RetriveDataToPlatform() { ReqHeader header = new ReqHeader(); //header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1/csr-m2m_01222990847/cnt-TEMP/la"; header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1/csr-m2m_" + tbDeviceCTN.Text + "/cnt-" + tbContainer.Text + "/la"; header.Method = "GET"; header.X_M2M_Origin = svr.entityId; header.X_M2M_RI = DateTime.Now.ToString("yyyyMMddHHmmss") + "data_retrive"; header.X_MEF_TK = svr.token; header.X_MEF_EKI = svr.enrmtKeyId; header.X_M2M_NM = string.Empty; header.Accept = "application/xml"; header.ContentType = string.Empty; string retStr = SendHttpRequest(header, string.Empty); if (retStr != string.Empty) { string format = string.Empty; string value = string.Empty; XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(retStr); //LogWrite(xDoc.OuterXml.ToString()); XmlNodeList xnList = xDoc.SelectNodes("/*"); //접근할 노드 foreach (XmlNode xn in xnList) { format = xn["cnf"].InnerText; // data format value = xn["con"].InnerText; // data value } //LogWrite("value = " + value); //LogWrite("format = " + format); if (format == "application/octet-stream") { lboneM2MRxData.Text = Encoding.UTF8.GetString(Convert.FromBase64String(value)); } else { lboneM2MRxData.Text = value; } } }
// 3. RemoteCSE-Get private void ReqRemoteCSEGet() { ReqHeader header = new ReqHeader(); header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1/" + svr.remoteCSEName; header.Method = "GET"; header.Accept = "application/vnd.onem2m-res+json"; header.X_M2M_RI = "RemoteCSE_Retrieve_1"; header.X_M2M_Origin = svr.entityId; header.X_MEF_TK = svr.token; header.X_MEF_EKI = "p_YI1FZnZwyPajxwKYv15g"; string retStr = SendHttpRequest(header, string.Empty); if (retStr != string.Empty) { LogWrite(retStr); } }
// 2. CSEBase-GET : oneM2M 접속 확인 private void ReqCSEBaseGET() { ReqHeader header = new ReqHeader(); header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1"; header.Method = "GET"; header.Accept = "application/json"; header.X_M2M_RI = "CSEBase_Retrieve_1"; header.X_M2M_Origin = svr.entityId; header.X_MEF_TK = svr.token; header.X_MEF_EKI = "p_YI1FZnZwyPajxwKYv15g"; string retStr = SendHttpRequest(header, string.Empty); if (retStr != string.Empty) { LogWrite(retStr); } }
private void DeviceCheckToPlatform() { ReqHeader header = new ReqHeader(); header.Url = brkUrlL + "/" + tbLwM2MEntityID.Text + "/10250/0/0"; header.Method = "GET"; header.X_M2M_Origin = svr.entityId; header.X_M2M_RI = DateTime.Now.ToString("yyyyMMddHHmmss") + "device_status"; header.X_MEF_TK = svr.token; header.X_MEF_EKI = svr.enrmtKeyId; header.X_M2M_NM = string.Empty; header.Accept = "application/vnd.onem2m-res+xml"; header.ContentType = "application/vnd.onem2m-res+xml;ty=4"; string retStr = SendHttpRequest(header, string.Empty); if (retStr != string.Empty) { string format = string.Empty; string value = string.Empty; XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(retStr); //LogWrite(xDoc.OuterXml.ToString()); XmlNodeList xnList = xDoc.SelectNodes("/*"); //접근할 노드 foreach (XmlNode xn in xnList) { format = xn["cnf"].InnerText; // data format value = xn["con"].InnerText; // data value } //LogWrite("value = " + value); //LogWrite("format = " + format); if (format == "application/octet-stream") { lbLwM2MRxData.Text = Encoding.UTF8.GetString(Convert.FromBase64String(value)); } else { lbLwM2MRxData.Text = value; } } }
// 3. RemoteCSE-Create private void ReqRemoteCSECreate() { ReqHeader header = new ReqHeader(); header.Url = brkUrl + "/IN_CSE-BASE-1/cb-1"; header.Method = "POST"; header.Accept = "application/vnd.onem2m-res+xml"; header.ContentType = "application/vnd.onem2m-res+xml;ty=16"; header.X_M2M_RI = DateTime.Now.ToString("yyyyMMddHHmmss") + "RemoteCSE_Create"; header.X_M2M_Origin = svr.entityId; header.X_MEF_TK = svr.token; header.X_MEF_EKI = svr.enrmtKeyId; header.X_M2M_NM = svr.remoteCSEName; string packetStr = "<m2m:csr xmlns:m2m=\"http://www.onem2m.org/xml/protocols\">"; packetStr += "<cst>3</cst>"; packetStr += "<cb>/" + svr.entityId + "</cb>"; packetStr += "<csi>/" + svr.entityId + "/cb-1</csi>"; packetStr += "<rr>true</rr>"; packetStr += "<poa>" + tbSeverIP.Text + ":" + tbSeverPort.Text + "</poa>"; packetStr += "</m2m:csr>"; string retStr = SendHttpRequest(header, packetStr); /* * var obj = new JObject(); * obj.Add("cst", "3"); * obj.Add("cb", "/" + svr.entityId + "/cb-1"); * obj.Add("csi", "/" + svr.entityId); * obj.Add("rr", "true"); * var arr = new JArray(); * //arr.Add("http://172.17.224.57:8180"); * arr.Add("http://" + tbSeverIP.Text + ":" + tbSeverPort.Text); * obj.Add("poa", arr); * //LogWriteobj.ToString()); * string retStr = SendHttpRequest(header, obj.ToString()); */ //if (retStr != string.Empty) //{ // LogWrite(retStr); // // 이미 같은 이름으로 생성되어 있다면 응답 : {"message": "CONFLICT_INVALID_RESOURCE_NAME"} //} }
private void SendDataToPlatform() { ReqHeader header = new ReqHeader(); header.Url = brkUrl + "/" + deviceEntityId + "/10250/0/1"; header.Method = "POST"; header.ContentType = "application/vnd.onem2m-res+xml;ty=4"; header.X_M2M_RI = "data_send_1"; header.X_M2M_Origin = svr.entityId; header.X_MEF_TK = svr.token; header.X_MEF_EKI = "p_YI1FZnZwyPajxwKYv15g"; string data = Convert.ToBase64String(Encoding.UTF8.GetBytes(tbData.Text)); string packetStr = "<m2m:cin xmlns:m2m=\"http://www.onem2m.org/xml/protocols\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.onem2m.org/xml/protocols CDT-contentInstance-v1_0_0.xsd\">"; packetStr += "<cnf>application/octet-stream</cnf>"; packetStr += "<con>" + data + "</con>"; packetStr += "</m2m:cin>"; string retStr = SendHttpRequest(header, packetStr); if (retStr != string.Empty) { LogWrite(retStr); } }
// 1. MEF 인증 private void RequestMEF() { ReqHeader header = new ReqHeader(); header.Url = mefUrl + "/mef/server"; header.Method = "POST"; header.ContentType = "application/xml"; string packetStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; packetStr += "<auth>"; packetStr += "<svcSvrCd>" + svr.svcSvrCd + "</svcSvrCd>"; packetStr += "<svcCd>" + svr.svcCd + "</svcCd>"; packetStr += "<svcSvrNum>" + svr.svcSvrNum + "</svcSvrNum>"; packetStr += "</auth>"; LogWrite("----------MEF 인증----------"); string retStr = SendHttpRequest(header, packetStr); // xml if (retStr != string.Empty) { ParsingXml(retStr); } }
public string SendHttpRequest(ReqHeader header, string data) { string resResult = string.Empty; try { wReq = (HttpWebRequest)WebRequest.Create(header.Url); wReq.Method = header.Method; if (header.Accept != string.Empty) { wReq.Accept = header.Accept; } if (header.ContentType != string.Empty) { wReq.ContentType = header.ContentType; } if (header.X_M2M_RI != string.Empty) { wReq.Headers.Add("X-M2M-RI", header.X_M2M_RI); } if (header.X_M2M_Origin != string.Empty) { wReq.Headers.Add("X-M2M-Origin", header.X_M2M_Origin); } if (header.X_M2M_NM != string.Empty) { wReq.Headers.Add("X-M2M-NM", header.X_M2M_NM); } if (header.X_MEF_TK != string.Empty) { wReq.Headers.Add("X-MEF-TK", header.X_MEF_TK); } if (header.X_MEF_EKI != string.Empty) { wReq.Headers.Add("X-MEF-EKI", header.X_MEF_EKI); } LogWriteNoTime(wReq.Method + " " + wReq.RequestUri + " HTTP/1.1"); LogWriteNoTime(""); for (int i = 0; i < wReq.Headers.Count; ++i) { LogWriteNoTime(wReq.Headers.Keys[i] + ": " + wReq.Headers[i]); } LogWriteNoTime(""); LogWriteNoTime(data); LogWriteNoTime(""); // POST 전송일 경우 if (header.Method == "POST") { byte[] byteArray = Encoding.UTF8.GetBytes(data); Stream dataStream = wReq.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); } LogWrite("----------Response from oneM2M----------"); wReq.Timeout = 20000; // 서버 응답을 20초동안 기다림 using (wRes = (HttpWebResponse)wReq.GetResponse()) { LogWriteNoTime("HTTP/1.1 " + (int)wRes.StatusCode + " " + wRes.StatusCode.ToString()); LogWriteNoTime(""); for (int i = 0; i < wRes.Headers.Count; ++i) { LogWriteNoTime("[" + wRes.Headers.Keys[i] + "] " + wRes.Headers[i]); } LogWriteNoTime(""); Stream respPostStream = wRes.GetResponseStream(); StreamReader readerPost = new StreamReader(respPostStream, Encoding.GetEncoding("UTF-8"), true); resResult = readerPost.ReadToEnd(); LogWriteNoTime(resResult); LogWriteNoTime(""); } return(resResult); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse)ex.Response; LogWriteNoTime("HTTP/1.1 " + (int)resp.StatusCode + " " + resp.StatusCode.ToString()); LogWriteNoTime(""); for (int i = 0; i < resp.Headers.Count; ++i) { LogWriteNoTime(" " + resp.Headers.Keys[i] + ": " + resp.Headers[i]); } LogWriteNoTime(""); Stream respPostStream = resp.GetResponseStream(); StreamReader readerPost = new StreamReader(respPostStream, Encoding.GetEncoding("UTF-8"), true); string resError = readerPost.ReadToEnd(); LogWriteNoTime(resError); LogWriteNoTime(""); //LogWrite("[" + (int)resp.StatusCode + "] " + resp.StatusCode.ToString()); } else { LogWrite(ex.ToString()); } return(resResult); } }