Skip to content

Commit

Permalink
add pnp column, add error for device method (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
YingXue authored Jul 24, 2020
1 parent 310d090 commit 003383b
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/app/api/dataTransforms/deviceSummaryTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('utils', () => {
DeviceId: 'test',
IotEdge: false,
LastActivityTime: '2019-07-18T10:01:20.0568390Z',
ModelId: 'dtmi:com:example:Thermostat;1',
Status: 'Enabled',
StatusUpdatedTime: null,

Expand All @@ -28,6 +29,7 @@ describe('utils', () => {
deviceId: 'test',
iotEdge: false,
lastActivityTime: '3:01:20 AM, July 18, 2019',
modelId: 'dtmi:com:example:Thermostat;1',
status: 'Enabled',
statusUpdatedTime: null,
};
Expand All @@ -42,6 +44,7 @@ describe('utils', () => {
expect(transformedDevice.lastActivityTime.match(isLocalTime)).toBeTruthy();
expect(transformedDevice.status).toEqual(deviceSummary.status);
expect(transformedDevice.statusUpdatedTime).toEqual(deviceSummary.statusUpdatedTime);
expect(transformedDevice.modelId).toEqual(deviceSummary.modelId);
});
});
});
1 change: 1 addition & 0 deletions src/app/api/dataTransforms/deviceSummaryTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const transformDevice = (device: Device): DeviceSummary => {
deviceId: device.DeviceId,
iotEdge: device.IotEdge,
lastActivityTime: parseDateTimeString(device.LastActivityTime),
modelId: device.ModelId,
status: device.Status,
statusUpdatedTime: parseDateTimeString(device.StatusUpdatedTime)
};
Expand Down
1 change: 1 addition & 0 deletions src/app/api/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Device {
AuthenticationType: string;
IotEdge: boolean;
ConnectionState: string;
ModelId: string;
}

export interface DataPlaneResponse<T> {
Expand Down
1 change: 1 addition & 0 deletions src/app/api/models/deviceSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface DeviceSummary {
authenticationType: string;
connectionState: string;
iotEdge: boolean;
modelId: string;
}
21 changes: 6 additions & 15 deletions src/app/api/shared/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'jest';
import * as utils from './utils';
import { ParameterType, OperationType, DeviceCapability, DeviceStatus } from '../models/deviceQuery';
import { LIST_PLUG_AND_PLAY_DEVICES } from '../../constants/devices';
import { LIST_IOT_DEVICES } from '../../constants/devices';

describe('utils', () => {
it('builds query string', () => {
Expand All @@ -16,7 +16,7 @@ describe('utils', () => {
currentPageIndex: 1,
deviceId: 'device1',
}
)).toEqual(`${LIST_PLUG_AND_PLAY_DEVICES} WHERE STARTSWITH(devices.deviceId, 'device1')`);
)).toEqual(`${LIST_IOT_DEVICES} WHERE STARTSWITH(devices.deviceId, 'device1')`);

expect(utils.buildQueryString(
{
Expand All @@ -25,11 +25,11 @@ describe('utils', () => {
currentPageIndex: 1,
deviceId: '',
}
)).toEqual(LIST_PLUG_AND_PLAY_DEVICES + ' ');
)).toEqual(LIST_IOT_DEVICES + ' ');

expect(utils.buildQueryString(
null
)).toEqual(LIST_PLUG_AND_PLAY_DEVICES);
)).toEqual(LIST_IOT_DEVICES);

expect(utils.buildQueryString(
{
Expand All @@ -44,7 +44,7 @@ describe('utils', () => {
currentPageIndex: 1,
deviceId: '',
}
)).toEqual(`${LIST_PLUG_AND_PLAY_DEVICES} WHERE (${ParameterType.edge}=true)`);
)).toEqual(`${LIST_IOT_DEVICES} WHERE (${ParameterType.edge}=true)`);

