|
|
|
@ -302,11 +302,12 @@ public class VirtualFireAlarmService {
|
|
|
|
|
@GET
|
|
|
|
|
@Produces("application/octet-stream")
|
|
|
|
|
public Response downloadSketch(@QueryParam("owner") String owner,
|
|
|
|
|
@QueryParam("deviceName") String customDeviceName,
|
|
|
|
|
@PathParam("sketch_type") String sketchType) {
|
|
|
|
|
|
|
|
|
|
ZipArchive zipFile = null;
|
|
|
|
|
try {
|
|
|
|
|
zipFile = createDownloadFile(owner, sketchType);
|
|
|
|
|
zipFile = createDownloadFile(owner, customDeviceName, sketchType);
|
|
|
|
|
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
|
|
|
|
rb.header("Content-Disposition",
|
|
|
|
|
"attachment; filename=\"" + zipFile.getFileName() + "\"");
|
|
|
|
@ -326,11 +327,12 @@ public class VirtualFireAlarmService {
|
|
|
|
|
@Path("manager/device/{sketch_type}/generate_link")
|
|
|
|
|
@GET
|
|
|
|
|
public Response generateSketchLink(@QueryParam("owner") String owner,
|
|
|
|
|
@QueryParam("deviceName") String customDeviceName,
|
|
|
|
|
@PathParam("sketch_type") String sketchType) {
|
|
|
|
|
|
|
|
|
|
ZipArchive zipFile = null;
|
|
|
|
|
try {
|
|
|
|
|
zipFile = createDownloadFile(owner, sketchType);
|
|
|
|
|
zipFile = createDownloadFile(owner, customDeviceName, sketchType);
|
|
|
|
|
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
|
|
|
|
return rb.build();
|
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
@ -345,7 +347,7 @@ public class VirtualFireAlarmService {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ZipArchive createDownloadFile(String owner, String sketchType)
|
|
|
|
|
private ZipArchive createDownloadFile(String owner, String customDeviceName, String sketchType)
|
|
|
|
|
throws DeviceManagementException, AccessTokenException, DeviceControllerException {
|
|
|
|
|
if (owner == null) {
|
|
|
|
|
throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!");
|
|
|
|
@ -356,7 +358,6 @@ public class VirtualFireAlarmService {
|
|
|
|
|
|
|
|
|
|
TokenClient accessTokenClient = new TokenClient(VirtualFireAlarmConstants.DEVICE_TYPE);
|
|
|
|
|
AccessTokenInfo accessTokenInfo = null;
|
|
|
|
|
|
|
|
|
|
accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
|
|
|
|
|
|
|
|
|
|
//create token
|
|
|
|
@ -368,35 +369,30 @@ public class VirtualFireAlarmService {
|
|
|
|
|
newXmppAccount.setAccountName(owner + "_" + deviceId);
|
|
|
|
|
newXmppAccount.setUsername(deviceId);
|
|
|
|
|
newXmppAccount.setPassword(accessToken);
|
|
|
|
|
|
|
|
|
|
String xmppEndPoint = XmppConfig.getInstance().getXmppControlQueue().getServerURL();
|
|
|
|
|
|
|
|
|
|
int indexOfChar = xmppEndPoint.lastIndexOf('/');
|
|
|
|
|
|
|
|
|
|
if (indexOfChar != -1) {
|
|
|
|
|
xmppEndPoint = xmppEndPoint.substring((indexOfChar + 1), xmppEndPoint.length());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newXmppAccount.setEmail(deviceId + "@wso2.com");
|
|
|
|
|
|
|
|
|
|
XmppServerClient xmppServerClient = new XmppServerClient();
|
|
|
|
|
xmppServerClient.initControlQueue();
|
|
|
|
|
boolean status;
|
|
|
|
|
|
|
|
|
|
if (XmppConfig.getInstance().isEnabled()) {
|
|
|
|
|
status = xmppServerClient.createXMPPAccount(newXmppAccount);
|
|
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
|
String msg =
|
|
|
|
|
"XMPP Account was not created for device - " + deviceId + " of owner - " +
|
|
|
|
|
owner +
|
|
|
|
|
". XMPP might have been disabled in org.wso2.carbon.device.mgt.iot.common.config.server.configs";
|
|
|
|
|
".XMPP might have been disabled in org.wso2.carbon.device.mgt.iot" +
|
|
|
|
|
".common.config.server.configs";
|
|
|
|
|
log.warn(msg);
|
|
|
|
|
throw new DeviceManagementException(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
status = register(deviceId, owner + "s_" + sketchType + "_" + deviceId.substring(0,
|
|
|
|
|
3),
|
|
|
|
|
owner);
|
|
|
|
|
|
|
|
|
|
//Register the device with CDMF
|
|
|
|
|
String deviceName = customDeviceName + "_" + deviceId;
|
|
|
|
|
status = register(deviceId, deviceName, owner);
|
|
|
|
|
// status = register(deviceId, owner + "s_" + sketchType + "_" + deviceId.substring(0, 3),
|
|
|
|
|
// owner);
|
|
|
|
|
if (!status) {
|
|
|
|
|
String msg = "Error occurred while registering the device with " + "id: " + deviceId
|
|
|
|
|
+ " owner:" + owner;
|
|
|
|
@ -406,8 +402,8 @@ public class VirtualFireAlarmService {
|
|
|
|
|
|
|
|
|
|
ZipUtil ziputil = new ZipUtil();
|
|
|
|
|
ZipArchive zipFile = null;
|
|
|
|
|
|
|
|
|
|
zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId, accessToken, refreshToken);
|
|
|
|
|
zipFile = ziputil.downloadSketch(owner, SUPER_TENANT, sketchType, deviceId, deviceName,
|
|
|
|
|
accessToken, refreshToken);
|
|
|
|
|
zipFile.setDeviceId(deviceId);
|
|
|
|
|
return zipFile;
|
|
|
|
|
}
|
|
|
|
@ -483,7 +479,8 @@ public class VirtualFireAlarmService {
|
|
|
|
|
String protocolString = protocol.toUpperCase();
|
|
|
|
|
String callUrlPattern = VirtualFireAlarmConstants.BULB_CONTEXT + switchToState;
|
|
|
|
|
|
|
|
|
|
log.info("Sending command: '" + callUrlPattern + "' to virtual-firealarm at: " + deviceIP + " " +
|
|
|
|
|
log.info("Sending command: '" + callUrlPattern + "' to virtual-firealarm at: " + deviceIP +
|
|
|
|
|
" " +
|
|
|
|
|
"via" + " " + protocolString);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
@ -492,11 +489,13 @@ public class VirtualFireAlarmService {
|
|
|
|
|
sendCommandViaHTTP(deviceIP, 9090, callUrlPattern, true);
|
|
|
|
|
break;
|
|
|
|
|
case MQTT_PROTOCOL:
|
|
|
|
|
sendCommandViaMQTT(owner, deviceId, VirtualFireAlarmConstants.BULB_CONTEXT.replace("/", ""),
|
|
|
|
|
sendCommandViaMQTT(owner, deviceId,
|
|
|
|
|
VirtualFireAlarmConstants.BULB_CONTEXT.replace("/", ""),
|
|
|
|
|
switchToState);
|
|
|
|
|
break;
|
|
|
|
|
case XMPP_PROTOCOL:
|
|
|
|
|
sendCommandViaXMPP(owner, deviceId, VirtualFireAlarmConstants.BULB_CONTEXT, switchToState);
|
|
|
|
|
sendCommandViaXMPP(owner, deviceId, VirtualFireAlarmConstants.BULB_CONTEXT,
|
|
|
|
|
switchToState);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if (protocolString == null) {
|
|
|
|
@ -551,24 +550,34 @@ public class VirtualFireAlarmService {
|
|
|
|
|
try {
|
|
|
|
|
switch (protocol) {
|
|
|
|
|
case HTTP_PROTOCOL:
|
|
|
|
|
log.info("Sending request to read sonar value at : " + deviceIp + " via " + HTTP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaHTTP(deviceIp, 9090, VirtualFireAlarmConstants.SONAR_CONTEXT, false);
|
|
|
|
|
log.info("Sending request to read sonar value at : " + deviceIp + " via " +
|
|
|
|
|
HTTP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaHTTP(deviceIp, 9090,
|
|
|
|
|
VirtualFireAlarmConstants.SONAR_CONTEXT, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MQTT_PROTOCOL:
|
|
|
|
|
log.info("Sending request to read sonar value at : " + deviceIp + " via " + MQTT_PROTOCOL);
|
|
|
|
|
sendCommandViaMQTT(owner, deviceId, VirtualFireAlarmConstants.SONAR_CONTEXT.replace("/", ""), "");
|
|
|
|
|
log.info("Sending request to read sonar value at : " + deviceIp + " via " +
|
|
|
|
|
MQTT_PROTOCOL);
|
|
|
|
|
sendCommandViaMQTT(owner, deviceId,
|
|
|
|
|
VirtualFireAlarmConstants.SONAR_CONTEXT.replace("/", ""),
|
|
|
|
|
"");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case XMPP_PROTOCOL:
|
|
|
|
|
log.info("Sending request to read sonar value at : " + deviceIp + " via " + XMPP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaXMPP(owner, deviceId, VirtualFireAlarmConstants.SONAR_CONTEXT, ".");
|
|
|
|
|
log.info("Sending request to read sonar value at : " + deviceIp + " via " +
|
|
|
|
|
XMPP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaXMPP(owner, deviceId,
|
|
|
|
|
VirtualFireAlarmConstants.SONAR_CONTEXT, ".");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (protocol == null) {
|
|
|
|
|
log.info("Sending request to read sonar value at : " + deviceIp + " via " + HTTP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaHTTP(deviceIp, 9090, VirtualFireAlarmConstants.SONAR_CONTEXT, false);
|
|
|
|
|
log.info("Sending request to read sonar value at : " + deviceIp + " via " +
|
|
|
|
|
HTTP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaHTTP(deviceIp, 9090,
|
|
|
|
|
VirtualFireAlarmConstants.SONAR_CONTEXT,
|
|
|
|
|
false);
|
|
|
|
|
} else {
|
|
|
|
|
replyMsg = "Requested protocol '" + protocol + "' is not supported";
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
@ -621,24 +630,38 @@ public class VirtualFireAlarmService {
|
|
|
|
|
try {
|
|
|
|
|
switch (protocol) {
|
|
|
|
|
case HTTP_PROTOCOL:
|
|
|
|
|
log.info("Sending request to read virtual-firealarm-temperature at : " + deviceIp + " via " + HTTP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaHTTP(deviceIp, 9090, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false);
|
|
|
|
|
log.info("Sending request to read virtual-firealarm-temperature at : " +
|
|
|
|
|
deviceIp + " via " + HTTP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaHTTP(deviceIp, 9090,
|
|
|
|
|
VirtualFireAlarmConstants.TEMPERATURE_CONTEXT,
|
|
|
|
|
false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MQTT_PROTOCOL:
|
|
|
|
|
log.info("Sending request to read virtual-firealarm-temperature at : " + deviceIp + " via " + MQTT_PROTOCOL);
|
|
|
|
|
sendCommandViaMQTT(owner, deviceId, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT.replace("/", ""), "");
|
|
|
|
|
log.info("Sending request to read virtual-firealarm-temperature at : " +
|
|
|
|
|
deviceIp + " via " + MQTT_PROTOCOL);
|
|
|
|
|
sendCommandViaMQTT(owner, deviceId,
|
|
|
|
|
VirtualFireAlarmConstants.TEMPERATURE_CONTEXT.replace("/",
|
|
|
|
|
""),
|
|
|
|
|
"");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case XMPP_PROTOCOL:
|
|
|
|
|
log.info("Sending request to read virtual-firealarm-temperature at : " + deviceIp + " via " + XMPP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaXMPP(owner, deviceId, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, ".");
|
|
|
|
|
log.info("Sending request to read virtual-firealarm-temperature at : " +
|
|
|
|
|
deviceIp + " via " + XMPP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaXMPP(owner, deviceId,
|
|
|
|
|
VirtualFireAlarmConstants.TEMPERATURE_CONTEXT,
|
|
|
|
|
".");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (protocol == null) {
|
|
|
|
|
log.info("Sending request to read virtual-firealarm-temperature at : " + deviceIp + " via " + HTTP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaHTTP(deviceIp, 9090, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false);
|
|
|
|
|
log.info("Sending request to read virtual-firealarm-temperature at : " +
|
|
|
|
|
deviceIp + " via " + HTTP_PROTOCOL);
|
|
|
|
|
replyMsg = sendCommandViaHTTP(deviceIp, 9090,
|
|
|
|
|
VirtualFireAlarmConstants
|
|
|
|
|
.TEMPERATURE_CONTEXT,
|
|
|
|
|
false);
|
|
|
|
|
} else {
|
|
|
|
|
replyMsg = "Requested protocol '" + protocol + "' is not supported";
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
@ -676,7 +699,9 @@ public class VirtualFireAlarmService {
|
|
|
|
|
return;
|
|
|
|
|
} else if (!registeredIp.equals(deviceIp)) {
|
|
|
|
|
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " +
|
|
|
|
|
deviceId + " is already registered under some other IP. Re-registration " + "required");
|
|
|
|
|
deviceId +
|
|
|
|
|
" is already registered under some other IP. Re-registration " +
|
|
|
|
|
"required");
|
|
|
|
|
response.setStatus(Response.Status.CONFLICT.getStatusCode());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -686,7 +711,8 @@ public class VirtualFireAlarmService {
|
|
|
|
|
ctx.setTenantDomain(SUPER_TENANT, true);
|
|
|
|
|
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx
|
|
|
|
|
.getOSGiService(DeviceAnalyticsService.class, null);
|
|
|
|
|
Object metdaData[] = {dataMsg.owner, VirtualFireAlarmConstants.DEVICE_TYPE, dataMsg.deviceId,
|
|
|
|
|
Object metdaData[] =
|
|
|
|
|
{dataMsg.owner, VirtualFireAlarmConstants.DEVICE_TYPE, dataMsg.deviceId,
|
|
|
|
|
System.currentTimeMillis()};
|
|
|
|
|
Object payloadData[] = {temperature};
|
|
|
|
|
try {
|
|
|
|
@ -896,7 +922,9 @@ public class VirtualFireAlarmService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String responseMsg = "";
|
|
|
|
|
String urlString = VirtualFireAlarmConstants.URL_PREFIX + deviceIp + ":" + deviceServerPort + callUrlPattern;
|
|
|
|
|
String urlString =
|
|
|
|
|
VirtualFireAlarmConstants.URL_PREFIX + deviceIp + ":" + deviceServerPort +
|
|
|
|
|
callUrlPattern;
|
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug(urlString);
|
|
|
|
|