iMAPSetAccessKey.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package kr.co.icontrols.wallpadconfig.imap;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.List;
  5. import android.content.Context;
  6. import com.artncore.WallPadDataMgr.WallpadSmartKeyRegData.SmartKeyDataClass;
  7. import com.artncore.WallPadDataMgr.WallpadStatusData;
  8. import com.artncore.WallPadDataMgr.wallpaddbmgr.AddressSet;
  9. import com.artncore.wallpadimap.RetProci;
  10. import com.artncore.wallpadimap.WallPadiMAPClient;
  11. /*
  12. class KEY_INFO
  13. {
  14. private int index = 0;
  15. private String sn = null;
  16. private String uid = null;
  17. private int key_num = 0;
  18. private int issue_cnt = 0;
  19. private String issue_date = null;
  20. private String message = null;
  21. /**
  22. * @param Index 인덱스정보
  23. * @param Sn 시리얼 넘버(미사용시 "null" 입력)
  24. * @param Uid UID정보 (미사용시 "null" 입력)
  25. * @param Key_num 키 또는 카드 번호
  26. * @param Issue_cnt 발급 횟수
  27. * @param Issue_date 발급날짜
  28. * @param message 부가정보 (미사용시 "null" 입력)
  29. *
  30. * **/
  31. /*
  32. public KEY_INFO (int Index, String Sn, String Uid, int Key_num, int Issue_cnt, String Issue_date, String Message)
  33. {
  34. this.index = Index;
  35. this.sn = Sn;
  36. this.uid = Uid;
  37. this.key_num = Key_num;
  38. this.issue_cnt = Issue_cnt;
  39. this.issue_date = Issue_date;
  40. this.message = Message;
  41. }
  42. public int getIndex(){return index;}
  43. public String getSn(){return sn;}
  44. public String getUID(){return uid;}
  45. public int getKey_Num(){return key_num;}
  46. public int getIssue_Cnt(){return issue_cnt;}
  47. public String getIssue_date(){return issue_date;}
  48. public String getMessage(){return message;}
  49. }
  50. */
  51. /**
  52. * @description
  53. * 본 Class는 iMAP API를 사용하기위해 필요한 클래스이다.<br>
  54. * Xml 통신을 사용하는 기능별로 하나의 클래스를 만들어 쓰는 것이 좋음.<br>
  55. * 예) 원격검침(iMAPMeteringClass), 주민투표(iMAPVotingClass) 등<br><br>
  56. *
  57. * 만약 간단하게 메시지 송 수신 하려면 본 클래스에서 GetXML() 수정하고<br>
  58. * 상위UI에서 SetItem, GetAttributeVal, GetItemVal을 사용하여 XML데이터를 <br>
  59. * 뽑아 사용하면 됨.<br><br>
  60. *
  61. * @example
  62. * Log.d("TAG", "Button Touched");<br>
  63. final iMAPBasicClass imapBasicClass = new iMAPBasicClass(getApplicationContext()); //객체 생성<br>
  64. imapBasicClass.SetServerIPcfg("10.2.12.3", 10000); //보낼 IP 및 포트 설정<br>
  65. imapBasicClass.DoWork(0);<br><br>
  66. imapBasicClass.SetRetCtr(new RetProci(){<br><br>
  67. &nbsp;@Override<br>
  68. public void ProcSuccess() {<br>
  69. // TODO Auto-generated method stub<br><br>
  70. //* 정상적인 메시지가 수신되면 이곳이 실행됨<br>
  71. //* 중간에 짤리거나 비정상적인 메시지는 이곳을 실행하지 않고 'ProcError'가 실행됨<br><br>
  72. *
  73. * *** Test XML Message ***<br>
  74. &lt;?xml version="1.0" encoding="utf-8"?&gt;<br>
  75. &lt;imap ver = "1.0" address = "10.2.12.3" sender = "단지서버"&gt;<br>
  76. &lt;service type = "reply" name= "event_notify" result = "ok"&gt;<br>
  77. &lt;event&gt; "elevator_up" &lt;/event&gt;<br>
  78. &lt;event&gt; "elevator_down" &lt;/event&gt;<br>
  79. &lt;event&gt; "elevator_stop" &lt;/event&gt;<br>
  80. &lt;valuelist year = "2007" month = "1" day = "0"elect_read ="40" elect_used ="38" gas_read ="5" gas_used ="4"/&gt;<br>
  81. &lt;/service&gt;<br>
  82. &lt;/imap&gt;<br><br>
  83. // 위 같은 메시지가 수신되었을때 아래 로그 출력 되는 값 참고하여 개발 요망!<br><br>
  84. Log.d(TAG, "ProcSuccess");<br><br>
  85. int ElementCount = imapBasicClass.SetItem("service");<br>
  86. Log.d(TAG, "ElementCount :"+ElementCount); //1<br><br>
  87. String AttributeType = imapBasicClass.GetAttributeVal(0, "type");<br>
  88. Log.d(TAG, "AttributeType :"+AttributeType); //"reply"<br><br>
  89. String AttributeName = imapBasicClass.GetAttributeVal(0, "name");<br>
  90. Log.d(TAG, "AttributeName :"+AttributeName); //"event_notify"<br><br>
  91. String AttributeResult = imapBasicClass.GetAttributeVal(0, "result");<br>
  92. Log.d(TAG, "AttributeResult :"+AttributeResult); //"ok"<br><br>
  93. int ElementCountEvent = imapBasicClass.SetItem("event");<br>
  94. Log.d(TAG, "ElementCount_Next :"+ElementCountEvent); //3<br><br>
  95. String AttributeEvent1 = imapBasicClass.GetItemVal(0);<br>
  96. Log.d(TAG, "AttributeEvent1 :"+AttributeEvent1); //"elevator_up"<br><br>
  97. String AttributeEvent2 = imapBasicClass.GetItemVal(1);<br>
  98. Log.d(TAG, "AttributeEvent2 :"+AttributeEvent2); //"elevator_down"<br><br>
  99. String AttributeEvent3 = imapBasicClass.GetItemVal(2);<br>
  100. Log.d(TAG, "AttributeEvent3 :"+AttributeEvent3); //"elevator_stop"<br><br>
  101. int ElementCount_valuelist = imapBasicClass.SetItem("valuelist");<br>
  102. Log.d(TAG, "ElementCount_valuelist :"+ElementCount_valuelist); //1<br><br>
  103. String AttributeYear = imapBasicClass.GetAttributeVal(0, "year");<br>
  104. Log.d(TAG, "AttributeYear :"+AttributeYear); //"2007"<br>
  105. }<br><br>
  106. &nbsp;@Override<br>
  107. public void ProcError() {<br>
  108. // TODO Auto-generated method stub<br>
  109. Log.d("Hyunsu", "ProcError");<br>
  110. }<br><br>
  111. &nbsp;@Override<br>
  112. public void ProcDoing() {<br>
  113. // TODO Auto-generated method stub<br>
  114. }});<br>
  115. */
  116. public class iMAPSetAccessKey extends WallPadiMAPClient{
  117. String myIP="NONE";
  118. String myDong="NONE";
  119. String myHo="NONE";
  120. Context ctx;
  121. RetProci Proc;
  122. String XMLHeader;
  123. WallpadStatusData DBMGR;
  124. String ServerIP = "10.0.1.10";
  125. int SvrPort = 10000;
  126. //final WallpadStatusData wallpadStatusData = new WallpadStatusData(ctx); //객체 생성
  127. //카드정보
  128. int mKey_Type;
  129. ID id = new ID();
  130. public static final class ID
  131. {
  132. //키 정보 설정 옵션
  133. public final static int SAVE = 1;
  134. public final static int DELETE = 2;
  135. public final static int RENEW = 3;
  136. //키 종류
  137. public final static int HKEY = 1;
  138. public final static int DKEY = 2;
  139. public final static int MKEY = 3;
  140. }
  141. public iMAPSetAccessKey(Context ctx)
  142. {
  143. DBMGR = new WallpadStatusData(ctx);
  144. AddressSet addc = DBMGR.getAddressMGR(); //DB에서 동호 정보 받아옴
  145. DBMGR.closeDB();
  146. //동, 호, ip 정보 저장
  147. myIP = addc.IPAddress;
  148. myDong = addc.Dong;
  149. myHo = addc.Ho;
  150. //XML Header 생성
  151. XMLHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"+
  152. "<imap ver = \"1.0\" address = \""+myIP+"\" sender = \""+myDong+"동 "+myHo+"호"+"\">\r\n" ;
  153. setContext(ctx);
  154. this.ctx = ctx;
  155. }
  156. private String SetXMLInfo(int actionType, int keyType)
  157. {
  158. String retXML = null;
  159. String sActionType = "renew";
  160. String sKeyType = "hkey";
  161. if(actionType == ID.SAVE)
  162. {
  163. sActionType = "save";
  164. }
  165. else if (actionType == ID.DELETE)
  166. {
  167. sActionType = "delete";
  168. }
  169. else if (actionType == ID.RENEW)
  170. {
  171. sActionType = "renew";
  172. }
  173. if(keyType == ID.HKEY)
  174. {
  175. sKeyType = "hkey";
  176. }
  177. else if (keyType == ID.DKEY)
  178. {
  179. sKeyType = "dkey";
  180. }
  181. else if (keyType == ID.MKEY)
  182. {
  183. sKeyType = "mkey";
  184. }
  185. mKey_Type = keyType;
  186. retXML = XMLHeader +
  187. " <service type=\"request\" name=\"set_accesskey_info\">\r\n"+
  188. " <action>\""+sActionType+"\"</action>\r\n" +
  189. " <key_type>\""+sKeyType+"\"</key_type>\r\n";
  190. return retXML;
  191. }
  192. @SuppressWarnings("unused")
  193. private String SetXML_KeyInfo(List<SmartKeyDataClass> getList)
  194. {
  195. String retXML = null;
  196. SimpleDateFormat CurYearFormat = new SimpleDateFormat("yyyy");
  197. SimpleDateFormat CurMonthFormat = new SimpleDateFormat("MM");
  198. SimpleDateFormat CurDayFormat = new SimpleDateFormat("dd");
  199. SimpleDateFormat CurHourFormat = new SimpleDateFormat("HH");
  200. SimpleDateFormat CurMinuteFormat = new SimpleDateFormat("mm");
  201. int ListSize = getList.size();
  202. if(mKey_Type == ID.HKEY)
  203. {
  204. retXML = " <hkey_info dong=\""+myDong+"\" ho=\""+myHo+"\" count=\""+ListSize+"\" />";
  205. }
  206. else if (mKey_Type == ID.DKEY)
  207. {
  208. retXML = " <dkey_info dong=\""+myDong+"\" count=\""+ListSize+"\" />";
  209. }
  210. else if (mKey_Type == ID.MKEY)
  211. {
  212. retXML = " <mkey_info count=\""+ListSize+"\" />";
  213. }
  214. retXML += "\r\n";
  215. if(ListSize > 0)
  216. {
  217. for(int i = 0; i<ListSize; i++)
  218. {
  219. Date tempDate = new Date(getList.get(i).RegDateCal.getTimeInMillis());
  220. String strYear = CurYearFormat.format(tempDate);
  221. String strMonth = CurMonthFormat.format(tempDate);
  222. String strDay = CurDayFormat.format(tempDate);
  223. String strHour = CurHourFormat.format(tempDate);
  224. String strMinute = CurHourFormat.format(tempDate);
  225. String temp_Issued_data = strYear+"."+strMonth+"."+strDay+"."+strHour+"."+strMinute;
  226. retXML += " <key_info index=\""+getList.get(i).id+"\" "
  227. + "sn=\""+getList.get(i).SerialNumber+"\" "
  228. + "uid=\""+getList.get(i).Uid+"\" "
  229. + "key_num=\""+getList.get(i).id+"\" "
  230. + "issue_cnt=\""+1+"\" "
  231. + "issued_date=\""+temp_Issued_data+"\" "
  232. + "message=\""+"null"+"\" "
  233. + "/>\r\n";
  234. }
  235. }
  236. retXML += " </service>\r\n</imap>";
  237. return retXML;
  238. }
  239. /**
  240. * 관리PC IP 및 포트 설정 <br>
  241. * 최초 생성 시 한번은 설정 해야함
  242. * **/
  243. public void SetAdminPC_IPcfg(String IPa, int port)
  244. {
  245. ServerIP = IPa;
  246. SvrPort = port;
  247. }
  248. public void SetRetCtr(RetProci retProci)
  249. {
  250. Proc = retProci;
  251. }
  252. protected void ProcComplite()
  253. {
  254. if(Proc != null)
  255. {
  256. SetItem("service");
  257. String result = GetAttributeVal(0, "result");
  258. if (result!=null)
  259. {
  260. if (result.equalsIgnoreCase("ok"))
  261. {
  262. String name = GetAttributeVal(0, "name");
  263. String type = GetAttributeVal(0, "type");
  264. if((name!=null && name.equalsIgnoreCase("set_accesskey_info")) &&
  265. (type!=null && type.equalsIgnoreCase("reply")))
  266. {
  267. Proc.ProcSuccess();
  268. }
  269. else
  270. {
  271. Proc.ProcError();
  272. }
  273. }
  274. else if (result.equalsIgnoreCase("fail"))
  275. {
  276. Proc.ProcError();
  277. }
  278. else
  279. {
  280. Proc.ProcError();
  281. }
  282. }
  283. else
  284. {
  285. Proc.ProcError();
  286. }
  287. }
  288. }
  289. protected void ProcError()
  290. {
  291. Proc.ProcError();
  292. }
  293. /**
  294. * set_accesskey_info 메시지를 관리PC로 보낼때 사용하는 함수임
  295. *
  296. * @param actionType ID.SAVE / ID.DELETE / ID.RENEW or 1 / 2 / 3
  297. * @param keyType ID.HKEY / ID.DKEY / ID.MKEY or 1 / 2 / 3
  298. * @param getList ArrayList<KEY_INFO>형 어레이 리스트 생성 후 삽입<br>
  299. * 예) ArrayList<KEY_INFO> arrayList = new ArrayList<KEY_INFO>();<br>
  300. * arrayList.add(new KEY_INFO(1, "null", "null", 1, 1, "null", "null"));
  301. *
  302. * @return boolean 형 타입 - true면 메시지 송신 성공. false면 메시지 송신 실패
  303. * **/
  304. public boolean DoWork(int actionType, int keyType, List<SmartKeyDataClass> getList) //1이면 List, 2이면 Contents
  305. {
  306. SetServerIPConfig(ServerIP, SvrPort);
  307. String SendMessage = null;
  308. if(actionType <= 0)
  309. {
  310. return false;
  311. }
  312. if(keyType <= 0)
  313. {
  314. return false;
  315. }
  316. if(getList == null)
  317. {
  318. return false;
  319. }
  320. SendMessage = SetXMLInfo(actionType, keyType);
  321. SendMessage += SetXML_KeyInfo(getList);
  322. SendXMLDoc(SendMessage, true);
  323. return true;
  324. }
  325. }