using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using com.baosight.hdsdk;
using com.baosight.hdsdk.domain.data;
using com.baosight.hdsdk.core;
using com.baosight.hdsdk.common;
using com.baosight.hdsdk.exception;
using com.baosight.hdsdk.adapter;

namespace csharpApp
{
    class Program
    {
        public static void sample1()
        {
            //string IP = "127.0.0.1";
            //int port = 5678;
            HDConnectionManager connmgr = new HDConnectionManager();
            HDConnection conn = new HDConnection("168.2.9.96", 5673, "", 5673, 10);           

            //连接服务器
            try
            {
                connmgr.ConnectToServer(conn);
            }
            catch (HDSdkException ex)
            {
                System.Console.WriteLine(ex.ToString());

                return;
            }

            //登录服务器
            String username = "admin";
            String password = "admin";
            try
            {
                connmgr.LoginToServer(username, password);

            }
            catch (HDSdkException ex)
            {
                System.Console.WriteLine(ex.ToString());

                return;
            }
       
            //添加Tag点int32
            String tagName = "test_tagname_int32";            
            HDDataManager mgr = new HDDataManager();
            HDTagManager tagManager = mgr.getTagManager();
            HDTag tag = new HDTag();
            tag.setName(tagName);
            tag.setTagDataType(HDTagDataType.INT32); //int32类型
            tag.setTagClass(HDTagClass.BASIC);  //普通点，区别于计算点、统计点、报警点
            tag.setDesc("tag desc, int32");

            long lComm = HDBasicTagPropMask.TAG_COMM_PROP_TAG_NAME |
                        HDBasicTagPropMask.TAG_COMM_PROP_TAG_TYPE |
                        HDBasicTagPropMask.TAG_COMM_PROP_TAG_CLASS |
                        HDBasicTagPropMask.TAG_COMM_PROP_DESCRIPTOR;
            long lExt = 0; //扩展属性掩码
            HDMask mask = new HDMask(lComm, lExt);

            try
            {
                tagManager.addBasicTag(tag, mask);
            }
            catch (HDSdkException ex)
            {
                System.Console.WriteLine(ex.ToString());
            }

            //添加Tag点float32
            tagName = "test_tagname_float32";
            tag.setName(tagName);
            tag.setTagDataType(HDTagDataType.FLOAT32); //float32类型
            tag.setTagClass(HDTagClass.BASIC);  //普通点，区别于计算点、统计点、报警点
            tag.setDesc("tag desc, float32");
            try
            {
                tagManager.addBasicTag(tag, mask);
            }
            catch (HDSdkException ex)
            {
                System.Console.WriteLine(ex.ToString());
            }

            //添加Tag点string
            tagName = "test_tagname_string";
            tag.setName(tagName);
            tag.setTagDataType(HDTagDataType.STRING); //string类型
            tag.setTagClass(HDTagClass.BASIC);  //普通点，区别于计算点、统计点、报警点
            tag.setDesc("tag desc, string");
            try
            {
                tagManager.addBasicTag(tag, mask);
            }
            catch (HDSdkException ex)
            {
                System.Console.WriteLine(ex.ToString());
            }

            //添加Tag点blob
            tagName = "test_tagname_blob";
            tag.setName(tagName);
            tag.setTagDataType(HDTagDataType.BLOB); //blob类型
            tag.setTagClass(HDTagClass.BASIC);  //普通点，区别于计算点、统计点、报警点
            tag.setDesc("tag desc, blob");
            try
            {
                tagManager.addBasicTag(tag, mask);
            }
            catch (HDSdkException ex)
            {
                System.Console.WriteLine(ex.ToString());
            }

            //查询Tag点          
            HDTag basicTag;
            uint[] tagID = new uint[4];
            tagID[0] = 0;
            basicTag = null;
            basicTag = tagManager.getBasicTagByName("test_tagname_int32");
            tagID[0] = basicTag.getId();

            basicTag = tagManager.getBasicTagByName("test_tagname_float32");
            tagID[1] = basicTag.getId();
            
            basicTag = tagManager.getBasicTagByName("test_tagname_string");
            tagID[2] = basicTag.getId();

            basicTag = tagManager.getBasicTagByName("test_tagname_blob");
            tagID[3] = basicTag.getId();
           

            //修改Tag点int32
            basicTag = tagManager.getBasicTagById(tagID[0]);
            basicTag.setDesc("modify desc");
            HDMask mask4modify = new HDMask(HDBasicTagPropMask.TAG_COMM_PROP_DESCRIPTOR, 0);
            tagManager.modifyBasicTag(basicTag, mask4modify);

            System.Console.WriteLine("Tag [" + basicTag.getName() + "] created. Prepare to saveSnapshot..");
            Thread.Sleep(30000);
            //给Tag点存数据
            HDDataWriter dataWriter = new HDDataWriter(mgr);
            HDDataRecord[] record = new HDDataRecord[4];
            
            //DateTime dataTime = new DateTime(2013, 6, 18, 15, 0, 1);
            Int64 dataTimeSec = 1371545749;
            UInt16 dataQuality_GOOD = 192;

            //tag记录int32
            record[0] = new HDDataRecord();
            record[0].setSecond(dataTimeSec);
            record[0].setQuality(dataQuality_GOOD);
            record[0].setValueStr("12345");

            //tag记录float32
            record[1] = new HDDataRecord();
            record[1].setSecond(dataTimeSec);
            record[1].setQuality(dataQuality_GOOD);
            record[1].setValueStr("123.45");

            //tag记录string
            record[2] = new HDDataRecord();
            record[2].setSecond(dataTimeSec);
            record[2].setQuality(dataQuality_GOOD);
            record[2].setValueStr("string");

            //tag记录blob
            record[3] = new HDDataRecord();
            record[3].setSecond(dataTimeSec);
            record[3].setQuality(dataQuality_GOOD);
            record[3].setValueStr("blob");

            //连续单点快照
            for (int j = 0; j < 2; j++)
            {
                for (int i = 0; i < 4; i++)
                {
                    //单点快照Int32,float32,string,blob
                    try
                    {
                        dataWriter.saveSnapshot(tagID[i], record[i]);
                        System.Console.WriteLine("TagID:" + tagID[i] + "save successfully!");
                    }
                    catch (HDSdkException ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                    }

                    //时间戳+1
                    dataTimeSec += 1;
                    record[i].setSecond(dataTimeSec);
                }
            }

            //多点快照接口(测试仅为特例的TagID数组中只含1个ID元素)
            uint[] tagIDArray = new uint[4];
            HDDataRecord[] snapRecords = new HDDataRecord[4];

            for (int i = 0; i < 4;i++ )
            {
                tagIDArray[i] = tagID[i];
                snapRecords[i] = record[i];
            }
            int[] err = new int[4];
            for (int j = 0; j < 2; j++)
            {
                try
                {
                    dataWriter.saveTagsSnapshots(tagIDArray, snapRecords, ref err[0]);
                    System.Console.WriteLine("TagArray save successfully!");
                }
                catch (HDSdkException ex)
                {
                    System.Console.WriteLine(ex.ToString());
                }
                //时间戳+1
                dataTimeSec += 1;
                for (int i = 0; i < 4; i++)
                {
                    record[i].setSecond(dataTimeSec);
                }             
            }
           
            //查询单点快照数据
            HDDataProvider dataProvider = new HDDataProvider(mgr);
            HDDataRecord recordQuery = null;
            //basicTag = tagManager.getBasicTagByName(tagname);
            //tagID = basicTag.getId();
            for (int i = 0; i < 4;i++ )
            {
                recordQuery = dataProvider.QuerySnapshotByTagID(tagID[i]);
                System.Console.WriteLine("Snapshot time: " + recordQuery.getSecond() + "  value: " + recordQuery.getValueStr());
            }
            
          
            //多点查询数字类型快照数据
            int nErrorcode= 0;
            uint[] DataTagID ={tagID[0],tagID[1]};
            

            List<KeyValuePair<HDDataRecord, int>> SnapDataRecords = null;
            SnapDataRecords =  dataProvider.QuerySnapshotsByTagID(DataTagID,  ref nErrorcode);
            for (int i = 0; i < 2;i++ )
            {
                System.Console.WriteLine("TagID:"+DataTagID[i]+"  SnapDataRecords show time: " + SnapDataRecords[i].Key.getSecond() + "  value: " + SnapDataRecords[i].Key.getValueStr());
            }

            //多点查询字符类型快照数据
            int CharErrorcode = 0;
            uint[] CharTagID = { tagID[2], tagID[3] };
            List<KeyValuePair<HDDataRecord, int>> SnapCharRecords = null; 
            SnapCharRecords = dataProvider.QuerySnapshotsByTagID(CharTagID, ref CharErrorcode);
            for (int i = 0; i < 2; i++)
            {
                System.Console.WriteLine("TagID:" + CharTagID[i] + "  SnapCharRecords show time: " + SnapCharRecords[i].Key.getSecond() + "  value: " + SnapCharRecords[i].Key.getValueStr());
            }
         
         
            //查询历史数据
            DateTime startTime = new DateTime(2013, 6, 18, 14, 50, 0);  
            DateTime endTime = new DateTime(2013, 6, 18, 17, 02, 33);
            int actualCount = 0;
            HDTimeRegion tr = new HDTimeRegion(startTime, endTime, true, true);
            List<HDDataRecord> recordList = null;
            for (int j = 0; j < 4; j++)
            {
                actualCount = dataProvider.GetRawRecordsCount(tagID[j], tr);
                recordList = dataProvider.QueryRawRecords(tagID[j], tr);
                for (int i = 0; i < actualCount; i++)
                {
                    System.Console.WriteLine("RawRecords: time: " + recordList[i].getSecond() + "  value: " + recordList[i].getValueStr());
                }
            }
            
            //删除Tag点
            for (int i = 0; i < 4;i++ )
            {
                tagManager.deleteTagById(tagID[i]);
                System.Console.WriteLine( i+ " TagID:" + tagID[i] + "  delete successfully!");             
            }
            basicTag = null;
            //异常操作test, 此函数外部必须捕获处理!!
            basicTag = tagManager.getBasicTagById(tagID[0]);  
        }

        public static void sample2()
        {
            HDConnectionManager connmgr = new HDConnectionManager();
            HDConnection conn = new HDConnection("168.2.9.96", 5673, "", 5673, 10);
            connmgr.ConnectToServer(conn);
            connmgr.LoginToServer("admin", "admin");

            //获取各个模块的操作工具类对象,整理参数调用接口实现功能
            HDDataManager mgr = new HDDataManager();
            HDTagManager tagManager = mgr.getTagManager();
            HDTag tag = new HDTag();
            uint tagID = 11;
            tag = tagManager.getBasicTagById(tagID);
            System.Console.WriteLine(tag.ToString());
            HDDataProvider dataProvider = mgr.getDataProvider();
            HDDataRecord recordQuery = null;
            recordQuery = dataProvider.QuerySnapshotByTagID(tagID);
            System.Console.WriteLine(recordQuery.getTimeStampStr() + " time, value:" + recordQuery.getValueStr());

            connmgr.DisconnectFromServer();
        }

        static void Main(string[] args)
        {
            try
            {
                sample1();
            }            
            catch (HDSdkException ex)
            {
                System.Console.WriteLine(ex.ToString());
            }

            sample2();
        }
    }
}
