public static Operation convertMobileOperationToOperation(MobileOperation mobileOperation) { Operation operation = new Operation(); Properties properties = new Properties(); operation.setCode(mobileOperation.getFeatureCode()); for (MobileOperationProperty mobileOperationProperty : mobileOperation.getProperties()) { properties.put(mobileOperationProperty.getProperty(), mobileOperationProperty.getValue()); } operation.setProperties(properties); return operation; }
@POST @Path("device/{deviceId}/buzz") public Response switchBuzzer( @PathParam("deviceId") String deviceId, @FormParam("state") String state) { if (state == null || state.isEmpty()) { log.error("State is not defined for the buzzer operation"); return Response.status(Response.Status.BAD_REQUEST).build(); } String switchToState = state.toUpperCase(); if (!switchToState.equals(VirtualFireAlarmConstants.STATE_ON) && !switchToState.equals(VirtualFireAlarmConstants.STATE_OFF)) { log.error("The requested state change shoud be either - 'ON' or 'OFF'"); return Response.status(Response.Status.BAD_REQUEST).build(); } try { if (!APIUtil.getDeviceAccessAuthorizationService() .isUserAuthorized( new DeviceIdentifier(deviceId, VirtualFireAlarmConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) { return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); } String resource = VirtualFireAlarmConstants.BULB_CONTEXT.replace("/", ""); PrivateKey serverPrivateKey = VirtualFirealarmSecurityManager.getServerPrivateKey(); String actualMessage = resource + ":" + switchToState; String encryptedMsg = VirtualFireAlarmServiceUtils.prepareSecurePayLoad(actualMessage, serverPrivateKey); String publishTopic = APIUtil.getTenantDomainOftheUser() + "/" + VirtualFireAlarmConstants.DEVICE_TYPE + "/" + deviceId; Operation commandOp = new CommandOperation(); commandOp.setCode("buzz"); commandOp.setType(Operation.Type.COMMAND); commandOp.setEnabled(true); commandOp.setPayLoad(encryptedMsg); Properties props = new Properties(); props.setProperty(VirtualFireAlarmConstants.MQTT_ADAPTER_TOPIC_PROPERTY_NAME, publishTopic); props.setProperty( VirtualFireAlarmConstants.CLIENT_JID_PROPERTY_KEY, deviceId + "@" + XmppConfig.getInstance().getServerName()); props.setProperty(VirtualFireAlarmConstants.SUBJECT_PROPERTY_KEY, "CONTROL-REQUEST"); props.setProperty( VirtualFireAlarmConstants.MESSAGE_TYPE_PROPERTY_KEY, VirtualFireAlarmConstants.CHAT_PROPERTY_KEY); commandOp.setProperties(props); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); deviceIdentifiers.add(new DeviceIdentifier(deviceId, VirtualFireAlarmConstants.DEVICE_TYPE)); APIUtil.getDeviceManagementService() .addOperation(VirtualFireAlarmConstants.DEVICE_TYPE, commandOp, deviceIdentifiers); return Response.ok().build(); } catch (DeviceAccessAuthorizationException e) { log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (VirtualFireAlarmException e) { String errorMsg = "Preparing Secure payload failed for device - [" + deviceId + "]"; log.error(errorMsg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (OperationManagementException e) { String msg = "Error occurred while executing command operation upon ringing the buzzer"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } }