# -*- coding:utf-8 -*-
import os
import sys
import time
import json
from baseClient import BaseClient
import numpy as np

class C22293Manager(BaseClient):
    def __init__(self):
        BaseClient.__init__(self)  # python 继承方式  
        self.device_name = "Chroma22293" 

    def __parse__(self, result, params = 1):
        if result is None:
            return False
        
        print result
        if params == 1:
            if result['device_cmd_result'] == u'ok;\r\n':
                return True
        
        if params == 2:
            if result['device_cmd_result'] == u'ok;\r\nok;\r\n':
                return True
        
        return False

    def setPattern(self, pattern, device=''):
        self.device_id = 1
        self.device_cmd = 'run ptn %s;\r' % pattern
        return self.__parse__(self.sendmsg())

    def setTiming(self, timing, device=''):
        self.device_id = 1
        self.device_cmd = 'run tim %s;\r' % timing
        return self.__parse__(self.sendmsg())

    def setTimingPattern(self, timing, pattern, device=''):
        self.device_id = 1
        self.device_cmd = 'run tim %s;run ptn %s;\r' % (timing, pattern)
        return self.__parse__(self.sendmsg(), 2)

    def getDeviceName(self, device=''):
        self.device_id = 1
        self.device_cmd = 'report model;\r'
        result = self.sendmsg()
        if result is None:
            return None
        return result['device_cmd_result']

    def getDeviceSoft(self, device=''):
        self.device_id = 1
        self.device_cmd = 'report ver;\r'
        result = self.sendmsg()
        if result is None:
            return None
        return result['device_cmd_result']

    def getStatus(self, device=''):
        self.device_id = 1
        self.device_cmd = 'dummy;\r'
        return self.__parse__(self.sendmsg())

    def setBlueOFF(self, device=''):
        self.device_id = 1
        self.device_cmd = 'b off;\r'
        return self.__parse__(self.sendmsg())

    def setBlueON(self, device=''):
        self.device_id = 1
        self.device_cmd = 'b on;\r'
        return self.__parse__(self.sendmsg())

    def setGreenOFF(self, device=''):
        self.device_id = 1
        self.device_cmd = 'g off;\r'
        return self.__parse__(self.sendmsg())

    def setGreenON(self, device=''):
        self.device_id = 1
        self.device_cmd = 'g on;\r'
        return self.__parse__(self.sendmsg())

    def setRedOFF(self, device=''):
        self.device_id = 1
        self.device_cmd = 'r off;\r'
        return self.__parse__(self.sendmsg())

    def setRedON(self, device=''):
        self.device_id = 1
        self.device_cmd = 'r on;\r'
        return self.__parse__(self.sendmsg())

    def setKeyBoardLock(self, device=''):
        self.device_id = 1
        self.device_cmd = 'kb lock on;\r'
        return self.__parse__(self.sendmsg())

    def setKeyBoardUnLock(self, device=''):
        self.device_id = 1
        self.device_cmd = 'kb lock off;\r'
        return self.__parse__(self.sendmsg())

if __name__ == "__main__":
    while(True):
        print u'测试开始\n'
        c2 = C22293Manager()
        print c2.getDeviceName()
        print c2.getDeviceSoft()
        print c2.getStatus()

        print c2.setPattern(11)
        print c2.setTiming(11)
        print c2.setTimingPattern(11,11)

        print c2.setBlueOFF()
        print c2.setBlueON()
        
        print c2.setGreenOFF()
        print c2.setGreenON()

        print c2.setRedOFF()
        print c2.setRedON()

        print c2.setKeyBoardLock()
        print c2.setKeyBoardUnLock()

        time.sleep(2)
        print u'测试结束\n'

