├── .eslintignore ├── .eslintrc.js ├── .github └── workflows │ ├── build.yml │ └── release.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── index.js ├── manifest.json ├── package.json ├── package.sh ├── static ├── image.png └── video.mp4 └── virtual-things-adapter.js /.eslintignore: -------------------------------------------------------------------------------- 1 | /.eslintrc.js 2 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'env': { 3 | 'browser': true, 4 | 'commonjs': true, 5 | 'es6': true, 6 | 'jasmine': true, 7 | 'jest': true, 8 | 'mocha': true, 9 | 'node': true 10 | }, 11 | 'extends': 'eslint:recommended', 12 | 'parser': 'babel-eslint', 13 | 'parserOptions': { 14 | 'sourceType': 'module' 15 | }, 16 | 'rules': { 17 | 'arrow-parens': [ 18 | 'error', 19 | 'always' 20 | ], 21 | 'arrow-spacing': 'error', 22 | 'block-scoped-var': 'error', 23 | 'block-spacing': [ 24 | 'error', 25 | 'always' 26 | ], 27 | 'brace-style': [ 28 | 'error', 29 | '1tbs' 30 | ], 31 | 'comma-dangle': [ 32 | 'error', 33 | 'always-multiline' 34 | ], 35 | 'comma-spacing': 'error', 36 | 'comma-style': [ 37 | 'error', 38 | 'last' 39 | ], 40 | 'computed-property-spacing': [ 41 | 'error', 42 | 'never' 43 | ], 44 | 'curly': 'error', 45 | 'dot-notation': 'error', 46 | 'eol-last': 'error', 47 | 'func-call-spacing': [ 48 | 'error', 49 | 'never' 50 | ], 51 | 'implicit-arrow-linebreak': [ 52 | 'error', 53 | 'beside' 54 | ], 55 | 'indent': [ 56 | 'error', 57 | 2, 58 | { 59 | 'ArrayExpression': 'first', 60 | 'CallExpression': { 61 | 'arguments': 'first' 62 | }, 63 | 'FunctionDeclaration': { 64 | 'parameters': 'first' 65 | }, 66 | 'FunctionExpression': { 67 | 'parameters': 'first' 68 | }, 69 | 'ObjectExpression': 'first', 70 | 'SwitchCase': 1 71 | } 72 | ], 73 | 'key-spacing': [ 74 | 'error', 75 | { 76 | 'afterColon': true, 77 | 'beforeColon': false, 78 | 'mode': 'strict' 79 | } 80 | ], 81 | 'keyword-spacing': [ 82 | 'error', 83 | { 84 | 'after': true, 85 | 'before': true 86 | } 87 | ], 88 | 'linebreak-style': [ 89 | 'error', 90 | 'unix' 91 | ], 92 | 'lines-between-class-members': [ 93 | 'error', 94 | 'always' 95 | ], 96 | 'max-len': [ 97 | 'error', 98 | 80 99 | ], 100 | 'multiline-ternary': [ 101 | 'error', 102 | 'always-multiline' 103 | ], 104 | 'no-console': 0, 105 | 'no-duplicate-imports': 'error', 106 | 'no-eval': 'error', 107 | 'no-floating-decimal': 'error', 108 | 'no-implicit-globals': 'error', 109 | 'no-implied-eval': 'error', 110 | 'no-lonely-if': 'error', 111 | 'no-multi-spaces': [ 112 | 'error', 113 | { 114 | 'ignoreEOLComments': true 115 | } 116 | ], 117 | 'no-multiple-empty-lines': 'error', 118 | 'no-prototype-builtins': 'off', 119 | 'no-return-assign': 'error', 120 | 'no-script-url': 'error', 121 | 'no-self-compare': 'error', 122 | 'no-sequences': 'error', 123 | 'no-shadow-restricted-names': 'error', 124 | 'no-tabs': 'error', 125 | 'no-trailing-spaces': 'error', 126 | 'no-undefined': 'error', 127 | 'no-unmodified-loop-condition': 'error', 128 | 'no-unused-vars': [ 129 | 'error', 130 | { 131 | 'argsIgnorePattern': '^_', 132 | 'varsIgnorePattern': '^_' 133 | } 134 | ], 135 | 'no-useless-computed-key': 'error', 136 | 'no-useless-concat': 'error', 137 | 'no-useless-constructor': 'error', 138 | 'no-useless-return': 'error', 139 | 'no-var': 'error', 140 | 'no-void': 'error', 141 | 'no-whitespace-before-property': 'error', 142 | 'object-curly-newline': [ 143 | 'error', 144 | { 145 | 'consistent': true 146 | } 147 | ], 148 | 'object-curly-spacing': [ 149 | 'error', 150 | 'never' 151 | ], 152 | 'object-property-newline': [ 153 | 'error', 154 | { 155 | 'allowMultiplePropertiesPerLine': true 156 | } 157 | ], 158 | 'operator-linebreak': [ 159 | 'error', 160 | 'after' 161 | ], 162 | 'padded-blocks': [ 163 | 'error', 164 | { 165 | 'blocks': 'never' 166 | } 167 | ], 168 | 'prefer-const': 'error', 169 | 'prefer-template': 'error', 170 | 'quote-props': [ 171 | 'error', 172 | 'as-needed' 173 | ], 174 | 'quotes': [ 175 | 'error', 176 | 'single', 177 | { 178 | 'allowTemplateLiterals': true 179 | } 180 | ], 181 | 'semi': [ 182 | 'error', 183 | 'always' 184 | ], 185 | 'semi-spacing': [ 186 | 'error', 187 | { 188 | 'after': true, 189 | 'before': false 190 | } 191 | ], 192 | 'semi-style': [ 193 | 'error', 194 | 'last' 195 | ], 196 | 'space-before-blocks': [ 197 | 'error', 198 | 'always' 199 | ], 200 | 'space-before-function-paren': [ 201 | 'error', 202 | { 203 | 'anonymous': 'never', 204 | 'asyncArrow': 'always', 205 | 'named': 'never' 206 | } 207 | ], 208 | 'space-in-parens': [ 209 | 'error', 210 | 'never' 211 | ], 212 | 'space-infix-ops': 'error', 213 | 'space-unary-ops': [ 214 | 'error', 215 | { 216 | 'nonwords': false, 217 | 'words': true 218 | } 219 | ], 220 | 'spaced-comment': [ 221 | 'error', 222 | 'always', 223 | { 224 | 'block': { 225 | 'balanced': true, 226 | 'exceptions': [ 227 | '*' 228 | ] 229 | } 230 | } 231 | ], 232 | 'switch-colon-spacing': [ 233 | 'error', 234 | { 235 | 'after': true, 236 | 'before': false 237 | } 238 | ], 239 | 'template-curly-spacing': [ 240 | 'error', 241 | 'never' 242 | ], 243 | 'yoda': 'error' 244 | } 245 | }; 246 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - master 7 | push: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | strategy: 15 | matrix: 16 | node-version: [ 17 | 10, 18 | 12, 19 | 14, 20 | ] 21 | steps: 22 | - uses: actions/checkout@v2 23 | - uses: actions/setup-node@v1 24 | with: 25 | node-version: ${{ matrix.node-version }} 26 | - name: Install dependencies 27 | run: | 28 | npm install 29 | - name: Lint with eslint 30 | run: | 31 | npm run lint 32 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v[0-9]+.[0-9]+.[0-9]+' 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v2 13 | - uses: actions/setup-node@v1 14 | with: 15 | node-version: 8 16 | - name: Set release version 17 | run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV 18 | - name: Package project 19 | run: | 20 | ./package.sh 21 | - name: Create Release 22 | id: create_release 23 | uses: actions/create-release@v1.0.0 24 | env: 25 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 26 | with: 27 | tag_name: ${{ github.ref }} 28 | release_name: Release ${{ env.RELEASE_VERSION }} 29 | draft: false 30 | prerelease: false 31 | - name: Upload Release Asset 32 | id: upload-release-asset 33 | uses: actions/upload-release-asset@v1.0.1 34 | env: 35 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 36 | with: 37 | upload_url: ${{ steps.create_release.outputs.upload_url }} 38 | asset_path: virtual-things-adapter-${{ env.RELEASE_VERSION }}.tgz 39 | asset_name: virtual-things-adapter-${{ env.RELEASE_VERSION }}.tgz 40 | asset_content_type: application/gnutar 41 | - name: Upload Release Asset Checksum 42 | id: upload-release-asset-checksum 43 | uses: actions/upload-release-asset@v1.0.1 44 | env: 45 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 46 | with: 47 | upload_url: ${{ steps.create_release.outputs.upload_url }} 48 | asset_path: virtual-things-adapter-${{ env.RELEASE_VERSION }}.tgz.sha256sum 49 | asset_name: virtual-things-adapter-${{ env.RELEASE_VERSION }}.tgz.sha256sum 50 | asset_content_type: text/plain 51 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.sha256sum 2 | *.tgz 3 | SHA256SUMS 4 | node_modules/ 5 | package-lock.json 6 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Community Participation Guidelines 2 | 3 | This repository is governed by Mozilla's code of conduct and etiquette guidelines. 4 | For more details, please read the 5 | [Mozilla Community Participation Guidelines](https://www.mozilla.org/about/governance/policies/participation/). 6 | 7 | ## How to Report 8 | For more information on how to report violations of the Community Participation Guidelines, please read our '[How to Report](https://www.mozilla.org/about/governance/policies/participation/reporting/)' page. 9 | 10 | 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * index.js - Loads the virtual things adapter 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | 'use strict'; 10 | 11 | const VirtualThingsAdapter = require('./virtual-things-adapter'); 12 | 13 | module.exports = (addonManager) => { 14 | new VirtualThingsAdapter(addonManager); 15 | }; 16 | -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "WebThingsIO", 3 | "description": "WebThings Gateway Virtual Things Adapter", 4 | "gateway_specific_settings": { 5 | "webthings": { 6 | "exec": "{nodeLoader} {path}", 7 | "primary_type": "adapter", 8 | "strict_max_version": "*", 9 | "strict_min_version": "0.10.0" 10 | } 11 | }, 12 | "homepage_url": "https://github.com/WebThingsIO/virtual-things-adapter", 13 | "id": "virtual-things-adapter", 14 | "license": "MPL-2.0", 15 | "manifest_version": 1, 16 | "name": "Virtual Things", 17 | "options": { 18 | "default": { 19 | "persistPropertyValues": false, 20 | "randomizePropertyValues": false, 21 | "excludeDefaultThings": false 22 | }, 23 | "schema": { 24 | "type": "object", 25 | "required": [ 26 | "persistPropertyValues", 27 | "randomizePropertyValues", 28 | "excludeDefaultThings" 29 | ], 30 | "properties": { 31 | "persistPropertyValues": { 32 | "description": "Whether or not to persist property values across restarts", 33 | "type": "boolean" 34 | }, 35 | "randomizePropertyValues": { 36 | "description": "Whether or not to periodically generate new property values", 37 | "type": "boolean" 38 | }, 39 | "excludeDefaultThings": { 40 | "description": "Whether or not to exclude the default virtual things", 41 | "type": "boolean" 42 | }, 43 | "customThings": { 44 | "description": "Custom virtual things to create", 45 | "type": "array", 46 | "items": { 47 | "type": "object", 48 | "required": [ 49 | "name" 50 | ], 51 | "properties": { 52 | "name": { 53 | "description": "Human-readable thing name, e.g. \"My Thing\".", 54 | "type": "string" 55 | }, 56 | "description": { 57 | "description": "Human-readable description of this thing.", 58 | "type": "string" 59 | }, 60 | "id": { 61 | "description": "Unique ID of this thing. This will be generated for you.", 62 | "readOnly": true, 63 | "type": "string" 64 | }, 65 | "@context": { 66 | "default": "https://iot.mozilla.org/schemas/", 67 | "description": "Schema context for the following @types.", 68 | "type": "string" 69 | }, 70 | "@type": { 71 | "description": "Pre-defined schema types of this thing. See: https://iot.mozilla.org/schemas", 72 | "items": { 73 | "type": "string" 74 | }, 75 | "type": "array" 76 | }, 77 | "properties": { 78 | "type": "array", 79 | "items": { 80 | "type": "object", 81 | "required": [ 82 | "name", 83 | "title", 84 | "type" 85 | ], 86 | "properties": { 87 | "name": { 88 | "description": "Machine-readable property name, e.g. \"prop1\".", 89 | "type": "string" 90 | }, 91 | "title": { 92 | "description": "Human-readable property name, e.g. \"My Property 1\".", 93 | "type": "string" 94 | }, 95 | "description": { 96 | "description": "Human-readable description of this property.", 97 | "type": "string" 98 | }, 99 | "@type": { 100 | "description": "Pre-defined schema type of this property. See: https://iot.mozilla.org/schemas", 101 | "type": "string" 102 | }, 103 | "type": { 104 | "description": "Data type of this property", 105 | "enum": [ 106 | "string", 107 | "integer", 108 | "number", 109 | "boolean", 110 | "null" 111 | ], 112 | "type": "string" 113 | }, 114 | "unit": { 115 | "description": "Unit of this property. Only relevant for numeric properties.", 116 | "type": "string" 117 | }, 118 | "minimum": { 119 | "description": "Minimum value of this property. Only relevant for numeric properties.", 120 | "type": "number" 121 | }, 122 | "maximum": { 123 | "description": "Maximum value of this property. Only relevant for numeric properties.", 124 | "type": "number" 125 | }, 126 | "multipleOf": { 127 | "description": "Property values must be a multiple of this number. Only relevant for numeric properties.", 128 | "type": "number" 129 | }, 130 | "enum": { 131 | "description": "Property values must be one of these options. Only relevant for string properties.", 132 | "type": "array", 133 | "items": { 134 | "type": "string" 135 | } 136 | }, 137 | "readOnly": { 138 | "description": "Whether or not this property is read-only.", 139 | "type": "boolean" 140 | }, 141 | "default": { 142 | "description": "Default value of this property. Use true/false for booleans.", 143 | "type": "string" 144 | } 145 | } 146 | } 147 | }, 148 | "actions": { 149 | "type": "array", 150 | "items": { 151 | "type": "object", 152 | "required": [ 153 | "name", 154 | "title" 155 | ], 156 | "properties": { 157 | "name": { 158 | "description": "Machine-readable action name, e.g. \"action1\".", 159 | "type": "string" 160 | }, 161 | "title": { 162 | "description": "Human-readable action name, e.g. \"My Action 1\".", 163 | "type": "string" 164 | }, 165 | "description": { 166 | "description": "Human-readable description of this action.", 167 | "type": "string" 168 | }, 169 | "input": { 170 | "description": "Optional JSON describing the inputs of this action (No inputs if empty)", 171 | "type": "string" 172 | }, 173 | "emitEvent": { 174 | "description": "Emit an event whenever this action gets executed", 175 | "type": "boolean" 176 | } 177 | } 178 | } 179 | } 180 | } 181 | } 182 | } 183 | } 184 | } 185 | }, 186 | "short_name": "Virtual", 187 | "version": "0.11.0" 188 | } 189 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "virtual-things-adapter", 3 | "version": "0.11.0", 4 | "description": "WebThings Gateway Virtual Things Adapter", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "eslint ." 8 | }, 9 | "homepage": "https://github.com/WebThingsIO/virtual-things-adapter", 10 | "author": "WebThingsIO", 11 | "license": "MPL-2.0", 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/WebThingsIO/virtual-things-adapter.git" 15 | }, 16 | "bugs": { 17 | "url": "https://github.com/WebThingsIO/virtual-things-adapter/issues" 18 | }, 19 | "dependencies": { 20 | "mkdirp": "^1.0.4", 21 | "node-persist": "^3.1.0", 22 | "uuid": "^8.3.1" 23 | }, 24 | "devDependencies": { 25 | "babel-eslint": "^10.1.0", 26 | "eslint": "^7.25.0" 27 | }, 28 | "files": [ 29 | "LICENSE", 30 | "SHA256SUMS", 31 | "index.js", 32 | "manifest.json", 33 | "static/image.png", 34 | "static/video.mp4", 35 | "virtual-things-adapter.js" 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /package.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | rm -rf node_modules 4 | 5 | npm install --production 6 | 7 | shasum --algorithm 256 manifest.json package.json *.js LICENSE > SHA256SUMS 8 | find static -type f -exec shasum --algorithm 256 {} \; >> SHA256SUMS 9 | 10 | find node_modules \( -type f -o -type l \) -exec shasum --algorithm 256 {} \; >> SHA256SUMS 11 | 12 | TARFILE=`npm pack` 13 | 14 | tar xzf ${TARFILE} 15 | cp -r node_modules ./package 16 | tar czf ${TARFILE} package 17 | 18 | shasum --algorithm 256 ${TARFILE} > ${TARFILE}.sha256sum 19 | 20 | rm -rf SHA256SUMS package 21 | -------------------------------------------------------------------------------- /static/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WebThingsIO/virtual-things-adapter/a05b4c96d9ff08bf5de8a11c3a7417025f6a0b3e/static/image.png -------------------------------------------------------------------------------- /static/video.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WebThingsIO/virtual-things-adapter/a05b4c96d9ff08bf5de8a11c3a7417025f6a0b3e/static/video.mp4 -------------------------------------------------------------------------------- /virtual-things-adapter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * VirtualThingsAdapter - an adapter for trying out virtual things 4 | * 5 | * This Source Code Form is subject to the terms of the Mozilla Public 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 | */ 9 | 10 | 'use strict'; 11 | 12 | const child_process = require('child_process'); 13 | const crypto = require('crypto'); 14 | const fs = require('fs'); 15 | const { 16 | Adapter, 17 | Database, 18 | Device, 19 | Event, 20 | Property, 21 | } = require('gateway-addon'); 22 | const manifest = require('./manifest.json'); 23 | const mkdirp = require('mkdirp'); 24 | const os = require('os'); 25 | const path = require('path'); 26 | const storage = require('node-persist'); 27 | const {v4: uuidv4} = require('uuid'); 28 | 29 | const DEBUG = false; 30 | 31 | const proc = child_process.spawnSync( 32 | 'ffmpeg', 33 | ['-version'], 34 | {encoding: 'utf8'} 35 | ); 36 | let ffmpegMajor = null, ffmpegMinor = null; 37 | if (proc.status === 0) { 38 | const version = proc.stdout.split('\n')[0].split(' ')[2]; 39 | ffmpegMajor = parseInt(version.split('.')[0], 10); 40 | ffmpegMinor = parseInt(version.split('.')[1], 10); 41 | } 42 | 43 | function getMediaPath(mediaDir) { 44 | if (mediaDir) { 45 | return path.join(mediaDir, 'virtual-things'); 46 | } 47 | 48 | let profileDir; 49 | if (process.env.hasOwnProperty('MOZIOT_HOME')) { 50 | profileDir = process.env.MOZIOT_HOME; 51 | } else { 52 | profileDir = path.join(os.homedir(), '.mozilla-iot'); 53 | } 54 | 55 | return path.join(profileDir, 'media', 'virtual-things'); 56 | } 57 | 58 | function getDataPath(dataDir) { 59 | if (dataDir) { 60 | return path.join(dataDir, 'virtual-things-adapter'); 61 | } 62 | 63 | let profileDir; 64 | if (process.env.hasOwnProperty('MOZIOT_HOME')) { 65 | profileDir = process.env.MOZIOT_HOME; 66 | } else { 67 | profileDir = path.join(os.homedir(), '.mozilla-iot'); 68 | } 69 | 70 | return path.join(profileDir, 'data', 'virtual-things-adapter'); 71 | } 72 | 73 | function randomNumber(integer, min, max) { 74 | if (typeof min === 'number' && typeof max === 'number') { 75 | if (integer) { 76 | min = Math.ceil(min); 77 | max = Math.floor(max); 78 | return Math.floor(Math.random() * (max - min + 1)) + min; 79 | } 80 | 81 | return Math.random() * (max - min) + min; 82 | } 83 | 84 | const value = Math.random(); 85 | 86 | if (integer) { 87 | return Math.floor(value); 88 | } 89 | 90 | return value; 91 | } 92 | 93 | function bool() { 94 | return { 95 | name: 'on', 96 | value: false, 97 | metadata: { 98 | title: 'On/Off', 99 | type: 'boolean', 100 | '@type': 'BooleanProperty', 101 | readOnly: true, 102 | }, 103 | }; 104 | } 105 | 106 | function on() { 107 | return { 108 | name: 'on', 109 | value: false, 110 | metadata: { 111 | title: 'On/Off', 112 | type: 'boolean', 113 | '@type': 'OnOffProperty', 114 | }, 115 | }; 116 | } 117 | 118 | function color(readOnly = false) { 119 | return { 120 | name: 'color', 121 | value: '#ffffff', 122 | metadata: { 123 | title: 'Color', 124 | type: 'string', 125 | '@type': 'ColorProperty', 126 | readOnly, 127 | }, 128 | }; 129 | } 130 | 131 | function colorTemperature() { 132 | return { 133 | name: 'colorTemperature', 134 | value: 2500, 135 | metadata: { 136 | title: 'Color Temperature', 137 | type: 'number', 138 | '@type': 'ColorTemperatureProperty', 139 | unit: 'kelvin', 140 | minimum: 2500, 141 | maximum: 9000, 142 | }, 143 | }; 144 | } 145 | 146 | function colorMode() { 147 | return { 148 | name: 'colorMode', 149 | value: 'color', 150 | metadata: { 151 | title: 'Color Mode', 152 | type: 'string', 153 | '@type': 'ColorModeProperty', 154 | enum: [ 155 | 'color', 156 | 'temperature', 157 | ], 158 | readOnly: true, 159 | }, 160 | }; 161 | } 162 | 163 | function brightness() { 164 | return { 165 | name: 'level', 166 | value: 0, 167 | metadata: { 168 | title: 'Brightness', 169 | type: 'number', 170 | '@type': 'BrightnessProperty', 171 | unit: 'percent', 172 | minimum: 0, 173 | maximum: 100, 174 | }, 175 | }; 176 | } 177 | 178 | function level(readOnly) { 179 | return { 180 | name: 'level', 181 | value: 0, 182 | metadata: { 183 | title: 'Level', 184 | type: 'number', 185 | '@type': 'LevelProperty', 186 | unit: 'percent', 187 | minimum: 0, 188 | maximum: 100, 189 | readOnly, 190 | }, 191 | }; 192 | } 193 | 194 | const onOffColorLight = { 195 | type: 'onOffColorLight', 196 | '@context': 'https://iot.mozilla.org/schemas', 197 | '@type': ['OnOffSwitch', 'Light', 'ColorControl'], 198 | name: 'Virtual On/Off Color Light', 199 | properties: [ 200 | on(), 201 | color(), 202 | ], 203 | actions: [], 204 | events: [], 205 | }; 206 | 207 | const onOffColorTemperatureLight = { 208 | type: 'onOffColorLight', 209 | '@context': 'https://iot.mozilla.org/schemas', 210 | '@type': ['OnOffSwitch', 'Light', 'ColorControl'], 211 | name: 'Virtual On/Off Color Temperature Light', 212 | properties: [ 213 | on(), 214 | colorTemperature(), 215 | ], 216 | actions: [], 217 | events: [], 218 | }; 219 | 220 | const dimmableColorLight = { 221 | type: 'dimmableColorLight', 222 | '@context': 'https://iot.mozilla.org/schemas', 223 | '@type': ['OnOffSwitch', 'Light', 'ColorControl'], 224 | name: 'Virtual Dimmable Color Light', 225 | properties: [ 226 | color(), 227 | colorTemperature(), 228 | colorMode(), 229 | brightness(), 230 | on(), 231 | ], 232 | actions: [], 233 | events: [], 234 | }; 235 | 236 | const multiLevelSwitch = { 237 | type: 'multiLevelSwitch', 238 | '@context': 'https://iot.mozilla.org/schemas', 239 | '@type': ['OnOffSwitch', 'MultiLevelSwitch'], 240 | name: 'Virtual Multi-level Switch', 241 | properties: [ 242 | level(false), 243 | on(), 244 | ], 245 | actions: [], 246 | events: [], 247 | }; 248 | 249 | const onOffSwitch = { 250 | type: 'onOffSwitch', 251 | '@context': 'https://iot.mozilla.org/schemas', 252 | '@type': ['OnOffSwitch'], 253 | name: 'Virtual On/Off Switch', 254 | properties: [ 255 | on(), 256 | ], 257 | actions: [], 258 | events: [], 259 | }; 260 | 261 | const binarySensor = { 262 | type: 'binarySensor', 263 | '@context': 'https://iot.mozilla.org/schemas', 264 | '@type': ['BinarySensor'], 265 | name: 'Virtual Binary Sensor', 266 | properties: [ 267 | bool(), 268 | ], 269 | actions: [], 270 | events: [], 271 | }; 272 | 273 | const multiLevelSensor = { 274 | type: 'multiLevelSensor', 275 | '@context': 'https://iot.mozilla.org/schemas', 276 | '@type': ['MultiLevelSensor'], 277 | name: 'Virtual Multi-level Sensor', 278 | properties: [ 279 | bool(), 280 | level(true), 281 | ], 282 | actions: [], 283 | events: [], 284 | }; 285 | 286 | const smartPlug = { 287 | type: 'smartPlug', 288 | '@context': 'https://iot.mozilla.org/schemas', 289 | '@type': ['OnOffSwitch', 'EnergyMonitor', 'SmartPlug', 'MultiLevelSwitch'], 290 | name: 'Virtual Smart Plug', 291 | properties: [ 292 | on(), 293 | level(false), 294 | { 295 | name: 'instantaneousPower', 296 | value: 0, 297 | metadata: { 298 | '@type': 'InstantaneousPowerProperty', 299 | title: 'Power', 300 | type: 'number', 301 | unit: 'watt', 302 | readOnly: true, 303 | }, 304 | }, 305 | { 306 | name: 'instantaneousPowerFactor', 307 | value: 0, 308 | metadata: { 309 | '@type': 'InstantaneousPowerFactorProperty', 310 | title: 'Power Factor', 311 | type: 'number', 312 | minimum: -1, 313 | maximum: 1, 314 | readOnly: true, 315 | }, 316 | }, 317 | { 318 | name: 'voltage', 319 | value: 0, 320 | metadata: { 321 | '@type': 'VoltageProperty', 322 | title: 'Voltage', 323 | type: 'number', 324 | unit: 'volt', 325 | readOnly: true, 326 | }, 327 | }, 328 | { 329 | name: 'current', 330 | value: 0, 331 | metadata: { 332 | '@type': 'CurrentProperty', 333 | title: 'Current', 334 | type: 'number', 335 | unit: 'ampere', 336 | readOnly: true, 337 | }, 338 | }, 339 | { 340 | name: 'frequency', 341 | value: 0, 342 | metadata: { 343 | '@type': 'FrequencyProperty', 344 | title: 'Frequency', 345 | type: 'number', 346 | unit: 'hertz', 347 | readOnly: true, 348 | }, 349 | }, 350 | ], 351 | actions: [], 352 | events: [], 353 | }; 354 | 355 | const onOffLight = { 356 | type: 'onOffLight', 357 | '@context': 'https://iot.mozilla.org/schemas', 358 | '@type': ['OnOffSwitch', 'Light'], 359 | name: 'Virtual On/Off Light', 360 | properties: [ 361 | on(), 362 | ], 363 | actions: [], 364 | events: [], 365 | }; 366 | 367 | const dimmableLight = { 368 | type: 'dimmableLight', 369 | '@context': 'https://iot.mozilla.org/schemas', 370 | '@type': ['OnOffSwitch', 'Light'], 371 | name: 'Virtual Dimmable Light', 372 | properties: [ 373 | on(), 374 | brightness(), 375 | ], 376 | actions: [], 377 | events: [], 378 | }; 379 | 380 | const doorSensor = { 381 | '@context': 'https://iot.mozilla.org/schemas', 382 | '@type': ['DoorSensor'], 383 | name: 'Virtual Door Sensor', 384 | properties: [ 385 | { 386 | name: 'open', 387 | value: false, 388 | metadata: { 389 | title: 'Open', 390 | type: 'boolean', 391 | '@type': 'OpenProperty', 392 | readOnly: true, 393 | }, 394 | }, 395 | ], 396 | actions: [], 397 | events: [], 398 | }; 399 | 400 | const motionSensor = { 401 | '@context': 'https://iot.mozilla.org/schemas', 402 | '@type': ['MotionSensor'], 403 | name: 'Virtual Motion Sensor', 404 | properties: [ 405 | { 406 | name: 'motion', 407 | value: false, 408 | metadata: { 409 | title: 'Motion', 410 | type: 'boolean', 411 | '@type': 'MotionProperty', 412 | readOnly: true, 413 | }, 414 | }, 415 | ], 416 | actions: [], 417 | events: [], 418 | }; 419 | 420 | const leakSensor = { 421 | '@context': 'https://iot.mozilla.org/schemas', 422 | '@type': ['LeakSensor'], 423 | name: 'Virtual Leak Sensor', 424 | properties: [ 425 | { 426 | name: 'leak', 427 | value: false, 428 | metadata: { 429 | title: 'Leak', 430 | type: 'boolean', 431 | '@type': 'LeakProperty', 432 | readOnly: true, 433 | }, 434 | }, 435 | ], 436 | actions: [], 437 | events: [], 438 | }; 439 | 440 | const temperatureSensor = { 441 | '@context': 'https://iot.mozilla.org/schemas', 442 | '@type': ['TemperatureSensor'], 443 | name: 'Virtual Temperature Sensor', 444 | properties: [ 445 | { 446 | name: 'temperature', 447 | value: 20, 448 | metadata: { 449 | title: 'Temperature', 450 | type: 'number', 451 | '@type': 'TemperatureProperty', 452 | unit: 'degree celsius', 453 | minimum: -20, 454 | maximum: 50, 455 | readOnly: true, 456 | }, 457 | }, 458 | ], 459 | actions: [], 460 | events: [], 461 | }; 462 | 463 | const pushButton = { 464 | '@context': 'https://iot.mozilla.org/schemas', 465 | '@type': ['PushButton'], 466 | name: 'Virtual Push Button', 467 | properties: [ 468 | { 469 | name: 'pushed', 470 | value: false, 471 | metadata: { 472 | title: 'Pushed', 473 | type: 'boolean', 474 | '@type': 'PushedProperty', 475 | readOnly: true, 476 | }, 477 | }, 478 | ], 479 | actions: [], 480 | events: [], 481 | }; 482 | 483 | const thing = { 484 | type: 'thing', 485 | '@context': 'https://iot.mozilla.org/schemas', 486 | '@type': [], 487 | name: 'Virtual Thing', 488 | properties: [ 489 | { 490 | name: 'boolProperty', 491 | value: true, 492 | metadata: { 493 | type: 'boolean', 494 | }, 495 | }, 496 | { 497 | name: 'stringProperty', 498 | value: 'blah', 499 | metadata: { 500 | type: 'string', 501 | }, 502 | }, 503 | { 504 | name: 'numberProperty', 505 | value: 12, 506 | metadata: { 507 | type: 'number', 508 | }, 509 | }, 510 | { 511 | name: 'numberUnitProperty', 512 | value: 34, 513 | metadata: { 514 | type: 'number', 515 | unit: 'metres', 516 | }, 517 | }, 518 | { 519 | name: 'numberUnitMinMaxProperty', 520 | value: 56, 521 | metadata: { 522 | type: 'number', 523 | unit: 'degrees', 524 | minimum: 0, 525 | maximum: 100, 526 | }, 527 | }, 528 | { 529 | name: 'numberEnumProperty', 530 | value: 0, 531 | metadata: { 532 | type: 'number', 533 | unit: 'something', 534 | enum: [ 535 | 0, 536 | 10, 537 | 20, 538 | 30, 539 | ], 540 | }, 541 | }, 542 | { 543 | name: 'stringEnumProperty', 544 | value: 'string1', 545 | metadata: { 546 | type: 'string', 547 | enum: [ 548 | 'string1', 549 | 'string2', 550 | 'string3', 551 | 'string4', 552 | ], 553 | }, 554 | }, 555 | ], 556 | actions: [], 557 | events: [], 558 | }; 559 | 560 | const actionsEventsThing = { 561 | type: 'thing', 562 | '@context': 'https://iot.mozilla.org/schemas', 563 | '@type': [], 564 | name: 'Virtual Actions & Events Thing', 565 | properties: [], 566 | actions: [ 567 | { 568 | name: 'basic', 569 | metadata: { 570 | title: 'No Input', 571 | description: 'An action with no inputs, fires an event', 572 | }, 573 | }, 574 | { 575 | name: 'single', 576 | metadata: { 577 | title: 'Single Input', 578 | description: 'An action with a single, non-object input', 579 | input: { 580 | type: 'number', 581 | }, 582 | }, 583 | }, 584 | { 585 | name: 'multiple', 586 | metadata: { 587 | title: 'Multiple Inputs', 588 | description: 'An action with mutiple, optional inputs', 589 | input: { 590 | type: 'object', 591 | properties: { 592 | stringInput: { 593 | type: 'string', 594 | }, 595 | booleanInput: { 596 | type: 'boolean', 597 | }, 598 | }, 599 | }, 600 | }, 601 | }, 602 | { 603 | name: 'advanced', 604 | metadata: { 605 | title: 'Advanced Inputs', 606 | description: 'An action with many inputs, some required', 607 | input: { 608 | type: 'object', 609 | required: [ 610 | 'numberInput', 611 | ], 612 | properties: { 613 | numberInput: { 614 | type: 'number', 615 | minimum: 0, 616 | maximum: 100, 617 | unit: 'percent', 618 | }, 619 | integerInput: { 620 | type: 'integer', 621 | unit: 'metre', 622 | }, 623 | stringInput: { 624 | type: 'string', 625 | }, 626 | booleanInput: { 627 | type: 'boolean', 628 | }, 629 | enumInput: { 630 | type: 'string', 631 | enum: [ 632 | 'enum string1', 633 | 'enum string2', 634 | 'enum string3', 635 | ], 636 | }, 637 | }, 638 | }, 639 | }, 640 | }, 641 | ], 642 | events: [ 643 | { 644 | name: 'virtualEvent', 645 | metadata: { 646 | description: 'An event from a virtual thing', 647 | type: 'number', 648 | }, 649 | }, 650 | ], 651 | }; 652 | 653 | const onOffSwitchWithPin = { 654 | type: 'onOffSwitch', 655 | '@context': 'https://iot.mozilla.org/schemas', 656 | '@type': ['OnOffSwitch'], 657 | name: 'Virtual On/Off Switch (with PIN)', 658 | properties: [ 659 | on(), 660 | ], 661 | actions: [], 662 | events: [], 663 | pin: { 664 | required: true, 665 | pattern: '^\\d{4}$', 666 | }, 667 | }; 668 | 669 | const onOffSwitchWithCredentials = { 670 | type: 'onOffSwitch', 671 | '@context': 'https://iot.mozilla.org/schemas', 672 | '@type': ['OnOffSwitch'], 673 | name: 'Virtual On/Off Switch (with credentials)', 674 | properties: [ 675 | on(), 676 | ], 677 | actions: [], 678 | events: [], 679 | credentialsRequired: true, 680 | }; 681 | 682 | const camera = { 683 | type: 'thing', 684 | '@context': 'https://iot.mozilla.org/schemas', 685 | '@type': ['Camera'], 686 | name: 'Virtual Camera', 687 | properties: [ 688 | { 689 | name: 'image', 690 | value: null, 691 | metadata: { 692 | type: 'null', 693 | '@type': 'ImageProperty', 694 | title: 'Image', 695 | readOnly: true, 696 | links: [ 697 | { 698 | rel: 'alternate', 699 | href: '/media/virtual-things/image.png', 700 | mediaType: 'image/png', 701 | }, 702 | ], 703 | }, 704 | }, 705 | ], 706 | actions: [], 707 | events: [], 708 | }; 709 | 710 | const videoCamera = { 711 | type: 'thing', 712 | '@context': 'https://iot.mozilla.org/schemas', 713 | '@type': ['VideoCamera'], 714 | name: 'Virtual Video Camera', 715 | properties: [ 716 | { 717 | name: 'video', 718 | value: null, 719 | metadata: { 720 | type: 'null', 721 | '@type': 'VideoProperty', 722 | title: 'Video', 723 | readOnly: true, 724 | links: [ 725 | { 726 | rel: 'alternate', 727 | href: '/media/virtual-things/index.mpd', 728 | mediaType: 'application/dash+xml', 729 | }, 730 | ], 731 | }, 732 | }, 733 | { 734 | name: 'streamActive', 735 | value: false, 736 | metadata: { 737 | type: 'boolean', 738 | title: 'Streaming', 739 | }, 740 | }, 741 | ], 742 | actions: [], 743 | events: [], 744 | }; 745 | 746 | const alarm = { 747 | '@context': 'https://iot.mozilla.org/schemas', 748 | '@type': ['Alarm'], 749 | name: 'Virtual Alarm', 750 | properties: [ 751 | { 752 | name: 'alarm', 753 | value: false, 754 | metadata: { 755 | title: 'Alarm', 756 | type: 'boolean', 757 | '@type': 'AlarmProperty', 758 | readOnly: true, 759 | }, 760 | }, 761 | ], 762 | actions: [ 763 | { 764 | name: 'trigger', 765 | metadata: { 766 | title: 'Trigger', 767 | description: 'Trigger alarm', 768 | }, 769 | }, 770 | { 771 | name: 'silence', 772 | metadata: { 773 | title: 'Silence', 774 | description: 'Silence alarm', 775 | }, 776 | }, 777 | ], 778 | events: [ 779 | { 780 | name: 'alarmEvent', 781 | metadata: { 782 | description: 'An alarm event from a virtual thing', 783 | type: 'string', 784 | '@type': 'AlarmEvent', 785 | readOnly: true, 786 | }, 787 | }, 788 | ], 789 | }; 790 | 791 | const energyMonitor = { 792 | '@context': 'https://iot.mozilla.org/schemas', 793 | '@type': ['EnergyMonitor'], 794 | name: 'Virtual Energy Monitor', 795 | properties: [ 796 | { 797 | name: 'instantaneousPower', 798 | value: 0, 799 | metadata: { 800 | '@type': 'InstantaneousPowerProperty', 801 | title: 'Power', 802 | type: 'number', 803 | unit: 'watt', 804 | readOnly: true, 805 | }, 806 | }, 807 | { 808 | name: 'instantaneousPowerFactor', 809 | value: 0, 810 | metadata: { 811 | '@type': 'InstantaneousPowerFactorProperty', 812 | title: 'Power Factor', 813 | type: 'number', 814 | minimum: -1, 815 | maximum: 1, 816 | readOnly: true, 817 | }, 818 | }, 819 | { 820 | name: 'voltage', 821 | value: 0, 822 | metadata: { 823 | '@type': 'VoltageProperty', 824 | title: 'Voltage', 825 | type: 'number', 826 | unit: 'volt', 827 | readOnly: true, 828 | }, 829 | }, 830 | { 831 | name: 'current', 832 | value: 0, 833 | metadata: { 834 | '@type': 'CurrentProperty', 835 | title: 'Current', 836 | type: 'number', 837 | unit: 'ampere', 838 | readOnly: true, 839 | }, 840 | }, 841 | { 842 | name: 'frequency', 843 | value: 0, 844 | metadata: { 845 | '@type': 'FrequencyProperty', 846 | title: 'Frequency', 847 | type: 'number', 848 | unit: 'hertz', 849 | readOnly: true, 850 | }, 851 | }, 852 | ], 853 | actions: [], 854 | events: [], 855 | }; 856 | 857 | const colorControl = { 858 | '@context': 'https://iot.mozilla.org/schemas', 859 | '@type': ['ColorControl'], 860 | name: 'Virtual Color Control', 861 | properties: [ 862 | color(), 863 | ], 864 | actions: [], 865 | events: [], 866 | }; 867 | 868 | const thermostat = { 869 | '@context': 'https://iot.mozilla.org/schemas', 870 | '@type': ['Thermostat', 'TemperatureSensor'], 871 | name: 'Virtual Thermostat', 872 | properties: [ 873 | { 874 | name: 'temperature', 875 | value: 20, 876 | metadata: { 877 | title: 'Temperature', 878 | type: 'number', 879 | '@type': 'TemperatureProperty', 880 | unit: 'degree celsius', 881 | minimum: 0, 882 | maximum: 100, 883 | readOnly: true, 884 | }, 885 | }, 886 | { 887 | name: 'heatingTargetTemperature', 888 | value: 19, 889 | metadata: { 890 | title: 'Heating Target', 891 | type: 'number', 892 | '@type': 'TargetTemperatureProperty', 893 | unit: 'degree celsius', 894 | minimum: 10, 895 | maximum: 38, 896 | multipleOf: 0.1, 897 | }, 898 | }, 899 | { 900 | name: 'coolingTargetTemperature', 901 | value: 25, 902 | metadata: { 903 | title: 'Cooling Target', 904 | type: 'number', 905 | '@type': 'TargetTemperatureProperty', 906 | unit: 'degree celsius', 907 | minimum: 10, 908 | maximum: 38, 909 | multipleOf: 0.1, 910 | }, 911 | }, 912 | { 913 | name: 'heatingCooling', 914 | value: 'heating', 915 | metadata: { 916 | title: 'Heating/Cooling', 917 | type: 'string', 918 | '@type': 'HeatingCoolingProperty', 919 | enum: ['off', 'heating', 'cooling'], 920 | readOnly: true, 921 | }, 922 | }, 923 | { 924 | name: 'thermostatMode', 925 | value: 'heat', 926 | metadata: { 927 | title: 'Mode', 928 | type: 'string', 929 | '@type': 'ThermostatModeProperty', 930 | enum: ['off', 'heat', 'cool', 'auto'], 931 | }, 932 | }, 933 | ], 934 | actions: [], 935 | events: [], 936 | }; 937 | 938 | const lock = { 939 | '@context': 'https://iot.mozilla.org/schemas', 940 | '@type': ['Lock'], 941 | name: 'Virtual Lock', 942 | properties: [ 943 | { 944 | name: 'locked', 945 | value: 'unlocked', 946 | metadata: { 947 | title: 'Current State', 948 | type: 'string', 949 | '@type': 'LockedProperty', 950 | enum: ['locked', 'unlocked', 'jammed', 'unknown'], 951 | readOnly: true, 952 | }, 953 | }, 954 | ], 955 | actions: [ 956 | { 957 | name: 'lock', 958 | metadata: { 959 | '@type': 'LockAction', 960 | title: 'Lock', 961 | description: 'Lock the locking mechanism', 962 | }, 963 | }, 964 | { 965 | name: 'unlock', 966 | metadata: { 967 | '@type': 'UnlockAction', 968 | title: 'Unlock', 969 | description: 'Unlock the locking mechanism', 970 | }, 971 | }, 972 | ], 973 | events: [], 974 | }; 975 | 976 | const colorSensor = { 977 | '@context': 'https://iot.mozilla.org/schemas', 978 | '@type': ['ColorSensor'], 979 | name: 'Virtual Color Sensor', 980 | properties: [ 981 | color(true), 982 | ], 983 | actions: [], 984 | events: [], 985 | }; 986 | 987 | const humiditySensor = { 988 | '@context': 'https://iot.mozilla.org/schemas', 989 | '@type': ['HumiditySensor'], 990 | name: 'Virtual Humidity Sensor', 991 | properties: [ 992 | { 993 | name: 'humidity', 994 | value: 20, 995 | metadata: { 996 | title: 'Humidity', 997 | type: 'number', 998 | '@type': 'HumidityProperty', 999 | unit: 'percent', 1000 | minimum: 0, 1001 | maximum: 100, 1002 | readOnly: true, 1003 | }, 1004 | }, 1005 | ], 1006 | actions: [], 1007 | events: [], 1008 | }; 1009 | 1010 | const airQualitySensor = { 1011 | '@context': 'https://iot.mozilla.org/schemas', 1012 | '@type': ['AirQualitySensor'], 1013 | name: 'Virtual Air Quality Sensor', 1014 | properties: [ 1015 | { 1016 | name: 'concentration', 1017 | value: 20, 1018 | metadata: { 1019 | title: 'Gas Concentration', 1020 | type: 'number', 1021 | '@type': 'ConcentrationProperty', 1022 | unit: 'ppm', 1023 | minimum: 0, 1024 | readOnly: true, 1025 | }, 1026 | }, 1027 | { 1028 | name: 'density', 1029 | value: 20, 1030 | metadata: { 1031 | title: 'Particulate Density', 1032 | type: 'number', 1033 | '@type': 'DensityProperty', 1034 | unit: 'micrograms per cubic metre', 1035 | minimum: 0, 1036 | readOnly: true, 1037 | }, 1038 | }, 1039 | ], 1040 | actions: [], 1041 | events: [], 1042 | }; 1043 | 1044 | const barometricPressureSensor = { 1045 | '@context': 'https://iot.mozilla.org/schemas', 1046 | '@type': ['BarometricPressureSensor'], 1047 | name: 'Virtual Barometric Pressure Sensor', 1048 | properties: [ 1049 | { 1050 | name: 'pressure', 1051 | value: 20, 1052 | metadata: { 1053 | title: 'Pressure', 1054 | type: 'number', 1055 | '@type': 'BarometricPressureProperty', 1056 | unit: 'hectopascal', 1057 | minimum: 0, 1058 | readOnly: true, 1059 | }, 1060 | }, 1061 | ], 1062 | actions: [], 1063 | events: [], 1064 | }; 1065 | 1066 | const smokeSensor = { 1067 | '@context': 'https://iot.mozilla.org/schemas', 1068 | '@type': ['SmokeSensor'], 1069 | name: 'Virtual Smoke Sensor', 1070 | properties: [ 1071 | { 1072 | name: 'smoke', 1073 | value: false, 1074 | metadata: { 1075 | title: 'Smoke', 1076 | type: 'boolean', 1077 | '@type': 'SmokeProperty', 1078 | readOnly: true, 1079 | }, 1080 | }, 1081 | ], 1082 | actions: [], 1083 | events: [], 1084 | }; 1085 | 1086 | if (ffmpegMajor !== null && ffmpegMajor >= 4) { 1087 | videoCamera.properties[0].metadata.links.push({ 1088 | rel: 'alternate', 1089 | href: '/media/virtual-things/master.m3u8', 1090 | mediaType: 'application/vnd.apple.mpegurl', 1091 | }); 1092 | } 1093 | 1094 | const VIRTUAL_THINGS = [ 1095 | onOffColorLight, 1096 | multiLevelSwitch, 1097 | dimmableColorLight, 1098 | onOffSwitch, 1099 | binarySensor, 1100 | multiLevelSensor, 1101 | smartPlug, 1102 | onOffLight, 1103 | dimmableLight, 1104 | thing, 1105 | actionsEventsThing, 1106 | onOffSwitchWithPin, 1107 | onOffColorTemperatureLight, 1108 | doorSensor, 1109 | motionSensor, 1110 | pushButton, 1111 | leakSensor, 1112 | temperatureSensor, 1113 | onOffSwitchWithCredentials, 1114 | camera, 1115 | videoCamera, 1116 | alarm, 1117 | energyMonitor, 1118 | colorControl, 1119 | thermostat, 1120 | lock, 1121 | colorSensor, 1122 | humiditySensor, 1123 | airQualitySensor, 1124 | barometricPressureSensor, 1125 | smokeSensor, 1126 | ]; 1127 | 1128 | /** 1129 | * A virtual property 1130 | */ 1131 | class VirtualThingsProperty extends Property { 1132 | constructor(device, name, descr, value) { 1133 | super(device, name, descr); 1134 | this.setCachedValue(value); 1135 | 1136 | if (device.adapter.config.randomizePropertyValues) { 1137 | this.interval = setInterval(() => { 1138 | let value; 1139 | 1140 | if (descr.enum && descr.enum.length > 0) { 1141 | value = descr.enum[randomNumber(true, 0, descr.enum.length - 1)]; 1142 | } else { 1143 | switch (descr.type) { 1144 | case 'boolean': 1145 | value = Math.random() >= 0.5; 1146 | break; 1147 | case 'string': { 1148 | if (descr['@type'] === 'ColorProperty') { 1149 | const randomComponent = () => { 1150 | return randomNumber(true, 0, 255) 1151 | .toString(16) 1152 | .padStart(2, '0'); 1153 | }; 1154 | value = `#${ 1155 | randomComponent()}${ 1156 | randomComponent()}${ 1157 | randomComponent()}`; 1158 | } else { 1159 | value = crypto.randomBytes(20).toString('hex'); 1160 | } 1161 | 1162 | break; 1163 | } 1164 | case 'number': 1165 | case 'integer': 1166 | value = randomNumber( 1167 | descr.type === 'integer', 1168 | descr.minimum, 1169 | descr.maximum 1170 | ); 1171 | break; 1172 | default: 1173 | return; 1174 | } 1175 | } 1176 | 1177 | if (value !== this.value) { 1178 | this.setCachedValue(value); 1179 | this.device.notifyPropertyChanged(this); 1180 | } 1181 | }, 30 * 1000); 1182 | } 1183 | } 1184 | 1185 | /** 1186 | * @param {any} value 1187 | * @return {Promise} a promise which resolves to the updated value. 1188 | */ 1189 | setValue(value) { 1190 | return new Promise((resolve, reject) => { 1191 | if (this.readOnly) { 1192 | reject('Read-only property'); 1193 | } else { 1194 | this.setCachedValue(value); 1195 | 1196 | const colorModeProperty = this.device.findProperty('colorMode'); 1197 | 1198 | switch (this.name) { 1199 | case 'streamActive': 1200 | if (this.value) { 1201 | this.device.adapter.startTranscode(); 1202 | } else { 1203 | this.device.adapter.stopTranscode(); 1204 | } 1205 | 1206 | break; 1207 | case 'thermostatMode': { 1208 | const heatingCooling = this.device.properties.get('heatingCooling'); 1209 | 1210 | if (this.value === 'heat') { 1211 | heatingCooling.setCachedValueAndNotify('heating'); 1212 | } else if (this.value === 'cool') { 1213 | heatingCooling.setCachedValueAndNotify('cooling'); 1214 | } else if (this.value === 'off') { 1215 | heatingCooling.setCachedValueAndNotify('off'); 1216 | } 1217 | 1218 | break; 1219 | } 1220 | case 'color': 1221 | if (colorModeProperty) { 1222 | colorModeProperty.setCachedValueAndNotify('color'); 1223 | } 1224 | break; 1225 | case 'colorTemperature': 1226 | if (colorModeProperty) { 1227 | colorModeProperty.setCachedValueAndNotify('temperature'); 1228 | } 1229 | break; 1230 | } 1231 | 1232 | resolve(this.value); 1233 | this.device.notifyPropertyChanged(this); 1234 | } 1235 | }); 1236 | } 1237 | 1238 | /** 1239 | * Set the current value. 1240 | */ 1241 | setCachedValue(value) { 1242 | if (this.type === 'boolean') { 1243 | this.value = !!value; 1244 | } else { 1245 | this.value = value; 1246 | } 1247 | 1248 | if (this.device.adapter.config.persistPropertyValues) { 1249 | const key = `${this.device.id}-${this.name}`; 1250 | storage.setItem(key, this.value).catch((e) => { 1251 | console.error('Failed to persist property value:', e); 1252 | }); 1253 | } 1254 | 1255 | return this.value; 1256 | } 1257 | } 1258 | 1259 | /** 1260 | * A virtual device 1261 | */ 1262 | class VirtualThingsDevice extends Device { 1263 | /** 1264 | * @param {VirtualThingsAdapter} adapter 1265 | * @param {String} id - A globally unique identifier 1266 | * @param {Object} template - the virtual thing to represent 1267 | */ 1268 | constructor(adapter, id, template) { 1269 | super(adapter, id); 1270 | 1271 | this.name = template.name; 1272 | 1273 | this.type = template.type; 1274 | this['@context'] = template['@context']; 1275 | this['@type'] = template['@type']; 1276 | 1277 | if (template.hasOwnProperty('pin')) { 1278 | this.pinRequired = template.pin.required; 1279 | this.pinPattern = template.pin.pattern; 1280 | } else { 1281 | this.pinRequired = false; 1282 | this.pinPattern = ''; 1283 | } 1284 | 1285 | this.credentialsRequired = !!template.credentialsRequired; 1286 | 1287 | const promises = []; 1288 | for (const prop of template.properties) { 1289 | let promise; 1290 | if (this.adapter.config.persistPropertyValues) { 1291 | const key = `${this.id}-${prop.name}`; 1292 | promise = storage.getItem(key).then((v) => { 1293 | if (typeof v === 'undefined' || v === null) { 1294 | return prop.value; 1295 | } 1296 | 1297 | return v; 1298 | }); 1299 | } else { 1300 | promise = Promise.resolve(prop.value); 1301 | } 1302 | 1303 | promises.push(promise.then((v) => { 1304 | this.properties.set( 1305 | prop.name, 1306 | new VirtualThingsProperty(this, prop.name, prop.metadata, v)); 1307 | })); 1308 | } 1309 | 1310 | for (const action of template.actions) { 1311 | this.addAction(action.name, action.metadata); 1312 | } 1313 | 1314 | for (const event of template.events) { 1315 | this.addEvent(event.name, event.metadata); 1316 | } 1317 | 1318 | Promise.all(promises).then(() => this.adapter.handleDeviceAdded(this)); 1319 | } 1320 | 1321 | performAction(action) { 1322 | console.log(`Performing action "${action.name}" with input:`, action.input); 1323 | 1324 | action.start(); 1325 | 1326 | if (this.id.startsWith('virtual-things-custom-')) { 1327 | if (this.events.has(action.name)) { 1328 | this.eventNotify(new Event(this, 1329 | action.name, 1330 | action.input)); 1331 | } 1332 | action.finish(); 1333 | return Promise.resolve(); 1334 | } 1335 | 1336 | switch (action.name) { 1337 | case 'basic': 1338 | this.eventNotify(new Event(this, 1339 | 'virtualEvent', 1340 | Math.floor(Math.random() * 100))); 1341 | break; 1342 | case 'trigger': { 1343 | const prop = this.properties.get('alarm'); 1344 | prop.setCachedValue(true); 1345 | this.notifyPropertyChanged(prop); 1346 | this.eventNotify(new Event(this, 1347 | 'alarmEvent', 1348 | 'Something happened!')); 1349 | break; 1350 | } 1351 | case 'silence': { 1352 | const prop = this.properties.get('alarm'); 1353 | prop.setCachedValue(false); 1354 | this.notifyPropertyChanged(prop); 1355 | break; 1356 | } 1357 | case 'lock': 1358 | case 'unlock': { 1359 | const targetState = action.name === 'lock' ? 'locked' : 'unlocked'; 1360 | 1361 | const prop = this.properties.get('locked'); 1362 | if (prop.value === targetState) { 1363 | action.finish(); 1364 | return Promise.resolve(); 1365 | } 1366 | 1367 | prop.setCachedValueAndNotify('unknown'); 1368 | setTimeout(() => { 1369 | // jam the lock 5% of the time. 1370 | if (randomNumber(true, 0, 19) === 2) { 1371 | prop.setCachedValueAndNotify('jammed'); 1372 | } else { 1373 | prop.setCachedValueAndNotify(targetState); 1374 | } 1375 | 1376 | this.notifyPropertyChanged(prop); 1377 | action.finish(); 1378 | }, 2000); 1379 | 1380 | return Promise.resolve(); 1381 | } 1382 | } 1383 | 1384 | action.finish(); 1385 | 1386 | return Promise.resolve(); 1387 | } 1388 | } 1389 | 1390 | /** 1391 | * Virtual Things adapter 1392 | * Instantiates one virtual device per template 1393 | */ 1394 | class VirtualThingsAdapter extends Adapter { 1395 | constructor(addonManager) { 1396 | super(addonManager, 'virtual-things', manifest.id); 1397 | 1398 | addonManager.addAdapter(this); 1399 | 1400 | this.mediaDir = getMediaPath(this.userProfile.mediaDir); 1401 | if (!fs.existsSync(this.mediaDir)) { 1402 | mkdirp.sync(this.mediaDir, {mode: 0o755}); 1403 | } 1404 | 1405 | this.dataDir = getDataPath(this.userProfile.dataDir); 1406 | if (!fs.existsSync(this.dataDir)) { 1407 | mkdirp.sync(this.dataDir, {mode: 0o755}); 1408 | } 1409 | 1410 | this.db = new Database(this.packageName); 1411 | this.db.open().then(() => { 1412 | return this.db.loadConfig(); 1413 | }).then((config) => { 1414 | this.config = config; 1415 | 1416 | if (this.config.persistPropertyValues) { 1417 | return storage.init({ 1418 | dir: this.dataDir, 1419 | }); 1420 | } 1421 | 1422 | return Promise.resolve(); 1423 | }).then(() => { 1424 | this.addAllThings(); 1425 | this.unloading = false; 1426 | this.copyImage(); 1427 | }).catch(console.error); 1428 | } 1429 | 1430 | copyImage() { 1431 | const imagePath = path.join(this.mediaDir, 'image.png'); 1432 | 1433 | if (!fs.existsSync(imagePath)) { 1434 | const localImagePath = path.join(__dirname, 'static', 'image.png'); 1435 | fs.copyFileSync(localImagePath, imagePath); 1436 | } 1437 | } 1438 | 1439 | startTranscode() { 1440 | if (this.unloading || this.transcodeProcess) { 1441 | return; 1442 | } 1443 | 1444 | if (ffmpegMajor === null) { 1445 | return; 1446 | } 1447 | 1448 | const videoPath = path.join(this.mediaDir, 'index.mpd'); 1449 | const localVideoPath = path.join(__dirname, 'static', 'video.mp4'); 1450 | 1451 | const args = [ 1452 | '-y', 1453 | '-re', 1454 | '-stream_loop', '-1', 1455 | '-i', localVideoPath, 1456 | '-window_size', '5', 1457 | '-extra_window_size', '10', 1458 | '-use_template', '1', 1459 | '-use_timeline', '1', 1460 | ]; 1461 | 1462 | if (ffmpegMajor >= 4) { 1463 | args.push( 1464 | '-streaming', '1', 1465 | '-hls_playlist', '1' 1466 | ); 1467 | } 1468 | 1469 | if (ffmpegMajor > 4 || (ffmpegMajor === 4 && ffmpegMinor >= 1)) { 1470 | args.push( 1471 | '-seg_duration', '2', 1472 | '-dash_segment_type', 'mp4' 1473 | ); 1474 | } 1475 | 1476 | args.push( 1477 | '-remove_at_exit', '1', 1478 | '-loglevel', 'quiet', 1479 | '-c:v', 'copy', 1480 | '-c:a', 'copy', 1481 | '-f', 'dash', 1482 | videoPath 1483 | ); 1484 | 1485 | this.transcodeProcess = child_process.spawn('ffmpeg', args); 1486 | this.transcodeProcess.on('close', () => { 1487 | this.transcodeProcess = null; 1488 | this.startTranscode(); 1489 | }); 1490 | this.transcodeProcess.on('error', console.error); 1491 | this.transcodeProcess.stdout.on('data', (data) => { 1492 | if (DEBUG) { 1493 | console.log(`ffmpeg: ${data}`); 1494 | } 1495 | }); 1496 | this.transcodeProcess.stderr.on('data', (data) => { 1497 | if (DEBUG) { 1498 | console.error(`ffmpeg: ${data}`); 1499 | } 1500 | }); 1501 | } 1502 | 1503 | stopTranscode() { 1504 | if (this.transcodeProcess) { 1505 | this.transcodeProcess.removeAllListeners(); 1506 | this.transcodeProcess.stdout.removeAllListeners(); 1507 | this.transcodeProcess.stderr.removeAllListeners(); 1508 | this.transcodeProcess.kill(); 1509 | this.transcodeProcess = null; 1510 | } 1511 | } 1512 | 1513 | startPairing() { 1514 | this.addAllThings(); 1515 | } 1516 | 1517 | addAllThings() { 1518 | if (!this.config.excludeDefaultThings) { 1519 | for (let i = 0; i < VIRTUAL_THINGS.length; i++) { 1520 | const id = `virtual-things-${i}`; 1521 | if (!this.devices[id]) { 1522 | new VirtualThingsDevice(this, id, VIRTUAL_THINGS[i]); 1523 | } 1524 | } 1525 | } 1526 | 1527 | if (this.config.customThings) { 1528 | for (const descr of this.config.customThings) { 1529 | if (!descr.id) { 1530 | descr.id = uuidv4(); 1531 | } 1532 | 1533 | const id = `virtual-things-custom-${descr.id}`; 1534 | if (this.devices[id]) { 1535 | continue; 1536 | } 1537 | 1538 | const properties = (descr.properties || []).map((property) => { 1539 | return Object.assign({}, property); 1540 | }); 1541 | for (const property of properties) { 1542 | // Clean up properties 1543 | if (!['number', 'integer'].includes(property.type)) { 1544 | delete property.unit; 1545 | delete property.minimum; 1546 | delete property.maximum; 1547 | delete property.multipleOf; 1548 | } else { 1549 | if (!property.unit) { 1550 | delete property.unit; 1551 | } 1552 | 1553 | if (property.minimum === property.maximum) { 1554 | delete property.minimum; 1555 | delete property.maximum; 1556 | } 1557 | 1558 | // default in the UI 1559 | if (property.multipleOf === 0) { 1560 | delete property.multipleOf; 1561 | } 1562 | } 1563 | 1564 | if ( 1565 | property.type !== 'string' || 1566 | !Array.isArray(property.enum) || 1567 | !property.enum.length 1568 | ) { 1569 | delete property.enum; 1570 | } 1571 | 1572 | switch (property.type) { 1573 | case 'integer': 1574 | case 'number': 1575 | property.default = Number(property.default); 1576 | break; 1577 | case 'boolean': 1578 | if (property.default === 'true') { 1579 | property.default = true; 1580 | } else if (property.default === 'false') { 1581 | property.default = false; 1582 | } else { 1583 | property.default = !!property.default; 1584 | } 1585 | break; 1586 | case 'null': 1587 | property.default = null; 1588 | break; 1589 | case 'string': 1590 | // just in case 1591 | property.default = `${property.default || ''}`; 1592 | break; 1593 | } 1594 | } 1595 | 1596 | const actions = (descr.actions || []).map((action) => { 1597 | return Object.assign({}, action); 1598 | }); 1599 | for (const action of actions) { 1600 | try { 1601 | action.input = JSON.parse(action.input); 1602 | } catch (ex) { 1603 | delete action.input; 1604 | } 1605 | } 1606 | 1607 | const newDescr = { 1608 | type: 'thing', 1609 | '@context': descr['@context'] || 'https://iot.mozilla.org/schemas', 1610 | '@type': descr['@type'] || [], 1611 | name: descr.name, 1612 | properties: [], 1613 | actions: [], 1614 | events: [], 1615 | }; 1616 | 1617 | for (const property of properties) { 1618 | const prop = { 1619 | name: property.name, 1620 | value: property.default, 1621 | metadata: { 1622 | title: property.title, 1623 | type: property.type, 1624 | }, 1625 | }; 1626 | 1627 | if (property.description) { 1628 | prop.metadata.description = property.description; 1629 | } 1630 | 1631 | if (property['@type']) { 1632 | prop.metadata['@type'] = property['@type']; 1633 | } 1634 | 1635 | if (property.unit) { 1636 | prop.metadata.unit = property.unit; 1637 | } 1638 | 1639 | if (property.hasOwnProperty('minimum')) { 1640 | prop.metadata.minimum = property.minimum; 1641 | } 1642 | 1643 | if (property.hasOwnProperty('maximum')) { 1644 | prop.metadata.maximum = property.maximum; 1645 | } 1646 | 1647 | if (property.hasOwnProperty('multipleOf')) { 1648 | prop.metadata.multipleOf = property.multipleOf; 1649 | } 1650 | 1651 | if (property.hasOwnProperty('enum')) { 1652 | prop.metadata.enum = property.enum; 1653 | } 1654 | 1655 | if (property.hasOwnProperty('readOnly')) { 1656 | prop.metadata.readOnly = property.readOnly; 1657 | } 1658 | 1659 | newDescr.properties.push(prop); 1660 | } 1661 | 1662 | for (const action of actions) { 1663 | const act = { 1664 | name: action.name, 1665 | metadata: { 1666 | title: action.title, 1667 | }, 1668 | }; 1669 | 1670 | if (action.description) { 1671 | act.metadata.description = action.description; 1672 | } 1673 | 1674 | if (action.input) { 1675 | act.metadata.input = action.input; 1676 | } 1677 | 1678 | if (action.emitEvent) { 1679 | const event = { 1680 | name: action.name, 1681 | metadata: { 1682 | title: action.title, 1683 | }, 1684 | }; 1685 | if (action.input) { 1686 | event.type = action.input.type; 1687 | } 1688 | newDescr.events.push(event); 1689 | } 1690 | 1691 | newDescr.actions.push(act); 1692 | } 1693 | 1694 | new VirtualThingsDevice(this, id, newDescr); 1695 | } 1696 | 1697 | return this.db.saveConfig(this.config); 1698 | } 1699 | 1700 | return Promise.resolve(); 1701 | } 1702 | 1703 | setPin(deviceId, pin) { 1704 | return new Promise((resolve, reject) => { 1705 | const device = this.getDevice(deviceId); 1706 | if (device && device.pinRequired && pin === '1234') { 1707 | resolve(); 1708 | } else { 1709 | reject('Invalid PIN'); 1710 | } 1711 | }); 1712 | } 1713 | 1714 | setCredentials(deviceId, username, password) { 1715 | return new Promise((resolve, reject) => { 1716 | const device = this.getDevice(deviceId); 1717 | if (device && device.credentialsRequired && username === 'user' && 1718 | password === 'password') { 1719 | resolve(); 1720 | } else { 1721 | reject('Invalid credentials'); 1722 | } 1723 | }); 1724 | } 1725 | 1726 | unload() { 1727 | if (this.config.randomizePropertyValues) { 1728 | for (const device of Object.values(this.devices)) { 1729 | for (const property of device.properties.values()) { 1730 | clearInterval(property.interval); 1731 | } 1732 | } 1733 | } 1734 | 1735 | this.unloading = true; 1736 | this.stopTranscode(); 1737 | return super.unload(); 1738 | } 1739 | } 1740 | 1741 | module.exports = VirtualThingsAdapter; 1742 | 1743 | --------------------------------------------------------------------------------