@kohost/api-client 1.0.0-alpha.1 → 1.0.0-alpha.3

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 (111) 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/SetAlarmCommand.js +21 -0
  5. package/commands/SetCourtesyCommand.js +21 -0
  6. package/commands/SetDimmerCommand.js +21 -0
  7. package/commands/SetLockCommand.js +21 -0
  8. package/commands/SetSceneControllerCommand.js +21 -0
  9. package/commands/SetSwitchCommand.js +21 -0
  10. package/commands/SetThermostatCommand.js +21 -0
  11. package/commands/SetWindowCoveringCommand.js +21 -0
  12. package/commands/VerifyDocumentCommand.js +24 -0
  13. package/commands/index.js +21 -0
  14. package/defs/deviceTypes.js +15 -0
  15. package/defs/formalDeviceTypes.js +6 -0
  16. package/defs/http.js +7 -0
  17. package/defs/index.js +11 -0
  18. package/errors/AppError.js +8 -0
  19. package/errors/AuthenticationError.js +9 -0
  20. package/errors/AuthorizationError.js +9 -0
  21. package/errors/DeviceCommError.js +9 -0
  22. package/errors/LoginError.js +9 -0
  23. package/errors/NotFoundError.js +9 -0
  24. package/errors/RequestError.js +9 -0
  25. package/errors/SystemCommError.js +9 -0
  26. package/errors/TokenExpiredError.js +9 -0
  27. package/errors/UnprocessableRequestError.js +9 -0
  28. package/errors/ValidationError.js +9 -0
  29. package/errors/index.js +15 -0
  30. package/events/Event.js +33 -0
  31. package/events/SystemCameraUpdatedEvent.js +17 -0
  32. package/events/SystemCourtesyUpdatedEvent.js +17 -0
  33. package/events/SystemDimmerUpdatedEvent.js +17 -0
  34. package/events/SystemLockUpdatedEvent.js +17 -0
  35. package/events/SystemReservationUpdatedEvent.js +17 -0
  36. package/events/SystemSceneControllerUpdatedEvent.js +17 -0
  37. package/events/SystemSourceUpdatedEvent.js +17 -0
  38. package/events/SystemSpaceUpdatedEvent.js +17 -0
  39. package/events/SystemSwitchUpdatedEvent.js +17 -0
  40. package/events/SystemThermostatUpdatedEvent.js +17 -0
  41. package/events/SystemUserUpdatedEvent.js +17 -0
  42. package/events/SystemWindowCoveringUpdatedEvent.js +17 -0
  43. package/events/index.js +28 -0
  44. package/http/handleResponseError.js +53 -0
  45. package/http/handleResponseSuccess.js +15 -0
  46. package/http/index.js +159 -0
  47. package/index.js +21 -71
  48. package/models/acl.js +29 -0
  49. package/models/admin/customer.js +28 -0
  50. package/models/admin/property.js +28 -0
  51. package/models/alarm.js +29 -0
  52. package/models/application.js +28 -0
  53. package/models/camera.js +29 -0
  54. package/models/courtesy.js +33 -0
  55. package/models/dimmer.js +49 -0
  56. package/models/discoveredDevice.js +30 -0
  57. package/models/gateway.js +37 -0
  58. package/models/index.js +56 -0
  59. package/models/integration.js +76 -0
  60. package/models/iotGateway.js +29 -0
  61. package/models/kohost.js +95 -0
  62. package/models/lock.js +33 -0
  63. package/models/mediaSource.js +29 -0
  64. package/models/motionSensor.js +29 -0
  65. package/models/reservation.js +36 -0
  66. package/models/room.js +185 -0
  67. package/models/scene.js +28 -0
  68. package/models/sceneController.js +29 -0
  69. package/models/space.js +99 -0
  70. package/models/switch.js +33 -0
  71. package/models/thermostat.js +80 -0
  72. package/models/ticket.js +91 -0
  73. package/models/user.js +58 -0
  74. package/models/windowCovering.js +49 -0
  75. package/package.json +27 -5
  76. package/prepare.js +4 -0
  77. package/schemas/acl.json +111 -0
  78. package/schemas/admin/customer.json +32 -0
  79. package/schemas/admin/property.json +54 -0
  80. package/schemas/alarm.json +5 -5
  81. package/schemas/application.json +24 -0
  82. package/schemas/camera.json +41 -0
  83. package/schemas/courtesy.json +5 -6
  84. package/schemas/definitions/common.json +21 -0
  85. package/schemas/{device.json → definitions/device.json} +22 -4
  86. package/schemas/dimmer.json +8 -5
  87. package/schemas/discoveredDevice.json +43 -0
  88. package/schemas/gateway.json +45 -13
  89. package/schemas/identification.json +35 -0
  90. package/schemas/integration.json +94 -0
  91. package/schemas/iotGateway.json +29 -0
  92. package/schemas/lock.json +8 -5
  93. package/schemas/mediaSource.json +151 -0
  94. package/schemas/motionSensor.json +26 -0
  95. package/schemas/payment.json +38 -0
  96. package/schemas/reservation.json +66 -0
  97. package/schemas/room.json +138 -0
  98. package/schemas/scene.json +118 -0
  99. package/schemas/sceneController.json +18 -6
  100. package/schemas/space.json +111 -0
  101. package/schemas/switch.json +8 -5
  102. package/schemas/thermostat.json +19 -10
  103. package/schemas/ticket.json +82 -0
  104. package/schemas/user.json +124 -0
  105. package/schemas/windowCovering.json +8 -5
  106. package/tests/unit/models/space.test.js +31 -0
  107. package/tests/unit/models/thermostat.test.js +146 -0
  108. package/useCases/http.json +902 -0
  109. package/utils/getDeviceTypes.js +7 -0
  110. package/utils/getFormalDeviceType.js +5 -0
  111. package/utils/schema.js +28 -0
