|
|
|
@ -11,7 +11,7 @@
|
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
|
|
|
|
*/
|
|
|
|
@ -20,10 +20,6 @@ package org.coffeeking.api.util;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.wso2.carbon.context.CarbonContext;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
|
|
|
|
|
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse;
|
|
|
|
|
import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry;
|
|
|
|
@ -31,6 +27,10 @@ import org.wso2.carbon.analytics.dataservice.commons.SortByField;
|
|
|
|
|
import org.wso2.carbon.analytics.dataservice.core.AnalyticsDataServiceUtils;
|
|
|
|
|
import org.wso2.carbon.analytics.datasource.commons.Record;
|
|
|
|
|
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
|
|
|
|
|
import org.wso2.carbon.context.CarbonContext;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
@ -42,114 +42,108 @@ import java.util.Map;
|
|
|
|
|
*/
|
|
|
|
|
public class APIUtil {
|
|
|
|
|
|
|
|
|
|
private static Log log = LogFactory.getLog(APIUtil.class);
|
|
|
|
|
private static Log log = LogFactory.getLog(APIUtil.class);
|
|
|
|
|
|
|
|
|
|
public static String getAuthenticatedUser() {
|
|
|
|
|
PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
String username = threadLocalCarbonContext.getUsername();
|
|
|
|
|
String tenantDomain = threadLocalCarbonContext.getTenantDomain();
|
|
|
|
|
if (username.endsWith(tenantDomain)) {
|
|
|
|
|
return username.substring(0, username.lastIndexOf("@"));
|
|
|
|
|
}
|
|
|
|
|
return username;
|
|
|
|
|
}
|
|
|
|
|
public static String getAuthenticatedUser() {
|
|
|
|
|
PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
String username = threadLocalCarbonContext.getUsername();
|
|
|
|
|
String tenantDomain = threadLocalCarbonContext.getTenantDomain();
|
|
|
|
|
if (username.endsWith(tenantDomain)) {
|
|
|
|
|
return username.substring(0, username.lastIndexOf("@"));
|
|
|
|
|
}
|
|
|
|
|
return username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DeviceManagementProviderService getDeviceManagementService() {
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService =
|
|
|
|
|
(DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
|
|
|
|
|
if (deviceManagementProviderService == null) {
|
|
|
|
|
String msg = "Device Management service has not initialized.";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new IllegalStateException(msg);
|
|
|
|
|
}
|
|
|
|
|
return deviceManagementProviderService;
|
|
|
|
|
}
|
|
|
|
|
public static DeviceManagementProviderService getDeviceManagementService() {
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService =
|
|
|
|
|
(DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
|
|
|
|
|
if (deviceManagementProviderService == null) {
|
|
|
|
|
throw new IllegalStateException("Device Management service has not initialized");
|
|
|
|
|
}
|
|
|
|
|
return deviceManagementProviderService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DeviceAccessAuthorizationService getDeviceAccessAuthorizationService() {
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
DeviceAccessAuthorizationService deviceAccessAuthorizationService =
|
|
|
|
|
(DeviceAccessAuthorizationService) ctx.getOSGiService(DeviceAccessAuthorizationService.class, null);
|
|
|
|
|
if (deviceAccessAuthorizationService == null) {
|
|
|
|
|
String msg = "Device Authorization service has not initialized.";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new IllegalStateException(msg);
|
|
|
|
|
}
|
|
|
|
|
return deviceAccessAuthorizationService;
|
|
|
|
|
}
|
|
|
|
|
public static DeviceAccessAuthorizationService getDeviceAccessAuthorizationService() {
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
DeviceAccessAuthorizationService deviceAccessAuthorizationService =
|
|
|
|
|
(DeviceAccessAuthorizationService) ctx.getOSGiService(DeviceAccessAuthorizationService.class, null);
|
|
|
|
|
if (deviceAccessAuthorizationService == null) {
|
|
|
|
|
throw new IllegalStateException("Device Authorization service has not initialized");
|
|
|
|
|
}
|
|
|
|
|
return deviceAccessAuthorizationService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static AnalyticsDataAPI getAnalyticsDataAPI() {
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
AnalyticsDataAPI analyticsDataAPI =
|
|
|
|
|
(AnalyticsDataAPI) ctx.getOSGiService(AnalyticsDataAPI.class, null);
|
|
|
|
|
if (analyticsDataAPI == null) {
|
|
|
|
|
String msg = "Analytics api service has not initialized.";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new IllegalStateException(msg);
|
|
|
|
|
}
|
|
|
|
|
return analyticsDataAPI;
|
|
|
|
|
}
|
|
|
|
|
public static AnalyticsDataAPI getAnalyticsDataAPI() {
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
AnalyticsDataAPI analyticsDataAPI =
|
|
|
|
|
(AnalyticsDataAPI) ctx.getOSGiService(AnalyticsDataAPI.class, null);
|
|
|
|
|
if (analyticsDataAPI == null) {
|
|
|
|
|
throw new IllegalStateException("Analytics api service has not initialized");
|
|
|
|
|
}
|
|
|
|
|
return analyticsDataAPI;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<SensorRecord> getAllEventsForDevice(String tableName, String query,
|
|
|
|
|
List<SortByField> sortByFields) throws AnalyticsException {
|
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
|
AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI();
|
|
|
|
|
int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query);
|
|
|
|
|
if (eventCount == 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
List<SearchResultEntry> resultEntries = analyticsDataAPI.search(tenantId, tableName, query, 0, eventCount,
|
|
|
|
|
sortByFields);
|
|
|
|
|
List<String> recordIds = getRecordIds(resultEntries);
|
|
|
|
|
AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds);
|
|
|
|
|
Map<String, SensorRecord> sensorDatas = createSensorData(AnalyticsDataServiceUtils.listRecords(
|
|
|
|
|
analyticsDataAPI, response));
|
|
|
|
|
List<SensorRecord> sortedSensorData = getSortedSensorData(sensorDatas, resultEntries);
|
|
|
|
|
return sortedSensorData;
|
|
|
|
|
}
|
|
|
|
|
public static List<SensorRecord> getAllEventsForDevice(String tableName, String query,
|
|
|
|
|
List<SortByField> sortByFields) throws AnalyticsException {
|
|
|
|
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
|
AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI();
|
|
|
|
|
int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query);
|
|
|
|
|
if (eventCount == 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
List<SearchResultEntry> resultEntries = analyticsDataAPI.search(tenantId, tableName, query, 0, eventCount,
|
|
|
|
|
sortByFields);
|
|
|
|
|
List<String> recordIds = getRecordIds(resultEntries);
|
|
|
|
|
AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds);
|
|
|
|
|
Map<String, SensorRecord> sensorDatas = createSensorData(AnalyticsDataServiceUtils.listRecords(
|
|
|
|
|
analyticsDataAPI, response));
|
|
|
|
|
List<SensorRecord> sortedSensorData = getSortedSensorData(sensorDatas, resultEntries);
|
|
|
|
|
return sortedSensorData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<String> getRecordIds(List<SearchResultEntry> searchResults) {
|
|
|
|
|
List<String> ids = new ArrayList<>();
|
|
|
|
|
for (SearchResultEntry searchResult : searchResults) {
|
|
|
|
|
ids.add(searchResult.getId());
|
|
|
|
|
}
|
|
|
|
|
return ids;
|
|
|
|
|
}
|
|
|
|
|
private static List<String> getRecordIds(List<SearchResultEntry> searchResults) {
|
|
|
|
|
List<String> ids = new ArrayList<>();
|
|
|
|
|
for (SearchResultEntry searchResult : searchResults) {
|
|
|
|
|
ids.add(searchResult.getId());
|
|
|
|
|
}
|
|
|
|
|
return ids;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<SensorRecord> getSortedSensorData(Map<String, SensorRecord> sensorDatas,
|
|
|
|
|
List<SearchResultEntry> searchResults) {
|
|
|
|
|
List<SensorRecord> sortedRecords = new ArrayList<>();
|
|
|
|
|
for (SearchResultEntry searchResultEntry : searchResults) {
|
|
|
|
|
sortedRecords.add(sensorDatas.get(searchResultEntry.getId()));
|
|
|
|
|
}
|
|
|
|
|
return sortedRecords;
|
|
|
|
|
}
|
|
|
|
|
public static List<SensorRecord> getSortedSensorData(Map<String, SensorRecord> sensorDatas,
|
|
|
|
|
List<SearchResultEntry> searchResults) {
|
|
|
|
|
List<SensorRecord> sortedRecords = new ArrayList<>();
|
|
|
|
|
for (SearchResultEntry searchResultEntry : searchResults) {
|
|
|
|
|
sortedRecords.add(sensorDatas.get(searchResultEntry.getId()));
|
|
|
|
|
}
|
|
|
|
|
return sortedRecords;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates the SensorDatas from records.
|
|
|
|
|
*
|
|
|
|
|
* @param records the records
|
|
|
|
|
* @return the Map of SensorRecord <id, SensorRecord>
|
|
|
|
|
*/
|
|
|
|
|
public static Map<String, SensorRecord> createSensorData(List<Record> records) {
|
|
|
|
|
Map<String, SensorRecord> sensorDatas = new HashMap<>();
|
|
|
|
|
for (Record record : records) {
|
|
|
|
|
SensorRecord sensorData = createSensorData(record);
|
|
|
|
|
sensorDatas.put(sensorData.getId(), sensorData);
|
|
|
|
|
}
|
|
|
|
|
return sensorDatas;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Creates the SensorDatas from records.
|
|
|
|
|
*
|
|
|
|
|
* @param records the records
|
|
|
|
|
* @return the Map of SensorRecord <id, SensorRecord>
|
|
|
|
|
*/
|
|
|
|
|
public static Map<String, SensorRecord> createSensorData(List<Record> records) {
|
|
|
|
|
Map<String, SensorRecord> sensorDatas = new HashMap<>();
|
|
|
|
|
for (Record record : records) {
|
|
|
|
|
SensorRecord sensorData = createSensorData(record);
|
|
|
|
|
sensorDatas.put(sensorData.getId(), sensorData);
|
|
|
|
|
}
|
|
|
|
|
return sensorDatas;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a SensorRecord object out of a Record object
|
|
|
|
|
*
|
|
|
|
|
* @param record the record object
|
|
|
|
|
* @return SensorRecord object
|
|
|
|
|
*/
|
|
|
|
|
public static SensorRecord createSensorData(Record record) {
|
|
|
|
|
SensorRecord recordBean = new SensorRecord();
|
|
|
|
|
recordBean.setId(record.getId());
|
|
|
|
|
recordBean.setValues(record.getValues());
|
|
|
|
|
return recordBean;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Create a SensorRecord object out of a Record object
|
|
|
|
|
*
|
|
|
|
|
* @param record the record object
|
|
|
|
|
* @return SensorRecord object
|
|
|
|
|
*/
|
|
|
|
|
public static SensorRecord createSensorData(Record record) {
|
|
|
|
|
SensorRecord recordBean = new SensorRecord();
|
|
|
|
|
recordBean.setId(record.getId());
|
|
|
|
|
recordBean.setValues(record.getValues());
|
|
|
|
|
return recordBean;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|