diff --git a/LICENSE.txt b/LICENSE.txt index 3017ed3..f288702 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,21 +1,674 @@ -MIT License - -Copyright (c) 2018 Thomas Wilson - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/setup.py b/setup.py index d984817..7f15ca6 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='shepherd', - version='0.2dev', + version='0.3dev', author='Thomas Wilson', author_email='t.wilson@distreon.com.au', packages=['shepherd', ], @@ -15,7 +15,7 @@ setup( entry_points={ 'console_scripts': ['shepherd=shepherd.core:cli'], }, - license='MIT license', + license='GPLv3+', description='Herd your mob of physically remote nodes', long_description=open('README.md').read(), ) diff --git a/shepherd.toml b/shepherd.toml deleted file mode 100644 index ca263f0..0000000 --- a/shepherd.toml +++ /dev/null @@ -1,10 +0,0 @@ -[shepherd] - plugin_path = "~/shepherd/" - plugins = ["picam","test"] - root_dir = "~/shepherd/" - conf_edit_path = "~/shepherd.toml" - test =1 -[picam] - [[picam.trigger]] - hour = "00-23" - minute = "*" \ No newline at end of file diff --git a/shepherd/camtest.py b/shepherd/camtest.py deleted file mode 100644 index fad1fbb..0000000 --- a/shepherd/camtest.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env python3 - -import cv2 -from PIL import Image, ImageDraw, ImageFont - -print(cv2.__version__) - -gst_str = ('v4l2src device=/dev/video0 ! ' - 'videoconvert ! appsink drop=true max-buffers=1 sync=false') -print(gst_str) - -logo_im = Image.open('smallshepherd.png') - -overlayfont = "DejaVuSansMono.ttf" - -try: - fnt = ImageFont.truetype(overlayfont, 50) -except IOError: - fnt = ImageFont.load_default() - -loaded_fonts={} -loaded_logos={} - -vidcap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) -while True: - breakpoint() - vidcap.grab() - read_flag, frame = vidcap.read() - print(read_flag) - #overlay = frame.copy() - # You may need to convert the color. - - #Convert over to PIL. Mostly so we can use our own font. - img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - im_pil = Image.fromarray(img) - - font_size = int(im_pil.height/40) - margin_size = int(font_size/5) - - if font_size not in loaded_fonts: - loaded_fonts[font_size] = ImageFont.truetype(overlayfont, int(font_size*0.9)) - - thisfont = loaded_fonts[font_size] - - if font_size not in loaded_logos: - newsize = (int(logo_im.width*(font_size/logo_im.height)),font_size) - loaded_logos[font_size] = logo_im.resize(newsize, Image.BILINEAR) - - thislogo = loaded_logos[font_size] - - - overlay = Image.new('RGBA',(im_pil.width,font_size+(2*margin_size)), (0,0,0)) - - overlay.paste(thislogo, (int((overlay.width-thislogo.width)/2),margin_size)) - - draw = ImageDraw.Draw(overlay) - draw.text((margin_size*2, margin_size), "SARDIcam-1", font=thisfont, - fill=(255, 255, 255, 255)) - - datetext = "2019-07-24 20:22:31" - datewidth, _ = draw.textsize(datetext,thisfont) - draw.text((overlay.width-(margin_size*2)-datewidth, margin_size), datetext, font=thisfont, - fill=(255, 255, 255, 255)) - - overlay.putalpha(128) - - im_pil.paste(overlay, (0,im_pil.height-overlay.height),overlay) - im_pil.save("test.jpg", "JPEG", quality=90) - - - - # For reversing the operation: - #im_np = np.asarray(im_pil) - - #cv2.rectangle(overlay,(200,200),(500,500),(255,0,0),-1) - #cv2.addWeighted(overlay, 0.3, frame, 0.7, 0, frame) - #cv2.imwrite("frame.jpg", frame) - -# print out build properties: -# print(cv2.getBuildInformation()) \ No newline at end of file diff --git a/shepherd/freezedry.py b/shepherd/freezedry.py deleted file mode 100644 index 89aaa35..0000000 --- a/shepherd/freezedry.py +++ /dev/null @@ -1,114 +0,0 @@ -from enum import Enum, auto -import inspect - -class RehydrateMethod(Enum): - DIRECT = auto() - INIT = auto() - CLASS_METHOD = auto() - -#freezedry, for when pickling is just a bit too intense - -# The class key is a reserved dict key used to flag that the dict should be unpacked back out to a class instance -class_key = "" -# The Pack module stores some state from init to keep a list of valid packable classes -freezedryables = {} - -# Decorator to mark class as packable and keep track of associated names and classes. When packed, the -# special key string "" indicates what class the current dict should be unpacked to - -# name argument is the string that will identify this class in a packed dict -def freezedryable(cls, rehydrate_method=RehydrateMethod.DIRECT, name=None): - if name is None: - cls._freezedry_name = cls.__name__ - else: - if isinstance(name, str): - raise Exception("freezedryable name must be a string") - cls._freezedry_name = name - cls._rehydrate_method = rehydrate_method - - if cls._freezedry_name in freezedryables: - raise Exception("Duplicate freezedryable class name "+cls._freezedry_name) - freezedryables[cls._freezedry_name] = cls - - def _freezedry(self): - dried_dict=_freezedry_dict(vars(self)) - dried_dict[class_key]=self._freezedry_name - return dried_dict - - cls.freezedry=_freezedry - #setattr(cls, "freezedry", freezedry) - return cls - - -def freezedry(hydrated_obj): - # If it's a primitive, store it. If it's a dict or list, recursively freezedry that. - # If it's an instance of another freezedryable class, call its .freezedry() method. - if isinstance(hydrated_obj, (str, int, float, bool, type(None))): - return hydrated_obj - elif isinstance(hydrated_obj, dict): - return _freezedry_dict(hydrated_obj) - elif isinstance(hydrated_obj, list): - dried_list = [] - for val in hydrated_obj: - dried_list.append(freezedry(val)) - return dried_list - elif hasattr(hydrated_obj, "_freezedry_name"): - return hydrated_obj.freezedry() - else: - raise Exception("Object "+str(hydrated_obj)+" is not freezedryable") - -def _freezedry_dict(hydrated_dict): - dried_dict = {} - for k,val in hydrated_dict.items(): - if not isinstance(k,str): - raise Exception("Non-string dictionary keys are not freezedryable") - if k == class_key: - raise Exception("Key "+class_key+" is reserved for internal freezedry use") - dried_dict[k]=freezedry(val) - return dried_dict - -def rehydrate(dried_obj): - if isinstance(dried_obj, (str, int, float, bool, type(None))): - return dried_obj - elif isinstance(dried_obj, dict): - return _rehydrate_dict(dried_obj) - elif isinstance(dried_obj, list): - hydrated_list = [] - for val in dried_obj: - hydrated_list.append(rehydrate(val)) - return hydrated_list - else: - raise Exception("Object "+str(dried_obj)+" is not rehydrateable") - -def _rehydrate_dict(dried_dict): - hydrated_dict = {} - for k,val in dried_dict.items(): - if not isinstance(k,str): - raise Exception("Non-string dictionary keys are not rehydrateable") - if k != class_key: - hydrated_dict[k]=rehydrate(val) - - # Check if this is an object that needs to be unpacked back to an instance - if class_key in dried_dict: - if dried_dict[class_key] not in freezedryables: - raise Exception("Class "+dried_dict[class_key]+" has not been decorated as freezedryable") - f_class=freezedryables[dried_dict[class_key]] - # If DIRECT, skip __init__ and set attributes back directly - if f_class._rehydrate_method == RehydrateMethod.DIRECT: - hydrated_instance = f_class.__new__(f_class) - hydrated_instance.__dict__.update(hydrated_dict) - #if INIT, pass all attributes as keywords to __init__ method - elif f_class._rehydrate_method == RehydrateMethod.INIT: - hydrated_instance = f_class(**hydrated_dict) - # IF CLASS_METHOD, pass all attributes as keyword aguments to classmethod "unpack()" - elif f_class._rehydrate_method == RehydrateMethod.CLASS_METHOD: - if inspect.ismethod(getattr(f_class, "rehydrate", None)): - hydrated_instance = f_class.rehydrate(**hydrated_dict) - else: - raise Exception("Class "+str(f_class)+" does not have classmethod 'rehydrate()'") - else: - raise Exception("Class _rehydrate_method "+str(f_class._rehydrate_method)+" is not supported") - - return hydrated_instance - else: - return hydrated_dict \ No newline at end of file diff --git a/shepherd/plugins/DejaVuSansMono.ttf b/shepherd/plugins/DejaVuSansMono.ttf deleted file mode 100644 index f578602..0000000 Binary files a/shepherd/plugins/DejaVuSansMono.ttf and /dev/null differ diff --git a/shepherd/plugins/aphidtrap.py b/shepherd/plugins/aphidtrap.py deleted file mode 100644 index 48179c1..0000000 --- a/shepherd/plugins/aphidtrap.py +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env python3 - -import shepherd.config -import shepherd.module - -import sys -import os -import time -import argparse - -from gpiozero import OutputDevice, Device -from gpiozero.pins.pigpio import PiGPIOFactory - -from shepherd.modules.betterservo import BetterServo - -Device.pin_factory = PiGPIOFactory() - - -APHIDTRAP_LED_PIN = 5 #Out2 - - -class AphidtrapConfDef(shepherd.config.ConfDefinition): - def __init__(self): - super().__init__() - - -class AphidtrapModule(shepherd.module.SimpleModule): - conf_def = AphidtrapConfDef() - - def setup(self): - - print("Aphidtrap config:") - print(self.config) - - self.led_power = OutputDevice(APHIDTRAP_LED_PIN, - active_high=True, - initial_value=False) - - def setup_other_modules(self): - self.modules.picam.hook_pre_cam.attach(self.led_on) - self.modules.picam.hook_post_cam.attach(self.led_off) - - def led_on(self): - self.led_power.on() - - def led_off(self): - self.led_power.off() - - - -def main(argv): - argparser = argparse.ArgumentParser( - description='Module for aphidtrap control functions. Run for testing') - argparser.add_argument("configfile", nargs='?', metavar="configfile", - help="Path to configfile", default="conf.toml") - - - args = argparser.parse_args() - confman = shepherd.config.ConfigManager() - - srcdict = {"aphidtrap": {}} - - if os.path.isfile(args.configfile): - confman.load(args.configfile) - else: - confman.load(srcdict) - - aphidtrap_mod = AphidtrapModule(confman.get_config("aphidtrap", AphidtrapConfDef()), - shepherd.module.Interface(None)) - - aphidtrap_mod.led_on() - time.sleep(2) - aphidtrap_mod.led_off() - - -if __name__ == "__main__": - main(sys.argv[1:]) diff --git a/shepherd/plugins/betterservo.py b/shepherd/plugins/betterservo.py deleted file mode 100644 index b4bdbc4..0000000 --- a/shepherd/plugins/betterservo.py +++ /dev/null @@ -1,147 +0,0 @@ -from gpiozero import PWMOutputDevice, SourceMixin, CompositeDevice - - -class BetterServo(SourceMixin, CompositeDevice): - """ - Copy of GPIOZero servo, but with control over pulse width and active_high - """ - def __init__( - self, pin=None, initial_value=0.0, - min_pulse_width=1/1000, max_pulse_width=2/1000, - frame_width=20/1000, pin_factory=None, active_high=True): - if min_pulse_width >= max_pulse_width: - raise ValueError('min_pulse_width must be less than max_pulse_width') - if max_pulse_width >= frame_width: - raise ValueError('max_pulse_width must be less than frame_width') - self._frame_width = frame_width - self._min_dc = min_pulse_width / frame_width - self._dc_range = (max_pulse_width - min_pulse_width) / frame_width - self._min_value = -1 - self._value_range = 2 - super(BetterServo, self).__init__( - pwm_device=PWMOutputDevice( - pin, frequency=int(1 / frame_width), pin_factory=pin_factory, - active_high=False - ), - pin_factory=pin_factory - ) - self.pwm_device.active_high=active_high - try: - self.value = initial_value - except: - self.close() - raise - - @property - def frame_width(self): - """ - The time between control pulses, measured in seconds. - """ - return self._frame_width - - @property - def min_pulse_width(self): - """ - The control pulse width corresponding to the servo's minimum position, - measured in seconds. - """ - return self._min_dc * self.frame_width - - @property - def max_pulse_width(self): - """ - The control pulse width corresponding to the servo's maximum position, - measured in seconds. - """ - return (self._dc_range * self.frame_width) + self.min_pulse_width - - @property - def pulse_width(self): - """ - Returns the current pulse width controlling the servo. - """ - if self.pwm_device.frequency is None: - return None - else: - return self.pwm_device.state * self.frame_width - - @pulse_width.setter - def pulse_width(self, value): - if value is None: - self.pwm_device.frequency = None - elif self.min_pulse_width <= value <= self.max_pulse_width: - self.pwm_device.frequency = int(1 / self.frame_width) - self.pwm_device.value = (value / self.frame_width) - else: - raise OutputDeviceBadValue("Servo pulse_width must be between min and max supplied during construction, or None") - - def min(self): - """ - Set the servo to its minimum position. - """ - self.value = -1 - - def mid(self): - """ - Set the servo to its mid-point position. - """ - self.value = 0 - - def max(self): - """ - Set the servo to its maximum position. - """ - self.value = 1 - - def detach(self): - """ - Temporarily disable control of the servo. This is equivalent to - setting :attr:`value` to ``None``. - """ - self.value = None - - def _get_value(self): - if self.pwm_device.frequency is None: - return None - else: - return ( - ((self.pwm_device.state - self._min_dc) / self._dc_range) * - self._value_range + self._min_value) - - @property - def value(self): - """ - Represents the position of the servo as a value between -1 (the minimum - position) and +1 (the maximum position). This can also be the special - value ``None`` indicating that the servo is currently "uncontrolled", - i.e. that no control signal is being sent. Typically this means the - servo's position remains unchanged, but that it can be moved by hand. - """ - result = self._get_value() - if result is None: - return result - else: - # NOTE: This round() only exists to ensure we don't confuse people - # by returning 2.220446049250313e-16 as the default initial value - # instead of 0. The reason _get_value and _set_value are split - # out is for descendents that require the un-rounded values for - # accuracy - return round(result, 14) - - @value.setter - def value(self, value): - if value is None: - self.pwm_device.frequency = None - elif -1 <= value <= 1: - self.pwm_device.frequency = int(1 / self.frame_width) - self.pwm_device.value = ( - self._min_dc + self._dc_range * - ((value - self._min_value) / self._value_range) - ) - else: - raise OutputDeviceBadValue( - "Servo value must be between -1 and 1, or None") - - @property - def is_active(self): - return self.value is not None diff --git a/shepherd/plugins/flytrap.py b/shepherd/plugins/flytrap.py deleted file mode 100644 index 03ed104..0000000 --- a/shepherd/plugins/flytrap.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python3 - -import shepherd.config as shconf -import shepherd.plugin - -import sys -import os -import time -import argparse - - - -class FlytrapPlugin(shepherd.plugin.Plugin): - @staticmethod - def define_config(confdef): - confdef.add_def('servo_open_pulse', shconf.IntDef(default=1200, minval=800, maxval=2200)) - confdef.add_def('servo_closed_pulse', shconf.IntDef(default=1800, minval=800, maxval=2200)) - confdef.add_def('servo_open_time', shconf.IntDef(default=5)) - - def __init__(self, pluginInterface, config): - super().__init__(pluginInterface, config) - self.config = config - self.interface = pluginInterface - self.plugins = pluginInterface.other_plugins - self.hooks = pluginInterface.hooks - - self.root_dir = os.path.expanduser(pluginInterface.coreconfig["root_dir"]) - self.id = pluginInterface.coreconfig["id"] - - print("Flytrap config:") - print(self.config) - - self.interface.attach_hook("usbcam", "pre_cam", self.led_on) - self.interface.attach_hook("usbcam", "post_cam", self.uv_camera) - - self.interface.register_function(self.test) - - def uv_camera(self): - self.led_off() - self.led_uv_on() - self.plugins["usbcam"].run_cameras(" UV") - self.led_uv_off() - self.run_servo() - - def led_on(self): - self.plugins["scout"].set_out1(True) - - def led_off(self): - self.plugins["scout"].set_out1(False) - - def led_uv_on(self): - self.plugins["scout"].set_out2(True) - - def led_uv_off(self): - self.plugins["scout"].set_out2(False) - - def run_servo(self): - self.plugins["scout"].set_aux5v(True) - #self.door_servo_power.on() - time.sleep(0.5) - - self.plugins["scout"].set_pwm1(True, self.config["servo_open_pulse"]) - #self.door_servo.pulse_width = self.config["servo_open_pulse"] / 1000000 - time.sleep(self.config["servo_open_time"]) - - self.plugins["scout"].set_pwm1(True, self.config["servo_closed_pulse"]) - #self.door_servo.pulse_width = self.config["servo_closed_pulse"] / 1000000 - time.sleep(self.config["servo_open_time"]) - self.plugins["scout"].set_pwm1(False, self.config["servo_closed_pulse"]) - #self.door_servo.detach() - self.plugins["scout"].set_aux5v(False) - #self.door_servo_power.off() - - def test(self): - self.led_on() - time.sleep(1) - self.led_off() - self.run_servo() diff --git a/shepherd/plugins/mothtrap.py b/shepherd/plugins/mothtrap.py deleted file mode 100644 index dc4079d..0000000 --- a/shepherd/plugins/mothtrap.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env python3 - -import shepherd.config as shconf -import shepherd.plugin - -import sys -import os -import time -import argparse - - - -class MothtrapPlugin(shepherd.plugin.Plugin): - @staticmethod - def define_config(confdef): - confdef.add_def('servo_open_pulse', shconf.IntDef(default=1200, minval=800, maxval=2200)) - confdef.add_def('servo_closed_pulse', shconf.IntDef(default=1800, minval=800, maxval=2200)) - confdef.add_def('servo_open_time', shconf.IntDef(default=5)) - - def __init__(self, pluginInterface, config): - super().__init__(pluginInterface, config) - self.config = config - self.interface = pluginInterface - self.plugins = pluginInterface.other_plugins - self.hooks = pluginInterface.hooks - - self.root_dir = os.path.expanduser(pluginInterface.coreconfig["root_dir"]) - self.id = pluginInterface.coreconfig["id"] - - print("Mothtrap config:") - print(self.config) - - - - #servo_max = self.config["servo_open_pulse"] / 1000000 - #servo_min = self.config["servo_closed_pulse"] / 1000000 - - #if servo_min > servo_max: - # servo_min, servo_max = servo_max, servo_min - - - #print(F"Supplied min: {servo_min}, max: {servo_max}") - - - self.interface.attach_hook("usbcam", "pre_cam", self.led_on) - self.interface.attach_hook("usbcam", "post_cam", self.led_off) - self.interface.attach_hook("usbcam", "post_cam", self.run_servo) - - self.interface.register_function(self.test) - - def led_on(self): - self.plugins["scout"].set_out1(True) - #self.led_power.on() - - def led_off(self): - self.plugins["scout"].set_out1(False) - #self.led_power.off() - - def run_servo(self): - self.plugins["scout"].set_aux5v(True) - #self.door_servo_power.on() - time.sleep(0.5) - - self.plugins["scout"].set_pwm1(True, self.config["servo_open_pulse"]) - #self.door_servo.pulse_width = self.config["servo_open_pulse"] / 1000000 - time.sleep(self.config["servo_open_time"]) - - self.plugins["scout"].set_pwm1(True, self.config["servo_closed_pulse"]) - #self.door_servo.pulse_width = self.config["servo_closed_pulse"] / 1000000 - time.sleep(self.config["servo_open_time"]) - self.plugins["scout"].set_pwm1(False, self.config["servo_closed_pulse"]) - #self.door_servo.detach() - self.plugins["scout"].set_aux5v(False) - #self.door_servo_power.off() - - def test(self): - self.led_on() - time.sleep(1) - self.led_off() - self.run_servo() diff --git a/shepherd/plugins/picam.py b/shepherd/plugins/picam.py deleted file mode 100644 index 26b1335..0000000 --- a/shepherd/plugins/picam.py +++ /dev/null @@ -1,174 +0,0 @@ -import io -import os -from datetime import datetime -import time - -import shepherd.config as shconf -import shepherd.plugin - - -from picamera import PiCamera -from PIL import Image, ImageDraw, ImageFont - - -asset_dir = os.path.dirname(os.path.realpath(__file__)) - -overlayfont_filename = os.path.join(asset_dir, "DejaVuSansMono.ttf") -logo_filename = os.path.join(asset_dir, "smallshepherd.png") - -# on server side, we want to be able to list commands that a module responds to -# without actually instantiating the module class. Add command templates into -# the conf_def, than attach to them in the interface? Was worried about having -# "two sources of truth", but you already need to match the conf_def to the -# name where you access the value in the module. Could have add_command, which -# you then add standard conf_def subclasses to, to reuse validation and server -# form generation logic... - - -class PiCamPlugin(shepherd.plugin.Plugin): - @staticmethod - def define_config(confdef): - confdef.add_def('upload_images', shconf.BoolDef(default=False, optional=True, - helptext="If true, move to an Uploader bucket. Requires Uploader plugin")) - confdef.add_def('upload_bucket', shconf.StringDef(default="", optional=True, - helptext="Name of uploader bucket to shift images to.")) - confdef.add_def('save_directory', shconf.StringDef(default="", optional=True, - helptext="Name of directory path to save images. If empty, a 'usbcamera' directory under the Shepherd root dir will be used")) - confdef.add_def('append_id', shconf.BoolDef(default=True, optional=True, - helptext="If true, add the system ID to the end of image filenames")) - confdef.add_def('show_overlay', shconf.BoolDef(default=True, optional=True, - helptext="If true, add an overlay on each image with the system ID and date.")) - confdef.add_def('overlay_desc', shconf.StringDef(default="", optional=True, - helptext="Text to add to the overlay after the system ID and camera name")) - confdef.add_def('jpeg_quality', shconf.IntDef(default=80, minval=60, maxval=95, optional=True, - helptext="JPEG quality to save with. Max of 95, passed directly to Pillow")) - - array = confdef.add_def('trigger', shconf.TableArrayDef( - helptext="Array of triggers that will use all cameras")) - array.add_def('hour', shconf.StringDef()) - array.add_def('minute', shconf.StringDef()) - array.add_def('second', shconf.StringDef(default="0", optional=True)) - - def __init__(self, pluginInterface, config): - super().__init__(pluginInterface, config) - self.config = config - self.interface = pluginInterface - self.plugins = pluginInterface.other_plugins - self.hooks = pluginInterface.hooks - - self.root_dir = os.path.expanduser(pluginInterface.coreconfig["root_dir"]) - self.id = pluginInterface.coreconfig["id"] - - self.interface.register_hook("pre_cam") - self.interface.register_hook("post_cam") - self.interface.register_function(self.camera_job) - # do some camera init stuff - - print("PiCamera config:") - print(self.config) - - # Seconds to wait for exposure and white balance auto-adjust to stabilise - self.stabilise_delay = 3 - - if self.config["save_directory"] is "": - self.save_directory = os.path.join(self.root_dir, "picamera") - else: - self.save_directory = self.config["save_directory"] - - if not os.path.exists(self.save_directory): - os.makedirs(self.save_directory) - - if self.config["show_overlay"]: - # Load assets - self.logo_im = Image.open(logo_filename) - - self.font_size_cache = {} - self.logo_size_cache = {} - - #global cam_led - #cam_led = LED(CAMERA_LED_PIN, active_high=False, initial_value=False) - - for trigger in self.config["trigger"]: - trigger_id = trigger["hour"]+'-' + trigger["minute"]+'-'+trigger["second"] - self.interface.add_job( - self.camera_job, trigger["hour"], trigger["minute"], trigger["second"], job_name=trigger_id) - - def _generate_overlay(self, width, height, image_time): - font_size = int(height/40) - margin_size = int(font_size/5) - - if font_size not in self.font_size_cache: - self.font_size_cache[font_size] = ImageFont.truetype( - overlayfont_filename, int(font_size*0.9)) - thisfont = self.font_size_cache[font_size] - - if font_size not in self.logo_size_cache: - newsize = (int(self.logo_im.width*( - font_size/self.logo_im.height)), font_size) - self.logo_size_cache[font_size] = self.logo_im.resize( - newsize, Image.BILINEAR) - thislogo = self.logo_size_cache[font_size] - - desc_text = self.config["overlay_desc"] - if self.config["append_id"]: - desc_text = self.id + " " + desc_text - - time_text = image_time.strftime("%Y-%m-%d %H:%M:%S") - - overlay = Image.new('RGBA', (width, font_size+(2*margin_size)), (0, 0, 0)) - overlay.paste(thislogo, (int((overlay.width-thislogo.width)/2), margin_size)) - - draw = ImageDraw.Draw(overlay) - draw.text((margin_size*2, margin_size), desc_text, - font=thisfont, fill=(255, 255, 255, 255)) - - datewidth, _ = draw.textsize(time_text, thisfont) - draw.text((overlay.width-(margin_size*2)-datewidth, margin_size), time_text, font=thisfont, - fill=(255, 255, 255, 255)) - - # make whole overlay half transparent - overlay.putalpha(128) - return overlay - - - def camera_job(self): - self.hooks.pre_cam() - - #Capture image - print("Running camera...") - stream = io.BytesIO() - with PiCamera() as picam: - picam.resolution = (3280, 2464) - picam.start_preview() - time.sleep(self.stabilise_delay) - picam.capture(stream, format='jpeg') - # "Rewind" the stream to the beginning so we can read its content - stream.seek(0) - img = Image.open(stream) - - #Process image - image_time = datetime.now() - - if self.config["show_overlay"]: - overlay = self._generate_overlay(img.width, img.height, image_time) - img.paste(overlay, (0, img.height-overlay.height), overlay) - - image_filename = image_time.strftime("%Y-%m-%d %H-%M-%S") - if self.config["append_id"]: - image_filename = image_filename + " " + self.id - - image_filename = image_filename + ".jpg" - image_filename = os.path.join(self.save_directory, image_filename) - img.save(image_filename+".writing", "JPEG", quality=self.config["jpeg_quality"]) - os.rename(image_filename+".writing", image_filename) - - if self.config["upload_images"]: - self.plugins["uploader"].move_to_bucket(image_filename, self.config["upload_bucket"]) - - self.hooks.post_cam() - - -if __name__ == "__main__": - pass - # print("main") - # main(sys.argv[1:]) diff --git a/shepherd/plugins/scout/__init__.py b/shepherd/plugins/scout/__init__.py deleted file mode 100644 index f7d1b79..0000000 --- a/shepherd/plugins/scout/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .scout import ScoutPlugin \ No newline at end of file diff --git a/shepherd/plugins/scout/scout.py b/shepherd/plugins/scout/scout.py deleted file mode 100644 index 4adab3e..0000000 --- a/shepherd/plugins/scout/scout.py +++ /dev/null @@ -1,208 +0,0 @@ -#!/usr/bin/env python3 - -""" -Plugin to interface with the shepherd scout pcb modules. Also semi-compatible -with the older TrapCtrl style boards and Pi HATs based on the SleepyPi2, provided -they are running the Shepherd Scout firmware. - -The TDW serial message format is used to pull data from the companion board and -interact with its RTC and set alarms. This library uses a seperate thread -to handle the comms with the supervising microcontroller. Interface functions -add a request to the queue, and some may wait for a state to be updated before -returning (with a timeout). -""" - -import shepherd.config as shconf -import shepherd.plugin -from . import tdw - -import queue -import threading -import re -import serial -import time -from datetime import datetime - -from enum import Enum, auto - -from collections import namedtuple - - -class MsgName(Enum): - BATV = "batv" - BATI = "bati" - TIME = "time" - ALARM = "alarm" - AUX5V = "aux5v" - PWM1 = "pwm1" - PWM2 = "pwm2" - OUT1 = "out1" - OUT2 = "out2" - VERSION = "version" - LOG = "log" - MEASUREMENT = "meas" - - def __str__(self): - return str(self.value) - - -logmsgs = ["LE_NONE - Empty log", - "LE_POWERUP - Scout startup", - "LE_PI_BOOT_TIMEOUT - Tried to turn on Pi but did not recieve succesful boot signal", - "LE_PI_ON - Pi has booted", - "LE_LOW_VOLT_START_SHUTDOWN - Initiated Pi shutdown due to low supply voltage", - "LE_PI_SIGNAL_START_SHUTDOWN - Pi started to shut itself down", - "LE_PI_SHUTDOWN_TIMEOUT - Pi did not signal successful shutdown, so killed power", - "LE_MAIN5V_DISABLE - Main Pi power turned off", - "LE_VOLT_GOOD_MAIN5V_ENABLE - Turned Pi power on after voltage raised enough", - "LE_ALARM_MAIN5V_ENABLE - Turned Pi power on after wakeup alarm was hit"] - - -class ScoutPlugin(shepherd.plugin.Plugin): - @staticmethod - def define_config(confdef): - confdef.add_def('boardver', shconf.StringDef()) - confdef.add_def('serialport', shconf.StringDef()) - - def __init__(self, pluginInterface, config): - super().__init__(pluginInterface, config) - self.config = config - self.interface = pluginInterface - self.plugins = pluginInterface.other_plugins - self.hooks = pluginInterface.hooks - - self.msg_handler = tdw.MessageHandler(config["serialport"], 57600) - - self.interface.register_function(self.get_batv) - self.interface.register_function(self.get_bati) - self.interface.register_function(self.get_time) - self.interface.register_function(self.set_alarm) - self.interface.register_function(self.set_aux5v) - self.interface.register_function(self.set_pwm1) - self.interface.register_function(self.set_pwm2) - self.interface.register_function(self.set_out1) - self.interface.register_function(self.set_out2) - self.interface.register_function(self.test_logs) - self.interface.register_function(self.get_logs) - self.interface.register_function(self.get_measurements) - - self.interface.register_function(self.test) - - def get_version(self): - rqst = self.msg_handler.send_request(MsgName.VERSION.value) - if rqst.wait_for_response(): - return rqst.response.arguments[0:2] - return None - - def get_batv(self): - rqst = self.msg_handler.send_request(MsgName.BATV.value) - if rqst.wait_for_response(): - return rqst.response.arguments[0] - return None - - def get_bati(self): - rqst = self.msg_handler.send_request(MsgName.BATI) - if rqst.wait_for_response(): - return rqst.response.arguments[0] - return None - - def set_aux5v(self, enabled): - cmd = self.msg_handler.send_command(MsgName.AUX5V, [str(enabled).lower()]) - if cmd.wait_for_response(): - return cmd.response.arguments[0] - return None - - def set_pwm1(self, enabled, pulse_length): - cmd = self.msg_handler.send_command( - MsgName.PWM1, [str(enabled).lower(), str(pulse_length)]) - if cmd.wait_for_response(): - return cmd.response.arguments[0] - return None - - def set_pwm2(self, enabled, pulse_length): - cmd = self.msg_handler.send_command( - MsgName.PWM2, [str(enabled).lower(), str(pulse_length)]) - if cmd.wait_for_response(): - return cmd.response.arguments[0] - return None - - def set_out1(self, enabled): - cmd = self.msg_handler.send_command(MsgName.OUT1, [str(enabled).lower()]) - if cmd.wait_for_response(): - return cmd.response.arguments[0] - return None - - def set_out2(self, enabled): - cmd = self.msg_handler.send_command(MsgName.OUT2, [str(enabled).lower()]) - if cmd.wait_for_response(): - return cmd.response.arguments[0] - return None - - def get_time(self): - rqst = self.msg_handler.send_request(MsgName.TIME) - if rqst.wait_for_response(): - return rqst.response.arguments[0] - return None - - def set_alarm(self, unix_time): - cmd = self.msg_handler.send_command(MsgName.ALARM, [unix_time]) - if cmd.wait_for_response(): - return cmd.response.arguments[0] - return None - - def get_logs(self): - rqst = self.msg_handler.send_request(MsgName.LOG) - if rqst.wait_for_response(): - return rqst.response.multipart_args - return None - - def get_measurements(self): - rqst = self.msg_handler.send_request(MsgName.MEASUREMENT) - if rqst.wait_for_response(): - return rqst.response.multipart_args - return None - - def test_logs(self): - rqst = self.msg_handler.send_request(MsgName.LOG) - if rqst.wait_for_response(): - for logline in reversed(rqst.response.multipart_args): - logdate = datetime.fromtimestamp(int(logline[0])) - batv = float(logline[1])/100.0 - logmessage = logmsgs[int(logline[2])] - print(F"::{logdate:%Y-%m-%d %H:%M:%S}:: {batv:.2f}V :: {logmessage}") - return None - - def test(self): - print("Testing companion board...") - print(F"Current RTC time is {self.get_time()}") - print(F"Current BatV is {self.get_batv()}") - print(F"Current BatI is {self.get_bati()}") - print("Turning on Out1 for 1 second") - self.set_out1(True) - time.sleep(1) - self.set_out1(False) - print("Turning on Out2 for 1 second") - self.set_out2(True) - time.sleep(1) - self.set_out2(False) - - print("Enabling auxilliary 5V") - self.set_aux5v(True) - - print("Sweeping PWM1 from 1000us to 2000us") - self.set_pwm1(True, 1000) - time.sleep(1) - self.set_pwm1(True, 2000) - time.sleep(1) - self.set_pwm1(False, 1000) - - print("Sweeping PWM2 from 1000us to 2000us") - self.set_pwm2(True, 1000) - time.sleep(1) - self.set_pwm2(True, 2000) - time.sleep(1) - self.set_pwm2(False, 1000) - self.set_aux5v(False) - print("Test finished") - - return None diff --git a/shepherd/plugins/scout/tdw.py b/shepherd/plugins/scout/tdw.py deleted file mode 100644 index f4c8868..0000000 --- a/shepherd/plugins/scout/tdw.py +++ /dev/null @@ -1,471 +0,0 @@ - -""" -Python module implementing the TDW (This Do What #!?) serial message format. - - -Currently only supports responses to sent messages, rather than generic RX. - -Intended use: - ->>> msg_handler = tdw.MessageHandler("/dev/ttyAMA0", 57600) ->>> rqst = msg_handler.send_request("BATV") ->>> if rqst.wait_for_response() ->>> print(rqst.response) - -""" - - -import sys -import queue -import threading -import serial -import re -import time -import logging -from enum import Enum - -log = logging.getLogger(__name__) - -DEFAULT = object() - -MAX_MSG_LEN = 128 -MAX_MSG_ARGS = 8 - - -# Return a Message object, allowing the the caller to check a "sent" flag to see if it's gone through -# yet. If needs_response was true, enable a "wait_for_response()" function that internally blocks until -# response is recieved or times out. Returns either None or a Response object. -# Note that unless a class overrides __bool__(), it will always evaluate to True, allowing "if wait_for_response():" -# to check if there's a returned message. A ".response" property gets filled after returning too, allowing -# use of that rather than the caller having to get a reference. -# Once "wait_for_response()"" returns, the message handler removes it's reference to the message and -# the response, so the caller can be sure that it won't be changed underneath it. - - -# Currently this is all designed only for comms initiated by Python, and doesn't handle responding -# to communication initiated by the device yet. For that, perhaps supply callback attachments -# for specific message names as well as a generic one, but by default queue them up and wait -# for a call to a "process_messages()" function or something - to allow callbacks to be handled -# in the same main thread. Optionally have a flag when creating the MessageHandler for it to -# dispatch callbacks in new threads asynchronously (or have individual flags when attaching callbacks -# perhaps?) - -class TDWException(Exception): - pass - - -class ResponseNotReceivedError(TDWException): - pass - - -class MessageType(Enum): - COMMENT = "#" - COMMAND = "!" - REQUEST = "?" - - def __str__(self): - return str(self.value) - - -class Message(): - """ - Representation of a message to be sent from this device. - Also used to track the corresponding response from the other device. - """ - - def __init__(self, msg_type, msg_name, arguments, multipart_args=None): - if not isinstance(msg_type, MessageType): - raise TypeError("Argument message_type must be a MessageType") - self._msg_type = msg_type - self._msg_name = str(msg_name) - self._arguments = arguments - self._multipart_args = multipart_args - - @property - def msg_type(self): - return self._msg_type - - @property - def msg_name(self): - return self._msg_name - - @property - def arguments(self): - return self._arguments - - @property - def multipart_args(self): - return self._multipart_args - - def __str__(self): - return F"<{self.__class__.__name__}: {self.__dict__}>" - - -class TXMessage(Message): - """ - Locks down and stringifies the arguments and multipart arg list into tuples on creation, avoid - them being changed underneath it once the message is added to the send queue. - """ - - def __init__(self, msg_type, msg_name, arguments, needs_response, response_timeout, multipart_args=None): - - # Allow single value or string as arg, convert to list - if isinstance(arguments, str) or (not hasattr(arguments, "__iter__")): - arguments = [arguments] - - # Stringify args and put in immutable tuples - immutable_args = tuple([str(arg) for arg in arguments]) - - immutable_multipart_args = None - if multipart_args is not None: - # Allow single value or string as arg, convert to list - if isinstance(multipart_args, str) or (not hasattr(multipart_args, "__iter__")): - multipart_args = [multipart_args] - - immutable_multipart_args = [] - for arglist in multipart_args: - immutable_multipart_args.append(tuple([str(arg) for arg in arglist])) - immutable_multipart_args = tuple(immutable_multipart_args) - - super().__init__(msg_type, msg_name, immutable_args, immutable_multipart_args) - - self._needs_response = needs_response - self._response_timeout = response_timeout - self._response = None - - # event that is triggered when a matching command response is - # recieved and the data parsed into ScoutState. ScoutState should - # only be then read via the lock on it. - self._responded = threading.Event() - - @property - def needs_response(self): - return self._needs_response - - @property - def response(self): - return self._response - - def wait_for_response(self): - if not self._needs_response: - raise Exception( - "Can't wait for response on a message that has not set 'needs_response=True'") - if self._responded.wait(self._response_timeout): - # Serial thread should have populated self._response by now - return self.response - else: - return False - - -class RXMessage(Message): - def __init__(self, msg_type, msg_name, arguments, is_multipart=False, multipart_count=0): - - super().__init__(msg_type, msg_name, arguments) - - self._multipart_count = multipart_count - self._is_multipart = is_multipart - if is_multipart: - self._multipart_args = [] - - @property - def multipart_count(self): - """ - The declared number of message parts. - //Not// necessarily the number or parts that were actually received. - For multipart messages that don't declare a count, this is None. - """ - return self._multipart_count - - @property - def is_multipart(self): - """ - Returns true if the message was a multi-part message - """ - return self._is_multipart - - -class MessageHandler(): - def __init__(self, serial_port, baud_rate, response_timeout=0.5, multipart_timeout=0.2, loop_delay=0.01): - self._tx_message_queue = queue.Queue() - - self._tx_message = None - self._tx_sent_time = None - - self._rx_message = None - self._last_rx_time = None - - self._rx_multipart_timeout = multipart_timeout - - # Default, can be overridden by individual messages - self.response_timeout = response_timeout - - # delay used in serial processing thread between iterations. - # If zero, the handler will just spin constantly asking if more serial bytes are available - # (a fcntl.ioctl call). If too large, you lose responsiveness when a new message comes in (when - # not waiting for a response the loop delay has an event trigger, so new messages get sent immediately) - # Very large values risk filling the serial buffer before data can be processed. - - # pyserial buffer is apparently 1024 or 4096 bytes, and at 57600 baud 10ms delay would only - # be 72 bytes. - self.loop_delay = loop_delay - - self._rx_string = "" - - self.port = serial.Serial() - self.port.baudrate = baud_rate # 57600 - # Set port to be non-blocking - self.port.timeout = 0 - self.port.port = serial_port - - # self._re_message_frame = re.compile(r"([#!?])(.+?)[\r\n]") - self._re_msg_start = re.compile(r"[#!?]") - self._frame_end_chars = "\r\n" - self._re_msg_bounds = re.compile(r"[#!?\r\n]") - - # start thread - self.thread = threading.Thread(target=self._serial_comm_thread, daemon=True) - self.thread.start() - - def send_message(self, message): - """ - Add a message to the queue to be sent - """ - if not isinstance(message, TXMessage): - raise TypeError("'message' argument must be of type TXMessage") - self._tx_message_queue.put(message) - - def send_comment(self, message_name, arguments=[], needs_response=False, response_timeout=DEFAULT): - if response_timeout is DEFAULT: - response_timeout = self.response_timeout - msg = TXMessage(MessageType.COMMENT, message_name, - arguments, needs_response, response_timeout) - self.send_message(msg) - return msg - - def send_command(self, message_name, arguments=[], needs_response=True, response_timeout=DEFAULT): - if response_timeout is DEFAULT: - response_timeout = self.response_timeout - msg = TXMessage(MessageType.COMMAND, message_name, - arguments, needs_response, response_timeout) - self.send_message(msg) - return msg - - def send_request(self, message_name, arguments=[], needs_response=True, response_timeout=DEFAULT): - if response_timeout is DEFAULT: - response_timeout = self.response_timeout - msg = TXMessage(MessageType.REQUEST, message_name, - arguments, needs_response, response_timeout) - self.send_message(msg) - return msg - - def response_from_request(self, message_name, arguments=[], response_timeout=DEFAULT): - ''' - Sends request and returns the response. Blocks while waiting. Throws ResponseNotReceivedError - if the response times out. - ''' - rqst = self.send_request(message_name, arguments, True, response_timeout) - if rqst.wait_for_response(): - return rqst.response.arguments - raise ResponseNotReceivedError(rqst) - - def response_from_command(self, message_name, arguments=[], response_timeout=DEFAULT): - ''' - Sends command and returns the response. Blocks while waiting. Throws ResponseNotReceivedError - if the response times out. - ''' - cmd = self.send_command(message_name, arguments, True, response_timeout) - if cmd.wait_for_response(): - return cmd.response.arguments - raise ResponseNotReceivedError(cmd) - - def _send_message(self): - """ - Actually send a message pulled from the queue. - Only called from the serial_comm thread - """ - - argstr = "" - if len(self._tx_message.arguments) > 0: - argstr = ':'+','.join(self._tx_message.arguments) - - send_str = F"{self._tx_message.msg_type}{self._tx_message.msg_name}{argstr}\n" - self.port.write(send_str.encode('utf-8')) - self._tx_sent_time = time.time() - log.debug(F"Msg TX: {send_str}") - # Only keep the current message around if we need to track a response - if not self._tx_message.needs_response: - self._tx_message = None - - def _find_msg_frame(self): - """ - Finds next frame in the _rx_string, trimming any excess. - Returns true if it found something and needs to be called again, - intended to be called in a loop. - - """ - match = self._re_msg_start.search(self._rx_string) - if match is None: - # No command start characters anywhere in the string, so ditch it - self._rx_string = "" - return False - - # trim anything before start - if match.start() > 0: - self._rx_string = self._rx_string[match.start():] - - # Search for next start or end character - match = self._re_msg_bounds.search(self._rx_string, 1) - if match is None: - # No end of frame found - if len(self._rx_string) > MAX_MSG_LEN: - # We have a message start, but too many characters after it without an end - # of frame, so ditch it. - self._rx_string = "" - return False - - if match[0] in self._frame_end_chars: - # Found our frame end - self._parse_message_text(self._rx_string[:match.start()]) - # Trim out the message we just found and start again - self._rx_string = self._rx_string[match.end():] - return True - else: - # Found another start character, trim and start again - self._rx_string = self._rx_string[match.start():] - return True - - def _parse_message_text(self, msg_str): - """ - Parse a recieved message string. - 'msg_str' contains the start character and everything after that, - not including the terminating linefeed. - Only called from the serial_comm thread - """ - - log.debug(F"Msg RX: {msg_str}") - self._last_rx_time = time.time() - - msg_name, _, msg_args_text = msg_str.strip(' \t').partition(':') - - # Convert the character to our enum for later comparison - msg_type = MessageType(msg_name[0]) - - msg_name = msg_name[1:] - msg_args = msg_args_text.split(',') - - # Trim whitespace around name and args - msg_name = msg_name.strip(' \t') - msg_args = [arg.strip(' \t') for arg in msg_args] - - multipart_count = None - multipart_start = False - multipart_end = False - if msg_args[-1][0] == '<': - multipart_start = True - # Message is indicating a multipart start - count_txt = msg_args[-1][1:].strip(" \t") - try: - multipart_count = int(count_txt) - except ValueError: - # If no count supplied or can't understand it, treat it as open - multipart_count = None - - # Remove the multipart start indicator from the args - msg_args = msg_args[:-1] - elif msg_args[-1][0] == '>': - multipart_end = True - # Remove the multipart end indicator from the args - msg_args = msg_args[:-1] - - if self._rx_message is not None: - # We have a multipart message in progress - if (self._rx_message.msg_name == msg_name) and (self._rx_message.msg_type == msg_type) and (not multipart_start): - # Only skip adding args to list if it's a blank mutipart end message - if not (multipart_end and (len(msg_args) == 0)): - self._rx_message._multipart_args.append(msg_args) - - if multipart_end or (len(self._rx_message.multipart_args) >= self._rx_message.multipart_count): - self._process_rx_message() - # We're done here - return - else: - # We've got a new message interrupting the in-progress one - # Close off existing rx message and continue - self._process_rx_message() - - self._rx_message = RXMessage(msg_type, msg_name, msg_args, - is_multipart=multipart_start, multipart_count=multipart_count) - if not self._rx_message.is_multipart: - self._process_rx_message() - - def _process_rx_message(self): - """ - Process a received and parsed message in self._rx_message, - and dispatches any necessary actions. - Clears self._rx_message after it's done. - """ - #print(F"Process rx_msg: {self._rx_message.msg_name}") - #print(F"tx message during processing is {self._tx_message.msg_name}") - - if (self._tx_message is not None) and (self._tx_message.msg_name == self._rx_message.msg_name) and (self._rx_message.msg_type == MessageType.COMMENT): - # _tx_message only hangs around if it's waiting for response. - # can't just assume next received message is a response, as the other - # device might be sending something else in the meantime, so check for name. - # Responses are always comments. - self._tx_message._response = self._rx_message - self._tx_message._responded.set() - self._tx_message = None - else: - pass - # TODO - Handle other type of received message and dispatch actions here - callbacks? - self._rx_message = None - - def _handle_serial_port(self): - # If there's bytes, read them and deal with them. The underlying port read locks - # the GIL, so use non-blocking mode. - if self.port.in_waiting > 0: - new_bytes = self.port.read(self.port.in_waiting) - self._rx_string = self._rx_string + new_bytes.decode('utf-8') - # Find and process message frames - while self._find_msg_frame(): - pass - # Loop back to check for more RX characters (the priority) - return - - if self._rx_message is not None: - if (time.time()-self._last_rx_time) >= self._rx_multipart_timeout: - # Been too long waiting for a multipart message part, timeout and process - # so the main program can use the message - self._process_rx_message() - - if self._tx_message is not None: - # Wait for current tx_message to time out - if (time.time()-self._tx_sent_time) >= self._tx_message._response_timeout: - # Timeout the request - self._tx_message = None - else: - time.sleep(self.loop_delay) - else: - # Try and get a new message to send - try: - self._tx_message = self._tx_message_queue.get( - block=True, timeout=self.loop_delay) - # Only gets here if current_tx_message is actually set - self._send_message() - except queue.Empty: - pass - - def _serial_comm_thread(self): - while True: - # Actual wait is on either the non-empty queue or a serial character to parse - # Serial comms is not synchronous, so need to be available to recieve characters - # at any point - try: - log.info(F"Connecting to serial port {self.port.port}, with baud {self.port.baudrate}...") - self.port.open() - while True: - self._handle_serial_port() - except serial.SerialException: - log.error("Could not open serial port") - time.sleep(1) - # If there's a serialexception, try to reopen the port diff --git a/shepherd/plugins/smallshepherd.png b/shepherd/plugins/smallshepherd.png deleted file mode 100644 index 0b12a6e..0000000 Binary files a/shepherd/plugins/smallshepherd.png and /dev/null differ diff --git a/shepherd/plugins/uploader.py b/shepherd/plugins/uploader.py deleted file mode 100644 index 710d36e..0000000 --- a/shepherd/plugins/uploader.py +++ /dev/null @@ -1,234 +0,0 @@ -#!/usr/bin/env python3 - -import shutil -import os -import threading -import paramiko -import shepherd.config as shconf -import shepherd.plugin -# configdef = shepherd.config.definition() - -# Can either import shepherd.config here, and call a function to build a config_def -# or can leave a config_def entry point. -# probably go with entry point, to stay consistent with the module - - -# on server side, we want to be able to list commands that a module responds to -# without actually instantiating the module class. Add command templates into -# the conf_def, than attach to them in the interface? Was worried about having -# "two sources of truth", but you already need to match the conf_def to the -# name where you access the value in the module. Could have add_command, which -# you then add standard conf_def subclasses to, to reuse validation and server -# form generation logic... - - -# The uploader plugin allows the definition of upload "buckets" - essentially -# as a way of collecting together settings for upload, timing, and retention settings -# Buckets will ignore any filenames ending with ".writing" or ".uploaded" -# The "move_to_bucket" interface function is provided, but bucket directories will -# also work with any files moved into them externally. - - -class Destination(): - def __init__(self, config, node_id, root_dir): - self.config = config - self.node_id = node_id - self.root_dir = root_dir - self.sendlist_condition = threading.Condition() - self.send_list = [] - - self.thread = threading.Thread(target=self._send_files) - self.thread.start() - - # Override this in subclasses, implementing the actual upload process. - # Return true on success, false on failure. - def upload(self, filepath, suffix): - print("Dummy uploading "+filepath) - return True - - def add_files_to_send(self, file_path_list): - self.sendlist_condition.acquire() - for file_path in file_path_list: - if file_path not in self.send_list: - self.send_list.append(file_path) - self.sendlist_condition.notify() - - self.sendlist_condition.release() - - def _file_available(self): - return len(self.send_list) > 0 - - def _send_files(self): - while True: - self.sendlist_condition.acquire() - # this drops through immediately if there is something to send, otherwise waits - self.sendlist_condition.wait_for(self._file_available) - file_to_send = self.send_list.pop(0) - os.rename(file_to_send, file_to_send+".uploading") - self.sendlist_condition.release() - - # Rename uploaded file to end with ".uploaded" on success, or back - # to original path on failure. - try: - self.upload(file_to_send, ".uploading") - os.rename(file_to_send+".uploading", file_to_send+".uploaded") - except Exception as e: - print(F"Upload failed with exception {e}") - os.rename(file_to_send+".uploading", file_to_send) - self.send_list.append(file_to_send) - - -class SFTPDestination(Destination): - def upload(self, filepath, suffix): - print("Starting upload...") - with paramiko.Transport((self.config["address"], - self.config["port"])) as transport: - transport.connect(username=self.config["username"], - password=self.config["password"]) - with paramiko.SFTPClient.from_transport(transport) as sftp: - print("Uploading "+filepath+" to " + - self.config["address"]+" via SFTP") - if self.config["add_id_to_path"]: - destdir = os.path.join(self.config["path"], - self.node_id) - else: - destdir = self.config["path"] - - try: - sftp.listdir(destdir) - except IOError: - print("Creating remot dir:" + destdir) - sftp.mkdir(destdir) - - print("Target dir:"+destdir) - sftp.put(filepath+suffix, - os.path.join(destdir, os.path.basename(filepath))) - - -class Bucket(): - def __init__(self, name, open_link_on_new, opportunistic, keep_copy, - destination, node_id, root_dir, path=None, old_path=None): - self.newfile_event = threading.Event() - self.newfile_event.set() - - self.node_id = node_id - self.root_dir = root_dir - - self.destination = destination - - self.path = path - if self.path is None: - self.path = os.path.join(self.root_dir, name) - if not os.path.exists(self.path): - os.makedirs(self.path) - - if keep_copy: - self.old_path = old_path - if self.old_path is None: - self.old_path = os.path.join( - self.root_dir, name + "_old") - if not os.path.exists(self.old_path): - os.makedirs(self.old_path) - - self.thread = threading.Thread(target=self._check_files) - self.thread.start() - - def _check_files(self): - - while True: - # NOTE: The reason we use an event here, rather than a lock or condition - # is that we're not sharing any internal state between the threads - just - # the filesystem itself and using the atomicity of file operations. While - # less clean in a pure python sense, this allows for more flexibility in - # allowing other sources of files - self.newfile_event.wait(timeout=10) - self.newfile_event.clear() - bucket_files = [] - for item in os.listdir(self.path): - item_path = os.path.join(self.path, item) - if (os.path.isfile(item_path) and - (not item.endswith(".writing")) and - (not item.endswith(".uploading")) and - (not item.endswith(".uploaded"))): - bucket_files.append(item_path) - #TODO check for .uploaded files and either delete or - # if keep_copy, move to self.old_path - - if bucket_files: - self.destination.add_files_to_send(bucket_files) - - -class UploaderPlugin(shepherd.plugin.Plugin): - @staticmethod - def define_config(confdef): - dests = confdef.add_def('destination', shconf.TableArrayDef()) - dests.add_def('name', shconf.StringDef()) - dests.add_def('protocol', shconf.StringDef()) - dests.add_def('address', shconf.StringDef(optional=True)) - dests.add_def('port', shconf.IntDef(optional=True)) - dests.add_def('path', shconf.StringDef(optional=True)) - dests.add_def('username', shconf.StringDef(optional=True)) - dests.add_def('password', shconf.StringDef(optional=True)) - dests.add_def('keyfile', shconf.StringDef( - default="", optional=True)) - dests.add_def('add_id_to_path', shconf.BoolDef( - default=True, optional=True)) - - buckets = confdef.add_def('bucket', shconf.TableArrayDef()) - buckets.add_def('name', shconf.StringDef()) - buckets.add_def('open_link_on_new', shconf.BoolDef()) - buckets.add_def('opportunistic', shconf.BoolDef( - default=True, optional=True)) - buckets.add_def('keep_copy', shconf.BoolDef()) - buckets.add_def('destination', shconf.StringDef()) - - def __init__(self, pluginInterface, config): - super().__init__(pluginInterface, config) - self.config = config - self.interface = pluginInterface - self.plugins = pluginInterface.other_plugins - self.hooks = pluginInterface.hooks - - self.root_dir = os.path.expanduser(pluginInterface.coreconfig["root_dir"]) - self.id = pluginInterface.coreconfig["id"] - - print("Uploader config:") - print(self.config) - - self.interface.register_function(self.move_to_bucket) - - self.destinations = {} - self.buckets = {} - - for dest_conf in self.config["destination"]: - if dest_conf["protocol"] == "sftp": - self.destinations[dest_conf["name"]] = SFTPDestination( - dest_conf, self.id, self.root_dir) - else: - self.destinations[dest_conf["name"]] = Destination( - dest_conf, self.id, self.root_dir) - - for bucketconf in self.config["bucket"]: - bucketconf["destination"] = self.destinations[bucketconf["destination"]] - self.buckets[bucketconf["name"]] = Bucket( - **bucketconf, node_id=self.id, root_dir=self.root_dir) - - def move_to_bucket(self, filepath, bucket_name): - # use intermediary step with ".writing" on the filename - # in case the source isn't in the same filesystem and so the - # move operation might not be atomic. Once it's there, the rename - # _is_ atomic - dest_path = os.path.join(self.buckets[bucket_name].path, - os.path.basename(filepath)) - temp_dest_path = dest_path + ".writing" - shutil.move(filepath, temp_dest_path) - os.rename(temp_dest_path, dest_path) - - # notify bucket to check for new files - self.buckets[bucket_name].newfile_event.set() - - -if __name__ == "__main__": - pass - # print("main") - # main(sys.argv[1:]) diff --git a/shepherd/plugins/usbcam.py b/shepherd/plugins/usbcam.py deleted file mode 100644 index 3adfad1..0000000 --- a/shepherd/plugins/usbcam.py +++ /dev/null @@ -1,327 +0,0 @@ -import io -import os -from datetime import datetime -import time -import re - -import shepherd.config as shconf -import shepherd.plugin - -import threading - -import subprocess - -from collections import namedtuple, OrderedDict -from operator import itemgetter - - -import cv2 -from PIL import Image, ImageDraw, ImageFont - -asset_dir = os.path.dirname(os.path.realpath(__file__)) - -overlayfont_filename = os.path.join(asset_dir, "DejaVuSansMono.ttf") -logo_filename = os.path.join(asset_dir, "smallshepherd.png") - -# Note: Add a lock to the gstreamer function, to avoid multiple triggers colliding - -CameraPort = namedtuple( - 'CameraPort', ['usbPath', 'devicePath']) - -# Short wrapper to allow use in a ``with`` context -class VideoCaptureCtx(): - def __init__(self, *args, **kwargs): - self.capture_dev = cv2.VideoCapture(*args, **kwargs) - def __enter__(self): - return self.capture_dev - def __exit__(self, *args): - self.capture_dev.release() - - -def get_connected_cameras(): - # This will return devices orderd by the USB path, regardless of the order they're connected in - device_list_str = subprocess.run( - ['v4l2-ctl', '--list-devices'], text=True, stdout=subprocess.PIPE).stdout - # in each match, first group is the USB path, second group is the device path - portlist = re.findall(r"-([\d.]+?)\):\n\s*?(\/dev\S+?)\n", device_list_str) - return [CameraPort(*port) for port in portlist] - - -def get_capture_formats(video_device): - """ - Call ``v4l2-ctl --device {video_device} --list-formats-ext`` and parse the output into a format dict - - Returns a dict with 4CC format codes as keys, and lists of (width,height) tuples as values - """ - device_fmt_str = subprocess.run( - ['v4l2-ctl', '--device', F'{video_device}', '--list-formats-ext'], text=True, stdout=subprocess.PIPE).stdout - - split_fmts = re.split(r"\[\d\]: '(\w{4}).*", device_fmt_str) - if len(split_fmts) < 3: - raise Exception("Did not get valid device format list output") - - # Iterate through successive pairs in the split, where the first is the format mode and the - # second is the text containing all the resolution options. Skip the first bit, which is rubbish - format_dict = {} - for fourcc, size_text in zip(split_fmts[1::2], split_fmts[2::2]): - resolutions = re.findall(r"(\d+?)x(\d+?)\D", size_text) - format_dict[fourcc] = resolutions - return format_dict - - -def get_largest_resolution(size_list): - """ - Accepts a list of tuples where the first element is a width and the second is a height. - - Returns a single resolution tuple representing the largest area from the list - """ - return max(size_list, key=lambda size: int(size[0]*int(size[1]))) - - -def set_camera_format_v4l2(video_device, fourcc, width, height): - """ - Set the camera device capture format using the external v4l2-ctl tool - """ - subprocess.run(['v4l2-ctl', '--device', F'{video_device}', - F'--set-fmt-video width={width},height={height},pixelformat={fourcc}'], text=True) - - -def set_camera_format_opencv(capture_device, fourcc, width, height): - """ - Set the camera device capture format using internal OpenCV set methods - """ - # VideoWriter_fourcc expects a list of characters, so need to unpack the string - capture_device.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*fourcc)) - capture_device.set(cv2.CAP_PROP_FRAME_WIDTH, int(width)) - capture_device.set(cv2.CAP_PROP_FRAME_HEIGHT, int(height)) - - -class USBCamPlugin(shepherd.plugin.Plugin): - @staticmethod - def define_config(confdef): - confdef.add_def('upload_images', shconf.BoolDef(default=False, optional=True, - helptext="If true, move to an Uploader bucket. Requires Uploader plugin")) - confdef.add_def('upload_bucket', shconf.StringDef(default="", optional=True, - helptext="Name of uploader bucket to shift images to.")) - confdef.add_def('save_directory', shconf.StringDef(default="", optional=True, - helptext="Name of directory path to save images. If empty, a 'usbcamera' directory under the Shepherd root dir will be used")) - confdef.add_def('append_id', shconf.BoolDef(default=True, optional=True, - helptext="If true, add the system ID to the end of image filenames")) - confdef.add_def('show_overlay', shconf.BoolDef(default=True, optional=True, - helptext="If true, add an overlay on each image with the system ID and date.")) - confdef.add_def('overlay_desc', shconf.StringDef(default="", optional=True, - helptext="Text to add to the overlay after the system ID and camera name")) - confdef.add_def('jpeg_quality', shconf.IntDef(default=85, minval=60, maxval=95, optional=True, - helptext="JPEG quality to save with. Max of 95, passed directly to Pillow")) - confdef.add_def('stabilise_delay', shconf.IntDef(default=5, minval=1, maxval=30, optional=True, - helptext="Number of seconds to wait after starting each camera for exposure and white balance to settle")) - - array = confdef.add_def('trigger', shconf.TableArrayDef( - helptext="Array of triggers that will use all cameras")) - array.add_def('hour', shconf.StringDef()) - array.add_def('minute', shconf.StringDef()) - array.add_def('second', shconf.StringDef(default="0", optional=True)) - - camarray = confdef.add_def('camera', shconf.TableArrayDef( - helptext="List of cameras to try and connect to. Multiple ports may be listed, and any not connected will be skipped on each trigger.")) - camarray.add_def('name', shconf.StringDef(default="", optional=False, - helptext="Name of camera, appended to filename and added to overlay")) - camarray.add_def('usb_port', shconf.StringDef(default="*", optional=False, - helptext="USB port descriptor of the from '3.4.1' (which would indicate port1 on a hub plugged into port4 on a hub plugged into port 3 of the system). This can be found by running 'v4l2-ctl --list-devices'. A single camera with a wildcard '*' port is also allowed, and will match any remaining available camera.")) - - def __init__(self, pluginInterface, config): - super().__init__(pluginInterface, config) - self.config = config - self.interface = pluginInterface - self.plugins = pluginInterface.other_plugins - self.hooks = pluginInterface.hooks - - self.root_dir = os.path.expanduser(pluginInterface.coreconfig["root_dir"]) - self.id = pluginInterface.coreconfig["id"] - - self.interface.register_hook("pre_cam") - self.interface.register_hook("post_cam") - self.interface.register_function(self.camera_job) - self.interface.register_function(self.run_cameras) - # do some camera init stuff - - print("USBCamera config:") - print(self.config) - - self.gstlock = threading.Lock() - - if self.config["save_directory"] is "": - self.save_directory = os.path.join(self.root_dir, "usbcamera") - else: - self.save_directory = self.config["save_directory"] - - if not os.path.exists(self.save_directory): - os.makedirs(self.save_directory) - - if self.config["show_overlay"]: - # Load assets - self.logo_im = Image.open(logo_filename) - - self.font_size_cache = {} - self.logo_size_cache = {} - - # Dict of camera names storing the USB path as the value - self.defined_cams = OrderedDict() - # List of wildcard camera names - self.wildcard_cams = [] - - # Go through camera configs sorted by name - for camera in sorted(self.config["camera"], key=itemgetter("name")): - if camera["name"] in self.defined_cams: - raise shconf.InvalidConfigError( - "Can't have more than one usb camera defined with the same config name") - if camera["usb_port"] == '*': - self.wildcard_cams.append(camera["name"]) - else: - self.defined_cams[camera["name"]] = camera["usb_port"] - - for trigger in self.config["trigger"]: - trigger_id = trigger["hour"]+'-' + trigger["minute"]+'-'+trigger["second"] - self.interface.add_job( - self.camera_job, trigger["hour"], trigger["minute"], trigger["second"], job_name=trigger_id) - - def _generate_overlay(self, width, height, image_time, camera_name): - font_size = int(height/40) - margin_size = int(font_size/5) - - if font_size not in self.font_size_cache: - self.font_size_cache[font_size] = ImageFont.truetype( - overlayfont_filename, int(font_size*0.9)) - thisfont = self.font_size_cache[font_size] - - if font_size not in self.logo_size_cache: - newsize = (int(self.logo_im.width*( - font_size/self.logo_im.height)), font_size) - self.logo_size_cache[font_size] = self.logo_im.resize( - newsize, Image.BILINEAR) - thislogo = self.logo_size_cache[font_size] - - desc_text = camera_name + " " + self.config["overlay_desc"] - if self.config["append_id"]: - desc_text = self.id + " " + desc_text - - time_text = image_time.strftime("%Y-%m-%d %H:%M:%S") - - overlay = Image.new('RGBA', (width, font_size+(2*margin_size)), (0, 0, 0)) - overlay.paste(thislogo, (int((overlay.width-thislogo.width)/2), margin_size)) - - draw = ImageDraw.Draw(overlay) - draw.text((margin_size*2, margin_size), desc_text, - font=thisfont, fill=(255, 255, 255, 255)) - - datewidth, _ = draw.textsize(time_text, thisfont) - draw.text((overlay.width-(margin_size*2)-datewidth, margin_size), time_text, font=thisfont, - fill=(255, 255, 255, 255)) - - # make whole overlay half transparent - overlay.putalpha(128) - return overlay - - def _process_image(self, cv_frame, camera_name): - - image_time = datetime.now() - - # Convert over to PIL. Mostly so we can use our own font. - img = Image.fromarray(cv2.cvtColor(cv_frame, cv2.COLOR_BGR2RGB)) - - if self.config["show_overlay"]: - overlay = self._generate_overlay(img.width, img.height, image_time, camera_name) - img.paste(overlay, (0, img.height-overlay.height), overlay) - - image_filename = image_time.strftime("%Y-%m-%d %H-%M-%S") - if self.config["append_id"]: - image_filename = image_filename + " " + self.id - - if camera_name is not "": - image_filename = image_filename+" "+camera_name - image_filename = image_filename + ".jpg" - image_filename = os.path.join(self.save_directory, image_filename) - img.save(image_filename+".writing", "JPEG", quality=self.config["jpeg_quality"]) - os.rename(image_filename+".writing", image_filename) - - if self.config["upload_images"]: - self.plugins["uploader"].move_to_bucket(image_filename, self.config["upload_bucket"]) - - def _capture_image(self, device_path, camera_name): - print("Running camera "+camera_name) - - with self.gstlock: - - #gst_str = ('v4l2src device='+device_path+' ! ' - # 'videoconvert ! appsink drop=true max-buffers=1 sync=false') - - #vidcap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) - - fmts = get_capture_formats(device_path) - - with VideoCaptureCtx(device_path, cv2.CAP_V4L2) as vidcap: - if "MJPG" in fmts: - size = get_largest_resolution(fmts["MJPG"]) - set_camera_format_opencv(vidcap, "MJPG", size[0], size[1]) - - - # stream only starts after first grab - - print("Starting cam") - read_flag, frame = vidcap.read() - delay_start = time.time() - while (time.time() - delay_start) < self.config["stabilise_delay"]: - vidcap.grab() - #time.sleep(self.config["stabilise_delay"]) - # clear old buffer - #print("Flushing capture") - #vidcap.grab() - print("Reading") - read_flag, frame = vidcap.read() - #print("Changing to YUYV") - #if "YUYV" in fmts: - # size = get_largest_resolution(fmts["YUYV"]) - # set_camera_format_opencv(vidcap, "YUYV", size[0], size[1]) - #print("Reading again") - #read_flag, frame2 = vidcap.read() - - - if read_flag: - self._process_image(frame, camera_name) - #self._process_image(frame2, camera_name+"(2)") - else: - print("Could not read camera "+camera_name + - " on USB port "+device_path) - - def run_cameras(self, name_suffix = ""): - connected_cams = OrderedDict(get_connected_cameras()) - - for defined_name, defined_usb_path in self.defined_cams.items(): - if defined_usb_path in connected_cams: - - self._capture_image(connected_cams.pop( - defined_usb_path), defined_name+name_suffix) - - else: - print("USB Camera "+defined_name+" on port " + - defined_usb_path+" is not currently connected") - - for cam_name in self.wildcard_cams: - if len(connected_cams) > 0: - self._capture_image(connected_cams.popitem( - last=False)[1], cam_name+name_suffix) - else: - print( - "No connected USB cameras are currently left to match to "+cam_name+" ") - break - - def camera_job(self): - self.hooks.pre_cam() - self.run_cameras() - self.hooks.post_cam() - - -if __name__ == "__main__": - pass - # print("main") - # main(sys.argv[1:]) diff --git a/shepherd/smallshepherd.png b/shepherd/smallshepherd.png deleted file mode 100644 index 0b12a6e..0000000 Binary files a/shepherd/smallshepherd.png and /dev/null differ diff --git a/test.toml b/test.toml deleted file mode 100644 index 6c65bf8..0000000 --- a/test.toml +++ /dev/null @@ -1,13 +0,0 @@ -[shepherd] - name = "test-node" - plugin_dir = "./" - root_dir = "~/shepherd/" - hostname = "shepherd-test" - control_server = "api.shepherd.distreon.net" - #control_server = "127.0.0.1:5000" - control_api_key = "v2EgvYzx79c8fCP4P7jlWxTZ3pc" -[scout] - boardver = "3" - serialport = "/dev/ttyUSB0" - - diff --git a/tests/test_config.py b/tests/test_config.py deleted file mode 100644 index be4e3f7..0000000 --- a/tests/test_config.py +++ /dev/null @@ -1,18 +0,0 @@ -import shepherd.config as config - -def test_freeze(): - confdef = config.ConfDefinition() - conf_def_dict = confdef.add_def('dictval', config.DictDef()) - conf_def_dict.add_def('intval', config.IntDef()) - conf_def_dict.add_def('strtval', config.StringDef()) - - confman = config.ConfigManager() - confman.add_confdef("test_bundle",confdef) - confman.load({"test_bundle": {'dictval': {'intval': 34, 'strval': "a"}}}) - - confman.freeze_value("test_bundle","dictval", "intval") - - confman.load({"test_bundle": {'dictval': {'intval': 34, 'strval': "b"}}}) - breakpoint() - print(confman.root_config) -