app.js 4.12 KB
// подключение express
const express = require("express");
// создаем объект приложения
const cors = require('cors')
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", cors(), (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("/getData", (req, res) => {    
    let terminal_sn = state.terminal_sn;
    let data = {
        "pult_sn" : terminal_sn,
        "serv_ip" : "unknown",
        "serv_port" : "unknown"
    };
    var msg = JSON.stringify(data, 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);