from __future__ import print_function
if 82 - 82: Iii1i
import datetime
import http
import json
import threading
import time
from collections import deque
from datetime import datetime
from http . client import HTTPConnection
from queue import Queue
if 87 - 87: Ii % i1i1i1111I . Oo / OooOoo * I1Ii1I1 - I1I
from . mMonitorInfo import mMonitorInfo
if 81 - 81: i1 + ooOOO / oOo0O00 * i1iiIII111 * IiIIii11Ii
from . wgpacket import wgGlobal
if 84 - 84: ooo000 - Ooo0Ooo + iI1iII1I1I1i . IIiIIiIi11I1
if 98 - 98: I11iiIi11i1I % oOO
if 31 - 31: i1I
if 9 - 9: IiI11Ii111 / oOo0O00 / IiIIii11Ii - I11iiIi11i1I - iI1iII1I1I1i
if 16 - 16: i1i1i1111I / i1iiIII111
if 3 - 3: i1 % i1 % i1i1i1111I . Ii * i1
if 9 - 9: i1iiIII111
if 10 - 10: ooOOO / IIiIIiIi11I1 * oOO / i1I / i1I
if 61 - 61: Ooo0Ooo - I1I
if 13 - 13: Ooo0Ooo
if 46 - 46: iI1iII1I1I1i - Ii * Oo * Ii
if 52 - 52: Oo + I1I / oOO / OooOoo - I1Ii1I1 - ooOOO
if 60 - 60: iI1iII1I1I1i . oOO
if 13 - 13: oOO
if 2 - 2: i1
if 22 - 22: IIiIIiIi11I1 - ooo000 / I1Ii1I1 . ooo000
if 1 - 1: iI1iII1I1I1i + Ooo0Ooo + oOO * IIiIIiIi11I1
if 20 - 20: I1I + Ii
if 75 - 75: Ii % i1iiIII111 * Ii . IIiIIiIi11I1 % I11iiIi11i1I % I1Ii1I1
if 8 - 8: I1Ii1I1 . IiI11Ii111 . i1 . Oo - i1I
if 32 - 32: Ii % i1i1i1111I % i1I - I11iiIi11i1I % i1iiIII111
if 34 - 34: i1iiIII111 * i1
if 34 - 34: oOo0O00 / i1iiIII111 - Iii1i . iI1iII1I1I1i
if 80 - 80: i1i1i1111I . I1I % ooOOO % IiIIii11Ii / i1i1i1111I
if 32 - 32: I1Ii1I1 + oOO - oOo0O00
if 79 - 79: Iii1i % oOO * Oo + ooOOO / Oo . oOO
if 20 - 20: IiI11Ii111 + i1iiIII111 / I1I
if 88 - 88: I11iiIi11i1I + ooOOO - i1i1i1111I . Ooo0Ooo * Ii + Iii1i
if 43 - 43: ooOOO * I1Ii1I1
if 95 - 95: Ooo0Ooo % Iii1i % i1i1i1111I . OooOoo
if 70 - 70: IiIIii11Ii
if 75 - 75: Ooo0Ooo / Ii / IiIIii11Ii + IiIIii11Ii . I1I
if 88 - 88: Oo * IiIIii11Ii
if 100 - 100: IiI11Ii111 - OooOoo * I1Ii1I1 / Ooo0Ooo / Iii1i
if 23 - 23: Ooo0Ooo + i1 * I1Ii1I1 + Oo * Ii - IIiIIiIi11I1
if 29 - 29: IiIIii11Ii - oOo0O00
if 30 - 30: I1I . ooo000
if 43 - 43: ooOOO . I11iiIi11i1I + ooo000
if 87 - 87: Iii1i + ooOOO . i1I / Ii + Oo
if 77 - 77: i1iiIII111 + IiI11Ii111 - Oo % ooo000
if 74 - 74: Ii + Ooo0Ooo
if 1 - 1: I1I % Ooo0Ooo + i1iiIII111 . i1iiIII111 % Oo
if 93 - 93: oOo0O00 % Ooo0Ooo * i1iiIII111
if 52 - 52: oOO + I1I / ooo000 - I1Ii1I1 * i1I % oOo0O00
if 52 - 52: oOo0O00 . I1I + i1I - i1iiIII111 % iI1iII1I1I1i
if 57 - 57: I1I * IIiIIiIi11I1 % I1Ii1I1 * i1i1i1111I
if 37 - 37: IiI11Ii111 * i1i1i1111I + oOo0O00 / I1I / OooOoo
if 4 - 4: i1
if 61 - 61: iI1iII1I1I1i . i1I - ooo000 / ooo000 - i1
if 19 - 19: Iii1i * Ooo0Ooo . I1Ii1I1 / I11iiIi11i1I * Ii - oOO
if 32 - 32: iI1iII1I1I1i
if 18 - 18: I11iiIi11i1I * IiI11Ii111 % iI1iII1I1I1i + IiI11Ii111
if 93 - 93: oOO - I1Ii1I1 - IIiIIiIi11I1 * ooOOO - i1
if 82 - 82: IIiIIiIi11I1 % i1 * ooOOO
if 57 - 57: oOo0O00
if 31 - 31: i1iiIII111 + i1i1i1111I % OooOoo
if 20 - 20: OooOoo - I1I
if 9 - 9: i1iiIII111 - iI1iII1I1I1i % Ii - I1I
if 54 - 54: Iii1i % ooo000 % Iii1i - IiIIii11Ii
if 39 - 39: oOO - oOO * i1 % IIiIIiIi11I1
if 29 - 29: IIiIIiIi11I1 - ooo000 . i1iiIII111
if 86 - 86: I1Ii1I1 - OooOoo - oOO % ooo000 . i1I % Iii1i
if 11 - 11: OooOoo - I1Ii1I1 - ooOOO . i1iiIII111 - iI1iII1I1I1i / i1iiIII111
if 44 - 44: Ooo0Ooo + i1i1i1111I + Iii1i - oOO
if 7 - 7: i1i1i1111I / Ii * Iii1i
if 32 - 32: IiI11Ii111 . OooOoo
class wgOptimizePrivilege ( object ) :
 if 31 - 31: Oo - i1I
 if 28 - 28: ooOOO * I1Ii1I1 + IiI11Ii111 % Oo
 if 100 - 100: Oo + i1I
 dictControllerEditPrivi = dict ( )
 activeControllerUploadAll = 0
 activeControllerEditPrivi = 0
 dealingControllerSN = 0
 dealingStatusUploadEdit = 0
 arrCommandPriviEdit = deque ( )
 taskqueueUDPQueue4OptimizePrivie = Queue ( )
 if 4 - 4: ooo000 % I1I - i1i1i1111I
 qControllerEditPrivi = deque ( )
 wakeupTime = 0
 Active = 1
 if 76 - 76: i1 * oOo0O00 . IiI11Ii111 * i1I . IiIIii11Ii . oOO
 AutoRun = 1
 if 55 - 55: i1i1i1111I + i1iiIII111 % Ooo0Ooo . Oo - IiIIii11Ii - iI1iII1I1I1i
 if 91 - 91: I1Ii1I1 - I11iiIi11i1I
 def __init__ ( self ) :
  super ( wgOptimizePrivilege , self ) . __init__ ( )
  if 84 - 84: oOO % iI1iII1I1I1i - Ooo0Ooo
  if 94 - 94: i1iiIII111 + i1i1i1111I / iI1iII1I1I1i + iI1iII1I1I1i / i1I
  if 79 - 79: i1iiIII111 - IIiIIiIi11I1 . I1Ii1I1 + I1I - ooOOO + i1iiIII111
  if 36 - 36: ooOOO * Iii1i % I1I % i1 . Ooo0Ooo
  if 63 - 63: IIiIIiIi11I1 / IIiIIiIi11I1 * Iii1i - oOo0O00 . i1
  if 52 - 52: oOo0O00 / i1I * IIiIIiIi11I1 + I11iiIi11i1I % Ooo0Ooo + oOO
  if 43 - 43: iI1iII1I1I1i * oOo0O00 + ooOOO
  if 30 - 30: I1I
 @ staticmethod
 def work ( ) :
  o00ooo0OO0000 = time . time ( )
  if wgOptimizePrivilege . Active :
   if 37 - 37: I1I
   if 38 - 38: I1Ii1I1 + oOo0O00
   if o00ooo0OO0000 > wgOptimizePrivilege . wakeupTime or ( 7 * 24 * 3600 + 3600 ) < abs (
 wgOptimizePrivilege . wakeupTime - o00ooo0OO0000 ) :
    if wgOptimizePrivilege . wakeupTime == 0 :
     iii1I1 = time . strptime ( time . strftime ( '%Y-%m-%d 02:45:00' ) , '%Y-%m-%d %H:%M:%S' )
     Ii1 = 7 - ( iii1I1 . tm_wday + 1 )
     if Ii1 == 0 :
      Ii1 = 7
     wgOptimizePrivilege . wakeupTime = datetime . strptime ( time . strftime ( '%Y-%m-%d 02:45:00' ) ,
 '%Y-%m-%d %H:%M:%S' ) . timestamp ( ) + Ii1 * 24 * 3600
    else :
     wgOptimizePrivilege . wakeupTime = o00ooo0OO0000 + 7 * 24 * 3600
     wgOptimizePrivilege . startWakeup ( )
     if 37 - 37: iI1iII1I1I1i - I1Ii1I1 + ooOOO . IIiIIiIi11I1
 @ staticmethod
 def updatePrivilegeCommandMode ( conSN , strCommandmethod , privlast , rpcID ) :
  if 15 - 15: ooo000
  if 63 - 63: i1I
  if wgOptimizePrivilege . Active and wgOptimizePrivilege . AutoRun :
   if strCommandmethod in [ 'UploadAllPrivileges' , 'UploadAllPrivilegesByZip' ] :
    if rpcID != 9999994004 :
     wgOptimizePrivilege . activeControllerUploadAll = time . time ( )
   elif strCommandmethod == 'AddEditPrivilege' :
    wgOptimizePrivilege . activeControllerEditPrivi = time . time ( )
    wgOptimizePrivilege . dictControllerEditPrivi [ conSN ] = time . time ( )
    if rpcID != 9999994001 and wgOptimizePrivilege . dealingControllerSN == conSN :
     i1Ii = privlast
     wgOptimizePrivilege . arrCommandPriviEdit . append ( i1Ii )
     if 32 - 32: oOO
   pass
   if 98 - 98: IiI11Ii111 % i1 % ooo000 % iI1iII1I1I1i
 @ staticmethod
 def startWakeup ( ) :
  if wgOptimizePrivilege . taskqueueUDPQueue4OptimizePrivie . empty ( ) :
   wgOptimizePrivilege . taskqueueUDPQueue4OptimizePrivie . put (
 "UDPQueue4wgOptimizePrivilege.taskqueueUDPQueue4OptimizePrivie" )
   if 57 - 57: Ooo0Ooo % i1I + IiI11Ii111
 @ staticmethod
 def DealUDPQueue4OptimizePrivilege ( ) :
  if wgOptimizePrivilege . Active :
   while True :
    if 8 - 8: Oo * I1I / i1i1i1111I + i1 / I1Ii1I1
    if 71 - 71: I1Ii1I1
    o00ooo0OO0000 = time . time ( )
    if wgOptimizePrivilege . qControllerEditPrivi . __len__ ( ) > 0 :
     I11IIiiI1 = wgOptimizePrivilege . qControllerEditPrivi . popleft ( )
     O0o = wgOptimizePrivilege . activeControllerUploadAll
     wgOptimizePrivilege . optimizePriv ( I11IIiiI1 , True )
     wgOptimizePrivilege . activeControllerUploadAll = O0o
    elif wgOptimizePrivilege . activeControllerUploadAll > 0 and abs (
 o00ooo0OO0000 - wgOptimizePrivilege . activeControllerUploadAll ) < 30 * 24 * 3600 :
     wgOptimizePrivilege . dictControllerEditPrivi . clear ( )
    elif wgOptimizePrivilege . dictControllerEditPrivi . __len__ ( ) > 0 :
     if wgOptimizePrivilege . AutoRun :
      for I11IIiiI1 in wgOptimizePrivilege . dictControllerEditPrivi . keys ( ) :
       wgOptimizePrivilege . optimizePriv ( I11IIiiI1 , False )
     wgOptimizePrivilege . activeControllerUploadAll = 0
     if 34 - 34: I1I % Oo - i1 % OooOoo * OooOoo + i1i1i1111I
     wgOptimizePrivilege . dictControllerEditPrivi . clear ( )
     if 8 - 8: IiI11Ii111 + i1I
     if 37 - 37: iI1iII1I1I1i . oOo0O00 * ooo000 * ooOOO
     if 65 - 65: iI1iII1I1I1i - IiI11Ii111 . Ooo0Ooo . i1iiIII111 * IiI11Ii111
     if 33 - 33: i1i1i1111I
    else :
     time . sleep ( 0.001 )
     try :
      iI1iiI11IIi1 = wgOptimizePrivilege . taskqueueUDPQueue4OptimizePrivie . get (
 block = True )
      wgOptimizePrivilege . taskqueueUDPQueue4OptimizePrivie . task_done ( )
     except Exception as OOO :
      pass
      if 53 - 53: Oo + i1iiIII111 / Ooo0Ooo
      if 89 - 89: ooo000 + Ii % i1i1i1111I - i1iiIII111
      if 33 - 33: ooo000 . Iii1i % oOO
 @ staticmethod
 def load ( ) :
  if wgOptimizePrivilege . Active :
   mMonitorInfo . wglog ( "{} wgOptimizePrivilege start.." . format ( time . strftime ( '%Y-%m-%d %H:%M:%S' ) ) )
   OoOOooO0oOO0Oo = threading . Thread (
 target = wgOptimizePrivilege . DealUDPQueue4OptimizePrivilege )
   OoOOooO0oOO0Oo . start ( )
   if 4 - 4: i1 % I1Ii1I1 * oOO + oOO . i1i1i1111I - i1
 @ staticmethod
 def sendByHttpWithMsg ( arrinfo , timeoutSec = 3 ) :
  I1i = None
  try :
   O0OOOOoO0o = wgGlobal . dataserverIP
   if wgGlobal . dataserverIP == "0.0.0.0" :
    O0OOOOoO0o = "127.0.0.1"
   o00ooOOO0Oo = wgGlobal . webServerPort
   if 37 - 37: i1I + IIiIIiIi11I1 % iI1iII1I1I1i / IIiIIiIi11I1 % i1iiIII111 + oOO
   Oo0o0Oo = http . client . HTTPConnection ( host = O0OOOOoO0o , port = o00ooOOO0Oo , timeout = 3 )
   o0O0OO0 = bytes ( json . dumps ( arrinfo , sort_keys = False , indent = 4 ) , encoding = 'utf8' )
   Iioo0Oo0oO0 = { "Content-type" : "application/json" , "Accept" : "application/json" }
   Oo0o0Oo . timeout = timeoutSec
   Oo0o0Oo . request ( "POST" , O0OOOOoO0o , o0O0OO0 , Iioo0Oo0oO0 )
   iII11I1iI = Oo0o0Oo . getresponse ( )
   O0O0o0oo00Oo = iII11I1iI . read ( )
   Oo0o0Oo . close ( )
   try :
    I11 = json . loads ( O0O0o0oo00Oo . decode ( "utf-8" ) )
    I1i = I11 [ 'result' ]
   except Exception as I1Ii :
    mMonitorInfo . wglog ( I1Ii )
    I1i = None
    return I1i
   return I1i
  except Exception as I1Ii :
   pass
   print ( I1Ii )
  return None
  if 6 - 6: oOo0O00 . IiIIii11Ii + Ooo0Ooo
 @ staticmethod
 def optimizePriv ( sn , force ) :
  I1i = None
  if wgOptimizePrivilege . Active :
   try :
    if 19 - 19: Ooo0Ooo % i1I
    if 88 - 88: I1Ii1I1 / i1i1i1111I
    o0ooO0OOO = ( {
 "jsonrpc" : "2.0" ,
 "method" : "GetPrivilegeTotal" ,
 "params" : [ { "ControllerSN" : sn } ] ,
 "id" : 4006
 } )
    I11iiI = wgOptimizePrivilege . sendByHttpWithMsg (
 o0ooO0OOO )
    if 45 - 45: Ooo0Ooo + ooOOO * oOo0O00 - ooo000 / IIiIIiIi11I1
    if I11iiI is not None and "PrivilegeTotal" in I11iiI :
     try :
      I11 = I11iiI
      i1I1I1Ii = 0
      O0 = I11 [ "PrivilegeTotal" ]
      Ii1Ii1 = 3
      if O0 > 100 :
       if 25 - 25: OooOoo + Oo * Ii * iI1iII1I1I1i + Ii
       Ii1Ii1 = Ii1Ii1 + O0 / 50
      if "DeletedPrivileteTotal" in I11 :
       i1I1I1Ii = I11 [ "DeletedPrivileteTotal" ]
       if 75 - 75: OooOoo * oOo0O00 . i1iiIII111
       if 96 - 96: I1I / oOO / I11iiIi11i1I + I11iiIi11i1I
      mMonitorInfo . wglog ( "optimizePriv00 sn={} {} " . format ( sn , I11iiI ) )
      mMonitorInfo . printCHS ( "optimizePriv00 sn={} {} " . format ( sn , I11iiI ) )
      if O0 > 0 and ( ( i1I1I1Ii > 50 and O0 > 500 ) or force ) :
       if 35 - 35: IIiIIiIi11I1 + oOo0O00
       wgOptimizePrivilege . dealingControllerSN = sn
       if 96 - 96: iI1iII1I1I1i . OooOoo . i1
       o0ooO0OOO = ( {
 "jsonrpc" : "2.0" ,
 "method" : "GetPrivilege" ,
 "params" : [ { "ControllerSN" : sn } ] ,
 "id" : 4005
 } )
       I11iiI = wgOptimizePrivilege . sendByHttpWithMsg ( o0ooO0OOO , Ii1Ii1 )
       if 87 - 87: ooo000 * IiIIii11Ii % ooo000 . ooOOO . Oo % iI1iII1I1I1i
       if I11iiI :
        mMonitorInfo . wglog ( "optimizePriv01 {} {}" . format ( "UploadAllPrivileges" , sn ) )
        OO0Oo0oOo00 = I11iiI [ "RecordInformation" ]
        o0ooO0OOO = ( {
 "jsonrpc" : "2.0" ,
 "method" : "UploadAllPrivileges" ,
 "params" : [ { "ControllerSN" : sn ,
 "Privilege" : OO0Oo0oOo00
 } ] ,
 "id" : 9999994004
        } )
        I11iiI = wgOptimizePrivilege . sendByHttpWithMsg ( o0ooO0OOO , Ii1Ii1 )
        if 42 - 42: i1i1i1111I / iI1iII1I1I1i * oOo0O00 * Ooo0Ooo * Oo
        I11 = I11iiI
        if I11 [ 'success' ] :
         I1i = "OK"
        else :
         I1i = I11 [ 'msg' ]
        mMonitorInfo . wglog ( "optimizePriv11 {} {} {} {}" . format ( "UploadAllPrivileges" , sn , I1i , I11iiI ) )
        if 18 - 18: Ii
        if I1i == "OK" :
         if wgOptimizePrivilege . arrCommandPriviEdit . __len__ ( ) > 0 :
          while wgOptimizePrivilege . arrCommandPriviEdit . __len__ ( ) > 0 :
           i1Ii = wgOptimizePrivilege . arrCommandPriviEdit . popleft ( )
           if 39 - 39: Oo . oOO / ooo000 / iI1iII1I1I1i
           if 48 - 48: oOO * i1I - OooOoo * Oo - IiI11Ii111 - Ooo0Ooo
           if 36 - 36: IiIIii11Ii
           if 28 - 28: i1 - Oo + IiIIii11Ii * oOO
           if 83 - 83: ooo000 . I1I + Oo
           if 59 - 59: I1I
           if 83 - 83: Iii1i % i1iiIII111 . OooOoo / I1I % oOO . I1I
           if 76 - 76: i1iiIII111 / OooOoo
           o0ooO0OOO = ( {
 "jsonrpc" : "2.0" ,
 "method" : "AddEditPrivilege" ,
 "params" : [
 {
 "ControllerSN" : sn ,
 "Privilege" : [
 i1Ii ,
 ]
 }
 ] ,
 "id" : 9999994001
           } )
           I11iiI = wgOptimizePrivilege . sendByHttpWithMsg ( o0ooO0OOO , Ii1Ii1 )
           print ( I11iiI )
           I11 = I11iiI
           if I11 [ 'success' ] :
            I1i = "OK"
           else :
            I1i = I11 [ 'msg' ]
            mMonitorInfo . wglog (
 "optimizePriv12 {} {} {} {}" . format ( "UploadAllPrivileges" , sn , I1i , I11iiI ) )
            break
            if 77 - 77: ooOOO
            if 19 - 19: ooOOO % I1Ii1I1
            if 15 - 15: OooOoo . IiIIii11Ii . i1I / Iii1i + ooOOO / Ii
            if 17 - 17: I11iiIi11i1I - i1i1i1111I . iI1iII1I1I1i - I11iiIi11i1I + Oo % iI1iII1I1I1i
            if 65 - 65: Ii % I11iiIi11i1I
            if 39 - 39: Iii1i * IIiIIiIi11I1 . Ooo0Ooo - Oo
            if 63 - 63: i1i1i1111I - i1iiIII111 . OooOoo % OooOoo . IiI11Ii111 + i1I
            if 71 - 71: ooo000 + I11iiIi11i1I % iI1iII1I1I1i + IiI11Ii111 % Oo - Oo
            if 84 - 84: I1I % iI1iII1I1I1i - Ooo0Ooo / iI1iII1I1I1i + Ooo0Ooo - Oo
            if 41 - 41: ooOOO + OooOoo + IIiIIiIi11I1 * i1i1i1111I
            if 12 - 12: i1i1i1111I
            if 56 - 56: IiIIii11Ii
            if 17 - 17: i1I . oOO % Oo + IiIIii11Ii - Ooo0Ooo
            if 93 - 93: oOo0O00
          wgOptimizePrivilege . arrCommandPriviEdit . clear ( )
          if 77 - 77: Oo + I11iiIi11i1I % I1I
          if 20 - 20: i1 - IiIIii11Ii . IiIIii11Ii % ooOOO . i1 % Ooo0Ooo
       wgOptimizePrivilege . dealingControllerSN = 0
       if 72 - 72: oOO % IiI11Ii111 . ooOOO * I1Ii1I1 . ooOOO
      else :
       if 90 - 90: IiIIii11Ii + I1Ii1I1 . OooOoo
       pass
     except Exception as o00o :
      mMonitorInfo . printCHS ( o00o )
      mMonitorInfo . wglog ( "optimizePriv901" )
      mMonitorInfo . wglog ( o00o )
      pass
      if 12 - 12: I1I % Ooo0Ooo - i1iiIII111 - IiIIii11Ii / i1iiIII111 + i1I
      if 77 - 77: Ii
      if 20 - 20: Ii % i1i1i1111I
   except Exception as OOO0OOOo0oOO :
    mMonitorInfo . printCHS ( OOO0OOOo0oOO )
    mMonitorInfo . wglog ( "optimizePriv902" )
    mMonitorInfo . wglog ( OOO0OOOo0oOO )
    if 15 - 15: i1I * I11iiIi11i1I - oOo0O00
    pass
   wgOptimizePrivilege . dealingControllerSN = 0
  return I1i
# dd678faae9ac167bc83abf78e5cb2f3f0688d3a3
