Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bcopy committed Sep 25, 2024
2 parents 8a4442d + fe4a961 commit c3eb8eb
Show file tree
Hide file tree
Showing 43 changed files with 1,952 additions and 755 deletions.
34 changes: 29 additions & 5 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ name: CI/CD

on:
push:
branches: [ main ]
branches: [ main, develop ]
tags:
- 'v*'
pull_request:
branches: [ main ]
branches: [ main, develop ]

jobs:

build-test-publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v2

Expand All @@ -27,23 +29,45 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Run tests
- name: Run unit tests
run: npm test

- name: Start Mosquitto
uses: namoshek/mosquitto-github-action@v1
with:
version: '1.6'
ports: '1883:1883'
container-name: 'mqtt'


- name: Run integration tests
run: npm run test:integration
env:
MQTT_BROKER_URL: mqtt://localhost:1883

- name: Build
run: npm run build

- name: Publish to GitHub Packages
run: npm publish
if: github.ref == 'refs/heads/main'
run: |
# Remove the existing "stable" tag if it exists
if npm dist-tag ls | grep -q "stable"; then
npm dist-tag rm $npm_package_name stable
fi
# Publish the package with the "stable" tag
npm version --no-git-tag-version stable
npm publish --tag stable
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

create-release:
needs: build-test-publish
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
30 changes: 27 additions & 3 deletions demo-aframe/aframe-demo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { HomieObserver, HomieEventType, createMqttHomieObserver } = HomieLit;

