/** * Get the event time used to calculate row key and qualifier. * * @param event * @return */ protected static long getEventTime(IDeviceEventCreateRequest event) { return (event.getEventDate() != null) ? event.getEventDate().getTime() : System.currentTimeMillis(); }
/** * Common logic for updating a device event (Only metadata can be updated). * * @param metadata * @param target * @throws SiteWhereException */ public static void deviceEventUpdateLogic(IDeviceEventCreateRequest request, DeviceEvent target) throws SiteWhereException { if (request.getMetadata() != null) { target.getMetadata().clear(); MetadataProvider.copy(request.getMetadata(), target); } }
/** * Common logic for creating a {@link DeviceStateChange} from an * {@link IDeviceStateChangeCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceStateChange deviceStateChangeCreateLogic(IDeviceAssignment assignment, IDeviceStateChangeCreateRequest request) throws SiteWhereException { require("Attribute", request.getAttribute()); require("Type", request.getType()); DeviceStateChange state = new DeviceStateChange(); deviceEventCreateLogic(request, assignment, state); state.setAttribute(request.getAttribute()); state.setType(request.getType()); state.setPreviousState(request.getPreviousState()); state.setNewState(request.getNewState()); return state; } }
/** * Common logic for creating {@link DeviceAlert} from * {@link IDeviceAlertCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceAlert deviceAlertCreateLogic(IDeviceAssignment assignment, IDeviceAlertCreateRequest request) throws SiteWhereException { DeviceAlert alert = new DeviceAlert(); deviceEventCreateLogic(request, assignment, alert); if (request.getSource() != null) { alert.setSource(request.getSource()); } else { alert.setSource(AlertSource.Device); } if (request.getLevel() != null) { alert.setLevel(request.getLevel()); } else { alert.setLevel(AlertLevel.Info); } alert.setType(request.getType()); alert.setMessage(request.getMessage()); return alert; }
/** * Common logic for creating {@link DeviceLocation} from * {@link IDeviceLocationCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceLocation deviceLocationCreateLogic(IDeviceAssignment assignment, IDeviceLocationCreateRequest request) throws SiteWhereException { DeviceLocation location = new DeviceLocation(); deviceEventCreateLogic(request, assignment, location); location.setLatitude(request.getLatitude()); location.setLongitude(request.getLongitude()); location.setElevation(request.getElevation()); return location; }
/** * Common logic for creating {@link DeviceCommandInvocation} from an * {@link IDeviceCommandInvocationCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceCommandInvocation deviceCommandInvocationCreateLogic(IDeviceAssignment assignment, IDeviceCommandInvocationCreateRequest request) throws SiteWhereException { requireNotNull("Initiator", request.getInitiator()); requireNotNull("Target", request.getTarget()); DeviceCommandInvocation ci = new DeviceCommandInvocation(); deviceEventCreateLogic(request, assignment, ci); ci.setCommandToken(request.getCommandToken()); ci.setInitiator(request.getInitiator()); ci.setInitiatorId(request.getInitiatorId()); ci.setTarget(request.getTarget()); ci.setTargetId(request.getTargetId()); ci.setParameterValues(request.getParameterValues()); return ci; }
/** * Common logic for creating a {@link DeviceCommandResponse} from an * {@link IDeviceCommandResponseCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceCommandResponse deviceCommandResponseCreateLogic(IDeviceAssignment assignment, IDeviceCommandResponseCreateRequest request) throws SiteWhereException { requireNotNull("Originating Event Id", request.getOriginatingEventId()); DeviceCommandResponse response = new DeviceCommandResponse(); deviceEventCreateLogic(request, assignment, response); response.setOriginatingEventId(request.getOriginatingEventId()); response.setResponseEventId(request.getResponseEventId()); response.setResponse(request.getResponse()); return response; }
/** * Common logic for creating {@link DeviceMeasurement} from * {@link IDeviceMeasurementCreateRequest}. * * @param request * @param assignment * @return * @throws SiteWhereException */ public static DeviceMeasurement deviceMeasurementCreateLogic(IDeviceMeasurementCreateRequest request, IDeviceAssignment assignment) throws SiteWhereException { DeviceMeasurement measurements = new DeviceMeasurement(); deviceEventCreateLogic(request, assignment, measurements); measurements.setName(request.getName()); measurements.setValue(request.getValue()); return measurements; }
/** * Common logic for creating {@link DeviceStreamData} from * {@link IDeviceStreamDataCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceStreamData deviceStreamDataCreateLogic(IDeviceAssignment assignment, IDeviceStreamDataCreateRequest request) throws SiteWhereException { DeviceStreamData streamData = new DeviceStreamData(); deviceEventCreateLogic(request, assignment, streamData); require(request.getStreamId()); streamData.setStreamId(request.getStreamId()); requireNotNull(request.getSequenceNumber()); streamData.setSequenceNumber(request.getSequenceNumber()); streamData.setData(request.getData()); return streamData; }
/** * Common logic for creating {@link DeviceMeasurements} from * {@link IDeviceMeasurementsCreateRequest}. * * @param request * @param assignment * @return * @throws SiteWhereException */ public static DeviceMeasurements deviceMeasurementsCreateLogic(IDeviceMeasurementsCreateRequest request, IDeviceAssignment assignment) throws SiteWhereException { DeviceMeasurements measurements = new DeviceMeasurements(); deviceEventCreateLogic(request, assignment, measurements); for (String key : request.getMeasurements().keySet()) { measurements.addOrReplaceMeasurement(key, request.getMeasurement(key)); } return measurements; }
@Override public void handleSendDeviceStreamDataRequest(String deviceToken, ISendDeviceStreamDataRequest request) throws SiteWhereException { IDeviceStreamData data = getDeviceStreamDataManagement().getDeviceStreamData(request.getStreamId(), request.getSequenceNumber()); SendDeviceStreamDataCommand command = new SendDeviceStreamDataCommand(); command.setDeviceToken(deviceToken); command.setStreamId(request.getStreamId()); command.setSequenceNumber(request.getSequenceNumber()); if (data != null) { command.setData(data.getData()); } else { command.setData(new byte[0]); } // getDeviceCommunication(getTenantEngine().getTenant()).deliverSystemCommand(hardwareId, // command); }
@Override public boolean isDuplicate(IDecodedDeviceRequest<?> request) throws SiteWhereException { if (request.getRequest() instanceof IDeviceEventCreateRequest) { IDeviceEventCreateRequest createRequest = (IDeviceEventCreateRequest) request.getRequest(); String alternateId = createRequest.getAlternateId(); if (alternateId != null) { IDeviceEvent existing = getDeviceEventManagement().getDeviceEventByAlternateId(alternateId); if (existing != null) { getLogger().info("Found event with same alternate id. Will be treated as duplicate."); return true; } return false; } } return false; }
/** * Get customer that should be used for the given request. * * @param registration * @return * @throws SiteWhereException */ protected ICustomer getCustomerFor(IDeviceRegistrationPayload registration) throws SiteWhereException { String customerToken = registration.getDeviceRegistrationRequest().getCustomerToken(); if (customerToken != null) { ICustomer override = getDeviceManagement().getCustomerByToken(customerToken); if (override == null) { throw new SiteWhereException("Registration request specified invalid customer token."); } return override; } else if (isUseDefaultCustomer()) { return getDefaultCustomer(); } return null; }
/** * Get device type that should be used for the given request. * * @param registration * @return * @throws SiteWhereException */ protected IDeviceType getDeviceTypeFor(IDeviceRegistrationPayload registration) throws SiteWhereException { String deviceTypeToken = registration.getDeviceRegistrationRequest().getDeviceTypeToken(); if (deviceTypeToken != null) { IDeviceType override = getDeviceManagement().getDeviceTypeByToken(deviceTypeToken); if (override == null) { throw new SiteWhereException("Registration request specified invalid device type token."); } return override; } else if (isUseDefaultDeviceType()) { return getDefaultDeviceType(); } throw new SiteWhereException("Device type not passed and no default provided."); }
/** * Convert device assignment event create request from API to GRPC. * * @param api * @return * @throws SiteWhereException */ public static GDeviceAssignmentEventCreateRequest asGrpcDeviceAssignmentEventCreateRequest( IDeviceAssignmentEventCreateRequest api) throws SiteWhereException { GDeviceAssignmentEventCreateRequest.Builder grpc = GDeviceAssignmentEventCreateRequest.newBuilder(); grpc.setDeviceAssignmentId(CommonModelConverter.asGrpcUuid(api.getDeviceAssignmentId())); grpc.setRequest(EventModelConverter.asGrpcDeviceEventCreateRequest(api.getRequest())); return grpc.build(); }
/** * Common logic for creating {@link DeviceAlert} from * {@link IDeviceAlertCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceAlert deviceAlertCreateLogic(IDeviceAssignment assignment, IDeviceAlertCreateRequest request) throws SiteWhereException { DeviceAlert alert = new DeviceAlert(); deviceEventCreateLogic(request, assignment, alert); if (request.getSource() != null) { alert.setSource(request.getSource()); } else { alert.setSource(AlertSource.Device); } if (request.getLevel() != null) { alert.setLevel(request.getLevel()); } else { alert.setLevel(AlertLevel.Info); } alert.setType(request.getType()); alert.setMessage(request.getMessage()); return alert; }
/** * Common logic for creating {@link DeviceLocation} from * {@link IDeviceLocationCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceLocation deviceLocationCreateLogic(IDeviceAssignment assignment, IDeviceLocationCreateRequest request) throws SiteWhereException { DeviceLocation location = new DeviceLocation(); deviceEventCreateLogic(request, assignment, location); location.setLatitude(request.getLatitude()); location.setLongitude(request.getLongitude()); location.setElevation(request.getElevation()); return location; }
/** * Common logic for creating a {@link DeviceCommandResponse} from an * {@link IDeviceCommandResponseCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceCommandResponse deviceCommandResponseCreateLogic(IDeviceAssignment assignment, IDeviceCommandResponseCreateRequest request) throws SiteWhereException { if (request.getOriginatingEventId() == null) { throw new SiteWhereSystemException(ErrorCode.IncompleteData, ErrorLevel.ERROR); } DeviceCommandResponse response = new DeviceCommandResponse(); deviceEventCreateLogic(request, assignment, response); response.setOriginatingEventId(request.getOriginatingEventId()); response.setResponseEventId(request.getResponseEventId()); response.setResponse(request.getResponse()); return response; }
/** * Convert device measurement create request from API to GRPC. * * @param api * @return * @throws SiteWhereException */ public static GDeviceMeasurementCreateRequest asGrpcDeviceMeasurementCreateRequest( IDeviceMeasurementCreateRequest api) throws SiteWhereException { GDeviceMeasurementCreateRequest.Builder grpc = GDeviceMeasurementCreateRequest.newBuilder(); grpc.setName(api.getName()); grpc.setValue(api.getValue()); grpc.setEvent(EventModelConverter.createGrpcDeviceEventCreateRequest(api)); return grpc.build(); }
/** * Convert device alert create request from API to GRPC. * * @param api * @return * @throws SiteWhereException */ public static GDeviceAlertCreateRequest asGrpcDeviceAlertCreateRequest(IDeviceAlertCreateRequest api) throws SiteWhereException { GDeviceAlertCreateRequest.Builder grpc = GDeviceAlertCreateRequest.newBuilder(); grpc.setSource(EventModelConverter.asGrpcAlertSource(api.getSource())); grpc.setLevel(EventModelConverter.asGrpcAlertLevel(api.getLevel())); grpc.setType(api.getType()); grpc.setAlertMessage(api.getMessage()); grpc.setEvent(EventModelConverter.createGrpcDeviceEventCreateRequest(api)); return grpc.build(); }