-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_printer_data.php
132 lines (110 loc) · 4.09 KB
/
get_printer_data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
$printers = array(
array(
'printerName' => 'printer1',
'url' => 'http://printer1.example.com/api/',
'apiKey' => 'apikey1',
),
array(
'printerName' => 'printer2',
'url' => 'http://printer2.example.com/api/',
'apiKey' => 'apikey2',
),
array(
'printerName' => 'printer3',
'url' => 'http://printer3.example.com/api/',
'apiKey' => 'apikey3',
),
);
$cacheFile = '/tmp/printer_data_cache.json'; // Set the cache file path
// Function to format time in hours and minutes
function formatTime($time) {
$hours = floor($time / 3600);
$minutes = floor(($time % 3600) / 60);
if ($hours == 0) {
return "${minutes}mins";
} else {
return "${hours}hrs, ${minutes}mins";
}
}
function fetchPrinterData() {
global $printers;
$printerData = array();
// Loop through each printer
foreach ($printers as $printer) {
$printerName = $printer['printerName'];
$printerUrl = $printer['url'];
$printerApiKey = $printer['apiKey'];
// Make a request to get the current job information
$urlJob = $printerUrl . 'job';
$chJob = curl_init();
curl_setopt($chJob, CURLOPT_URL, $urlJob);
curl_setopt($chJob, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chJob, CURLOPT_HTTPHEADER, array('X-Api-Key: ' . $printerApiKey));
$responseJob = curl_exec($chJob);
curl_close($chJob);
// Make a request to get the server settings
$urlSettings = $printerUrl . 'settings';
$chSettings = curl_init();
curl_setopt($chSettings, CURLOPT_URL, $urlSettings);
curl_setopt($chSettings, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chSettings, CURLOPT_HTTPHEADER, array('X-Api-Key: ' . $printerApiKey));
$responseSettings = curl_exec($chSettings);
curl_close($chSettings);
// Parse the job information response JSON
$job = json_decode($responseJob, true);
// Parse the server settings response JSON
$settings = json_decode($responseSettings, true);
// Determine the state and set the color class accordingly
$state = isset($job['state']) ? strtolower($job['state']) : 'offline';
$colorClass = $state === 'operational' ? 'ready' : ($state === 'printing' ? 'printing' : 'offline');
// Assemble printer data
if ($state === 'offline') {
$printerData[] = array(
'name' => $printer['printerName'],
'status' => 'Offline',
'progress' => '',
'elapsed' => '',
'left' => '',
'colorClass' => $colorClass,
);
} else {
$printerData[] = array(
'name' => $settings['appearance']['name'],
'status' => ($state === 'offline') ? 'Offline' : ($state === 'operational' ? 'Ready' : $job['state']),
'progress' => (isset($job['progress']['printTime']) && isset($job['progress']['printTimeLeft'])) ? intval(($job['progress']['printTime'] / ($job['progress']['printTime'] + $job['progress']['printTimeLeft'])) * 100) : '',
'elapsed' => (isset($job['progress']['printTime'])) ? formatTime($job['progress']['printTime']) : '',
'left' => (isset($job['progress']['printTimeLeft'])) ? formatTime($job['progress']['printTimeLeft']) : '',
'colorClass' => $colorClass,
);
}
}
return $printerData;
}
function getCachedData() {
global $cacheFile;
if (file_exists($cacheFile)) {
$cacheTime = filemtime($cacheFile);
$currentTime = time();
if ($currentTime - $cacheTime < 60) { // 60 seconds = 1 minute
return file_get_contents($cacheFile);
}
}
return false; // Cache doesn't exist or is outdated
}
function saveDataToCache($data) {
global $cacheFile;
file_put_contents($cacheFile, $data);
}
$cachedData = getCachedData();
if ($cachedData) {
// If cached data is available and not expired, use it
header('Content-Type: application/json');
echo $cachedData;
} else {
$printerData = fetchPrinterData();
saveDataToCache(json_encode($printerData));
header('Content-Type: application/json');
echo json_encode($printerData);
}
?>