committed by
GitHub
76 changed files with 1382 additions and 321 deletions
@ -0,0 +1,221 @@ |
|||
/** |
|||
* Copyright © 2016 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* 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 specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.mqtt.rpc; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.eclipse.paho.client.mqttv3.*; |
|||
import org.junit.Assert; |
|||
import org.junit.Before; |
|||
import org.junit.Ignore; |
|||
import org.junit.Test; |
|||
import org.springframework.http.HttpStatus; |
|||
import org.springframework.http.ResponseEntity; |
|||
import org.springframework.web.client.HttpClientErrorException; |
|||
import org.thingsboard.client.tools.RestClient; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|||
import org.thingsboard.server.mqtt.AbstractFeatureIntegrationTest; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
import static org.junit.Assert.assertEquals; |
|||
import static org.junit.Assert.assertNotNull; |
|||
|
|||
/** |
|||
* @author Valerii Sosliuk |
|||
*/ |
|||
@Slf4j |
|||
public class MqttServerSideRpcIntegrationTest extends AbstractFeatureIntegrationTest { |
|||
|
|||
private static final String MQTT_URL = "tcp://localhost:1883"; |
|||
private static final String BASE_URL = "http://localhost:8080"; |
|||
|
|||
private static final String USERNAME = "tenant@thingsboard.org"; |
|||
private static final String PASSWORD = "tenant"; |
|||
|
|||
private RestClient restClient; |
|||
|
|||
@Before |
|||
public void beforeTest() throws Exception { |
|||
restClient = new RestClient(BASE_URL); |
|||
restClient.login(USERNAME, PASSWORD); |
|||
} |
|||
|
|||
@Test |
|||
public void testServerMqttOneWayRpc() throws Exception { |
|||
Device device = new Device(); |
|||
device.setName("Test One-Way Server-Side RPC"); |
|||
Device savedDevice = getSavedDevice(device); |
|||
DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice); |
|||
assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId()); |
|||
String accessToken = deviceCredentials.getCredentialsId(); |
|||
assertNotNull(accessToken); |
|||
|
|||
String clientId = MqttAsyncClient.generateClientId(); |
|||
MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId); |
|||
|
|||
MqttConnectOptions options = new MqttConnectOptions(); |
|||
options.setUserName(accessToken); |
|||
client.connect(options); |
|||
Thread.sleep(3000); |
|||
client.subscribe("v1/devices/me/rpc/request/+", 1); |
|||
client.setCallback(new TestMqttCallback(client)); |
|||
|
|||
String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}"; |
|||
String deviceId = savedDevice.getId().getId().toString(); |
|||
ResponseEntity result = restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/oneway/" + deviceId, setGpioRequest, String.class); |
|||
Assert.assertEquals(HttpStatus.OK, result.getStatusCode()); |
|||
Assert.assertNull(result.getBody()); |
|||
} |
|||
|
|||
@Test |
|||
public void testServerMqttOneWayRpcDeviceOffline() throws Exception { |
|||
Device device = new Device(); |
|||
device.setName("Test One-Way Server-Side RPC Device Offline"); |
|||
Device savedDevice = getSavedDevice(device); |
|||
DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice); |
|||
assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId()); |
|||
String accessToken = deviceCredentials.getCredentialsId(); |
|||
assertNotNull(accessToken); |
|||
|
|||
String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}"; |
|||
String deviceId = savedDevice.getId().getId().toString(); |
|||
try { |
|||
restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/oneway/" + deviceId, setGpioRequest, String.class); |
|||
Assert.fail("HttpClientErrorException expected, but not encountered"); |
|||
} catch (HttpClientErrorException e) { |
|||
log.error(e.getMessage(), e); |
|||
Assert.assertEquals(HttpStatus.REQUEST_TIMEOUT, e.getStatusCode()); |
|||
Assert.assertEquals("408 null", e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
@Test |
|||
public void testServerMqttOneWayRpcDeviceDoesNotExist() throws Exception { |
|||
String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}"; |
|||
String nonExistentDeviceId = UUID.randomUUID().toString(); |
|||
try { |
|||
restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/oneway/" + nonExistentDeviceId, setGpioRequest, String.class); |
|||
Assert.fail("HttpClientErrorException expected, but not encountered"); |
|||
} catch (HttpClientErrorException e) { |
|||
log.error(e.getMessage(), e); |
|||
Assert.assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode()); |
|||
Assert.assertEquals("400 null", e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
@Test |
|||
public void testServerMqttTwoWayRpc() throws Exception { |
|||
|
|||
Device device = new Device(); |
|||
device.setName("Test Two-Way Server-Side RPC"); |
|||
Device savedDevice = getSavedDevice(device); |
|||
DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice); |
|||
assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId()); |
|||
String accessToken = deviceCredentials.getCredentialsId(); |
|||
assertNotNull(accessToken); |
|||
|
|||
String clientId = MqttAsyncClient.generateClientId(); |
|||
MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, clientId); |
|||
|
|||
MqttConnectOptions options = new MqttConnectOptions(); |
|||
options.setUserName(accessToken); |
|||
client.connect(options); |
|||
Thread.sleep(3000); |
|||
client.subscribe("v1/devices/me/rpc/request/+", 1); |
|||
client.setCallback(new TestMqttCallback(client)); |
|||
|
|||
String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}"; |
|||
String deviceId = savedDevice.getId().getId().toString(); |
|||
String result = getStringResult(setGpioRequest, "twoway", deviceId); |
|||
Assert.assertEquals("{\"value1\":\"A\",\"value2\":\"B\"}", result); |
|||
} |
|||
|
|||
@Test |
|||
public void testServerMqttTwoWayRpcDeviceOffline() throws Exception { |
|||
Device device = new Device(); |
|||
device.setName("Test Two-Way Server-Side RPC Device Offline"); |
|||
Device savedDevice = getSavedDevice(device); |
|||
DeviceCredentials deviceCredentials = getDeviceCredentials(savedDevice); |
|||
assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId()); |
|||
String accessToken = deviceCredentials.getCredentialsId(); |
|||
assertNotNull(accessToken); |
|||
|
|||
String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}"; |
|||
String deviceId = savedDevice.getId().getId().toString(); |
|||
try { |
|||
restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/twoway/" + deviceId, setGpioRequest, String.class); |
|||
Assert.fail("HttpClientErrorException expected, but not encountered"); |
|||
} catch (HttpClientErrorException e) { |
|||
log.error(e.getMessage(), e); |
|||
Assert.assertEquals(HttpStatus.REQUEST_TIMEOUT, e.getStatusCode()); |
|||
Assert.assertEquals("408 null", e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
@Test |
|||
public void testServerMqttTwoWayRpcDeviceDoesNotExist() throws Exception { |
|||
String setGpioRequest = "{\"method\":\"setGpio\",\"params\":{\"pin\": \"23\",\"value\": 1}}"; |
|||
String nonExistentDeviceId = UUID.randomUUID().toString(); |
|||
try { |
|||
restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/oneway/" + nonExistentDeviceId, setGpioRequest, String.class); |
|||
Assert.fail("HttpClientErrorException expected, but not encountered"); |
|||
} catch (HttpClientErrorException e) { |
|||
log.error(e.getMessage(), e); |
|||
Assert.assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode()); |
|||
Assert.assertEquals("400 null", e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
private Device getSavedDevice(Device device) { |
|||
return restClient.getRestTemplate().postForEntity(BASE_URL + "/api/device", device, Device.class).getBody(); |
|||
} |
|||
|
|||
private DeviceCredentials getDeviceCredentials(Device savedDevice) { |
|||
return restClient.getRestTemplate().getForEntity(BASE_URL + "/api/device/" + savedDevice.getId().getId().toString() + "/credentials", DeviceCredentials.class).getBody(); |
|||
} |
|||
|
|||
private String getStringResult(String requestData, String callType, String deviceId) { |
|||
return restClient.getRestTemplate().postForEntity(BASE_URL + "api/plugins/rpc/" + callType + "/" + deviceId, requestData, String.class).getBody(); |
|||
} |
|||
|
|||
private static class TestMqttCallback implements MqttCallback { |
|||
|
|||
private final MqttAsyncClient client; |
|||
|
|||
TestMqttCallback(MqttAsyncClient client) { |
|||
this.client = client; |
|||
} |
|||
|
|||
@Override |
|||
public void connectionLost(Throwable throwable) { |
|||
} |
|||
|
|||
@Override |
|||
public void messageArrived(String requestTopic, MqttMessage mqttMessage) throws Exception { |
|||
log.info("Message Arrived: " + mqttMessage.getPayload().toString()); |
|||
MqttMessage message = new MqttMessage(); |
|||
String responseTopic = requestTopic.replace("request", "response"); |
|||
message.setPayload("{\"value1\":\"A\", \"value2\":\"B\"}".getBytes()); |
|||
client.publish(responseTopic, message); |
|||
} |
|||
|
|||
@Override |
|||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { |
|||
|
|||
} |
|||
} |
|||
} |
|||
File diff suppressed because one or more lines are too long
@ -0,0 +1,40 @@ |
|||
/* |
|||
* Copyright © 2016 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* 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 specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
export default angular.module('thingsboard.filters.keyboardShortcut', []) |
|||
.filter('keyboardShortcut', KeyboardShortcut) |
|||
.name; |
|||
|
|||
/*@ngInject*/ |
|||
function KeyboardShortcut($window) { |
|||
return function(str) { |
|||
if (!str) return; |
|||
var keys = str.split('-'); |
|||
var isOSX = /Mac OS X/.test($window.navigator.userAgent); |
|||
|
|||
var seperator = (!isOSX || keys.length > 2) ? '+' : ''; |
|||
|
|||
var abbreviations = { |
|||
M: isOSX ? '⌘' : 'Ctrl', |
|||
A: isOSX ? 'Option' : 'Alt', |
|||
S: 'Shift' |
|||
}; |
|||
|
|||
return keys.map(function(key, index) { |
|||
var last = index == keys.length - 1; |
|||
return last ? key : abbreviations[key]; |
|||
}).join(seperator); |
|||
}; |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
/* |
|||
* Copyright © 2016 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* 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 specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
export default angular.module('thingsboard.directives.mousepointMenu', []) |
|||
.directive('tbMousepointMenu', MousepointMenu) |
|||
.name; |
|||
|
|||
/*@ngInject*/ |
|||
function MousepointMenu() { |
|||
|
|||
var linker = function ($scope, $element, $attrs, RightClickContextMenu) { |
|||
|
|||
$scope.$mdOpenMousepointMenu = function($event){ |
|||
RightClickContextMenu.offsets = function(){ |
|||
var offset = $element.offset(); |
|||
var x = $event.pageX - offset.left; |
|||
var y = $event.pageY - offset.top; |
|||
|
|||
var offsets = { |
|||
left: x, |
|||
top: y |
|||
} |
|||
return offsets; |
|||
} |
|||
RightClickContextMenu.open($event); |
|||
}; |
|||
|
|||
$scope.$mdCloseMousepointMenu = function() { |
|||
RightClickContextMenu.close(); |
|||
} |
|||
} |
|||
|
|||
return { |
|||
restrict: "A", |
|||
link: linker, |
|||
require: 'mdMenu' |
|||
}; |
|||
} |
|||
@ -0,0 +1,191 @@ |
|||
/* |
|||
* Copyright © 2016 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* 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 specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
import angularStorage from 'angular-storage'; |
|||
|
|||
export default angular.module('thingsboard.itembuffer', [angularStorage]) |
|||
.factory('itembuffer', ItemBuffer) |
|||
.factory('bufferStore', function(store) { |
|||
var newStore = store.getNamespacedStore('tbBufferStore', null, null, false); |
|||
return newStore; |
|||
}) |
|||
.name; |
|||
|
|||
/*@ngInject*/ |
|||
function ItemBuffer(bufferStore) { |
|||
|
|||
const WIDGET_ITEM = "widget_item"; |
|||
|
|||
var service = { |
|||
copyWidget: copyWidget, |
|||
hasWidget: hasWidget, |
|||
pasteWidget: pasteWidget, |
|||
addWidgetToDashboard: addWidgetToDashboard |
|||
} |
|||
|
|||
return service; |
|||
|
|||
/** |
|||
aliasesInfo { |
|||
datasourceAliases: { |
|||
datasourceIndex: { |
|||
aliasName: "...", |
|||
deviceId: "..." |
|||
} |
|||
} |
|||
targetDeviceAliases: { |
|||
targetDeviceAliasIndex: { |
|||
aliasName: "...", |
|||
deviceId: "..." |
|||
} |
|||
} |
|||
.... |
|||
} |
|||
**/ |
|||
|
|||
function copyWidget(widget, aliasesInfo, originalColumns) { |
|||
var widgetItem = { |
|||
widget: widget, |
|||
aliasesInfo: aliasesInfo, |
|||
originalColumns: originalColumns |
|||
} |
|||
bufferStore.set(WIDGET_ITEM, angular.toJson(widgetItem)); |
|||
} |
|||
|
|||
function hasWidget() { |
|||
return bufferStore.get(WIDGET_ITEM); |
|||
} |
|||
|
|||
function pasteWidget(targetDasgboard, position) { |
|||
var widgetItemJson = bufferStore.get(WIDGET_ITEM); |
|||
if (widgetItemJson) { |
|||
var widgetItem = angular.fromJson(widgetItemJson); |
|||
var widget = widgetItem.widget; |
|||
var aliasesInfo = widgetItem.aliasesInfo; |
|||
var originalColumns = widgetItem.originalColumns; |
|||
var targetRow = -1; |
|||
var targetColumn = -1; |
|||
if (position) { |
|||
targetRow = position.row; |
|||
targetColumn = position.column; |
|||
} |
|||
addWidgetToDashboard(targetDasgboard, widget, aliasesInfo, originalColumns, targetRow, targetColumn); |
|||
} |
|||
} |
|||
|
|||
function addWidgetToDashboard(dashboard, widget, aliasesInfo, originalColumns, row, column) { |
|||
var theDashboard; |
|||
if (dashboard) { |
|||
theDashboard = dashboard; |
|||
} else { |
|||
theDashboard = {}; |
|||
} |
|||
if (!theDashboard.configuration) { |
|||
theDashboard.configuration = {}; |
|||
} |
|||
if (!theDashboard.configuration.deviceAliases) { |
|||
theDashboard.configuration.deviceAliases = {}; |
|||
} |
|||
updateAliases(theDashboard, widget, aliasesInfo); |
|||
|
|||
if (!theDashboard.configuration.widgets) { |
|||
theDashboard.configuration.widgets = []; |
|||
} |
|||
var targetColumns = 24; |
|||
if (theDashboard.configuration.gridSettings && |
|||
theDashboard.configuration.gridSettings.columns) { |
|||
targetColumns = theDashboard.configuration.gridSettings.columns; |
|||
} |
|||
if (targetColumns != originalColumns) { |
|||
var ratio = targetColumns / originalColumns; |
|||
widget.sizeX *= ratio; |
|||
widget.sizeY *= ratio; |
|||
} |
|||
if (row > -1 && column > - 1) { |
|||
widget.row = row; |
|||
widget.col = column; |
|||
} else { |
|||
row = 0; |
|||
for (var w in theDashboard.configuration.widgets) { |
|||
var existingWidget = theDashboard.configuration.widgets[w]; |
|||
var wRow = existingWidget.row ? existingWidget.row : 0; |
|||
var wSizeY = existingWidget.sizeY ? existingWidget.sizeY : 1; |
|||
var bottom = wRow + wSizeY; |
|||
row = Math.max(row, bottom); |
|||
} |
|||
widget.row = row; |
|||
widget.col = 0; |
|||
} |
|||
theDashboard.configuration.widgets.push(widget); |
|||
return theDashboard; |
|||
} |
|||
|
|||
function updateAliases(dashboard, widget, aliasesInfo) { |
|||
var deviceAliases = dashboard.configuration.deviceAliases; |
|||
var aliasInfo; |
|||
var newAliasId; |
|||
for (var datasourceIndex in aliasesInfo.datasourceAliases) { |
|||
aliasInfo = aliasesInfo.datasourceAliases[datasourceIndex]; |
|||
newAliasId = getDeviceAliasId(deviceAliases, aliasInfo); |
|||
widget.config.datasources[datasourceIndex].deviceAliasId = newAliasId; |
|||
} |
|||
for (var targetDeviceAliasIndex in aliasesInfo.targetDeviceAliases) { |
|||
aliasInfo = aliasesInfo.targetDeviceAliases[targetDeviceAliasIndex]; |
|||
newAliasId = getDeviceAliasId(deviceAliases, aliasInfo); |
|||
widget.config.targetDeviceAliasIds[targetDeviceAliasIndex] = newAliasId; |
|||
} |
|||
} |
|||
|
|||
function getDeviceAliasId(deviceAliases, aliasInfo) { |
|||
var newAliasId; |
|||
for (var aliasId in deviceAliases) { |
|||
if (deviceAliases[aliasId].deviceId === aliasInfo.deviceId) { |
|||
newAliasId = aliasId; |
|||
break; |
|||
} |
|||
} |
|||
if (!newAliasId) { |
|||
var newAliasName = createDeviceAliasName(deviceAliases, aliasInfo.aliasName); |
|||
newAliasId = 0; |
|||
for (aliasId in deviceAliases) { |
|||
newAliasId = Math.max(newAliasId, aliasId); |
|||
} |
|||
newAliasId++; |
|||
deviceAliases[newAliasId] = {alias: newAliasName, deviceId: aliasInfo.deviceId}; |
|||
} |
|||
return newAliasId; |
|||
} |
|||
|
|||
function createDeviceAliasName(deviceAliases, alias) { |
|||
var c = 0; |
|||
var newAlias = angular.copy(alias); |
|||
var unique = false; |
|||
while (!unique) { |
|||
unique = true; |
|||
for (var devAliasId in deviceAliases) { |
|||
var devAlias = deviceAliases[devAliasId]; |
|||
if (newAlias === devAlias.alias) { |
|||
c++; |
|||
newAlias = alias + c; |
|||
unique = false; |
|||
} |
|||
} |
|||
} |
|||
return newAlias; |
|||
} |
|||
|
|||
|
|||
} |
|||
Loading…
Reference in new issue