Last active
January 9, 2019 12:38
-
-
Save axeda/6465414 to your computer and use it in GitHub Desktop.
Helpful SDK v1 Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.axeda.common.id.Identifier | |
| import com.axeda.drm.sdk.Context | |
| import com.axeda.drm.sdk.agent.commands.CommandStatus | |
| import com.axeda.drm.sdk.agent.commands.SetDataItem | |
| import com.axeda.drm.sdk.audit.AuditCategory | |
| import com.axeda.drm.sdk.audit.AuditCategoryList | |
| import com.axeda.drm.sdk.audit.AuditEntry | |
| import com.axeda.drm.sdk.audit.AuditEntryFinder | |
| import com.axeda.drm.sdk.audit.AuditMessage | |
| import com.axeda.drm.sdk.common.Operator | |
| import com.axeda.drm.sdk.contact.Contact | |
| import com.axeda.drm.sdk.contact.ContactTypeFinder | |
| import com.axeda.drm.sdk.contact.DeviceContact | |
| import com.axeda.drm.sdk.contact.DeviceContactFinder | |
| import com.axeda.drm.sdk.contact.Location | |
| import com.axeda.drm.sdk.contact.LocationFinder | |
| import com.axeda.drm.sdk.contact.Organization | |
| import com.axeda.drm.sdk.contact.Region | |
| import com.axeda.drm.sdk.data.Alarm | |
| import com.axeda.drm.sdk.data.AlarmEntry | |
| import com.axeda.drm.sdk.data.AlarmFinder | |
| import com.axeda.drm.sdk.data.CurrentDataFinder | |
| import com.axeda.drm.sdk.data.DataValue | |
| import com.axeda.drm.sdk.data.DataValueEntry | |
| import com.axeda.drm.sdk.data.DataValueList | |
| import com.axeda.drm.sdk.data.Event | |
| import com.axeda.drm.sdk.data.EventEntry | |
| import com.axeda.drm.sdk.data.EventFinder | |
| import com.axeda.drm.sdk.data.HistoricalAlarmFinder | |
| import com.axeda.drm.sdk.data.HistoricalDataFinder | |
| import com.axeda.drm.sdk.data.UploadedFile | |
| import com.axeda.drm.sdk.data.UploadedFileFinder | |
| import com.axeda.drm.sdk.device.Condition | |
| import com.axeda.drm.sdk.device.ConditionFinder | |
| import com.axeda.drm.sdk.device.DataItem | |
| import com.axeda.drm.sdk.device.Device | |
| import com.axeda.drm.sdk.device.DeviceFinder | |
| import com.axeda.drm.sdk.device.DeviceGroup | |
| import com.axeda.drm.sdk.device.DeviceGroupFinder | |
| import com.axeda.drm.sdk.device.DeviceProperty | |
| import com.axeda.drm.sdk.device.DevicePropertyFinder | |
| import com.axeda.drm.sdk.device.Model | |
| import com.axeda.drm.sdk.device.ModelFinder | |
| import com.axeda.drm.sdk.device.Unit | |
| import com.axeda.drm.sdk.m2m.DeviceAssociation | |
| import com.axeda.drm.sdk.m2m.DeviceAssociationFinder | |
| import com.axeda.drm.sdk.m2m.ModelAssociation | |
| import com.axeda.drm.sdk.m2m.ModelAssociationFinder | |
| import com.axeda.drm.sdk.mobilelocation.CurrentMobileLocationFinder | |
| import com.axeda.drm.sdk.mobilelocation.HistoricalMobileLocationFinder | |
| import com.axeda.drm.sdk.mobilelocation.MobileLocation | |
| import com.axeda.drm.sdk.privilege.Privilege | |
| import com.axeda.drm.sdk.privilege.PrivilegeFinder | |
| import com.axeda.drm.sdk.rules.engine.Expression | |
| import com.axeda.drm.sdk.rules.engine.ExpressionRule | |
| import com.axeda.drm.sdk.rules.engine.ExpressionRuleFinder | |
| import com.axeda.drm.sdk.rules.engine.RuleAssociation | |
| import com.axeda.drm.sdk.rules.engine.RuleAssociationFinder | |
| import com.axeda.drm.sdk.user.User | |
| import com.axeda.drm.sdk.user.UserFinder | |
| import com.axeda.drm.sdk.user.UserGroup | |
| import com.axeda.drm.sdk.user.UserGroupFinder | |
| import groovy.json.JsonSlurper | |
| import com.axeda.drm.services.device.DataItemType | |
| import com.axeda.drm.sdk.rules.engine.RuleType | |
| import com.axeda.drm.sdk.common.EntityType | |
| import com.axeda.common.sdk.jdbc.StringQuery | |
| import com.axeda.drm.sdk.device.Property | |
| import com.axeda.drm.sdk.device.PropertyType | |
| def findAlarmOnDevice(Context CONTEXT, com.axeda.drm.sdk.device.Device device, String alarmState, String alarmName){ | |
| AlarmFinder alarmFinder = new AlarmFinder(CONTEXT); | |
| alarmFinder.setAlarmName(alarmName); | |
| alarmFinder.setDevice(device) | |
| alarmFinder.setState(AlarmState.valueOf(alarmState)) | |
| Alarm alarm = alarmFinder.find(); | |
| alarm | |
| } | |
| def createAlarmEntry(Context CONTEXT, String alarmName, com.axeda.drm.sdk.device.Device device, int severity){ | |
| AlarmEntry alarmEntry=new AlarmEntry(CONTEXT, device, alarmName, severity) | |
| alarmEntry.store() | |
| alarmEntry | |
| } | |
| def getAuditCategory(String categoryName){ | |
| AuditCategory auditCategory = AuditCategory.getByString(categoryName) | |
| auditCategory | |
| } | |
| def auditMessage(Context CONTEXT, String category, String message, Long assetId) { | |
| AuditCategory auditCategory = AuditCategory.getByString(category) ?: AuditCategory.DATA_MANAGEMENT | |
| def message | |
| if (assetId == null) { | |
| message = new AuditMessage(CONTEXT, "com.axeda.drm.rules.functions.AuditLogAction", auditCategory, [message]).store() | |
| } else { | |
| message = new AuditMessage(CONTEXT, "com.axeda.drm.rules.functions.AuditLogAction", auditCategory, [message], new Identifier(assetId)) | |
| message.store() | |
| } | |
| message | |
| } | |
| def getAuditEntries(Context CONTEXT, String categoryName, int daysBack){ | |
| AuditCategoryList auditCategoryList = new AuditCategoryList() | |
| auditCategoryList.add(AuditCategory.getByString(categoryName)) | |
| AuditEntryFinder auditEntryFinder= new AuditEntryFinder(CONTEXT) | |
| auditEntryFinder.setCategories(auditCategoryList) | |
| auditEntryFinder.setToDate(new Date()) | |
| auditEntryFinder.setFromDate(new Date() - daysBack) | |
| auditEntryFinder.setSortType(SortType.DATE) | |
| auditEntryFinder.sortDescending() | |
| List<AuditEntry> audits = auditEntryFinder.findAll() | |
| audits | |
| } | |
| def getAuditEntriesOnDevice(Context CONTEXT, com.axeda.drm.sdk.device.Device device, String categoryName, int daysBack){ | |
| AuditCategoryList auditCategoryList = new AuditCategoryList() | |
| auditCategoryList.add(AuditCategory.getByString(categoryName)) | |
| AuditEntryFinder auditEntryFinder= new AuditEntryFinder(CONTEXT) | |
| auditEntryFinder.setCategories(auditCategoryList) | |
| auditEntryFinder.setToDate(new Date()) | |
| auditEntryFinder.setFromDate(new Date() - daysBack) | |
| auditEntryFinder.setSortType(SortType.DATE) | |
| auditEntryFinder.setDevice(device) | |
| auditEntryFinder.sortDescending() | |
| List<AuditEntry> audits = auditEntryFinder.findAll() | |
| audits | |
| } | |
| def setDeviceCondition(Context CONTEXT, String conditionName, com.axeda.drm.sdk.device.Device device){ | |
| ConditionFinder conditionFinder = new ConditionFinder(CONTEXT) | |
| conditionFinder.setName(conditionName) | |
| Condition condition = conditionFinder.find() | |
| if (condition == null){ | |
| throw new Exception("Condition $conditionName not found.") | |
| } | |
| device.setCondition(condition) | |
| device.store() | |
| device | |
| } | |
| def findOrCreateDeviceContact(Context CONTEXT, com.axeda.drm.sdk.device.Device device, String firstName, String lastName, String locationName, String contactTypeName, Collection emails, Collection mobiles){ | |
| DeviceContactFinder deviceContactFinder = new DeviceContactFinder(CONTEXT) | |
| deviceContactFinder.setDeviceId(device.id) | |
| DeviceContact deviceContact = deviceContactFinder.findOne() | |
| def contactMethods = [email:[],mobile:[]] | |
| if (!deviceContact){ | |
| locationName = locationName ?: "Default Location" | |
| LocationFinder locationFinder = new LocationFinder(CONTEXT) | |
| locationFinder.setName(locationName) | |
| def location = locationFinder.findOne() | |
| contactTypeName = contactTypeName ?: "Other" | |
| ContactTypeFinder contactTypeFinder = new ContactTypeFinder(CONTEXT) | |
| contactTypeFinder.setName(contactTypeName) | |
| def contactType = contactTypeFinder.findOne() | |
| // create a new contact to hold contact methods for this asset | |
| def newcontact = new Contact(CONTEXT, firstName, lastName, location.organization, location) | |
| newcontact.store() | |
| deviceContact = new DeviceContact(CONTEXT, device, newcontact, contactType) | |
| device.addContact(deviceContact) | |
| device.store() | |
| } | |
| contactMethods.email = deviceContact.contact.getContactMethod("E-mail").replace(" ","")?.tokenize(",") | |
| contactMethods.mobile = deviceContact.contact.getContactMethod("Phone").replace(" ","")?.tokenize(",") | |
| if (emails?.size() > 0 || mobiles?.size() > 0){ | |
| emails?.each{ email -> | |
| def emailstring = deviceContact.contact.getContactMethod("E-mail") && | |
| deviceContact.contact.getContactMethod("E-mail") != "" ? | |
| deviceContact.contact.getContactMethod("E-mail") + "," + email : | |
| if (deviceContact.contact.getContactMethod("E-mail")){ | |
| deviceContact.contact.removeContactMethod(ContactMethodType.getEmailContactMethodType(CONTEXT)) | |
| } | |
| deviceContact.contact.addContactMethod(ContactMethodType.getEmailContactMethodType(CONTEXT), email) | |
| } | |
| mobiles?.each{ mobile -> | |
| def mobilestring = deviceContact.contact.getContactMethod("Phone") && | |
| deviceContact.contact.getContactMethod("Phone") != "" ? | |
| deviceContact.contact.getContactMethod("Phone") + "," + mobile : | |
| mobile | |
| if (deviceContact.contact.getContactMethod("Phone")){ | |
| deviceContact.contact.removeContactMethod(ContactMethodType.getPhoneContactMethodType(CONTEXT)) | |
| } | |
| deviceContact.contact.addContactMethod(ContactMethodType.getPhoneContactMethodType(CONTEXT), mobile) | |
| } | |
| deviceContact.contact.store() | |
| } | |
| deviceContact | |
| } | |
| def findCurrentData(Context CONTEXT, com.axeda.drm.sdk.device.Device device, String dataItemName){ | |
| CurrentDataFinder currentDataFinder = new CurrentDataFinder(CONTEXT, device) | |
| def value | |
| if (dataItemName){ | |
| value = currentDataFinder.find(dataItemName) | |
| } | |
| else { | |
| value = currentDataFinder.find() | |
| } | |
| value | |
| } | |
| def findCurrentMobileLocation(Context CONTEXT, com.axeda.drm.sdk.device.Device device){ | |
| CurrentMobileLocationFinder currentMobileLocationFinder = new CurrentMobileLocationFinder(CONTEXT) | |
| // deviceId is a Long, which can be accessed from the device Identifier with .value | |
| currentMobileLocationFinder.deviceId = device.id.value | |
| MobileLocation mobileLocation = currentMobileLocationFinder.find() | |
| mobileLocation | |
| } | |
| def findOrCreateDataItem(Context CONTEXT, com.axeda.drm.sdk.device.Model model, String dataItemName, String dataItemValue){ | |
| // if no dataItemValue to determine type from, sets to String as default | |
| DataItem dataItem = model.dataItems?.find{ it.name == dataItemName } | |
| if (dataItemValue && dataItemValue.isNumber()){ | |
| dataItemValue = Double.valueOf(dataItemValue) | |
| } | |
| if (dataItem == null){ | |
| if (dataItemValue?.isNumber()){ | |
| dataItem = new DataItem(CONTEXT, model,DataItemType.ANALOG, dataItemName) | |
| } | |
| else if (dataItemValue == "true" || dataItemValue == "false"){ | |
| dataItem = new DataItem(CONTEXT, model,DataItemType.BOOLEAN, dataItemName) | |
| } | |
| else { | |
| dataItem = new DataItem(CONTEXT, model,DataItemType.STRING, dataItemName) | |
| } | |
| dataItem.store() | |
| } | |
| dataItem | |
| } | |
| def setDataValueOnEnterprise(Context CONTEXT, com.axeda.drm.sdk.device.Device device, DataItem dataItem, Object dataItemValue){ | |
| DataValueEntry dve | |
| try { | |
| dve = new DataValueEntry(CONTEXT, device, dataItem, dataItemValue) | |
| dve.store() | |
| } | |
| catch (Exception ex){ | |
| throw new Exception("Data Item Type ${dataItem.type.toString()} does not agree with ${dataItemValue}") | |
| } | |
| dve | |
| } | |
| def findOrCreateDevice(Context CONTEXT, com.axeda.drm.sdk.device.Model model, String serialNumber){ | |
| DeviceFinder deviceFinder = new DeviceFinder(CONTEXT) | |
| deviceFinder.setModel(model) | |
| deviceFinder.setSerialNumber(serialNumber) | |
| Device device = deviceFinder.find() | |
| if (!device){ | |
| device = new Device(CONTEXT, serialNumber, model); | |
| device.store(); | |
| } | |
| device | |
| } | |
| def findAssociatedSourceDevice(Context CONTEXT, com.axeda.drm.sdk.device.Device source, com.axeda.drm.sdk.device.Device target) { | |
| DeviceAssociationFinder deviceAssociationFinder = new DeviceAssociationFinder(CONTEXT) | |
| deviceAssociationFinder.setDestinationId target.id | |
| DeviceFinder deviceFinder = new DeviceFinder(CONTEXT) | |
| Device sourceDevice | |
| def ids = deviceAssociationFinder.getSourceIds() | |
| ids.each {Identifier id -> | |
| deviceFinder.id = id | |
| Device sample = null | |
| try {sample = deviceFinder.find()} catch (Exception e) {} | |
| if (sample != null) { | |
| sourceDevice = sample | |
| } | |
| } | |
| if (ids == null || ids?.size == 0){ | |
| DeviceAssociation deviceAssociation = new DeviceAssociation(CONTEXT, source?.id, target?.id) | |
| deviceAssociation.store() | |
| deviceAssociation = new DeviceAssociation(CONTEXT, source?.id, source?.id) | |
| deviceAssociation.store() | |
| } | |
| sourceDevice | |
| } | |
| def findOrCreateDeviceGroup(Context CONTEXT, String deviceGroupName, String rootGroupName){ | |
| rootGroupName = rootGroupName ?: "*Root*" | |
| DeviceGroupFinder deviceGroupFinder = new DeviceGroupFinder(CONTEXT) | |
| deviceGroupFinder.setName(deviceGroupName); | |
| DeviceGroup deviceGroup = deviceGroupFinder.find(); | |
| if (!deviceGroup){ | |
| deviceGroupFinder.setName(StringQuery.like(rootGroupName)); | |
| def rootgroup = deviceGroupFinder.find() | |
| deviceGroup = new DeviceGroup(CONTEXT,rootgroup,deviceGroupName) | |
| deviceGroup.store() | |
| } | |
| deviceGroup | |
| } | |
| def updateOrCreateDeviceProperties(Context CONTEXT, com.axeda.drm.sdk.device.Device device, String propertyType, Map tuples){ | |
| def propType = propertyType ?: "DEVICE_TYPE" | |
| try { | |
| propType = PropertyType.valueOf(propType) | |
| } | |
| catch (Exception e){ | |
| throw new Exception("Property Type $propertyType is invalid") | |
| } | |
| DevicePropertyFinder devicePropertyFinder = new DevicePropertyFinder(CONTEXT) | |
| devicePropertyFinder.type = PropertyType.valueOf(propertyType) | |
| devicePropertyFinder.id = device.id | |
| DeviceProperty deviceProperty = devicePropertyFinder.findOne() | |
| if (deviceProperty == null){ | |
| deviceProperty = new DeviceProperty(CONTEXT) | |
| deviceProperty.setId(device.id) | |
| deviceProperty.setType(propType) | |
| def propertiesList = tuples.collect{ k, v -> def prop = new Property(0, k.asType(String), v.asType(String)) } | |
| deviceProperty.setProperties(propertiesList) | |
| deviceProperty.store() | |
| } | |
| else { | |
| List<Property> props = deviceProperty.getProperties() | |
| tuples.each{ name, value -> | |
| def prop = props.find{ it.name == name } | |
| prop.value = value | |
| } | |
| deviceProperty.store() | |
| } | |
| deviceProperty | |
| } | |
| def findEvent(Context CONTEXT, com.axeda.drm.sdk.device.Device device, String eventName){ | |
| EventFinder eventFinder = new EventFinder(CONTEXT); | |
| eventFinder.setDevice(device); | |
| eventFinder.setName(eventName); | |
| Event event = eventFinder.find() | |
| event | |
| } | |
| def findOrCreateExpressionRule(Context CONTEXT, String expressionRuleName, String description, String triggername, String ifExpression, String thenExpression, boolean enabled, boolean consecutive){ | |
| ExpressionRuleFinder expressionRuleFinder = new ExpressionRuleFinder(CONTEXT) | |
| expressionRuleFinder.setName(expressionRuleName) | |
| ExpressionRule expressionRule = expressionRuleFinder.findOne() | |
| if (!expressionRule){ | |
| expressionRule = new ExpressionRule(CONTEXT) | |
| expressionRule.setName(expressionRuleName) | |
| expressionRule.setDescription(description) | |
| expressionRule.setTriggerName(triggername) | |
| expressionRule.setIfExpression(new Expression(ifExpression)) | |
| expressionRule.setThenExpression(new Expression(thenExpression)) | |
| expressionRule.setEnabled(enabled) | |
| // consecutive false could result in a scenario where the person's threshold was violated but the rule had already fired below the | |
| // threshold before coming back down so it wouldn't fire for them | |
| expressionRule.setConsecutive(consecutive) | |
| // arguments are not used for validate method | |
| def result = expressionRule.validate(null, null) | |
| if (result != "OK"){ | |
| throw new Exception("Expression Rule $expressionRuleName is invalid: $result") | |
| } | |
| expressionRule.store() | |
| } | |
| expressionRule | |
| } | |
| def getHistoricalAlarms(Context CONTEXT, com.axeda.drm.sdk.device.Device device, int daysBack, String alarmState){ | |
| HistoricalAlarmFinder alarmFinder = new HistoricalAlarmFinder(CONTEXT) | |
| def start = new Date() - daysBack | |
| def end = new Date() | |
| alarmFinder.setDate(DateQuery.between(start,end)) | |
| alarmFinder.setState(AlarmState.valueOf(alarmState)) | |
| alarmFinder.setDevice(device) | |
| List<Alarm> alarmList = alarmFinder.findAll() | |
| alarmList | |
| } | |
| def getHistoricalData(Context CONTEXT, com.axeda.drm.sdk.device.Device device, DataItem dataItem, int daysBack){ | |
| HistoricalDataFinder dataFinder = new HistoricalDataFinder(CONTEXT, device) | |
| def start = new Date() - daysBack | |
| def end = new Date() | |
| dataFinder.setStartDate(start) | |
| dataFinder.setEndDate(end) | |
| DataValueList historicalDataValueList = dataFinder.find(dataItem) | |
| List<DataValue> historicalDataValues = historicalDataValueList.list | |
| historicalDataValues | |
| } | |
| def getHistoricalMobileLocations(Context CONTEXT, com.axeda.drm.sdk.device.Device device, int lastNValues, int daysBack){ | |
| lastNValues = lastNValues ?: 1000 | |
| HistoricalMobileLocationFinder historicalMobileLocationFinder = new HistoricalMobileLocationFinder(CONTEXT) | |
| historicalMobileLocationFinder.setLastN(lastNValues) | |
| // setDeviceId method takes the Long value of the Identifier, not the Identifier itself | |
| historicalMobileLocationFinder.setDeviceId(device.id.value as Long) | |
| def start = new Date() - daysBack | |
| def end = new Date() | |
| historicalMobileLocationFinder.setStartDate(start) | |
| historicalMobileLocationFinder.setEndDate(end) | |
| List<MobileLocation> mobileLocations = historicalMobileLocationFinder.findAll() | |
| mobileLocations | |
| } | |
| def findDeviceById(Context CONTEXT, Long deviceId){ | |
| DeviceFinder deviceFinder = new DeviceFinder(CONTEXT, new Identifier(deviceId)); | |
| Device device = deviceFinder.find(); | |
| device | |
| } | |
| def findOrCreateLocation(Context CONTEXT, String locationName, String address, String city){ | |
| LocationFinder locationFinder = new LocationFinder(CONTEXT) | |
| locationFinder.setName(locationName) | |
| Location location = locationFinder.findOne() | |
| if (!location){ | |
| location = new Location(CONTEXT, locationName, address, city); | |
| location.store(); | |
| } | |
| location | |
| } | |
| def createMobileLocation(Context CONTEXT, com.axeda.drm.sdk.device.Device device, Double latitude, Double longitude, Double altitude, Date start, Date end){ | |
| MobileLocation mobileLocation = new MobileLocation(CONTEXT, latitude, longitude, altitude, start, end, device.id.value); | |
| mobileLocation.store() | |
| mobileLocation | |
| } | |
| def findOrCreateModel(Context CONTEXT, String modelName){ | |
| ModelFinder modelFinder = new ModelFinder(CONTEXT) | |
| modelFinder.setName(modelName) | |
| def model = modelFinder.find() | |
| if (!model){ | |
| model = new Model(CONTEXT, modelName); | |
| model.store(); | |
| } | |
| model | |
| } | |
| def findOrCreateSourceModelAssociation(Context CONTEXT, com.axeda.drm.sdk.device.Model source, com.axeda.drm.sdk.device.Model target) { | |
| ModelAssociationFinder modelAssociationFinder = new ModelAssociationFinder(CONTEXT) | |
| modelAssociationFinder.setDestinationId target.id | |
| ModelFinder modelFinder = new ModelFinder(CONTEXT) | |
| Model sourceModel | |
| def ids = modelAssociationFinder.getSourceIds() | |
| ids.each {Identifier id -> | |
| modelFinder.id = id | |
| Model sample = null | |
| try {sample = modelFinder.find()} catch (Exception e) {} | |
| if (sample != null) { | |
| sourceModel = sample | |
| } | |
| } | |
| if (ids == null || ids?.size == 0){ | |
| ModelAssociation modelAssociation = new ModelAssociation(CONTEXT, source?.id, target?.id) | |
| modelAssociation.store() | |
| modelAssociation = new ModelAssociation(CONTEXT, source?.id, source?.id) | |
| modelAssociation.store() | |
| } | |
| sourceModel | |
| } | |
| def getAssets(Context CONTEXT, com.axeda.drm.sdk.device.Model model, String dataItemName, String dataItemValue, String operatorTypeStr){ | |
| // strings have to be one of the following | |
| // "EQUAL_TO", "LESS_THAN", "NOT_EQUAL_TO", "GREATER_THAN", "GRT_THAN_EQUAL_TO", "LESS_THAN_EQUAL_TO" | |
| DeviceFinder deviceFinder = new DeviceFinder(CONTEXT) | |
| deviceFinder.setModel(model) | |
| deviceFinder.setDataItemName(dataItemName) | |
| deviceFinder.setDataItemValue(dataItemValue) | |
| deviceFinder.setDataItemOperator(new Operator(operatorTypeStr)) | |
| def assets = deviceFinder.findAll() | |
| assets | |
| } | |
| def addLocationToOrganization(Context CONTEXT, String organizationName, String locationName, String address, String city){ | |
| LocationFinder locationFinder = new LocationFinder(CONTEXT) | |
| locationFinder.setName(locationName) | |
| Location location = locationFinder.findOne() | |
| if (!location){ | |
| location = new Location(CONTEXT, locationName, address, city); | |
| location.store(); | |
| } | |
| Organization organization = new Organization(CONTEXT, organizationName); | |
| organization.addLocation(location); | |
| organization.store(); | |
| organization | |
| } | |
| def getPrivileges(Context CONTEXT, Collection includeWords, Collection excludeWords){ | |
| // include certain privileges with the following words | |
| def includes = includeWords && includeWords?.size() > 0 ? includeWords.join("|") : "add|view|alarm|extend|modify|device|file" | |
| // exclude certain privileges with the following words | |
| def excludes = excludeWords && excludeWords?.size() > 0 ? excludeWords.join("|") : "gas|user|tenant|partner|instruction|report" | |
| PrivilegeFinder privilegeFinder = new PrivilegeFinder(CONTEXT) | |
| def expr = /(?i)[a-z\-]*(${includes})[a-z\-]*/ | |
| def notexpr = /(?i)[a-z\-]*(${excludes})[a-z\-]*/ | |
| def requiredPrivileges = privilegeFinder.findAll().findAll{ | |
| it.name ==~ expr && !(it.name ==~ notexpr) | |
| }.unique() | |
| requiredPrivileges | |
| } | |
| def createRegions(Context CONTEXT, String parentName, String childName){ | |
| Region parentRegion = new Region(CONTEXT, parentName); | |
| parentRegion.store(); | |
| Region childRegion = new Region(CONTEXT, childName); | |
| childRegion.store(); | |
| parentRegion.addChild(childRegion); | |
| parentRegion.store(); | |
| parentRegion | |
| } | |
| def validateRequest(com.axeda.drm.sdk.scripto.Request request, String hostname, String filename, String body){ | |
| // default header host | |
| assert Request.headers.host == hostname | |
| // custom header with filename | |
| assert Request?.headers?.'Content-Disposition' == filename | |
| // input stream with POSTed file content | |
| assert Request.inputStream?.available() > 0 | |
| // body of POSTed text content as a string | |
| assert Request.body == body | |
| // parse the POSTed text content into a JSON object | |
| def slurper = new JsonSlurper() | |
| def requestJSON = slurper.parseText(Request.body) | |
| } | |
| def findOrCreateRuleAssociations(Context CONTEXT, Object entity, Object rule, String ruleType, String entityType){ | |
| // rule type is whether this is an expression rule | |
| def strRuleType = ruleType ?: "EXPRESSION_RULE" | |
| def strEntityType = entityType ?: "DEVICE_INCLUDE" | |
| def enumRuleType | |
| def enumEntityType | |
| try { | |
| enumRuleType = RuleType.valueOf(strRuleType) | |
| } | |
| catch (Exception e){ | |
| throw new Exception("Rule Type $ruleType is not valid") | |
| } | |
| try { | |
| enumEntityType = EntityType.valueOf(strEntityType) | |
| } | |
| catch (Exception e){ | |
| throw new Exception("Entity Type $entityType is not valid") | |
| } | |
| RuleAssociationFinder ruleAssociationFinder = new RuleAssociationFinder(CONTEXT) | |
| ruleAssociationFinder.setRuleId(rule.id.value) | |
| ruleAssociationFinder.setRuleType(enumRuleType) | |
| ruleAssociationFinder.setEntityId(entity.id.value) | |
| ruleAssociationFinder.setEntityType(enumEntityType) | |
| def ruleAssociations = ruleAssociationFinder.findAll() | |
| if (!ruleAssociations || ruleAssociations?.size() == 0){ | |
| def ruleAssociation = new RuleAssociation(CONTEXT) | |
| ruleAssociation.entityId = entity.id.value | |
| ruleAssociation.entityType = enumEntityType | |
| ruleAssociation.ruleType = enumRuleType | |
| ruleAssociation.setRuleId(rule.id.value) | |
| ruleAssociation.store() | |
| ruleAssociations = [ruleAssociation] | |
| } | |
| ruleAssociations | |
| } | |
| def findDevicesWithPartialName(Context CONTEXT, String partialName, com.axeda.drm.sdk.device.Model model){ | |
| DeviceFinder deviceFinder = new DeviceFinder(CONTEXT) | |
| if (model){ | |
| deviceFinder.setModel(model) | |
| } | |
| deviceFinder.setDeviceName(StringQuery.like("*${partialName}*")) | |
| List<Device> devices = deviceFinder.findAll() | |
| devices | |
| } | |
| def findUploadedFilesForDevice(Context CONTEXT, com.axeda.drm.sdk.device.Device device){ | |
| UploadedFileFinder uploadedFileFinder = new UploadedFileFinder(CONTEXT) | |
| uploadedFileFinder.device = device | |
| List<UploadedFile> uploadedFiles = uploadedFileFinder.findAll() | |
| uploadedFiles | |
| } | |
| def findOrCreateUser(Context CONTEXT, String username, String password, String email, UserGroup userGroup){ | |
| // be careful not to call this function using variables called username and password | |
| // those are reserved for the already-created calling user | |
| /* | |
| def user = findOrCreateUser( | |
| CONTEXT | |
| , parameters.user | |
| , parameters.pass | |
| , parameters.email | |
| , userGroup | |
| ) | |
| */ | |
| UserFinder userFinder = new UserFinder(CONTEXT) | |
| userFinder.setUsername(username) | |
| User user = userFinder.find() | |
| if (!user){ | |
| user = new User(CONTEXT, username, password, email); | |
| user.store(); | |
| } | |
| if (userGroup){ | |
| userGroup.addUser(user) | |
| userGroup.store(); | |
| } | |
| user | |
| } | |
| def findOrCreateUserGroup(Context CONTEXT, String userGroupName, String userGroupDescription, List<Privilege> requiredPrivileges, com.axeda.drm.sdk.device.DeviceGroup deviceGroup, boolean deviceGroupSecurity, boolean organizationSecurity, boolean locationSecurity, boolean regionSecurity){ | |
| UserGroupFinder userGroupFinder = new UserGroupFinder(CONTEXT) | |
| userGroupFinder.setName(userGroupName) | |
| UserGroup userGroup = userGroupFinder.find() | |
| if (!userGroup){ | |
| userGroup = new UserGroup(CONTEXT,userGroupName, userGroupDescription,null); | |
| userGroup.setDeviceGroupSecurity(deviceGroupSecurity); | |
| userGroup.setOrganizationSecurity(organizationSecurity); | |
| userGroup.setLocationSecurity(locationSecurity); | |
| userGroup.setRegionSecurity(regionSecurity); | |
| userGroup.store(); | |
| if (deviceGroup){ | |
| userGroup.addDeviceGroup(deviceGroup) | |
| } | |
| } | |
| if (requiredPrivileges && requiredPrivileges?.size() > 0){ | |
| requiredPrivileges.each{ priv -> | |
| userGroup.assignPrivilege(priv) | |
| } | |
| } | |
| userGroup.store(); | |
| return userGroup | |
| } | |
| def findRulesApplied(Context CONTEXT, Long entityId, String entityType){ | |
| entityType = entityType ?: "MODEL" | |
| RuleAssociationFinder ruleAssociationFinder = new RuleAssociationFinder(CONTEXT) | |
| ruleAssociationFinder.setEntityType(EntityType.valueOf(entityType)) | |
| ruleAssociationFinder.setEntityId(entityId) | |
| def ruleAssocs = ruleAssociationFinder.findAll() | |
| return ruleAssocs.inject([]){ target, ra -> | |
| ExpressionRuleFinder erf = new ExpressionRuleFinder(CONTEXT) | |
| erf.setId(new Identifier(ra.ruleId)) | |
| ExpressionRule expressionRule = erf.findOne() | |
| target << [ | |
| name: expressionRule.name | |
| , id: expressionRule.id.value | |
| , desc: expressionRule.description | |
| , ifExpr: expressionRule.ifExpression.toString() | |
| , thenExpr: expressionRule.thenExpression.toString() | |
| , enabled: expressionRule.enabled | |
| , consecutive: expressionRule.consecutive | |
| ] | |
| target | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment