app.js
3.63 KB
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
133
134
135
136
137
138
139
// подключение express
const express = require("express");
// создаем объект приложения
const app = express();
// определяем обработчик для маршрута "/"
var g_sign = "debug_sign";
//var g_card_sn = "04c6a56abf2282";
var g_card_sn = "04c6a56abf2211";
var g_terminal_id = -997616665;
var g_nocard = false;
//
//g_sign = "1571396701:b93dbf3232b7e85072f8a18dffc7043d";
//g_card_sn = "70eb011a";
//g_terminal_id = 271665294;
let state = {
devices: [
{
availableCards: [
makeCard(g_card_sn),
],
id: 31549,
model: "YHY632A-1004"
}
],
terminal_sn: g_terminal_id
};
function makeCard(card_sn) {
return {
id: card_sn,
type: "ULTRA_LIGHT",
sign: g_sign
};
}
function makeDevice(cards) {
return {
availableCards: cards,
id: 31549,
model: "YHY632A-1004"
}
}
app.get("/getRfid", (req, res) => {
var msg = JSON.stringify(state, null, '\t');
if (req.query.callback !== undefined) {
msg = req.query.callback + "(" + msg + ");";
return res.type('application/javascript').send(msg);
}
res.type('application/json').send(msg);
});
// положить карту
app.get("/", (req, res) => {
res.status(200).send(`<ul>
<li>
<a href="/getRfid">Get Rfid status</a>
</li>
<br>
<li>
<button onclick='doCmd("/ctl/put/04c6a56abf2211") '>Положить 1</button>
<button onclick='doCmd("/ctl/put/AABBCCDD") '> Положить 2</button>
<button onclick='doCmd("/ctl/put/BBAACCDD") '> Положить 3</button>
</li>
<br>
<li>
<button onclick='doCmd("/ctl/put") '> Положить </button>
<button onclick='doCmd("/ctl/take") '> Поднять </button>
</li>
<br>
<li>
<button onclick='doCmd("/ctl/connect")'>connect device</button>
<button onclick='doCmd("/ctl/disconnect")'>disconnect device</button>
</li>
</ul>
<textarea id="statustext" name="Text1" cols="40" rows="40" style="width:100%" ></textarea>
<script language="javascript">
async function doCmd(name) {
await fetch(name, { method: "POST" });
await updateStatus();
}
async function updateStatus() {
const response = await fetch("/getRfid")
const result = await response.json();
document.getElementById('statustext').value= JSON.stringify(result, null, ' ');
}
setInterval(updateStatus, 1000);
updateStatus();
</script>
`);
});
// положить карту
app.post("/ctl/put", (req, res) => {
state.devices = [makeDevice([makeCard(g_card_sn)])];
res.status(200).send("ok");
});
// положить карту
app.post("/ctl/put/:sn", (req, res) => {
g_card_sn= req.params.sn;
state.devices = [makeDevice([makeCard(g_card_sn)])];
res.status(200).send("ok");
});
// снять карту
app.post("/ctl/take", (req, res) => {
state.devices = [makeDevice([])];
res.status(200).send("ok");
});
app.post("/ctl/disconnect", (req, res) => {
state.devices = []
res.status(200).send("ok");
});
app.post("/ctl/connect", (req, res) => {
state.devices = [makeDevice([])];
res.status(200).send("ok");
});
// начинаем прослушивать подключения на 3000 порту
app.listen(8888);
//setInterval( function() {
// g_nocard = !g_nocard;
// console.log("rfid change", g_nocard);
//}, 20000);