diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 67f8f1f..5a227be 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -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 @@ -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 diff --git a/demo-aframe/aframe-demo.js b/demo-aframe/aframe-demo.js index cada5cf..b21876c 100644 --- a/demo-aframe/aframe-demo.js +++ b/demo-aframe/aframe-demo.js @@ -1,3 +1,5 @@ +const { HomieObserver, HomieEventType, createMqttHomieObserver } = HomieLit; + // Wait for A-Frame to be ready AFRAME.registerComponent('homie-light-switch', { init: function () { @@ -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`); } }); @@ -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); }, @@ -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'); diff --git a/demo-aframe/index.html b/demo-aframe/index.html index 6d6fba0..2ae73d7 100644 --- a/demo-aframe/index.html +++ b/demo-aframe/index.html @@ -25,6 +25,8 @@
Click on the light switch to toggle the light bulb.
+Switch State: Unknown
+Bulb State: Unknown