#!/usr/bin/env python
# -*- coding: gb2312 -*-

import sys
import time
from datetime import date

sys.path.append(".")
import server ,security, record

class TestSDK:
    def ConnectServer(self):
        self.myServer = server.Server('127.0.0.1', 5673, '', 5673)
        self.myServer.connect()   
        self.myServer.login('admin', 'admin')
        self.myTagMgr = self.myServer.tag_mgr
        
    def AddTag(self):
        attr = {'tagname' : 'testtag', 'tagtype' : 'int8'}        
        try:
            self.myTagMgr.add_tag(**attr)
        except:
            self.myTag = self.myTagMgr.get_tag('testtag')
            print self.myTag.tagid
        
    def AddRecord(self):
        self.myTag = self.myTagMgr.get_tag('testtag')        
        hdrecord = record.Record(int(time.strftime('%m%d%H%M%S', time.localtime())), 0)
        hdrecord.tagtype = self.myTag.tagtype
        hdrecord.quality = 192        
        hdrecord.value = 123
        self.myTag.save_snapshot(hdrecord)        
        
    def QuerySnapShot(self):
        hdrecord = self.myTag.get_snapshot()
        print hdrecord.value
        print hdrecord.sec

    def Disconnet(self):        
        self.myTagMgr.delete_tag('testtag')
        self.myServer.disconnect()
        
if __name__ == '__main__':
    testA = TestSDK()    
    print "PythonSDK_V3 sample.."
    
    testA.ConnectServer()
    testA.AddTag()
    time.sleep(30)
    testA.AddRecord()
    testA.QuerySnapShot()
    testA.Disconnet()
    