|
|
@ -10,6 +10,7 @@ import org.mobidgim.mobidigimproject.model.register.enums.Protocol; |
|
|
import org.mobidgim.mobidigimproject.model.register.sebDTO.CanField; |
|
|
import org.mobidgim.mobidigimproject.model.register.sebDTO.CanField; |
|
|
import org.mobidgim.mobidigimproject.model.register.sebDTO.ModbusField; |
|
|
import org.mobidgim.mobidigimproject.model.register.sebDTO.ModbusField; |
|
|
import org.mobidgim.mobidigimproject.model.register.sebDTO.OpcUaField; |
|
|
import org.mobidgim.mobidigimproject.model.register.sebDTO.OpcUaField; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import javax.swing.text.html.parser.Parser; |
|
|
import javax.swing.text.html.parser.Parser; |
|
|
@ -25,8 +26,15 @@ import java.util.Map; |
|
|
@Service |
|
|
@Service |
|
|
public class SettingService { |
|
|
public class SettingService { |
|
|
|
|
|
|
|
|
|
|
|
private final ObjectMapper mapper; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
public SettingService(ObjectMapper mapper) { |
|
|
|
|
|
this.mapper = mapper; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void saveFileWithDevice(DeviceDTO request) throws IOException { |
|
|
public void saveFileWithDevice(DeviceDTO request) throws IOException { |
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
|
|
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); |
|
|
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); |
|
|
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request); |
|
|
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request); |
|
|
|
|
|
|
|
|
@ -49,40 +57,21 @@ public class SettingService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public RegisterDTO saveFileWithRegister(RegisterDTO request) throws IOException { |
|
|
public RegisterDTO saveFileWithRegister(RegisterDTO request) throws IOException { |
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
|
|
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> finalJsonMap = mapper.convertValue(request, Map.class); |
|
|
if (request.getProtocol() != Protocol.OPC_UA) { |
|
|
|
|
|
request.setOpcUaFields(null); |
|
|
finalJsonMap.remove("opc_ua_field"); |
|
|
|
|
|
finalJsonMap.remove("modbus_field"); |
|
|
|
|
|
finalJsonMap.remove("can_field"); |
|
|
|
|
|
|
|
|
|
|
|
if (request.getProtocol() != null) { |
|
|
|
|
|
Protocol protocol = request.getProtocol(); |
|
|
|
|
|
|
|
|
|
|
|
if (protocol == Protocol.OPC_UA){ |
|
|
|
|
|
List<OpcUaField> opcUaField = request.getOpcUaField(); |
|
|
|
|
|
if(opcUaField != null && !opcUaField.isEmpty()){ |
|
|
|
|
|
finalJsonMap.put("OPC_UA", opcUaField); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else if(protocol == Protocol.MODBUS){ |
|
|
|
|
|
List<ModbusField> modbusField = request.getModbusField(); |
|
|
|
|
|
if(modbusField != null && !modbusField.isEmpty()){ |
|
|
|
|
|
finalJsonMap.put("MODBUS", modbusField); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(request.getCanInput() == CanInput.On){ |
|
|
if (request.getProtocol() != Protocol.MODBUS) { |
|
|
List<CanField> canField = request.getCanField(); |
|
|
request.setModbusFields(null); |
|
|
if(canField != null && !canField.isEmpty()){ |
|
|
|
|
|
finalJsonMap.put("CAN", canField); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(finalJsonMap); |
|
|
if (request.getCanInput() != CanInput.On) { |
|
|
|
|
|
request.setCanFields(null); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request); |
|
|
|
|
|
|
|
|
String directoryPath = "./temp"; |
|
|
String directoryPath = "./temp"; |
|
|
Path dirPath = Paths.get(directoryPath); |
|
|
Path dirPath = Paths.get(directoryPath); |
|
|
@ -98,9 +87,10 @@ public class SettingService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public RegisterDTO readFileWithRegister() throws IOException { |
|
|
public RegisterDTO readFileWithRegister() throws IOException { |
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
String filename = "./temp/registerConfig.json"; |
|
|
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); |
|
|
Path filePath = Paths.get(filename); |
|
|
return mapper.readValue(new File("./temp/deviceConfig.json"), RegisterDTO.class); |
|
|
String jsonString = Files.readString(filePath); |
|
|
|
|
|
return this.mapper.readValue(jsonString, RegisterDTO.class); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|