// Wait for A-Frame to be ready
AFRAME.registerComponent('homie-light-switch', {
init: function () {
Expand Down Expand Up @@ -29,12 +31,14 @@ AFRAME.registerComponent('homie-light-switch', {
toggleSwitch: function () {
this.switchState = !this.switchState;
this.stateProperty.setValue(this.switchState);
const bulbComponent = document.querySelector('#bulb').components["homie-light-bulb"].updateBulbState(this.switchState);
this.updateSwitchVisual();
},

updateSwitchVisual: function () {
const switchEl = this.el.querySelector('a-box');
switchEl.setAttribute('color', `${this.switchState ? '#4CC3D9' : '#4C03D9'}`);
switchEl.setAttribute('rotation', `0 ${this.switchState ? '30' : '-30'} 0`);
}
});

Expand All @@ -55,9 +59,6 @@ AFRAME.registerComponent('homie-light-bulb', {
bulbEl.setAttribute('color', '#FFF');
this.el.appendChild(bulbEl);

// Listen for property changes
// this.stateProperty.on('value', (value) => this.updateBulbState(value));

// Initialize bulb state
this.updateBulbState(false);
},
Expand All @@ -84,3 +85,26 @@ AFRAME.registerComponent('homie-light-bulb', {
}
});

// Connect HomieObserver to MQTT broker
observer = createMqttHomieObserver("ws://localhost:9001");

observer.updated$.subscribe(
(event) => {
if (event.type == 'property') {
if (event.device.id === 'switch' && event.node.id === 'switch' && event.property.id === 'state') {
document.getElementById('switch-state').textContent = `Switch State: ${event.property.value}`;
const switchComponent = document.querySelector('#switch').components["homie-light-switch"];
switchComponent.toggleSwitch();
switchComponent.updateSwitchVisual();

}
}
},
(error) => {
console.error('Error in subscription:', error);
done(error);
}
);


observer.subscribe('switch/switch/state');
21 changes: 6 additions & 15 deletions demo-aframe/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<div id="info">
<h1>Homie-Lit A-Frame Demo</h1>
<p>Click on the light switch to toggle the light bulb.</p>
<p id="switch-state">Switch State: Unknown</p>
<p id="bulb-state">Bulb State: Unknown</p>
</div>
<a-scene>
<a-camera position="0 1 1" look-controls>
Expand All @@ -33,26 +35,15 @@ <h1>Homie-Lit A-Frame Demo</h1>
geometry="primitive: ring; radiusInner: 0.015; radiusOuter: 0.02"
material="color: black; shader: flat; opacity: 0.7">
</a-entity>

</a-camera>

<a-entity homie-light-switch position="-1 0.5 -3" rotation="0 30 0"></a-entity>
<a-entity homie-light-bulb position="1 0.5 -3" rotation="0 -30 0"></a-entity>
<a-entity id="switch" homie-light-switch position="-1 0.5 -3" rotation="0 30 0"></a-entity>
<a-entity id="bulb" homie-light-bulb position="1 0.5 -3" rotation="0 -30 0"></a-entity>

<a-entity light="type: ambient; color: #BBB"></a-entity>
<a-sky color="#ECECEC"></a-sky>
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
</a-scene>
<script>
// Connect devices after scene loaded
document.querySelector('a-scene').addEventListener('loaded', function () {
const switchDevice = document.querySelector('[homie-light-switch]').components['homie-light-switch'].device;
const bulbDevice = document.querySelector('[homie-light-bulb]').components['homie-light-bulb'].device;

// // Connect switch state to bulb state (no on() subscription function available)
// switchDevice.getNode('switch').getProperty('state').on('value', (value) => {
// bulbDevice.getNode('bulb').getProperty('state').setValue(value);
// });
});
</script>

</body>
</html>
21 changes: 21 additions & 0 deletions demo-aframe/toggle_switch_tcp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# MQTT broker details
MQTT_HOST="localhost"
MQTT_PORT="1883"
MQTT_TOPIC="homie/switch/switch/state"

# Function to publish MQTT message
publish_mqtt_message() {
local state="$1"
echo "Publishing state: $state to topic: $MQTT_TOPIC"
mosquitto_pub -h "$MQTT_HOST" -p "$MQTT_PORT" -t "$MQTT_TOPIC" -m "$state"
}

# Main loop
while true; do
publish_mqtt_message "true"
sleep 2
publish_mqtt_message "false"
sleep 2
done
1 change: 0 additions & 1 deletion demo/dist

This file was deleted.

2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home-Lit Demo: Light Switch and Light Bulb</title>
<link rel="stylesheet" href="styles.css">
<script src="http://localhost:9000/homie-lit.js"></script>
<script src="/homie-lit.js"></script>
<script src="./js/main.js" type="module"></script>
</head>
<body>
Expand Down
4 changes: 2 additions & 2 deletions demo/js/lightBulb.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class LightSwitchElement extends LitElement {
<p>Switch is ${isOn ? 'On' : 'Off'}</p>
</div>
`;
}
return html`<div>No device set</div>`;
}
else return html`<div>No device set</div>`;
}

_toggleState() {
Expand Down
25 changes: 25 additions & 0 deletions dist/homie-lit.core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions dist/homie-lit.core.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
229 changes: 228 additions & 1 deletion dist/homie-lit.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions dist/homie-lit.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions dist/homie-lit.min.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*! Bundled license information:

@jspm/core/nodelibs/browser/buffer.js:
(*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> *)
*/

/**
* @license
* Copyright 2017 Google LLC
Expand Down
2 changes: 1 addition & 1 deletion dist/homie-lit.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/integrationtest/HomieObserver.integration.test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions dist/integrationtest/HomieSanityTest.integration.test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
78 changes: 78 additions & 0 deletions dist/src/HomieObserver.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/// <reference types="node" />
import { Observable } from 'rxjs';
interface HomieProperty {
id: string;
value: any;
}
interface HomieNode {
id: string;
properties: {
[key: string]: HomieProperty;
};
}
interface HomieDevice {
id: string;
nodes: {
[key: string]: HomieNode;
};
}
declare enum HomieEventType {
Device = "device",
Node = "node",
Property = "property"
}
interface HomieDeviceEvent {
type: HomieEventType.Device;
device: HomieDevice;
}
interface HomieNodeEvent {
type: HomieEventType.Node;
device: HomieDevice;
node: HomieNode;
}
interface HomiePropertyEvent {
type: HomieEventType.Property;
device: HomieDevice;
node: HomieNode;
property: HomieProperty;
}
type HomieEvent = HomieDeviceEvent | HomieNodeEvent | HomiePropertyEvent;
interface MqttMessageHandler {
handleMessage(topic: string, message: Buffer): void;
subscribe(topic: string): void;
}
declare class MqttClient implements MqttMessageHandler {
private client;
private homiePrefix;
private messageCallback;
constructor(brokerUrl: string, options: {
homiePrefix?: string | undefined;
} | undefined, messageCallback: (event: HomieEvent) => void);
subscribe(pattern: string): void;
private getSubscriptionTopic;
handleMessage(topic: string, message: Buffer): void;
private handleDeviceState;
private handleNodeState;
private handlePropertyState;
disconnect(): void;
}
declare class HomieObserver {
private messageHandler;
private devices;
private onCreate;
private onUpdate;
private onDelete;
constructor(messageHandler: MqttMessageHandler);
subscribe(topic: string): void;
get created$(): Observable<HomieEvent>;
get updated$(): Observable<HomieEvent>;
get deleted$(): Observable<HomieEvent>;
processEvent(event: HomieEvent): void;
private processDeviceEvent;
private processNodeEvent;
private processPropertyEvent;
}
declare function createMqttHomieObserver(brokerUrl: string, options?: {
homiePrefix?: string;
}): HomieObserver;
export { HomieObserver, MqttClient, MqttMessageHandler, createMqttHomieObserver, HomieEventType, HomieEvent };
29 changes: 29 additions & 0 deletions dist/src/HomiePropertyBuffer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Observable } from 'rxjs';
import { HomieObserver } from './HomieObserver';
interface BufferedPropertyUpdate {
deviceId: string;
nodeId: string;
propertyId: string;
value: any;
priority: number;
}
interface PropertyGroup {
name: string;
properties: string[];
priority: number;
}
export declare class HomiePropertyBuffer {
private homieObserver;
private bufferTimeMs;
private propertyUpdates$;
private propertyGroups;
private bufferedUpdates$;
constructor(homieObserver: HomieObserver, bufferTimeMs?: number);
addPropertyGroup(group: PropertyGroup): void;
private getPropertyPriority;
private setupPropertyUpdateStream;
private setupBufferedUpdatesStream;
getBufferedUpdates(): Observable<BufferedPropertyUpdate[]>;
processBufferedUpdates(processor: (updates: BufferedPropertyUpdate[]) => void): void;
}
export {};
Loading

0 comments on commit c3eb8eb

Please sign in to comment.