Advertisement
Guest User

Untitled

a guest
Jan 15th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 6.76 KB | None | 0 0
  1. #include <Array.au3>
  2. #include <File.au3>
  3. #include <Date.au3>
  4. #include <IE.au3>
  5. #include <_XMLDomWrapper.au3>
  6.  
  7. Dim $arrLogFile[1]
  8.  
  9. Global $RecordingPath = "Y:\Recorded TV"
  10. Global $DVBVResponseXMLFile
  11. Global $ResponseXMLpath = @ScriptDir & "\DVBVRecordMonitor.xml"
  12. Global $DebugFile = @ScriptDir & "\RecordMonitor.txt"
  13. Global $DVBViewerLogPath = "C:\ProgramData\CMUV\DVBViewer\svcdebug.log"
  14. Dim $arrRecordingIndexes[1], $arrRecordingTimerIDs[1]
  15. Global $FoundRecording = False, $RestartRecording = False, $TimeStampFound = False
  16.  
  17. $arrCompleteRecordings = _FileListToArray($RecordingPath, "*.ts")
  18.  
  19. If $arrCompleteRecordings = 0 Then
  20.     $CompleteRecordingCount = 0
  21. Else
  22.     ;_ArrayDisplay($arrCompleteRecordings)
  23.     $CompleteRecordingCount = $arrCompleteRecordings[0]
  24. EndIf
  25. _Log("Monitor started...")
  26. ;_ArrayDisplay($arrCompleteRecordings)
  27.  
  28. ;~ _ArrayDelete($arrCompleteRecordings, 11)
  29. ;~ $CompleteRecordingCount = $CompleteRecordingCount-1
  30. ;~ _ArrayDisplay($arrCompleteRecordings)
  31.  
  32. While 1
  33.  
  34.     ;_Log("Checking recordings....")
  35.  
  36.     $arrRecordings = _FileListToArray($RecordingPath, "*.ts")
  37.  
  38.     If $arrRecordings = 0 Then
  39.         $NewRecordingCount = 0
  40.     Else
  41.         $NewRecordingCount = $arrRecordings[0]
  42.     EndIf
  43.  
  44. ;~  For $i = 1 to uBound($arrRecordings)-1
  45. ;~      _Log("Found recording: " & $arrRecordings[$i])
  46. ;~  Next
  47.  
  48.     ;_Log("New recording count: " & $arrRecordings[0] & " vs old: " & $CompleteRecordingCount)
  49.  
  50.     If $NewRecordingCount <> $CompleteRecordingCount Then
  51.  
  52.         _Log("Recording count changed...")
  53.  
  54.         For $x = 1 to uBound($arrRecordings)-1
  55.  
  56.             $d = _ArraySearch($arrCompleteRecordings, $arrRecordings[$x])
  57.  
  58.             If $d = -1 Then
  59.  
  60.                 _Log("Found new recording: " & $arrRecordings[$x])
  61.  
  62.                 $arrNewRecTimeStamp = FileGetTime($RecordingPath & "\" & $arrRecordings[$x])
  63.  
  64.                 If $arrNewRecTimeStamp = 0 Then
  65.  
  66.                     _Log("Failed to get file modified time, retrying...")
  67.  
  68.                     $Timer = 0
  69.  
  70.                     While 1
  71.  
  72.                         If $Timer > 30 then
  73.                             ExitLoop
  74.                         EndIf
  75.  
  76.                         $arrNewRecTimeStamp = FileGetTime($RecordingPath & "\" & $arrRecordings[$x])
  77.  
  78.                         If $arrNewRecTimeStamp <> 0 then
  79.                             $TimeStampFound = True
  80.                             ExitLoop
  81.                         EndIf
  82.  
  83.                         Sleep(2000)
  84.  
  85.                         $Timer = $Timer + 2
  86.  
  87.                     WEnd
  88.  
  89.                 Else
  90.                     $TimeStampFound = True
  91.                 EndIf
  92.  
  93.                 If $TimeStampFound Then
  94.  
  95.                     $NewRecTimeStamp = $arrNewRecTimeStamp[0] & "/" & $arrNewRecTimeStamp[1] & "/" & $arrNewRecTimeStamp[2] & " " & $arrNewRecTimeStamp[3] & ":" & $arrNewRecTimeStamp[4] & ":" & $arrNewRecTimeStamp[5]
  96.  
  97.                     $RecordingAge = _DateDiff("s", $NewRecTimeStamp,  _NowCalc())
  98.                     _Log("Recording is " & $RecordingAge & " seconds old...")
  99.  
  100.  
  101.  
  102.                     If $RecordingAge < 90 then Sleep(80000)
  103.  
  104.                     $LogFileToCheck = $RecordingPath & "\" & StringReplace($arrRecordings[$x], ".ts", ".log")
  105.                     _Log("Log file: " & $LogFileToCheck)
  106.  
  107.                     $Timer = 0
  108.                     $ErrorCount = 0
  109.  
  110.                     While 1
  111.  
  112.                         If $Timer > 30 then ExitLoop
  113.  
  114.                         $OpenLog = _FileReadToArray($LogFileToCheck,$arrLogFile)
  115.  
  116.                         If $OpenLog = 1 then
  117.  
  118.                             For $y = 1 to uBound($arrLogFile)-1
  119.                                 If StringInStr($arrLogFile[$y], "Errors") Then
  120.                                     $ErrorCount = $ErrorCount+1
  121.                                 EndIf
  122.                             Next
  123.  
  124.                             ExitLoop
  125.  
  126.                         Else
  127.                             _Log("Couldn't access log retrying...")
  128.                             Sleep(3000)
  129.                             $Timer = $Timer + 1
  130.                         EndIf
  131.  
  132.                     WEnd
  133.  
  134.                     _Log("Detected " & $ErrorCount & " occurances of errors")
  135.  
  136.                     If $ErrorCount > 8 Then
  137.                         _RestartRecording()
  138.                     Else
  139.                         _Log("No error detected, leaving this recording.")
  140.                     EndIf
  141.  
  142.                     $TimeStampFound = False
  143.  
  144.                 Else
  145.                     _Log("Couldn't get file modified time, skipping this recording...")
  146.                 EndIf
  147.  
  148.                 _ArrayAdd($arrCompleteRecordings,$arrRecordings[$x])
  149.                 $CompleteRecordingCount = $CompleteRecordingCount+1
  150.                 $arrRecordings[0] = $arrRecordings[0]+1
  151.  
  152.             EndIf
  153.         Next
  154.  
  155.         ;Nmber of recordings has decreased so let check run above in case a recording has started AND files(s) have been deleted then set recording count to new, lower value.
  156.         If $arrRecordings[0] < $CompleteRecordingCount Then
  157.             _Log("Recording count decreased")
  158.             $CompleteRecordingCount = $arrRecordings[0]
  159.         EndIf
  160.  
  161.     EndIf
  162.  
  163.     Sleep(60000)
  164.  
  165. WEnd
  166.  
  167. Func _RestartRecording()
  168.  
  169.     _Log("Attempting to restart the recording...")
  170.     ReDim $arrRecordingIndexes[1]
  171.  
  172.     FileDelete($ResponseXMLpath)
  173.  
  174.     $oResponse = InetGet("http://192.168.0.2:8089/api/timerlist.html",$ResponseXMLpath,1,1)
  175.  
  176.     Do
  177.         Sleep(250)
  178.     Until InetGetInfo($oResponse, 2) ; Check if the download is complete.
  179.  
  180.  
  181.     If FileExists($ResponseXMLpath) and $oResponse <> 0 Then
  182.  
  183.         $PchResponseXMLFile = _XMLFileOpen($ResponseXMLpath, "", -1, False)
  184.  
  185.         $TimerRecStatus = _XMLGetValue ( "//Timers/Timer/Recording" )
  186.         $TimerIDs = _XMLGetValue ( "//Timers/Timer/ID" )
  187.  
  188. ;~      If IsArray($TimerRecStatus) Then
  189. ;~          _ArrayDisplay($TimerRecStatus)
  190. ;~          _ArrayDisplay($TimerIDs)
  191. ;~      EndIf
  192.  
  193.         For $g = 1 to uBound($TimerRecStatus)-1
  194.             If $TimerRecStatus[$g] = -1 Then
  195.                 $FoundRecording = True
  196.                 _Log("Found a live recording in the Timers XML at index " & $g)
  197.                 _ArrayAdd($arrRecordingIndexes, $g)
  198.             EndIf
  199.  
  200.         Next
  201.  
  202.         ;_ArrayDisplay($arrRecordingIndexes, "Array Recording Indexes")
  203.  
  204.         If $FoundRecording Then
  205.             For $h = 1 to uBound($arrRecordingIndexes)-1
  206.                 $TimerFileName = _XMLGetValue ( "//Timers/Timer[" & $arrRecordingIndexes[$h] & "]/RealFilename" )
  207.                 _Log("Recording filename: " & $TimerFileName[1])
  208.                 ;_ArrayDisplay($TimerFileName, "$TimerFileName")
  209.  
  210.                 If $TimerFileName[1] = $RecordingPath & "\" & $arrRecordings[$x] Then
  211.                     _Log("Recording filename matches the one reporting errors...")
  212.                     ReDim $arrRecordingTimerIDs[$h][2]
  213.                     $arrRecordingTimerIDs[$h-1][0] = $TimerIDs[$arrRecordingIndexes[$h]]
  214.                     $arrRecordingTimerIDs[$h-1][1] = $TimerFileName[1]
  215.                     $RestartRecording = True
  216.                 Else
  217.                     _Log("Recording filename does not match the one reporting errors...")
  218.                     _Log("Reported errors: " & $RecordingPath & "\" & $arrRecordings[$x])
  219.                     _Log("Recording found in xml: " & $TimerFileName[1])
  220.                 EndIf
  221.  
  222.             Next
  223.  
  224.             ;_ArrayDisplay($arrRecordingTimerIDs, "Final Timer IDs")
  225.  
  226.             If $RestartRecording Then
  227.  
  228.                 _Log("Restarting the recording...")
  229.  
  230.                 $IEInstance = _IECreate("http://192.168.0.2:8089/api/timeredit.html?id=" & $arrRecordingTimerIDs[0][0] & "&enable=0")
  231.                 _IEQuit($IEInstance)
  232.  
  233.                 Sleep(20000)
  234.  
  235.                 $IEInstance = _IECreate("http://192.168.0.2:8089/api/timeredit.html?id=" & $arrRecordingTimerIDs[0][0] & "&enable=1")
  236.                 _IEQuit($IEInstance)
  237.  
  238.                 $RestartRecording = False
  239.                 _Log("Restart attempt complete")
  240.  
  241.             EndIf
  242.  
  243.         EndIf
  244.  
  245.     EndIf
  246.  
  247. EndFunc
  248.  
  249. Func _Log($logstring)
  250.     FileWriteLine($DebugFile, "[" & _DateTimeFormat(_NowCalc(), 0) & "] " & $logstring)
  251.     ConsoleWrite("[" & _DateTimeFormat(_NowCalc(), 0) & "] " & $logstring & @CRLF)
  252. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement