-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.js
176 lines (143 loc) · 4.03 KB
/
route.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var express = require('express');
const bodyParser = require("body-parser");
var router = express.Router();
var db = require('./dbclient');
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }))
var evt = require('events');
var fs = require('fs')
const main = require('./parsing');
const request = require('./request');
const exportDB = require('./exportDB');
const updateStatus = require('./orderStatus');
// Запуск работы обработки заказа
function readConfig(){
config = JSON.parse(fs.readFileSync('config.json','utf8'));
return config;
}
router.get('/config',(req,res)=>{
temp_config = readConfig();
temp_config.password = ''
temp_config.user = ''
ans = {
config: temp_config,
status: "ОК"
}
res.send(ans);
});
function cheackFilds(fields, obj){
ans = true;
if(obj==undefined)
return false
for(var i=0;i<fields.length;i++){
if(!(fields[i] in obj)){
ans=false;
break;
}
}
return ans;
}
router.post('/saveConfig',(req,res)=>{
console.log('saveConfig',req.body);
fileds = ['host','port','user','password','period'];
if (!cheackFilds(fileds,req.body)){
var ans = { status: 'error',
commet: 'Настройки конфигурации введены неверно'}
res.send(ans);
}else{
ans = {status:'ОК'};
res.send(ans);
data = JSON.stringify(req.body);
fs.writeFileSync('config.json',data);
}
})
//Обновление остатков
router.post('/update',(req,res) => {
console.log(res.body);
main.updateProduct();
// request.run_request(req.body);
ans = { status:'ОК'}
res.send(ans);
})
router.post('/autoUpdate',(req,res) => {
ans = { status:'ОК'};
config = readConfig();
main.autoRun(config.period);
res.send(ans);
})
router.post('/stop', (req,res) => {
ans = { status:'OK'};
var emt = new evt.EventEmitter();
emt.emit('stopUpdate');
console.log('stop');
clearTimeout(main.timer);
main.job.cancel();
// main.logStream.write(datatime.create().format('Y-m-d H:M:S'))
//main.logStream.write('Процедура автообновления остановлена \n')
main.online = false;
})
router.post('/sendOrders', (req, res) => {
db.selectOrder((orders)=>{
ans = { status: 'ОК',
orders: orders
}
res.send(ans);
})
})
//Отправка заказа на ftp сервер
router.post('/updateOrder', (req, res) => {
var ans = '123';
res.send(ans);
request.run_request(req.body);
})
router.get('/getStatus', (req,res) => {
console.log(main.online)
if (main.online){
ans = { status:'OK',
serviceStatus: "Включен"};
}else{
ans = { status:'OK',
serviceStatus: "Отключен"};
}
res.send(ans);
})
router.get('/getLogfile', (req,res) => {
fs.readFile('log_file.txt',{encoding:'utf-8'},(err,data) =>{
if (err) throw err;
var arr = data.toString().split('\n');
ans = { status:'OK',
array : arr,
};
res.send(ans);
})
})
router.get('/getLogfileRequest', (req,res) => {
fs.readFile('log_file_request.txt',{encoding:'utf-8'},(err,data) =>{
if (err) throw err;
var arr = data.toString().split('\n');
ans = { status:'OK',
array : arr,
};
res.send(ans);
})
})
router.get('/configform', (req,res) => {
fs.readFile('configform.html','utf8',(err,data) =>{
res.send(data);
})
})
router.get('/exportDb', (req,res) => {
//main.exportDB().then(
exportDB.exportDB().then( () => {
var ans = { status: "OK"};
res.send(ans);
},
error => {
var ans = { status: "error", commet: error };
console.log(ans)
console.log(error)
res.send(ans);
}
)
})
exports.route=router;