@kohost/api-client 1.0.0-alpha.2 → 1.0.0-alpha.4

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 (115) hide show
  1. package/.eslintrc.js +10 -0
  2. package/bitbucket-pipelines.yml +39 -0
  3. package/commands/Command.js +38 -0
  4. package/commands/DiscoverUsersCommand.js +21 -0
  5. package/commands/SetAlarmCommand.js +21 -0
  6. package/commands/SetCourtesyCommand.js +21 -0
  7. package/commands/SetDimmerCommand.js +21 -0
  8. package/commands/SetLockCommand.js +21 -0
  9. package/commands/SetSceneControllerCommand.js +21 -0
  10. package/commands/SetSwitchCommand.js +21 -0
  11. package/commands/SetThermostatCommand.js +21 -0
  12. package/commands/SetWindowCoveringCommand.js +21 -0
  13. package/commands/VerifyDocumentCommand.js +24 -0
  14. package/commands/index.js +25 -0
  15. package/defs/deviceTypes.js +15 -0
  16. package/defs/formalDeviceTypes.js +6 -0
  17. package/defs/http.js +7 -0
  18. package/defs/index.js +11 -0
  19. package/errors/AppError.js +8 -0
  20. package/errors/AuthenticationError.js +9 -0
  21. package/errors/AuthorizationError.js +9 -0
  22. package/errors/DeviceCommError.js +9 -0
  23. package/errors/LoginError.js +9 -0
  24. package/errors/NotFoundError.js +9 -0
  25. package/errors/RequestError.js +9 -0
  26. package/errors/SystemCommError.js +9 -0
  27. package/errors/TokenExpiredError.js +9 -0
  28. package/errors/UnprocessableRequestError.js +9 -0
  29. package/errors/ValidationError.js +9 -0
  30. package/errors/index.js +15 -0
  31. package/events/Event.js +33 -0
  32. package/events/SystemCameraUpdatedEvent.js +17 -0
  33. package/events/SystemCourtesyUpdatedEvent.js +17 -0
  34. package/events/SystemDimmerUpdatedEvent.js +17 -0
  35. package/events/SystemLockUpdatedEvent.js +17 -0
  36. package/events/SystemReservationUpdatedEvent.js +17 -0
  37. package/events/SystemSceneControllerUpdatedEvent.js +17 -0
  38. package/events/SystemSourceUpdatedEvent.js +17 -0
  39. package/events/SystemSpaceUpdatedEvent.js +17 -0
  40. package/events/SystemSwitchUpdatedEvent.js +17 -0
  41. package/events/SystemThermostatUpdatedEvent.js +17 -0
  42. package/events/SystemUserUpdatedEvent.js +17 -0
  43. package/events/SystemWindowCoveringUpdatedEvent.js +17 -0
  44. package/events/index.js +28 -0
  45. package/http/handleResponseError.js +53 -0
  46. package/http/handleResponseSuccess.js +15 -0
  47. package/http/index.js +165 -0
  48. package/index.js +19 -2
  49. package/models/acl.js +29 -0
  50. package/models/admin/customer.js +28 -0
  51. package/models/admin/property.js +28 -0
  52. package/models/alarm.js +25 -3
  53. package/models/application.js +28 -0
  54. package/models/camera.js +29 -0
  55. package/models/courtesy.js +29 -3
  56. package/models/dimmer.js +45 -3
  57. package/models/discoveredDevice.js +30 -0
  58. package/models/gateway.js +33 -3
  59. package/models/index.js +38 -1
  60. package/models/integration.js +76 -0
  61. package/models/iotGateway.js +29 -0
  62. package/models/kohost.js +95 -0
  63. package/models/lock.js +29 -3
  64. package/models/mediaSource.js +29 -0
  65. package/models/motionSensor.js +29 -0
  66. package/models/reservation.js +36 -0
  67. package/models/room.js +185 -0
  68. package/models/scene.js +28 -0
  69. package/models/sceneController.js +24 -5
  70. package/models/space.js +99 -0
  71. package/models/switch.js +29 -3
  72. package/models/thermostat.js +71 -31
  73. package/models/ticket.js +91 -0
  74. package/models/user.js +58 -0
  75. package/models/windowCovering.js +44 -5
  76. package/package.json +30 -7
  77. package/prepare.js +4 -0
  78. package/schemas/acl.json +111 -0
  79. package/schemas/admin/customer.json +32 -0
  80. package/schemas/admin/property.json +174 -0
  81. package/schemas/alarm.json +5 -5
  82. package/schemas/application.json +24 -0
  83. package/schemas/camera.json +41 -0
  84. package/schemas/courtesy.json +5 -6
  85. package/schemas/definitions/common.json +21 -0
  86. package/schemas/definitions/device.json +21 -3
  87. package/schemas/dimmer.json +8 -5
  88. package/schemas/discoveredDevice.json +43 -0
  89. package/schemas/gateway.json +45 -13
  90. package/schemas/identification.json +35 -0
  91. package/schemas/integration.json +94 -0
  92. package/schemas/iotGateway.json +29 -0
  93. package/schemas/lock.json +8 -5
  94. package/schemas/mediaSource.json +151 -0
  95. package/schemas/motionSensor.json +26 -0
  96. package/schemas/payment.json +38 -0
  97. package/schemas/reservation.json +66 -0
  98. package/schemas/room.json +138 -0
  99. package/schemas/scene.json +118 -0
  100. package/schemas/sceneController.json +9 -9
  101. package/schemas/space.json +111 -0
  102. package/schemas/switch.json +8 -5
  103. package/schemas/thermostat.json +19 -10
  104. package/schemas/ticket.json +82 -0
  105. package/schemas/user.json +124 -0
  106. package/schemas/windowCovering.json +8 -5
  107. package/tests/unit/models/space.test.js +31 -0
  108. package/tests/unit/models/thermostat.test.js +146 -0
  109. package/tests/unit/models/user.test.js +28 -0
  110. package/useCases/http.json +1098 -0
  111. package/utils/getDeviceTypes.js +7 -0
  112. package/utils/getFormalDeviceType.js +5 -0
  113. package/utils/schema.js +28 -0
  114. package/vite.config.js +22 -0
  115. package/utils/compiler.js +0 -40
