Commit 8ce022b03cc6effda8c6b19fbeffc2f4822636af

Authored by Vladyslav_Prykhodko
Committed by Andrew Shvayka
1 parent 65032092

UI: Add support RPC request persistent parameter

@@ -18,8 +18,8 @@ @@ -18,8 +18,8 @@
18 "resources": [], 18 "resources": [],
19 "templateHtml": "<div style=\"height: 100%; overflow-y: auto;\" id=\"device-terminal\"></div>", 19 "templateHtml": "<div style=\"height: 100%; overflow-y: auto;\" id=\"device-terminal\"></div>",
20 "templateCss": ".cmd .cursor.blink {\n -webkit-animation-name: terminal-underline;\n -moz-animation-name: terminal-underline;\n -ms-animation-name: terminal-underline;\n animation-name: terminal-underline;\n}\n.terminal .inverted, .cmd .inverted {\n border-bottom-color: #aaa;\n}\n\n", 20 "templateCss": ".cmd .cursor.blink {\n -webkit-animation-name: terminal-underline;\n -moz-animation-name: terminal-underline;\n -ms-animation-name: terminal-underline;\n animation-name: terminal-underline;\n}\n.terminal .inverted, .cmd .inverted {\n border-bottom-color: #aaa;\n}\n\n",
21 - "controllerScript": "var requestTimeout = 500;\n\nself.onInit = function() {\n var subscription = self.ctx.defaultSubscription;\n var rpcEnabled = subscription.rpcEnabled;\n var deviceName = 'Simulated';\n var prompt;\n if (subscription.targetDeviceName && subscription.targetDeviceName.length) {\n deviceName = subscription.targetDeviceName;\n }\n if (self.ctx.settings.requestTimeout) {\n requestTimeout = self.ctx.settings.requestTimeout;\n }\n var greetings = 'Welcome to ThingsBoard RPC debug terminal.\\n\\n';\n if (!rpcEnabled) {\n greetings += 'Target device is not set!\\n\\n';\n prompt = '';\n } else {\n greetings += 'Current target device for RPC commands: [[b;#fff;]' + deviceName + ']\\n\\n';\n greetings += 'Please type [[b;#fff;]\\'help\\'] to see usage.\\n';\n prompt = '[[b;#8bc34a;]' + deviceName +']> ';\n }\n \n var terminal = $('#device-terminal', self.ctx.$container).terminal(\n function(command) {\n if (command !== '') {\n try {\n var localCommand = command.trim();\n var requestUUID = uuidv4();\n if (localCommand === 'help') {\n printUsage(this);\n } else {\n var spaceIndex = localCommand.indexOf(' ');\n if (spaceIndex === -1 && !localCommand.length) {\n this.error(\"Wrong number of arguments!\");\n this.echo(' ');\n } else {\n var params;\n if (spaceIndex === -1) {\n spaceIndex = localCommand.length;\n }\n var name = localCommand.substr(0, spaceIndex);\n var args = localCommand.substr(spaceIndex + 1);\n if (args.length) {\n try {\n params = JSON.parse(args);\n } catch (e) {\n params = args;\n }\n }\n performRpc(this, name, params, requestUUID);\n }\n }\n } catch(e) {\n this.error(new String(e));\n }\n } else {\n this.echo('');\n }\n }, {\n greetings: greetings,\n prompt: prompt,\n enabled: rpcEnabled\n });\n \n if (!rpcEnabled) {\n terminal.error('No RPC target detected!').pause();\n }\n}\n\n\nfunction printUsage(terminal) {\n var commandsListText = '\\n[[b;#fff;]Usage:]\\n';\n commandsListText += ' <method> [params body]]\\n\\n';\n commandsListText += '[[b;#fff;]Example 1:]\\n'; \n commandsListText += ' myRemoteMethod1 myText\\n\\n'; \n commandsListText += '[[b;#fff;]Example 2:]\\n'; \n commandsListText += ' myOtherRemoteMethod \"{\\\\\"key1\\\\\": 2, \\\\\"key2\\\\\": \\\\\"myVal\\\\\"}\"\\n'; \n terminal.echo(new String(commandsListText));\n}\n\n\nfunction performRpc(terminal, method, params, requestUUID) {\n terminal.pause();\n self.ctx.controlApi.sendTwoWayCommand(method, params, requestTimeout, requestUUID).subscribe(\n function success(responseBody) {\n terminal.echo(JSON.stringify(responseBody));\n terminal.echo(' ');\n terminal.resume();\n },\n function fail() {\n var errorText = self.ctx.defaultSubscription.rpcErrorText;\n terminal.error(errorText);\n terminal.echo(' ');\n terminal.resume();\n }\n );\n}\n\n\nfunction uuidv4() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);\n return v.toString(16);\n });\n}\n\n \nself.onDestroy = function() {\n}",  
22 - "settingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"Settings\",\n \"properties\": {\n \"requestTimeout\": {\n \"title\": \"RPC request timeout (ms)\",\n \"type\": \"number\",\n \"default\": 500\n }\n },\n \"required\": [\"requestTimeout\"]\n },\n \"form\": [\n \"requestTimeout\"\n ]\n}", 21 + "controllerScript": "var requestTimeout = 500;\nvar requestPersistent = false;\n\nself.onInit = function() {\n var subscription = self.ctx.defaultSubscription;\n var rpcEnabled = subscription.rpcEnabled;\n var deviceName = 'Simulated';\n var prompt;\n if (subscription.targetDeviceName && subscription.targetDeviceName.length) {\n deviceName = subscription.targetDeviceName;\n }\n if (self.ctx.settings.requestTimeout) {\n requestTimeout = self.ctx.settings.requestTimeout;\n }\n if (self.ctx.settings.requestPersistent) {\n requestPersistent = self.ctx.settings.requestPersistent;\n }\n var greetings = 'Welcome to ThingsBoard RPC debug terminal.\\n\\n';\n if (!rpcEnabled) {\n greetings += 'Target device is not set!\\n\\n';\n prompt = '';\n } else {\n greetings += 'Current target device for RPC commands: [[b;#fff;]' + deviceName + ']\\n\\n';\n greetings += 'Please type [[b;#fff;]\\'help\\'] to see usage.\\n';\n prompt = '[[b;#8bc34a;]' + deviceName +']> ';\n }\n \n var terminal = $('#device-terminal', self.ctx.$container).terminal(\n function(command) {\n if (command !== '') {\n try {\n var localCommand = command.trim();\n var requestUUID = uuidv4();\n if (localCommand === 'help') {\n printUsage(this);\n } else {\n var spaceIndex = localCommand.indexOf(' ');\n if (spaceIndex === -1 && !localCommand.length) {\n this.error(\"Wrong number of arguments!\");\n this.echo(' ');\n } else {\n var params;\n if (spaceIndex === -1) {\n spaceIndex = localCommand.length;\n }\n var name = localCommand.substr(0, spaceIndex);\n var args = localCommand.substr(spaceIndex + 1);\n if (args.length) {\n try {\n params = JSON.parse(args);\n } catch (e) {\n params = args;\n }\n }\n performRpc(this, name, params, requestUUID);\n }\n }\n } catch(e) {\n this.error(new String(e));\n }\n } else {\n this.echo('');\n }\n }, {\n greetings: greetings,\n prompt: prompt,\n enabled: rpcEnabled\n });\n \n if (!rpcEnabled) {\n terminal.error('No RPC target detected!').pause();\n }\n}\n\n\nfunction printUsage(terminal) {\n var commandsListText = '\\n[[b;#fff;]Usage:]\\n';\n commandsListText += ' <method> [params body]]\\n\\n';\n commandsListText += '[[b;#fff;]Example 1:]\\n'; \n commandsListText += ' myRemoteMethod1 myText\\n\\n'; \n commandsListText += '[[b;#fff;]Example 2:]\\n'; \n commandsListText += ' myOtherRemoteMethod \"{\\\\\"key1\\\\\": 2, \\\\\"key2\\\\\": \\\\\"myVal\\\\\"}\"\\n'; \n terminal.echo(new String(commandsListText));\n}\n\n\nfunction performRpc(terminal, method, params, requestUUID) {\n terminal.pause();\n self.ctx.controlApi.sendTwoWayCommand(method, params, requestTimeout, requestPersistent, requestUUID).subscribe(\n function success(responseBody) {\n terminal.echo(JSON.stringify(responseBody));\n terminal.echo(' ');\n terminal.resume();\n },\n function fail() {\n var errorText = self.ctx.defaultSubscription.rpcErrorText;\n terminal.error(errorText);\n terminal.echo(' ');\n terminal.resume();\n }\n );\n}\n\n\nfunction uuidv4() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\n var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);\n return v.toString(16);\n });\n}\n\n \nself.onDestroy = function() {\n}",
  22 + "settingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"Settings\",\n \"properties\": {\n \"requestTimeout\": {\n \"title\": \"RPC request timeout (ms)\",\n \"type\": \"number\",\n \"default\": 500\n },\n \"requestPersistent\": {\n \"title\": \"RPC request persistent\",\n \"type\": \"boolean\",\n \"default\": false\n }\n },\n \"required\": [\"requestTimeout\"]\n },\n \"form\": [\n \"requestTimeout\",\n \"requestPersistent\"\n ]\n}",
