def UpdateLogic():

    ServerState = serv.server_state()
    ServerState.UpdateState()

    global PrevServerState

    if ServerState.IsDiffState( PrevServerState ) == False:       

        if ServerState.CpuTerm != PrevServerState.CpuTerm:
            if ServerState.CpuTerm == False:
                sc.SetLedColor( sc.LED_UP, sc.color( 222, 222, 0 ) )
                Log( "WARNING cpu term"	)
            else:
                sc.SetLedColor( sc.LED_UP, DefaultColor )
                Log( "OK cpu term"	)
        
        if ServerState.Raid != PrevServerState.Raid:
            if ServerState.Raid == False:
                sc.SetLedColor( sc.LED_ALL, sc.color( 222, 0, 0 ) )
                Log( "WARNING Raid"	)
            else:
                sc.SetLedColor( sc.LED_ALL, DefaultColor )
                Log( "OK Raid"	)
        
        if ServerState.Memory != PrevServerState.Memory:
            if ServerState.Memory == False:
                sc.SetLedColor( sc.LED_DOWN, sc.color( 0, 222, 222 ) )
                Log( "WARNING Memory"	)
            else:
                sc.SetLedColor( sc.LED_DOWN, DefaultColor )
                Log( "OK Memory"	)
        
        if ServerState.HddRaid != PrevServerState.HddRaid:
            if ServerState.HddRaid == False:
                sc.SetLedColor( sc.LED_RIGHT, sc.color( 0, 0, 222 ) )
                Log( "WARNING HddRaid"	)
            else:
                sc.SetLedColor( sc.LED_RIGHT, DefaultColor )
                Log( "OK HddRaid"	)

        if ServerState.HddTorrent != PrevServerState.HddTorrent:
            if ServerState.HddTorrent == False:
                sc.SetLedColor( sc.LED_LEFT, sc.color( 0, 0, 222 ) )
                Log( "WARNING HddTorrent"	)
            else:
                sc.SetLedColor( sc.LED_LEFT, DefaultColor )
                Log( "OK HddTorrent"	)

        if ServerState.HddSystem != PrevServerState.HddSystem:
            if ServerState.HddSystem == False:
                sc.SetLedColor( sc.LED_LEFT, sc.color( 0, 0, 222 ) )
                sc.SetLedColor( sc.LED_RIGHT, sc.color( 0, 0, 222 ) )
                Log( "WARNING HddSystem"	)
            else:
                sc.SetLedColor( sc.LED_LEFT, DefaultColor )
                sc.SetLedColor( sc.LED_RIGHT, DefaultColor )
                Log( "OK HddSystem"	)

    PrevServerState = copy.deepcopy( ServerState )
def main():
	Log( "start----------------------------------------" )

	LogCounter = 0
	sc.HidInit()

	sc.SetCommand( sc.TC_IS_RUN )
	#sc.SetCommand( sc.TC_DEBUG_PAUSE )

	sc.SetLedColor( sc.LED_ALL, sc.color( 0, 30, 0 ) )

	print sc.m_ControllerData
	time.sleep(1)

	g_LastChangeConfig = None 
	UpdateLogicPreckallerCounter = 0

	while True:
		try:
		    if g_LastChangeConfig != os.path.getmtime( CONFIG_PATCH ):
		    	g_LastChangeConfig = ReloadConfig()
		except:
		    Log( "error config" )
		    time.sleep(2)

		PrintStat()
		TermControl()

		time.sleep(1)
		sc.SetCommand( sc.TC_IS_RUN )
		try:		
		    LogStatus()
		except:
		    Log( "error status" )
		    time.sleep(2)

		if LogCounter == 0:
			LogCounter = 60 #log
			LogTerm()

		LogCounter -= 1

		if UpdateLogicPreckallerCounter == 0:
			try:
			    UpdateLogic()
			except:
			    Log( "error logic" )
			    time.sleep(2)

			UpdateLogicPreckallerCounter = 5

		UpdateLogicPreckallerCounter -= 1

		time.sleep(1)
	print "fin"
def ReloadConfig():
    try:         
        g_Config = ConfigParser.RawConfigParser()
        g_Config.read( CONFIG_PATCH )

        global Log_Path
        Log_Path = g_Config.get('main', 'log_path')
        global Status_File
        Status_File = g_Config.get('main', 'status_file')
        
        global DefaultColor
        DefaultColor.R = g_Config.getint('mode_normal_color', 'r')
        DefaultColor.G = g_Config.getint('mode_normal_color', 'g')
        DefaultColor.B = g_Config.getint('mode_normal_color', 'b')
        sc.SetLedColor( sc.LED_ALL, DefaultColor )

        manual_fan_power_hdd[ 0 ] = g_Config.getint('main', 'manual_fan_power_pwr')
        manual_fan_power_hdd[ 1 ] = g_Config.getint('main', 'manual_fan_power_cpu')
        manual_fan_power_hdd[ 2 ] = g_Config.getint('main', 'manual_fan_power_out')
        manual_fan_power_hdd[ 3 ] = g_Config.getint('main', 'manual_fan_power_hdd')

        for I in range( 0, 4 ):
            if manual_fan_power_hdd[ I ] != -1 :
                sc.SetFanPower( sc.FAN[ I ], manual_fan_power_hdd[ I ] )

        temp_add[ 0 ] = g_Config.getint('main', 'temp_add_pwr')
        temp_add[ 1 ] = g_Config.getint('main', 'temp_add_cpu')
        temp_add[ 2 ] = g_Config.getint('main', 'temp_add_out')
        temp_add[ 3 ] = g_Config.getint('main', 'temp_add_hdd')

        fan_power_add[ 0 ] = g_Config.getint('main', 'fan_power_add_pwr')
        fan_power_add[ 1 ] = g_Config.getint('main', 'fan_power_add_cpu')
        fan_power_add[ 2 ] = g_Config.getint('main', 'fan_power_add_out')
        fan_power_add[ 3 ] = g_Config.getint('main', 'fan_power_add_hdd')

        return os.path.getmtime( CONFIG_PATCH )
    except:
        #todo log error
        sc.SetLedColor( sc.LED_ALL, sc.color( 99, 99, 99 ) )
        return os.path.getmtime( CONFIG_PATCH )
import ConfigParser
import copy
import hid_api as sc
import puppy_serv_api as serv

#from random import randint

CONFIG_PATCH = "/raid/shared/prog/PythonCode/hid_serv/config.cfg"

TermLo =        [  10, 140, 150, 155, 160 ]
TermHi =        [ 140, 150, 155, 160, 255 ]
FanSpeedLo =    [  80, 120, 130, 180, 220 ]
FanSpeedHi =    [ 120, 160, 180, 220, 255 ]
TermSpeedCount = 5

DefaultColor = sc.color( 111, 111, 111 )
temp_add = [0] * 4
fan_power_add = [0] * 4
manual_fan_power_hdd = [0] * 4

   
g_Config = ConfigParser.RawConfigParser 
Log_Path = "/raid/sustem/logs/hid_serv/"
Status_File = "hidserv"

LogicIsChange = 0
PrevServerState = serv.server_state()
#-----------------------------------------------

def ReloadConfig():
    try: