root / HServer / 00.Server / 00.Program / routers / agent.js @ 41
이력 | 보기 | 이력해설 | 다운로드 (5.93 KB)
| 1 | 39 | HKM | var express = require('express'); |
|---|---|---|---|
| 2 | var mongoose = require('mongoose'); |
||
| 3 | var dbCenter = require('../db_modules/dbCenter'); |
||
| 4 | var mySocket = require('../socket.io_modules/mySocket'); |
||
| 5 | //#region [ database ]
|
||
| 6 | |||
| 7 | var agentSchema = mongoose.Schema({
|
||
| 8 | mac: String, // 맥 |
||
| 9 | ip: String, // 아이피 |
||
| 10 | state: String, // 상태 / play | down | ready |
||
| 11 | |||
| 12 | contents: String, // 컨텐츠 |
||
| 13 | connect: String,
|
||
| 14 | |||
| 15 | lastUpdateDate: String,
|
||
| 16 | files: [],
|
||
| 17 | updatafiles: [],
|
||
| 18 | deletefiles: [],
|
||
| 19 | |||
| 20 | starts: [],
|
||
| 21 | parameters: []
|
||
| 22 | }); |
||
| 23 | |||
| 24 | //var agentModel = mongoose.model('agent', agentSchema); // agent model 정의
|
||
| 25 | |||
| 26 | var agentContorl = {};
|
||
| 27 | |||
| 28 | agentContorl.createCollection = function(err){ |
||
| 29 | console.log('[Agent] createcollection 호출');
|
||
| 30 | if(err) {
|
||
| 31 | console.log('[Agent] createcollection err : ' + err);
|
||
| 32 | } else {
|
||
| 33 | console.log('[Agent] collection 생성 성공');
|
||
| 34 | } |
||
| 35 | }; |
||
| 36 | |||
| 37 | agentContorl.test = function(){ |
||
| 38 | console.log('agent test');
|
||
| 39 | } |
||
| 40 | |||
| 41 | //#endregion
|
||
| 42 | |||
| 43 | //#region [ variable ]
|
||
| 44 | var collectionName = 'agent'; |
||
| 45 | //#endregion
|
||
| 46 | |||
| 47 | //#region [ API ]
|
||
| 48 | |||
| 49 | const router = express.Router(); |
||
| 50 | |||
| 51 | // udp로 받은 신호를 반환해주는 post
|
||
| 52 | router.post('/',function(req, res){ |
||
| 53 | console.log('[agent] agent 등록 시도');
|
||
| 54 | |||
| 55 | dbCenter.findClient(collectionName, req.body.mac, function(err, result){
|
||
| 56 | if(err){
|
||
| 57 | console.log('[agent find] err : ' + err.toString());
|
||
| 58 | res.send({'result':null});
|
||
| 59 | res.end(); |
||
| 60 | return;
|
||
| 61 | } else {
|
||
| 62 | if(result){
|
||
| 63 | console.log('[agent find] 결과를 찾았습니다.');
|
||
| 64 | |||
| 65 | dbCenter.findAll('contents', function(err, docs){ |
||
| 66 | if(err){
|
||
| 67 | console.log('[agent][getAll] err : ' + err.toSring());
|
||
| 68 | |||
| 69 | } else {
|
||
| 70 | console.log('[agent][getAll] 전체 데이터 호출 성공');
|
||
| 71 | console.log(docs); |
||
| 72 | |||
| 73 | result.files = docs[0].files;
|
||
| 74 | |||
| 75 | console.log(result.files); |
||
| 76 | res.send({'result':result});
|
||
| 77 | res.end(); |
||
| 78 | } |
||
| 79 | }); |
||
| 80 | |||
| 81 | } else {
|
||
| 82 | console.log('[agent find] 결과가 없습니다. 새로운 것입니다.');
|
||
| 83 | dbCenter.add(collectionName, req.body, function(err){
|
||
| 84 | if(err){
|
||
| 85 | console.log('[agent add] err : ' + err.toString());
|
||
| 86 | res.send({'result':null});
|
||
| 87 | res.end(); |
||
| 88 | return;
|
||
| 89 | } else {
|
||
| 90 | console.log('[agent add] 성공');
|
||
| 91 | res.send({'result':req.body});
|
||
| 92 | res.end(); |
||
| 93 | } |
||
| 94 | }); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | }); |
||
| 98 | }); |
||
| 99 | |||
| 100 | // 전체 조회
|
||
| 101 | router.get('/', function(req, res){ |
||
| 102 | console.log('[agent] 전체조회 호출');
|
||
| 103 | |||
| 104 | dbCenter.findAll(collectionName, function(err, docs){
|
||
| 105 | if(err){
|
||
| 106 | console.log('[agent][getAll] err : ' + err.toSring());
|
||
| 107 | res.send({'result':'fail'});
|
||
| 108 | res.end(); |
||
| 109 | } else {
|
||
| 110 | console.log('[agent][getAll] 전체 데이터 호출 성공');
|
||
| 111 | res.send(docs); |
||
| 112 | res.end(); |
||
| 113 | } |
||
| 114 | }); |
||
| 115 | }); |
||
| 116 | |||
| 117 | // 특정 정보로 조회
|
||
| 118 | router.get('/:mac', function(req, res){ |
||
| 119 | console.log('[agent] get/:mac');
|
||
| 120 | }); |
||
| 121 | |||
| 122 | // 수정
|
||
| 123 | router.put('/:mac', function(req, res){ |
||
| 124 | console.log('[agent] put/:' + req.params.mac);
|
||
| 125 | |||
| 126 | dbCenter.findClient(collectionName, req.params.mac, function(err, result){
|
||
| 127 | if(err){
|
||
| 128 | console.log('[agent put] err : ' + err.toString());
|
||
| 129 | res.send({'result':'err'});
|
||
| 130 | res.end(); |
||
| 131 | } else {
|
||
| 132 | if(result){
|
||
| 133 | console.log('[agent put] 결과를 찾았습니다.');
|
||
| 134 | console.log('result : ' + JSON.stringify(result));
|
||
| 135 | |||
| 136 | console.log(req.body); |
||
| 137 | |||
| 138 | // 바뀔 데이터
|
||
| 139 | var setdata = {};
|
||
| 140 | |||
| 141 | setdata.contents = req.body.contents; |
||
| 142 | setdata.files = req.body.files; |
||
| 143 | |||
| 144 | setdata.starts = req.body.starts; |
||
| 145 | setdata.parameters = req.body.parameters; |
||
| 146 | setdata.lastUpdateDate = req.body.lastUpdateDate; |
||
| 147 | |||
| 148 | |||
| 149 | setdata.updateFiles = req.body.updataFiles; |
||
| 150 | setdata.deleteFiles = req.body.deleteFiles; |
||
| 151 | |||
| 152 | dbCenter.modifiy(collectionName, result, setdata, function(err, result){
|
||
| 153 | if(err){
|
||
| 154 | console.log('[agnet put] : err : ' + err.toString());
|
||
| 155 | res.send({'result':'fail'});
|
||
| 156 | res.end(); |
||
| 157 | } else {
|
||
| 158 | |||
| 159 | mySocket.modifyData(result); |
||
| 160 | |||
| 161 | res.send({'result':'success'});
|
||
| 162 | res.end(); |
||
| 163 | } |
||
| 164 | }); |
||
| 165 | |||
| 166 | } else {
|
||
| 167 | console.log('[agent put] 결과를 찾지못했습니다.');
|
||
| 168 | res.send({'result':'not found'});
|
||
| 169 | res.end(); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | }); |
||
| 173 | }); |
||
| 174 | |||
| 175 | // 삭제
|
||
| 176 | router.delete('/:mac', function(req, res){ |
||
| 177 | console.log('[agent delete/:mac');
|
||
| 178 | }); |
||
| 179 | |||
| 180 | router.post('/test', function(req, res){ |
||
| 181 | console.log('req : ' + req.body._ip );
|
||
| 182 | |||
| 183 | //var agentModel = mongoose.model('agent', agentSchema); // agent model 정의
|
||
| 184 | |||
| 185 | var agentModel ;
|
||
| 186 | |||
| 187 | dbCenter.findAll(agentModel, function(err, docs){
|
||
| 188 | if(err){
|
||
| 189 | console.log('[agent][getAll] err : ' + err.toString());
|
||
| 190 | res.send({'result':'fail'});
|
||
| 191 | res.end(); |
||
| 192 | } else {
|
||
| 193 | console.log('[agent][getAll] 전체 데이터 호출 성공');
|
||
| 194 | res.send(docs); |
||
| 195 | res.end(); |
||
| 196 | } |
||
| 197 | }); |
||
| 198 | }); |
||
| 199 | |||
| 200 | //#endregion
|
||
| 201 | |||
| 202 | exports.default = router;
|