# -*- coding: UTF-8 -*-
import io
import sys
import time
from ssat_sdk.device_manage import ScbcCopyKey
from ssat_sdk import sat_environment

'''
接口说明：
Open:打开串口
    函数声明：
        int Open(串口号,波特率,数据位,奇偶校验,停止位)
        返回1表示打开成功，返回0表示打开失败
    示例：
    result = ScbcCopyKey.Open(3, 115200, 8, 0, 1)
    if result == 1:
        print('打开成功')
    else:
        print('打开失败')
    # 关闭串口
    ScbcCopyKey.Close()
    

Close：关闭串口
    函数声明：
        void Close()
        无返回值
    示例：
        ScbcCopyKey.Close()
    
IsOpen：串口是否打开
    函数声明：
        int IsOpen()
        返回1表示已打开串口，返回0表示打开失败
    示例：
        result = ScbcCopyKey.IsOpen() 
        if result == 1:
            print('打开成功')
        else:
            print('打开失败')

EnterFactory：进入工厂模式
    函数声明：
        int EnterFactory()
        返回1表示成功进入，返回0表示进入失败
    示例：
    result = ScbcCopyKey.Open(3, 115200, 8, 0, 1)
    if result == 1:
        if ScbcCopyKey.EnterFactory():
            print "进入工厂模式成功"
            # do something
            # ............
            # 离开工厂模式
            ScbcCopyKey.LeaveFactory()
        else:
            print "进入工厂模式失败"
    
    ScbcCopyKey.Close()
    
其他接口略：
    GetXXXX：获取指定类型值
    SetXXXX(buffer, len)：设置指定类型值，buffer为字节数组, len为buffer长度;
    SetXXXXBF(file)：设置指定类型值，file为数据的文件路径;
'''


'''
    用例为完整的调用流程：Open->EnterFactory->所有协议操作->LeaveFactory->Close;
'''

# 使用测试地址,测试数据进行测试;
def testGetUserId():
    userName = "auto"
    password = "123456"
    token = ScbcCopyKey.HTTPLogin("http://test.dsp.server.qhmoka.com/login", userName, password, 1)
    if token.__len__():
        print u"login token = ", token      
        userId = ScbcCopyKey.HTTPGetUserId("http://test.dsp.server.qhmoka.com/api/automation/getUserId", token, "MOKA-AF-MS6586-0000L", "fe6015c2966118d45e745d3bdeabc052f87e7847", "5c:ad:76:e0:55:79")
        if userId == -1:
            print u"获取userId失败"
        else:
            print u"userId=",userId

