Skip to content

Instantly share code, notes, and snippets.

@saranicole
Created July 15, 2014 14:07
Show Gist options
  • Select an option

  • Save saranicole/879d56e7cf277d763881 to your computer and use it in GitHub Desktop.

Select an option

Save saranicole/879d56e7cf277d763881 to your computer and use it in GitHub Desktop.
AccumulationDeletion
import com.axeda.drm.sdk.Context
import com.axeda.drm.sdk.device.DeviceGroupFinder
import com.axeda.platform.sdk.v1.services.ServiceFactory
import com.axeda.common.sdk.id.Identifier
import com.axeda.drm.sdk.data.HistoricalDataFinder
import com.axeda.drm.sdk.device.Device
import com.axeda.drm.sdk.device.Model
import com.axeda.drm.sdk.device.DeviceFinder
import net.sf.json.JSONObject
import com.axeda.platform.sdk.v1.services.data.DataAccumulatorService
import com.axeda.drm.sdk.device.DeviceGroup
import com.axeda.drm.sdk.device.DataItem
import com.axeda.drm.sdk.data.DataValue
import net.sf.json.JSONArray
import com.axeda.platform.sdk.v1.services.extobject.ExtendedObjectService
import com.axeda.platform.sdk.v1.services.extobject.ExtendedObjectSearchCriteria
final def response = [:]
final def timeProfiles = [:]
def scriptStartTime = new Date()
try {
final def CONTEXT = Context.create()
def calendar = Calendar.getInstance()
def startOfPreviousWeek = getStartOfPreviousWeek(calendar, new Date())
def serviceFactory = new ServiceFactory()
def eoSvc = serviceFactory.extendedObjectService
def daSvc = serviceFactory.dataAccumulatorService
def appState = fetchAppState(new ServiceFactory().extendedObjectService, "smv.v1.", "Smartvending")
def assetGroupFinder = new DeviceGroupFinder(CONTEXT)
assetGroupFinder.setId(new Identifier(appState.root_assetgroup_id as Long))
def rootGroup = assetGroupFinder.find()
def assetGroups = getAllChildAssetGroups(rootGroup)
assetGroups.each{ group ->
daSvc.listAccumulations(group.id.value).each { acc ->
def (name, timestamp) = acc.split(/_/);
if (timestamp.size() < 8)
logger.info(acc)
// daSvc.deleteAccumulation(acc, group.id.value)
}
}
}
catch (any) {
logger.error any.localizedMessage
response += [
error: any.getClass()?.canonicalName,
message: any.localizedMessage
]
}
return ['Content-Type': 'application/json', 'Content': JSONObject.fromObject(response).toString(2)];
private List<DeviceGroup> getAllChildAssetGroups(DeviceGroup parent) {
def result = [], visited = []
visited << parent
while(visited) {
def currentGroup = visited.remove(0) as DeviceGroup
currentGroup.deviceGroups.each { visited << it }
result << currentGroup
}
result
}
private Map<String, String> fetchAppState(ExtendedObjectService eoSvc, String namespace, String applicationName) {
def criteria = new ExtendedObjectSearchCriteria()
criteria.extendedObjectClassName = "com.axeda.customapp.v1.AppState"
criteria.extendedClientKey = (namespace + applicationName)
def queryResult = eoSvc.findExtendedObjects(criteria, -1, -1, null)
if (queryResult.size() == 1) {
return queryResult[0]?.properties?.inject([:]) { map, property ->
map << [(property.propertyType.name): property.value]
}
} else {
throw new Exception("Unable to fetch AppState for this application. Contact your administrator.")
}
}
private Date getStartOfPreviousWeek(Calendar calendar, Date currentDate) {
calendar.setTime(currentDate)
calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR) - 1)
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
calendar.getTime().clearTime()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment