Search in sources :

Example 1 with VirtualSerialPortConfig

use of com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig in project ma-modules-public by infiniteautomation.

the class VirtualSerialPortRestV2Controller method delete.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Delete virtual serial port", notes = "")
@RequestMapping(method = RequestMethod.DELETE, consumes = { "application/json", "application/sero-json" }, produces = { "application/json", "text/csv", "application/sero-json" }, value = { "/{xid}" })
public ResponseEntity<VirtualSerialPortConfig> delete(@ApiParam(value = "Valid Virtual serial port XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    // Check to see if it already exists
    VirtualSerialPortConfig existing = VirtualSerialPortConfigDao.instance.getByXid(xid);
    if (existing == null)
        throw new NotFoundRestException();
    // Ensure we set the xid
    existing.setXid(xid);
    // Save it
    VirtualSerialPortConfigDao.instance.remove(existing);
    return new ResponseEntity<>(existing, HttpStatus.OK);
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with VirtualSerialPortConfig

use of com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig in project ma-modules-public by infiniteautomation.

the class VirtualSerialPortRestV2Controller method save.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Create a virtual serial port", notes = "Cannot already exist, admin only")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "application/sero-json" }, produces = { "application/json", "text/csv", "application/sero-json" })
public ResponseEntity<VirtualSerialPortConfig> save(@ApiParam(value = "Serial Port", required = true) @RequestBody(required = true) VirtualSerialPortConfig model, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    // Check to see if it already exists
    if (!StringUtils.isEmpty(model.getXid())) {
        VirtualSerialPortConfig existing = VirtualSerialPortConfigDao.instance.getByXid(model.getXid());
        if (existing != null) {
            throw new AlreadyExistsRestException(model.getXid());
        }
    }
    // Validate
    model.ensureValid();
    // Save it
    VirtualSerialPortConfigDao.instance.save(model);
    // Put a link to the updated data in the header?
    URI location = builder.path("/v2/virtual-serial-ports/{xid}").buildAndExpand(model.getXid()).toUri();
    return getResourceCreated(model, location);
}
Also used : AlreadyExistsRestException(com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with VirtualSerialPortConfig

use of com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig in project ma-core-public by infiniteautomation.

the class VirtualSerialPortConfigDeserializer method deserialize.

/* (non-Javadoc)
	 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
	 */
@Override
public VirtualSerialPortConfig deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String typeName = tree.get("portType").asText();
    Class<?> clazz;
    switch(typeName) {
        case "SERIAL_SOCKET_BRIDGE":
            clazz = SerialSocketBridgeConfig.class;
            break;
        case "SERIAL_SERVER_SOCKET_BRIDGE":
            clazz = SerialServerSocketBridgeConfig.class;
            break;
        default:
            throw new ModelNotFoundException(typeName);
    }
    VirtualSerialPortConfig model = (VirtualSerialPortConfig) mapper.treeToValue(tree, clazz);
    return model;
}
Also used : ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with VirtualSerialPortConfig

use of com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig in project ma-core-public by infiniteautomation.

the class VirtualSerialPortImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    boolean isNew = false;
    if (StringUtils.isBlank(xid)) {
        xid = VirtualSerialPortConfigDao.instance.generateUniqueXid();
        isNew = true;
    }
    VirtualSerialPortConfig vo = VirtualSerialPortConfigDao.instance.getByXid(xid);
    if (vo == null) {
        isNew = true;
        String typeStr = json.getString("type");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.virtualserialport.missingType", xid, VirtualSerialPortConfig.PORT_TYPE_CODES);
        else {
            try {
                Class<?> virtualPortClass = VirtualSerialPortConfigResolver.findClass(typeStr);
                vo = (VirtualSerialPortConfig) virtualPortClass.newInstance();
                vo.setXid(xid);
            } catch (TranslatableJsonException ex) {
                addFailureMessage("emport.virtualserialport.prefix", xid, ex.getMsg());
            } catch (InstantiationException e) {
                addFailureMessage("emport.virtualserialport.prefix", xid, e.getMessage());
            } catch (IllegalAccessException e) {
                addFailureMessage("emport.virtualserialport.prefix", xid, e.getMessage());
            }
        }
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // Now validate it. Use a new response object so we can distinguish errors in this vo from
            // other errors.
            ProcessResult voResponse = new ProcessResult();
            vo.validate(voResponse);
            if (voResponse.getHasMessages())
                setValidationMessages(voResponse, "emport.virtualserialport.prefix", xid);
            else {
                // Sweet. Save it.
                VirtualSerialPortConfigDao.instance.save(vo);
                addSuccessMessage(isNew, "emport.virtualserialport.prefix", xid);
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.virtualserialport.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.virtualserialport.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig)

Example 5 with VirtualSerialPortConfig

use of com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig in project ma-core-public by infiniteautomation.

the class SerialPortManagerImpl method initialize.

/* (non-Javadoc)
     * @see com.infiniteautomation.mango.io.serial.SerialPortManager#initialize(boolean)
     */
@Override
public void initialize(boolean safe) throws LifecycleException {
    if (safe)
        return;
    try {
        String[] portNames;
        Map<String, Boolean> portOwnership = new HashMap<String, Boolean>();
        switch(SerialNativeInterface.getOsType()) {
            case SerialNativeInterface.OS_LINUX:
                portNames = SerialPortList.getPortNames(Common.envProps.getString("serial.port.linux.path", "/dev/"), Pattern.compile(Common.envProps.getString("serial.port.linux.regex", "((cu|ttyS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO|COM)[0-9]{1,3}|rs(232|485)-[0-9])")));
                break;
            case SerialNativeInterface.OS_MAC_OS_X:
                portNames = SerialPortList.getPortNames(Common.envProps.getString("serial.port.osx.path", "/dev/"), Pattern.compile(Common.envProps.getString("serial.port.osx.regex", // Was "tty.(serial|usbserial|usbmodem).*")
                "(cu|tty)..*")));
                break;
            case SerialNativeInterface.OS_WINDOWS:
                portNames = SerialPortList.getPortNames(Common.envProps.getString("serial.port.windows.path", ""), Pattern.compile(Common.envProps.getString("serial.port.windows.regex", "")));
            default:
                portNames = SerialPortList.getPortNames();
                break;
        }
        for (SerialPortIdentifier port : ownedPorts) portOwnership.put(port.getName(), true);
        for (String portName : portNames) {
            if (!portOwnership.containsKey(portName)) {
                freePorts.add(new SerialPortIdentifier(portName, SerialPortTypes.JSSC));
                portOwnership.put(portName, false);
            } else if (LOG.isDebugEnabled())
                LOG.debug("Not adding port " + portName + " to free ports because it is owned.");
        }
        // Collect any Virtual Comm Ports from the DB and load them in
        List<VirtualSerialPortConfig> list = VirtualSerialPortConfigDao.instance.getAll();
        if (list != null) {
            for (VirtualSerialPortConfig config : list) {
                if (!portOwnership.containsKey(config.getPortName())) {
                    freePorts.add(new VirtualSerialPortIdentifier(config));
                    portOwnership.put(config.getPortName(), false);
                } else if (LOG.isWarnEnabled()) {
                    LOG.warn("Virtual serial port config " + config.getXid() + " named " + config.getPortName() + " not available due to name conflict with other serial port or it was open during refresh.");
                }
            }
        }
        initialized = true;
    } catch (UnsatisfiedLinkError e) {
        throw new LifecycleException(e.getMessage());
    } catch (NoClassDefFoundError e) {
        throw new LifecycleException("Comm configuration error. Check that your serial port DLL or libraries have been correctly installed. " + e.getMessage());
    }
}
Also used : LifecycleException(com.serotonin.util.LifecycleException) HashMap(java.util.HashMap) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig)

Aggregations

VirtualSerialPortConfig (com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)2 URI (java.net.URI)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AlreadyExistsRestException (com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException)1 JsonException (com.serotonin.json.JsonException)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 LifecycleException (com.serotonin.util.LifecycleException)1 HashMap (java.util.HashMap)1 ResponseEntity (org.springframework.http.ResponseEntity)1