const Koa = require("koa");
|
const app = new Koa();
|
const views = require("koa-views");
|
const router = require('koa-router');
|
const json = require("koa-json");
|
const onerror = require("koa-onerror");
|
const bodyparser = require("koa-bodyparser");
|
const logger = require("koa-logger");
|
const koajwt = require("koa-jwt");
|
const cors = require("koa2-cors");
|
const koaBody = require("koa-body");
|
const user = require("./routes/user");
|
const base = require("./routes/base");
|
const apicloud = require("./routes/apicloud");
|
const apiSDK = require("./routes/apiSDK");
|
const NewsOptionNoToken = require("./routes/NewsOptionNoToken");
|
const Dataapi = require("./routes/dataapi");
|
const schedule_ = require("./models/taskSchedule.js");
|
|
|
|
var http = require("http");
|
var fs = require("fs");
|
var paths = require("path");
|
var querystring = require("querystring");
|
const mysql = require("./utils/mysql_db");
|
|
// const parse = require("await-busboy");
|
|
// const parts = parse(ctx);
|
// let part, stream, tmp_name, file_path, filename;
|
// while ((part = await parts)) {
|
// if (part.length) {
|
// // 此处解析到form的fields
|
// console.log({ key: part[0], value: part[1] });
|
// } else {
|
// // 此处解析到文件并以可读流形式返回,通过nodejs官方API存储
|
// if (
|
// ActionType === "uploadimage" &&
|
// img_type.indexOf(path.extname(part.filename)) >= 0
|
// ) {
|
// filename = "pic_" + new Date().getTime() + "_" + part.filename;
|
// file_path = path.join(img_path, filename);
|
// } else if (ActionType === "uploadfile") {
|
// filename = "file_" + new Date().getTime() + "_" + part.filename;
|
// file_path = path.join(files_path, filename);
|
// }
|
// stream = fs.createWriteStream(path.join(static_path, file_path));
|
// part.pipe(stream);
|
// tmp_name = part.filename;
|
// }
|
// }
|
|
// var createError = require("http-errors");
|
// var express = require('express');
|
// var ueditor = require("ueditor");
|
// var path = require('path')
|
|
global.equState =[
|
{ monitorpointId: 8, status: 1, monitorpointName: '嘉定融媒体143' },
|
{ monitorpointId: 9, status: 1, monitorpointName: '闵行融媒体144' },
|
{ monitorpointId: 10, status: 1, monitorpointName: '青浦融媒体145' },
|
{ monitorpointId: 11, status: 1, monitorpointName: '宝山融媒体146' },
|
{ monitorpointId: 12, status: 1, monitorpointName: '宝山有线147' },
|
{ monitorpointId: 13, status: 1, monitorpointName: '嘉定有线149' },
|
{ monitorpointId: 14, status: 1, monitorpointName: '松江融媒体151' },
|
{ monitorpointId: 15, status: 1, monitorpointName: '松江有线141' },
|
{ monitorpointId: 16, status: 1, monitorpointName: '闵行有线148' },
|
{ monitorpointId: 17, status: 1, monitorpointName: '金山有线150' },
|
{ monitorpointId: 18, status: 1, monitorpointName: '奉贤融媒体152' },
|
{ monitorpointId: 19, status: 1, monitorpointName: '青浦有线153' },
|
{ monitorpointId: 20, status: 1, monitorpointName: '临港有线154' },
|
{ monitorpointId: 21, status: 1, monitorpointName: '艺海大厦155' },
|
{ monitorpointId: 22, status: 1, monitorpointName: '金山融媒体156' },
|
{ monitorpointId: 23, status: 1, monitorpointName: '浦东有线157' },
|
{ monitorpointId: 24, status: 1, monitorpointName: '奉贤有线158' },
|
{ monitorpointId: 25, status: 1, monitorpointName: '崇明融媒体160' },
|
{ monitorpointId: 26, status: 1, monitorpointName: '长兴岛有线164' },
|
{ monitorpointId: 27, status: 1, monitorpointName: '崇明有线163' },
|
{ monitorpointId: 28, status: 1, monitorpointName: '广播大厦162' },
|
{ monitorpointId: 29, status: 1, monitorpointName: '题桥AES168' },
|
{ monitorpointId: 30, status: 1, monitorpointName: '玖富科技园126' },
|
{ monitorpointId: 31, status: 1, monitorpointName: '青浦白鹤有线123' },
|
{ monitorpointId: 32, status: 1, monitorpointName: '祝桥有线161' },
|
{ monitorpointId: 33, status: 1, monitorpointName: '嘉定外冈167' },
|
{ monitorpointId: 34, status: 1, monitorpointName: '台湾清晰1557' }
|
]
|
|
var apps = http.createServer();
|
apps.on("request", function (req, res) {
|
console.log("+++++++++");
|
if (
|
(req.url === "/warnning/diskused" && req.method === "POST") ||
|
(req.url === "/warnning/memoryused" && req.method === "POST")
|
) {
|
var data = "";
|
req.on("data", function (chunk) {
|
data += chunk;
|
});
|
req.on("end", function () {
|
data = decodeURI(data);
|
});
|
}
|
console.log("+++++++++");
|
});
|
|
apps.listen(4444, function () {
|
console.log("------------");
|
console.log("ok");
|
console.log("------------");
|
});
|
|
app.use(
|
koaBody({
|
multipart: true,
|
// formidable: {
|
// maxFileSize: 200*1024*1024 // 设置上传文件大小最大限制,默认2M
|
// }
|
})
|
);
|
// error handler
|
onerror(app);
|
|
// middlewares
|
app.use(
|
bodyparser({
|
enableTypes: ["json", "form", "text"],
|
})
|
);
|
app.use(json());
|
app.use(logger());
|
app.use(require("koa-static")(__dirname + "/public"));
|
app.use(
|
views(__dirname + "/views", {
|
extension: "pug",
|
})
|
);
|
|
// logger
|
app.use(async (ctx, next) => {
|
const start = new Date();
|
await next();
|
const ms = new Date() - start;
|
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
|
});
|
|
// jwt
|
app.use(
|
koajwt({
|
secret: "FM_Monitor",
|
passthrough: true,
|
}).unless({
|
path: [
|
/\/user\/login/,
|
/\/user\/info/,
|
/\/user\/ifPhoneExist/,
|
/\/user\/withoutToken/,
|
/\/user\/ReportScanResult/,
|
/\/user\/ReportRecordResult/,
|
], //除了login,info,其他为测试数据,用完删除
|
})
|
);
|
|
// cors
|
app.use(
|
cors({
|
origin: function (ctx) {
|
const whiteList = [
|
"http://192.168.119.100:8080",
|
"http://192.168.119.100",
|
"https://192.168.119.100:8443",
|
"http://localhost:8080",
|
"http://localhost",
|
"http://localhost:9380",
|
"https://localhost:8443",
|
"http://127.0.0.1",
|
"http://127.0.0.1:8080",
|
"https://127.0.0.1:8443",
|
"http://resgis.sh.cn",
|
];
|
if (ctx.url === "/user/") {
|
return "*"; // 允许来自所有域名请求
|
}
|
if (whiteList.includes(ctx.header.origin)) {
|
return ctx.header.origin;
|
}
|
},
|
exposeHeaders: ["WWW-Authenticate", "Server-Authorization"],
|
maxAge: 5,
|
credentials: true,
|
allowMethods: ["GET", "POST", "DELETE", "PUT"],
|
allowHeaders: ["Content-Type", "Authorization", "Accept"],
|
})
|
);
|
|
// logger
|
const logsUtil = require("./utils/logs.js");
|
app.use(async (ctx, next) => {
|
// console.log("------------------------------");
|
// console.log(ctx);
|
// console.log("------------------------------");
|
const start = new Date(); // 响应开始时间
|
let intervals; // 响应间隔时间
|
try {
|
await next();
|
intervals = new Date() - start;
|
// logsUtil.logResponse(ctx, intervals); //记录响应日志
|
} catch (error) {
|
intervals = new Date() - start;
|
logsUtil.logError(ctx, error, intervals); //记录异常日志
|
}
|
});
|
|
// function deviceChanges() {
|
// var p = new Promise(function (resolve, reject) {
|
// resolve(this.getDeviceStatus());
|
// });
|
// return p;
|
// }
|
|
const server = require("http").Server(app.callback());
|
const io = require("socket.io")(server);
|
io.on("connection", (socket) => {
|
console.log("初始化成功!下面可以用socket绑定事件和触发事件了");
|
// socket.on("send", (data) => {
|
// console.log("客户端发送的内容:", data);
|
// socket.emit("getMsg", "我是返回的消息... ...");
|
// });
|
// socket.emit("getMsg",msg);
|
// socket.emit("getMsg", "我是初始化3s后的返回消息... ...");
|
});
|
setInterval(() => {
|
schedule_.getDeviceStatus().then(res => {
|
let changeArr = []
|
res.forEach(item => {
|
global.equState.forEach(item1 => {
|
if (item.monitorpointId == item1.monitorpointId) {
|
if (item.status === item1.status) {
|
|
}
|
else {
|
changeArr.push(item)
|
}
|
}
|
})
|
})
|
global.equState = res
|
console.log(changeArr)
|
if (changeArr.length > 0) {
|
io.sockets.emit('getMsg', JSON.stringify(changeArr));
|
}
|
})
|
console.log("123")
|
}, 60000);
|
|
server.listen(3030);
|
|
|
// schedule_.job_();
|
|
// const parse = require('await-busboy')
|
|
// //使用模块
|
// app.use("/ueditor/ue", ueditor(path.join(__dirname, 'public'), function (req, res, next) {
|
// // ueditor 客户发起上传图片请求
|
// if (req.query.action === 'uploadimage') {
|
// var foo = req.ueditor;
|
|
// var imgname = req.ueditor.filename;
|
|
// var img_url = '/Users/xujinggan/Desktop/stu/';
|
// res.ue_up(img_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
|
// res.setHeader('Content-Type', 'text/html');//IE8下载需要设置返回头尾text/html 不然json返回文件会被直接下载打开
|
// }
|
// // 客户端发起图片列表请求
|
// else if (req.query.action === 'listimage') {
|
// var dir_url = '/images/ueditor/';
|
// res.ue_list(dir_url); // 客户端会列出 dir_url 目录下的所有图片
|
// }
|
// // 客户端发起其它请求
|
// else {
|
// // console.log('config.json')
|
// res.setHeader('Content-Type', 'application/json');
|
// res.redirect('/ueditor/jsp/config.json');
|
// }
|
// }));
|
|
app.use(user.routes(), user.allowedMethods());
|
app.use(base.routes(), base.allowedMethods());
|
app.use(apicloud.routes(), apicloud.allowedMethods());
|
app.use(apiSDK.routes(), apiSDK.allowedMethods());
|
app.use(NewsOptionNoToken.routes(), NewsOptionNoToken.allowedMethods());
|
app.use(Dataapi.routes(), Dataapi.allowedMethods());
|
|
// error-handling
|
app.on("error", (err, ctx) => {
|
console.error("server error", err, ctx);
|
});
|
|
module.exports = app;
|