/** * Persist multiple alert events. * * @param builders * @return * @throws SiteWhereException */ public List<IDeviceAlert> persistAlerts(List<DeviceAlertCreateRequest.Builder> builders) throws SiteWhereException { List<DeviceAlertCreateRequest> requests = new ArrayList<>(); for (DeviceAlertCreateRequest.Builder builder : builders) { DeviceAlertCreateRequest request = builder.build(); requests.add(request); } return events.addDeviceAlerts(getDeviceAssignment().getId(), requests.toArray(new DeviceAlertCreateRequest[0])); }
public IDeviceCommandInvocation persist(DeviceCommandInvocationCreateRequest.Builder builder) throws SiteWhereException { DeviceCommandInvocationCreateRequest request = builder.build(); return events.addDeviceCommandInvocations(getDeviceAssignment().getId(), request).get(0); }
/** * Persist multiple location events. * * @param builders * @return * @throws SiteWhereException */ public List<IDeviceLocation> persistLocations(List<DeviceLocationCreateRequest.Builder> builders) throws SiteWhereException { List<DeviceLocationCreateRequest> requests = new ArrayList<>(); for (DeviceLocationCreateRequest.Builder builder : builders) { DeviceLocationCreateRequest request = builder.build(); requests.add(request); } return events.addDeviceLocations(getDeviceAssignment().getId(), requests.toArray(new DeviceLocationCreateRequest[0])); }
/** * Persist multiple measurement events. * * @param builders * @return * @throws SiteWhereException */ public List<IDeviceMeasurement> persistMeasurements(List<DeviceMeasurementCreateRequest.Builder> builders) throws SiteWhereException { List<DeviceMeasurementCreateRequest> requests = new ArrayList<>(); for (DeviceMeasurementCreateRequest.Builder builder : builders) { DeviceMeasurementCreateRequest request = builder.build(); requests.add(request); } return events.addDeviceMeasurements(getDeviceAssignment().getId(), requests.toArray(new DeviceMeasurementCreateRequest[0])); }
/** * Persist a single location event. * * @param builder * @return * @throws SiteWhereException */ public IDeviceLocation persist(DeviceLocationCreateRequest.Builder builder) throws SiteWhereException { DeviceLocationCreateRequest request = builder.build(); return events.addDeviceLocations(getDeviceAssignment().getId(), request).get(0); }
/** * Persist a single measurement event. * * @param builder * @return * @throws SiteWhereException */ public IDeviceMeasurement persist(DeviceMeasurementCreateRequest.Builder builder) throws SiteWhereException { DeviceMeasurementCreateRequest request = builder.build(); return events.addDeviceMeasurements(getDeviceAssignment().getId(), request).get(0); }
/** * Persist a single alert event. * * @param builder * @return * @throws SiteWhereException */ public IDeviceAlert persist(DeviceAlertCreateRequest.Builder builder) throws SiteWhereException { DeviceAlertCreateRequest request = builder.build(); return events.addDeviceAlerts(getDeviceAssignment().getId(), request).get(0); }
@Override public void handleDeviceStreamDataRequest(String deviceToken, IDeviceStreamDataCreateRequest request) throws SiteWhereException { IDeviceAssignment assignment = getCurrentAssignment(deviceToken); getDeviceStreamDataManagement().addDeviceStreamData(assignment.getId(), request); }
/** * Executes logic to process a batch of device events. * * @param assignmentToken * @param batch * @param management * @return * @throws SiteWhereException */ public static DeviceEventBatchResponse deviceEventBatchLogic(IDeviceAssignment assignment, IDeviceEventBatch batch, IDeviceEventManagement management) throws SiteWhereException { DeviceEventBatchResponse response = new DeviceEventBatchResponse(); for (IDeviceMeasurementCreateRequest mx : batch.getMeasurements()) { response.getCreatedMeasurements().addAll(management.addDeviceMeasurements(assignment.getId(), mx)); } for (IDeviceLocationCreateRequest location : batch.getLocations()) { response.getCreatedLocations().addAll(management.addDeviceLocations(assignment.getId(), location)); } for (IDeviceAlertCreateRequest alert : batch.getAlerts()) { response.getCreatedAlerts().addAll(management.addDeviceAlerts(assignment.getId(), alert)); } return response; }
@Override public void createLocation(IDeviceAssignment assignment, double latitude, double longitude, double elevation, boolean updateState) throws SiteWhereException { DeviceLocationCreateRequest location = new DeviceLocationCreateRequest(); location.setLatitude(latitude); location.setLongitude(longitude); location.setElevation(elevation); location.setEventDate(new Date()); location.setUpdateState(updateState); getDeviceEventManagement().addDeviceLocations(assignment.getId(), location); }
/** * Common logic for creating {@link DeviceStream} from * {@link IDeviceStreamCreateRequest}. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceStream deviceStreamCreateLogic(IDeviceAssignment assignment, IDeviceStreamCreateRequest request) throws SiteWhereException { DeviceStream stream = new DeviceStream(); stream.setId(UUID.randomUUID()); stream.setAssignmentId(assignment.getId()); // Content type is required. require("Content Type", request.getContentType()); stream.setContentType(request.getContentType()); return stream; } }
@Override public IDeviceAssignment updateDeviceAssignment(UUID id, IDeviceAssignmentCreateRequest request) throws SiteWhereException { IDeviceAssignment updated = super.updateDeviceAssignment(id, request); updateTenantAuthentication(); DeviceStateChangeCreateRequest state = new DeviceStateChangeCreateRequest(); state.setAttribute(IDeviceStateChangeCreateRequest.ATTRIBUTE_ASSIGNMENT); state.setType("update"); getDeviceEventManagement().addDeviceStateChanges(updated.getId(), state); return updated; }
@Override public IDeviceAssignment createDeviceAssignment(IDeviceAssignmentCreateRequest request) throws SiteWhereException { IDeviceAssignment created = super.createDeviceAssignment(request); updateTenantAuthentication(); DeviceStateChangeCreateRequest state = new DeviceStateChangeCreateRequest(); state.setAttribute(IDeviceStateChangeCreateRequest.ATTRIBUTE_ASSIGNMENT); state.setType("create"); getDeviceEventManagement().addDeviceStateChanges(created.getId(), state); return created; }
@Override public IDeviceAssignment endDeviceAssignment(UUID id) throws SiteWhereException { IDeviceAssignment updated = super.endDeviceAssignment(id); updateTenantAuthentication(); DeviceStateChangeCreateRequest state = new DeviceStateChangeCreateRequest(); state.setAttribute(IDeviceStateChangeCreateRequest.ATTRIBUTE_ASSIGNMENT); state.setType("end"); getDeviceEventManagement().addDeviceStateChanges(updated.getId(), state); return updated; }
@Override public void handleDeviceStreamRequest(String deviceToken, IDeviceStreamCreateRequest request) throws SiteWhereException { IDeviceAssignment assignment = getCurrentAssignment(deviceToken); DeviceStreamAckCommand ack = new DeviceStreamAckCommand(); try { getDeviceStreamManagement().createDeviceStream(assignment.getId(), request); ack.setStatus(DeviceStreamStatus.DeviceStreamCreated); } catch (SiteWhereException e) { getLogger().error("Unable to create device stream.", e); ack.setStatus(DeviceStreamStatus.DeviceStreamFailed); } // getDeviceCommunication(getTenantEngine().getTenant()).deliverSystemCommand(hardwareId, // ack); }
/** * Forward the given event to the Kafka persisted events topic. * * @param deviceAssignmentId * @param event * @return * @throws SiteWhereException */ protected <T extends IDeviceEvent> List<T> forwardEvents(UUID deviceAssignmentId, List<T> events) throws SiteWhereException { IDeviceAssignment assignment = assertDeviceAssignmentById(deviceAssignmentId); for (T event : events) { PersistedEventPayload api = new PersistedEventPayload(); api.setDeviceId(assignment.getDeviceId()); api.setEvent(event); GPersistedEventPayload payload = EventModelConverter.asGrpcPersistedEventPayload(api); getTenantEngine().getInboundPersistedEventsProducer().send(assignment.getId().toString(), EventModelMarshaler.buildPersistedEventPayloadMessage(payload)); } return events; }
/** * Common creation logic for all device events. * * @param request * @param assignment * @param target * @throws SiteWhereException */ public static void deviceEventCreateLogic(IDeviceEventCreateRequest request, IDeviceAssignment assignment, DeviceEvent target) throws SiteWhereException { target.setId(UUID.randomUUID()); target.setAlternateId(request.getAlternateId()); target.setDeviceId(assignment.getDeviceId()); target.setDeviceAssignmentId(assignment.getId()); target.setCustomerId(assignment.getCustomerId()); target.setAreaId(assignment.getAreaId()); target.setAssetId(assignment.getAssetId()); if (request.getEventDate() != null) { target.setEventDate(request.getEventDate()); } else { target.setEventDate(new Date()); } target.setReceivedDate(new Date()); MetadataProvider.copy(request.getMetadata(), target); }
@Override public void sendCommand(IDeviceAssignment assignment, String commandName, Map<String, String> parameters) throws SiteWhereException { IDevice device = getDeviceManagement().getDevice(assignment.getDeviceId()); DeviceCommandSearchCriteria criteria = new DeviceCommandSearchCriteria(1, 0); criteria.setDeviceTypeId(device.getDeviceTypeId()); ISearchResults<IDeviceCommand> commands = getDeviceManagement().listDeviceCommands(criteria); IDeviceCommand match = null; for (IDeviceCommand command : commands.getResults()) { if (command.getName().equals(commandName)) { match = command; } } if (match == null) { throw new SiteWhereException("Command not executed. No command found matching: " + commandName); } DeviceCommandInvocationCreateRequest create = new DeviceCommandInvocationCreateRequest(); create.setCommandToken(match.getToken()); create.setParameterValues(parameters); create.setInitiator(CommandInitiator.Script); create.setTarget(CommandTarget.Assignment); create.setTargetId(assignment.getToken()); create.setEventDate(new Date()); getDeviceEventManagement().addDeviceCommandInvocations(assignment.getId(), create); }
/** * Common logic for creating a device alarm. * * @param assignment * @param request * @return * @throws SiteWhereException */ public static DeviceAlarm deviceAlarmCreateLogic(IDeviceAssignment assignment, IDeviceAlarmCreateRequest request) throws SiteWhereException { DeviceAlarm alarm = new DeviceAlarm(); alarm.setId(UUID.randomUUID()); alarm.setDeviceId(assignment.getDeviceId()); alarm.setDeviceAssignmentId(assignment.getId()); alarm.setCustomerId(assignment.getCustomerId()); alarm.setAreaId(assignment.getAreaId()); alarm.setAssetId(assignment.getAssetId()); require("Alarm message", request.getAlarmMessage()); alarm.setAlarmMessage(request.getAlarmMessage()); alarm.setTriggeringEventId(request.getTriggeringEventId()); alarm.setState(request.getState() != null ? request.getState() : DeviceAlarmState.Triggered); alarm.setTriggeredDate(request.getTriggeredDate() != null ? request.getTriggeredDate() : new Date()); alarm.setAcknowledgedDate(request.getAcknowledgedDate()); alarm.setResolvedDate(request.getResolvedDate()); MetadataProvider.copy(request.getMetadata(), alarm); return alarm; }
/** * Common logic for creating new device state object and populating it from * request. * * @param request * @param device * @param deviceAssignment * @return * @throws SiteWhereException */ public static DeviceState deviceStateCreateLogic(IDeviceStateCreateRequest request, IDevice device, IDeviceAssignment deviceAssignment) throws SiteWhereException { DeviceState state = new DeviceState(); state.setId(UUID.randomUUID()); state.setDeviceId(device.getId()); state.setDeviceTypeId(request.getDeviceTypeId()); state.setDeviceAssignmentId(deviceAssignment.getId()); state.setCustomerId(request.getCustomerId()); state.setAreaId(request.getAreaId()); state.setAssetId(request.getAssetId()); state.setLastInteractionDate(request.getLastInteractionDate()); state.setPresenceMissingDate(request.getPresenceMissingDate()); state.setLastLocationEventId(request.getLastLocationEventId()); state.setLastMeasurementEventIds(request.getLastMeasurementEventIds()); state.setLastAlertEventIds(request.getLastAlertEventIds()); return state; }