├── .gitignore
├── HISTORY.md
├── LICENSE
├── README.md
├── assets
└── screens
│ ├── screen1.png
│ ├── screen1_thumb.png
│ ├── screen2.png
│ ├── screen2_thumb.png
│ ├── screen3.png
│ ├── screen3_thumb.png
│ ├── screen4.png
│ ├── screen4_thumb.png
│ ├── screen5.png
│ └── screen5_thumb.png
├── device-config-schema.coffee
├── devices
├── mqtt-buttons.coffee
├── mqtt-contact-sensor.coffee
├── mqtt-dimmer.coffee
├── mqtt-input.coffee
├── mqtt-presence-sensor.coffee
├── mqtt-sensor.coffee
├── mqtt-shutter.coffee
└── mqtt-switch.coffee
├── mqtt-config-schema.coffee
├── mqtt.coffee
├── package.json
└── predicates_and_actions
├── mqtt_action.coffee
└── mqtt_predicate.coffee
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | doc/*
3 | .js
4 | npm-debug.log
5 |
--------------------------------------------------------------------------------
/HISTORY.md:
--------------------------------------------------------------------------------
1 | # Release History
2 |
3 | * 20190906, V0.9.13
4 | * Fixture: Add lastValue parameter to MqttSensor constructor to be able to restore
5 | attributes from database on startup
6 | * Added default initialization for MqttSensor attributes values in case the values cannot be
7 | restored from database
8 | * Added experimental device discovery for Tasmota switch and dimmer devices, issue #42
9 | * Added support for wildcards (#/+) on state topics, issue #11
10 | * Added JSON payload filtering for MqttSwitch state values, issue #34
11 | * Added JSON payload filtering for MqttPresenceSensor state values, issue #45
12 | * Added JSON payload filtering for MqttDimmer, MqttButtons, MqttContactSensor, and MqttShutter
13 | state values
14 | * Added recovery of last state from database for MqttButtons device on startup
15 | * Added support for displaying the status (last button pressed) for MqttButtons device, issue #43
16 | * Fixed setting of default brokerId in case no brokerId has been set in the plugin config
17 |
18 | * 20190824, V0.9.12
19 | * Added predicate to trigger rules by a received MQTT message, PR #44, thanks @crycode-de
20 | * Allow for client connection with username/password authentication if broker allows
21 | anonymous access
22 |
23 | * 20190820, V0.9.11
24 | * Added peer dependency to ensure pimatic plugin manager will pickup the latest release
25 | * Minor fixes to auto resetting presence, PR #41, thanks @qistoph
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU AFFERO GENERAL PUBLIC LICENSE
2 | Version 3, 19 November 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU Affero General Public License is a free, copyleft license for
11 | software and other kinds of works, specifically designed to ensure
12 | cooperation with the community in the case of network server software.
13 |
14 | The licenses for most software and other practical works are designed
15 | to take away your freedom to share and change the works. By contrast,
16 | our General Public Licenses are intended to guarantee your freedom to
17 | share and change all versions of a program--to make sure it remains free
18 | software for all its users.
19 |
20 | When we speak of free software, we are referring to freedom, not
21 | price. Our General Public Licenses are designed to make sure that you
22 | have the freedom to distribute copies of free software (and charge for
23 | them if you wish), that you receive source code or can get it if you
24 | want it, that you can change the software or use pieces of it in new
25 | free programs, and that you know you can do these things.
26 |
27 | Developers that use our General Public Licenses protect your rights
28 | with two steps: (1) assert copyright on the software, and (2) offer
29 | you this License which gives you legal permission to copy, distribute
30 | and/or modify the software.
31 |
32 | A secondary benefit of defending all users' freedom is that
33 | improvements made in alternate versions of the program, if they
34 | receive widespread use, become available for other developers to
35 | incorporate. Many developers of free software are heartened and
36 | encouraged by the resulting cooperation. However, in the case of
37 | software used on network servers, this result may fail to come about.
38 | The GNU General Public License permits making a modified version and
39 | letting the public access it on a server without ever releasing its
40 | source code to the public.
41 |
42 | The GNU Affero General Public License is designed specifically to
43 | ensure that, in such cases, the modified source code becomes available
44 | to the community. It requires the operator of a network server to
45 | provide the source code of the modified version running there to the
46 | users of that server. Therefore, public use of a modified version, on
47 | a publicly accessible server, gives the public access to the source
48 | code of the modified version.
49 |
50 | An older license, called the Affero General Public License and
51 | published by Affero, was designed to accomplish similar goals. This is
52 | a different license, not a version of the Affero GPL, but Affero has
53 | released a new version of the Affero GPL which permits relicensing under
54 | this license.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | TERMS AND CONDITIONS
60 |
61 | 0. Definitions.
62 |
63 | "This License" refers to version 3 of the GNU Affero General Public License.
64 |
65 | "Copyright" also means copyright-like laws that apply to other kinds of
66 | works, such as semiconductor masks.
67 |
68 | "The Program" refers to any copyrightable work licensed under this
69 | License. Each licensee is addressed as "you". "Licensees" and
70 | "recipients" may be individuals or organizations.
71 |
72 | To "modify" a work means to copy from or adapt all or part of the work
73 | in a fashion requiring copyright permission, other than the making of an
74 | exact copy. The resulting work is called a "modified version" of the
75 | earlier work or a work "based on" the earlier work.
76 |
77 | A "covered work" means either the unmodified Program or a work based
78 | on the Program.
79 |
80 | To "propagate" a work means to do anything with it that, without
81 | permission, would make you directly or secondarily liable for
82 | infringement under applicable copyright law, except executing it on a
83 | computer or modifying a private copy. Propagation includes copying,
84 | distribution (with or without modification), making available to the
85 | public, and in some countries other activities as well.
86 |
87 | To "convey" a work means any kind of propagation that enables other
88 | parties to make or receive copies. Mere interaction with a user through
89 | a computer network, with no transfer of a copy, is not conveying.
90 |
91 | An interactive user interface displays "Appropriate Legal Notices"
92 | to the extent that it includes a convenient and prominently visible
93 | feature that (1) displays an appropriate copyright notice, and (2)
94 | tells the user that there is no warranty for the work (except to the
95 | extent that warranties are provided), that licensees may convey the
96 | work under this License, and how to view a copy of this License. If
97 | the interface presents a list of user commands or options, such as a
98 | menu, a prominent item in the list meets this criterion.
99 |
100 | 1. Source Code.
101 |
102 | The "source code" for a work means the preferred form of the work
103 | for making modifications to it. "Object code" means any non-source
104 | form of a work.
105 |
106 | A "Standard Interface" means an interface that either is an official
107 | standard defined by a recognized standards body, or, in the case of
108 | interfaces specified for a particular programming language, one that
109 | is widely used among developers working in that language.
110 |
111 | The "System Libraries" of an executable work include anything, other
112 | than the work as a whole, that (a) is included in the normal form of
113 | packaging a Major Component, but which is not part of that Major
114 | Component, and (b) serves only to enable use of the work with that
115 | Major Component, or to implement a Standard Interface for which an
116 | implementation is available to the public in source code form. A
117 | "Major Component", in this context, means a major essential component
118 | (kernel, window system, and so on) of the specific operating system
119 | (if any) on which the executable work runs, or a compiler used to
120 | produce the work, or an object code interpreter used to run it.
121 |
122 | The "Corresponding Source" for a work in object code form means all
123 | the source code needed to generate, install, and (for an executable
124 | work) run the object code and to modify the work, including scripts to
125 | control those activities. However, it does not include the work's
126 | System Libraries, or general-purpose tools or generally available free
127 | programs which are used unmodified in performing those activities but
128 | which are not part of the work. For example, Corresponding Source
129 | includes interface definition files associated with source files for
130 | the work, and the source code for shared libraries and dynamically
131 | linked subprograms that the work is specifically designed to require,
132 | such as by intimate data communication or control flow between those
133 | subprograms and other parts of the work.
134 |
135 | The Corresponding Source need not include anything that users
136 | can regenerate automatically from other parts of the Corresponding
137 | Source.
138 |
139 | The Corresponding Source for a work in source code form is that
140 | same work.
141 |
142 | 2. Basic Permissions.
143 |
144 | All rights granted under this License are granted for the term of
145 | copyright on the Program, and are irrevocable provided the stated
146 | conditions are met. This License explicitly affirms your unlimited
147 | permission to run the unmodified Program. The output from running a
148 | covered work is covered by this License only if the output, given its
149 | content, constitutes a covered work. This License acknowledges your
150 | rights of fair use or other equivalent, as provided by copyright law.
151 |
152 | You may make, run and propagate covered works that you do not
153 | convey, without conditions so long as your license otherwise remains
154 | in force. You may convey covered works to others for the sole purpose
155 | of having them make modifications exclusively for you, or provide you
156 | with facilities for running those works, provided that you comply with
157 | the terms of this License in conveying all material for which you do
158 | not control copyright. Those thus making or running the covered works
159 | for you must do so exclusively on your behalf, under your direction
160 | and control, on terms that prohibit them from making any copies of
161 | your copyrighted material outside their relationship with you.
162 |
163 | Conveying under any other circumstances is permitted solely under
164 | the conditions stated below. Sublicensing is not allowed; section 10
165 | makes it unnecessary.
166 |
167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168 |
169 | No covered work shall be deemed part of an effective technological
170 | measure under any applicable law fulfilling obligations under article
171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172 | similar laws prohibiting or restricting circumvention of such
173 | measures.
174 |
175 | When you convey a covered work, you waive any legal power to forbid
176 | circumvention of technological measures to the extent such circumvention
177 | is effected by exercising rights under this License with respect to
178 | the covered work, and you disclaim any intention to limit operation or
179 | modification of the work as a means of enforcing, against the work's
180 | users, your or third parties' legal rights to forbid circumvention of
181 | technological measures.
182 |
183 | 4. Conveying Verbatim Copies.
184 |
185 | You may convey verbatim copies of the Program's source code as you
186 | receive it, in any medium, provided that you conspicuously and
187 | appropriately publish on each copy an appropriate copyright notice;
188 | keep intact all notices stating that this License and any
189 | non-permissive terms added in accord with section 7 apply to the code;
190 | keep intact all notices of the absence of any warranty; and give all
191 | recipients a copy of this License along with the Program.
192 |
193 | You may charge any price or no price for each copy that you convey,
194 | and you may offer support or warranty protection for a fee.
195 |
196 | 5. Conveying Modified Source Versions.
197 |
198 | You may convey a work based on the Program, or the modifications to
199 | produce it from the Program, in the form of source code under the
200 | terms of section 4, provided that you also meet all of these conditions:
201 |
202 | a) The work must carry prominent notices stating that you modified
203 | it, and giving a relevant date.
204 |
205 | b) The work must carry prominent notices stating that it is
206 | released under this License and any conditions added under section
207 | 7. This requirement modifies the requirement in section 4 to
208 | "keep intact all notices".
209 |
210 | c) You must license the entire work, as a whole, under this
211 | License to anyone who comes into possession of a copy. This
212 | License will therefore apply, along with any applicable section 7
213 | additional terms, to the whole of the work, and all its parts,
214 | regardless of how they are packaged. This License gives no
215 | permission to license the work in any other way, but it does not
216 | invalidate such permission if you have separately received it.
217 |
218 | d) If the work has interactive user interfaces, each must display
219 | Appropriate Legal Notices; however, if the Program has interactive
220 | interfaces that do not display Appropriate Legal Notices, your
221 | work need not make them do so.
222 |
223 | A compilation of a covered work with other separate and independent
224 | works, which are not by their nature extensions of the covered work,
225 | and which are not combined with it such as to form a larger program,
226 | in or on a volume of a storage or distribution medium, is called an
227 | "aggregate" if the compilation and its resulting copyright are not
228 | used to limit the access or legal rights of the compilation's users
229 | beyond what the individual works permit. Inclusion of a covered work
230 | in an aggregate does not cause this License to apply to the other
231 | parts of the aggregate.
232 |
233 | 6. Conveying Non-Source Forms.
234 |
235 | You may convey a covered work in object code form under the terms
236 | of sections 4 and 5, provided that you also convey the
237 | machine-readable Corresponding Source under the terms of this License,
238 | in one of these ways:
239 |
240 | a) Convey the object code in, or embodied in, a physical product
241 | (including a physical distribution medium), accompanied by the
242 | Corresponding Source fixed on a durable physical medium
243 | customarily used for software interchange.
244 |
245 | b) Convey the object code in, or embodied in, a physical product
246 | (including a physical distribution medium), accompanied by a
247 | written offer, valid for at least three years and valid for as
248 | long as you offer spare parts or customer support for that product
249 | model, to give anyone who possesses the object code either (1) a
250 | copy of the Corresponding Source for all the software in the
251 | product that is covered by this License, on a durable physical
252 | medium customarily used for software interchange, for a price no
253 | more than your reasonable cost of physically performing this
254 | conveying of source, or (2) access to copy the
255 | Corresponding Source from a network server at no charge.
256 |
257 | c) Convey individual copies of the object code with a copy of the
258 | written offer to provide the Corresponding Source. This
259 | alternative is allowed only occasionally and noncommercially, and
260 | only if you received the object code with such an offer, in accord
261 | with subsection 6b.
262 |
263 | d) Convey the object code by offering access from a designated
264 | place (gratis or for a charge), and offer equivalent access to the
265 | Corresponding Source in the same way through the same place at no
266 | further charge. You need not require recipients to copy the
267 | Corresponding Source along with the object code. If the place to
268 | copy the object code is a network server, the Corresponding Source
269 | may be on a different server (operated by you or a third party)
270 | that supports equivalent copying facilities, provided you maintain
271 | clear directions next to the object code saying where to find the
272 | Corresponding Source. Regardless of what server hosts the
273 | Corresponding Source, you remain obligated to ensure that it is
274 | available for as long as needed to satisfy these requirements.
275 |
276 | e) Convey the object code using peer-to-peer transmission, provided
277 | you inform other peers where the object code and Corresponding
278 | Source of the work are being offered to the general public at no
279 | charge under subsection 6d.
280 |
281 | A separable portion of the object code, whose source code is excluded
282 | from the Corresponding Source as a System Library, need not be
283 | included in conveying the object code work.
284 |
285 | A "User Product" is either (1) a "consumer product", which means any
286 | tangible personal property which is normally used for personal, family,
287 | or household purposes, or (2) anything designed or sold for incorporation
288 | into a dwelling. In determining whether a product is a consumer product,
289 | doubtful cases shall be resolved in favor of coverage. For a particular
290 | product received by a particular user, "normally used" refers to a
291 | typical or common use of that class of product, regardless of the status
292 | of the particular user or of the way in which the particular user
293 | actually uses, or expects or is expected to use, the product. A product
294 | is a consumer product regardless of whether the product has substantial
295 | commercial, industrial or non-consumer uses, unless such uses represent
296 | the only significant mode of use of the product.
297 |
298 | "Installation Information" for a User Product means any methods,
299 | procedures, authorization keys, or other information required to install
300 | and execute modified versions of a covered work in that User Product from
301 | a modified version of its Corresponding Source. The information must
302 | suffice to ensure that the continued functioning of the modified object
303 | code is in no case prevented or interfered with solely because
304 | modification has been made.
305 |
306 | If you convey an object code work under this section in, or with, or
307 | specifically for use in, a User Product, and the conveying occurs as
308 | part of a transaction in which the right of possession and use of the
309 | User Product is transferred to the recipient in perpetuity or for a
310 | fixed term (regardless of how the transaction is characterized), the
311 | Corresponding Source conveyed under this section must be accompanied
312 | by the Installation Information. But this requirement does not apply
313 | if neither you nor any third party retains the ability to install
314 | modified object code on the User Product (for example, the work has
315 | been installed in ROM).
316 |
317 | The requirement to provide Installation Information does not include a
318 | requirement to continue to provide support service, warranty, or updates
319 | for a work that has been modified or installed by the recipient, or for
320 | the User Product in which it has been modified or installed. Access to a
321 | network may be denied when the modification itself materially and
322 | adversely affects the operation of the network or violates the rules and
323 | protocols for communication across the network.
324 |
325 | Corresponding Source conveyed, and Installation Information provided,
326 | in accord with this section must be in a format that is publicly
327 | documented (and with an implementation available to the public in
328 | source code form), and must require no special password or key for
329 | unpacking, reading or copying.
330 |
331 | 7. Additional Terms.
332 |
333 | "Additional permissions" are terms that supplement the terms of this
334 | License by making exceptions from one or more of its conditions.
335 | Additional permissions that are applicable to the entire Program shall
336 | be treated as though they were included in this License, to the extent
337 | that they are valid under applicable law. If additional permissions
338 | apply only to part of the Program, that part may be used separately
339 | under those permissions, but the entire Program remains governed by
340 | this License without regard to the additional permissions.
341 |
342 | When you convey a copy of a covered work, you may at your option
343 | remove any additional permissions from that copy, or from any part of
344 | it. (Additional permissions may be written to require their own
345 | removal in certain cases when you modify the work.) You may place
346 | additional permissions on material, added by you to a covered work,
347 | for which you have or can give appropriate copyright permission.
348 |
349 | Notwithstanding any other provision of this License, for material you
350 | add to a covered work, you may (if authorized by the copyright holders of
351 | that material) supplement the terms of this License with terms:
352 |
353 | a) Disclaiming warranty or limiting liability differently from the
354 | terms of sections 15 and 16 of this License; or
355 |
356 | b) Requiring preservation of specified reasonable legal notices or
357 | author attributions in that material or in the Appropriate Legal
358 | Notices displayed by works containing it; or
359 |
360 | c) Prohibiting misrepresentation of the origin of that material, or
361 | requiring that modified versions of such material be marked in
362 | reasonable ways as different from the original version; or
363 |
364 | d) Limiting the use for publicity purposes of names of licensors or
365 | authors of the material; or
366 |
367 | e) Declining to grant rights under trademark law for use of some
368 | trade names, trademarks, or service marks; or
369 |
370 | f) Requiring indemnification of licensors and authors of that
371 | material by anyone who conveys the material (or modified versions of
372 | it) with contractual assumptions of liability to the recipient, for
373 | any liability that these contractual assumptions directly impose on
374 | those licensors and authors.
375 |
376 | All other non-permissive additional terms are considered "further
377 | restrictions" within the meaning of section 10. If the Program as you
378 | received it, or any part of it, contains a notice stating that it is
379 | governed by this License along with a term that is a further
380 | restriction, you may remove that term. If a license document contains
381 | a further restriction but permits relicensing or conveying under this
382 | License, you may add to a covered work material governed by the terms
383 | of that license document, provided that the further restriction does
384 | not survive such relicensing or conveying.
385 |
386 | If you add terms to a covered work in accord with this section, you
387 | must place, in the relevant source files, a statement of the
388 | additional terms that apply to those files, or a notice indicating
389 | where to find the applicable terms.
390 |
391 | Additional terms, permissive or non-permissive, may be stated in the
392 | form of a separately written license, or stated as exceptions;
393 | the above requirements apply either way.
394 |
395 | 8. Termination.
396 |
397 | You may not propagate or modify a covered work except as expressly
398 | provided under this License. Any attempt otherwise to propagate or
399 | modify it is void, and will automatically terminate your rights under
400 | this License (including any patent licenses granted under the third
401 | paragraph of section 11).
402 |
403 | However, if you cease all violation of this License, then your
404 | license from a particular copyright holder is reinstated (a)
405 | provisionally, unless and until the copyright holder explicitly and
406 | finally terminates your license, and (b) permanently, if the copyright
407 | holder fails to notify you of the violation by some reasonable means
408 | prior to 60 days after the cessation.
409 |
410 | Moreover, your license from a particular copyright holder is
411 | reinstated permanently if the copyright holder notifies you of the
412 | violation by some reasonable means, this is the first time you have
413 | received notice of violation of this License (for any work) from that
414 | copyright holder, and you cure the violation prior to 30 days after
415 | your receipt of the notice.
416 |
417 | Termination of your rights under this section does not terminate the
418 | licenses of parties who have received copies or rights from you under
419 | this License. If your rights have been terminated and not permanently
420 | reinstated, you do not qualify to receive new licenses for the same
421 | material under section 10.
422 |
423 | 9. Acceptance Not Required for Having Copies.
424 |
425 | You are not required to accept this License in order to receive or
426 | run a copy of the Program. Ancillary propagation of a covered work
427 | occurring solely as a consequence of using peer-to-peer transmission
428 | to receive a copy likewise does not require acceptance. However,
429 | nothing other than this License grants you permission to propagate or
430 | modify any covered work. These actions infringe copyright if you do
431 | not accept this License. Therefore, by modifying or propagating a
432 | covered work, you indicate your acceptance of this License to do so.
433 |
434 | 10. Automatic Licensing of Downstream Recipients.
435 |
436 | Each time you convey a covered work, the recipient automatically
437 | receives a license from the original licensors, to run, modify and
438 | propagate that work, subject to this License. You are not responsible
439 | for enforcing compliance by third parties with this License.
440 |
441 | An "entity transaction" is a transaction transferring control of an
442 | organization, or substantially all assets of one, or subdividing an
443 | organization, or merging organizations. If propagation of a covered
444 | work results from an entity transaction, each party to that
445 | transaction who receives a copy of the work also receives whatever
446 | licenses to the work the party's predecessor in interest had or could
447 | give under the previous paragraph, plus a right to possession of the
448 | Corresponding Source of the work from the predecessor in interest, if
449 | the predecessor has it or can get it with reasonable efforts.
450 |
451 | You may not impose any further restrictions on the exercise of the
452 | rights granted or affirmed under this License. For example, you may
453 | not impose a license fee, royalty, or other charge for exercise of
454 | rights granted under this License, and you may not initiate litigation
455 | (including a cross-claim or counterclaim in a lawsuit) alleging that
456 | any patent claim is infringed by making, using, selling, offering for
457 | sale, or importing the Program or any portion of it.
458 |
459 | 11. Patents.
460 |
461 | A "contributor" is a copyright holder who authorizes use under this
462 | License of the Program or a work on which the Program is based. The
463 | work thus licensed is called the contributor's "contributor version".
464 |
465 | A contributor's "essential patent claims" are all patent claims
466 | owned or controlled by the contributor, whether already acquired or
467 | hereafter acquired, that would be infringed by some manner, permitted
468 | by this License, of making, using, or selling its contributor version,
469 | but do not include claims that would be infringed only as a
470 | consequence of further modification of the contributor version. For
471 | purposes of this definition, "control" includes the right to grant
472 | patent sublicenses in a manner consistent with the requirements of
473 | this License.
474 |
475 | Each contributor grants you a non-exclusive, worldwide, royalty-free
476 | patent license under the contributor's essential patent claims, to
477 | make, use, sell, offer for sale, import and otherwise run, modify and
478 | propagate the contents of its contributor version.
479 |
480 | In the following three paragraphs, a "patent license" is any express
481 | agreement or commitment, however denominated, not to enforce a patent
482 | (such as an express permission to practice a patent or covenant not to
483 | sue for patent infringement). To "grant" such a patent license to a
484 | party means to make such an agreement or commitment not to enforce a
485 | patent against the party.
486 |
487 | If you convey a covered work, knowingly relying on a patent license,
488 | and the Corresponding Source of the work is not available for anyone
489 | to copy, free of charge and under the terms of this License, through a
490 | publicly available network server or other readily accessible means,
491 | then you must either (1) cause the Corresponding Source to be so
492 | available, or (2) arrange to deprive yourself of the benefit of the
493 | patent license for this particular work, or (3) arrange, in a manner
494 | consistent with the requirements of this License, to extend the patent
495 | license to downstream recipients. "Knowingly relying" means you have
496 | actual knowledge that, but for the patent license, your conveying the
497 | covered work in a country, or your recipient's use of the covered work
498 | in a country, would infringe one or more identifiable patents in that
499 | country that you have reason to believe are valid.
500 |
501 | If, pursuant to or in connection with a single transaction or
502 | arrangement, you convey, or propagate by procuring conveyance of, a
503 | covered work, and grant a patent license to some of the parties
504 | receiving the covered work authorizing them to use, propagate, modify
505 | or convey a specific copy of the covered work, then the patent license
506 | you grant is automatically extended to all recipients of the covered
507 | work and works based on it.
508 |
509 | A patent license is "discriminatory" if it does not include within
510 | the scope of its coverage, prohibits the exercise of, or is
511 | conditioned on the non-exercise of one or more of the rights that are
512 | specifically granted under this License. You may not convey a covered
513 | work if you are a party to an arrangement with a third party that is
514 | in the business of distributing software, under which you make payment
515 | to the third party based on the extent of your activity of conveying
516 | the work, and under which the third party grants, to any of the
517 | parties who would receive the covered work from you, a discriminatory
518 | patent license (a) in connection with copies of the covered work
519 | conveyed by you (or copies made from those copies), or (b) primarily
520 | for and in connection with specific products or compilations that
521 | contain the covered work, unless you entered into that arrangement,
522 | or that patent license was granted, prior to 28 March 2007.
523 |
524 | Nothing in this License shall be construed as excluding or limiting
525 | any implied license or other defenses to infringement that may
526 | otherwise be available to you under applicable patent law.
527 |
528 | 12. No Surrender of Others' Freedom.
529 |
530 | If conditions are imposed on you (whether by court order, agreement or
531 | otherwise) that contradict the conditions of this License, they do not
532 | excuse you from the conditions of this License. If you cannot convey a
533 | covered work so as to satisfy simultaneously your obligations under this
534 | License and any other pertinent obligations, then as a consequence you may
535 | not convey it at all. For example, if you agree to terms that obligate you
536 | to collect a royalty for further conveying from those to whom you convey
537 | the Program, the only way you could satisfy both those terms and this
538 | License would be to refrain entirely from conveying the Program.
539 |
540 | 13. Remote Network Interaction; Use with the GNU General Public License.
541 |
542 | Notwithstanding any other provision of this License, if you modify the
543 | Program, your modified version must prominently offer all users
544 | interacting with it remotely through a computer network (if your version
545 | supports such interaction) an opportunity to receive the Corresponding
546 | Source of your version by providing access to the Corresponding Source
547 | from a network server at no charge, through some standard or customary
548 | means of facilitating copying of software. This Corresponding Source
549 | shall include the Corresponding Source for any work covered by version 3
550 | of the GNU General Public License that is incorporated pursuant to the
551 | following paragraph.
552 |
553 | Notwithstanding any other provision of this License, you have
554 | permission to link or combine any covered work with a work licensed
555 | under version 3 of the GNU General Public License into a single
556 | combined work, and to convey the resulting work. The terms of this
557 | License will continue to apply to the part which is the covered work,
558 | but the work with which it is combined will remain governed by version
559 | 3 of the GNU General Public License.
560 |
561 | 14. Revised Versions of this License.
562 |
563 | The Free Software Foundation may publish revised and/or new versions of
564 | the GNU Affero General Public License from time to time. Such new versions
565 | will be similar in spirit to the present version, but may differ in detail to
566 | address new problems or concerns.
567 |
568 | Each version is given a distinguishing version number. If the
569 | Program specifies that a certain numbered version of the GNU Affero General
570 | Public License "or any later version" applies to it, you have the
571 | option of following the terms and conditions either of that numbered
572 | version or of any later version published by the Free Software
573 | Foundation. If the Program does not specify a version number of the
574 | GNU Affero General Public License, you may choose any version ever published
575 | by the Free Software Foundation.
576 |
577 | If the Program specifies that a proxy can decide which future
578 | versions of the GNU Affero General Public License can be used, that proxy's
579 | public statement of acceptance of a version permanently authorizes you
580 | to choose that version for the Program.
581 |
582 | Later license versions may give you additional or different
583 | permissions. However, no additional obligations are imposed on any
584 | author or copyright holder as a result of your choosing to follow a
585 | later version.
586 |
587 | 15. Disclaimer of Warranty.
588 |
589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597 |
598 | 16. Limitation of Liability.
599 |
600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608 | SUCH DAMAGES.
609 |
610 | 17. Interpretation of Sections 15 and 16.
611 |
612 | If the disclaimer of warranty and limitation of liability provided
613 | above cannot be given local legal effect according to their terms,
614 | reviewing courts shall apply local law that most closely approximates
615 | an absolute waiver of all civil liability in connection with the
616 | Program, unless a warranty or assumption of liability accompanies a
617 | copy of the Program in return for a fee.
618 |
619 | END OF TERMS AND CONDITIONS
620 |
621 | How to Apply These Terms to Your New Programs
622 |
623 | If you develop a new program, and you want it to be of the greatest
624 | possible use to the public, the best way to achieve this is to make it
625 | free software which everyone can redistribute and change under these terms.
626 |
627 | To do so, attach the following notices to the program. It is safest
628 | to attach them to the start of each source file to most effectively
629 | state the exclusion of warranty; and each file should have at least
630 | the "copyright" line and a pointer to where the full notice is found.
631 |
632 |
633 | Copyright (C)
634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published
637 | by the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
662 |
663 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # pimatic-mqtt
3 |
4 | [](https://badge.fury.io/js/pimatic-mqtt)
5 |
6 | MQTT plugin for Pimatic
7 |
8 | ## Screenshots
9 | [![Screenshot 1][screen1_thumb]](https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen1.png)
10 | [![Screenshot 2][screen2_thumb]](https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen2.png)
11 | [![Screenshot 3][screen3_thumb]](https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen3.png)
12 | [![Screenshot 4][screen4_thumb]](https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen4.png)
13 | [![Screenshot 5][screen5_thumb]](https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen5.png)
14 |
15 | [screen1_thumb]: https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen1_thumb.png?v=1
16 | [screen2_thumb]: https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen2_thumb.png?v=1
17 | [screen3_thumb]: https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen3_thumb.png?v=1
18 | [screen4_thumb]: https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen4_thumb.png?v=1
19 | [screen5_thumb]: https://github.com/wutu/pimatic-mqtt/raw/master/assets/screens/screen5_thumb.png?v=1
20 |
21 | ## Status of implementation
22 |
23 | This version supports the following
24 |
25 | * General sensor (numeric and text data from payload)
26 | * Switch
27 | * PresenceSensor
28 | * ContactSensor
29 | * Dimmer
30 | * Buttons
31 | * Shutter
32 | * Input
33 |
34 | ## Sponsoring
35 |
36 | Do you like this plugin? Then please consider a donation to support the development.
37 |
38 |
39 |
40 | ## Getting Started
41 |
42 | This section is still work in progress.
43 |
44 | ## Plugin Configuration
45 |
46 | While run MQTT broker on localhost and on a standard port, without autentification, you can load the plugin by editing your `config.json` to include the following
47 | in the `plugins` section.
48 |
49 | {
50 | "plugin": "mqtt",
51 | "active": true,
52 | "brokers": [
53 | {
54 | "brokerId": "default"
55 | }
56 | ]
57 | }
58 |
59 | Configuration with two Brokers
60 |
61 | {
62 | "plugin": "mqtt",
63 | "active": true,
64 | "brokers": [
65 | {
66 | "brokerId": "default"
67 | "host": "localhost"
68 | },
69 | {
70 | "brokerId": "eclipse",
71 | "host": "iot.eclipse.org"
72 | }
73 | ]
74 | }
75 |
76 | The configuration for a broker is an object comprising the following properties.
77 |
78 | | Property | Default | Type | Description |
79 | |:--------------------|:------------|:--------|:----------------------------------------------------------------------------------------|
80 | | brokerId | "default" | String | Id of the broker |
81 | | host | "127.0.0.1" | String | Broker hostname or IP |
82 | | port | 1883 | Integer | Broker port |
83 | | keepalive | 180 | Integer | Keepalive in seconds |
84 | | clientId | pimatic* | String | *pimatic + random number or your own clientId |
85 | | protocolId | "MQTT" | String | With broker that supports only MQTT 3.1 (not 3.1.1 compliant), you should pass "MQIsdp" |
86 | | protocolVer | 4 | Integer | With broker that supports only MQTT 3.1 (not 3.1.1 compliant), you should pass 3 |
87 | | cleanSession | true | Boolean | Set to false to receive QoS 1 and 2 messages while offline |
88 | | reconnect | 5000 | Integer | Reconnect period in milliseconds |
89 | | timeout | 30000 | Integer | Connect timeout in milliseconds |
90 | | queueQoSZero | true | Boolean | If connection is broken, queue outgoing QoS zero messages |
91 | | username | - | String | The login name |
92 | | password | - | String | The Password |
93 | | certPath | - | String | Path to the certificate of the client in PEM format, required for TLS connection |
94 | | keyPath | - | String | Path to the key of the client in PEM format, required for TLS connection |
95 | | rejectUnauthorized | true | Boolean | Whether to reject self signed certificates |
96 | | ca | - | String | Path to the trusted CA list |
97 |
98 |
99 | ## Device Configuration
100 |
101 | Devices must be added manually to the device section of your pimatic config.
102 |
103 | ### Generic sensor
104 |
105 | `MqttSensor` is based on the Sensor device class. Handles numeric and text data from the payload.
106 |
107 | {
108 | "name": "Soil Hygrometer analog reading",
109 | "id": "wemosd1r2-2",
110 | "class": "MqttSensor",
111 | "attributes": [
112 | {
113 | "name": "soil-hygrometer",
114 | "topic": "wemosd1r2/moisture/humidity",
115 | "type": "number",
116 | "acronym": "rH"
117 | }
118 | ]
119 | },
120 | {
121 | "name": "ESP01 with battery",
122 | "id": "esp01",
123 | "class": "MqttSensor",
124 | "attributes": [
125 | {
126 | "name": "temperature",
127 | "topic": "myhome/firstfloor/office/esp01/dht11/temperature",
128 | "type": "number",
129 | "unit": "°C",
130 | "acronym": "DHT-11-Temperature"
131 | },
132 | {
133 | "name": "humidity",
134 | "topic": "myhome/firstfloor/office/esp01/dht11/humidity",
135 | "type": "number",
136 | "unit": "%",
137 | "acronym": "DHT-11-Humidity"
138 | }
139 | ]
140 | },
141 | {
142 | "name": "Mosquitto",
143 | "id": "mosquitto",
144 | "class": "MqttSensor",
145 | "attributes": [
146 | {
147 | "name": "connected-clients",
148 | "topic": "$SYS/broker/clients/connected",
149 | "type": "number",
150 | "acronym": "Clients",
151 | "discrete": true
152 | },
153 | {
154 | "name": "ram-usage",
155 | "topic": "$SYS/broker/heap/current",
156 | "type": "number",
157 | "unit": "B",
158 | "acronym": "RAM usage"
159 | }
160 | ],
161 | "xAttributeOptions": [
162 | {
163 | "name": "connected-clients",
164 | "displaySparkline": false
165 | },
166 | {
167 | "name": "ram-usage",
168 | "displaySparkline": false
169 | }
170 | ]
171 | }
172 |
173 | Supports lookup table to translate received message to another value.
174 |
175 | {
176 | "name": "Sensor with lookup",
177 | "id": "sensor-with-lookup",
178 | "class": "MqttSensor",
179 | "attributes": [
180 | {
181 | "name": "state",
182 | "topic": "some/topic",
183 | "type": "string",
184 | "unit": "",
185 | "acronym": "",
186 | "messageMap": {
187 | "0": "Not ready",
188 | "1": "Ready",
189 | "2": "Completed"
190 | }
191 | }
192 | ]
193 | }
194 |
195 | Accepts flat JSON message
196 |
197 | Sample mqtt message: {"rel_pressue": "30.5015", "wind_ave": "0.00", "rain": "0", "rainin": "0", "hum_in": "64", "temp_in_f": "66.4", "dailyrainin": "0", "wind_dir": "225", "temp_in_c": "19.1", "hum_out": "81", "dailyrain": "0", "wind_gust": "0.00", "idx": "2015-10-22 21:41:03", "temp_out_f": "49.6", "temp_out_c": "9.8"}
198 |
199 | {
200 | "class": "MqttSensor",
201 | "id": "weatherstation",
202 | "name": "Weather Station",
203 | "attributes": [
204 | {
205 | "name": "temp_in_c",
206 | "topic": "weatherstation",
207 | "type": "number",
208 | "unit": "c",
209 | "acronym": "Inside Temperature"
210 | },
211 | {
212 | "name": "temp_out_c",
213 | "topic": "weatherstation",
214 | "type": "number",
215 | "unit": "c",
216 | "acronym": "Outside Temperature"
217 | }
218 | ]
219 | }
220 |
221 | Accepts JSON message with hierarchy
222 |
223 | Sample mqtt message: {"kodi_details": {"title": "", "fanart": "", "label": "The.Victorias.Secret.Fashion.Show.2015.720p.HDTV.x264.mkv", "type": "unknown", "streamdetails": {"video": [{"stereomode": "", "width": 1280, "codec": "h264", "aspect": 1.7777780294418335, "duration": 2537, "height": 720}], "audio": [{"channels": 6, "codec": "ac3", "language": ""}], "subtitle": [{"language": ""}]}}, "val": ""}
224 |
225 | {
226 | "name": "Kodi media info",
227 | "id": "kodi-media-info",
228 | "class": "MqttSensor",
229 | "attributes": [
230 | {
231 | "name": "kodi_details.label",
232 | "topic": "kodi/status/title",
233 | "type": "string",
234 | "acronym": "label"
235 | },
236 | {
237 | "name": "kodi_details.streamdetails.video.0.codec",
238 | "topic": "kodi/status/title",
239 | "type": "string",
240 | "acronym": "codec"
241 | }
242 | ]
243 | }
244 |
245 | It has the following configuration properties:
246 |
247 | | Property | Default | Type | Description |
248 | |:-----------|:----------|:--------|:--------------------------------------------|
249 | | brokerId | "default" | String | Id of the broker |
250 | | topic | - | String | Topic for device state |
251 | | qos | 0 | Number | The QoS level of the topic and stateTopic (if exist) |
252 | | type | "number" | String | The type of the variable(string or number) |
253 | | unit | - | String | Attribute unit |
254 | | acronym | - | String | Acronym to show as value label in the frontend |
255 | | discrete | false | Boolean | Should be set to true if the value does not change continuously over time. |
256 | | division | - | Number | Constants that will divide the value obtained |
257 | | multiplier | - | Number | Constant that will multiply the value obtained |
258 | | messageMap | - | Object | Even Pimatic 9, you must manually configure this. We're working on it. |
259 |
260 | ### Switch Device
261 |
262 | `MqttSwitch` is based on the PowerSwitch device class.
263 |
264 | {
265 | "name": "MQTT Switch",
266 | "id": "switch",
267 | "class": "MqttSwitch",
268 | "topic": "wemosd1r2/gpio/2/set",
269 | "stateTopic": "wemosd1r2/gpio/2/state"
270 | "onMessage": "1",
271 | "offMessage": "0"
272 | }
273 |
274 | It has the following configuration properties:
275 |
276 | | Property | Default | Type | Description |
277 | |:-----------|:----------|:--------|:--------------------------------------------|
278 | | brokerId | "default" | String | Id of the broker |
279 | | topic | - | String | Topic for device state |
280 | | onMessage | "1" | String | Message to switch on |
281 | | offMessage | "0" | String | Message to switch off |
282 | | stateTopic | - | String | Topic that communicates state, if exists |
283 | | stateValueKey | - | String | The key or path to the state value, given that the payload contains a JSON object |
284 | | qos | 0 | Number | The QoS level of the topic and stateTopic (if exist) |
285 | | retain | false | Boolean | If the published message should have the retain flag on or not. |
286 |
287 |
288 | Device exhibits the following attributes:
289 |
290 | | Property | Unit | Type | Acronym | Description |
291 | |:--------------|:------|:--------|:--------|:---------------------------------------|
292 | | state | - | Boolean | - | Switch State, true is on, false is off |
293 |
294 | The following predicates and actions are supported:
295 |
296 | * {device} is turned on|off
297 | * switch {device} on|off
298 | * toggle {device}
299 |
300 | ### Presence Sensor
301 |
302 | `MqttPresenceSensor` is a digital input device based on the `PresenceSensor` device class.
303 |
304 | {
305 | "name": "MQTT PIR Sensor",
306 | "id": "mqtt-pir-sensor",
307 | "class": "MqttPresenceSensor",
308 | "topic": "wemosd1r2/pir/presence",
309 | "onMessage": "1",
310 | "offMessage": "0"
311 | }
312 |
313 | It has the following configuration properties:
314 |
315 | | Property | Default | Type | Description |
316 | |:-----------|:----------|:--------|:--------------------------------------------|
317 | | brokerId | "default" | String | Id of the broker |
318 | | topic | - | String | Topic for device state |
319 | | stateValueKey | - | String | The key or path to the state value, given that the payload contains a JSON object |
320 | | onMessage | "1" | String | Message that invokes positive status |
321 | | offMessage | "0" | String | Message that invokes negative status |
322 | | qos | 0 | Number | The QoS level of the topic and stateTopic (if exist) |
323 |
324 | The presence sensor exhibits the following attributes:
325 |
326 | | Property | Unit | Type | Acronym | Description |
327 | |:--------------|:------|:--------|:--------|:---------------------------------------|
328 | | presence | - | Boolean | - | Presence State, true is present, false is absent |
329 |
330 | The following predicates are supported:
331 |
332 | * {device} is present|absent
333 |
334 | ### Contact Sensor
335 |
336 | `MqttContactSensor` is a digital input device based on the `ContactSensor` device class.
337 |
338 | {
339 | "name": "MQTT Contact",
340 | "id": "mqtt-contact",
341 | "class": "MqttContactSensor",
342 | "topic": "wemosd1r2/contact/state",
343 | "onMessage": "1",
344 | "offMessage": "0"
345 | }
346 |
347 | It has the following configuration properties:
348 |
349 | | Property | Default | Type | Description |
350 | |:-----------|:----------|:--------|:--------------------------------------------|
351 | | brokerId | "default" | String | Id of the broker |
352 | | topic | - | String | Topic for device state |
353 | | stateValueKey | - | String | The key or path to the state value, given that the payload contains a JSON object |
354 | | onMessage | "1" | String | Message that invokes positive status |
355 | | offMessage | "0" | String | Message that invokes negative status |
356 | | qos | 0 | Number | The QoS level of the topic and stateTopic (if exist) |
357 |
358 | The presence sensor exhibits the following attributes:
359 |
360 | | Property | Unit | Type | Acronym | Description |
361 | |:--------------|:------|:--------|:--------|:---------------------------------------|
362 | | contact | - | Boolean | - | Contact State, true is opened, false is closed |
363 |
364 |
365 | The following predicates are supported:
366 |
367 | * {device} is opened|closed
368 |
369 | ### Dimmer Device
370 |
371 | `MqttDimmer` is based on the Dimmer device class.
372 |
373 | {
374 | "name": "MQTT Dimmer",
375 | "id": "mqtt-dimmer",
376 | "class": "MqttDimmer",
377 | "topic": "wemosd1r2/pcapwm/5/brightness",
378 | "stateTopic": "wemosd1r2/pcapwm/5/state",
379 | "resolution": 4096
380 | },
381 | {
382 | "topic": "dimmer/cmd",
383 | "resolution": 1024,
384 | "id": "dimmer",
385 | "name": "Dimmer",
386 | "class": "MqttDimmer",
387 | "message": "pwm,15,value,2000"
388 | }
389 |
390 | It has the following configuration properties:
391 |
392 | | Property | Default | Type | Description |
393 | |:-----------|:----------|:--------|:--------------------------------------------------|
394 | | brokerId | "default" | String | Id of the broker |
395 | | topic | - | String | Topic for control dimmer brightness. |
396 | | resolution | 256 | Integer | Resolution of this dimmer. For percent set 101. |
397 | | message | "value" | String | Format for outgoing message. |
398 | | stateTopic | - | String | Topic that communicates state, if exists |
399 | | stateValueKey | - | String | The key or path to the state value, given that the payload contains a JSON object |
400 | | qos | 0 | Number | The QoS level of the topic and stateTopic (if exist) |
401 | | retain | false | Boolean | If the published message should have the retain flag on or not. |
402 |
403 | The Dimmer Action Provider:
404 |
405 | * dim [the] device to value%
406 |
407 | ### Buttons Device
408 |
409 | `MqttButtons` is based on the ButtonsDevice device class.
410 |
411 | {
412 | "name": "Buttons",
413 | "id": "buttons-demo",
414 | "class": "MqttButtons",
415 | "buttons": [
416 | {
417 | "id": "button1",
418 | "text": "Press me",
419 | "topic": "some/topic",
420 | "message": "1"
421 | }
422 | ]
423 | }
424 |
425 | It has the following configuration properties for each button:
426 |
427 | | Property | Default | Type | Description |
428 | |:-----------|:----------|:--------|:--------------------------------------------|
429 | | brokerId | "default" | String | Id of the broker |
430 | | id | - | String | Button id |
431 | | text | - | String | Button text |
432 | | topic | - | String | Topic for device state |
433 | | message | - | String | Publish message when pressed |
434 | | stateTopic | - | String | Topic that communicates state, if exists |
435 | | stateValueKey | - | String | The key or path to the state value, given that the payload contains a JSON object |
436 | | qos | 0 | Number | The QoS level of the topic and stateTopic (if exist) |
437 | | confirm | false | Boolean | Ask the user to confirm the button press |
438 |
439 | The Button Action Provider
440 |
441 | * press [the] device
442 |
443 | ## Rules
444 |
445 | You can publish mqtt messages in rules with the action:
446 |
447 | `publish mqtt message "" on topic "" [on broker ListOfBrokers] [qos: 0|1|2] [retain: true|false]`
448 |
449 | "rules": [
450 | {
451 | "id": "my-rule",
452 | "rule": "when every 3 seconds then publish mqtt message \"msg\" on topic \"topic\" on broker default qos: 1 retain: true",
453 | "active": true,
454 | "logging": false,
455 | "name": "Publish mqtt"
456 | }
457 | ]
458 |
459 | You can trigger rules by mqtt messages with the predicate:
460 |
461 | `mqtt received "" on topic "" [via broker ListOfBrokers] [qos: 0|1|2]`
462 |
463 | "rules": [
464 | {
465 | "id": "my-rule-2",
466 | "name": "Receive mqtt",
467 | "rule": "when mqtt received \"1\" on topic \"topic\" via broker default qos: 0 then log \"Yeah!\"",
468 | "active": true,
469 | "logging": true
470 | }
471 | ]
472 |
473 | ## To Do
474 |
475 | 'x' marks done To Do items
476 |
477 | - [ ] Add RGB device
478 | - [x] Reflecting external condition for dimmer
479 | - [x] Reflecting external condition for buttons
480 | - [x] QoS and retain flag
481 | - [x] Processing JSON-encoded object
482 | - [x] Make payload configurable for all device
483 | - [x] Buttons Device
484 | - [x] Configurable PWM range for Dimmer
485 | - [ ] Configurable CIE1931 correction for Dimmer
486 | - [x] Support for more then one Broker
487 | - [ ] Sending all variables from Pimatic to Broker/s
488 | - [ ] Control Pimatic over MQTT :)
489 | - [x] Integration with ActionProvider
490 | - [x] TLS support
491 | - [x] Add shutter device
492 | - [x] Add text and numeric input device
493 | - [x] JSON filtering for state values
494 |
495 | ## Credits
496 |
497 | sweet pi for his work on best automatization software Pimatic and all guys from the pimatic community.
498 |
499 | Andre Miller for for his module pimatic-mqtt-simple from which it comes also part of the code.
500 |
501 | Marcus Wittig for his nice module pimatic-johnny-five which was a big inspiration.
502 |
--------------------------------------------------------------------------------
/assets/screens/screen1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen1.png
--------------------------------------------------------------------------------
/assets/screens/screen1_thumb.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen1_thumb.png
--------------------------------------------------------------------------------
/assets/screens/screen2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen2.png
--------------------------------------------------------------------------------
/assets/screens/screen2_thumb.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen2_thumb.png
--------------------------------------------------------------------------------
/assets/screens/screen3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen3.png
--------------------------------------------------------------------------------
/assets/screens/screen3_thumb.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen3_thumb.png
--------------------------------------------------------------------------------
/assets/screens/screen4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen4.png
--------------------------------------------------------------------------------
/assets/screens/screen4_thumb.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen4_thumb.png
--------------------------------------------------------------------------------
/assets/screens/screen5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen5.png
--------------------------------------------------------------------------------
/assets/screens/screen5_thumb.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wutu/pimatic-mqtt/cd5399fdb810591f8fdcfa237392658040deaf51/assets/screens/screen5_thumb.png
--------------------------------------------------------------------------------
/device-config-schema.coffee:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | title: "pimatic-mqtt device config schemas"
3 | MqttSwitch: {
4 | title: "MqttSwitch config options"
5 | type: "object"
6 | extensions: ["xLink", "xConfirm", "xOnLabel", "xOffLabel"]
7 | properties:
8 | brokerId:
9 | description: "Id of the broker"
10 | type: "string"
11 | default: "default"
12 | topic:
13 | description: "Topic for control switch"
14 | type: "string"
15 | onMessage:
16 | description: "Message to switch on"
17 | type: "string"
18 | default: "1"
19 | offMessage:
20 | description: "Message to switch off"
21 | type: "string"
22 | default: "0"
23 | stateTopic:
24 | description: "Topic that communicates state, if exists"
25 | type: "string"
26 | default: ""
27 | stateValueKey:
28 | description: "The key or path to the state value, given that the payload contains a JSON object"
29 | type: "string"
30 | required: false
31 | qos:
32 | description: "The QoS level of the topic and stateTopic(if exist). Default is 0 and also be used to publishing messages."
33 | type: "number"
34 | default: 0
35 | enum: [0, 1, 2]
36 | retain:
37 | description: "If the published message should have the retain flag on or not."
38 | type: "boolean"
39 | default: false
40 | }
41 | MqttDimmer: {
42 | title: "MqttDimmer config options"
43 | type: "object"
44 | extensions: ["xLink"]
45 | properties:
46 | brokerId:
47 | description: "Id of the broker"
48 | type: "string"
49 | default: "default"
50 | topic:
51 | description: "Topic for control dimmer brightness"
52 | type: "string"
53 | resolution:
54 | description: "Device resolution"
55 | type: "integer"
56 | default: 256
57 | message:
58 | description: "Format for outgoing messages"
59 | type: "string"
60 | default: "value"
61 | stateTopic:
62 | description: "Topic that communicates state, if exists"
63 | type: "string"
64 | default: ""
65 | stateValueKey:
66 | description: "The key or path to the state value, given that the payload contains a JSON object"
67 | type: "string"
68 | required: false
69 | qos:
70 | description: "The QoS level of the topic and stateTopic(if exist). Default is 0 and also be used to publishing messages."
71 | type: "number"
72 | default: 0
73 | enum: [0, 1, 2]
74 | retain:
75 | description: "If the published message should have the retain flag on or not."
76 | type: "boolean"
77 | default: false
78 | }
79 | MqttSensor: {
80 | title: "MqttSensor config options"
81 | type: "object"
82 | extensions: ["xLink", "xAttributeOptions"]
83 | properties:
84 | brokerId:
85 | description: "Id of the broker"
86 | type: "string"
87 | default: "default"
88 | attributes:
89 | description: "Attributes of device"
90 | required: ["name", "topic"]
91 | type: "array"
92 | default: []
93 | format: "table"
94 | items:
95 | type: "object"
96 | properties:
97 | name:
98 | description: "Attribute name"
99 | type: "string"
100 | topic:
101 | description: "Attribute topic"
102 | type: "string"
103 | qos:
104 | description: "The QoS level of the topic and stateTopic(if exist). Default is 0 and also be used to publishing messages."
105 | type: "number"
106 | default: 0
107 | enum: [0, 1, 2]
108 | type:
109 | description: "The type of the variable."
110 | type: "string"
111 | default: "number"
112 | enum: ["string", "number"]
113 | unit:
114 | description: "Attribute unit"
115 | type: "string"
116 | default: ""
117 | acronym:
118 | description: "Acronym to show as value label in the frontend"
119 | type: "string"
120 | default: ""
121 | discrete:
122 | description: "Should be set to true if the value does not change continuously over time."
123 | type: "boolean"
124 | default: false
125 | division:
126 | description: "Constant that will divide the value obtained."
127 | type: "number"
128 | default: ""
129 | multiplier:
130 | description: "Constant that will multiply the value obtained."
131 | type: "number"
132 | default: ""
133 | messageMap:
134 | type: "object"
135 | default: {}
136 | }
137 | MqttPresenceSensor: {
138 | title: "MqttPresenceSensor config options"
139 | type: "object"
140 | extensions: ["xLink", "xPresentLabel", "xAbsentLabel"]
141 | required: ["topic"]
142 | properties:
143 | brokerId:
144 | description: "Id of the broker"
145 | type: "string"
146 | default: "default"
147 | topic:
148 | description: "Device state topic"
149 | type: "string"
150 | stateValueKey:
151 | description: "The key or path to the state value, given that the payload contains a JSON object"
152 | type: "string"
153 | required: false
154 | onMessage:
155 | description: "Message that invokes positive status"
156 | type: "string"
157 | default: "1"
158 | offMessage:
159 | description: "Message that invokes negative status"
160 | type: "string"
161 | default: "0"
162 | qos:
163 | description: "The QoS level of the topic and stateTopic(if exist). Default is 0 and also be used to publishing messages."
164 | type: "number"
165 | default: 0
166 | enum: [0, 1, 2]
167 | autoReset:
168 | description: "Reset the state to absent after resetTime"
169 | type: "boolean"
170 | default: false
171 | resetTime:
172 | description: "Time (in ms) after which the presence value is reset to absent."
173 | type: "integer"
174 | default: 30000
175 | }
176 | MqttContactSensor: {
177 | title: "MqttContactSensor config options"
178 | type: "object"
179 | extensions: ["xLink", "xOpenedLabel", "xClosedLabel"]
180 | required: ["topic"]
181 | properties:
182 | brokerId:
183 | description: "Id of the broker"
184 | type: "string"
185 | default: "default"
186 | topic:
187 | description: "Device state topic"
188 | type: "string"
189 | stateValueKey:
190 | description: "The key or path to the state value, given that the payload contains a JSON object"
191 | type: "string"
192 | required: false
193 | onMessage:
194 | description: "Message that invokes positive status"
195 | type: "string"
196 | default: "1"
197 | offMessage:
198 | description: "Message that invokes negative status"
199 | type: "string"
200 | default: "0"
201 | qos:
202 | description: "The QoS level of the topic and stateTopic(if exist). Default is 0 and also be used to publishing messages."
203 | type: "number"
204 | default: 0
205 | enum: [0, 1, 2]
206 | }
207 | MqttButtons: {
208 | title: "MqttButtons config options"
209 | type: "object"
210 | extensions: ["xLink"]
211 | properties:
212 | brokerId:
213 | description: "Id of the broker"
214 | type: "string"
215 | default: "default"
216 | enableActiveButton:
217 | description: "Highlight last pressed button if enabled"
218 | type: "boolean"
219 | default: true
220 | buttons:
221 | description: "Buttons to display"
222 | type: "array"
223 | default: []
224 | format: "table"
225 | items:
226 | type: "object"
227 | properties:
228 | id:
229 | description: "Button id"
230 | type: "string"
231 | text:
232 | description: "Button text"
233 | type: "string"
234 | topic:
235 | description: "The MQTT topic to publish commands"
236 | type: "string"
237 | message:
238 | description: "Message"
239 | type: "string"
240 | default: "1"
241 | stateTopic:
242 | description: "Topic that communicates state, if exists"
243 | type: "string"
244 | default: ""
245 | stateValueKey:
246 | description: "The key or path to the state value, given that the payload contains a JSON object"
247 | type: "string"
248 | required: false
249 | qos:
250 | description: "The QoS level of the topic and stateTopic(if exist). Default is 0 and also be used to publishing messages."
251 | type: "number"
252 | default: 0
253 | enum: [0, 1, 2]
254 | confirm:
255 | description: "Ask the user to confirm the button press"
256 | type: "boolean"
257 | default: false
258 | }
259 | MqttShutter: {
260 | title: "MqttShutterController config options"
261 | type: "object"
262 | extensions: ["xLink", "xConfirm"]
263 | properties:
264 | brokerId:
265 | description: "Id of the broker"
266 | type: "string"
267 | default: "default"
268 | topic:
269 | description: "Topic for control Shutter"
270 | type: "string"
271 | upMessage:
272 | description: "Custom Up message"
273 | type: "string"
274 | default: "up"
275 | downMessage:
276 | description: "Custom Down message"
277 | type: "string"
278 | default: "down"
279 | stopMessage:
280 | description: "Custom Stop message"
281 | type: "string"
282 | default: "stop"
283 | rollingTime:
284 | description: "Approx. amount of time (in seconds) for shutter to close or open completely."
285 | type: "number"
286 | default: 10
287 | stateTopic:
288 | description: "Topic that communicates state, if exists"
289 | type: "string"
290 | default: ""
291 | stateValueKey:
292 | description: "The key or path to the state value, given that the payload contains a JSON object"
293 | type: "string"
294 | required: false
295 | qos:
296 | description: "The QoS level of the topic and stateTopic(if exist). Default is 0 and also be used to publishing messages."
297 | type: "number"
298 | default: 0
299 | enum: [0, 1, 2]
300 | retain:
301 | description: "If the published message should have the retain flag on or not."
302 | type: "boolean"
303 | default: false
304 | }
305 | MqttInput: {
306 | title: "MQTT InputDevice config"
307 | type: "object"
308 | extensions: ["xLink"]
309 | properties:
310 | brokerId:
311 | description: "Id of the broker"
312 | type: "string"
313 | default: "default"
314 | topic:
315 | description: "Topic for control Shutter"
316 | type: "string"
317 | type:
318 | description: "The type of the input"
319 | type: "string"
320 | default: "string"
321 | enum: ["string", "number"]
322 | min:
323 | description: "Minimum value for numeric values"
324 | type: "number"
325 | required: false
326 | max:
327 | description: "Maximum value for numeric values"
328 | type: "number"
329 | required: false
330 | step:
331 | description: "Step size for minus and plus buttons for numeric values"
332 | type: "number"
333 | default: 1
334 | qos:
335 | description: "The QoS level of the topic and stateTopic(if exist). Default is 0 and also be used to publishing messages."
336 | type: "number"
337 | default: 0
338 | enum: [0, 1, 2]
339 | retain:
340 | description: "If the published message should have the retain flag on or not."
341 | type: "boolean"
342 | default: false
343 | }
344 | }
345 |
--------------------------------------------------------------------------------
/devices/mqtt-buttons.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | assert = env.require 'cassert'
5 | match = require 'mqtt-wildcard'
6 | flatten = require 'flat'
7 |
8 | class MqttButtons extends env.devices.ButtonsDevice
9 |
10 | constructor: (@config, @plugin, lastState) ->
11 | assert(@plugin.brokers[@config.brokerId])
12 |
13 | @name = @config.name
14 | @id = @config.id
15 | super(@config)
16 |
17 | @_lastPressedButton = lastState?.button?.value or null
18 | @mqttclient = @plugin.brokers[@config.brokerId].client
19 |
20 | if @mqttclient.connected
21 | @onConnect()
22 |
23 | @mqttclient.on('connect', =>
24 | @onConnect()
25 | )
26 |
27 | triggerState = (button, value) =>
28 | if value == button.message
29 | @_lastPressedButton = button.id
30 | @emit 'button', button.id
31 |
32 | @mqttclient.on 'message', (topic, message) =>
33 | for b in @config.buttons
34 | if match(topic, b.stateTopic)?
35 | try data = JSON.parse(message.toString()) if b.stateValueKey?
36 | if typeof data is 'object' and Object.keys(data).length != 0
37 | for key, data of flatten(data)
38 | if key == b.stateValueKey
39 | triggerState(b, "#{data}")
40 | found = true
41 | if not found
42 | env.logger.debug "{@name} with id:#{@id}: State topic payload does not contain the given key #{b.stateValueKey}"
43 | else
44 | triggerState(b, message.toString())
45 |
46 |
47 |
48 |
49 | buttonPressed: (buttonId) ->
50 | for b in @config.buttons
51 | if b.id is buttonId
52 | @_lastPressedButton = buttonId
53 | @emit 'button', b.id
54 | @mqttclient.publish(b.topic, b.message, { qos: b.qos or 0 })
55 | return Promise.resolve()
56 | Promise.reject(new Error("No button with the id #{buttonId} found"))
57 |
58 |
59 | onConnect: () ->
60 | for b in @config.buttons
61 | if b.stateTopic
62 | @mqttclient.subscribe(b.stateTopic, { qos: b.qos or 0 })
63 |
64 | destroy: () ->
65 | for b in @config.buttons
66 | if b.stateTopic
67 | @mqttclient.unsubscribe(b.stateTopic)
68 | super()
69 |
--------------------------------------------------------------------------------
/devices/mqtt-contact-sensor.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | assert = env.require 'cassert'
5 | match = require 'mqtt-wildcard'
6 | flatten = require 'flat'
7 |
8 | class MqttContactSensor extends env.devices.ContactSensor
9 |
10 | constructor: (@config, @plugin, lastState) ->
11 | assert(@plugin.brokers[@config.brokerId])
12 |
13 | @id = @config.id
14 | @name = @config.name
15 | @_contact = lastState?.contact?.value or false
16 | @mqttclient = @plugin.brokers[@config.brokerId].client
17 |
18 | if @mqttclient.connected
19 | @onConnect()
20 |
21 | @mqttclient.on('connect', =>
22 | @onConnect()
23 | )
24 |
25 | triggerState = (value) =>
26 | switch value
27 | when @config.onMessage
28 | @_setContact(true)
29 | when @config.offMessage
30 | @_setContact(false)
31 | else
32 | env.logger.debug "#{@name} with id:#{@id}: Message is not in harmony with onMessage or offMessage in config.json or with default values"
33 |
34 | @mqttclient.on('message', (topic, message) =>
35 | if match(topic, @config.topic)?
36 | try data = JSON.parse(message) if @config.stateValueKey?
37 | if typeof data is 'object' and Object.keys(data).length != 0
38 | for key, data of flatten(data)
39 | if key == @config.stateValueKey
40 | triggerState("#{data}")
41 | found = true
42 | if not found
43 | env.logger.debug "{@name} with id:#{@id}: State topic payload does not contain the given key #{@config.stateValueKey}"
44 | else
45 | triggerState(message.toString())
46 |
47 | )
48 |
49 | super()
50 |
51 | onConnect: () ->
52 | @mqttclient.subscribe(@config.topic, { qos: @config.qos })
53 |
54 | getContact: () -> Promise.resolve(@_contact)
55 |
56 | destroy: () ->
57 | @mqttclient.unsubscribe(@config.topic)
58 | super()
59 |
--------------------------------------------------------------------------------
/devices/mqtt-dimmer.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | assert = env.require 'cassert'
5 | match = require 'mqtt-wildcard'
6 | flatten = require 'flat'
7 |
8 | class MqttDimmer extends env.devices.DimmerActuator
9 |
10 | constructor: (@config, @plugin, lastState) ->
11 | assert(@plugin.brokers[@config.brokerId])
12 |
13 | @name = @config.name
14 | @id = @config.id
15 | super()
16 |
17 | @message = @config.message
18 | @_state = lastState?.state?.value or off
19 | @_dimlevel = lastState?.dimlevel?.value or 0
20 | @resolution = (@config.resolution - 1) or 255
21 | @mqttclient = @plugin.brokers[@config.brokerId].client
22 |
23 | if @mqttclient.connected
24 | @onConnect()
25 |
26 | @mqttclient.on('connect', =>
27 | @onConnect()
28 | )
29 |
30 | if @config.stateTopic
31 | triggerDimlevel = (value) =>
32 | payload = parseInt(value, 10);
33 | percentLevel = @getPercentLevel(payload)
34 | if percentLevel != @_dimlevel
35 | if percentLevel <= 100
36 | @_setDimlevel(percentLevel)
37 | else
38 | env.logger.error ("value: #{percentLevel} is out of range")
39 |
40 | @mqttclient.on 'message', (topic, message) =>
41 | if match(topic, @config.stateTopic)?
42 | try data = JSON.parse(message) if @config.stateValueKey?
43 | if typeof data is 'object' and Object.keys(data).length != 0
44 | for key, data of flatten(data)
45 | if key == @config.stateValueKey
46 | triggerDimlevel("#{data}")
47 | found = true
48 | if not found
49 | env.logger.debug "{@name} with id:#{@id}: State topic payload does not contain the given key #{@config.stateValueKey}"
50 | else
51 | triggerDimlevel(message.toString())
52 |
53 | onConnect: () ->
54 | if @config.stateTopic
55 | @mqttclient.subscribe(@config.stateTopic, { qos: @config.qos })
56 |
57 |
58 | # Convert the PWM resolution by config value
59 | # Support for CIE correction will be added latter
60 | getDevLevel: (percentLevel) ->
61 | return (percentLevel * (@resolution / 100)).toFixed(0)
62 |
63 | # Convert device resolution value back to percent value
64 | getPercentLevel: (devLevel) ->
65 | percentLevel = Math.ceil((devLevel * 100) / @resolution)
66 | return parseInt(percentLevel, 10)
67 |
68 | turnOn: ->
69 | level = @getDevLevel(100)
70 | @mqttclient.publish(@config.topic, level, { qos: @config.qos, retain: @config.retain })
71 | @_setDimlevel(100)
72 | return Promise.resolve()
73 |
74 | turnOff: ->
75 | @mqttclient.publish(@config.topic, "0", { qos: @config.qos, retain: @config.retain })
76 | @_setDimlevel(0)
77 | return Promise.resolve()
78 |
79 | changeDimlevelTo: (dimlevel) ->
80 | if @_dimlevel is dimlevel then return Promise.resolve true
81 | level = @getDevLevel(dimlevel)
82 | @payload = @message.replace("value", "#{level}")
83 | @mqttclient.publish(@config.topic, @payload, { qos: @config.qos, retain: @config.retain })
84 | @_setDimlevel(dimlevel)
85 | return Promise.resolve()
86 |
87 | # getDimlevel: -> Promise.resolve(@_dimlevel)
88 |
89 | destroy: () ->
90 | if @config.stateTopic
91 | @mqttclient.unsubscribe(@config.stateTopic)
92 | super()
93 |
--------------------------------------------------------------------------------
/devices/mqtt-input.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | assert = env.require 'cassert'
5 |
6 | class MqttInput extends env.devices.InputDevice
7 |
8 | constructor: (@config, @plugin, lastState) ->
9 | assert(@plugin.brokers[@config.brokerId])
10 |
11 | @id = @config.id
12 | @name = @config.name
13 |
14 | @defaultValue = if @_inputType is "string" then "" else 0
15 | @input = lastState?.input?.value or @defaultValue
16 |
17 | @mqttclient = @plugin.brokers[@config.brokerId].client
18 |
19 | super(@config)
20 |
21 | getInput: () -> Promise.resolve(@input)
22 |
23 | _setInput: (value) ->
24 | unless @input is value
25 | @input = value
26 | @emit 'input', value
27 |
28 | changeInputTo: (value) ->
29 | if @config.type is "number"
30 | if isNaN(value)
31 | throw new Error("Input value is not a number")
32 | else
33 | @mqttclient.publish(@config.topic, value, { qos: @config.qos, retain: @config.retain })
34 | @_setInput(parseFloat(value))
35 | else
36 | @mqttclient.publish(@config.topic, value, { qos: @config.qos, retain: @config.retain })
37 | @_setInput value
38 | return Promise.resolve()
39 |
40 | destroy: ->
41 | super()
42 |
--------------------------------------------------------------------------------
/devices/mqtt-presence-sensor.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | assert = env.require 'cassert'
5 | match = require 'mqtt-wildcard'
6 | flatten = require 'flat'
7 |
8 | class MqttPresenceSensor extends env.devices.PresenceSensor
9 |
10 | constructor: (@config, @plugin, lastState) ->
11 | assert(@plugin.brokers[@config.brokerId])
12 |
13 | @id = @config.id
14 | @name = @config.name
15 | super()
16 |
17 | @_presence = lastState?.presence?.value or false
18 | @mqttclient = @plugin.brokers[@config.brokerId].client
19 |
20 | if @mqttclient.connected
21 | @onConnect()
22 |
23 | if @config.autoReset and @_presence
24 | @_resetPresenceTimeout = setTimeout(@resetPresence, @config.resetTime)
25 |
26 | @mqttclient.on('connect', =>
27 | @onConnect()
28 | )
29 |
30 | triggerState = (value) =>
31 | switch value
32 | when @config.onMessage
33 | @_setPresence(yes)
34 | when @config.offMessage
35 | @_setPresence(no)
36 | else
37 | env.logger.debug "#{@name} with id:#{@id}: Message is not in harmony with onMessage or offMessage in config.json or with default values"
38 |
39 | @mqttclient.on('message', (topic, message) =>
40 | if match(topic, @config.topic)?
41 | if @_resetPresenceTimeout?
42 | clearTimeout(@_resetPresenceTimeout)
43 | @_resetPresenceTimeout = null
44 |
45 | try data = JSON.parse(message) if @config.stateValueKey?
46 | if typeof data is 'object' and Object.keys(data).length != 0
47 | flat = flatten(data)
48 | for key, data of flat
49 | if key == @config.stateValueKey
50 | triggerState("#{data}")
51 | found = true
52 | if not found
53 | env.logger.debug "{@name} with id:#{@id}: State topic payload does not contain the given key #{@config.stateValueKey}"
54 | else
55 | triggerState(message.toString())
56 |
57 | if @config.autoReset and @_presence
58 | @_resetPresenceTimeout = setTimeout(@resetPresence, @config.resetTime)
59 | )
60 |
61 | onConnect: () ->
62 | @mqttclient.subscribe(@config.topic, { qos: @config.qos })
63 |
64 | getPresence: () -> Promise.resolve(@_presence)
65 |
66 | resetPresence: () =>
67 | @_setPresence(no)
68 |
69 | destroy: () ->
70 | @mqttclient.unsubscribe(@config.topic)
71 | super()
72 |
--------------------------------------------------------------------------------
/devices/mqtt-sensor.coffee:
--------------------------------------------------------------------------------
1 |
2 | module.exports = (env) ->
3 |
4 | Promise = env.require 'bluebird'
5 | flatten = require 'flat'
6 | assert = env.require 'cassert'
7 | match = require 'mqtt-wildcard'
8 |
9 | # Original code comes from the module pimatic-mqtt-simple.
10 | # The author is Andre Miller (https://github.com/andremiller).
11 | class MqttSensor extends env.devices.Sensor
12 |
13 | constructor: (@config, @plugin, lastState) ->
14 | assert(@plugin.brokers[@config.brokerId])
15 |
16 | @name = @config.name
17 | @id = @config.id
18 | @attributes = {}
19 | @mqttvars = {}
20 | @mqttclient = @plugin.brokers[@config.brokerId].client
21 |
22 | @attributeValue = {}
23 |
24 | if @mqttclient.connected
25 | @onConnect()
26 |
27 | @mqttclient.on('connect', =>
28 | @onConnect()
29 | )
30 |
31 | @mqttclient.on('message', (topic, message) =>
32 | for attr, i in @config.attributes
33 | do (attr) =>
34 | if match(topic, attr.topic)?
35 | name = attr.name
36 | try data = JSON.parse(message)
37 | if typeof data is 'object' and Object.keys(data).length != 0
38 | flat = flatten(data)
39 | for key, data of flat
40 | if key == name
41 | if attr.type == 'number'
42 | if attr.division
43 | payload = Number("#{data}") / attr.division
44 | @setValue(payload, name)
45 | return
46 | if attr.multiplier
47 | payload = Number("#{data}") * attr.multiplier
48 | @setValue(payload, name)
49 | return
50 | else
51 | payload = Number("#{data}")
52 | @setValue(payload, name)
53 | return
54 | else
55 | payload = ("#{data}")
56 | @setValue(payload, name)
57 | return
58 | else
59 | if attr.type == 'number'
60 | if attr.division
61 | payload = (Number(message) / attr.division)
62 | @setValue(payload, name)
63 | return
64 | if attr.multiplier
65 | payload = (Number(message) * attr.multiplier)
66 | @setValue(payload, name)
67 | return
68 | if attr.messageMap && attr.messageMap[message]
69 | payload = Number(attr.messageMap[message])
70 | @setValue(payload, name)
71 | return
72 | else
73 | payload = Number(message)
74 | @setValue(payload, name)
75 | return
76 | else
77 | if attr.messageMap && attr.messageMap[message]
78 | payload = attr.messageMap[message]
79 | @setValue(payload, name)
80 | return
81 | else
82 | payload = message.toString()
83 | @setValue(payload, name)
84 | return
85 |
86 | )
87 |
88 | for attr, i in @config.attributes
89 | do (attr) =>
90 | name = attr.name
91 | @attributes[name] = {
92 | description: name
93 | }
94 |
95 | @attributes[name].description = name
96 | @attributes[name].type = attr.type or 'number'
97 | @attributes[name].unit = attr.unit or ''
98 | @attributes[name].discrete = attr.discrete or false
99 | @attributes[name].acronym = attr.acronym or null
100 | @attributes[name].division = attr.division or null
101 | @attributes[name].multiplier = attr.multiplier or null
102 |
103 | @mqttvars[name] = lastState?[name]?.value
104 | if not @mqttvars[name]?
105 | switch attr.unit
106 | when 'number' then @mqttvars[name] = 0
107 | when 'boolean' then @mqttvars[name] = false
108 | else @mqttvars[name] = ''
109 |
110 | @_createGetter name, ( => Promise.resolve @mqttvars[name] )
111 |
112 | super()
113 |
114 | setValue: (payload, name) ->
115 | @mqttvars[name] = payload
116 | @emit name, payload
117 |
118 | onConnect: () ->
119 | # Subscribe to the topics
120 | for attr, i in @config.attributes
121 | do (attr) =>
122 | _qos = attr.qos or 0
123 | @mqttclient.subscribe(attr.topic, { qos: _qos })
124 |
125 | destroy: () ->
126 | for attr, i in @config.attributes
127 | do (attr) =>
128 | @mqttclient.unsubscribe(attr.topic)
129 | super()
130 |
--------------------------------------------------------------------------------
/devices/mqtt-shutter.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | assert = env.require 'cassert'
5 | match = require 'mqtt-wildcard'
6 |
7 | class MqttShutter extends env.devices.ShutterController
8 |
9 | constructor: (@config, @plugin, LastState) ->
10 | assert(@plugin.brokers[@config.brokerId])
11 |
12 | @name = @config.name
13 | @id = @config.id
14 | super()
15 |
16 | @_position = lastState?.position?.value or 'stopped'
17 | @mqttclient = @plugin.brokers[@config.brokerId].client
18 |
19 | if @mqttclient.connected
20 | @onConnect()
21 |
22 | @mqttclient.on('connect', =>
23 | @onConnect()
24 | )
25 |
26 | if @config.stateTopic
27 | triggerState = (value) =>
28 | switch value
29 | when @config.upMessage
30 | @_setPosition('up')
31 | when @config.downMessage
32 | @_setPosition('down')
33 | when @config.stopMessage
34 | @_setPosition('stopped')
35 | else
36 | env.logger.debug "#{@name} with id:#{@id} - Message is not in accordance with config."
37 |
38 | @mqttclient.on 'message', (topic, message) =>
39 | if match(topic, @config.stateTopic)?
40 | try data = JSON.parse(message) if @config.stateValueKey?
41 | if typeof data is 'object' and Object.keys(data).length != 0
42 | for key, data of flatten(data)
43 | if key == @config.stateValueKey
44 | triggerState("#{data}")
45 | found = true
46 | if not found
47 | env.logger.debug "{@name} with id:#{@id}: State topic payload does not contain the given key #{@config.stateValueKey}"
48 | else
49 | triggerState(message.toString)
50 |
51 | onConnect: () ->
52 | if @config.stateTopic
53 | @mqttclient.subscribe(@config.stateTopic, { qos: @config.qos })
54 |
55 | moveToPosition: (position) ->
56 | if position is 'up' then payload = @config.upMessage else payload = @config.downMessage
57 | @mqttclient.publish(@config.topic, payload, { qos: @config.qos, retain: @config.retain })
58 | @_setPosition(position)
59 | return Promise.resolve()
60 |
61 | stop: ->
62 | @mqttclient.publish(@config.topic, @config.stopMessage, { qos: @config.qos, retain: @config.retain })
63 | @_setPosition('stopped')
64 | return Promise.resolve()
65 |
66 | destroy: () ->
67 | if @config.stateTopic
68 | @mqttclient.unsubscribe(@config.stateTopic)
69 | super()
70 |
--------------------------------------------------------------------------------
/devices/mqtt-switch.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | assert = env.require 'cassert'
5 | match = require 'mqtt-wildcard'
6 | flatten = require 'flat'
7 |
8 | class MqttSwitch extends env.devices.PowerSwitch
9 |
10 | constructor: (@config, @plugin, lastState) ->
11 | assert(@plugin.brokers[@config.brokerId])
12 |
13 | @name = @config.name
14 | @id = @config.id
15 | super()
16 |
17 | @_state = lastState?.state?.value or off
18 | @mqttclient = @plugin.brokers[@config.brokerId].client
19 |
20 | if @mqttclient.connected
21 | @onConnect()
22 |
23 | @mqttclient.on('connect', =>
24 | @onConnect()
25 | )
26 |
27 | if @config.stateTopic
28 | triggerState = (value) =>
29 | switch value
30 | when @config.onMessage
31 | @_setState(on)
32 | when @config.offMessage
33 | @_setState(off)
34 | else
35 | env.logger.debug "#{@name} with id:#{@id}: Message is not in harmony with onMessage or offMessage in config.json or with default values"
36 |
37 | @mqttclient.on('message', (topic, message) =>
38 | if match(topic, @config.stateTopic)?
39 | try data = JSON.parse(message) if @config.stateValueKey?
40 | if typeof data is 'object' and Object.keys(data).length != 0
41 | for key, data of flatten(data)
42 | if key == @config.stateValueKey
43 | triggerState("#{data}")
44 | found = true
45 | if not found
46 | env.logger.debug "{@name} with id:#{@id}: State topic payload does not contain the given key #{@config.stateValueKey}"
47 | else
48 | triggerState(message.toString())
49 | )
50 |
51 | onConnect: () ->
52 | if @config.stateTopic
53 | @mqttclient.subscribe(@config.stateTopic, { qos: @config.qos })
54 |
55 | changeStateTo: (state) ->
56 | message = (if state then @config.onMessage else @config.offMessage)
57 | @mqttclient.publish(@config.topic, message, { qos: @config.qos, retain: @config.retain })
58 | @_setState(state)
59 | return Promise.resolve()
60 |
61 | destroy: () ->
62 | if @config.stateTopic
63 | @mqttclient.unsubscribe(@config.stateTopic)
64 | super()
65 |
--------------------------------------------------------------------------------
/mqtt-config-schema.coffee:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | title: "MQTT plugin config options"
3 | type: "object"
4 | properties:
5 | debug:
6 | description: "Debug mode. Writes debug messages to the pimatic log, if set to true."
7 | type: "boolean"
8 | default: false
9 | brokers:
10 | description: "List of MQTT brokers"
11 | type: "array"
12 | format: "table"
13 | items:
14 | type: "object"
15 | properties:
16 | brokerId:
17 | description: "The brokerId of the MQTT broker which can be set for each device. Use 'default' for default Broker"
18 | type: "string"
19 | default: "default"
20 | host:
21 | description: "The IP or hostname of the MQTT broker (Default: 127.0.0.1)"
22 | type: "string"
23 | default: "127.0.0.1"
24 | port:
25 | description: "The port of the MQTT broker (Default: 1883)"
26 | type: "integer"
27 | default: 1883
28 | keepalive:
29 | description: "keepalive in seconds"
30 | type: "integer"
31 | default: 180
32 | clientId:
33 | description: "Client Id"
34 | type: "string"
35 | default: ""
36 | protocolId:
37 | description: "MQTT protocol ID"
38 | type: "string"
39 | default: "MQTT"
40 | protocolVer:
41 | description: "MQTT protocol version"
42 | type: "integer"
43 | default: 4
44 | cleanSession:
45 | description: "Set to false to receive QoS 1 and 2 messages while offline"
46 | type: "boolean"
47 | default: true
48 | reconnect:
49 | description: "reconnectPeriod in milliseconds"
50 | type: "integer"
51 | default: 10000
52 | timeout:
53 | description: "connectTimeout in milliseconds"
54 | type: "integer"
55 | default: 30000
56 | username:
57 | description: "The login name"
58 | type: "string"
59 | default: ""
60 | password:
61 | description: "The password"
62 | type: "string"
63 | default: ""
64 | queueQoSZero:
65 | description: "If connection is broken, queue outgoing QoS zero messages"
66 | type: "boolean"
67 | default: true
68 | certPath:
69 | description: "Path to the certificate of the client in PEM format, required for TLS connection"
70 | type: "string"
71 | default: ""
72 | keyPath:
73 | description: "Path to the key of the client in PEM format, required for TLS connection"
74 | type: "string"
75 | default: ""
76 | rejectUnauthorized:
77 | description: "Whether to reject self signed certificates"
78 | type: "boolean"
79 | default: true
80 | ca:
81 | description: "Path to the trusted CA list"
82 | type: "string"
83 | default: ""
84 | ssl:
85 | description: "Force the use of TLS/SSL"
86 | type: "boolean"
87 | default: false
88 | }
89 |
--------------------------------------------------------------------------------
/mqtt.coffee:
--------------------------------------------------------------------------------
1 | # Pimatic MQTT plugin
2 | module.exports = (env) ->
3 |
4 | mqtt = require 'mqtt'
5 | match = require 'mqtt-wildcard'
6 | Promise = env.require 'bluebird'
7 | pluginConfigDef = require './mqtt-config-schema'
8 | configProperties = pluginConfigDef.properties.brokers.items.properties
9 |
10 | deviceTypes = {}
11 | for device in [
12 | 'mqtt-switch'
13 | 'mqtt-dimmer'
14 | 'mqtt-sensor'
15 | 'mqtt-presence-sensor'
16 | 'mqtt-contact-sensor'
17 | 'mqtt-buttons'
18 | 'mqtt-shutter'
19 | 'mqtt-input'
20 | ]
21 | # convert kebap-case to camel-case notation with first character capitalized
22 | className = device.replace /(^[a-z])|(\-[a-z])/g, ($1) -> $1.toUpperCase().replace('-','')
23 | deviceTypes[className] = require('./devices/' + device)(env)
24 |
25 | # import predicates and actions
26 | MqttActionProvider = require('./predicates_and_actions/mqtt_action')(env)
27 | MqttPredicateProvider = require('./predicates_and_actions/mqtt_predicate')(env)
28 |
29 | # Pimatic MQTT Plugin class
30 | class MqttPlugin extends env.plugins.Plugin
31 |
32 | # transfer config - from single Broker to multiple Brokers
33 | prepareConfig: (config) =>
34 | try
35 | if not config.brokers?
36 | keys = Object.keys configProperties
37 | broker = {}
38 | keys.forEach (key) =>
39 | if config[key]?
40 | broker[key] = config[key]
41 | delete config[key]
42 | config.brokers = []
43 | broker["brokerId"] = "default"
44 | config.brokers.push broker
45 | catch error
46 | env.logger.error "Unable to migrate config: " + error
47 |
48 | init: (app, @framework, @config) =>
49 |
50 | @brokers = { }
51 |
52 | for brokerConfig in @config.brokers
53 | broker = {
54 | id: brokerConfig.brokerId ? configProperties.brokerId.default
55 | client: null
56 | }
57 |
58 | options = (
59 | host: brokerConfig.host
60 | port: brokerConfig.port
61 | keepalive: brokerConfig.keepalive
62 | clientId: brokerConfig.clientId or 'pimatic_' + Math.random().toString(16).substr(2, 8)
63 | protocolId: brokerConfig.protocolId
64 | protocolVersion: brokerConfig.protocolVer
65 | clean: brokerConfig.cleanSession
66 | reconnectPeriod: brokerConfig.reconnect or 10000
67 | connectTimeout: brokerConfig.timeout
68 | queueQoSZero: brokerConfig.queueQoSZero
69 | certPath: brokerConfig.certPath
70 | keyPath: brokerConfig.keyPath
71 | rejectUnauthorized: brokerConfig.rejectUnauthorized
72 | ca: brokerConfig.ca
73 | debug: @config.debug
74 | )
75 | if brokerConfig.username? and brokerConfig.username isnt ""
76 | options.username = brokerConfig.username
77 | options.password = if brokerConfig.password then new Buffer(brokerConfig.password) else false
78 |
79 | if brokerConfig.ca or brokerConfig.certPath or brokerConfig.keyPath or brokerConfig.ssl
80 | options.protocol = 'mqtts'
81 |
82 | mqttClient = null
83 |
84 | Connection = new Promise( (resolve, reject) =>
85 | mqttClient = new mqtt.connect(options)
86 | id = broker.id
87 | mqttClient.on("connect", () =>
88 | resolve()
89 | )
90 | mqttClient.on('error', reject)
91 |
92 | broker.client = mqttClient
93 |
94 | mqttClient.on "connect", () =>
95 | env.logger.info "Successfully connected to MQTT Broker #{id}"
96 |
97 | mqttClient.on 'reconnect', () =>
98 | env.logger.info "Reconnecting to MQTT Broker #{id}"
99 |
100 | mqttClient.on 'offline', () =>
101 | env.logger.info "MQTT Broker #{id} is offline"
102 |
103 | mqttClient.on 'error', (error) ->
104 | env.logger.error "Broker #{id} #{error}"
105 | env.logger.debug error.stack
106 |
107 | mqttClient.on 'close', () ->
108 | env.logger.info "Connection with MQTT Broker #{id} was closed"
109 | )
110 |
111 | @brokers[broker.id] = broker
112 | env.logger.debug(broker)
113 |
114 |
115 | # register devices
116 | deviceConfigDef = require("./device-config-schema")
117 |
118 | for className, classType of deviceTypes
119 | env.logger.debug "Registering device class #{className}"
120 | @framework.deviceManager.registerDeviceClass(className, {
121 | configDef: deviceConfigDef[className],
122 | createCallback: @callbackHandler(className, classType)
123 | })
124 |
125 | @framework.ruleManager.addActionProvider(new MqttActionProvider(@framework, @))
126 | @framework.ruleManager.addPredicateProvider(new MqttPredicateProvider(@framework, @))
127 |
128 | @framework.deviceManager.on('discover', (eventData) =>
129 | @framework.deviceManager.discoverMessage 'pimatic-mqtt', 'Searching for devices'
130 | _seen = {}
131 | seen = (topic, deviceType) ->
132 | if _seen[topic]?
133 | if _seen[topic].hasOwnProperty deviceType
134 | return true
135 | else
136 | _seen[topic] = {}
137 | _seen[topic][deviceType] = ''
138 | return false
139 |
140 | for id, broker of @brokers
141 | client = broker.client
142 |
143 | onConnect = () =>
144 | client.subscribe('stat/+/RESULT', { qos: 0 })
145 |
146 | client.on('message', (topic, message) =>
147 | env.logger.debug "New message", topic, message.toString()
148 | if match(topic, 'stat/+/RESULT')?
149 | try data = JSON.parse(message)
150 | if typeof data is 'object' and Object.keys(data).length != 0
151 | if data.Dimmer? and not seen(topic, 'Dimmer')
152 | device =
153 | class: 'MqttDimmer'
154 | name: 'MqttDimmer ' + topic
155 | brokerId: broker.id
156 | stateTopic: topic
157 | stateValueKey: 'Dimmer'
158 | topic: "cmnd/#{topic.match(/stat\/(\w+)\/RESULT/)?[1]}/DIMMER"
159 | resolution: 100
160 |
161 | process.nextTick(
162 | @_discoveryCallbackHandler('pimatic-mqtt', device.name, device)
163 | )
164 |
165 | if data.POWER? and not seen(topic, 'POWER')
166 | device =
167 | class: 'MqttSwitch'
168 | name: 'MqttSwitch ' + topic
169 | brokerId: broker.id
170 | stateTopic: topic
171 | stateValueKey: 'POWER'
172 | topic: "cmnd/#{topic.match(/stat\/(\w+)\/RESULT/)?[1]}/POWER"
173 | onMessage: 'ON'
174 | offMessage: 'OFF'
175 |
176 | process.nextTick(
177 | @_discoveryCallbackHandler('pimatic-mqtt', device.name, device)
178 | )
179 | )
180 |
181 | if client.connected
182 | onConnect()
183 | else
184 | @mqttclient.on('connect', => onConnect())
185 |
186 | setTimeout ->
187 | client.unsubscribe('stat/+/RESULT')
188 | , eventData.time
189 | )
190 |
191 | callbackHandler: (className, classType) ->
192 | # this closure is required to keep the className and classType context as part of the iteration
193 | return (config, lastState) =>
194 | return new classType(config, @, lastState)
195 |
196 | _discoveryCallbackHandler: (pluginName, deviceName, deviceConfig) ->
197 | return () =>
198 | @framework.deviceManager.discoveredDevice pluginName, deviceName, deviceConfig
199 |
200 | # ###Finally
201 | # Create a instance of my plugin
202 | # and return it to the framework.
203 | return new MqttPlugin
204 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "pimatic-mqtt",
3 | "description": "MQTT support for Pimatic",
4 | "author_name": "Marek Kail",
5 | "author": "Marek Kail",
6 | "author_url": "https://github.com/wutu",
7 | "npmUser": "wutu",
8 | "main": "mqtt",
9 | "files": [
10 | "mqtt.coffee",
11 | "mqtt-config-schema.coffee",
12 | "device-config-schema.coffee",
13 | "devices",
14 | "assets",
15 | "predicates_and_actions",
16 | "README.md",
17 | "LICENSE"
18 | ],
19 | "version": "0.9.13",
20 | "homepage": "http://github.com/wutu/pimatic-mqtt",
21 | "repository": {
22 | "type": "git",
23 | "url": "git://github.com/wutu/pimatic-mqtt.git"
24 | },
25 | "configSchema": "mqtt-config-schema.coffee",
26 | "peerDependencies": {
27 | "pimatic": ">=0.9.0 <1.0.0"
28 | },
29 | "dependencies": {
30 | "flat": ">=2.0.0",
31 | "mqtt": ">=2.0.0",
32 | "mqtt-wildcard": "^3.0.9"
33 | },
34 | "engines": {
35 | "node": ">= 4",
36 | "npm": ">= 2"
37 | },
38 | "bugs": {
39 | "url": "https://github.com/wutu/pimatic-mqtt/issues"
40 | },
41 | "contributors": [
42 | {
43 | "name": "kcsoft",
44 | "url": "https://github.com/kcsoft"
45 | },
46 | {
47 | "name": "Marcus Wittig",
48 | "url": "https://github.com/mwitig"
49 | },
50 | {
51 | "name": "Jonathan Foucher",
52 | "url": "https://github.com/jfoucher"
53 | }
54 | ],
55 | "keywords": [
56 | "pimatic",
57 | "mqtt",
58 | "subscribe",
59 | "publish",
60 | "publish/subscribe",
61 | "IoT"
62 | ],
63 | "license": "AGPL-3.0",
64 | "maintainers": [
65 | {
66 | "name": "wutu",
67 | "url": "https://github.com/wutu"
68 | }
69 | ]
70 | }
71 |
--------------------------------------------------------------------------------
/predicates_and_actions/mqtt_action.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | M = env.matcher
5 |
6 | class MqttActionHandler extends env.actions.ActionHandler
7 |
8 | constructor: (@framework, @plugin, @stringTopic, @stringBrokerId, @stringMessage, @stringQoS, @stringRetain) ->
9 |
10 | executeAction: (simulate) ->
11 | @framework.variableManager.evaluateStringExpression(@stringTopic).then( (strTopic) =>
12 | @framework.variableManager.evaluateStringExpression(@stringBrokerId).then( (strBrokerId) =>
13 | @framework.variableManager.evaluateStringExpression(@stringMessage).then( (strMessage) =>
14 | @framework.variableManager.evaluateExpression(@stringQoS).then( (strQoS) =>
15 | @framework.variableManager.evaluateExpression(@stringRetain).then( (strRet) =>
16 | if simulate
17 | return Promise.resolve("publish mqtt message " + strMessage + " on topic " + strTopic + " on broker " + strBrokerId + " qos: " + strQoS + " retain: " + strRet)
18 | else
19 | retFlag = if strRet is "true" then true else false
20 | numQoS = Number(strQoS)
21 | @plugin.brokers[strBrokerId].client.publish(strTopic, strMessage, { qos: numQoS, retain: retFlag})
22 | return Promise.resolve("publish mqtt message " + strMessage + " on topic " + strTopic + " on broker " + strBrokerId + " qos: " + strQoS + " retain: " + strRet)
23 | )
24 | )
25 | )
26 | )
27 | )
28 |
29 | # action provider for publishing mqtt messages
30 | class MqttActionProvider extends env.actions.ActionProvider
31 |
32 | constructor: (@framework, @plugin) ->
33 |
34 | parseAction: (input, context) ->
35 |
36 | brokersId = []
37 | for id of @plugin.brokers
38 | brokersId.push id
39 |
40 | strToTokens = (str) => ["\"#{str}\""]
41 |
42 | stringMessage = null
43 | stringTopic = null
44 | stringQoS = strToTokens '0'
45 | stringRetain = strToTokens "false"
46 | stringBrokerId = strToTokens "default"
47 | match = null
48 |
49 | setMessageString = (m, tokens) => stringMessage = tokens
50 | setTopicString = (m, tokens) => stringTopic = tokens
51 | setBrokerIdString = (m, tokens) => stringBrokerId = tokens
52 |
53 | m = M(input, context)
54 | .match('publish mqtt message ').matchStringWithVars(setMessageString)
55 | .match(' on topic ').matchStringWithVars(setTopicString)
56 |
57 | next = m.match(' on broker ').match(brokersId, (next, b) =>
58 | stringBrokerId = strToTokens(b)
59 | if next.hadMatch() then m = next
60 | )
61 |
62 | next = m.match(' qos: ').match(['0','1','2'], (next, q) =>
63 | stringQoS = strToTokens(q)
64 | if next.hadMatch() then m = next
65 | )
66 |
67 | next = m.match(' retain: ').match(['false','true'], (next, r) =>
68 | stringRetain = strToTokens(r)
69 | if next.hadMatch() then m = next
70 | )
71 |
72 | if m.hadMatch()
73 | match = m.getFullMatch()
74 | return {
75 | token: match
76 | nextInput: input.substring(match.length)
77 | actionHandler: new MqttActionHandler(
78 | @framework, @plugin, stringTopic, stringBrokerId, stringMessage, stringQoS, stringRetain
79 | )
80 | }
81 |
82 | return MqttActionProvider
83 |
--------------------------------------------------------------------------------
/predicates_and_actions/mqtt_predicate.coffee:
--------------------------------------------------------------------------------
1 | module.exports = (env) ->
2 |
3 | Promise = env.require 'bluebird'
4 | M = env.matcher
5 |
6 | class MqttPredicateProvider extends env.predicates.PredicateProvider
7 | constructor: (@framework, @plugin) ->
8 |
9 | parsePredicate: (input, context) ->
10 |
11 | brokersId = []
12 | for id of @plugin.brokers
13 | brokersId.push id
14 |
15 | message = null
16 | topic = null
17 | brokerId = "default"
18 | qos = "0"
19 |
20 | m = M(input, context)
21 | .match("mqtt received ")
22 | .matchString( (m,expr) =>
23 | message = expr
24 | )
25 | .match(" on topic ")
26 | .matchString( (m,expr) =>
27 | topic = expr
28 | )
29 | .optional( (m) =>
30 | next = m
31 | m.match(" via broker ")
32 | .match(brokersId , (m, expr) =>
33 | brokerId = expr
34 | )
35 | )
36 | .optional( (m) =>
37 | next = m
38 | m.match(" qos: ")
39 | .match(["0","1","2"], (m, expr) =>
40 | next = m
41 | qos = parseInt expr
42 | )
43 | return next
44 | )
45 |
46 | if m.hadMatch()
47 | match = m.getFullMatch()
48 | return {
49 | token: match
50 | nextInput: input.substring(match.length)
51 | predicateHandler: new MqttPredicateHandler(@framework, @plugin, brokerId, topic, message, qos)
52 | }
53 | else
54 | return null
55 |
56 | class MqttPredicateHandler extends env.predicates.PredicateHandler
57 | constructor: (@framework, @plugin, @brokerId, @topic, @message, @qos) ->
58 | @mqttclient = @plugin.brokers[@brokerId].client
59 | super()
60 |
61 | setup: ->
62 | env.logger.debug "PredicateHandler", @brokerId, @topic, @message
63 |
64 | # handle received messages
65 | @mqttclient.on('message', @recvListener = (topic, message) =>
66 | message = message.toString()
67 | # check topic
68 | return if topic isnt @topic
69 |
70 | # check message
71 | return if message isnt @message and @message isnt "*"
72 |
73 | @emit 'change', 'event'
74 | )
75 |
76 | @mqttclient.subscribe @topic, { qos: @qos }
77 |
78 | super()
79 |
80 | destroy: ->
81 | @mqttclient.removeListener 'received', @recvListener
82 | @mqttclient.unsubscribe @topic
83 | super()
84 |
85 | getValue: -> Promise.resolve false
86 | getType: -> 'event'
87 |
88 | return MqttPredicateProvider
89 |
--------------------------------------------------------------------------------