新聞資訊
在這篇文章中,我們將解釋如何配置打印機以及如何使用 PHP 構(gòu)建偵聽器和錯誤查看器頁面,并在后面使用 MySQL 數(shù)據(jù)庫來存儲警報。
所有 Link-OS 打印機都支持 HTTP-POST 協(xié)議,以將消息從打印機發(fā)送到網(wǎng)絡(luò)服務(wù)器。這些消息包含有關(guān)打印機狀態(tài)和可能的錯誤狀態(tài)的信息,因此,如果使用正確,此功能可用于監(jiān)視一組打印機。
先決條件
LinkOS 打印機
使用 PHP 和 MySQL 的 Web 服務(wù)器
基本的 PHP 和 MySQL 知識
步驟1
我們首先配置打印機以將消息發(fā)送到 Web 服務(wù)器。
支持的消息類型如下:
所有消息
缺紙
色帶用完
頭打開
色帶輸入
切刀卡住
打印機暫停
媒體低
色帶低位
電量不足
無效頭
國家/地區(qū)代碼錯誤
電池缺失
介質(zhì)盒
介質(zhì)盒加載失敗
介質(zhì)盒彈出失敗
介質(zhì)盒強制彈出
為簡單起見,本博客將使用接收所有消息的選項,但您可以縮小列表范圍并僅選擇您真正需要的消息。
為了配置打印機,您需要向其 發(fā)送以下命令
! U1 setvar "alerts.configured" "COLD START,SNMP,Y,N,255.255.255.255,162,N"
! U1 setvar "weblink.restore_defaults" ""
! U1 setvar "alerts.add" "ALL MESSAGES,HTTP-POST,Y,Y,http://www.example.com/postlistener.php,0,N,"
! U1 setvar "device.reset" ""
前 2 個命令僅需要確保打印機設(shè)置為我們將用于此解決方案的設(shè)置的默認配置,而第三個命令是啟用警報的實際配置命令,最后一個命令只是重新啟動打印機,以便它可以應(yīng)用更改。
例如,如果您只想接收“缺紙”和“色帶用完”錯誤的警報,則必須發(fā)送以下命令,每個命令對應(yīng)一種類型的警報
! U1 setvar "alerts.add" "PAPER OUT,HTTP-POST,Y,Y,http://www.example.com/postlistener.php,0,N,"
! U1 setvar "alerts.add" "RIBBON OUT,HTTP-POST,Y,Y,http://www.example.com/postlistener.php,0,N,"
打印機重新啟動后,它將開始發(fā)送警報。
步驟 2
假設(shè)您有一個支持 PHP 和 MySQL 的網(wǎng)絡(luò)服務(wù)器,您應(yīng)該創(chuàng)建(或已經(jīng)擁有)一個管理數(shù)據(jù)庫連接的文件,類似于以下
conn.php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "http-post";
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_query($conn,"set names 'utf8'");因此,您需要創(chuàng)建一個名為“http-post”的數(shù)據(jù)庫并導入以下空表轉(zhuǎn)儲,該表已包含用于存儲警報的所有必需字段
-- phpMyAdmin SQL Dump
-- version 4.9.7
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `http-post`
--
-- --------------------------------------------------------
--
-- Structure of the table `logs`
--
CREATE TABLE `logs` (
`id` int(100) NOT NULL,
`sn` varchar(20) NOT NULL,
`alerttype` varchar(50) NOT NULL,
`alertmessage` varchar(50) NOT NULL,
`date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Indexes for the table `logs`
--
ALTER TABLE `logs`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for the table `logs`
--
ALTER TABLE `logs`
MODIFY `id` int(100) NOT NULL AUTO_INCREMENT;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;步驟3
一旦配置了打印機,數(shù)據(jù)庫就準備好了,并且我們已經(jīng)配置了與它的連接,我們需要構(gòu)建監(jiān)聽器頁面。
查看頁面中的評論以了解更多詳細信息。
postlistener.php
// get the alert message from the POST message
$alertMsg = urldecode($_POST["alertMsg"]);
// get the printer serial number
$sn = urldecode($_POST["uniqueId"]);
// since all the messages are in the format "type: message"
// this command splits the type of message and the message content into 2 different variables
list($alerttype, $alertmessage) = explode (": ", $alertMsg);
// including the PHP file which manages the database connection
include "conn.php";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// editing the messages into lower case
$alerttype = ucfirst(strtolower($alerttype));
$alertmessage = ucfirst(strtolower($alertmessage));
// query to insert the message into the database
$stmt = $conn->prepare("INSERT INTO logs (sn, alerttype, alertmessage) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $sn, $alerttype, $alertmessage);
$stmt->execute();
$stmt->close();
$conn->close();以下文件只是從數(shù)據(jù)庫中提取數(shù)據(jù)并創(chuàng)建一個表來顯示它們
postview.php
// creating a table to show the results
echo “<table border='1' width='75%'>
<th>Date</th>
<th>Serial number</th>
<th>Alert type</th>
<th>Alert message</th>”;
// including the PHP file which manages the database connection
include "conn.php";
// querying the database to show everything in the table called "logs"
$query = "SELECT * FROM logs";
// fetching the result from the database into a multidimensional array
$result = mysqli_query($conn,$query);
// fetching the array into single rows and reiterating this operation to show the data
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["date"] . "</td><td>" . $row["sn"] . "</td><td>" . $row["alerttype"] . "</td><td>" . $row["alertmessage"] . "</td>";
echo "</tr>";
}