23 "dataKeySettingsSchema": "{}\n", 23 "dataKeySettingsSchema": "{}\n",
24 "defaultConfig": "{\"targetDeviceAliases\":[],\"showTitle\":true,\"backgroundColor\":\"#010101\",\"color\":\"rgba(255, 254, 254, 0.87)\",\"padding\":\"0px\",\"settings\":{\"parseGpioStatusFunction\":\"return body[pin] === true;\",\"gpioStatusChangeRequest\":{\"method\":\"setGpioStatus\",\"paramsBody\":\"{\\n \\\"pin\\\": \\\"{$pin}\\\",\\n \\\"enabled\\\": \\\"{$enabled}\\\"\\n}\"},\"requestTimeout\":500,\"switchPanelBackgroundColor\":\"#b71c1c\",\"gpioStatusRequest\":{\"method\":\"getGpioStatus\",\"paramsBody\":\"{}\"},\"gpioList\":[{\"pin\":1,\"label\":\"GPIO 1\",\"row\":0,\"col\":0,\"_uniqueKey\":0},{\"pin\":2,\"label\":\"GPIO 2\",\"row\":0,\"col\":1,\"_uniqueKey\":1},{\"pin\":3,\"label\":\"GPIO 3\",\"row\":1,\"col\":0,\"_uniqueKey\":2}]},\"title\":\"RPC debug terminal\",\"dropShadow\":true,\"enableFullscreen\":true,\"widgetStyle\":{},\"titleStyle\":{\"fontSize\":\"16px\",\"fontWeight\":400},\"useDashboardTimewindow\":true,\"showLegend\":false,\"actions\":{}}" 24 "defaultConfig": "{\"targetDeviceAliases\":[],\"showTitle\":true,\"backgroundColor\":\"#010101\",\"color\":\"rgba(255, 254, 254, 0.87)\",\"padding\":\"0px\",\"settings\":{\"parseGpioStatusFunction\":\"return body[pin] === true;\",\"gpioStatusChangeRequest\":{\"method\":\"setGpioStatus\",\"paramsBody\":\"{\\n \\\"pin\\\": \\\"{$pin}\\\",\\n \\\"enabled\\\": \\\"{$enabled}\\\"\\n}\"},\"requestTimeout\":500,\"switchPanelBackgroundColor\":\"#b71c1c\",\"gpioStatusRequest\":{\"method\":\"getGpioStatus\",\"paramsBody\":\"{}\"},\"gpioList\":[{\"pin\":1,\"label\":\"GPIO 1\",\"row\":0,\"col\":0,\"_uniqueKey\":0},{\"pin\":2,\"label\":\"GPIO 2\",\"row\":0,\"col\":1,\"_uniqueKey\":1},{\"pin\":3,\"label\":\"GPIO 3\",\"row\":1,\"col\":0,\"_uniqueKey\":2}]},\"title\":\"RPC debug terminal\",\"dropShadow\":true,\"enableFullscreen\":true,\"widgetStyle\":{},\"titleStyle\":{\"fontSize\":\"16px\",\"fontWeight\":400},\"useDashboardTimewindow\":true,\"showLegend\":false,\"actions\":{}}"
25 } 25 }
@@ -69,8 +69,8 @@ export interface WidgetSubscriptionApi { @@ -69,8 +69,8 @@ export interface WidgetSubscriptionApi {
69 } 69 }
70 70
71 export interface RpcApi { 71 export interface RpcApi {
72 - sendOneWayCommand: (method: string, params?: any, timeout?: number, requestUUID?: string) => Observable<any>;  
73 - sendTwoWayCommand: (method: string, params?: any, timeout?: number, requestUUID?: string) => Observable<any>; 72 + sendOneWayCommand: (method: string, params?: any, timeout?: number, persistent?: boolean, requestUUID?: string) => Observable<any>;
  73 + sendTwoWayCommand: (method: string, params?: any, timeout?: number, persistent?: boolean, requestUUID?: string) => Observable<any>;
74 } 74 }
75 75
76 export interface IWidgetUtils { 76 export interface IWidgetUtils {
@@ -299,8 +299,8 @@ export interface IWidgetSubscription { @@ -299,8 +299,8 @@ export interface IWidgetSubscription {
299 onResetTimewindow(): void; 299 onResetTimewindow(): void;
300 updateTimewindowConfig(newTimewindow: Timewindow): void; 300 updateTimewindowConfig(newTimewindow: Timewindow): void;
301 301
302 - sendOneWayCommand(method: string, params?: any, timeout?: number, requestUUID?: string): Observable<any>;  
303 - sendTwoWayCommand(method: string, params?: any, timeout?: number, requestUUID?: string): Observable<any>; 302 + sendOneWayCommand(method: string, params?: any, timeout?: number, persistent?: boolean, requestUUID?: string): Observable<any>;
  303 + sendTwoWayCommand(method: string, params?: any, timeout?: number, persistent?: boolean, requestUUID?: string): Observable<any>;
304 clearRpcError(): void; 304 clearRpcError(): void;
305 305
306 subscribe(): void; 306 subscribe(): void;
@@ -644,12 +644,12 @@ export class WidgetSubscription implements IWidgetSubscription { @@ -644,12 +644,12 @@ export class WidgetSubscription implements IWidgetSubscription {
644 } 644 }
645 } 645 }
646 646
647 - sendOneWayCommand(method: string, params?: any, timeout?: number, requestUUID?: string): Observable<any> {  
648 - return this.sendCommand(true, method, params, timeout, requestUUID); 647 + sendOneWayCommand(method: string, params?: any, timeout?: number, persistent?: boolean, requestUUID?: string): Observable<any> {
  648 + return this.sendCommand(true, method, params, timeout, persistent, requestUUID);
649 } 649 }
650 650
651 - sendTwoWayCommand(method: string, params?: any, timeout?: number, requestUUID?: string): Observable<any> {  
652 - return this.sendCommand(false, method, params, timeout, requestUUID); 651 + sendTwoWayCommand(method: string, params?: any, timeout?: number, persistent?: boolean, requestUUID?: string): Observable<any> {
  652 + return this.sendCommand(false, method, params, timeout, persistent, requestUUID);
653 } 653 }
654 654
655 clearRpcError(): void { 655 clearRpcError(): void {
@@ -658,7 +658,8 @@ export class WidgetSubscription implements IWidgetSubscription { @@ -658,7 +658,8 @@ export class WidgetSubscription implements IWidgetSubscription {
658 this.callbacks.onRpcErrorCleared(this); 658 this.callbacks.onRpcErrorCleared(this);
659 } 659 }
660 660
661 - sendCommand(oneWayElseTwoWay: boolean, method: string, params?: any, timeout?: number, requestUUID?: string): Observable<any> { 661 + sendCommand(oneWayElseTwoWay: boolean, method: string, params?: any,
  662 + timeout?: number, persistent?: boolean, requestUUID?: string): Observable<any> {
662 if (!this.rpcEnabled) { 663 if (!this.rpcEnabled) {
663 return throwError(new Error('Rpc disabled!')); 664 return throwError(new Error('Rpc disabled!'));
664 } else { 665 } else {
@@ -670,6 +671,7 @@ export class WidgetSubscription implements IWidgetSubscription { @@ -670,6 +671,7 @@ export class WidgetSubscription implements IWidgetSubscription {
670 const requestBody: any = { 671 const requestBody: any = {
671 method, 672 method,
672 params, 673 params,
  674 + persistent,
673 requestUUID 675 requestUUID
674 }; 676 };
675 if (timeout && timeout > 0) { 677 if (timeout && timeout > 0) {
@@ -189,16 +189,16 @@ export class WidgetContext { @@ -189,16 +189,16 @@ export class WidgetContext {
189 }; 189 };
190 190
191 controlApi: RpcApi = { 191 controlApi: RpcApi = {
192 - sendOneWayCommand: (method, params, timeout, requestUUID) => { 192 + sendOneWayCommand: (method, params, timeout, persistent, requestUUID) => {
193 if (this.defaultSubscription) { 193 if (this.defaultSubscription) {
194 - return this.defaultSubscription.sendOneWayCommand(method, params, timeout, requestUUID); 194 + return this.defaultSubscription.sendOneWayCommand(method, params, timeout, persistent, requestUUID);
195 } else { 195 } else {
196 return of(null); 196 return of(null);
197 } 197 }
198 }, 198 },
199 - sendTwoWayCommand: (method, params, timeout, requestUUID) => { 199 + sendTwoWayCommand: (method, params, timeout, persistent, requestUUID) => {
200 if (this.defaultSubscription) { 200 if (this.defaultSubscription) {
201 - return this.defaultSubscription.sendTwoWayCommand(method, params, timeout, requestUUID); 201 + return this.defaultSubscription.sendTwoWayCommand(method, params, timeout, persistent, requestUUID);
202 } else { 202 } else {
203 return of(null); 203 return of(null);
204 } 204 }
@@ -126,7 +126,7 @@ export const timewindowCompletion: TbEditorCompletion = { @@ -126,7 +126,7 @@ export const timewindowCompletion: TbEditorCompletion = {
126 } 126 }
127 } 127 }
128 } 128 }
129 -} 129 +};
130 130
131 export const widgetContextCompletions: TbEditorCompletions = { 131 export const widgetContextCompletions: TbEditorCompletions = {
132 ctx: { 132 ctx: {
@@ -465,6 +465,12 @@ export const widgetContextCompletions: TbEditorCompletions = { @@ -465,6 +465,12 @@ export const widgetContextCompletions: TbEditorCompletions = {
465 description: 'Maximum delay in milliseconds to wait until response/acknowledgement is received.', 465 description: 'Maximum delay in milliseconds to wait until response/acknowledgement is received.',
466 type: 'number', 466 type: 'number',
467 optional: true 467 optional: true
  468 + },
  469 + {
  470 + name: 'persistent',
  471 + description: 'RPC request persistent',
  472 + type: 'boolean',
  473 + optional: true
468 } 474 }
469 ], 475 ],
470 return: { 476 return: {
@@ -492,6 +498,12 @@ export const widgetContextCompletions: TbEditorCompletions = { @@ -492,6 +498,12 @@ export const widgetContextCompletions: TbEditorCompletions = {
492 description: 'Maximum delay in milliseconds to wait until response/acknowledgement is received.', 498 description: 'Maximum delay in milliseconds to wait until response/acknowledgement is received.',
493 type: 'number', 499 type: 'number',
494 optional: true 500 optional: true
  501 + },
  502 + {
  503 + name: 'persistent',
  504 + description: 'RPC request persistent',
  505 + type: 'boolean',
  506 + optional: true
495 } 507 }
496 ], 508 ],
497 return: { 509 return: {
@@ -657,4 +669,4 @@ export const widgetContextCompletions: TbEditorCompletions = { @@ -657,4 +669,4 @@ export const widgetContextCompletions: TbEditorCompletions = {
657 ...serviceCompletions 669 ...serviceCompletions
658 } 670 }
659 } 671 }
660 -} 672 +};