@alwaysai/device-agent-schemas 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.eslintrc +25 -0
  2. package/README.md +8 -0
  3. package/bitbucket-pipelines.yml +10 -0
  4. package/jest.config.js +3 -0
  5. package/lib/constants.d.ts +18 -0
  6. package/lib/constants.d.ts.map +1 -0
  7. package/lib/constants.js +21 -0
  8. package/lib/constants.js.map +1 -0
  9. package/lib/index.d.ts +8 -0
  10. package/lib/index.d.ts.map +1 -0
  11. package/lib/index.js +11 -0
  12. package/lib/index.js.map +1 -0
  13. package/lib/schemas/action-schema.d.ts +35 -0
  14. package/lib/schemas/action-schema.d.ts.map +1 -0
  15. package/lib/schemas/action-schema.js +31 -0
  16. package/lib/schemas/action-schema.js.map +1 -0
  17. package/lib/schemas/application-logs-schema.d.ts +37 -0
  18. package/lib/schemas/application-logs-schema.d.ts.map +1 -0
  19. package/lib/schemas/application-logs-schema.js +33 -0
  20. package/lib/schemas/application-logs-schema.js.map +1 -0
  21. package/lib/schemas/application-state-schema.d.ts +68 -0
  22. package/lib/schemas/application-state-schema.d.ts.map +1 -0
  23. package/lib/schemas/application-state-schema.js +57 -0
  24. package/lib/schemas/application-state-schema.js.map +1 -0
  25. package/lib/schemas/common-schema.d.ts +17 -0
  26. package/lib/schemas/common-schema.d.ts.map +1 -0
  27. package/lib/schemas/common-schema.js +21 -0
  28. package/lib/schemas/common-schema.js.map +1 -0
  29. package/lib/schemas/device-status-schema.d.ts +41 -0
  30. package/lib/schemas/device-status-schema.d.ts.map +1 -0
  31. package/lib/schemas/device-status-schema.js +36 -0
  32. package/lib/schemas/device-status-schema.js.map +1 -0
  33. package/lib/types.d.ts +21 -0
  34. package/lib/types.d.ts.map +1 -0
  35. package/lib/types.js +74 -0
  36. package/lib/types.js.map +1 -0
  37. package/lib/types.test.d.ts +2 -0
  38. package/lib/types.test.d.ts.map +1 -0
  39. package/lib/types.test.js +118 -0
  40. package/lib/types.test.js.map +1 -0
  41. package/lib/utils.d.ts +2 -0
  42. package/lib/utils.d.ts.map +1 -0
  43. package/lib/utils.js +8 -0
  44. package/lib/utils.js.map +1 -0
  45. package/package.json +45 -0
  46. package/src/constants.ts +19 -0
  47. package/src/index.ts +38 -0
  48. package/src/schemas/action-schema.ts +36 -0
  49. package/src/schemas/application-logs-schema.ts +39 -0
  50. package/src/schemas/application-state-schema.ts +74 -0
  51. package/src/schemas/common-schema.ts +18 -0
  52. package/src/schemas/device-status-schema.ts +43 -0
  53. package/src/types.test.ts +119 -0
  54. package/src/types.ts +95 -0
  55. package/src/utils.ts +3 -0
  56. package/tsconfig.build.json +4 -0
  57. package/tsconfig.json +92 -0
