<%
' Background cache refresh endpoint
' Called asynchronously to update cache without blocking users
' DO NOT call this directly in browser - it's for internal async calls only

Response.Buffer = True
Response.ContentType = "text/plain"

Dim hostIP, cacheKey
hostIP = Request.QueryString("ip")

' Validate IP parameter
If hostIP = "" Then
    Response.Write("Error: No IP specified")
    Response.End
End If

' Include Zabbix functions
%>
<!--#include file="./includes/zabbix.asp"-->
<%

cacheKey = "zabbix_" & hostIP

' Fetch fresh data from Zabbix
Dim zabbixConnected, pingStatus, tonerJSON, resultData(2)

On Error Resume Next
zabbixConnected = ZabbixLogin()

If zabbixConnected = "1" Then
    pingStatus = GetPrinterPingStatus(hostIP)
    tonerJSON = GetPrinterTonerLevels(hostIP)
Else
    pingStatus = "-1"
    tonerJSON = ""
End If

If Err.Number <> 0 Then
    Response.Write("Error: " & Err.Description)
    Application.Lock
    Application(cacheKey & "_refreshing") = "false"
    Application.Unlock
    Response.End
End If
On Error Goto 0

' Update cache with fresh data
resultData(0) = zabbixConnected
resultData(1) = pingStatus
resultData(2) = tonerJSON

Application.Lock
Application(cacheKey) = resultData
Application(cacheKey & "_time") = Now()
Application(cacheKey & "_refreshing") = "false"
Application.Unlock

Response.Write("OK: Cache updated for " & hostIP & " at " & Now())
%>
