Browse Source

upload register config

v2
gudae01 1 year ago
parent
commit
29df90d2f7
  1. 10
      src/main/java/org/mobidgim/mobidigimproject/controller/Controller.java
  2. 14
      src/main/java/org/mobidgim/mobidigimproject/service/SettingService.java
  3. 83
      src/main/resources/static/index.html

10
src/main/java/org/mobidgim/mobidigimproject/controller/Controller.java

@ -9,6 +9,9 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RestController
@RequestMapping("/setting")
@ -44,4 +47,11 @@ public class Controller {
var entity = settingService.readFileWithRegister();
return Api.OK(entity);
}
@PostMapping("/register-upload")
public Api<Object> toUpload(@RequestBody RegisterDTO request) throws IOException {
boolean success = settingService.registerUpload(request);
return success ? Api.OK("succes upload") : Api.ERROR("false upload");
}
}

14
src/main/java/org/mobidgim/mobidigimproject/service/SettingService.java

@ -17,6 +17,7 @@ import org.springframework.stereotype.Service;
import javax.swing.text.html.parser.Parser;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
@ -100,4 +101,17 @@ public class SettingService {
return registerDTO;
}
public boolean registerUpload(RegisterDTO request) throws IOException{
try{
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request);
Files.write(Paths.get("./temp/registerConfig.json"),json.getBytes(StandardCharsets.UTF_8));
return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
}

83
src/main/resources/static/index.html

@ -450,6 +450,7 @@
<div class="config-actions">
<button id="loadProtocolBtn">Load Protocol File</button>
<button id="saveProtocolBtn">Save to Protocol</button>
<button id="uploadProtocolBtn">Upload to Device</button>
</div>
</section>
@ -531,6 +532,7 @@
<div id="modbus-bottom-actions">
<button id="loadModbusBtn">Load Protocol File</button>
<button id="saveModbusBtn">Save Protocol File</button>
<button id="uploadModbusBtn">Upload to Device</button>
</div>
</section>
@ -604,6 +606,7 @@
<div id="opcUa-bottom-actions">
<button id="loadOpcUaFileBtn">Load Protocol File</button>
<button id="saveOpcUaFileBtn">Save to Protocol</button>
<button id="uploadOpcuaFileBtn">Upload to Device</button>
</div>
</section>
@ -670,6 +673,7 @@
<div id="can-bottom-actions">
<button id="loadCanBtn">Load Protocol File</button>
<button id="saveCanBtn">Save Protocol File</button>
<button id="uploadCanBtn">Upload to Device</button>
</div>
</section>
@ -1704,6 +1708,85 @@
}
});
});
document.getElementById('uploadProtocolBtn').addEventListener('click', () => {
fetch('http://localhost:8080/setting/register-upload', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(collectConfigData())
})
.then(res => res.json())
.then(data => {
// result_code가 200이면 성공
if (data.result && data.result.result_code === 200) {
alert('하드웨어 업로드 성공!');
} else {
alert('하드웨어 업로드 실패: ' + (data.result?.result_message || data.body || ''));
}
})
.catch(err => {
alert('통신 오류: ' + err.message);
});
});
document.getElementById('uploadModbusBtn').addEventListener('click', () => {
fetch('http://localhost:8080/setting/register-upload', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(collectConfigData())
})
.then(res => res.json())
.then(data => {
// result_code가 200이면 성공
if (data.result && data.result.result_code === 200) {
alert('하드웨어 업로드 성공!');
} else {
alert('하드웨어 업로드 실패: ' + (data.result?.result_message || data.body || ''));
}
})
.catch(err => {
alert('통신 오류: ' + err.message);
});
});
document.getElementById('uploadOpcuaFileBtn').addEventListener('click', () => {
fetch('http://localhost:8080/setting/register-upload', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(collectConfigData())
})
.then(res => res.json())
.then(data => {
// result_code가 200이면 성공
if (data.result && data.result.result_code === 200) {
alert('하드웨어 업로드 성공!');
} else {
alert('하드웨어 업로드 실패: ' + (data.result?.result_message || data.body || ''));
}
})
.catch(err => {
alert('통신 오류: ' + err.message);
});
});
document.getElementById('uploadCanBtn').addEventListener('click', () => {
fetch('http://localhost:8080/setting/register-upload', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(collectConfigData())
})
.then(res => res.json())
.then(data => {
// result_code가 200이면 성공
if (data.result && data.result.result_code === 200) {
alert('하드웨어 업로드 성공!');
} else {
alert('하드웨어 업로드 실패: ' + (data.result?.result_message || data.body || ''));
}
})
.catch(err => {
alert('통신 오류: ' + err.message);
});
});
</script>
</body>
</html>
Loading…
Cancel
Save