if __name__ == "__main__":        
    # 打开串口;
    result = ScbcCopyKey.Open(int(sat_environment.getSerialCOM()[3:]), 115200, 8, 0, 1)
    if result:
        # 进入工厂模式;
        if ScbcCopyKey.EnterFactory():
            print u"获取ProjectId",ScbcCopyKey.GetProjectId()           
            print u"获取软件版本",ScbcCopyKey.GetSoftVersion()
            
            # HTTPGetUserId所需要参数;
            deviceId = ScbcCopyKey.GetDeviceId()
            print u"获取DeviceID",deviceId
            # HTTPGetUserId所需要参数;
            clientType = ScbcCopyKey.GetClientType()
            print u"获取ClientType",clientType
            # HTTPGetUserId所需要参数;
            mac = ScbcCopyKey.GetMAC()
            print u"获取MAC",mac
            
            print u"获取HDCP",ScbcCopyKey.GetHDCPKey()
            print u"获取HDCP22",ScbcCopyKey.GetHDCPKey22()
            print u"GetWidi",ScbcCopyKey.GetWidi()
            print u"GetNetflixESN",ScbcCopyKey.GetNetflixESN()
            print u"GetWidevine",ScbcCopyKey.GetWidevine()
            print u"GetCiKey",ScbcCopyKey.GetCiKey()
            print u"GetOSDLanguage",ScbcCopyKey.GetOSDLanguage()
            print u"GetShopLanguage",ScbcCopyKey.GetShopLanguage()
            print u"GetChannel",ScbcCopyKey.GetChannel()
            
            # 获取UserId;
            if 1:
                # 测试环境： http://test.dsp.server.qhmoka.com
                # 正式环境：https://dsp.server.qhmoka.com
                # 正式使用时，请使用正式环境的url地址替换测试环境url;
                userName = "auto"
                password = "123456"
                token = ScbcCopyKey.HTTPLogin("http://test.dsp.server.qhmoka.com/login", userName, password, 1)
                
                # token长度不为0, 表示成功获取token值;
                if token.__len__():
                    print u"login token = ", token                    
                    userId = ScbcCopyKey.HTTPGetUserId("http://test.dsp.server.qhmoka.com/api/automation/getUserId", token, clientType, deviceId, mac.replace('-', ':'))
                    # userId==-1表示获取失败;
                    if userId == -1:
                        print u"获取userId失败"
                    else:
                        print u"userId=",userId

            if 0:
                file = r"D:\SAT\Allkey\RT2851-1PCS\DID\did1711.bin"
                print u"SetDeviceId", ScbcCopyKey.SetDeviceIdBF(file)

                file = r"D:\SAT\Allkey\RT2851-1PCS\MAC\TCL_MAC_CC-A1-2B-09-01-C3.bin"
                print u"SetMAC", ScbcCopyKey.SetMACBF(file)

                file = r"D:\SAT\Allkey\RT2851-1PCS\RealTek1.4\Encoder3_00000004.bin"
                print u"SetHDCPBF", ScbcCopyKey.SetHDCPKeyBF(file)

                file = r"D:\SAT\Allkey\RT2851-1PCS\RealTek2.2\PUBHDCP22_0000_20190710_0053303.bin"
                print u"SetHDCP22BF", ScbcCopyKey.SetHDCPKey22BF(file)

                file = r"D:\SAT\Allkey\RT2851-1PCS\RT2851 CI KEY\00000478-0735185308117697.bin"
                print u"SetCiKeyBF", ScbcCopyKey.SetCiKeyBF(file)

                file = r"D:\SAT\Allkey\RT2851-1PCS\RT2851 widevine\RT2851-Widevine_111"
                print u"SetWidevineBF", ScbcCopyKey.SetWidevineBF(file)

            # 预置频道;
            '''
            以下数值均为16进制
            "HZ":	        "01"
            "Poland":	    "02"
            "TTET":	        "03"           
            "WX":	        "04"
            "NM":	        "05"
            "ODM":	        "06"
            "Vietnam":	    "07"  
            "Thailand":	    "08"
            "Philippines":	"09"
            "Indonesia":	"0A"
            "Russia":	    "0B"
            "Egypt":	    "0C"
            "JAC":	        "0D"
            "Pakistan":	    "0E" 
			"Benne":	    "0F"
            "Araby":	    "10"
            "Eltholathia":	"11"  
            "Tunisia":	    "12"
            "UPAC":     	"14"
            "Condor":	    "13"            
            "PKV":	        "15"     
            "LIXIANG":	    "16"
            "LEXUAN":   	"17"  
            "TOSHIBA":	    "18"
            "Algeria":	    "19"            
            "CVT":	        "1A"
            "SouthAfrica":	"1B"
            "ORION":	    "1C"
            "Mexico":	    "1D"                                       
            "RV":	        "1E"            
            "SEMP":	        "1F"                      
            '''
            # HZ
            print "SetChannel", ScbcCopyKey.SetChannel(1)
            # Mexico = 0x1D
            print "SetChannel", ScbcCopyKey.SetChannel(29)
            
            # 显示工厂菜单;
            print "ShowFactoryMenu", ScbcCopyKey.ShowFactoryMenu()
            time.sleep(2)
            print "HideFactoryMenu", ScbcCopyKey.HideFactoryMenu()

            # 显示工厂内容;
            print "ShowFactoryInformation", ScbcCopyKey.ShowFactoryInformation()
            time.sleep(2)
            print "HideFactoryInformation", ScbcCopyKey.HideFactoryInformation()

            # 用例中不开启老化;
            if 0:
                # 进入老化模式;
                print "EnterAgingModel", ScbcCopyKey.EnterAgingModel()
                time.sleep(10)
                print "LeaveAgingModel", ScbcCopyKey.LeaveAgingModel()
                # 读取老化时间;
                print "ReadAgingTime", ScbcCopyKey.ReadAgingTime()

            if 0:
                # 红绿蓝增益;
                print "SetRedGainRegister", ScbcCopyKey.SetRedGainRegister(100)
                print "SetGreenGainRegister", ScbcCopyKey.SetGreenGainRegister(200)
                print "SetBlueGainRegister", ScbcCopyKey.SetBlueGainRegister(300)

                # 红绿蓝偏移;
                print "SetRedOffsetRegister", ScbcCopyKey.SetRedOffsetRegister(100)
                print "SetGreenOffsetRegister", ScbcCopyKey.SetGreenOffsetRegister(200)
                print "SetBlueOffsetRegister", ScbcCopyKey.SetBlueOffsetRegister(300)

            # 离开工厂模式;
            ScbcCopyKey.LeaveFactory()
        #endif

        # 关闭串口
        ScbcCopyKey.Close()
    #endif