@kohost/api-client 3.0.0-beta.5 → 3.0.0-beta.50

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 (210) hide show
  1. package/README.md +71 -0
  2. package/dist/cjs/AMQPClient/index.js +290 -0
  3. package/dist/cjs/{Client.js → Client/index.js} +1938 -986
  4. package/dist/cjs/Commands/CheckInReservationCommand.js +19 -0
  5. package/dist/cjs/Commands/Command.js +34 -0
  6. package/dist/cjs/Commands/CreateImageUploadEndpointCommand.js +17 -0
  7. package/dist/cjs/Commands/CreateShortLinkCommand.js +20 -0
  8. package/dist/cjs/Commands/DiscoverReservationRoomTypeUpsellOptionsCommand.js +20 -0
  9. package/dist/cjs/Commands/DiscoverReservationsCommand.js +20 -0
  10. package/dist/cjs/Commands/DiscoverRoomTypesCommand.js +19 -0
  11. package/dist/cjs/Commands/DiscoverRoomsCommand.js +35 -0
  12. package/dist/cjs/Commands/DiscoverUsersCommand.js +19 -0
  13. package/dist/cjs/Commands/GetMobileKeyCommand.js +24 -0
  14. package/dist/cjs/Commands/GetProductsCommand.js +17 -0
  15. package/dist/cjs/Commands/OCRDocumentCommand.js +15 -0
  16. package/dist/cjs/Commands/SellProductsCommand.js +17 -0
  17. package/dist/cjs/Commands/SendEmailCommand.js +24 -0
  18. package/dist/cjs/Commands/SendSMSCommand.js +21 -0
  19. package/dist/cjs/Commands/SetAlarmCommand.js +17 -0
  20. package/dist/cjs/Commands/SetCourtesyCommand.js +17 -0
  21. package/dist/cjs/Commands/SetDimmerCommand.js +17 -0
  22. package/dist/cjs/Commands/SetLockCommand.js +17 -0
  23. package/dist/cjs/Commands/SetMediaCommand.js +17 -0
  24. package/dist/cjs/Commands/SetSceneCommand.js +17 -0
  25. package/dist/cjs/Commands/SetSwitchCommand.js +17 -0
  26. package/dist/cjs/Commands/SetThermostatCommand.js +17 -0
  27. package/dist/cjs/Commands/SetWindowCoveringCommand.js +17 -0
  28. package/dist/cjs/Commands/UpdateReservationCommand.js +19 -0
  29. package/dist/cjs/Commands/UpdateUserCommand.js +41 -0
  30. package/dist/cjs/Commands/UploadImageCommand.js +17 -0
  31. package/dist/cjs/Commands/index.js +55 -0
  32. package/dist/cjs/Errors/AppError.js +8 -0
  33. package/dist/cjs/Errors/AuthenticationError.js +9 -0
  34. package/dist/cjs/Errors/AuthorizationError.js +9 -0
  35. package/dist/cjs/Errors/DeviceCommError.js +9 -0
  36. package/dist/cjs/Errors/LoginError.js +9 -0
  37. package/dist/cjs/Errors/NotFoundError.js +9 -0
  38. package/dist/cjs/Errors/RequestError.js +9 -0
  39. package/dist/cjs/Errors/SystemCommError.js +9 -0
  40. package/dist/cjs/Errors/TokenExpiredError.js +9 -0
  41. package/dist/cjs/Errors/UnprocessableRequestError.js +9 -0
  42. package/dist/cjs/Errors/ValidationError.js +9 -0
  43. package/dist/cjs/Errors/index.js +15 -0
  44. package/dist/cjs/Events/ApplicationInUseEvent.js +17 -0
  45. package/dist/cjs/Events/ApplicationOutOfUseEvent.js +17 -0
  46. package/dist/cjs/Events/EmailEvent.js +18 -0
  47. package/dist/cjs/Events/Event.js +55 -0
  48. package/dist/cjs/Events/SMSEvent.js +18 -0
  49. package/dist/cjs/Events/SMSSentEvent.js +17 -0
  50. package/dist/cjs/Events/ShortLinkCreatedEvent.js +17 -0
  51. package/dist/cjs/Events/SystemCameraUpdatedEvent.js +17 -0
  52. package/dist/cjs/Events/SystemCourtesyUpdatedEvent.js +17 -0
  53. package/dist/cjs/Events/SystemDimmerUpdatedEvent.js +17 -0
  54. package/dist/cjs/Events/SystemGatewayUpdatedEvent.js +17 -0
  55. package/dist/cjs/Events/SystemLockUpdatedEvent.js +17 -0
  56. package/dist/cjs/Events/SystemMediaSourceUpdatedEvent.js +17 -0
  57. package/dist/cjs/Events/SystemMotionSensorUpdatedEvent.js +17 -0
  58. package/dist/cjs/Events/SystemProductUpdatedEvent.js +17 -0
  59. package/dist/cjs/Events/SystemReservationUpdatedEvent.js +17 -0
  60. package/dist/cjs/Events/SystemSceneControllerUpdatedEvent.js +17 -0
  61. package/dist/cjs/Events/SystemSpaceTypeUpdatedEvent.js +17 -0
  62. package/dist/cjs/Events/SystemSpaceUpdatedEvent.js +17 -0
  63. package/dist/cjs/Events/SystemSwitchUpdatedEvent.js +17 -0
  64. package/dist/cjs/Events/SystemThermostatUpdatedEvent.js +17 -0
  65. package/dist/cjs/Events/SystemUserUpdatedEvent.js +17 -0
  66. package/dist/cjs/Events/SystemWindowCoveringUpdatedEvent.js +17 -0
  67. package/dist/cjs/Events/index.js +50 -0
  68. package/dist/cjs/Models/Alarm.js +29 -0
  69. package/dist/cjs/Models/Camera.js +29 -0
  70. package/dist/cjs/Models/Courtesy.js +33 -0
  71. package/dist/cjs/Models/Credential.js +29 -0
  72. package/dist/cjs/Models/Dimmer.js +61 -0
  73. package/dist/cjs/Models/DiscoveredDevice.js +30 -0
  74. package/dist/cjs/Models/EmailMessage.js +29 -0
  75. package/dist/cjs/Models/EnergyReport.js +29 -0
  76. package/dist/cjs/Models/EnergyReportShard.js +29 -0
  77. package/dist/cjs/Models/Gateway.js +29 -0
  78. package/dist/cjs/Models/Identification.js +32 -0
  79. package/dist/cjs/Models/Kohost.js +96 -0
  80. package/dist/cjs/Models/Lock.js +33 -0
  81. package/dist/cjs/Models/MediaFile.js +41 -0
  82. package/dist/cjs/Models/MediaSource.js +29 -0
  83. package/dist/cjs/Models/MotionSensor.js +29 -0
  84. package/dist/cjs/Models/Organization.js +28 -0
  85. package/dist/cjs/Models/Product.js +29 -0
  86. package/dist/cjs/Models/Property.js +28 -0
  87. package/dist/cjs/Models/Reservation.js +98 -0
  88. package/dist/cjs/Models/Room.js +212 -0
  89. package/dist/cjs/Models/Scene.js +183 -0
  90. package/dist/cjs/Models/ShortLink.js +29 -0
  91. package/dist/cjs/Models/SmsMessage.js +29 -0
  92. package/dist/cjs/Models/Space.js +97 -0
  93. package/dist/cjs/Models/SpaceType.js +30 -0
  94. package/dist/cjs/Models/Switch.js +33 -0
  95. package/dist/cjs/Models/SystemUser.js +48 -0
  96. package/dist/cjs/Models/Thermostat.js +80 -0
  97. package/dist/cjs/Models/Ticket.js +105 -0
  98. package/dist/cjs/Models/User.js +73 -0
  99. package/dist/cjs/Models/WindowCovering.js +49 -0
  100. package/dist/cjs/Models/index.js +68 -0
  101. package/dist/cjs/SocketIoClient/index.js +74 -0
  102. package/dist/cjs/defs/deviceTypes.js +15 -0
  103. package/dist/cjs/defs/formalDeviceTypes.js +6 -0
  104. package/dist/cjs/defs/index.js +11 -0
  105. package/dist/cjs/{index.cjs.js → index.js} +7 -1
  106. package/dist/cjs/schemas/alarm.json +103 -0
  107. package/dist/cjs/schemas/camera.json +50 -0
  108. package/dist/cjs/schemas/courtesy.json +46 -0
  109. package/dist/cjs/schemas/credential.json +31 -0
  110. package/dist/cjs/schemas/definitions.json +191 -0
  111. package/dist/cjs/schemas/dimmer.json +43 -0
  112. package/dist/cjs/schemas/discoveredDevice.json +31 -0
  113. package/dist/cjs/schemas/emailMessage.json +71 -0
  114. package/dist/cjs/schemas/energyReport.json +86 -0
  115. package/dist/cjs/schemas/energyReportShard.json +75 -0
  116. package/dist/cjs/schemas/gateway.json +35 -0
  117. package/dist/cjs/schemas/identification.json +61 -0
  118. package/dist/cjs/schemas/lock.json +45 -0
  119. package/dist/cjs/schemas/mediaFile.json +67 -0
  120. package/dist/cjs/schemas/mediaSource.json +184 -0
  121. package/dist/cjs/schemas/motionSensor.json +32 -0
  122. package/dist/cjs/schemas/organization.json +36 -0
  123. package/dist/cjs/schemas/payment.json +47 -0
  124. package/dist/cjs/schemas/product.json +40 -0
  125. package/dist/cjs/schemas/property.json +246 -0
  126. package/dist/cjs/schemas/reservation.json +118 -0
  127. package/dist/cjs/schemas/room.json +98 -0
  128. package/dist/cjs/schemas/scene.json +121 -0
  129. package/dist/cjs/schemas/shortLink.json +30 -0
  130. package/dist/cjs/schemas/smsMessage.json +61 -0
  131. package/dist/cjs/schemas/space.json +120 -0
  132. package/dist/cjs/schemas/spaceType.json +30 -0
  133. package/dist/cjs/schemas/switch.json +42 -0
  134. package/dist/cjs/schemas/systemUser.json +95 -0
  135. package/dist/cjs/schemas/thermostat.json +153 -0
  136. package/dist/cjs/schemas/ticket.json +96 -0
  137. package/dist/cjs/schemas/user.json +152 -0
  138. package/dist/cjs/schemas/windowCovering.json +43 -0
  139. package/dist/cjs/utils/errorFactory.js +8 -0
  140. package/dist/cjs/utils/eventFactory.js +10 -0
  141. package/dist/cjs/utils/getDeviceTypes.js +7 -0
  142. package/dist/cjs/utils/getFormalDeviceType.js +5 -0
  143. package/dist/cjs/utils/index.js +11 -0
  144. package/dist/cjs/utils/schema.js +25 -0
  145. package/dist/esm/Client.js +2293 -1205
  146. package/dist/esm/Client.js.map +4 -4
  147. package/dist/esm/Commands.js +478 -73
  148. package/dist/esm/Commands.js.map +3 -3
  149. package/dist/esm/Errors.js +35 -0
  150. package/dist/esm/Errors.js.map +1 -1
  151. package/dist/esm/Events.js +266 -63
  152. package/dist/esm/Events.js.map +3 -3
  153. package/dist/esm/Models.js +2853 -833
  154. package/dist/esm/Models.js.map +4 -4
  155. package/dist/esm/SocketIoClient.js +4748 -0
  156. package/dist/esm/SocketIoClient.js.map +7 -0
  157. package/dist/esm/defs.js +407 -279
  158. package/dist/esm/defs.js.map +4 -4
  159. package/dist/esm/index.js +17 -10
  160. package/dist/esm/utils.js +742 -85
  161. package/dist/esm/utils.js.map +3 -3
  162. package/dist/useCases/{AdminUpdateProperty.js → AdminUpdateCustomer.js} +2 -2
  163. package/dist/useCases/AssignSpaceToReservation.js +32 -0
  164. package/dist/useCases/BatchNotifyCheckIn.js +32 -0
  165. package/dist/useCases/CreateImageUploadEndpoint.js +32 -0
  166. package/dist/useCases/{CreateSource.js → CreateMediaSource.js} +2 -2
  167. package/dist/useCases/{AdminListCustomers.js → CreateOrganization.js} +3 -3
  168. package/dist/useCases/{LoginGateway.js → CreateProperty.js} +2 -2
  169. package/dist/useCases/{AdminCreateProperty.js → CreateSpaceType.js} +2 -2
  170. package/dist/useCases/DeleteMediaFile.js +32 -0
  171. package/dist/useCases/{DeleteSource.js → DeleteMediaSource.js} +2 -2
  172. package/dist/useCases/DeleteSpaceType.js +32 -0
  173. package/dist/useCases/DescribeLockCredential.js +32 -0
  174. package/dist/useCases/{DescribeIntegration.js → DescribeMediaSource.js} +3 -3
  175. package/dist/useCases/DescribeMyAccessCredentials.js +32 -0
  176. package/dist/useCases/{AdminDescribeProperty.js → DescribeOrganization.js} +2 -2
  177. package/dist/useCases/DescribeProduct.js +32 -0
  178. package/dist/useCases/{ListIntegrations.js → DescribeProperty.js} +3 -3
  179. package/dist/useCases/DescribeReservationEarlyCheckInProducts.js +32 -0
  180. package/dist/useCases/DescribeReservationRoomUpgrades.js +32 -0
  181. package/dist/useCases/{DeleteIntegration.js → DescribeSpaceType.js} +2 -2
  182. package/dist/useCases/ListMediaSources.js +32 -0
  183. package/dist/useCases/{AdminListProperties.js → ListMyTickets.js} +2 -2
  184. package/dist/useCases/{UpdateIntegration.js → ListOrganizations.js} +4 -4
  185. package/dist/useCases/ListProperties.js +32 -0
  186. package/dist/useCases/ListScenes.js +32 -0
  187. package/dist/useCases/ListSpaceTypes.js +32 -0
  188. package/dist/useCases/ListTeam.js +32 -0
  189. package/dist/useCases/ListUserSpaces.js +32 -0
  190. package/dist/useCases/LogoutUser.js +32 -0
  191. package/dist/useCases/PurchaseReservationEarlyCheckInProducts.js +32 -0
  192. package/dist/useCases/{CreateIntegrationDeviceMapEntry.js → PurchaseReservationRoomUpgrades.js} +2 -2
  193. package/dist/useCases/RequestLoginLink.js +1 -1
  194. package/dist/useCases/{CreateIntegration.js → RequestMyKeyToken.js} +2 -2
  195. package/dist/useCases/SendCheckInSMS.js +32 -0
  196. package/dist/useCases/{SetSource.js → SetDimmer.js} +2 -2
  197. package/dist/useCases/{DescribeSource.js → SetMediaSource.js} +3 -3
  198. package/dist/useCases/{UpdateSource.js → SetRoomScene.js} +3 -3
  199. package/dist/useCases/SetSpaceScene.js +32 -0
  200. package/dist/useCases/UpdateMediaSource.js +32 -0
  201. package/dist/useCases/UpdateProperty.js +32 -0
  202. package/dist/useCases/UpdateSpaceType.js +32 -0
  203. package/dist/useCases/{ListSources.js → UploadImage.js} +4 -4
  204. package/package.json +25 -10
  205. package/dist/cjs/Commands.js +0 -568
  206. package/dist/cjs/Errors.js +0 -174
  207. package/dist/cjs/Events.js +0 -375
  208. package/dist/cjs/Models.js +0 -3582
  209. package/dist/cjs/defs.js +0 -175
  210. package/dist/cjs/utils.js +0 -246
