123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- const express = require('express');
- const robot = require('./robot')
- const {getRustConfig} = require('./config')
- const http = require("./utils/http");
- const file = require('./utils/file')
- const {logger, fileLogger} = require("./utils/logger");
- const {spawn, exec, fork, execSync} = require("child_process");
- const path = require("path");
- const {ka, tr} = require("date-fns/locale");
- const {getAppMap} = require("./robot");
- const crypto = require('crypto');
- const fs = require("fs");
- function init() {
- const app = express();
- const port = 3000;
-
- app.use(express.json());
- app.use((req, res, next) => {
- var k = "$8gBV!f&L@E"
- if ("timestamp" in req.headers) {
- var timestamp = req.headers.timestamp
- var md5 = req.headers.catilla
-
- const md5Str = crypto.createHash('md5').update(k + timestamp).digest('hex');
- logger.info(JSON.stringify(md5Str))
- if (md5 === md5Str) {
- next();
- } else {
- res.status(403).send('拒绝访问!');
- }
- } else {
- res.status(403).send('没有访问权限!');
- }
- });
-
- app.get('/', (req, res) => {
- res.send('is OK!');
- });
- app.get('/robotList', (req, res) => {
- var map = robot.getAppMap()
- var list = new Array();
- robot.appMap.forEach((value, key) => {
-
- list.push({
- id: value.id,
- nowBalance: value.nowBalance + "",
- posNum: value.posNum + "",
- messlist: value.messlist,
- threadStatus: value.threadStatus,
- errorMessage: value.errorMessage,
- threadStartTime: value.threadStartTime,
- status: value.status,
- restartStatus: value.restartStatus
- })
- })
- res.send({'code': 200, 'data': JSON.stringify(list), "message": "SUCCESS"});
- });
-
- app.get('/isOK', (req, res) => {
- res.send({'code': 200, 'data': true, "message": "SUCCESS"});
- });
- app.get('/logs', (req, res) => {
-
- const param = req.query;
- const n = param.n
- const id = param.id
- var port = -1;
- robot.appMap.forEach((value, key) => {
-
- if (value.id + "" === id + "") {
- port = value.port
- }
- })
- let result = [];
- if (port !== -1) {
- var logPath = "./logs" + port
- logger.info(`访问-日志相对目录:${logPath}`)
-
- const directoryPath = path.resolve(logPath);
- try {
- file.checkPathSync(directoryPath)
- file.getLastFile(logPath, 500, async (fileNameList, _) => {
-
- result = await file.readLastNLines(logPath, fileNameList, n);
-
- res.send({'code': 200, 'data': result, "message": "SUCCESS"});
- });
- } catch (e) {
- logger.info('获取日志异常了~~', e);
- res.send({'code': 200, 'data': [], "message": "SUCCESS"});
- }
- } else {
- res.send({'code': 200, 'data': result, "message": "SUCCESS"});
- }
- });
- app.post('/execute', (req, res) => {
-
- const param = req.body;
-
- (async (param) => {
-
- var executeType = param.executeType
- if (executeType === "RUN") {
- await robot.run(param)
- } else if (executeType === "STOP") {
- await robot.closeApp(param)
- } else if (executeType === "RESTART") {
- await robot.restartApp(param)
- }
- })(param);
-
- res.send({'code': 200, 'data': "null", "message": "SUCCESS"});
- })
-
- app.get('/searchPositions', (req, res) => {
- const param = req.query;
- logger.info(JSON.stringify(param));
- robot.searchPositions(param)
- res.send({'code': 200, 'data': true, "message": "SUCCESS"});
- })
-
- app.get('/predictorState', (req, res) => {
- const param = req.query;
- logger.info(JSON.stringify(param));
- var id = param.id;
- var port = -1;
- robot.appMap.forEach((value, key) => {
-
- if (value.id + "" === id + "") {
- port = value.port
- }
- })
- if (port !== -1) {
- logger.info('#####################predictorState:');
- var config = getRustConfig()
- var com = `curl -X GET -H "auth: 4L" -H "token: r7T$8gBV!f&L@E2+" "http://127.0.0.1:${port}/predictor_state"`;
- logger.info('#####################com:', com);
- execSync(com, (error, stdout, stderr) => {
- if (error) {
- logger.info(`#####################汇报:获取状态请求失败: ${error.message}`)
- res.send({'code': -1, 'data': null, "message": `获取状态请求失败`});
- }
- logger.info('#####################-d', stdout);
- logger.info('#####################-ok');
- res.send({'code': 200, 'data': stdout, "message": "SUCCESS"});
- });
- } else {
- res.send({'code': -1, 'data': null, "message": "机器人不存在"});
- }
- })
-
- app.listen(port, () => {
-
- });
- logger.info('--web 启动');
- }
- function robotNodeStatus() {
- var config = getRustConfig()
- http.request_post(`${config.reportedUrl}/report/beOnline`, {}, {...config.headers}).then((data) => {
-
- logger.info('#####################汇报:node 在线-上报成功!', data);
- }).catch((error) => {
- logger.error(`node 在线!-上报失败: ${error.message}`);
- });
- }
- function isOK(req, res) {
- res.send({'code': 200, 'data': "null", "message": "SUCCESS"});
- }
- module.exports = {
- init,
- robotNodeStatus
- };
|