#NoTrayIcon
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Resources\script.ico
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_File_Add=Resources\start.wav, WAVE, START
#AutoIt3Wrapper_Res_File_Add=Resources\end.wav, WAVE, END
#AutoIt3Wrapper_Res_File_Add=Resources\endtoday.wav, WAVE, ENDTODAY
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/striponly
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Date.au3>
#include <WinAPIEx.au3>;Yashied, http://autoit-script.ru/index.php/topic,47.0.html
#include <APIConstants.au3>
Opt('TrayMenuMode', 1)
Opt('MustDeclareVars', 1)
Global $aCalls, $iIndex, $iCountCalls, $sToDayOld = @MDAY, $sToDay, $fNew, $sNextDate, $iStart, $iTimeOut, _
$aIcon[3][2] = [['В периоде', 101],['Вне периода', 104],['На сегодня все', 100]], _
$sIconFile = @SystemDir & '\user32.dll', $nExit
If WinExists(@ScriptName & '{[/@$@\]}') Then Exit
AutoItWinSetTitle(@ScriptName & '{[/@$@\]}')
$nExit = TrayCreateItem('Выход')
TraySetState()
_GetCallsToday($aCalls, $iIndex, $iCountCalls)
If $iCountCalls Then
$iTimeOut = $aCalls[$aCalls[0][$iIndex]][$iIndex] * 1000
TraySetIcon($sIconFile, $aIcon[Number(Not $iIndex)][1])
TraySetToolTip($aIcon[Number(Not $iIndex)][0])
Else
$sNextDate = _DateAdd('d', 1, _NowCalcDate())
$iTimeOut = _DateDiff('s', _NowCalc(), $sNextDate & ' 00:00:00') * 1000
$fNew = True
TraySetIcon($sIconFile, $aIcon[2][1])
TraySetToolTip($aIcon[2][0])
EndIf
$iStart = TimerInit()
_WinAPI_EmptyWorkingSet()
ConsoleWrite($iTimeOut & @LF)
ConsoleWrite($iIndex & @LF)
ConsoleWrite($iCountCalls & @LF)
While 1
If TimerDiff($iStart) >= $iTimeOut Then
If $fNew Then
_GetCallsToday($aCalls, $iIndex, $iCountCalls)
If $iCountCalls Then
$iTimeOut = $aCalls[$aCalls[0][$iIndex]][$iIndex] * 1000
$fNew = False
TraySetIcon($sIconFile, $aIcon[Number(Not $iIndex)][1])
TraySetToolTip($aIcon[Number(Not $iIndex)][0])
Else
$sNextDate = _DateAdd('d', 1, _NowCalcDate())
$iTimeOut = _DateDiff('s', _NowCalc(), $sNextDate & ' 00:00:00') * 1000
$fNew = True
TraySetIcon($sIconFile, $aIcon[2][1])
TraySetToolTip($aIcon[2][0])
EndIf
$iStart = TimerInit()
_WinAPI_EmptyWorkingSet()
Else
$iCountCalls -= 1
If $iCountCalls Then
ConsoleWrite($aIcon[$iIndex][0] & @LF)
ConsoleWrite(@HOUR & ':' & @MIN & ':' & @SEC & @LF)
_PlaySound($iIndex)
TraySetIcon($sIconFile, $aIcon[$iIndex][1])
TraySetToolTip($aIcon[$iIndex][0])
$aCalls[0][$iIndex] += 1
$iIndex = Number(Not $iIndex)
$iTimeOut = $aCalls[$aCalls[0][$iIndex]][$iIndex] * 1000
Else
$sToDay = @MDAY
If $sToDayOld <> $sToDay Then
$sToDayOld = $sToDay
_GetCallsToday($aCalls, $iIndex, $iCountCalls)
If $iCountCalls Then
$iTimeOut = $aCalls[$aCalls[0][$iIndex]][$iIndex] * 1000
$fNew = False
TraySetIcon($sIconFile, 104)
TraySetToolTip('Wait...')
Else
$sNextDate = _DateAdd('d', 1, _NowCalcDate())
$iTimeOut = _DateDiff('s', _NowCalc(), $sNextDate & ' 00:00:00') * 1000
$fNew = True
TraySetIcon($sIconFile, $aIcon[2][1])
TraySetToolTip($aIcon[2][0])
EndIf
$iStart = TimerInit()
_WinAPI_EmptyWorkingSet()
Else
$sNextDate = _DateAdd('d', 1, _NowCalcDate())
$iTimeOut = _DateDiff('s', _NowCalc(), $sNextDate & ' 00:00:00') * 1000
$fNew = True
_PlaySound(2)
TraySetIcon($sIconFile, $aIcon[2][1])
TraySetToolTip($aIcon[2][0])
ConsoleWrite($aIcon[2][0] & @LF)
ConsoleWrite(@HOUR & ':' & @MIN & ':' & @SEC & @LF)
EndIf
EndIf
ConsoleWrite($iTimeOut & @LF)
EndIf
EndIf
Switch TrayGetMsg()
Case 0
ContinueLoop
Case $nExit
Exit
EndSwitch
WEnd
Func _GetCallsToday(ByRef $a_Calls, ByRef $i_Index, ByRef $i_CountCalls)
Local $s_File = @ScriptDir & '\Condition.txt', $s_Pattern = '(?m)^(.*\Q' & @MDAY & '.' & @MON & '.' & @YEAR & '\E.*)$', _
$s_Text, $a_TempToDay, $i_Ubound, $a_TempString, $i_Count, $i_TempStart, $i_TempEnd
$s_Text = _CheckFile($s_File)
$a_Calls = 0
$i_CountCalls = 0
$i_Index = 1
$a_TempToDay = StringRegExp($s_Text, $s_Pattern, 3)
If @error Then Return
$i_Ubound = UBound($a_TempToDay)
Dim $a_Calls[$i_Ubound + 1][2]
For $i = 0 To $i_Ubound - 1
$a_TempString = StringSplit($a_TempToDay[$i], '-', 2)
If UBound($a_TempString) <> 2 Then ContinueLoop
For $j = 0 To 1
$a_TempString[$j] = StringStripWS($a_TempString[$j], 7)
$a_TempString[$j] = StringRegExpReplace($a_TempString[$j], '^(\d{2})\.(\d{2})\.(\d{4})(.*?)$', '$3/$2/$1$4:00')
$a_TempString[$j] = _DateDiff('s', _NowCalc(), $a_TempString[$j])
Next
If $a_TempString[0] >= 0 Or $a_TempString[1] >= 0 Then
$i_Count += 1
If $a_TempString[0] >= 0 Then
$a_Calls[$i_Count][0] = $a_TempString[0]
$i_CountCalls += 1
If Not $a_Calls[0][0] Then $a_Calls[0][0] = $i_Count
EndIf
If $a_TempString[1] >= 0 Then
$a_Calls[$i_Count][1] = $a_TempString[1]
$i_CountCalls += 1
If Not $a_Calls[0][1] Then $a_Calls[0][1] = $i_Count
EndIf
EndIf
Next
If $i_Count Then
ReDim $a_Calls[$i_Count + 1][2]
If $a_Calls[0][0] Then
If $a_Calls[0][0] <= $a_Calls[0][1] Then $i_Index = 0
EndIf
EndIf
EndFunc ;==>_GetCallsToday
Func _CheckFile($s_File)
Local $s_Text, $a_Temp, $a_TempString, $a_Array[1][2]
If Not FileExists($s_File) Then
TrayTip('Периоды', 'Отсутствует файл с временными периодами', 5, 3)
Sleep(5000)
Exit
EndIf
$s_Text = FileRead($s_File)
If Not $s_Text Then
TrayTip('Периоды', 'Ошибка чтения файла с временными периодами', 5, 3)
Sleep(5000)
Exit
EndIf
$s_Text = StringRegExpReplace($s_Text, '([\r\n]*$)', '')
$a_Temp = StringSplit(StringStripCR($s_Text), @LF)
ReDim $a_Array[$a_Temp[0] + 1][2]
$a_Array[0][0] = $a_Temp[0]
For $i = 1 To $a_Temp[0]
$a_TempString = StringSplit($a_Temp[$i], '-', 2)
If UBound($a_TempString) <> 2 Then
TrayTip('Периоды', 'Ошибка в формате файла с временными периодами', 5, 3)
Sleep(5000)
Exit
EndIf
For $j = 0 To 1
$a_Array[$i][$j] = StringRegExpReplace($a_TempString[$j], '.*(\d{2})\.(\d{2})\.(\d{4})\s+(\d{2}):(\d{2}).*', '$3$2$1$4$5')
If @extended <> 5 Then
TrayTip('Периоды', 'Ошибка в формате файла с временными периодами', 5, 3)
Sleep(5000)
Exit
EndIf
$a_Array[$i][$j] = Number($a_Array[$i][$j])
Next
Next
For $i = 1 To $a_Array[0][0] - 1
If $a_Array[$i][0] >= $a_Array[$i][1] Or $a_Array[$i + 1][0] <= $a_Array[$i][1] Then
ConsoleWrite($i & @LF)
TrayTip('Периоды', 'Ошибка в формате файла с временными периодами', 5, 3)
Sleep(5000)
Exit
EndIf
Next
Return $s_Text
EndFunc ;==>_CheckFile
Func _PlaySound($i_Index)
Local $s_Dir = @ScriptDir & '\Resources\', _
$a_Sound[3][2] = [['start.wav', 'START'],['end.wav', 'END'],['endtoday.wav', 'ENDTODAY']]
If @Compiled Then
_WinAPI_PlaySound($a_Sound[$i_Index][1], BitOR($SND_ASYNC, $SND_RESOURCE), _WinAPI_GetModuleHandle(0))
Else
SoundPlay($s_Dir & $a_Sound[$i_Index][0])
EndIf
EndFunc ;==>_PlaySound