@@ -0,0 +1,48 @@
1
+ // Create the User Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/systemUser.json");
4
+ const Kohost = require("./Kohost");
5
+
6
+ schemas.add(schema);
7
+
8
+ const validator = schemas.compile(schema);
9
+
10
+ class SystemUser extends Kohost {
11
+ constructor(data) {
12
+ super(data);
13
+ }
14
+
15
+ static validatePhone(phoneNumber) {
16
+ const regex = /^\+?[1-9]\d{1,14}$/;
17
+ return regex.test(phoneNumber);
18
+ }
19
+
20
+ static validateEmail(email) {
21
+ //eslint-disable-next-line no-useless-escape
22
+ const regex =
23
+ /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
24
+ return regex.test(email);
25
+ }
26
+ }
27
+
28
+ Object.defineProperty(SystemUser.prototype, "schema", {
29
+ value: schema,
30
+ });
31
+
32
+ Object.defineProperty(SystemUser.prototype, "validator", {
33
+ get: function () {
34
+ return validator;
35
+ },
36
+ });
37
+
38
+ Object.defineProperty(SystemUser, "validProperties", {
39
+ value: Object.keys(schema.properties),
40
+ });
41
+
42
+ Object.defineProperty(SystemUser.prototype, "fullName", {
43
+ get: function () {
44
+ return `${this.firstName} ${this.lastName}`;
45
+ },
46
+ });
47
+
48
+ module.exports = SystemUser;
@@ -0,0 +1,80 @@
1
+ const schemas = require("../utils/schema");
2
+ const schema = require("../schemas/thermostat.json");
3
+ const Kohost = require("./Kohost");
4
+
5
+ schemas.add(schema);
6
+ const validator = schemas.compile(schema);
7
+
8
+ class Thermostat extends Kohost {
9
+ constructor(data) {
10
+ super(data);
11
+ }
12
+
13
+ toCelsius() {
14
+ if (this.temperatureScale === "fahrenheit")
15
+ this.currentTemperature = ((this.currentTemperature - 32) * 5) / 9;
16
+ this.temperatureScale = "celsius";
17
+ return this.currentTemperature;
18
+ }
19
+
20
+ toFahrenheit() {
21
+ if (this.temperatureScale === "celsius")
22
+ this.currentTemperature = (this.currentTemperature * 9) / 5 + 32;
23
+ this.temperatureScale = "fahrenheit";
24
+ return this.currentTemperature;
25
+ }
26
+
27
+ static getActionDelta(old, _new) {
28
+ const delta = {};
29
+ for (const action in _new) {
30
+ if (this.actionProperties.includes(action)) {
31
+ switch (action) {
32
+ case "hvacMode":
33
+ case "fanMode": {
34
+ if (old[action] !== _new[action]) delta[action] = 1;
35
+ break;
36
+ }
37
+ case "setpoints": {
38
+ const setpoints = _new[action];
39
+ for (const setpoint in setpoints) {
40
+ if (old[action][setpoint].value !== setpoints[setpoint].value) {
41
+ const min =
42
+ setpoints[setpoint].min || old[action][setpoint].min;
43
+ const max =
44
+ setpoints[setpoint].max || old[action][setpoint].max;
45
+ const oldValue = old[action][setpoint].value;
46
+ const value = setpoints[setpoint].value;
47
+ // get percentage change relative to min and max
48
+ const percentChange = (value - oldValue) / (max - min);
49
+ // get the delta
50
+ delta[`setpoints.${setpoint}`] = percentChange;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ return delta;
59
+ }
60
+ }
61
+
62
+ Object.defineProperty(Thermostat.prototype, "schema", {
63
+ value: schema,
64
+ });
65
+
66
+ Object.defineProperty(Thermostat.prototype, "validator", {
67
+ get: function () {
68
+ return validator;
69
+ },
70
+ });
71
+
72
+ Object.defineProperty(Thermostat, "validProperties", {
73
+ value: Object.keys(schema.properties),
74
+ });
75
+
76
+ Object.defineProperty(Thermostat, "actionProperties", {
77
+ value: ["hvacMode", "fanMode", "setpoints"],
78
+ });
79
+
80
+ module.exports = Thermostat;
@@ -0,0 +1,105 @@
1
+ // Create the User Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/ticket.json");
4
+ const Kohost = require("./Kohost");
5
+ const MediaFile = require("./MediaFile");
6
+
7
+ const sortBy = require("lodash.sortby");
8
+ const findLast = require("lodash.findlast");
9
+
10
+ const { nanoid } = require("nanoid");
11
+ const cloneDeep = require("lodash.clonedeep");
12
+
13
+ schemas.add(schema);
14
+ const validator = schemas.compile(schema);
15
+
16
+ class Ticket extends Kohost {
17
+ constructor(data) {
18
+ const ticketData = mapConversationData(data);
19
+ super(ticketData);
20
+ }
21
+
22
+ static generateMessageId(len = 16) {
23
+ return nanoid(len);
24
+ }
25
+ }
26
+
27
+ Object.defineProperty(Ticket.prototype, "schema", {
28
+ value: schema,
29
+ });
30
+
31
+ Object.defineProperty(Ticket.prototype, "validator", {
32
+ get: function () {
33
+ return validator;
34
+ },
35
+ });
36
+
37
+ Object.defineProperty(Ticket, "validProperties", {
38
+ value: Object.keys(schema.properties),
39
+ });
40
+
41
+ Object.defineProperty(Ticket.prototype, "responseTime", {
42
+ get: function () {
43
+ const conversation = this.conversation;
44
+ const requester = this.requester;
45
+
46
+ if (conversation.length === 0) return 0;
47
+ const mapped = conversation.map((msg) => {
48
+ if (typeof msg.timestamp === "string")
49
+ msg.timestamp = new Date(msg.timestamp);
50
+ return msg;
51
+ });
52
+ const sorted = sortBy(mapped, ["timestamp"]);
53
+ const firstMsg = sorted.find((entry) => entry.userId === requester);
54
+ const firstResponse = sorted.find((entry) => entry.userId !== requester);
55
+
56
+ if (firstMsg && firstResponse) {
57
+ const firstMsgTime = firstMsg.timestamp.getTime() / 1000;
58
+ const firstResponseTime = firstResponse.timestamp.getTime() / 1000;
59
+ return firstResponseTime - firstMsgTime;
60
+ }
61
+
62
+ return 0;
63
+ },
64
+ });
65
+
66
+ Object.defineProperty(Ticket.prototype, "resolutionTime", {
67
+ get: function () {
68
+ if (this.status !== "closed") return 0;
69
+ const createdAt = this.createdAt.getTime() / 1000;
70
+ const solvedAt = this.solvedAt
71
+ ? this.solvedAt.getTime() / 1000
72
+ : this.updatedAt.getTime() / 1000;
73
+
74
+ return Math.abs(solvedAt - createdAt);
75
+ },
76
+ });
77
+
78
+ Object.defineProperty(Ticket.prototype, "lastResponder", {
79
+ get: function () {
80
+ const conversation = this.conversation;
81
+ const requester = this.requester;
82
+
83
+ const sorted = sortBy(conversation, ["timestamp"]);
84
+
85
+ const lastFromNonRequester = findLast(sorted, function (c) {
86
+ return c.userId !== requester;
87
+ });
88
+
89
+ if (!lastFromNonRequester) return null;
90
+ else return lastFromNonRequester.userId;
91
+ },
92
+ });
93
+
94
+ function mapConversationData(data) {
95
+ const ticketData = cloneDeep(data);
96
+ ticketData.conversation = ticketData.conversation.map((msg) => {
97
+ if (msg.media) {
98
+ msg.media = new MediaFile(msg.media);
99
+ }
100
+ return msg;
101
+ });
102
+ return ticketData;
103
+ }
104
+
105
+ module.exports = Ticket;
@@ -0,0 +1,73 @@
1
+ // Create the User Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/user.json");
4
+ const paymentSchema = require("../schemas/payment.json");
5
+ const Kohost = require("./Kohost");
6
+ const MediaFile = require("./MediaFile");
7
+ const Reservation = require("./Reservation");
8
+
9
+ const { nanoid } = require("nanoid/async");
10
+
11
+ schemas.add(paymentSchema);
12
+ schemas.add(schema);
13
+
14
+ const validator = schemas.compile(schema);
15
+
16
+ class User extends Kohost {
17
+ constructor(data) {
18
+ if (data.photo) data.photo = new MediaFile(data.photo);
19
+ if (data.reservations)
20
+ data.reservations = data.reservations.map((res) => new Reservation(res));
21
+ super(data);
22
+ }
23
+
24
+ static validatePhone(phoneNumber) {
25
+ const regex = /^\+?[1-9]\d{1,14}$/;
26
+ return regex.test(phoneNumber);
27
+ }
28
+
29
+ static validateEmail(email) {
30
+ //eslint-disable-next-line no-useless-escape
31
+ const regex =
32
+ /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
33
+ return regex.test(email);
34
+ }
35
+
36
+ static async generatePassword(len = 16) {
37
+ return await nanoid(len);
38
+ }
39
+ }
40
+
41
+ Object.defineProperty(User.prototype, "schema", {
42
+ value: schema,
43
+ });
44
+
45
+ Object.defineProperty(User.prototype, "validator", {
46
+ get: function () {
47
+ return validator;
48
+ },
49
+ });
50
+
51
+ Object.defineProperty(User, "validProperties", {
52
+ value: Object.keys(schema.properties),
53
+ });
54
+
55
+ Object.defineProperty(User.prototype, "fullName", {
56
+ get: function () {
57
+ return `${this.firstName} ${this.lastName}`;
58
+ },
59
+ });
60
+
61
+ Object.defineProperty(User.prototype, "roles", {
62
+ get: function () {
63
+ const roles = new Set();
64
+ if (this.permissions) {
65
+ for (const permission of this.permissions) {
66
+ roles.add(permission.role);
67
+ }
68
+ }
69
+ return Array.from(roles);
70
+ },
71
+ });
72
+
73
+ module.exports = User;
@@ -0,0 +1,49 @@
1
+ // Create the WindowCovering Model
2
+ const schemas = require("../utils/schema");
3
+ const schema = require("../schemas/windowCovering.json");
4
+ const Kohost = require("./Kohost");
5
+
6
+ schemas.add(schema);
7
+ const validator = schemas.compile(schema);
8
+
9
+ class WindowCovering 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 === "position") {
19
+ const oldPos = old[action];
20
+ const newPos = _new[action];
21
+ delta[action] = newPos - oldPos / 100;
22
+ } else if (old[action] !== _new[action]) {
23
+ delta[action] = 1;
24
+ }
25
+ }
26
+ }
27
+ return delta;
28
+ }
29
+ }
30
+
31
+ Object.defineProperty(WindowCovering.prototype, "schema", {
32
+ value: schema,
33
+ });
34
+
35
+ Object.defineProperty(WindowCovering.prototype, "validator", {
36
+ get: function () {
37
+ return validator;
38
+ },
39
+ });
40
+
41
+ Object.defineProperty(WindowCovering, "validProperties", {
42
+ value: Object.keys(schema.properties),
43
+ });
44
+
45
+ Object.defineProperty(WindowCovering, "actionProperties", {
46
+ value: ["position"],
47
+ });
48
+
49
+ module.exports = WindowCovering;
@@ -0,0 +1,68 @@
1
+ const Reservation = require("./Reservation");
2
+ const MediaFile = require("./MediaFile");
3
+ const Switch = require("./Switch");
4
+ const Alarm = require("./Alarm");
5
+ const Dimmer = require("./Dimmer");
6
+ const Lock = require("./Lock");
7
+ const Thermostat = require("./Thermostat");
8
+ const WindowCovering = require("./WindowCovering");
9
+ const Identification = require("./Identification");
10
+ const User = require("./User");
11
+ const SystemUser = require("./SystemUser");
12
+ const Courtesy = require("./Courtesy");
13
+ const Camera = require("./Camera");
14
+ const MotionSensor = require("./MotionSensor");
15
+ const MediaSource = require("./MediaSource");
16
+ const Room = require("./Room");
17
+
18
+ const Space = require("./Space");
19
+ const SpaceType = require("./SpaceType");
20
+
21
+ const Ticket = require("./Ticket");
22
+ const Scene = require("./Scene");
23
+ const Gateway = require("./Gateway");
24
+ const Product = require("./Product");
25
+ const DiscoveredDevice = require("./DiscoveredDevice");
26
+ const Credential = require("./Credential");
27
+ const ShortLink = require("./ShortLink");
28
+ const EnergyReportShard = require("./EnergyReportShard");
29
+ const EnergyReport = require("./EnergyReport");
30
+ const SMSMessage = require("./SmsMessage");
31
+ const EmailMessage = require("./EmailMessage");
32
+
33
+ const Property = require("./Property");
34
+ const Organization = require("./Organization");
35
+
36
+ module.exports = {
37
+ Organization,
38
+ Property,
39
+ MediaFile,
40
+ Gateway,
41
+ Switch,
42
+ Alarm,
43
+ Dimmer,
44
+ Lock,
45
+ Courtesy,
46
+ Camera,
47
+ MotionSensor,
48
+ Thermostat,
49
+ WindowCovering,
50
+ MediaSource,
51
+ Identification,
52
+ Product,
53
+ User,
54
+ SystemUser,
55
+ Room,
56
+ Space,
57
+ SpaceType,
58
+ Ticket,
59
+ Scene,
60
+ DiscoveredDevice,
61
+ Reservation,
62
+ Credential,
63
+ ShortLink,
64
+ EnergyReportShard,
65
+ EnergyReport,
66
+ SMSMessage,
67
+ EmailMessage,
68
+ };
@@ -0,0 +1,74 @@
1
+ const io = require("socket.io-client");
2
+ const { EventEmitter } = require("events");
3
+
4
+ module.exports = class SocketIoClient extends EventEmitter {
5
+ constructor(config = { url: null, propertyId: null, options: {} }) {
6
+ super();
7
+ if (!config.url) throw new Error("Websocket URL / endpoint not provided");
8
+ if (!config.propertyId) throw new Error("Property ID not provided");
9
+ this.url = config.url;
10
+ this.propertyId = config.propertyId;
11
+ this.options = {
12
+ autoConnect: false,
13
+ forceNew: false,
14
+ reconnection: true,
15
+ reconnectionAttempts: Infinity,
16
+ reconnectionDelay: 1000,
17
+ withCredentials: true,
18
+ transports: ["websocket", "polling"],
19
+ upgrade: true,
20
+ ...config.options,
21
+ };
22
+
23
+ this.socket = io(this.url, this.options);
24
+
25
+ this.socket.on("connect", () => {
26
+ this.emit("connect", this.socket);
27
+ });
28
+
29
+ this.socket.on("disconnect", (reason) => {
30
+ this.emit("disconnect", reason);
31
+ });
32
+
33
+ this.socket.on("reconnect_attempt", (data) => {
34
+ this.emit("reconnect_attempt", data);
35
+ });
36
+
37
+ this.socket.on("connect_error", (error) => {
38
+ this.emit("connect_error", error);
39
+ });
40
+ }
41
+
42
+ get connected() {
43
+ return this.socket?.connected || false;
44
+ }
45
+
46
+ get disconnected() {
47
+ return this.socket?.disconnected || false;
48
+ }
49
+
50
+ connect() {
51
+ this.socket.connect();
52
+ }
53
+
54
+ disconnect() {
55
+ this.socket.disconnect();
56
+ }
57
+
58
+ reconnect() {
59
+ this.disconnect();
60
+ this.connect();
61
+ }
62
+
63
+ subscribe(event, callback) {
64
+ this.socket.on(event, callback);
65
+ }
66
+
67
+ unsubscribe(event, callback) {
68
+ this.socket.off(event, callback);
69
+ }
70
+
71
+ send(event, { data = {}, query = {}, ...rest }) {
72
+ this.socket.emit(event, { data, query, ...rest });
73
+ }
74
+ };
@@ -0,0 +1,15 @@
1
+ const types = [
2
+ "dimmer",
3
+ "switch",
4
+ "thermostat",
5
+ "lock",
6
+ "windowCovering",
7
+ "courtesy",
8
+ "alarm",
9
+ "camera",
10
+ "mediaSource",
11
+ "motionSensor",
12
+ "gateway",
13
+ ];
14
+
15
+ module.exports = types;
@@ -0,0 +1,6 @@
1
+ const deviceTypes = require("./deviceTypes");
2
+ const formalDeviceTypes = deviceTypes.map(
3
+ (type) => type.charAt(0).toUpperCase() + type.slice(1)
4
+ );
5
+
6
+ module.exports = formalDeviceTypes;
@@ -0,0 +1,11 @@
1
+ const deviceTypes = require("./deviceTypes");
2
+ const formalDeviceTypes = require("./formalDeviceTypes");
3
+ const { defs: httpDefs } = require("../Client");
4
+
5
+ const defs = {
6
+ http: httpDefs,
7
+ deviceTypes,
8
+ formalDeviceTypes,
9
+ };
10
+
11
+ module.exports = defs;
@@ -5,13 +5,19 @@ const Events = require("./Events");
5
5
  const defs = require("./defs");
6
6
  const utils = require("./utils");
7
7
  const Client = require("./Client");
8
+ const SocketIoClient = require("./SocketIoClient");
9
+ const AMQPClient = require("./AMQPClient");
10
+
8
11
  const Kohost = {
9
12
  Models,
10
13
  Errors,
11
14
  Commands,
12
15
  Events,
13
16
  Client,
17
+ SocketIoClient,
18
+ AMQPClient,
14
19
  defs,
15
- utils
20
+ utils: utils,
16
21
  };
22
+
17
23
  module.exports = Kohost;
@@ -0,0 +1,103 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema",
3
+ "$id": "alarm.json",
4
+ "title": "Alarm",
5
+ "description": "Any smart alarm system",
6
+ "type": "object",
7
+ "properties": {
8
+ "id": {
9
+ "$ref": "definitions.json#/definitions/id"
10
+ },
11
+ "name": {
12
+ "type": "string"
13
+ },
14
+ "type": {
15
+ "$ref": "definitions.json#/definitions/type"
16
+ },
17
+ "systemData": {
18
+ "$ref": "definitions.json#/definitions/systemData"
19
+ },
20
+ "supportedNotifications": {
21
+ "$ref": "definitions.json#/definitions/supportedNotifications"
22
+ },
23
+ "notification": {
24
+ "$ref": "definitions.json#/definitions/notification"
25
+ },
26
+ "driver": {
27
+ "$ref": "definitions.json#/definitions/driver"
28
+ },
29
+ "areas": {
30
+ "type": "array",
31
+ "items": {
32
+ "type": "object",
33
+ "properties": {
34
+ "number": {
35
+ "type": "number"
36
+ },
37
+ "name": {
38
+ "type": "string"
39
+ },
40
+ "securityMode": {
41
+ "type": "string",
42
+ "enum": ["arming", "disarming", "armed", "disarmed", "alarm"]
43
+ },
44
+ "readyToArm": {
45
+ "type": "boolean"
46
+ }
47
+ },
48
+ "additionalProperties": false
49
+ }
50
+ },
51
+ "zones": {
52
+ "type": "array",
53
+ "items": {
54
+ "type": "object",
55
+ "properties": {
56
+ "number": {
57
+ "type": "number",
58
+ "minimum": 0
59
+ },
60
+ "name": {
61
+ "type": "string"
62
+ },
63
+ "secure": {
64
+ "type": "boolean"
65
+ },
66
+ "bypassed": {
67
+ "type": "boolean"
68
+ }
69
+ },
70
+ "additionalProperties": false
71
+ }
72
+ },
73
+ "supportedTroubles": {
74
+ "type": "array",
75
+ "uniqueItems": true,
76
+ "items": {
77
+ "type": "string",
78
+ "enum": ["battery", "ac", "phone", "bell", "fire"]
79
+ }
80
+ },
81
+ "troubles": {
82
+ "type": "array",
83
+ "uniqueItems": true,
84
+ "items": {
85
+ "$ref": "#/properties/supportedTroubles/items"
86
+ }
87
+ },
88
+ "watts": {
89
+ "$ref": "definitions.json#/definitions/watts"
90
+ }
91
+ },
92
+ "additionalProperties": false,
93
+ "required": [
94
+ "id",
95
+ "type",
96
+ "systemData",
97
+ "areas",
98
+ "zones",
99
+ "supportedTroubles",
100
+ "troubles",
101
+ "driver"
102
+ ]
103
+ }