@@ -0,0 +1,17 @@
1
+ const Event = require("./Event");
2
+
3
+ class SystemSourceUpdatedEvent extends Event {
4
+ constructor(source) {
5
+ super(source);
6
+ }
7
+
8
+ get name() {
9
+ return "SystemSourceUpdated";
10
+ }
11
+
12
+ get routingKey() {
13
+ return `source.${this.data.id}.updated`;
14
+ }
15
+ }
16
+
17
+ module.exports = SystemSourceUpdatedEvent;
@@ -0,0 +1,17 @@
1
+ const Event = require("./Event");
2
+
3
+ class SystemSpaceUpdatedEvent extends Event {
4
+ constructor(space) {
5
+ super(space);
6
+ }
7
+
8
+ get name() {
9
+ return "SystemSpaceUpdated";
10
+ }
11
+
12
+ get routingKey() {
13
+ return `space.${this.data.id}.updated`;
14
+ }
15
+ }
16
+
17
+ module.exports = SystemSpaceUpdatedEvent;
@@ -0,0 +1,17 @@
1
+ const Event = require("./Event");
2
+
3
+ class SystemSwitchUpdatedEvent extends Event {
4
+ constructor(_switch) {
5
+ super(_switch);
6
+ }
7
+
8
+ get name() {
9
+ return "SystemSwitchUpdated";
10
+ }
11
+
12
+ get routingKey() {
13
+ return `switch.${this.data.id}.updated`;
14
+ }
15
+ }
16
+
17
+ module.exports = SystemSwitchUpdatedEvent;
@@ -0,0 +1,17 @@
1
+ const Event = require("./Event");
2
+
3
+ class SystemThermostatUpdatedEvent extends Event {
4
+ constructor(thermostat) {
5
+ super(thermostat);
6
+ }
7
+
8
+ get name() {
9
+ return "SystemThermostatUpdated";
10
+ }
11
+
12
+ get routingKey() {
13
+ return `thermostat.${this.data.id}.updated`;
14
+ }
15
+ }
16
+
17
+ module.exports = SystemThermostatUpdatedEvent;
@@ -0,0 +1,17 @@
1
+ const Event = require("./Event");
2
+
3
+ class SystemUserUpdatedEvent extends Event {
4
+ constructor(thermostat) {
5
+ super(thermostat);
6
+ }
7
+
8
+ get name() {
9
+ return "SystemUserUpdated";
10
+ }
11
+
12
+ get routingKey() {
13
+ return `user.${this.data.id}.updated`;
14
+ }
15
+ }
16
+
17
+ module.exports = SystemUserUpdatedEvent;
@@ -0,0 +1,17 @@
1
+ const Event = require("./Event");
2
+
3
+ class SystemWindowCoveringUpdatedEvent extends Event {
4
+ constructor(wc) {
5
+ super(wc);
6
+ }
7
+
8
+ get name() {
9
+ return "SystemWindowCoveringUpdated";
10
+ }
11
+
12
+ get routingKey() {
13
+ return `windowCovering.${this.data.id}.updated`;
14
+ }
15
+ }
16
+
17
+ module.exports = SystemWindowCoveringUpdatedEvent;
@@ -0,0 +1,28 @@
1
+ const SystemThermostatUpdatedEvent = require("./SystemThermostatUpdatedEvent");
2
+ const SystemDimmerUpdatedEvent = require("./SystemDimmerUpdatedEvent");
3
+ const SystemSwitchUpdatedEvent = require("./SystemSwitchUpdatedEvent");
4
+ const SystemLockUpdatedEvent = require("./SystemLockUpdatedEvent");
5
+ const SystemCameraUpdatedEvent = require("./SystemCameraUpdatedEvent");
6
+ const SystemSceneControllerUpdatedEvent = require("./SystemSceneControllerUpdatedEvent");
7
+ const SystemWindowCoveringUpdatedEvent = require("./SystemWindowCoveringUpdatedEvent");
8
+ const SystemSourceUpdatedEvent = require("./SystemSourceUpdatedEvent");
9
+ const SystemCourtesyUpdatedEvent = require("./SystemCourtesyUpdatedEvent");
10
+
11
+ const SystemUserUpdatedEvent = require("./SystemUserUpdatedEvent");
12
+ const SystemSpaceUpdatedEvent = require("./SystemSpaceUpdatedEvent");
13
+ const SystemReservationUpdatedEvent = require("./SystemReservationUpdatedEvent");
14
+
15
+ module.exports = {
16
+ SystemThermostatUpdatedEvent,
17
+ SystemDimmerUpdatedEvent,
18
+ SystemSwitchUpdatedEvent,
19
+ SystemLockUpdatedEvent,
20
+ SystemCameraUpdatedEvent,
21
+ SystemSceneControllerUpdatedEvent,
22
+ SystemWindowCoveringUpdatedEvent,
23
+ SystemSourceUpdatedEvent,
24
+ SystemCourtesyUpdatedEvent,
25
+ SystemUserUpdatedEvent,
26
+ SystemSpaceUpdatedEvent,
27
+ SystemReservationUpdatedEvent,
28
+ };
@@ -0,0 +1,53 @@
1
+ const defs = require("../defs").http;
2
+
3
+ module.exports = function handleResponseError(error) {
4
+ const { config: originalReq } = error;
5
+ if (!error.response) return Promise.reject(error);
6
+ const { status, data } = error.response;
7
+ const errorCode = data && data.error && data.error.code;
8
+ const errorMessage = data && data.error && data.error.message;
9
+
10
+ try {
11
+ const expectedError = status >= 400 && status < 500;
12
+
13
+ if (errorMessage && errorMessage === "Login Required") {
14
+ this.onLoginRequired();
15
+ return Promise.reject(error);
16
+ }
17
+
18
+ // prettier-ignore
19
+ const newTokensNeeded = expectedError && errorCode === 1004 && status === 401;
20
+
21
+ if (status === 401 && !newTokensNeeded) {
22
+ return Promise.reject(error);
23
+ }
24
+ if (status === 400 && errorCode === 1010) {
25
+ this.onLoginRequired();
26
+ return Promise.reject(error);
27
+ }
28
+
29
+ if (newTokensNeeded) {
30
+ return this.RefreshToken({
31
+ headers: {
32
+ [defs.refreshTokenHeader]: this.refreshToken,
33
+ },
34
+ }).then((response) => {
35
+ // update headers with the new tokens
36
+ if (
37
+ response &&
38
+ response.headers &&
39
+ response.headers[this.authTokenHeaderKey]
40
+ ) {
41
+ const newToken = response.headers[this.authTokenHeaderKey];
42
+ originalReq.headers[defs.authTokenHeader] = newToken;
43
+ this.authToken = newToken;
44
+ return this.http(originalReq);
45
+ }
46
+ });
47
+ }
48
+ } catch (error) {
49
+ console.log(error);
50
+ }
51
+
52
+ return Promise.reject(error);
53
+ };
@@ -0,0 +1,15 @@
1
+ module.exports = function handleResponseSuccess(response) {
2
+ if (response?.data?.data) {
3
+ response.query = response.data.query;
4
+ response.pagination = response.data.pagination;
5
+ response.data = response.data.data;
6
+ }
7
+ if (!this.isBrowser && response.headers[this.authTokenHeaderKey]) {
8
+ this.authToken = response.headers[this.authTokenHeaderKey];
9
+ }
10
+
11
+ if (!this.isBrowser && response.headers[this.refreshTokenHeaderKey]) {
12
+ this.refreshToken = response.headers[this.refreshTokenHeaderKey];
13
+ }
14
+ return response;
15
+ };
package/http/index.js ADDED
@@ -0,0 +1,165 @@
1
+ const { EventEmitter } = require("events");
2
+ const defs = require("../defs").http;
3
+ const handleResponseError = require("./handleResponseError");
4
+ const handleResponseSuccess = require("./handleResponseSuccess");
5
+
6
+ const useCases = require("../useCases/http.json");
7
+
8
+ /*
9
+ Creates methods for each use case in the API
10
+
11
+ @param {Object} Client - The client class
12
+ @returns undefined
13
+ */
14
+
15
+ function useCaseMethodFactory(Client) {
16
+ const map = new Map(useCases);
17
+ for (const [useCase, data] of map.entries()) {
18
+ if (data.http) {
19
+ const { method, path } = data.http;
20
+
21
+ /*
22
+ Creates a method for each use case in the API
23
+ @param {Object} options - The options to send to the API
24
+ @param {Object} options.headers - The headers to send to the API
25
+ @param {Object} options.params - The params for the reuquest to build the URL
26
+ @param {Object} options.data - The body to send to the API. Valid for POST and PUT requests
27
+ @parms {Object} options.query - The query for the request to build the URL
28
+ @returns {Promise} The response from the API
29
+ */
30
+
31
+ //eslint-disable-next-line no-inner-declarations
32
+ function UseCase(requestData, options = {}) {
33
+ if (!this._init) {
34
+ // wait a second for the client to initialize
35
+ return new Promise((resolve) => {
36
+ setTimeout(() => {
37
+ resolve(UseCase.call(this, requestData));
38
+ }, 500);
39
+ });
40
+ }
41
+ if (!requestData) requestData = {};
42
+
43
+ // get parameters from path
44
+ const pathParams = path.match(/:[a-zA-Z0-9]+/g);
45
+
46
+ const { data, params, query, headers } = requestData;
47
+
48
+ // replace path parameters with values from params
49
+ let url = path;
50
+ if (pathParams && params) {
51
+ for (const param of pathParams) {
52
+ const paramName = param.replace(":", "");
53
+ url = url.replace(param, params[paramName]);
54
+ }
55
+ }
56
+
57
+ // make sure all parameters have been replaced
58
+ if (url.match(/:[a-zA-Z0-9]+/g)) {
59
+ const missingParams = url.match(/:[a-zA-Z0-9]+/g);
60
+ // remove the colon from the parameter name
61
+ const missing = missingParams.map((param) => param.replace(":", ""));
62
+ return Promise.reject(
63
+ new Error(`Missing parameters: ${missing.join(", ")}`)
64
+ );
65
+ }
66
+
67
+ const config = {
68
+ method: method.toLowerCase(),
69
+ url: url,
70
+ ...options,
71
+ };
72
+
73
+ if (data) config.data = data;
74
+ if (query) config.params = query;
75
+ if (headers) config.headers = headers;
76
+
77
+ return this._http.request(config);
78
+ }
79
+
80
+ Client.prototype[useCase] = UseCase;
81
+ }
82
+ }
83
+ }
84
+
85
+ class KohostApiClient extends EventEmitter {
86
+ /*
87
+ @param {Object} options - The options to create the client
88
+ @param {String} options.tenantId - The tenant ID
89
+ @param {String} options.url - The base URL for the API endpint
90
+ @param {Object} options.headers - Additional headers to send with each request
91
+
92
+ */
93
+ constructor(
94
+ options = {
95
+ url: "",
96
+ tenantId: "",
97
+ headers: {},
98
+ }
99
+ ) {
100
+ super();
101
+ if (!options.url) throw new Error("options.url is required");
102
+ if (!options.tenantId) throw new Error("options.tenant is required");
103
+ this.options = options;
104
+ // eslint-disable-next-line no-undef
105
+ this.isBrowser = typeof window !== "undefined";
106
+ this._init = false;
107
+
108
+ import("axios").then((axiosModule) => {
109
+ const axios = axiosModule.default;
110
+ this._http = axios.create({
111
+ baseURL: options.url,
112
+ responseType: "json",
113
+ withCredentials: true,
114
+ headers: {
115
+ "Content-Type": "application/json",
116
+ Accept: "application/json",
117
+ [defs.tenantHeader]: options.tenantId,
118
+ ...options.headers,
119
+ },
120
+ });
121
+
122
+ this._http.interceptors.response.use(
123
+ handleResponseSuccess.bind(this),
124
+ handleResponseError.bind(this)
125
+ );
126
+
127
+ this._http.interceptors.request.use((config) => {
128
+ if (!this.isBrowser) {
129
+ config.headers[defs.authTokenHeader] = this.authToken;
130
+ }
131
+ return config;
132
+ });
133
+ this._init = true;
134
+ });
135
+ }
136
+
137
+ get authTokenHeaderKey() {
138
+ return defs.authTokenHeader.toLowerCase();
139
+ }
140
+
141
+ get refreshTokenHeaderKey() {
142
+ return defs.refreshTokenHeader.toLowerCase();
143
+ }
144
+
145
+ get lsTokenKey() {
146
+ return `${this.options.tenantId}_${defs.authTokenHeader}`;
147
+ }
148
+
149
+ get authToken() {
150
+ return this._authToken;
151
+ }
152
+
153
+ /*
154
+ @param {String} token - The token to set
155
+ @returns undefined
156
+ */
157
+
158
+ set authToken(token) {
159
+ this._authToken = token;
160
+ }
161
+ }
162
+
163
+ useCaseMethodFactory(KohostApiClient);
164
+
165
+ module.exports = KohostApiClient;
package/index.js CHANGED
@@ -1,6 +1,23 @@
1
1
  const Models = require("./models");