@@ -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 (response.headers[this.authTokenHeaderKey]) {
8
+ this.authToken = response.headers[this.authTokenHeaderKey];
9
+ }
10
+
11
+ if (response.headers[this.refreshTokenHeaderKey]) {
12
+ this.refreshToken = response.headers[this.refreshTokenHeaderKey];
13
+ }
14
+ return response;
15
+ };
package/http/index.js ADDED
@@ -0,0 +1,159 @@
1
+ const { EventEmitter } = require("events");
2
+ const axios = require("axios");
3
+ const defs = require("../defs").http;
4
+ const handleResponseError = require("./handleResponseError");
5
+ const handleResponseSuccess = require("./handleResponseSuccess");
6
+
7
+ const useCases = require("../useCases/http.json");
8
+
9
+ /*
10
+ Creates methods for each use case in the API
11
+
12
+ @param {Object} Client - The client class
13
+ @returns undefined
14
+ */
15
+
16
+ function useCaseMethodFactory(Client) {
17
+ const map = new Map(useCases);
18
+ for (const [useCase, data] of map.entries()) {
19
+ if (data.http) {
20
+ const { method, path } = data.http;
21
+
22
+ /*
23
+ Creates a method for each use case in the API
24
+ @param {Object} options - The options to send to the API
25
+ @param {Object} options.headers - The headers to send to the API
26
+ @param {Object} options.params - The params for the reuquest to build the URL
27
+ @param {Object} options.data - The body to send to the API. Valid for POST and PUT requests
28
+ @parms {Object} options.query - The query for the request to build the URL
29
+ @returns {Promise} The response from the API
30
+ */
31
+
32
+ //eslint-disable-next-line no-inner-declarations
33
+ function UseCase(options) {
34
+ if (!options) options = {};
35
+
36
+ // get parameters from path
37
+ const pathParams = path.match(/:[a-zA-Z0-9]+/g);
38
+
39
+ const { data, params, query, headers } = options;
40
+
41
+ // replace path parameters with values from params
42
+ let url = path;
43
+ if (pathParams && params) {
44
+ for (const param of pathParams) {
45
+ const paramName = param.replace(":", "");
46
+ url = url.replace(param, params[paramName]);
47
+ }
48
+ }
49
+
50
+ // make sure all parameters have been replaced
51
+ if (url.match(/:[a-zA-Z0-9]+/g)) {
52
+ const missingParams = url.match(/:[a-zA-Z0-9]+/g);
53
+ // remove the colon from the parameter name
54
+ const missing = missingParams.map((param) => param.replace(":", ""));
55
+ return Promise.reject(
56
+ new Error(`Missing parameters: ${missing.join(", ")}`)
57
+ );
58
+ }
59
+
60
+ const config = {
61
+ method: method.toLowerCase(),
62
+ url: url,
63
+ };
64
+
65
+ if (data) config.data = data;
66
+ if (query) config.params = query;
67
+ if (headers) config.headers = headers;
68
+
69
+ return this._http.request(config);
70
+ }
71
+
72
+ Client.prototype[useCase] = UseCase;
73
+ }
74
+ }
75
+ }
76
+
77
+ class KohostApiClient extends EventEmitter {
78
+ /*
79
+ @param {Object} options - The options to create the client
80
+ @param {String} options.tenantId - The tenant ID
81
+ @param {String} options.url - The base URL for the API endpint
82
+ @param {Object} options.headers - Additional headers to send with each request
83
+
84
+ */
85
+ constructor(
86
+ options = {
87
+ url: "",
88
+ tenantId: "",
89
+ headers: {},
90
+ }
91
+ ) {
92
+ super();
93
+ if (!options.url) throw new Error("options.url is required");
94
+ if (!options.tenantId) throw new Error("options.tenant is required");
95
+ this.options = options;
96
+ // eslint-disable-next-line no-undef
97
+ this.isBrower = typeof window !== "undefined";
98
+ this._http = axios.create({
99
+ baseURL: options.url,
100
+ responseType: "json",
101
+ headers: {
102
+ "Content-Type": "application/json",
103
+ Accept: "application/json",
104
+ [defs.tenantHeader]: options.tenantId,
105
+ ...options.headers,
106
+ },
107
+ });
108
+
109
+ this._http.interceptors.response.use(
110
+ handleResponseSuccess.bind(this),
111
+ handleResponseError.bind(this)
112
+ );
113
+
114
+ this._http.interceptors.request.use((config) => {
115
+ config.headers[defs.authTokenHeader] = this.authToken;
116
+ return config;
117
+ });
118
+ }
119
+
120
+ get authTokenHeaderKey() {
121
+ return defs.authTokenHeader.toLowerCase();
122
+ }
123
+
124
+ get refreshTokenHeaderKey() {
125
+ return defs.refreshTokenHeader.toLowerCase();
126
+ }
127
+
128
+ get lsTokenKey() {
129
+ return `${this.options.tenantId}_${defs.authTokenHeader}`;
130
+ }
131
+
132
+ get authToken() {
133
+ if (this.isBrower) {
134
+ // get token from localStorage
135
+ // eslint-disable-next-line no-undef
136
+ return window.localStorage.getItem(this.lsTokenKey);
137
+ } else {
138
+ return this._authToken;
139
+ }
140
+ }
141
+
142
+ /*
143
+ @param {String} token - The token to set
144
+ @returns undefined
145
+ */
146
+
147
+ set authToken(token) {
148
+ if (this.isBrower) {
149
+ // eslint-disable-next-line no-undef
150
+ window.localStorage.setItem(this.lsTokenKey, token);
151
+ } else {
152
+ this._authToken = token;
153
+ }
154
+ }
155
+ }
156
+
157
+ useCaseMethodFactory(KohostApiClient);
158
+
159
+ module.exports = KohostApiClient;
package/index.js CHANGED
@@ -1,73 +1,23 @@
1
- const Ajv = require("ajv");
2
- const ajv = new Ajv({ allErrors: true });
3
-
4
- const deviceSchema = require("./schemas/device.json");
5
- const switchSchema = require("./schemas/switch.json");
6
- const alarmSchema = require("./schemas/alarm.json");
7
- const dimmerSchema = require("./schemas/dimmer.json");
8
- const gatewaySchema = require("./schemas/gateway.json");
9
- const lockSchema = require("./schemas/lock.json");
10
- const sceneControllerSchema = require("./schemas/sceneController.json");
11
- const thermostatSchema = require("./schemas/thermostat.json");
12
- const windowCoveringSchema = require("./schemas/windowCovering.json");
13
-
14
- ajv.addSchema(deviceSchema);
15
-
16
- // compile all schemas
17
- const Switch = createClass(ajv.compile(switchSchema), "Switch");
18
- const Alarm = createClass(ajv.compile(alarmSchema), "Alarm");
19
- const Dimmer = createClass(ajv.compile(dimmerSchema), "Dimmer");
20
- const Gateway = createClass(ajv.compile(gatewaySchema), "Gateway");
21
- const Lock = createClass(ajv.compile(lockSchema), "Lock");
22
- const SceneController = createClass(
23
- ajv.compile(sceneControllerSchema),
24
- "SceneController"
25
- );
26
- const Thermostat = createClass(ajv.compile(thermostatSchema), "Thermostat");
27
- const WindowCovering = createClass(
28
- ajv.compile(windowCoveringSchema),
29
- "WindowCovering"
30
- );
31
-
32
- function createClass(validator, name) {
33
- class Generic {
34
- constructor(data) {
35
- // apply schema properties to the class
36
- //this._validator = validator;
37
- this.validate(data);
38
- Object.keys(validator.schema.properties).forEach((key) => {
39
- if (data[key] !== undefined) this[key] = data[key];
40
- });
41
- }
42
-
43
- validate(data) {
44
- const valid = this._validator(data);
45
- if (!valid)
46
- throw new Error(`Invalid ${name}`, { cause: this._validator.errors });
47
- }
48
- }
49
-
50
- // change the name of the class
51
- Object.defineProperty(Generic, "name", { value: name });
52
- Generic.prototype._validator = validator;
53
- return Generic;
54
- }
55
-
56
- // Thermostat.prototype.toCelsius = function () {
57
- // if (this.temperatureScale === "farenheit")
58
- // this.currentTemperature = ((this.currentTemperature - 32) * 5) / 9;
59
- // };
60
-
61
- // export all classes on the Models object
62
- module.exports = {
63
- Models: {
64
- Switch,
65
- Alarm,
66
- Dimmer,
67
- Gateway,
68
- Lock,
69
- SceneController,
70
- Thermostat,
71
- WindowCovering,
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");
9
+
10
+ const Kohost = {
11
+ Models,
12
+ Errors,
13
+ Commands,
14
+ Events,
15
+ defs,
16
+ Client: HttpClient,
17
+ utils: {
18
+ getFormalDeviceType,
19
+ getDeviceTypes,
72
20
  },
73
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;
@@ -0,0 +1,29 @@
1
+ // create the Alarm Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/alarm.json");
4
+ const Kohost = require("./kohost");
5
+
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
+ });
28
+
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;
@@ -0,0 +1,33 @@
1
+ // create the Courtesy Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/courtesy.json");
4
+ const Kohost = require("./kohost");
5
+
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
+ });
32
+
33
+ module.exports = Courtesy;
@@ -0,0 +1,49 @@
1
+ // Create the Dimmer Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/dimmer.json");
4
+ const Kohost = require("./kohost");
5
+
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
+ });
48
+
49
+ module.exports = Dimmer;