package com.baosight.hdsdk.example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.tuple.ImmutablePair;

import com.baosight.hdjni.HDErrcode;
import com.baosight.hdsdk.HDConnectionFactory;
import com.baosight.hdsdk.HDDataProvider;
import com.baosight.hdsdk.HDDataWriter;
import com.baosight.hdsdk.HDTagManager;
import com.baosight.hdsdk.HDServerFactory;
import com.baosight.hdsdk.common.HDBasicTagPropMask;
import com.baosight.hdsdk.common.HDTagClass;
import com.baosight.hdsdk.common.HDTagDataType;
import com.baosight.hdsdk.domain.data.HDBasicTag;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.domain.data.HDDataServer;
import com.baosight.hdsdk.domain.data.HDRecord;
import com.baosight.hdsdk.exception.HDSdkException;

public class testHDB {

	public static void main(String[] args) {
		//set server ip port
		String serverIP = "127.0.0.1";
		HDDataServer server = HDServerFactory.getHDDataServer(serverIP,5673,"",5673);
		HDDataConnection hdconn = null;
		try {
			hdconn = HDConnectionFactory.getHDDataConnection(server);
			hdconn.loginToServer("admin", "admin");
		} catch (HDSdkException e) {
			e.printStackTrace();
		}
		
		HDDataWriter dw = new HDDataWriter(hdconn);
		HDDataProvider dataProvider = new HDDataProvider(hdconn); 
		
		int tagID = 0;		
		HDTagManager tm;
		HDBasicTag tag = null;
		tm = hdconn.getTagManager();
		
		//add tag
		tag = new HDBasicTag();	
		String tagName = "test_int32_1";
		tag.setName(tagName);
		tag.setTagDataType(HDTagDataType.INT32);
		tag.setTagClass(HDTagClass.BASIC);
		tag.setDesc("int32");		
		long mask = HDBasicTagPropMask.TAG_PROP_TAG_NAME |
					HDBasicTagPropMask.TAG_PROP_TAG_TYPE |
					HDBasicTagPropMask.TAG_PROP_TAG_CLASS |
					HDBasicTagPropMask.TAG_PROP_DESCRIPTOR;
		try {				
			tm.addBasicTag(tag, mask,0);						
		} catch(HDSdkException e) {
			e.printStackTrace();
		}
		
		try{
			tag = tm.getBasicTagByName(tagName);
		}catch(HDSdkException e){
			e.printStackTrace();
		}
		tagID = tag.getId();
		System.out.println("tagid:");
		System.out.println(tagID);
		//wait for datanode sync tags..
		try { 
			Thread.sleep(30000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		
		//save record
		HDRecord record = new HDRecord();
		Date now = new Date();		
		record.setSecond(now.getTime() / 1000);
		record.setQuality(192);
		record.setValueStr("3");				
		try{
			dw.saveSnapshot(tagID, record);
		}catch(HDSdkException e){
			e.printStackTrace();
		}
		 
		List<Integer> tagIDs = new ArrayList<Integer>();
		tagIDs.add(tagID);
		List<ImmutablePair<HDRecord, Integer>> recordList = null;
		HDErrcode errall = new HDErrcode();
		try{
			recordList = dataProvider.querySnapshotsByIDs(tagIDs, errall);
			record = recordList.get(0).left;
			System.out.println("value:");
			System.out.println(record.getValueStr());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		//disconnect server
		try {
			hdconn.dispose();
		} catch (HDSdkException e) {
			e.printStackTrace();
		}		
	}
}
