From 1e2c8ce56c996f3b6714c640ac7023abe94537bb Mon Sep 17 00:00:00 2001 From: Tomasz Kostrzewa Date: Wed, 27 Dec 2023 21:25:59 +0100 Subject: [PATCH] modified: HP_printer_555/hp_smart_tank_plus_555_head_clean.ps1 --- .../hp_smart_tank_plus_555_head_clean.ps1 | 112 ++++++++++-------- 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/HP_printer_555/hp_smart_tank_plus_555_head_clean.ps1 b/HP_printer_555/hp_smart_tank_plus_555_head_clean.ps1 index daf887e..6f0a398 100644 --- a/HP_printer_555/hp_smart_tank_plus_555_head_clean.ps1 +++ b/HP_printer_555/hp_smart_tank_plus_555_head_clean.ps1 @@ -2,19 +2,23 @@ # HP Smart Tank Plus 555 head clean. # -# requeres snmp +# requere installed snmp + +# set time when printer can run head cleanning +$running_time_start = 8 +$running_time_stop = 22 + +# set day interval between last print and head cleannig +$days_between = 7 +$sec_per_day = 86400 +$days_between = $days_between * $sec_per_day -$days = 7 # It indicates the number of days that must pass since the last cleaning session. -$secs_per_day = 86400 -$days = $days * $secs_per_day # Converted to seconds. $location = Get-Location $path_last_run = "$($location)/hp_clean_last_run" $path_log = "$($location)/hp_clean_log" $snmp_ver = "2c" $snmp_community = "public" -$snmp_code_01 = "iso.3.6.1.2.1.43.10.2.1.4.1.1" # OID for printed pages durring actuall sesion -$snmp_code_02 = "iso.3.6.1.2.1.43.10.2.1.5.1.1" # OID for printed pages to actuall session -$snmp_code_n = 1 # counter for pages starts from 0, so summary we need to decrase by 1 +$snmp_code = "iso.3.6.1.2.1.43.10.2.1.4.1.1" # OID for printed pages durring actuall sesion $printer_ip = "" # printer IP $url = "http://$($printer_ip)/DevMgmt/InternalPrintDyn.xml" # url where we need to POST body for start HEAD cleaning. $body = "cleaningVerificationPage" @@ -51,63 +55,71 @@ function log_store { $tmp | Set-Content $path_log } } +$logs += log -l_level 0 -l_message "Script Started" +$actual_time = get-date -Format "yyyy-MM-dd HH:mm:ss.ffff" -# Create last_run file where we can store last run data and document count. +# check if hour of running isnt restricted by $running_time_start and $running_time_stop +if (($actual_time.hour -lt $running_time_start) -and ($actual_time.hour -gt $running_time_stop)) { + $logs += log -l_level 0 -l_message "Hours between $running_time_start and $running_time_stop are restricted. EXIT" + log_store + exit +} + +# check / create last run control file if (-not(Test-Path $path_last_run)) { $logs += log -l_level 1 -l_message "$($path_last_run) Does not exists - CREATING." $last_run = [PSCustomObject]@{ date = Get-Date -Format "yyyy-MM-dd HH:mm:ss.ffff" - p_count = $null + p_count = (snmpget -v $snmp_ver -c $snmp_community $printer_ip $snmp_code).Split(" ")[-1] } - $logs += log -l_level 0 -l_message "Actual Date Stored in $($path_last_run)" + $logs += log -l_level 0 -l_message "Actual date and printed documents count are stored in $($path_last_run)" $last_run | Export-Csv -Path $path_last_run -NoTypeInformation # Get last run date and amount of printed documents } else { $last_run = Import-Csv $path_last_run - $logs += log -l_level 0 -l_message "Last Run Date: $($last_run.date)" + $logs += log -l_level 0 -l_message "Last Run Date: $($last_run.date), Last printed documents count: $($last_run.p_count)" } -# If the current date is earlier than the retrieved date, we proceed further -$actual_run = Get-Date -$difference = [DateTime]$actual_run - [DateTime]$last_run.Date | Select-Object TotalSeconds -ExpandProperty TotalSeconds -if ($difference -gt $days) { +# Get actuall amount of printed pages +try { + $actual_p_count = (snmpget -v $snmp_ver -c $snmp_community $printer_ip $snmp_code).Split(" ")[-1] +} catch { + $logs += log -l_level 2 -l_message "Getting printed page count got ERROR. EXIT" + log_store + exit +} +# Check if printed page count changed +if ($actual_p_count -gt $last_run.p_count) { + $logs += log -l_level 0 -l_message "Printed page count changed from $($last_run.p_count) to $actual_p_count. Changing last run time from $($last_run.date) to $actual_time" + $last_run.p_count = $actual_p_count + $last_run.date = $actual_time + $last_run | Export-Csv -Path $path_last_run -NoTypeInformation + log_store + exit +} + +# Check if between $last_run.date and $actual_time passed $days_between +$difference = [DateTime]$actual_time - [DateTime]$last_run.Date | Select-Object TotalSeconds -ExpandProperty TotalSeconds +if ($difference -gt $days_between) { + # start head cleaning try { - $tmp_01 = (snmpget -v $snmp_ver -c $snmp_community $printer_ip $snmp_code_01).Split(" ")[-1] - $tmp_02 = (snmpget -v $snmp_ver -c $snmp_community $printer_ip $snmp_code_02).Split(" ")[-1] - $tmp = [int]$tmp_01 + [int]$tmp_02 - [int]$snmp_code_n - $logs += log -l_level 0 -l_message "SNMP recived $($last_run[0].p_count) | CONTINUE to clean head" - # If the number of printed documents is greater than or equal to the number of documents since the last run: - try { - if ($tmp -ge $last_run[0].p_count) { - $last_run[0].p_count = $tmp - $tmp = Invoke-RestMethod -Method Post -Body $body -Headers $headers -Uri $url - $logs += log -l_level 0 -l_message "HEAD CLEANED $($last_run[0].p_count)" - $last_run | Export-Csv -Path $path_last_run -NoTypeInformation - $logs += log -l_level 0 -l_message "Head Cleaned | EXIT" - log_store - exit - } else { - $logs += log -l_level 0 -l_message "Something was printed in meantime. | EXIT" - $last_run | Export-Csv -Path $path_last_run -NoTypeInformation - log_store - exit - } - } catch { - $logs += log -l_level 2 -l_message "Cannot Clean Head: $($Error[0].Exception) | Printed: $($last_run[0].p_count) | CLOSE" + $tmp = Invoke-RestMethod -Method Post -Body $body -Headers $headers -Uri $url + $logs += log -l_level 0 -l_message "Head Cleaning Started" + } catch { + $logs += log -l_level 2 -l_message "Error durring connecting to printer." log_store exit - } - } catch { - $logs += log -l_level 2 -l_message "snmpget got error: $($Error[0].Exception) | Printed: $($last_run[0].p_count) | CLOSE" - $logs += log_store - log_store - exit - } -} else { - $logs += log -l_level 0 -l_message "Actual Date is less than $($days) seconds since $($last_run[0].Date) = $difference | Printed: $($last_run[0].p_count) | CLOSE" - log_store - Exit + } } -log_store -# Poprawić logikę. Dodać drukowanie strony tylko za dnia. \ No newline at end of file +# store actuall amount of printed pages and actuall date +$actual_p_count = (snmpget -v $snmp_ver -c $snmp_community $printer_ip $snmp_code).Split(" ")[-1] +$actual_time = get-date -Format "yyyy-MM-dd HH:mm:ss.ffff" +$last_run.p_count = $actual_p_count +$last_run.date = $actual_time + +$last_run | Export-Csv -Path $path_last_run -NoTypeInformation + +$logs += log -l_level 0 -l_message "Head Cleaning Stopped. Actual date: $actual_time. Actuall printed page count: $actual_p_count" +log_store +exit \ No newline at end of file