2
+ const Errors = require("./errors");
3
+ const Commands = require("./commands");
4
+ const Events = require("./events");
5
+ const HttpClient = require("./http");
6
+ const defs = require("./defs");
7
+ const getFormalDeviceType = require("./utils/getFormalDeviceType");
8
+ const getDeviceTypes = require("./utils/getDeviceTypes");
2
9
 
3
- // export all classes on the Models object
4
- module.exports = {
10
+ const Kohost = {
5
11
  Models,
12
+ Errors,
13
+ Commands,
14
+ Events,
15
+ defs,
16
+ Client: HttpClient,
17
+ utils: {
18
+ getFormalDeviceType: getFormalDeviceType,
19
+ getDeviceTypes: getDeviceTypes,
20
+ },
6
21
  };
22
+
23
+ module.exports = Kohost;
package/models/acl.js ADDED
@@ -0,0 +1,29 @@
1
+ // create the ACL model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/acl.json");
4
+ const Kohost = require("./kohost");
5
+
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class ACL extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+ }
14
+
15
+ Object.defineProperty(ACL.prototype, "schema", {
16
+ value: schema,
17
+ });
18
+
19
+ Object.defineProperty(ACL.prototype, "validator", {
20
+ get: function () {
21
+ return validator;
22
+ },
23
+ });
24
+
25
+ Object.defineProperty(ACL, "validProperties", {
26
+ value: Object.keys(schema.properties),
27
+ });
28
+
29
+ module.exports = ACL;
@@ -0,0 +1,28 @@
1
+ const schemas = require("../../utils/schema");
2
+ const schema = require("../../schemas/admin/customer.json");
3
+ const Kohost = require("../kohost");
4
+
5
+ schemas.add(schema);
6
+ const validator = schemas.compile(schema);
7
+
8
+ class Customer extends Kohost {
9
+ constructor(data) {
10
+ super(data);
11
+ }
12
+ }
13
+
14
+ Object.defineProperty(Customer.prototype, "schema", {
15
+ value: schema,
16
+ });
17
+
18
+ Object.defineProperty(Customer.prototype, "validator", {
19
+ get: function () {
20
+ return validator;
21
+ },
22
+ });
23
+
24
+ Object.defineProperty(Customer, "validProperties", {
25
+ value: Object.keys(schema.properties),
26
+ });
27
+
28
+ module.exports = Customer;
@@ -0,0 +1,28 @@
1
+ const schemas = require("../../utils/schema");
2
+ const schema = require("../../schemas/admin/property.json");
3
+ const Kohost = require("../kohost");
4
+
5
+ schemas.add(schema);
6
+ const validator = schemas.compile(schema);
7
+
8
+ class Property extends Kohost {
9
+ constructor(data) {
10
+ super(data);
11
+ }
12
+ }
13
+
14
+ Object.defineProperty(Property.prototype, "schema", {
15
+ value: schema,
16
+ });
17
+
18
+ Object.defineProperty(Property.prototype, "validator", {
19
+ get: function () {
20
+ return validator;
21
+ },
22
+ });
23
+
24
+ Object.defineProperty(Property, "validProperties", {
25
+ value: Object.keys(schema.properties),
26
+ });
27
+
28
+ module.exports = Property;
package/models/alarm.js CHANGED
@@ -1,7 +1,29 @@
1
1
  // create the Alarm Model
