package kr.co.icontrols.wallpadconfig.imap;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.content.Context;
import com.artncore.WallPadDataMgr.WallpadSmartKeyRegData.SmartKeyDataClass;
import com.artncore.WallPadDataMgr.WallpadStatusData;
import com.artncore.WallPadDataMgr.wallpaddbmgr.AddressSet;
import com.artncore.wallpadimap.RetProci;
import com.artncore.wallpadimap.WallPadiMAPClient;
/*
class KEY_INFO
{
private int index = 0;
private String sn = null;
private String uid = null;
private int key_num = 0;
private int issue_cnt = 0;
private String issue_date = null;
private String message = null;
/**
* @param Index 인덱스정보
* @param Sn 시리얼 넘버(미사용시 "null" 입력)
* @param Uid UID정보 (미사용시 "null" 입력)
* @param Key_num 키 또는 카드 번호
* @param Issue_cnt 발급 횟수
* @param Issue_date 발급날짜
* @param message 부가정보 (미사용시 "null" 입력)
*
* **/
/*
public KEY_INFO (int Index, String Sn, String Uid, int Key_num, int Issue_cnt, String Issue_date, String Message)
{
this.index = Index;
this.sn = Sn;
this.uid = Uid;
this.key_num = Key_num;
this.issue_cnt = Issue_cnt;
this.issue_date = Issue_date;
this.message = Message;
}
public int getIndex(){return index;}
public String getSn(){return sn;}
public String getUID(){return uid;}
public int getKey_Num(){return key_num;}
public int getIssue_Cnt(){return issue_cnt;}
public String getIssue_date(){return issue_date;}
public String getMessage(){return message;}
}
*/
/**
* @description
* 본 Class는 iMAP API를 사용하기위해 필요한 클래스이다.
* Xml 통신을 사용하는 기능별로 하나의 클래스를 만들어 쓰는 것이 좋음.
* 예) 원격검침(iMAPMeteringClass), 주민투표(iMAPVotingClass) 등
*
* 만약 간단하게 메시지 송 수신 하려면 본 클래스에서 GetXML() 수정하고
* 상위UI에서 SetItem, GetAttributeVal, GetItemVal을 사용하여 XML데이터를
* 뽑아 사용하면 됨.
*
* @example
* Log.d("TAG", "Button Touched");
final iMAPBasicClass imapBasicClass = new iMAPBasicClass(getApplicationContext()); //객체 생성
imapBasicClass.SetServerIPcfg("10.2.12.3", 10000); //보낼 IP 및 포트 설정
imapBasicClass.DoWork(0);
imapBasicClass.SetRetCtr(new RetProci(){
@Override
public void ProcSuccess() {
// TODO Auto-generated method stub
//* 정상적인 메시지가 수신되면 이곳이 실행됨
//* 중간에 짤리거나 비정상적인 메시지는 이곳을 실행하지 않고 'ProcError'가 실행됨
*
* *** Test XML Message ***
<?xml version="1.0" encoding="utf-8"?>
<imap ver = "1.0" address = "10.2.12.3" sender = "단지서버">
<service type = "reply" name= "event_notify" result = "ok">
<event> "elevator_up" </event>
<event> "elevator_down" </event>
<event> "elevator_stop" </event>
<valuelist year = "2007" month = "1" day = "0"elect_read ="40" elect_used ="38" gas_read ="5" gas_used ="4"/>
</service>
</imap>
// 위 같은 메시지가 수신되었을때 아래 로그 출력 되는 값 참고하여 개발 요망!
Log.d(TAG, "ProcSuccess");
int ElementCount = imapBasicClass.SetItem("service");
Log.d(TAG, "ElementCount :"+ElementCount); //1
String AttributeType = imapBasicClass.GetAttributeVal(0, "type");
Log.d(TAG, "AttributeType :"+AttributeType); //"reply"
String AttributeName = imapBasicClass.GetAttributeVal(0, "name");
Log.d(TAG, "AttributeName :"+AttributeName); //"event_notify"
String AttributeResult = imapBasicClass.GetAttributeVal(0, "result");
Log.d(TAG, "AttributeResult :"+AttributeResult); //"ok"
int ElementCountEvent = imapBasicClass.SetItem("event");
Log.d(TAG, "ElementCount_Next :"+ElementCountEvent); //3
String AttributeEvent1 = imapBasicClass.GetItemVal(0);
Log.d(TAG, "AttributeEvent1 :"+AttributeEvent1); //"elevator_up"
String AttributeEvent2 = imapBasicClass.GetItemVal(1);
Log.d(TAG, "AttributeEvent2 :"+AttributeEvent2); //"elevator_down"
String AttributeEvent3 = imapBasicClass.GetItemVal(2);
Log.d(TAG, "AttributeEvent3 :"+AttributeEvent3); //"elevator_stop"
int ElementCount_valuelist = imapBasicClass.SetItem("valuelist");
Log.d(TAG, "ElementCount_valuelist :"+ElementCount_valuelist); //1
String AttributeYear = imapBasicClass.GetAttributeVal(0, "year");
Log.d(TAG, "AttributeYear :"+AttributeYear); //"2007"
}
@Override
public void ProcError() {
// TODO Auto-generated method stub
Log.d("Hyunsu", "ProcError");
}
@Override
public void ProcDoing() {
// TODO Auto-generated method stub
}});
*/
public class iMAPSetAccessKey extends WallPadiMAPClient{
String myIP="NONE";
String myDong="NONE";
String myHo="NONE";
Context ctx;
RetProci Proc;
String XMLHeader;
WallpadStatusData DBMGR;
String ServerIP = "10.0.1.10";
int SvrPort = 10000;
//final WallpadStatusData wallpadStatusData = new WallpadStatusData(ctx); //객체 생성
//카드정보
int mKey_Type;
ID id = new ID();
public static final class ID
{
//키 정보 설정 옵션
public final static int SAVE = 1;
public final static int DELETE = 2;
public final static int RENEW = 3;
//키 종류
public final static int HKEY = 1;
public final static int DKEY = 2;
public final static int MKEY = 3;
}
public iMAPSetAccessKey(Context ctx)
{
DBMGR = new WallpadStatusData(ctx);
AddressSet addc = DBMGR.getAddressMGR(); //DB에서 동호 정보 받아옴
DBMGR.closeDB();
//동, 호, ip 정보 저장
myIP = addc.IPAddress;
myDong = addc.Dong;
myHo = addc.Ho;
//XML Header 생성
XMLHeader = "\r\n"+
"\r\n" ;
setContext(ctx);
this.ctx = ctx;
}
private String SetXMLInfo(int actionType, int keyType)
{
String retXML = null;
String sActionType = "renew";
String sKeyType = "hkey";
if(actionType == ID.SAVE)
{
sActionType = "save";
}
else if (actionType == ID.DELETE)
{
sActionType = "delete";
}
else if (actionType == ID.RENEW)
{
sActionType = "renew";
}
if(keyType == ID.HKEY)
{
sKeyType = "hkey";
}
else if (keyType == ID.DKEY)
{
sKeyType = "dkey";
}
else if (keyType == ID.MKEY)
{
sKeyType = "mkey";
}
mKey_Type = keyType;
retXML = XMLHeader +
" \r\n"+
" \""+sActionType+"\"\r\n" +
" \""+sKeyType+"\"\r\n";
return retXML;
}
@SuppressWarnings("unused")
private String SetXML_KeyInfo(List getList)
{
String retXML = null;
SimpleDateFormat CurYearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat CurMonthFormat = new SimpleDateFormat("MM");
SimpleDateFormat CurDayFormat = new SimpleDateFormat("dd");
SimpleDateFormat CurHourFormat = new SimpleDateFormat("HH");
SimpleDateFormat CurMinuteFormat = new SimpleDateFormat("mm");
int ListSize = getList.size();
if(mKey_Type == ID.HKEY)
{
retXML = " ";
}
else if (mKey_Type == ID.DKEY)
{
retXML = " ";
}
else if (mKey_Type == ID.MKEY)
{
retXML = " ";
}
retXML += "\r\n";
if(ListSize > 0)
{
for(int i = 0; i\r\n";
}
}
retXML += " \r\n";
return retXML;
}
/**
* 관리PC IP 및 포트 설정
* 최초 생성 시 한번은 설정 해야함
* **/
public void SetAdminPC_IPcfg(String IPa, int port)
{
ServerIP = IPa;
SvrPort = port;
}
public void SetRetCtr(RetProci retProci)
{
Proc = retProci;
}
protected void ProcComplite()
{
if(Proc != null)
{
SetItem("service");
String result = GetAttributeVal(0, "result");
if (result!=null)
{
if (result.equalsIgnoreCase("ok"))
{
String name = GetAttributeVal(0, "name");
String type = GetAttributeVal(0, "type");
if((name!=null && name.equalsIgnoreCase("set_accesskey_info")) &&
(type!=null && type.equalsIgnoreCase("reply")))
{
Proc.ProcSuccess();
}
else
{
Proc.ProcError();
}
}
else if (result.equalsIgnoreCase("fail"))
{
Proc.ProcError();
}
else
{
Proc.ProcError();
}
}
else
{
Proc.ProcError();
}
}
}
protected void ProcError()
{
Proc.ProcError();
}
/**
* set_accesskey_info 메시지를 관리PC로 보낼때 사용하는 함수임
*
* @param actionType ID.SAVE / ID.DELETE / ID.RENEW or 1 / 2 / 3
* @param keyType ID.HKEY / ID.DKEY / ID.MKEY or 1 / 2 / 3
* @param getList ArrayList형 어레이 리스트 생성 후 삽입
* 예) ArrayList arrayList = new ArrayList();
* arrayList.add(new KEY_INFO(1, "null", "null", 1, 1, "null", "null"));
*
* @return boolean 형 타입 - true면 메시지 송신 성공. false면 메시지 송신 실패
* **/
public boolean DoWork(int actionType, int keyType, List getList) //1이면 List, 2이면 Contents
{
SetServerIPConfig(ServerIP, SvrPort);
String SendMessage = null;
if(actionType <= 0)
{
return false;
}
if(keyType <= 0)
{
return false;
}
if(getList == null)
{
return false;
}
SendMessage = SetXMLInfo(actionType, keyType);
SendMessage += SetXML_KeyInfo(getList);
SendXMLDoc(SendMessage, true);
return true;
}
}