package/src/types.ts ADDED
@@ -0,0 +1,95 @@
1
+ import Ajv, { JSONSchemaType } from 'ajv';
2
+ import { commonSchema } from './schemas/common-schema';
3
+ import { deviceStatusSchema, DeviceStatsMessage } from './schemas/device-status-schema';
4
+ import {
5
+ applicationStateSchema,
6
+ AppStateMessage,
7
+ } from './schemas/application-state-schema';
8
+ import { actionSchema, ActionMessage } from './schemas/action-schema';
9
+ import { applicationLogsSchema, AppLogsMessage } from './schemas/application-logs-schema';
10
+ import { DEVICE_ID_PATTERN } from './constants';
11
+ import { loadSchemaAsObject } from './utils';
12
+
13
+ // Message Packet Structures
14
+ export interface DeviceAgentMessage {
15
+ deviceId: string;
16
+ timestamp: string;
17
+ topic: string;
18
+ payload: ActionMessage | DeviceStatsMessage | AppLogsMessage | AppStateMessage;
19
+ }
20
+
21
+ // Note: the other schemas do not need to be added, because they will
22
+ // automatically be added in the compile step due to $ref usage (and $id lookup)
23
+ const ajv = new Ajv({
24
+ schemas: [commonSchema],
25
+ });
26
+
27
+ const messageSchema = loadSchemaAsObject({
28
+ $id: 'iot_message_schema.json',
29
+ title: 'Payload',
30
+ description:
31
+ 'A schema that describes the messaging payload structure for the alwaysAI Device Agent Cloud Connection',
32
+ type: 'object',
33
+ properties: {
34
+ timestamp: {
35
+ type: 'string',
36
+ },
37
+ deviceId: {
38
+ description: 'The version of the project associated with the message payload.',
39
+ type: 'string',
40
+ },
41
+ topic: {
42
+ oneOf: [
43
+ { $ref: '#/$defs/applicationManagementTopic' },
44
+ { $ref: '#/$defs/deviceManagementTopic' },
45
+ ],
46
+ },
47
+ payload: {
48
+ oneOf: [
49
+ { $ref: 'action_schema.json#/$defs/action' },
50
+ { $ref: 'device_schema.json#/$defs/status' },
51
+ { $ref: 'state_schema.json#/$defs/applicationState' },
52
+ { $ref: 'application_logs_schema.json#/$defs/applicationLogs' },
53
+ ],
54
+ },
55
+ },
56
+ required: ['timestamp', 'deviceId', 'topic', 'payload'],
57
+ additionalProperties: false,
58
+ $defs: {
59
+ applicationManagementTopic: {
60
+ description:
61
+ 'The specific topic to which the message payload was published, for application management commands.',
62
+ type: 'string',
63
+ pattern: `^device/${DEVICE_ID_PATTERN}/topic/application-management`,
64
+ },
65
+ deviceManagementTopic: {
66
+ description:
67
+ 'The specific topic to which the message payload was published, for device management data.',
68
+ type: 'string',
69
+ pattern: `^device/${DEVICE_ID_PATTERN}/topic/device-management`,
70
+ },
71
+ },
72
+ });
73
+
74
+ // Device Status Codec and Validation Method
75
+ export const deviceMessageSchemaType: JSONSchemaType<DeviceStatsMessage> =
76
+ loadSchemaAsObject(deviceStatusSchema);
77
+ export const validateDeviceStatsMessage = ajv.compile(deviceMessageSchemaType);
78
+
79
+ // Application Log Codec and Validation Method
80
+ export const appLogsMessageSchemaType: JSONSchemaType<AppLogsMessage> =
81
+ loadSchemaAsObject(applicationLogsSchema);
82
+ export const validateAppLogsMessage = ajv.compile(appLogsMessageSchemaType);
83
+
84
+ // Application State Codec and Validation Method
85
+ export const appStateMessageSchemaType: JSONSchemaType<AppStateMessage> =
86
+ loadSchemaAsObject(applicationStateSchema);
87
+ export const validateAppStateMessage = ajv.compile(appStateMessageSchemaType);
88
+
89
+ export const actionMessageSchemaType: JSONSchemaType<ActionMessage> =
90
+ loadSchemaAsObject(actionSchema);
91
+ export const validateActionMessage = ajv.compile(actionMessageSchemaType);
92
+
93
+ // Overall IoT Core Message Codec and Validation Method
94
+ const messageSchemaType: JSONSchemaType<DeviceAgentMessage> = messageSchema;
95
+ export const validateDeviceAgentMessage = ajv.compile(messageSchemaType);
package/src/utils.ts ADDED
@@ -0,0 +1,3 @@
1
+ export function loadSchemaAsObject(schema: object) {
2
+ return JSON.parse(JSON.stringify(schema));
3
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {}
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "extends": "@alwaysai/tsconfig/nodejs/tsconfig.json",
3
+ "include": [
4
+ "src",
5
+ "src/types/.ts",
6
+ "src/schemas/.ts"
7
+ ],
8
+ "compilerOptions": {
9
+ "rootDir": "./src",
10
+ "outDir": "./lib",
11
+ // "allowJs": false,
12
+ "allowSyntheticDefaultImports": true,
13
+ // "allowUnreachableCode": false,
14
+ // "allowUnusedLabels": false,
15
+ "alwaysStrict": false,
16
+ // "baseUrl": ".",
17
+ // "build": false,
18
+ // "charset": "utf8",
19
+ // "checkJs": false,
20
+ // "composite": true,
21
+ "declaration": true,
22
+ // "declarationDir": ".",
23
+ "declarationMap": true,
24
+ // "diagnostics": false,
25
+ // "disableSizeLimit": false,
26
+ "downlevelIteration": true,
27
+ // "emitBOM": false,
28
+ // "emitDeclarationOnly": false,
29
+ // "emitDecoratorMetadata": false,
30
+ // "esModuleInterop": false,
31
+ // "experimentalDecorators": false,
32
+ "forceConsistentCasingInFileNames": true,
33
+ // "importHelpers": false,
34
+ // "inlineSourceMap": false,
35
+ // "inlineSources": false,
36
+ // "isolatedModules": false,
37
+ // "jsx": "preserve",
38
+ // "jsxFactory": "React.createElement",
39
+ // "keyofStringsOnly": false,
40
+ // "lib": [],
41
+ // "listEmittedFiles": false,
42
+ // "listFiles": false,
43
+ // "locale": "en",
44
+ // "mapRoot": ".",
45
+ // "maxNodeModuleJsDepth": 0,
46
+ "module": "commonjs",
47
+ "moduleResolution": "node",
48
+ "newLine": "lf",
49
+ // "noEmit": false,
50
+ // "noEmitHelpers": false,
51
+ "noEmitOnError": false,
52
+ "noErrorTruncation": true,
53
+ // "noFallthroughCasesInSwitch": false,
54
+ "noImplicitAny": false,
55
+ "noImplicitReturns": false,
56
+ "noImplicitThis": false,
57
+ // "noImplicitUseStrict": false,
58
+ // "noLib": false,
59
+ // "noStrictGenericChecks": false,
60
+ // "noResolve": false,
61
+ // "noUnusedLocals": false,
62
+ // "noUnusedParameters": false,
63
+ // "out": DEPRECATED
64
+ // "outDir": "lib",
65
+ // "outFile": "dist/lib.js",
66
+ // "paths": [],
67
+ // "preserveConstEnums": false,
68
+ // "preserveSymlinks": false,
69
+ // "preserveWatchOutput": false,
70
+ // "pretty": true,
71
+ // "reactNamespace": DEPRECATED
72
+ // "removeComments": false,
73
+ "resolveJsonModule": true,
74
+ // "rootDir": "src",
75
+ // "rootDirs": ["src"],
76
+ // "skipDefaultLibCheck": DEPRECATED
77
+ // "skipLibCheck": false,
78
+ "sourceMap": true,
79
+ // "sourceRoot": ".",
80
+ "strict": false,
81
+ "strictFunctionTypes": false,
82
+ "strictNullChecks": true,
83
+ "strictPropertyInitialization": false
84
+ // "stripInternal": false,
85
+ // "suppressExcessPropertyErrors": false,
86
+ // "suppressImplicitAnyIndexErrors": false,
87
+ // "target": "esnext",
88
+ // "traceResolution": false,
89
+ // "types": [],
90
+ // "typeRoots": [],
91
+ }
92
+ }