2
- const { createModel } = require("../utils/compiler");
3
- const alarmSchema = require("../schemas/alarm.json");
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/alarm.json");
4
+ const Kohost = require("./kohost");
4
5
 
5
- const Alarm = createModel({ schema: alarmSchema, name: "Alarm" });
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class Alarm extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+ }
14
+
15
+ Object.defineProperty(Alarm.prototype, "schema", {
16
+ value: schema,
17
+ });
18
+
19
+ Object.defineProperty(Alarm.prototype, "validator", {
20
+ get: function () {
21
+ return validator;
22
+ },
23
+ });
24
+
25
+ Object.defineProperty(Alarm, "validProperties", {
26
+ value: Object.keys(schema.properties),
27
+ });
6
28
 
7
29
  module.exports = Alarm;
@@ -0,0 +1,28 @@
1
+ // create the Application model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/application.json");
4
+ const Kohost = require("./kohost");
5
+
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+ class Application extends Kohost {
9
+ constructor(data) {
10
+ super(data);
11
+ }
12
+ }
13
+
14
+ Object.defineProperty(Application.prototype, "schema", {
15
+ value: schema,
16
+ });
17
+
18
+ Object.defineProperty(Application.prototype, "validator", {
19
+ get: function () {
20
+ return validator;
21
+ },
22
+ });
23
+
24
+ Object.defineProperty(Application, "validProperties", {
25
+ value: Object.keys(schema.properties),
26
+ });
27
+
28
+ module.exports = Application;
@@ -0,0 +1,29 @@
1
+ // create the Alarm Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/camera.json");
4
+ const Kohost = require("./kohost");
5
+
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class Camera extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+ }
14
+
15
+ Object.defineProperty(Camera.prototype, "schema", {
16
+ value: schema,
17
+ });
18
+
19
+ Object.defineProperty(Camera.prototype, "validator", {
20
+ get: function () {
21
+ return validator;
22
+ },
23
+ });
24
+
25
+ Object.defineProperty(Camera, "validProperties", {
26
+ value: Object.keys(schema.properties),
27
+ });
28
+
29
+ module.exports = Camera;
@@ -1,7 +1,33 @@
1
1
  // create the Courtesy Model
