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

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.
package/index.js CHANGED
@@ -1,73 +1,6 @@
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
- // };
1
+ const Models = require("./models");
60
2
 
61
3
  // export all classes on the Models object
62
4
  module.exports = {
63
- Models: {
64
- Switch,
65
- Alarm,
66
- Dimmer,
67
- Gateway,
68
- Lock,
69
- SceneController,
70
- Thermostat,
71
- WindowCovering,
72
- },
5
+ Models,
73
6
  };
@@ -0,0 +1,7 @@
1
+ // create the Alarm Model
2
+ const { createModel } = require("../utils/compiler");
3
+ const alarmSchema = require("../schemas/alarm.json");
4
+
5
+ const Alarm = createModel({ schema: alarmSchema, name: "Alarm" });
6
+
7
+ module.exports = Alarm;
@@ -0,0 +1,7 @@
1
+ // create the Courtesy Model
2
+ const { createModel } = require("../utils/compiler");
3
+ const courtesySchema = require("../schemas/courtesy.json");
4
+
5
+ const Courtesy = createModel({ schema: courtesySchema, name: "Courtesy" });
6
+
7
+ module.exports = Courtesy;
@@ -0,0 +1,7 @@
1
+ // Create the Dimmer Model
2
+ const { createModel } = require("../utils/compiler");
3
+ const dimmerSchema = require("../schemas/dimmer.json");
4
+
5
+ const Dimmer = createModel({ schema: dimmerSchema, name: "Dimmer" });
6
+
7
+ module.exports = Dimmer;
@@ -0,0 +1,7 @@
1
+ // Create the Gateway Model
2
+ const { createModel } = require("../utils/compiler");
3
+ const gatewaySchema = require("../schemas/gateway.json");
4
+
5
+ const Gateway = createModel({ schema: gatewaySchema, name: "Gateway" });
6
+
7
+ module.exports = Gateway;
@@ -0,0 +1,19 @@
1
+ const Gateway = require("./gateway");
2
+ const Switch = require("./switch");
3
+ const Alarm = require("./alarm");
4
+ const Dimmer = require("./dimmer");
5
+ const Lock = require("./lock");
6
+ const SceneController = require("./sceneController");
7
+ const Thermostat = require("./thermostat");
8
+ const WindowCovering = require("./windowCovering");
9
+
10
+ module.exports = {
11
+ Gateway,
12
+ Switch,
13
+ Alarm,
14
+ Dimmer,
15
+ Lock,
16
+ SceneController,
17
+ Thermostat,
18
+ WindowCovering,
19
+ };
package/models/lock.js ADDED
@@ -0,0 +1,7 @@
1
+ // Create the Lock Model
2
+ const { createModel } = require("../utils/compiler");
3
+ const lockSchema = require("../schemas/lock.json");
4
+
5
+ const Lock = createModel({ schema: lockSchema, name: "Lock" });
6
+
7
+ module.exports = Lock;
@@ -0,0 +1,10 @@
1
+ // Create the SceneController Model
2
+ const { createModel } = require("../utils/compiler");
3
+ const sceneControllerSchema = require("../schemas/sceneController.json");
4
+
5
+ const SceneController = createModel({
6
+ schema: sceneControllerSchema,
7
+ name: "SceneController",
8
+ });
9
+
10
+ module.exports = SceneController;
@@ -0,0 +1,7 @@
1
+ // create the Switch model
2
+ const { createModel } = require("../utils/compiler");
3
+ const switchSchema = require("../schemas/switch.json");
4
+
5
+ const Switch = createModel({ schema: switchSchema, name: "Switch" });
6
+
7
+ module.exports = Switch;
@@ -0,0 +1,40 @@
1
+ const { createModel } = require("../utils/compiler");
2
+ const thermostatSchema = require("../schemas/thermostat.json");
3
+
4
+ function toCelsius() {
5
+ if (this.temperatureScale === "farenheit")
6
+ this.currentTemperature = ((this.currentTemperature - 32) * 5) / 9;
7
+ return this.currentTemperature;
8
+ }
9
+
10
+ const Thermostat = createModel({
11
+ schema: thermostatSchema,
12
+ name: "Thermostat",
13
+ methods: [toCelsius],
14
+ });
15
+
16
+ const data = {
17
+ id: "123",
18
+ type: "thermostat",
19
+ supportedNotifications: ["idle", "motionDetection"],
20
+ systemData: {},
21
+ currentTemperature: 20,
22
+ hvacMode: "off",
23
+ fanMode: "off",
24
+ fanState: "off",
25
+ hvacState: "off",
26
+ setpoints: {
27
+ heat: {
28
+ value: 60,
29
+ min: 0,
30
+ max: 99,
31
+ },
32
+ },
33
+ temperatureScale: "farenheit",
34
+ supportedHvacModes: ["off", "heat", "cool"],
35
+ supportedFanModes: ["off", "low", "medium", "high"],
36
+ };
37
+
38
+ console.log(new Thermostat(data));
39
+
40
+ module.exports = Thermostat;
@@ -0,0 +1,10 @@
1
+ // Create the WindowCovering Model
2
+ const { createModel } = require("../utils/compiler");
3
+ const windowCoveringSchema = require("../schemas/windowCovering.json");
4
+
5
+ const WindowCovering = createModel({
6
+ schema: windowCoveringSchema,
7
+ name: "WindowCovering",
8
+ });
9
+
10
+ module.exports = WindowCovering;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "ajv": "^8.11.0"
4
4
  },