expect(utils.buildQueryString(
{
Expand All @@ -59,7 +59,7 @@ describe('utils', () => {
currentPageIndex: 1,
deviceId: '',
}
)).toEqual(`${LIST_PLUG_AND_PLAY_DEVICES} WHERE (${ParameterType.status}='enabled')`);
)).toEqual(`${LIST_IOT_DEVICES} WHERE (${ParameterType.status}='enabled')`);
});

it('converts query object to string', () => {
Expand Down Expand Up @@ -110,15 +110,6 @@ describe('utils', () => {
expect(utils.escapeValue(`o'really`)).toEqual(`'o'really'`);
});

it('coverts pnp query clauses', () => {
expect(utils.toPnPClause(utils.PnPQueryPrefix.HAS_CAPABILITY_MODEL, 'urn:contoso:com:EnvironmentalSensor:1')).toEqual(
`HAS_CAPABILITYMODEL('urn:contoso:com:EnvironmentalSensor', 1)`
);
expect(utils.toPnPClause(utils.PnPQueryPrefix.HAS_INTERFACE, 'urn:contoso:com:EnvironmentalSensor')).toEqual(
`HAS_INTERFACE('urn:contoso:com:EnvironmentalSensor')`
);
});

it('gets connectionObject from hub connection string', () => {
const connectionObject = utils.getConnectionInfoFromConnectionString('HostName=test.azure-devices-int.net;SharedAccessKeyName=iothubowner;SharedAccessKey=key');
expect(connectionObject.hostName = 'test.azure-devices-int.net');
Expand Down
17 changes: 3 additions & 14 deletions src/app/api/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
**********************************************************/
import { createHmac } from 'crypto';
import { IoTHubConnectionSettings } from '../services/devicesService';
import { LIST_PLUG_AND_PLAY_DEVICES, SAS_EXPIRES_MINUTES } from '../../constants/devices';
import { LIST_IOT_DEVICES, SAS_EXPIRES_MINUTES } from '../../constants/devices';
import { DeviceQuery, QueryClause, ParameterType, OperationType } from '../models/deviceQuery';
import { MILLISECONDS_PER_SECOND, SECONDS_PER_MINUTE } from '../../constants/shared';

Expand Down Expand Up @@ -81,8 +81,8 @@ export const getConnectionInfoFromConnectionString = (connectionString: string):

export const buildQueryString = (query: DeviceQuery) => {
return query ?
`${LIST_PLUG_AND_PLAY_DEVICES} ${queryToString(query)}` :
LIST_PLUG_AND_PLAY_DEVICES;
`${LIST_IOT_DEVICES} ${queryToString(query)}` :
LIST_IOT_DEVICES;
};

export const queryToString = (query: DeviceQuery) => {
Expand Down Expand Up @@ -127,17 +127,6 @@ export const escapeValue = (value: string) => {
return value;
};

export const toPnPClause = (pnpFunctionName: string, value: string): string => {
const urnVersionRegEx = new RegExp(/:[0-9]+$/);
if (urnVersionRegEx.test(value)) {
const urnVersion = urnVersionRegEx.exec(value)[0].replace(':', '');
const urnName = value.replace(urnVersionRegEx, '');

return `${pnpFunctionName}(${escapeValue(urnName)}, ${urnVersion})`;
}
return `${pnpFunctionName}('${value}')`; // when provided value is not urn with version, pass it in as string
};

export const toEdgeClause = (edgeFunctionName: string, value: string): string => {
return `${edgeFunctionName}=${value === 'true' ? true : false}`;
};
Expand Down
5 changes: 3 additions & 2 deletions src/app/constants/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License
**********************************************************/
export const LIST_PLUG_AND_PLAY_DEVICES = `
export const LIST_IOT_DEVICES = `
SELECT deviceId as DeviceId,
status as Status,
lastActivityTime as LastActivityTime,
statusUpdatedTime as StatusUpdatedTime,
authenticationType as AuthenticationType,
cloudToDeviceMessageCount as CloudToDeviceMessageCount,
connectionState as ConnectionState,
capabilities.iotEdge as IotEdge
capabilities.iotEdge as IotEdge,
modelId as ModelId
FROM devices`;
export const DEVICE_TWIN_QUERY_STRING = ' SELECT * FROM devices WHERE deviceId = {deviceId}';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ exports[`DeviceList matches snapshot 1`] = `
"minWidth": 100,
"name": "deviceLists.columns.statusUpdatedTime",
},
Object {
"fieldName": "modelId",
"isMultiline": true,
"isResizable": true,
"key": "modelId",
"maxWidth": 400,
"minWidth": 100,
"name": "deviceLists.columns.isPnpDevice",
},
Object {
"fieldName": "edge",
"isResizable": true,
Expand Down
10 changes: 10 additions & 0 deletions src/app/devices/deviceList/components/deviceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export const DeviceList: React.FC = () => {
maxWidth: SMALL_COLUMN_WIDTH, minWidth: 100, name: t(ResourceKeys.deviceLists.columns.authenticationType)},
{ fieldName: 'statusUpdatedTime', isMultiline: true, isResizable: true, key: 'statusUpdatedTime',
maxWidth: MEDIUM_COLUMN_WIDTH, minWidth: 100, name: t(ResourceKeys.deviceLists.columns.statusUpdatedTime)},
{ fieldName: 'modelId', isMultiline: true, isResizable: true, key: 'modelId',
maxWidth: LARGE_COLUMN_WIDTH, minWidth: 100, name: t(ResourceKeys.deviceLists.columns.isPnpDevice)},
{ fieldName: 'edge', isResizable: true, key: 'edge',
minWidth: 100, name: t(ResourceKeys.deviceLists.columns.isEdgeDevice.label)},
];
Expand Down Expand Up @@ -195,6 +197,14 @@ export const DeviceList: React.FC = () => {
t(ResourceKeys.deviceLists.columns.isEdgeDevice.yes) : t(ResourceKeys.deviceLists.columns.isEdgeDevice.no)}
/>
);
case 'modelId':
return (
<Label
key={column.key}
>
{item.modelId}
</Label>
);
default:
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/dataPlaneHelper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('server', () => {

it('generates data plane response with no httpResponse', () => {
const response = processDataPlaneResponse(null, null);
expect(response.body).toEqual(`Cannot read property 'headers' of null`);
expect(response.body).toEqual({body: {Message: `Cannot read property 'headers' of null`}});
});

it('generates data plane response using device status code', () => {
Expand Down
11 changes: 9 additions & 2 deletions src/server/dataPlaneHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ export const generateDataPlaneResponse = (httpRes: request.Response, body: any,
export const processDataPlaneResponse = (httpRes: request.Response, body: any): {body: {body: any, headers?: any}, statusCode: number} => { // tslint:disable-line:no-any
try {
if (httpRes.headers && httpRes.headers[DEVICE_STATUS_HEADER]) { // handles happy failure cases when error code is returned as a header
let deviceResponseBody;
try {
deviceResponseBody = body && JSON.parse(body);
}
catch {
throw new Error('Failed to parse response from device. The response is expected to be a JSON document up to 128 KB. Learn more: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-direct-methods#method-lifecycle.');
}
return {
body: {body: body && JSON.parse(body)},
body: {body: deviceResponseBody},
statusCode: parseInt(httpRes.headers[DEVICE_STATUS_HEADER] as string) // tslint:disable-line:radix
};
}
Expand All @@ -49,7 +56,7 @@ export const processDataPlaneResponse = (httpRes: request.Response, body: any):
}
catch (error) {
return {
body: error.message,
body: {body: {Message: error.message}},
statusCode: SERVER_ERROR
};
}
Expand Down

0 comments on commit 003383b

Please sign in to comment.