2
- const { createModel } = require("../utils/compiler");
3
- const courtesySchema = require("../schemas/courtesy.json");
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/courtesy.json");
4
+ const Kohost = require("./kohost");
4
5
 
5
- const Courtesy = createModel({ schema: courtesySchema, name: "Courtesy" });
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class Courtesy extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+ }
14
+
15
+ Object.defineProperty(Courtesy.prototype, "schema", {
16
+ value: schema,
17
+ });
18
+
19
+ Object.defineProperty(Courtesy.prototype, "validator", {
20
+ get: function () {
21
+ return validator;
22
+ },
23
+ });
24
+
25
+ Object.defineProperty(Courtesy, "validProperties", {
26
+ value: Object.keys(schema.properties),
27
+ });
28
+
29
+ Object.defineProperty(Courtesy, "actionProperties", {
30
+ value: ["state"],
31
+ });
6
32
 
7
33
  module.exports = Courtesy;
package/models/dimmer.js CHANGED
@@ -1,7 +1,49 @@
1
1
  // Create the Dimmer Model
2
- const { createModel } = require("../utils/compiler");
3
- const dimmerSchema = require("../schemas/dimmer.json");
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/dimmer.json");
4
+ const Kohost = require("./kohost");
4
5
 
5
- const Dimmer = createModel({ schema: dimmerSchema, name: "Dimmer" });
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class Dimmer extends Kohost {
10
+ constructor(data) {
11
+ super(data);
12
+ }
13
+
14
+ static getActionDelta(old, _new) {
15
+ const delta = {};
16
+ for (const action in _new) {
17
+ if (this.actionProperties?.includes(action)) {
18
+ if (action === "level") {
19
+ const oldLevel = old[action];
20
+ const newLevel = _new[action];
21
+ delta[action] = newLevel - oldLevel / 100;
22
+ } else {
23
+ delta[action] = 1;
24
+ }
25
+ }
26
+ }
27
+ return delta;
28
+ }
29
+ }
30
+
31
+ Object.defineProperty(Dimmer.prototype, "schema", {
32
+ value: schema,
33
+ });
34
+
35
+ Object.defineProperty(Dimmer.prototype, "validator", {
36
+ get: function () {
37
+ return validator;
38
+ },
39
+ });
40
+
41
+ Object.defineProperty(Dimmer, "validProperties", {
42
+ value: Object.keys(schema.properties),
43
+ });
44
+
45
+ Object.defineProperty(Dimmer, "actionProperties", {
46
+ value: ["level"],
47
+ });
6
48
 
7
49
  module.exports = Dimmer;