5
5
  "name": "@kohost/api-client",
6
- "version": "1.0.0-alpha.1",
6
+ "version": "1.0.0-alpha.2",
7
7
  "description": "API client for Kohost utils",
8
8
  "main": "index.js",
9
9
  "devDependencies": {},
@@ -31,7 +31,7 @@
31
31
  }
32
32
  },
33
33
  "notification": {
34
- "type": "object",
34
+ "type": ["object", "null"],
35
35
  "properties": {
36
36
  "name": {
37
37
  "type": "string",
@@ -20,6 +20,9 @@
20
20
  "notification": {
21
21
  "$ref": "https://api.kohost.app/schemas/v3/device.json#/definitions/notification"
22
22
  },
23
+ "subType": {
24
+ "type": ["string", "null"]
25
+ },
23
26
  "supportedScenes": {
24
27
  "type": "array",
25
28
  "uniqueItems": true,
@@ -28,7 +31,7 @@
28
31
  }
29
32
  },
30
33
  "scene": {
31
- "type": "object",
34
+ "type": ["object", "null"],
32
35
  "properties": {
33
36
  "name": {
34
37
  "type": "string",
@@ -39,8 +42,17 @@
39
42
  "minimum": 1655907956593
40
43
  }
41
44
  }
45
+ },
46
+ "level": {
47
+ "type": "number",
48
+ "minimum": 0,
49
+ "maximum": 100
50
+ },
51
+ "state": {
52
+ "type": "string",
53
+ "enum": ["on", "off"]
42
54
  }
43
55
  },
44
- "additionalProperties": false,
56
+ "additionalProperties": true,
45
57
  "required": ["id", "type", "systemData", "supportedScenes", "scene"]
46
58
  }
@@ -0,0 +1,40 @@
1
+ const Ajv = require("ajv");
2
+ const ajv = new Ajv({ allErrors: true });
3
+
4
+ // load definitions
5
+ const deviceSchema = require("../schemas/definitions/device.json");
6
+
7
+ ajv.addSchema(deviceSchema);
8
+
9
+ function createModel({ schema, name, methods = [] }) {
10
+ const validator = ajv.compile(schema);
11
+ class Model {
12
+ constructor(data) {
13
+ // apply schema properties to the class
14
+ this.validate(data);
15
+ Object.keys(validator.schema.properties).forEach((key) => {
16
+ if (data[key] !== undefined) this[key] = data[key];
17
+ });
18
+ }
19
+
20
+ validate(data) {
21
+ const valid = this._validator(data);
22
+ if (!valid)
23
+ throw new Error(`Invalid ${name}`, { cause: this._validator.errors });
24
+ }
25
+ }
26
+
27
+ // change the name of the class
28
+ Object.defineProperty(Model, "name", { value: name });
29
+ Model.prototype._validator = validator;
30
+ Model.prototype.schema = schema;
31
+ methods.forEach((method) => {
32
+ const methodName = method.name;
33
+ Model.prototype[methodName] = method;
34
+ });
35
+ return Model;
36
+ }
37
+
38
+ module.exports = {
39
+ createModel,
40
+ };