Advertisement
Guest User

Untitled

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