├── .github ├── dependabot.yml └── workflows │ ├── maven-publish.yml │ └── maven.yml ├── .gitignore ├── 20mm_Box.gx ├── LICENSE ├── Readme.md ├── pom.xml └── src ├── main ├── java │ ├── de │ │ └── slg │ │ │ └── ddnss │ │ │ └── printertool │ │ │ ├── clients │ │ │ ├── AdventurerClient.java │ │ │ ├── AdventurerCommands.java │ │ │ ├── FlashForgeDiscoverClient.java │ │ │ ├── PrinterInfo.java │ │ │ ├── PrinterStatus.java │ │ │ ├── TcpPrinterClient.java │ │ │ └── UdpDiscoveryClient.java │ │ │ ├── exceptions │ │ │ ├── FlashForgePrinterException.java │ │ │ └── FlashForgePrinterTransferException.java │ │ │ └── util │ │ │ └── Util.java │ └── org │ │ └── apache │ │ └── commons │ │ └── codec │ │ ├── BinaryDecoder.java │ │ ├── BinaryEncoder.java │ │ ├── CharEncoding.java │ │ ├── Decoder.java │ │ ├── DecoderException.java │ │ ├── Encoder.java │ │ ├── EncoderException.java │ │ ├── StringDecoder.java │ │ ├── StringEncoder.java │ │ └── binary │ │ └── Hex.java └── resources │ └── logback.xml └── test ├── java └── de │ └── slg │ └── ddnss │ └── printertool │ └── clients │ ├── AdventurerClientTest.java │ ├── ByteCRC32.java │ ├── FlashForgeDiscoverClientTest.java │ ├── JUnit5TestSuite.java │ ├── TcpClientTest.java │ └── UpdClientTest.java └── resources ├── 20mm_Box.gx └── logback.xml /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "maven" 9 | directory: "/" 10 | schedule: 11 | interval: "weekly" 12 | 13 | - package-ecosystem: "github-actions" 14 | directory: "`/.github/workflows" 15 | schedule: 16 | interval: "daily" 17 | -------------------------------------------------------------------------------- /.github/workflows/maven-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a package using Maven and then publish it to GitHub packages when a release is created 2 | # For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path 3 | 4 | name: Maven Package 5 | 6 | on: 7 | release: 8 | types: [created] 9 | 10 | jobs: 11 | build: 12 | 13 | runs-on: ubuntu-latest 14 | permissions: 15 | contents: read 16 | packages: write 17 | 18 | steps: 19 | - uses: actions/checkout@v3 20 | - name: Set up JDK 17 21 | uses: actions/setup-java@v3 22 | with: 23 | java-version: '17' 24 | distribution: 'temurin' 25 | server-id: github # Value of the distributionManagement/repository/id field of the pom.xml 26 | settings-path: ${{ github.workspace }} # location for the settings.xml file 27 | 28 | - name: Build with Maven 29 | run: mvn -B package --file pom.xml 30 | 31 | - name: Publish to GitHub Packages Apache Maven 32 | run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml 33 | env: 34 | GITHUB_TOKEN: ${{ github.token }} 35 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven 3 | 4 | # This workflow uses actions that are not certified by GitHub. 5 | # They are provided by a third-party and are governed by 6 | # separate terms of service, privacy policy, and support 7 | # documentation. 8 | 9 | name: Java CI with Maven 10 | 11 | on: 12 | push: 13 | branches: [ "main" ] 14 | pull_request: 15 | branches: [ "main" ] 16 | 17 | jobs: 18 | build: 19 | 20 | runs-on: ubuntu-latest 21 | 22 | steps: 23 | - uses: actions/checkout@v3 24 | - name: Set up JDK 17 25 | uses: actions/setup-java@v3 26 | with: 27 | java-version: '17' 28 | distribution: 'temurin' 29 | cache: maven 30 | - name: Build with Maven 31 | run: mvn -B package --file pom.xml 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Java 2 | *.class 3 | *.jar 4 | *.war 5 | *.ear 6 | 7 | # Eclipse 8 | .project 9 | .classpath 10 | .settings 11 | 12 | # Idea 13 | .idea 14 | *.iml 15 | *.iws 16 | *.ipr 17 | 18 | # OS 19 | Thumbs.db 20 | .DS_Store 21 | 22 | # Gradle 23 | .gradle 24 | !gradle-wrapper.jar 25 | 26 | # Maven 27 | /target** 28 | 29 | # Build 30 | out 31 | build 32 | bin 33 | 34 | # Other 35 | *.log 36 | *.swp 37 | *.bak 38 | 39 | # dBeaver 40 | credentials-config.json 41 | data-sources.json 42 | project-metadata.json 43 | 44 | # Node/npm 45 | node 46 | node_modules 47 | 48 | package-lock.json 49 | package.json 50 | /webpack.config.js 51 | /webpack.generated.js 52 | -------------------------------------------------------------------------------- /20mm_Box.gx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Slugger2k/FlashForgePrinterApi/6a30121df522d5054838f0c0f876fd4ffcfb8327/20mm_Box.gx -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # FlashForge Adventurer 3 Printer API 3 | This library enables you to communicate with your FlashForge Adventurer 3 printer and maybe other 4 | models from FlashForge. It is completely written in Java and communicates with the printer 5 | via raw TCP. 6 | You can use this library to build your own monitoring or printing tools. 7 | 8 | The library find's your printer in the network with a UDP broadcast message which is send when you 9 | instantiate the UpdDiscoveryClient class. 10 | 11 | Broadcast: c0a800de46500000 => 16Bytes to 225.0.0.9:19000 12 | Replay from 192.168.0.247: My 3D Printer 13 | 14 | 15 | 16 | 17 | ## The following commands are implemented so far: 18 | 19 | - HELLO = M601 S1 20 | 21 | CMD M601 Received. 22 | Control Success. 23 | ok 24 | 25 | - BYE = M602 26 | 27 | CMD M602 Received. 28 | Control Release. 29 | ok 30 | 31 | - INFO = 115 32 | 33 | CMD M115 Received. 34 | Machine Type: FlashForge Adventurer III 35 | Machine Name: My 3D Printer 36 | Firmware: v1.1.7 37 | SN: XXXXXXXXXXXX 38 | X: 150 Y: 150 Z: 150 39 | Tool Count: 1 40 | Mac Address: 88:A9:A7:XX:XX:XX 41 | ok 42 | 43 | - INFO_STATUS = M119 44 | 45 | CMD M119 Received. 46 | Endstop: X-max:1 Y-max:0 Z-max:0 47 | MachineStatus: READY 48 | MoveMode: READY 49 | Status: S:1 L:0 J:0 F:0 50 | ok 51 | 52 | - INFO_XYZAB = M114 53 | 54 | CMD M114 Received. 55 | X:0 Y:0 Z:0 A:0 B:0 56 | ok 57 | 58 | - INFO_CAL = M650 59 | 60 | CMD M650 Received. 61 | X: 1.0 Y: 0.5 62 | ok 63 | 64 | - TEMP = M105 65 | 66 | CMD M105 Received. 67 | T0:22 /0 B:11/0 68 | ok 69 | 70 | - LED_ON = M146 r255 g255 b255 F0 71 | 72 | CMD M146 Received. 73 | ok 74 | 75 | - LED_OFF = M146 r0 g0 b0 F0 76 | 77 | CMD M146 Received. 78 | ok 79 | 80 | - PRINT_STATUS = M27 81 | 82 | CMD M27 Received. 83 | SD printing byte 0/100 84 | ok 85 | 86 | - SAVE_FILE = M29 87 | 88 | CMD M29 Received. 89 | Done saving file. 90 | ok 91 | 92 | - PREPARE_PRINT = M28 size filename 93 | 94 | CMD M28 Received. 95 | Writing to file: 0:/user/Snake.gx 96 | ok 97 | Send data: 4112 bytes 98 | N0000 ok. 99 | Send data: 4112 bytes 100 | N0001 ok. 101 | Send data: 4112 bytes 102 | N0002 ok. 103 | Send data: 4112 bytes 104 | N0003 ok. 105 | . 106 | . 107 | . 108 | 109 | - PRINT_START = M23 filename 110 | 111 | CMD M23 Received. 112 | File opened: 20mm_Box.gx Size: 154674 113 | File selected 114 | ok 115 | 116 | - PRINT_STOP = M26 117 | 118 | CMD M26 Received. 119 | ok 120 | 121 | 122 | 123 | ## Example: 124 | 125 | AdventurerClient client = new AdventurerClient(new FlashForgeDiscoverClient().getPrinterAddress()); 126 | client.print(Paths.get("/home/slg/eclipse-workspace-2020/printertool/20mm_Box.gx")); 127 | 128 | 129 | 130 | ### Sequenz to transfer a file to the printer and start the print. 131 | ```mermaid 132 | graph LR 133 | A[PREPARE_PRINT] --> B[SAVE_FILE] 134 | B[SAVE_FILE] --> C[PRINT] 135 | ``` 136 | 137 | ## Transfer data packet header 138 | 139 | For transferring data to the printer you have to split the packets in 4096 byte pieces and build the CRC32 checksum from it. 140 | You also have to count the real length of the packet because of the last packet need to know its size, a packet counter is also needed. 141 | 142 | 143 | #### Header: 144 | 145 | | | header | counter | length | CRC32 | 146 | |----------|----------|----------|----------|----------| 147 | | packet | 5a5aa5a5 | 0000026d | 00000b7e | 66d6707b | 148 | | ASCII | ZZ¥¥ | 621 | 2942 | | 149 | 150 | 151 | 152 | ## FAQ 153 | 154 | - There is currently only logging to sysout implemented, but it's easy to replace because 155 | there are only System.out.println() calls for info logging and in Exceptions e.printStackTrace() calls 156 | for error logging. 157 | 158 | Working in progress... 159 | 160 | if you have any questions please drop me a mail `slg at slg.ddnss.de` 161 | 162 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | de.slg.ddnss.flashforge 6 | printer-api 7 | 0.0.5 8 | jar 9 | 10 | 11 | UTF-8 12 | 17 13 | 17 14 | 15 | 16 | 17 | 18 | 19 | ch.qos.logback 20 | logback-classic 21 | 1.5.10 22 | 23 | 24 | org.slf4j 25 | slf4j-api 26 | 2.0.16 27 | 28 | 29 | org.junit.jupiter 30 | junit-jupiter 31 | 5.11.2 32 | test 33 | 34 | 35 | org.junit.jupiter 36 | junit-jupiter-engine 37 | 5.11.2 38 | test 39 | 40 | 41 | org.junit.platform 42 | junit-platform-runner 43 | 1.11.2 44 | test 45 | 46 | 47 | FlashForgePrinterApi 48 | This library enables you to communicate with your FlashForge Adventurer 3 printer and maybe other 49 | printers from FlashForge. It is completely written in Java and communicates via TCP with the printer. 50 | You can use this library to build you own monitoring or printing tools. 51 | https://slg.ddnss.de 52 | 53 | Christian Müller 54 | https://slg.ddnss.de 55 | 56 | 57 | Christian Müller 58 | https://slg.ddnss.de 59 | 60 | 61 | 62 | github 63 | GitHub Packages 64 | https://maven.pkg.github.com/slugger2k/FlashForgePrinterApi 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/clients/AdventurerClient.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException; 4 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterTransferException; 5 | import de.slg.ddnss.printertool.util.Util; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import java.util.List; 10 | 11 | import static de.slg.ddnss.printertool.clients.AdventurerCommands.*; 12 | 13 | 14 | public class AdventurerClient extends TcpPrinterClient { 15 | 16 | private final static Logger log = LoggerFactory.getLogger(AdventurerClient.class); 17 | 18 | public AdventurerClient(String hostname) { 19 | super(hostname); 20 | } 21 | 22 | public boolean print(String filename, byte[] readAllLines) throws FlashForgePrinterException { 23 | log.info("File: {}/{} byte", filename, readAllLines.length); 24 | 25 | log.info(sendCommand(CMD_PREPARE_PRINT.replaceAll("%%size%%", "" + readAllLines.length) 26 | .replaceAll("%%filename%%", filename))); 27 | 28 | try { 29 | List gcode = Util.prepareRawData(readAllLines); 30 | sendRawData(gcode); 31 | log.info(sendCommand(CMD_SAVE_FILE)); 32 | log.info(sendCommand(CMD_PRINT_START.replaceAll("%%filename%%", filename))); 33 | return true; 34 | } catch (FlashForgePrinterTransferException e) { 35 | log.error(e.getMessage(), e); 36 | } 37 | 38 | return false; 39 | } 40 | 41 | public boolean setLed(boolean on) throws FlashForgePrinterException { 42 | String replay = sendCommand(on ? CMD_LED_ON : CMD_LED_OFF); 43 | log.info(replay); 44 | return replay.contentEquals("CMD M146 Received.\nok"); 45 | } 46 | 47 | public boolean stopPrinting() throws FlashForgePrinterException { 48 | String replay = sendCommand(CMD_PRINT_STOP); 49 | log.info(replay); 50 | return replay.trim().contentEquals("CMD M26 Received.\nok"); 51 | } 52 | 53 | public PrinterInfo getPrinterInfo() throws FlashForgePrinterException { 54 | String replay = sendCommand(CMD_INFO_STATUS).trim(); 55 | return new PrinterInfo(replay); 56 | } 57 | 58 | public PrinterStatus getPrinterStatus() throws FlashForgePrinterException { 59 | String replay = sendCommand(CMD_INFO).trim(); 60 | return new PrinterStatus(replay); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/clients/AdventurerCommands.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | public class AdventurerCommands { 4 | 5 | /** 6 | * Commands for FlashForge Adventurer 3 Printer, 7 | * maybe there work on other models, but i only have access to a Adventurer 3. 8 | * 9 | * IDLE-SEQUENZ: M601 S1, M115, M650, M115, M114, M27, M119, M105 10 | * PRINT-SEQUNEZ: M28 size filename, Send Data with header, M29, M23 filename 11 | */ 12 | public static final String CMD_HELLO = "~M601 S1"; 13 | public static final String CMD_BYE = "~M602"; 14 | public static final String CMD_INFO = "~M119"; 15 | public static final String CMD_INFO_STATUS = "~M115"; 16 | public static final String CMD_INFO_XYZAB = "~M114"; 17 | public static final String CMD_INFO_CAL = "~M650"; 18 | public static final String CMD_TEMP = "~M105"; 19 | public static final String CMD_LED_ON = "~M146 r255 g255 b255 F0"; 20 | public static final String CMD_LED_OFF = "~M146 r0 g0 b0 F0"; 21 | public static final String CMD_PRINT_STATUS = "~M27"; 22 | public static final String CMD_SAVE_FILE = "~M29\r"; 23 | public static final String CMD_PREPARE_PRINT = "~M28 %%size%% 0:/user/%%filename%%\r"; 24 | public static final String CMD_PRINT_START = "~M23 0:/user/%%filename%%\r"; 25 | public static final String CMD_PRINT_STOP = "~M26\r"; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/clients/FlashForgeDiscoverClient.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException; 4 | 5 | import java.net.DatagramPacket; 6 | 7 | public class FlashForgeDiscoverClient extends UdpDiscoveryClient { 8 | 9 | private final String discoveryPayload = "c0a800de46500000"; 10 | private DatagramPacket receiveMessage; 11 | 12 | public FlashForgeDiscoverClient() throws FlashForgePrinterException { 13 | super(); 14 | } 15 | 16 | public FlashForgeDiscoverClient discoverPrinter() throws FlashForgePrinterException { 17 | receiveMessage = sendMessage(discoveryPayload); 18 | return this; 19 | } 20 | 21 | public String getPrinterAddress() { 22 | return receiveMessage.getAddress().getHostAddress(); 23 | } 24 | 25 | public String getPrinterName() { 26 | return new String(receiveMessage.getData()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/clients/PrinterInfo.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | 4 | public class PrinterInfo { 5 | 6 | private String mashineType; 7 | private String mashineName; 8 | private String firmwareVersion; 9 | private String seriesNr; 10 | private String dimesions; 11 | private int toolCount; 12 | private String mac; 13 | 14 | 15 | public PrinterInfo() { 16 | } 17 | 18 | /* 19 | * parses a string like: 20 | * 21 | * Machine Type: FlashForge Adventurer III\n 22 | * Machine Name: My 3D Printer\n 23 | * Firmware: v1.1.7\n 24 | * SN: SNFFAD229083\n 25 | * X: 150 Y: 150 Z: 150\n 26 | * Tool Count: 1\n 27 | * Mac Address: 88:A9:A7:90:77:A5\n 28 | * \n 29 | * ok 30 | */ 31 | public PrinterInfo(String replay) { 32 | String[] split = replay.split("\\n"); 33 | setMashineType(split[1].split(":")[1].trim()); 34 | setMashineName(split[2].split(":")[1].trim()); 35 | setFirmwareVersion(split[3].split(":")[1].trim()); 36 | setSeriesNr(split[4].split(":")[1].trim()); 37 | setDimesions(split[5].trim()); 38 | setToolCount(Integer.valueOf(split[6].split(":")[1].trim())); 39 | setMac(split[7].trim()); 40 | } 41 | 42 | public String getMashineType() { 43 | return mashineType; 44 | } 45 | public void setMashineType(String mashineType) { 46 | this.mashineType = mashineType; 47 | } 48 | public String getMashineName() { 49 | return mashineName; 50 | } 51 | public void setMashineName(String mashineName) { 52 | this.mashineName = mashineName; 53 | } 54 | public String getFirmwareVersion() { 55 | return firmwareVersion; 56 | } 57 | public void setFirmwareVersion(String firmwareVersion) { 58 | this.firmwareVersion = firmwareVersion; 59 | } 60 | public String getSeriesNr() { 61 | return seriesNr; 62 | } 63 | public void setSeriesNr(String seriesNr) { 64 | this.seriesNr = seriesNr; 65 | } 66 | public String getDimesions() { 67 | return dimesions; 68 | } 69 | public void setDimesions(String dimesions) { 70 | this.dimesions = dimesions; 71 | } 72 | public int getToolCount() { 73 | return toolCount; 74 | } 75 | public void setToolCount(int toolCount) { 76 | this.toolCount = toolCount; 77 | } 78 | public String getMac() { 79 | return mac; 80 | } 81 | public void setMac(String mac) { 82 | this.mac = mac; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "PrinterInfo [mashineType=" + mashineType + ", mashineName=" + mashineName + ", firmwareVersion=" 88 | + firmwareVersion + ", seriesNr=" + seriesNr + ", dimesions=" + dimesions + ", toolCount=" + toolCount 89 | + ", mac=" + mac + "]"; 90 | } 91 | 92 | @Override 93 | public int hashCode() { 94 | final int prime = 31; 95 | int result = 1; 96 | result = prime * result + ((dimesions == null) ? 0 : dimesions.hashCode()); 97 | result = prime * result + ((firmwareVersion == null) ? 0 : firmwareVersion.hashCode()); 98 | result = prime * result + ((mac == null) ? 0 : mac.hashCode()); 99 | result = prime * result + ((mashineName == null) ? 0 : mashineName.hashCode()); 100 | result = prime * result + ((mashineType == null) ? 0 : mashineType.hashCode()); 101 | result = prime * result + ((seriesNr == null) ? 0 : seriesNr.hashCode()); 102 | result = prime * result + toolCount; 103 | return result; 104 | } 105 | 106 | @Override 107 | public boolean equals(Object obj) { 108 | if (this == obj) 109 | return true; 110 | if (obj == null) 111 | return false; 112 | if (getClass() != obj.getClass()) 113 | return false; 114 | PrinterInfo other = (PrinterInfo) obj; 115 | if (dimesions == null) { 116 | if (other.dimesions != null) 117 | return false; 118 | } else if (!dimesions.equals(other.dimesions)) 119 | return false; 120 | if (firmwareVersion == null) { 121 | if (other.firmwareVersion != null) 122 | return false; 123 | } else if (!firmwareVersion.equals(other.firmwareVersion)) 124 | return false; 125 | if (mac == null) { 126 | if (other.mac != null) 127 | return false; 128 | } else if (!mac.equals(other.mac)) 129 | return false; 130 | if (mashineName == null) { 131 | if (other.mashineName != null) 132 | return false; 133 | } else if (!mashineName.equals(other.mashineName)) 134 | return false; 135 | if (mashineType == null) { 136 | if (other.mashineType != null) 137 | return false; 138 | } else if (!mashineType.equals(other.mashineType)) 139 | return false; 140 | if (seriesNr == null) { 141 | if (other.seriesNr != null) 142 | return false; 143 | } else if (!seriesNr.equals(other.seriesNr)) 144 | return false; 145 | if (toolCount != other.toolCount) 146 | return false; 147 | return true; 148 | } 149 | 150 | } 151 | -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/clients/PrinterStatus.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | public class PrinterStatus { 7 | 8 | private final static Logger log = LoggerFactory.getLogger(PrinterStatus.class); 9 | 10 | private String status; 11 | private int extruderTemp; 12 | private int bedTemp; 13 | private int x; 14 | private int y; 15 | private int z; 16 | 17 | public PrinterStatus() { 18 | } 19 | 20 | /* 21 | * 22 | * CMD M119 Received. 23 | * Endstop: X-max:1 Y-max:0 Z-max:0 24 | * MachineStatus: READY 25 | * MoveMode: READY 26 | * Status: S:1 L:0 J:0 F:0 27 | * ok 28 | */ 29 | public PrinterStatus(String replay) { 30 | log.info(replay); 31 | String[] status = replay.split("\\n"); 32 | setStatus(status[2].split(":")[1].trim()); 33 | } 34 | 35 | public String getStatus() { 36 | return status; 37 | } 38 | public void setStatus(String status) { 39 | this.status = status; 40 | } 41 | public int getExtruderTemp() { 42 | return extruderTemp; 43 | } 44 | public void setExtruderTemp(int extruderTemp) { 45 | this.extruderTemp = extruderTemp; 46 | } 47 | public int getBedTemp() { 48 | return bedTemp; 49 | } 50 | public void setBedTemp(int bedTemp) { 51 | this.bedTemp = bedTemp; 52 | } 53 | public int getX() { 54 | return x; 55 | } 56 | public void setX(int x) { 57 | this.x = x; 58 | } 59 | public int getY() { 60 | return y; 61 | } 62 | public void setY(int y) { 63 | this.y = y; 64 | } 65 | public int getZ() { 66 | return z; 67 | } 68 | public void setZ(int z) { 69 | this.z = z; 70 | } 71 | 72 | 73 | @Override 74 | public String toString() { 75 | return "PrinterStatus [status=" + status + ", extruderTemp=" + extruderTemp + ", bedTemp=" + bedTemp + ", x=" 76 | + x + ", y=" + y + ", z=" + z + "]"; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/clients/TcpPrinterClient.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException; 4 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterTransferException; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import java.io.*; 9 | import java.net.NoRouteToHostException; 10 | import java.net.Socket; 11 | import java.net.UnknownHostException; 12 | import java.util.List; 13 | 14 | public class TcpPrinterClient implements Closeable { 15 | 16 | private final static Logger log = LoggerFactory.getLogger(TcpPrinterClient.class); 17 | 18 | private Socket socket; 19 | private final int port = 8899; 20 | private final int timeout = 25000; 21 | private final String hostname; 22 | 23 | public TcpPrinterClient(String hostname) { 24 | this.hostname = hostname; 25 | } 26 | 27 | public String sendCommand(String cmd) throws FlashForgePrinterException { 28 | log.info("Send Command: " + cmd); 29 | try { 30 | checkSocket(); 31 | OutputStream output = socket.getOutputStream(); 32 | PrintWriter writer = new PrintWriter(output, true); 33 | writer.println(cmd); 34 | return receiveMultiLineReplay(socket); 35 | } catch (NoRouteToHostException e) { 36 | throw new FlashForgePrinterTransferException("Error while connecting. No route to host [" + socket.getInetAddress().getHostAddress() + "]."); 37 | } catch (UnknownHostException e) { 38 | throw new FlashForgePrinterTransferException("Error while connecting. Unknown host [" + socket.getInetAddress().getHostAddress() + "]."); 39 | } catch (IOException e) { 40 | throw new FlashForgePrinterException("Error while building or writing outputstream.", e); 41 | } 42 | } 43 | 44 | public void sendRawData(List rawData) throws FlashForgePrinterException { 45 | try { 46 | checkSocket(); 47 | DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 48 | for (byte[] bs : rawData) { 49 | log.info("Send data: " + bs.length + " bytes"); 50 | dos.write(bs); 51 | String replay = receiveSingleLineReplay(socket); 52 | log.info(replay); 53 | if (!replay.matches("N\\d{4,}\\sok.")) { 54 | throw new FlashForgePrinterTransferException("Error while transfering data."); 55 | } 56 | } 57 | } catch (NoRouteToHostException e) { 58 | throw new FlashForgePrinterTransferException("Error while connecting. No route to host [" + socket.getInetAddress().getHostAddress() + "]."); 59 | } catch (UnknownHostException e) { 60 | throw new FlashForgePrinterTransferException("Error while connecting. Unknown host [" + socket.getInetAddress().getHostAddress() + "]."); 61 | } catch (IOException e) { 62 | throw new FlashForgePrinterException("Error while building or writing outputstream.", e); 63 | } 64 | } 65 | 66 | private void checkSocket() throws IOException { 67 | if (socket == null || socket.isClosed()) { 68 | log.info("Socket null or closed"); 69 | socket = new Socket(hostname, port); 70 | socket.setSoTimeout(timeout); 71 | } 72 | } 73 | 74 | public String receiveMultiLineReplay(Socket socket) throws FlashForgePrinterException { 75 | var answer = new StringBuilder(); 76 | BufferedReader reader; 77 | try { 78 | InputStream input = socket.getInputStream(); 79 | reader = new BufferedReader(new InputStreamReader(input)); 80 | 81 | String line; 82 | while ((line = reader.readLine()) != null) { 83 | answer.append(line).append("\n"); 84 | if (line.equalsIgnoreCase("ok")) { 85 | break; 86 | } 87 | } 88 | } catch (IOException e) { 89 | throw new FlashForgePrinterException("Error while building or reading inputstream.", e); 90 | } 91 | 92 | return answer.toString().trim(); 93 | } 94 | 95 | public String receiveSingleLineReplay(Socket socket) throws FlashForgePrinterException { 96 | BufferedReader reader; 97 | try { 98 | InputStream input = socket.getInputStream(); 99 | reader = new BufferedReader(new InputStreamReader(input)); 100 | 101 | String line = reader.readLine(); 102 | return line.trim(); 103 | } catch (IOException e) { 104 | throw new FlashForgePrinterException("Error while building or reading inputstream.", e); 105 | } 106 | } 107 | 108 | @Override 109 | public void close() { 110 | try { 111 | socket.close(); 112 | } catch (Exception e) { 113 | log.error(e.getMessage(), e); 114 | } 115 | } 116 | } -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/clients/UdpDiscoveryClient.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import java.io.IOException; 8 | import java.net.DatagramPacket; 9 | import java.net.DatagramSocket; 10 | import java.net.InetAddress; 11 | import java.net.UnknownHostException; 12 | 13 | public class UdpDiscoveryClient { 14 | 15 | private final static Logger log = LoggerFactory.getLogger(UdpDiscoveryClient.class); 16 | 17 | private static final String HOSTNAME = "225.0.0.9"; 18 | private final InetAddress address; 19 | private static final int PORT = 19000; 20 | private static final int TIMEOUT = 1000; 21 | private DatagramSocket socket; 22 | private byte[] buf; 23 | 24 | public UdpDiscoveryClient() throws FlashForgePrinterException { 25 | try { 26 | address = InetAddress.getByName(HOSTNAME); 27 | } catch (UnknownHostException e) { 28 | throw new FlashForgePrinterException("UnknownHostException for host: " + HOSTNAME, e); 29 | } 30 | } 31 | 32 | public DatagramPacket sendMessage(String msg) throws FlashForgePrinterException { 33 | try { 34 | buf = msg.getBytes(); 35 | DatagramPacket packet = new DatagramPacket(buf, buf.length, address, PORT); 36 | socket = new DatagramSocket(); 37 | socket.setSoTimeout(TIMEOUT); 38 | log.info("Broadcast: " + msg + " => " + buf.length + "Bytes to " + address.getHostAddress() + ":" + PORT); 39 | socket.send(packet); 40 | return receiveMessage(); 41 | } catch (IOException e) { 42 | throw new FlashForgePrinterException("Error while send or receive broadcast.", e); 43 | } 44 | } 45 | 46 | private DatagramPacket receiveMessage() throws IOException { 47 | DatagramPacket packet = new DatagramPacket(buf, buf.length); 48 | socket.receive(packet); 49 | log.info("Replay from " + packet.getAddress().getHostAddress() + ": " + new String(packet.getData())); 50 | socket.close(); 51 | return packet; 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/exceptions/FlashForgePrinterException.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.exceptions; 2 | 3 | public class FlashForgePrinterException extends Exception { 4 | 5 | /** 6 | * 7 | */ 8 | private static final long serialVersionUID = 1L; 9 | 10 | public FlashForgePrinterException(String message) { 11 | super(message); 12 | } 13 | 14 | public FlashForgePrinterException(String message, Throwable e) { 15 | super(message, e); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/exceptions/FlashForgePrinterTransferException.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.exceptions; 2 | 3 | public class FlashForgePrinterTransferException extends FlashForgePrinterException { 4 | 5 | /** 6 | * 7 | */ 8 | private static final long serialVersionUID = 1L; 9 | 10 | public FlashForgePrinterTransferException(String message) { 11 | super(message); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/de/slg/ddnss/printertool/util/Util.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.ArrayList; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | import java.util.zip.CRC32; 8 | import java.util.zip.Checksum; 9 | 10 | import org.apache.commons.codec.DecoderException; 11 | import org.apache.commons.codec.binary.Hex; 12 | 13 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterTransferException; 14 | 15 | public class Util { 16 | 17 | // Packet-length = 4112 = 4096 + 16 18 | private static final int PACKETSIZE = 4096; 19 | 20 | public static byte[] concatenateArrays(byte[] a, byte[] b) { 21 | byte[] concat = new byte[a.length + b.length]; 22 | 23 | int i = 0; 24 | for (byte aa : a) { 25 | concat[i] = aa; 26 | i++; 27 | } 28 | 29 | for (byte bb : b) { 30 | concat[i] = bb; 31 | i++; 32 | } 33 | return concat; 34 | } 35 | 36 | /** 37 | * generate the header of each packageBundle(4096) with a counter, a CRC32 checksum and packetsize 38 | * 39 | * @param packetBundleCounter 40 | * @param crc32Checksum 41 | * @param lastPackage 42 | * @return 43 | * @throws FlashForgePrinterTransferException 44 | * @throws DecoderException 45 | */ 46 | private static byte[] generatePrintPrePayload(int packetBundleCounter, int packetSize, String crc32Checksum, boolean lastPackage) throws FlashForgePrinterTransferException { 47 | final byte[] printPrePayload = { (byte) 0x5a, (byte) 0x5a, (byte) 0xa5, (byte) 0xa5, // ZZ¥¥ 48 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // 4 bytes counter 49 | (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x00, // packetSize full = 0x00 0x00 0x10 0x00 = 4096 50 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 // 4 bytes CRC32 checksum 51 | }; 52 | 53 | addPacketBundleCounter(packetBundleCounter, printPrePayload); 54 | addPacketSize(packetSize, printPrePayload); 55 | addChecksum(crc32Checksum, printPrePayload); 56 | 57 | return printPrePayload; 58 | } 59 | 60 | public static void addPacketBundleCounter(int packetBundleCounter, byte[] printPrePayload) { 61 | byte[] packetBundleCounterArray = ByteBuffer.allocate(4).putInt(packetBundleCounter).array(); 62 | printPrePayload[4] = packetBundleCounterArray[0]; 63 | printPrePayload[5] = packetBundleCounterArray[1]; 64 | printPrePayload[6] = packetBundleCounterArray[2]; 65 | printPrePayload[7] = packetBundleCounterArray[3]; 66 | } 67 | 68 | public static void addPacketSize(int packetSize, byte[] printPrePayload) { 69 | byte[] packetSizeByteArray = ByteBuffer.allocate(4).putInt(packetSize).array(); 70 | printPrePayload[8] = packetSizeByteArray[0]; 71 | printPrePayload[9] = packetSizeByteArray[1]; 72 | printPrePayload[10] = packetSizeByteArray[2]; 73 | printPrePayload[11] = packetSizeByteArray[3]; 74 | } 75 | 76 | public static void addChecksum(String crc32Checksum, byte[] printPrePayload) throws FlashForgePrinterTransferException { 77 | try { 78 | byte[] decodeHex = null; 79 | decodeHex = Hex.decodeHex("00000000".substring(crc32Checksum.length()) + crc32Checksum); 80 | printPrePayload[15] = decodeHex[3]; 81 | printPrePayload[14] = decodeHex[2]; 82 | printPrePayload[13] = decodeHex[1]; 83 | printPrePayload[12] = decodeHex[0]; 84 | } catch (Exception e) { 85 | throw new FlashForgePrinterTransferException("Error while decoding String checksum to byte array!"); 86 | } 87 | } 88 | 89 | public static String calcChecksum(byte[] fileToPrintArray) { 90 | Checksum checksum = new CRC32(); 91 | checksum.update(fileToPrintArray, 0, fileToPrintArray.length); 92 | long checksumValue = checksum.getValue(); 93 | String crc32Checksum = Long.toHexString(checksumValue); 94 | return crc32Checksum; 95 | } 96 | 97 | public static List prepareRawData(byte[] readAllLines) throws FlashForgePrinterTransferException { 98 | List gcode = new ArrayList(); 99 | 100 | byte[] array = null; 101 | int i = 0; 102 | int packetCounter = 0; 103 | 104 | for (byte b : readAllLines) { 105 | 106 | if (i == 0) { 107 | array = new byte[PACKETSIZE]; 108 | } 109 | 110 | array[i] = b; 111 | 112 | i++; 113 | if (i == PACKETSIZE) { 114 | String crc32Checksum = calcChecksum(array); 115 | array = Util.concatenateArrays(generatePrintPrePayload(packetCounter, i, crc32Checksum, false), array); 116 | packetCounter++; 117 | gcode.add(array); 118 | i = 0; 119 | } 120 | } 121 | String crc32Checksum = calcChecksum(Arrays.copyOfRange(array, 0, i)); 122 | array = Util.concatenateArrays(generatePrintPrePayload(packetCounter, i, crc32Checksum, true), array); 123 | gcode.add(array); 124 | return gcode; 125 | } 126 | } -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/BinaryDecoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Defines common decoding methods for byte array decoders. 22 | * 23 | */ 24 | public interface BinaryDecoder extends Decoder { 25 | 26 | /** 27 | * Decodes a byte array and returns the results as a byte array. 28 | * 29 | * @param source 30 | * A byte array which has been encoded with the appropriate encoder 31 | * @return a byte array that contains decoded content 32 | * @throws DecoderException 33 | * A decoder exception is thrown if a Decoder encounters a failure condition during the decode process. 34 | */ 35 | byte[] decode(byte[] source) throws DecoderException; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/BinaryEncoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Defines common encoding methods for byte array encoders. 22 | * 23 | */ 24 | public interface BinaryEncoder extends Encoder { 25 | 26 | /** 27 | * Encodes a byte array and return the encoded data as a byte array. 28 | * 29 | * @param source 30 | * Data to be encoded 31 | * @return A byte array containing the encoded data 32 | * @throws EncoderException 33 | * thrown if the Encoder encounters a failure condition during the encoding process. 34 | */ 35 | byte[] encode(byte[] source) throws EncoderException; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/CharEncoding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Character encoding names required of every implementation of the Java platform. 22 | * 23 | * From the Java documentation Standard charsets: 25 | *

26 | * Every implementation of the Java platform is required to support the following character encodings. Consult the 27 | * release documentation for your implementation to see if any other encodings are supported. Consult the release 28 | * documentation for your implementation to see if any other encodings are supported. 29 | *

30 | * 31 | *
    32 | *
  • {@code US-ASCII}

    33 | * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.

  • 34 | *
  • {@code ISO-8859-1}

    35 | * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.

  • 36 | *
  • {@code UTF-8}

    37 | * Eight-bit Unicode Transformation Format.

  • 38 | *
  • {@code UTF-16BE}

    39 | * Sixteen-bit Unicode Transformation Format, big-endian byte order.

  • 40 | *
  • {@code UTF-16LE}

    41 | * Sixteen-bit Unicode Transformation Format, little-endian byte order.

  • 42 | *
  • {@code UTF-16}

    43 | * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order 44 | * accepted on input, big-endian used on output.)

  • 45 | *
46 | * 47 | * This perhaps would best belong in the [lang] project. Even if a similar interface is defined in [lang], it is not 48 | * foreseen that [codec] would be made to depend on [lang]. 49 | * 50 | *

51 | * This class is immutable and thread-safe. 52 | *

53 | * 54 | * @see Standard charsets 55 | * @since 1.4 56 | */ 57 | public class CharEncoding { 58 | 59 | /** 60 | * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. 61 | *

62 | * Every implementation of the Java platform is required to support this character encoding. 63 | *

64 | * 65 | * @see Standard charsets 66 | */ 67 | public static final String ISO_8859_1 = "ISO-8859-1"; 68 | 69 | /** 70 | * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. 71 | *

72 | * Every implementation of the Java platform is required to support this character encoding. 73 | *

74 | * 75 | * @see Standard charsets 76 | */ 77 | public static final String US_ASCII = "US-ASCII"; 78 | 79 | /** 80 | * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark 81 | * (either order accepted on input, big-endian used on output) 82 | *

83 | * Every implementation of the Java platform is required to support this character encoding. 84 | *

85 | * 86 | * @see Standard charsets 87 | */ 88 | public static final String UTF_16 = "UTF-16"; 89 | 90 | /** 91 | * Sixteen-bit Unicode Transformation Format, big-endian byte order. 92 | *

93 | * Every implementation of the Java platform is required to support this character encoding. 94 | *

95 | * 96 | * @see Standard charsets 97 | */ 98 | public static final String UTF_16BE = "UTF-16BE"; 99 | 100 | /** 101 | * Sixteen-bit Unicode Transformation Format, little-endian byte order. 102 | *

103 | * Every implementation of the Java platform is required to support this character encoding. 104 | *

105 | * 106 | * @see Standard charsets 107 | */ 108 | public static final String UTF_16LE = "UTF-16LE"; 109 | 110 | /** 111 | * Eight-bit Unicode Transformation Format. 112 | *

113 | * Every implementation of the Java platform is required to support this character encoding. 114 | *

115 | * 116 | * @see Standard charsets 117 | */ 118 | public static final String UTF_8 = "UTF-8"; 119 | } 120 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/Decoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Provides the highest level of abstraction for Decoders. 22 | *

23 | * This is the sister interface of {@link Encoder}. All Decoders implement this common generic interface. 24 | * Allows a user to pass a generic Object to any Decoder implementation in the codec package. 25 | *

26 | * One of the two interfaces at the center of the codec package. 27 | * 28 | */ 29 | public interface Decoder { 30 | 31 | /** 32 | * Decodes an "encoded" Object and returns a "decoded" Object. Note that the implementation of this interface will 33 | * try to cast the Object parameter to the specific type expected by a particular Decoder implementation. If a 34 | * {@link ClassCastException} occurs this decode method will throw a DecoderException. 35 | * 36 | * @param source 37 | * the object to decode 38 | * @return a 'decoded" object 39 | * @throws DecoderException 40 | * a decoder exception can be thrown for any number of reasons. Some good candidates are that the 41 | * parameter passed to this method is null, a param cannot be cast to the appropriate type for a 42 | * specific encoder. 43 | */ 44 | Object decode(Object source) throws DecoderException; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/DecoderException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Thrown when there is a failure condition during the decoding process. This exception is thrown when a {@link Decoder} 22 | * encounters a decoding specific exception such as invalid data, or characters outside of the expected range. 23 | * 24 | */ 25 | public class DecoderException extends Exception { 26 | 27 | /** 28 | * Declares the Serial Version Uid. 29 | * 30 | * @see Always Declare Serial Version Uid 31 | */ 32 | private static final long serialVersionUID = 1L; 33 | 34 | /** 35 | * Constructs a new exception with {@code null} as its detail message. The cause is not initialized, and may 36 | * subsequently be initialized by a call to {@link #initCause}. 37 | * 38 | * @since 1.4 39 | */ 40 | public DecoderException() { 41 | super(); 42 | } 43 | 44 | /** 45 | * Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently 46 | * be initialized by a call to {@link #initCause}. 47 | * 48 | * @param message 49 | * The detail message which is saved for later retrieval by the {@link #getMessage()} method. 50 | */ 51 | public DecoderException(final String message) { 52 | super(message); 53 | } 54 | 55 | /** 56 | * Constructs a new exception with the specified detail message and cause. 57 | *

58 | * Note that the detail message associated with {@code cause} is not automatically incorporated into this 59 | * exception's detail message. 60 | * 61 | * @param message 62 | * The detail message which is saved for later retrieval by the {@link #getMessage()} method. 63 | * @param cause 64 | * The cause which is saved for later retrieval by the {@link #getCause()} method. A {@code null} 65 | * value is permitted, and indicates that the cause is nonexistent or unknown. 66 | * @since 1.4 67 | */ 68 | public DecoderException(final String message, final Throwable cause) { 69 | super(message, cause); 70 | } 71 | 72 | /** 73 | * Constructs a new exception with the specified cause and a detail message of (cause==null ? 74 | * null : cause.toString()) (which typically contains the class and detail message of {@code cause}). 75 | * This constructor is useful for exceptions that are little more than wrappers for other throwables. 76 | * 77 | * @param cause 78 | * The cause which is saved for later retrieval by the {@link #getCause()} method. A {@code null} 79 | * value is permitted, and indicates that the cause is nonexistent or unknown. 80 | * @since 1.4 81 | */ 82 | public DecoderException(final Throwable cause) { 83 | super(cause); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/Encoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Provides the highest level of abstraction for Encoders. 22 | *

23 | * This is the sister interface of {@link Decoder}. Every implementation of Encoder provides this 24 | * common generic interface which allows a user to pass a generic Object to any Encoder implementation 25 | * in the codec package. 26 | * 27 | */ 28 | public interface Encoder { 29 | 30 | /** 31 | * Encodes an "Object" and returns the encoded content as an Object. The Objects here may just be 32 | * {@code byte[]} or {@code String}s depending on the implementation used. 33 | * 34 | * @param source 35 | * An object to encode 36 | * @return An "encoded" Object 37 | * @throws EncoderException 38 | * An encoder exception is thrown if the encoder experiences a failure condition during the encoding 39 | * process. 40 | */ 41 | Object encode(Object source) throws EncoderException; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/EncoderException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Thrown when there is a failure condition during the encoding process. This exception is thrown when an 22 | * {@link Encoder} encounters a encoding specific exception such as invalid data, inability to calculate a checksum, 23 | * characters outside of the expected range. 24 | * 25 | */ 26 | public class EncoderException extends Exception { 27 | 28 | /** 29 | * Declares the Serial Version Uid. 30 | * 31 | * @see Always Declare Serial Version Uid 32 | */ 33 | private static final long serialVersionUID = 1L; 34 | 35 | /** 36 | * Constructs a new exception with {@code null} as its detail message. The cause is not initialized, and may 37 | * subsequently be initialized by a call to {@link #initCause}. 38 | * 39 | * @since 1.4 40 | */ 41 | public EncoderException() { 42 | super(); 43 | } 44 | 45 | /** 46 | * Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently 47 | * be initialized by a call to {@link #initCause}. 48 | * 49 | * @param message 50 | * a useful message relating to the encoder specific error. 51 | */ 52 | public EncoderException(final String message) { 53 | super(message); 54 | } 55 | 56 | /** 57 | * Constructs a new exception with the specified detail message and cause. 58 | * 59 | *

60 | * Note that the detail message associated with {@code cause} is not automatically incorporated into this 61 | * exception's detail message. 62 | *

63 | * 64 | * @param message 65 | * The detail message which is saved for later retrieval by the {@link #getMessage()} method. 66 | * @param cause 67 | * The cause which is saved for later retrieval by the {@link #getCause()} method. A {@code null} 68 | * value is permitted, and indicates that the cause is nonexistent or unknown. 69 | * @since 1.4 70 | */ 71 | public EncoderException(final String message, final Throwable cause) { 72 | super(message, cause); 73 | } 74 | 75 | /** 76 | * Constructs a new exception with the specified cause and a detail message of (cause==null ? 77 | * null : cause.toString()) (which typically contains the class and detail message of {@code cause}). 78 | * This constructor is useful for exceptions that are little more than wrappers for other throwables. 79 | * 80 | * @param cause 81 | * The cause which is saved for later retrieval by the {@link #getCause()} method. A {@code null} 82 | * value is permitted, and indicates that the cause is nonexistent or unknown. 83 | * @since 1.4 84 | */ 85 | public EncoderException(final Throwable cause) { 86 | super(cause); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/StringDecoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Defines common decoding methods for String decoders. 22 | * 23 | */ 24 | public interface StringDecoder extends Decoder { 25 | 26 | /** 27 | * Decodes a String and returns a String. 28 | * 29 | * @param source 30 | * the String to decode 31 | * @return the encoded String 32 | * @throws DecoderException 33 | * thrown if there is an error condition during the Encoding process. 34 | */ 35 | String decode(String source) throws DecoderException; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/StringEncoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec; 19 | 20 | /** 21 | * Defines common encoding methods for String encoders. 22 | * 23 | */ 24 | public interface StringEncoder extends Encoder { 25 | 26 | /** 27 | * Encodes a String and returns a String. 28 | * 29 | * @param source 30 | * the String to encode 31 | * @return the encoded String 32 | * @throws EncoderException 33 | * thrown if there is an error condition during the encoding process. 34 | */ 35 | String encode(String source) throws EncoderException; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/main/java/org/apache/commons/codec/binary/Hex.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.apache.commons.codec.binary; 19 | 20 | import java.nio.ByteBuffer; 21 | import java.nio.charset.Charset; 22 | import java.nio.charset.StandardCharsets; 23 | 24 | import org.apache.commons.codec.BinaryDecoder; 25 | import org.apache.commons.codec.BinaryEncoder; 26 | import org.apache.commons.codec.CharEncoding; 27 | import org.apache.commons.codec.DecoderException; 28 | import org.apache.commons.codec.EncoderException; 29 | 30 | /** 31 | * Converts hexadecimal Strings. The Charset used for certain operation can be set, the default is set in 32 | * {@link #DEFAULT_CHARSET_NAME} 33 | * 34 | * This class is thread-safe. 35 | * 36 | * @since 1.1 37 | */ 38 | public class Hex implements BinaryEncoder, BinaryDecoder { 39 | 40 | /** 41 | * Default charset is {@link StandardCharsets#UTF_8}. 42 | * 43 | * @since 1.7 44 | */ 45 | public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; 46 | 47 | /** 48 | * Default charset name is {@link CharEncoding#UTF_8}. 49 | * 50 | * @since 1.4 51 | */ 52 | public static final String DEFAULT_CHARSET_NAME = CharEncoding.UTF_8; 53 | 54 | /** 55 | * Used to build output as hex. 56 | */ 57 | private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 58 | 'e', 'f' }; 59 | 60 | /** 61 | * Used to build output as hex. 62 | */ 63 | private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 64 | 'E', 'F' }; 65 | 66 | /** 67 | * Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The 68 | * returned array will be half the length of the passed array, as it takes two characters to represent any given 69 | * byte. An exception is thrown if the passed char array has an odd number of elements. 70 | * 71 | * @param data An array of characters containing hexadecimal digits 72 | * @return A byte array containing binary data decoded from the supplied char array. 73 | * @throws DecoderException Thrown if an odd number of characters or illegal characters are supplied 74 | */ 75 | public static byte[] decodeHex(final char[] data) throws DecoderException { 76 | final byte[] out = new byte[data.length >> 1]; 77 | decodeHex(data, out, 0); 78 | return out; 79 | } 80 | 81 | /** 82 | * Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The 83 | * returned array will be half the length of the passed array, as it takes two characters to represent any given 84 | * byte. An exception is thrown if the passed char array has an odd number of elements. 85 | * 86 | * @param data An array of characters containing hexadecimal digits 87 | * @param out A byte array to contain the binary data decoded from the supplied char array. 88 | * @param outOffset The position within {@code out} to start writing the decoded bytes. 89 | * @return the number of bytes written to {@code out}. 90 | * @throws DecoderException Thrown if an odd number of characters or illegal characters are supplied 91 | * @since 1.15 92 | */ 93 | public static int decodeHex(final char[] data, final byte[] out, final int outOffset) throws DecoderException { 94 | final int len = data.length; 95 | 96 | if ((len & 0x01) != 0) { 97 | throw new DecoderException("Odd number of characters."); 98 | } 99 | 100 | final int outLen = len >> 1; 101 | if (out.length - outOffset < outLen) { 102 | throw new DecoderException("Output array is not large enough to accommodate decoded data."); 103 | } 104 | 105 | // two characters form the hex value. 106 | for (int i = outOffset, j = 0; j < len; i++) { 107 | int f = toDigit(data[j], j) << 4; 108 | j++; 109 | f = f | toDigit(data[j], j); 110 | j++; 111 | out[i] = (byte) (f & 0xFF); 112 | } 113 | 114 | return outLen; 115 | } 116 | 117 | /** 118 | * Converts a String representing hexadecimal values into an array of bytes of those same values. The returned array 119 | * will be half the length of the passed String, as it takes two characters to represent any given byte. An 120 | * exception is thrown if the passed String has an odd number of elements. 121 | * 122 | * @param data A String containing hexadecimal digits 123 | * @return A byte array containing binary data decoded from the supplied char array. 124 | * @throws DecoderException Thrown if an odd number of characters or illegal characters are supplied 125 | * @since 1.11 126 | */ 127 | public static byte[] decodeHex(final String data) throws DecoderException { 128 | return decodeHex(data.toCharArray()); 129 | } 130 | 131 | /** 132 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. 133 | * The returned array will be double the length of the passed array, as it takes two characters to represent any 134 | * given byte. 135 | * 136 | * @param data a byte[] to convert to hex characters 137 | * @return A char[] containing lower-case hexadecimal characters 138 | */ 139 | public static char[] encodeHex(final byte[] data) { 140 | return encodeHex(data, true); 141 | } 142 | 143 | /** 144 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. 145 | * The returned array will be double the length of the passed array, as it takes two characters to represent any 146 | * given byte. 147 | * 148 | * @param data a byte[] to convert to Hex characters 149 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase 150 | * @return A char[] containing hexadecimal characters in the selected case 151 | * @since 1.4 152 | */ 153 | public static char[] encodeHex(final byte[] data, final boolean toLowerCase) { 154 | return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); 155 | } 156 | 157 | /** 158 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. 159 | * The returned array will be double the length of the passed array, as it takes two characters to represent any 160 | * given byte. 161 | * 162 | * @param data a byte[] to convert to hex characters 163 | * @param toDigits the output alphabet (must contain at least 16 chars) 164 | * @return A char[] containing the appropriate characters from the alphabet For best results, this should be either 165 | * upper- or lower-case hex. 166 | * @since 1.4 167 | */ 168 | protected static char[] encodeHex(final byte[] data, final char[] toDigits) { 169 | final int l = data.length; 170 | final char[] out = new char[l << 1]; 171 | encodeHex(data, 0, data.length, toDigits, out, 0); 172 | return out; 173 | } 174 | 175 | /** 176 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. 177 | * 178 | * @param data a byte[] to convert to hex characters 179 | * @param dataOffset the position in {@code data} to start encoding from 180 | * @param dataLen the number of bytes from {@code dataOffset} to encode 181 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase 182 | * @return A char[] containing the appropriate characters from the alphabet For best results, this should be either 183 | * upper- or lower-case hex. 184 | * @since 1.15 185 | */ 186 | public static char[] encodeHex(final byte[] data, final int dataOffset, final int dataLen, 187 | final boolean toLowerCase) { 188 | final char[] out = new char[dataLen << 1]; 189 | encodeHex(data, dataOffset, dataLen, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER, out, 0); 190 | return out; 191 | } 192 | 193 | /** 194 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. 195 | * 196 | * @param data a byte[] to convert to hex characters 197 | * @param dataOffset the position in {@code data} to start encoding from 198 | * @param dataLen the number of bytes from {@code dataOffset} to encode 199 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase 200 | * @param out a char[] which will hold the resultant appropriate characters from the alphabet. 201 | * @param outOffset the position within {@code out} at which to start writing the encoded characters. 202 | * @since 1.15 203 | */ 204 | public static void encodeHex(final byte[] data, final int dataOffset, final int dataLen, 205 | final boolean toLowerCase, final char[] out, final int outOffset) { 206 | encodeHex(data, dataOffset, dataLen, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER, out, outOffset); 207 | } 208 | 209 | /** 210 | * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. 211 | * 212 | * @param data a byte[] to convert to hex characters 213 | * @param dataOffset the position in {@code data} to start encoding from 214 | * @param dataLen the number of bytes from {@code dataOffset} to encode 215 | * @param toDigits the output alphabet (must contain at least 16 chars) 216 | * @param out a char[] which will hold the resultant appropriate characters from the alphabet. 217 | * @param outOffset the position within {@code out} at which to start writing the encoded characters. 218 | */ 219 | private static void encodeHex(final byte[] data, final int dataOffset, final int dataLen, final char[] toDigits, 220 | final char[] out, final int outOffset) { 221 | // two characters form the hex value. 222 | for (int i = dataOffset, j = outOffset; i < dataOffset + dataLen; i++) { 223 | out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; 224 | out[j++] = toDigits[0x0F & data[i]]; 225 | } 226 | } 227 | 228 | /** 229 | * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order. The 230 | * returned array will be double the length of the passed array, as it takes two characters to represent any given 231 | * byte. 232 | * 233 | *

All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method 234 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.

235 | * 236 | * @param data a byte buffer to convert to hex characters 237 | * @return A char[] containing lower-case hexadecimal characters 238 | * @since 1.11 239 | */ 240 | public static char[] encodeHex(final ByteBuffer data) { 241 | return encodeHex(data, true); 242 | } 243 | 244 | /** 245 | * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order. The 246 | * returned array will be double the length of the passed array, as it takes two characters to represent any given 247 | * byte. 248 | * 249 | *

All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method 250 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.

251 | * 252 | * @param data a byte buffer to convert to hex characters 253 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase 254 | * @return A char[] containing hexadecimal characters in the selected case 255 | * @since 1.11 256 | */ 257 | public static char[] encodeHex(final ByteBuffer data, final boolean toLowerCase) { 258 | return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); 259 | } 260 | 261 | /** 262 | * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order. The 263 | * returned array will be double the length of the passed array, as it takes two characters to represent any given 264 | * byte. 265 | * 266 | *

All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method 267 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.

268 | * 269 | * @param byteBuffer a byte buffer to convert to hex characters 270 | * @param toDigits the output alphabet (must be at least 16 characters) 271 | * @return A char[] containing the appropriate characters from the alphabet For best results, this should be either 272 | * upper- or lower-case hex. 273 | * @since 1.11 274 | */ 275 | protected static char[] encodeHex(final ByteBuffer byteBuffer, final char[] toDigits) { 276 | return encodeHex(toByteArray(byteBuffer), toDigits); 277 | } 278 | 279 | /** 280 | * Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned 281 | * String will be double the length of the passed array, as it takes two characters to represent any given byte. 282 | * 283 | * @param data a byte[] to convert to hex characters 284 | * @return A String containing lower-case hexadecimal characters 285 | * @since 1.4 286 | */ 287 | public static String encodeHexString(final byte[] data) { 288 | return new String(encodeHex(data)); 289 | } 290 | 291 | /** 292 | * Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned 293 | * String will be double the length of the passed array, as it takes two characters to represent any given byte. 294 | * 295 | * @param data a byte[] to convert to hex characters 296 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase 297 | * @return A String containing lower-case hexadecimal characters 298 | * @since 1.11 299 | */ 300 | public static String encodeHexString(final byte[] data, final boolean toLowerCase) { 301 | return new String(encodeHex(data, toLowerCase)); 302 | } 303 | 304 | /** 305 | * Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned 306 | * String will be double the length of the passed array, as it takes two characters to represent any given byte. 307 | * 308 | *

All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method 309 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.

310 | * 311 | * @param data a byte buffer to convert to hex characters 312 | * @return A String containing lower-case hexadecimal characters 313 | * @since 1.11 314 | */ 315 | public static String encodeHexString(final ByteBuffer data) { 316 | return new String(encodeHex(data)); 317 | } 318 | 319 | /** 320 | * Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned 321 | * String will be double the length of the passed array, as it takes two characters to represent any given byte. 322 | * 323 | *

All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method 324 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.

325 | * 326 | * @param data a byte buffer to convert to hex characters 327 | * @param toLowerCase {@code true} converts to lowercase, {@code false} to uppercase 328 | * @return A String containing lower-case hexadecimal characters 329 | * @since 1.11 330 | */ 331 | public static String encodeHexString(final ByteBuffer data, final boolean toLowerCase) { 332 | return new String(encodeHex(data, toLowerCase)); 333 | } 334 | 335 | /** 336 | * Convert the byte buffer to a byte array. All bytes identified by 337 | * {@link ByteBuffer#remaining()} will be used. 338 | * 339 | * @param byteBuffer the byte buffer 340 | * @return the byte[] 341 | */ 342 | private static byte[] toByteArray(final ByteBuffer byteBuffer) { 343 | final int remaining = byteBuffer.remaining(); 344 | // Use the underlying buffer if possible 345 | if (byteBuffer.hasArray()) { 346 | final byte[] byteArray = byteBuffer.array(); 347 | if (remaining == byteArray.length) { 348 | byteBuffer.position(remaining); 349 | return byteArray; 350 | } 351 | } 352 | // Copy the bytes 353 | final byte[] byteArray = new byte[remaining]; 354 | byteBuffer.get(byteArray); 355 | return byteArray; 356 | } 357 | 358 | /** 359 | * Converts a hexadecimal character to an integer. 360 | * 361 | * @param ch A character to convert to an integer digit 362 | * @param index The index of the character in the source 363 | * @return An integer 364 | * @throws DecoderException Thrown if ch is an illegal hex character 365 | */ 366 | protected static int toDigit(final char ch, final int index) throws DecoderException { 367 | final int digit = Character.digit(ch, 16); 368 | if (digit == -1) { 369 | throw new DecoderException("Illegal hexadecimal character " + ch + " at index " + index); 370 | } 371 | return digit; 372 | } 373 | 374 | private final Charset charset; 375 | 376 | /** 377 | * Creates a new codec with the default charset name {@link #DEFAULT_CHARSET} 378 | */ 379 | public Hex() { 380 | // use default encoding 381 | this.charset = DEFAULT_CHARSET; 382 | } 383 | 384 | /** 385 | * Creates a new codec with the given Charset. 386 | * 387 | * @param charset the charset. 388 | * @since 1.7 389 | */ 390 | public Hex(final Charset charset) { 391 | this.charset = charset; 392 | } 393 | 394 | /** 395 | * Creates a new codec with the given charset name. 396 | * 397 | * @param charsetName the charset name. 398 | * @throws java.nio.charset.UnsupportedCharsetException If the named charset is unavailable 399 | * @since 1.4 400 | * @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable 401 | */ 402 | public Hex(final String charsetName) { 403 | this(Charset.forName(charsetName)); 404 | } 405 | 406 | /** 407 | * Converts an array of character bytes representing hexadecimal values into an array of bytes of those same values. 408 | * The returned array will be half the length of the passed array, as it takes two characters to represent any given 409 | * byte. An exception is thrown if the passed char array has an odd number of elements. 410 | * 411 | * @param array An array of character bytes containing hexadecimal digits 412 | * @return A byte array containing binary data decoded from the supplied byte array (representing characters). 413 | * @throws DecoderException Thrown if an odd number of characters is supplied to this function 414 | * @see #decodeHex(char[]) 415 | */ 416 | @Override 417 | public byte[] decode(final byte[] array) throws DecoderException { 418 | return decodeHex(new String(array, getCharset()).toCharArray()); 419 | } 420 | 421 | /** 422 | * Converts a buffer of character bytes representing hexadecimal values into an array of bytes of those same values. 423 | * The returned array will be half the length of the passed array, as it takes two characters to represent any given 424 | * byte. An exception is thrown if the passed char array has an odd number of elements. 425 | * 426 | *

All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method 427 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.

428 | * 429 | * @param buffer An array of character bytes containing hexadecimal digits 430 | * @return A byte array containing binary data decoded from the supplied byte array (representing characters). 431 | * @throws DecoderException Thrown if an odd number of characters is supplied to this function 432 | * @see #decodeHex(char[]) 433 | * @since 1.11 434 | */ 435 | public byte[] decode(final ByteBuffer buffer) throws DecoderException { 436 | return decodeHex(new String(toByteArray(buffer), getCharset()).toCharArray()); 437 | } 438 | 439 | /** 440 | * Converts a String or an array of character bytes representing hexadecimal values into an array of bytes of those 441 | * same values. The returned array will be half the length of the passed String or array, as it takes two characters 442 | * to represent any given byte. An exception is thrown if the passed char array has an odd number of elements. 443 | * 444 | * @param object A String, ByteBuffer, byte[], or an array of character bytes containing hexadecimal digits 445 | * @return A byte array containing binary data decoded from the supplied byte array (representing characters). 446 | * @throws DecoderException Thrown if an odd number of characters is supplied to this function or the object is not 447 | * a String or char[] 448 | * @see #decodeHex(char[]) 449 | */ 450 | @Override 451 | public Object decode(final Object object) throws DecoderException { 452 | if (object instanceof String) { 453 | return decode(((String) object).toCharArray()); 454 | } else if (object instanceof byte[]) { 455 | return decode((byte[]) object); 456 | } else if (object instanceof ByteBuffer) { 457 | return decode((ByteBuffer) object); 458 | } else { 459 | try { 460 | return decodeHex((char[]) object); 461 | } catch (final ClassCastException e) { 462 | throw new DecoderException(e.getMessage(), e); 463 | } 464 | } 465 | } 466 | 467 | /** 468 | * Converts an array of bytes into an array of bytes for the characters representing the hexadecimal values of each 469 | * byte in order. The returned array will be double the length of the passed array, as it takes two characters to 470 | * represent any given byte. 471 | *

472 | * The conversion from hexadecimal characters to the returned bytes is performed with the charset named by 473 | * {@link #getCharset()}. 474 | *

475 | * 476 | * @param array a byte[] to convert to hex characters 477 | * @return A byte[] containing the bytes of the lower-case hexadecimal characters 478 | * @since 1.7 No longer throws IllegalStateException if the charsetName is invalid. 479 | * @see #encodeHex(byte[]) 480 | */ 481 | @Override 482 | public byte[] encode(final byte[] array) { 483 | return encodeHexString(array).getBytes(this.getCharset()); 484 | } 485 | 486 | /** 487 | * Converts byte buffer into an array of bytes for the characters representing the hexadecimal values of each byte 488 | * in order. The returned array will be double the length of the passed array, as it takes two characters to 489 | * represent any given byte. 490 | * 491 | *

The conversion from hexadecimal characters to the returned bytes is performed with the charset named by 492 | * {@link #getCharset()}.

493 | * 494 | *

All bytes identified by {@link ByteBuffer#remaining()} will be used; after this method 495 | * the value {@link ByteBuffer#remaining() remaining()} will be zero.

496 | * 497 | * @param array a byte buffer to convert to hex characters 498 | * @return A byte[] containing the bytes of the lower-case hexadecimal characters 499 | * @see #encodeHex(byte[]) 500 | * @since 1.11 501 | */ 502 | public byte[] encode(final ByteBuffer array) { 503 | return encodeHexString(array).getBytes(this.getCharset()); 504 | } 505 | 506 | /** 507 | * Converts a String or an array of bytes into an array of characters representing the hexadecimal values of each 508 | * byte in order. The returned array will be double the length of the passed String or array, as it takes two 509 | * characters to represent any given byte. 510 | *

511 | * The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by 512 | * {@link #getCharset()}. 513 | *

514 | * 515 | * @param object a String, ByteBuffer, or byte[] to convert to hex characters 516 | * @return A char[] containing lower-case hexadecimal characters 517 | * @throws EncoderException Thrown if the given object is not a String or byte[] 518 | * @see #encodeHex(byte[]) 519 | */ 520 | @Override 521 | public Object encode(final Object object) throws EncoderException { 522 | byte[] byteArray; 523 | if (object instanceof String) { 524 | byteArray = ((String) object).getBytes(this.getCharset()); 525 | } else if (object instanceof ByteBuffer) { 526 | byteArray = toByteArray((ByteBuffer) object); 527 | } else { 528 | try { 529 | byteArray = (byte[]) object; 530 | } catch (final ClassCastException e) { 531 | throw new EncoderException(e.getMessage(), e); 532 | } 533 | } 534 | return encodeHex(byteArray); 535 | } 536 | 537 | /** 538 | * Gets the charset. 539 | * 540 | * @return the charset. 541 | * @since 1.7 542 | */ 543 | public Charset getCharset() { 544 | return this.charset; 545 | } 546 | 547 | /** 548 | * Gets the charset name. 549 | * 550 | * @return the charset name. 551 | * @since 1.4 552 | */ 553 | public String getCharsetName() { 554 | return this.charset.name(); 555 | } 556 | 557 | /** 558 | * Returns a string representation of the object, which includes the charset name. 559 | * 560 | * @return a string representation of the object. 561 | */ 562 | @Override 563 | public String toString() { 564 | return super.toString() + "[charsetName=" + this.charset + "]"; 565 | } 566 | } 567 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{dd.MM.yyyy HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{50}.%M - %msg%n 8 | 9 | 10 | 11 | 12 | data/bot.log 13 | 14 | 15 | %d{dd.MM.yyyy HH:mm:ss.SSS} [%thread] %level %logger{50}.%M - %msg%n 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/test/java/de/slg/ddnss/printertool/clients/AdventurerClientTest.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import static de.slg.ddnss.printertool.clients.AdventurerCommands.CMD_BYE; 4 | import static de.slg.ddnss.printertool.clients.AdventurerCommands.CMD_PRINT_STOP; 5 | import static org.junit.jupiter.api.Assertions.assertTrue; 6 | 7 | import java.io.IOException; 8 | import java.nio.file.Files; 9 | import java.nio.file.Paths; 10 | 11 | import org.junit.jupiter.api.AfterAll; 12 | import org.junit.jupiter.api.BeforeAll; 13 | import org.junit.jupiter.api.Test; 14 | 15 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException; 16 | 17 | class AdventurerClientTest { 18 | 19 | private static String printerAddress; 20 | 21 | @BeforeAll 22 | static void init() { 23 | try { 24 | printerAddress = new FlashForgeDiscoverClient().discoverPrinter().getPrinterAddress(); 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | 30 | @Test 31 | void printTest() throws FlashForgePrinterException, IOException { 32 | AdventurerClient client = new AdventurerClient(printerAddress); 33 | boolean print = client.print("20mm_Box.gx", Files.readAllBytes(Paths.get("20mm_Box.gx"))); 34 | client.close(); 35 | assertTrue(print); 36 | } 37 | 38 | @Test 39 | void ledTest() throws FlashForgePrinterException { 40 | AdventurerClient client = new AdventurerClient(printerAddress); 41 | boolean replay = client.setLed(false); 42 | System.out.println(replay); 43 | client.close(); 44 | assertTrue(replay); 45 | } 46 | 47 | @Test 48 | void printStopTest() throws FlashForgePrinterException { 49 | AdventurerClient client = new AdventurerClient(printerAddress); 50 | boolean replay = client.stopPrinting(); 51 | client.close(); 52 | assertTrue(replay); 53 | } 54 | 55 | @Test 56 | void getPrinterInfoTest() throws FlashForgePrinterException { 57 | AdventurerClient client = new AdventurerClient(printerAddress); 58 | PrinterInfo printerInfo = client.getPrinterInfo(); 59 | System.out.println(printerInfo); 60 | client.close(); 61 | } 62 | 63 | @AfterAll 64 | static void cleanup() throws FlashForgePrinterException { 65 | AdventurerClient adventurerClient = new AdventurerClient(printerAddress); 66 | String sendOk = adventurerClient.sendCommand(CMD_PRINT_STOP); 67 | System.out.println(sendOk); 68 | assertTrue(sendOk.contains("Received")); 69 | assertTrue(sendOk.contains("ok")); 70 | 71 | sendOk = adventurerClient.sendCommand(CMD_BYE); 72 | System.out.println(sendOk); 73 | assertTrue(sendOk.equals("CMD M602 Received.\nControl Release.\nok")); 74 | 75 | adventurerClient.close(); 76 | } 77 | 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/de/slg/ddnss/printertool/clients/ByteCRC32.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.apache.commons.codec.DecoderException; 6 | import org.apache.commons.codec.binary.Hex; 7 | import org.junit.jupiter.api.Test; 8 | 9 | import de.slg.ddnss.printertool.util.Util; 10 | 11 | class ByteCRC32 { 12 | 13 | String hex = "592d32372e37312046333030300a473120582d31382e363320592d32352e35380a473120582d31332e363520592d32382e30310a473120582d31332e363520592d32382e30310a473120582d31372e383020592d32342e3837204538382e393833362046313830300a473120582d31382e303520592d32342e3336204538382e393931360a473120582d31382e323020592d32342e3537204538382e393935330a473120582d32322e373520592d32312e3132204538392e303736360a473120582d32332e323220592d32312e3737204538392e303838300a473120582d31342e313520592d32382e3634204538392e323530300a473120582d31332e363520592d32382e3031204538392e323631340a473120582d31372e363320592d32342e37332046333030300a473120582d31372e373720592d32342e33390a473120582d31362e323220592d32322e33390a473120582d31312e323020592d32342e38350a473120582d31312e323020592d32342e38350a473120582d31352e353020592d32312e3630204538392e333338322046313830300a473120582d31352e353220592d32312e3338204538392e333431330a473120582d31352e373420592d32312e3332204538392e333434360a473120582d31352e373920592d32312e3338204538392e333435370a473120582d32302e333820592d31372e3931204538392e343237360a473120582d32302e383520592d31382e3535204538392e343338390a473120582d31362e353720592d32312e3739204538392e353135330a473120582d31362e353420592d32322e3032204538392e353138360a473120582d31362e333220592d32322e3038204538392e353231390a473120582d31362e323720592d32322e3032204538392e353233300a473120582d31312e363920592d32352e3438204538392e363034370a473120582d31312e323020592d32342e3835204538392e363136300a473120582d31352e323820592d32312e34382046333030300a473120582d31352e333220592d32312e31340a473120582d31332e383120592d31392e32300a473120582d382e363720592d32312e37350a473120582d382e363720592d32312e37350a473120582d31322e393720592d31382e3439204538392e363932392046313830300a473120582d31332e323220592d31372e3939204538392e373030380a473120582d31332e333720592d31382e3139204538392e373034340a473120582d31382e303720592d31342e3634204538392e373838320a473120582d31382e353220592d31352e3330204538392e373939360a473120582d392e313920592d32322e3336204538392e393636320a473120582d382e363720592d32312e3735204538392e393737360a473120582d31322e383020592d31382e33352046333030300a473120582d31322e393420592d31382e30310a473120582d31312e333920592d31362e30310a473120582d362e303720592d31382e37300a473120582d362e303720592d31382e37300a473120582d31352e383520592d31312e3330204539302e313532312046313830300a473120582d31362e323920592d31312e3937204539302e313633360a473120582d362e363020592d31392e3331204539302e333336360a473120582d362e303720592d31382e3730204539302e333438310a473120582d31302e353920592d31342e39352046333030300a473120582d382e393820592d31322e38320a473120582d332e343620592d31352e36360a473120582d332e343620592d31352e36360a473120582d382e303720592d31322e3137204539302e343330342046313830300a473120582d382e343420592d31312e3637204539302e343339330a473120582d382e353520592d31312e3831204539302e343431380a473120582d31332e363220592d372e3937204539302e353332330a473120582d31342e303820592d382e3633204539302e353433380a473120582d392e353020592d31322e3039204539302e363235350a473120582d392e313420592d31322e3539204539302e363334330a473120582d392e303320592d31322e3435204539302e363336380a473120582d332e393820592d31362e3237204539302e373237300a473120582d332e343620592d31352e3636204539302e373338340a473120582d372e393220592d31322e30322046333030300a473120582d382e313320592d31312e36370a473120582d362e353620592d392e36330a473120582d312e303120592d31322e35300a473120582d312e303120592d31322e35300a473120582d352e373120592d382e3934204539302e383232332046313830300a473120582d352e393920592d382e3433204539302e383330360a473120582d362e313320592d382e3632204539302e383333390a473120582d31312e323620592d342e3734204539302e393235350a473120582d31312e373420592d352e3338204539302e393336390a473120582d372e303420592d382e3934204539312e303230380a473120582d362e373620592d392e3435204539312e303239310a473120582d362e363220592d392e3236204539312e303332350a473120582d312e343920592d31332e3133204539312e313233390a473120582d312e303120592d31322e3530204539312e313335320a473120582d352e353520592d382e38302046333030300a473120582d352e373020592d382e34350a473120582d342e313520592d362e34340a47312058312e343220592d392e33320a47312058312e343220592d392e33320a473120582d332e333020592d352e3735204539312e323139352046313830300a473120582d332e353820592d352e3234204539312e323237370a473120582d332e373220592d352e3433204539312e323331310a473120582d382e383420592d312e3535204539312e333232350a473120582d392e333420592d322e3138204539312e333334300a473120582d342e363320592d352e3735204539312e343138310a473120582d342e333420592d362e3236204539312e343236350a473120582d342e323020592d362e3037204539312e343239380a473120582e393320592d392e3935204539312e353231340a47312058312e343220592d392e3332204539312e353332380a473120582d332e313420592d352e36302046333030300a473120582d332e323920592d352e32360a473120582d312e373420592d332e32350a47312058332e383320592d362e31330a47312058332e383320592d362e31330a473120582d2e383320592d322e3630204539312e363136302046313830300a473120582d312e323020592d322e3130204539312e363234380a473120582d312e333120592d322e3234204539312e363237340a473120582d362e34342059312e3634204539312e373138390a473120582d362e39312059312e3030204539312e373330320a473120582d322e323720592d322e3532204539312e383133310a473120582d312e393020592d332e3032204539312e383232300a473120582d312e373920592d322e3838204539312e383234350a47312058332e333320592d362e3736204539312e393136300a47312058332e383320592d362e3133204539312e393237340a473120582d2e363820592d322e34352046333030300a473120582d2e383920592d322e31300a473120582e363820592d2e30370a47312058362e333120592d322e39390a47312058362e333120592d322e39390a47312058312e353320592e3633204539322e303132382046313830300a47312058312e32352059312e3134204539322e303231310a47312058312e313120592e3935204539322e303234340a473120582d342e31302059342e3839204539322e313137340a473120582d342e35372059342e3234204539322e313238380a473120582e313420592e3638204539322e323132390a473120582e353220592e3137204539322e323231390a473120582e363220592e3331204539322e323234340a47312058352e383120592d332e3632204539322e333137300a47312058362e333120592d322e3939204539322e333238350a47312058312e363920592e37372046333030300a47312058312e35342059312e31320a47312058332e30392059332e31320a47312058382e373320592e32300a47312058382e373320592e32300a47312058342e30342059332e3734204539322e343132312046313830300a47312058332e36312059342e3236204539322e343231370a47312058332e35322059342e3134204539322e343233390a473120582d312e36392059382e3038204539322e353136390a473120582d322e31392059372e3435204539322e353238330a47312058322e35312059332e3930204539322e363132320a47312058322e39362059332e3339204539322e363231380a47312058332e30342059332e3530204539322e363233380a47312058382e323620592d2e3435204539322e373137300a47312058382e373320592e3230204539322e373238340a47312058342e31392059332e39302046333030300a47312058332e39342059342e32370a47312058352e35302059362e33310a4731205831302e39362059332e35320a4731205831302e39362059332e35320a47312058362e33352059372e3032204539322e383130382046313830300a47312058362e30392059372e3533204539322e383138390a47312058352e39332059372e3333204539322e383232360a473120582e3931205931312e3133204539322e393132320a473120582e3337205931302e3534204539322e393233360a47312058342e39372059372e3036204539332e303035370a47312058352e33342059362e3535204539332e303134370a47312058352e34352059362e3639204539332e303137320a4731205831302e35332059322e3835204539332e313037380a4731205831302e39362059332e3532204539332e313139320a47312058362e35322059372e31362046333030300a47312058362e33372059372e35310a47312058372e39322059392e35300a47312058372e39322059392e35300a4731205831332e30382059362e39330a4731205831332e30382059362e39330a47312058332e3631205931342e3130204539332e323838332046313830300a47312058332e3037205931332e3531204539332e323939370a47312058372e3339205931302e3233204539332e333736390a47312058372e37352059392e3733204539332e333835360a47312058372e38362059392e3838204539332e333838330a4731205831322e36362059362e3235204539332e343734300a4731205831332e30382059362e3933204539332e343835330a47312058382e3732205931302e35372046333030300a4731205831302e3333205931322e36390a4731205831352e3234205931302e33320a4731205831352e3234205931302e33320a4731205831312e3136205931332e3431204539332e353538322046313830300a4731205831302e3933205931332e3933204539332e353636330a4731205831302e3736205931332e3731204539332e353730320a47312058362e3238205931372e3039204539332e363530310a473120"; 14 | 15 | @Test 16 | void testOddError() throws DecoderException { 17 | System.out.println(hex); 18 | String crc32Checksum = Util.calcChecksum(Hex.decodeHex(hex)); 19 | assertEquals("320d9b9", crc32Checksum); 20 | } 21 | 22 | String goldenLast = "5a5aa5a5" 23 | + "0000026d" 24 | + "00000b7e" 25 | + "66d6707b" 26 | + "0a473120582d31352e313820592d32312e38342046333030300a473120582d31352e313820592d32312e38340a473120582d31362e303720592d32322e333020453132332e3231343520463330300a473120582d31362e313620592d32312e383320453132332e323231340a473120582d31362e363020592d32312e393020453132332e323237370a473120582d31362e343020592d32302e393120453132332e323432310a473120582d31352e313820592d32312e383420453132332e323633390a473120582d31352e343120592d32312e33332046333030300a473120582d31332e323020592d31372e39360a3b657874727564655f726174696f3a302e39360a473120582d31332e323020592d31372e39360a473120582d31322e383220592d31372e343520453132332e3237323620463330300a3b70657263656e740a3b657874727564655f726174696f3a310a3b6c617965723a302e30380a3b7368656c6c0a473120453131372e373732362046313830300a473120582d31322e303020592d31382e37322046333030300a4731205a31312e30343020463432300a473120453132332e323732362046313830300a473120582d31322e303020592d31382e37322046333030300a473120582d31322e373220592d31382e313920453132332e3238353320463330300a473120582d31332e393520592d31392e353620453132332e333131350a473120582d31342e343920592d32302e313520453132332e333232390a473120582d31342e383620592d32302e333920453132332e333239320a473120582d31352e303920592d32302e323220453132332e333333330a473120582d31342e393620592d31392e383020453132332e333339350a473120582d31342e353420592d31392e313120453132332e333531300a473120582d31332e353520592d31372e353520453132332e333737330a473120582d31342e323620592d31372e303220453132332e333930300a473120582d31342e373320592d31372e363720453132332e343031340a473120582d31352e323220592d31382e333020453132332e343132370a473120582d31342e343220592d31382e393020453132332e343237300a473120582d31352e383720592d32302e383220453132332e343631320a473120582d31362e313520592d32302e363120453132332e343636320a473120582d31362e323920592d32312e333120453132332e343736340a473120582d31362e313920592d32312e363220453132332e343831300a473120582d31362e303520592d32312e363820453132332e343833320a473120582d31352e393520592d32312e383320453132332e343835370a473120582d31352e353820592d32312e383420453132332e343931300a473120582d31342e393520592d32312e353120453132332e353031310a473120582d31352e323320592d32312e333020453132332e353036310a473120582d31332e373820592d31392e333920453132332e353430330a473120582d31322e393820592d32302e303020453132332e353534360a473120582d31322e303020592d31382e373220453132332e353737350a3b696e66696c6c0a473120582d31322e353420592d31382e36372046333030300a3b657874727564655f726174696f3a312e34360a473120582d31322e353420592d31382e36370a473120582d31332e323020592d31392e343820453132332e3539393220463330300a3b657874727564655f726174696f3a310a473120582d31332e383120592d31392e32302046333030300a473120582d31342e333720592d32302e32390a473120582d31342e383720592d32302e35380a473120582d31352e323720592d32302e32380a473120582d31352e313320592d31392e37320a473120582d31342e323320592d31382e38380a473120582d31342e323320592d31382e38380a473120582d31342e363520592d31382e33380a3b657874727564655f726174696f3a312e34360a473120582d31342e363520592d31382e33380a473120582d31342e303620592d31372e353220453132332e3632303920463330300a3b657874727564655f726174696f3a310a473120582d31342e323320592d31382e38382046333030300a473120582d31352e313320592d31392e37320a473120582d31352e323720592d32302e32380a473120582d31352e313520592d32302e35330a3b657874727564655f726174696f3a302e39360a473120582d31352e313520592d32302e35330a473120582d31352e373520592d32312e333320453132332e3633343620463330300a473120582d31362e303020592d32312e313620453132332e363338370a3b7368656c6c0a3b657874727564655f726174696f3a310a473120453131382e313338372046313830300a473120582d31322e333620592d31352e35372046333030300a473120453132332e363338372046313830300a473120582d31322e333620592d31352e35372046333030300a473120582d31312e383720592d31352e313020453132332e3634383420463330300a3b7368656c6c0a473120453131382e313438342046313830300a473120582d31312e313320592d31362e35302046333030300a473120453132332e363438342046313830300a473120582d31312e313320592d31362e35302046333030300a473120582d31302e383220592d31352e393120453132332e3635373920463330300a3b70657263656e740a3b6c617965723a302e30380a3b7368656c6c0a473120453131382e313537392046313830300a473120582d31322e353020592d31382e33352046333030300a4731205a31312e31323020463432300a473120453132332e363537392046313830300a473120582d31322e353020592d31382e33352046333030300a473120582d31322e363020592d31382e323720453132332e3635393720463330300a473120582d31332e363620592d31392e343820453132332e363832360a473120582d31332e343620592d31392e363320453132332e363836310a473120582d31322e393120592d31392e313020453132332e363937300a473120582d31322e383520592d31392e303320453132332e363938330a473120582d31322e353020592d31382e333520453132332e373039320a3b7368656c6c0a473120453131382e323039322046313830300a473120582d31332e363720592d31372e34362046333030300a473120453132332e373039322046313830300a473120582d31332e363720592d31372e34362046333030300a473120582d31332e373720592d31372e333920453132332e3731313020463330300a473120582d31342e313720592d31372e373920453132332e373139300a473120582d31342e343720592d31382e313620453132332e373235380a473120582d31342e373420592d31382e363620453132332e373333390a473120582d31342e353420592d31382e383220453132332e373337350a473120582d31332e363720592d31372e343620453132332e373630350a3b7368656c6c0a473120453131382e323630352046313830300a473120582d31352e333820592d32302e31302046333030300a473120453132332e373630352046313830300a473120582d31352e333820592d32302e31302046333030300a473120582d31342e363720592d31392e313420453132332e3737373520463330300a3b7368656c6c0a473120453131382e323737352046313830300a473120582d31342e363720592d32302e36342046333030300a473120453132332e373737352046313830300a473120582d31342e363720592d32302e36342046333030300a473120582d31332e393420592d31392e373020453132332e3739343520463330300a4d3130370a3b70657263656e740a473120453131382e323934352046313830300a3b656e642067636f64650a4d3130342053302054300a4d3134302053302054300a47313632205a0a473238205820590a4d31333220582059204120420a4d3635320a4739310a4d31380a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; 27 | String last = "0a473120582d31352e313820592d32312e38342046333030300a473120582d31352e313820592d32312e38340a473120582d31362e303720592d32322e333020453132332e3231343520463330300a473120582d31362e313620592d32312e383320453132332e323231340a473120582d31362e363020592d32312e393020453132332e323237370a473120582d31362e343020592d32302e393120453132332e323432310a473120582d31352e313820592d32312e383420453132332e323633390a473120582d31352e343120592d32312e33332046333030300a473120582d31332e323020592d31372e39360a3b657874727564655f726174696f3a302e39360a473120582d31332e323020592d31372e39360a473120582d31322e383220592d31372e343520453132332e3237323620463330300a3b70657263656e740a3b657874727564655f726174696f3a310a3b6c617965723a302e30380a3b7368656c6c0a473120453131372e373732362046313830300a473120582d31322e303020592d31382e37322046333030300a4731205a31312e30343020463432300a473120453132332e323732362046313830300a473120582d31322e303020592d31382e37322046333030300a473120582d31322e373220592d31382e313920453132332e3238353320463330300a473120582d31332e393520592d31392e353620453132332e333131350a473120582d31342e343920592d32302e313520453132332e333232390a473120582d31342e383620592d32302e333920453132332e333239320a473120582d31352e303920592d32302e323220453132332e333333330a473120582d31342e393620592d31392e383020453132332e333339350a473120582d31342e353420592d31392e313120453132332e333531300a473120582d31332e353520592d31372e353520453132332e333737330a473120582d31342e323620592d31372e303220453132332e333930300a473120582d31342e373320592d31372e363720453132332e343031340a473120582d31352e323220592d31382e333020453132332e343132370a473120582d31342e343220592d31382e393020453132332e343237300a473120582d31352e383720592d32302e383220453132332e343631320a473120582d31362e313520592d32302e363120453132332e343636320a473120582d31362e323920592d32312e333120453132332e343736340a473120582d31362e313920592d32312e363220453132332e343831300a473120582d31362e303520592d32312e363820453132332e343833320a473120582d31352e393520592d32312e383320453132332e343835370a473120582d31352e353820592d32312e383420453132332e343931300a473120582d31342e393520592d32312e353120453132332e353031310a473120582d31352e323320592d32312e333020453132332e353036310a473120582d31332e373820592d31392e333920453132332e353430330a473120582d31322e393820592d32302e303020453132332e353534360a473120582d31322e303020592d31382e373220453132332e353737350a3b696e66696c6c0a473120582d31322e353420592d31382e36372046333030300a3b657874727564655f726174696f3a312e34360a473120582d31322e353420592d31382e36370a473120582d31332e323020592d31392e343820453132332e3539393220463330300a3b657874727564655f726174696f3a310a473120582d31332e383120592d31392e32302046333030300a473120582d31342e333720592d32302e32390a473120582d31342e383720592d32302e35380a473120582d31352e323720592d32302e32380a473120582d31352e313320592d31392e37320a473120582d31342e323320592d31382e38380a473120582d31342e323320592d31382e38380a473120582d31342e363520592d31382e33380a3b657874727564655f726174696f3a312e34360a473120582d31342e363520592d31382e33380a473120582d31342e303620592d31372e353220453132332e3632303920463330300a3b657874727564655f726174696f3a310a473120582d31342e323320592d31382e38382046333030300a473120582d31352e313320592d31392e37320a473120582d31352e323720592d32302e32380a473120582d31352e313520592d32302e35330a3b657874727564655f726174696f3a302e39360a473120582d31352e313520592d32302e35330a473120582d31352e373520592d32312e333320453132332e3633343620463330300a473120582d31362e303020592d32312e313620453132332e363338370a3b7368656c6c0a3b657874727564655f726174696f3a310a473120453131382e313338372046313830300a473120582d31322e333620592d31352e35372046333030300a473120453132332e363338372046313830300a473120582d31322e333620592d31352e35372046333030300a473120582d31312e383720592d31352e313020453132332e3634383420463330300a3b7368656c6c0a473120453131382e313438342046313830300a473120582d31312e313320592d31362e35302046333030300a473120453132332e363438342046313830300a473120582d31312e313320592d31362e35302046333030300a473120582d31302e383220592d31352e393120453132332e3635373920463330300a3b70657263656e740a3b6c617965723a302e30380a3b7368656c6c0a473120453131382e313537392046313830300a473120582d31322e353020592d31382e33352046333030300a4731205a31312e31323020463432300a473120453132332e363537392046313830300a473120582d31322e353020592d31382e33352046333030300a473120582d31322e363020592d31382e323720453132332e3635393720463330300a473120582d31332e363620592d31392e343820453132332e363832360a473120582d31332e343620592d31392e363320453132332e363836310a473120582d31322e393120592d31392e313020453132332e363937300a473120582d31322e383520592d31392e303320453132332e363938330a473120582d31322e353020592d31382e333520453132332e373039320a3b7368656c6c0a473120453131382e323039322046313830300a473120582d31332e363720592d31372e34362046333030300a473120453132332e373039322046313830300a473120582d31332e363720592d31372e34362046333030300a473120582d31332e373720592d31372e333920453132332e3731313020463330300a473120582d31342e313720592d31372e373920453132332e373139300a473120582d31342e343720592d31382e313620453132332e373235380a473120582d31342e373420592d31382e363620453132332e373333390a473120582d31342e353420592d31382e383220453132332e373337350a473120582d31332e363720592d31372e343620453132332e373630350a3b7368656c6c0a473120453131382e323630352046313830300a473120582d31352e333820592d32302e31302046333030300a473120453132332e373630352046313830300a473120582d31352e333820592d32302e31302046333030300a473120582d31342e363720592d31392e313420453132332e3737373520463330300a3b7368656c6c0a473120453131382e323737352046313830300a473120582d31342e363720592d32302e36342046333030300a473120453132332e373737352046313830300a473120582d31342e363720592d32302e36342046333030300a473120582d31332e393420592d31392e373020453132332e3739343520463330300a4d3130370a3b70657263656e740a473120453131382e323934352046313830300a3b656e642067636f64650a4d3130342053302054300a4d3134302053302054300a47313632205a0a473238205820590a4d31333220582059204120420a4d3635320a4739310a4d31380a"; 28 | @Test 29 | void testLastPackage() throws DecoderException { 30 | String crc32Checksum = Util.calcChecksum(Hex.decodeHex(last)); 31 | assertEquals("66d6707b", crc32Checksum); 32 | } 33 | 34 | String box_gold = "5a5aa5a50000004a00000" 35 | + "828" 36 | + "b5f2a7b23120582d382e32362059332e303120453838342e313431330a473120582d382e32362059332e353820453838342e313537360a47312058332e353820592d382e323620453838342e363334330a47312058342e313520592d382e323620453838342e363530360a473120582d382e32362059342e313520453838352e313530330a473120582d382e32362059342e373220453838352e313636350a47312058342e373220592d382e323620453838352e363839310a47312058352e333020592d382e323620453838352e373035360a473120582d382e32362059352e333020453838362e323531360a473120582d382e32362059352e383720453838362e323637390a47312058352e383720592d382e323620453838362e383336380a47312058362e343420592d382e323620453838362e383533310a473120582d382e32362059362e343420453838372e343434390a473120582d382e32362059372e303220453838372e343631340a47312058372e303220592d382e323620453838382e303736370a47312058372e353920592d382e323620453838382e303933300a473120582d382e32362059372e353920453838382e373331310a473120582d382e32362059382e313620453838382e373437340a47312058382e313620592d382e323620453838392e343038350a47312058382e323620592d382e323620453838392e343131340a47312058382e323620592d372e373820453838392e343235300a473120582d372e37382059382e323620453839302e303730390a473120582d372e32312059382e323620453839302e303837310a47312058382e323620592d372e323120453839302e373130300a47312058382e323620592d362e363420453839302e373236330a473120582d362e36342059382e323620453839312e333236320a473120582d362e30372059382e323620453839312e333432340a47312058382e323620592d362e303720453839312e393139340a47312058382e323620592d352e343920453839312e393335390a473120582d352e34392059382e323620453839322e343839360a473120582d342e39322059382e323620453839322e353035380a47312058382e323620592d342e393220453839332e303336350a47312058382e323620592d342e333520453839332e303532370a473120582d342e33352059382e323620453839332e353630350a473120582d332e37372059382e323620453839332e353737300a47312058382e323620592d332e373720453839342e303631340a47312058382e323620592d332e323020453839342e303737360a473120582d332e32302059382e323620453839342e353339310a473120582d322e36332059382e323620453839342e353535330a47312058382e323620592d322e363320453839342e393933380a47312058382e323620592d322e303620453839352e303130310a473120582d322e30362059382e323620453839352e343235350a473120582d312e34382059382e323620453839352e343432310a47312058382e323620592d312e343820453839352e383334320a47312058382e323620592d2e393120453839352e383530350a473120582d2e39312059382e323620453839362e323139370a473120582d2e33342059382e323620453839362e323336300a47312058382e323620592d2e333420453839362e353832320a47312058382e323620592e323320453839362e353938340a473120582e32332059382e323620453839362e393231380a473120582e38312059382e323620453839362e393338320a47312058382e323620592e383120453839372e323338320a47312058382e32362059312e333820453839372e323534350a47312058312e33382059382e323620453839372e353331340a47312058312e39352059382e323620453839372e353437370a47312058382e32362059312e393520453839"; 37 | String box_last = "5a5aa5a50000004a00000" 38 | + "b7e" 39 | + "b5f2a7b23120582d382e32362059332e303120453838342e313431330a473120582d382e32362059332e353820453838342e313537360a47312058332e353820592d382e323620453838342e363334330a47312058342e313520592d382e323620453838342e363530360a473120582d382e32362059342e313520453838352e313530330a473120582d382e32362059342e373220453838352e313636350a47312058342e373220592d382e323620453838352e363839310a47312058352e333020592d382e323620453838352e373035360a473120582d382e32362059352e333020453838362e323531360a473120582d382e32362059352e383720453838362e323637390a47312058352e383720592d382e323620453838362e383336380a47312058362e343420592d382e323620453838362e383533310a473120582d382e32362059362e343420453838372e343434390a473120582d382e32362059372e303220453838372e343631340a47312058372e303220592d382e323620453838382e303736370a47312058372e353920592d382e323620453838382e303933300a473120582d382e32362059372e353920453838382e373331310a473120582d382e32362059382e313620453838382e373437340a47312058382e313620592d382e323620453838392e343038350a47312058382e323620592d382e323620453838392e343131340a47312058382e323620592d372e373820453838392e343235300a473120582d372e37382059382e323620453839302e303730390a473120582d372e32312059382e323620453839302e303837310a47312058382e323620592d372e323120453839302e373130300a47312058382e323620592d362e363420453839302e373236330a473120582d362e36342059382e323620453839312e333236320a473120582d362e30372059382e323620453839312e333432340a47312058382e323620592d362e303720453839312e393139340a47312058382e323620592d352e343920453839312e393335390a473120582d352e34392059382e323620453839322e343839360a473120582d342e39322059382e323620453839322e353035380a47312058382e323620592d342e393220453839332e303336350a47312058382e323620592d342e333520453839332e303532370a473120582d342e33352059382e323620453839332e353630350a473120582d332e37372059382e323620453839332e353737300a47312058382e323620592d332e373720453839342e303631340a47312058382e323620592d332e323020453839342e303737360a473120582d332e32302059382e323620453839342e353339310a473120582d322e36332059382e323620453839342e353535330a47312058382e323620592d322e363320453839342e393933380a47312058382e323620592d322e303620453839352e303130310a473120582d322e30362059382e323620453839352e343235350a473120582d312e34382059382e323620453839352e343432310a47312058382e323620592d312e343820453839352e383334320a47312058382e323620592d2e393120453839352e383530350a473120582d2e39312059382e323620453839362e323139370a473120582d2e33342059382e323620453839362e323336300a47312058382e323620592d2e333420453839362e353832320a47312058382e323620592e323320453839362e353938340a473120582e32332059382e323620453839362e393231380a473120582e38312059382e323620453839362e393338320a47312058382e323620592e383120453839372e323338320a47312058382e32362059312e333820453839372e323534350a47312058312e33382059382e323620453839372e353331340a47312058312e39352059382e323620453839372e353437370a47312058382e32362059312e393520453839"; 40 | @Test 41 | void testBoxLastPackage() throws DecoderException { 42 | String crc32Checksum = Util.calcChecksum(Hex.decodeHex(box_last)); 43 | assertEquals("4d7ad8c9", crc32Checksum); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/de/slg/ddnss/printertool/clients/FlashForgeDiscoverClientTest.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertNotNull; 4 | 5 | import org.junit.jupiter.api.Test; 6 | import org.junit.jupiter.api.Timeout; 7 | 8 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException; 9 | 10 | class FlashForgeDiscoverClientTest { 11 | 12 | @Test 13 | @Timeout(value = 5) 14 | void discoverTest() throws FlashForgePrinterException { 15 | FlashForgeDiscoverClient client = new FlashForgeDiscoverClient(); 16 | assertNotNull(client.discoverPrinter().getPrinterAddress()); 17 | assertNotNull(client.getPrinterAddress()); 18 | assertNotNull(client.getPrinterName()); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/de/slg/ddnss/printertool/clients/JUnit5TestSuite.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import org.junit.platform.runner.JUnitPlatform; 4 | import org.junit.platform.suite.api.SelectPackages; 5 | import org.junit.runner.RunWith; 6 | 7 | @RunWith(JUnitPlatform.class) 8 | @SelectPackages("de.slg.ddnss.printertool.clients") 9 | public class JUnit5TestSuite { 10 | } -------------------------------------------------------------------------------- /src/test/java/de/slg/ddnss/printertool/clients/TcpClientTest.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import static de.slg.ddnss.printertool.clients.AdventurerCommands.*; 4 | import static org.junit.jupiter.api.Assertions.assertEquals; 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; 6 | import static org.junit.jupiter.api.Assertions.assertTrue; 7 | 8 | import java.io.IOException; 9 | import java.nio.file.Files; 10 | import java.nio.file.Path; 11 | import java.nio.file.Paths; 12 | import java.util.List; 13 | 14 | import org.junit.jupiter.api.AfterAll; 15 | import org.junit.jupiter.api.Assertions; 16 | import org.junit.jupiter.api.BeforeAll; 17 | import org.junit.jupiter.api.Test; 18 | 19 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException; 20 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterTransferException; 21 | import de.slg.ddnss.printertool.util.Util; 22 | 23 | class TcpClientTest { 24 | 25 | private static TcpPrinterClient client; 26 | private static String printerAddress; 27 | 28 | @BeforeAll 29 | static void init() { 30 | try { 31 | FlashForgeDiscoverClient flashForgeDiscoverClient = new FlashForgeDiscoverClient(); 32 | flashForgeDiscoverClient.discoverPrinter(); 33 | printerAddress = flashForgeDiscoverClient.getPrinterAddress(); 34 | assertNotNull(printerAddress); 35 | client = new TcpPrinterClient(printerAddress); 36 | assertNotNull(client); 37 | } catch (FlashForgePrinterException e) { 38 | e.printStackTrace(); 39 | } 40 | } 41 | 42 | @Test 43 | void m601test() throws FlashForgePrinterException { 44 | String sendOk = client.sendCommand(CMD_HELLO); 45 | System.out.println(sendOk); 46 | assertEquals("CMD M601 Received.\nControl Success.\nok", sendOk); 47 | } 48 | 49 | @Test 50 | void m115test() throws FlashForgePrinterException { 51 | String sendOk = client.sendCommand(CMD_INFO_STATUS); 52 | System.out.println(sendOk); 53 | assertEquals("CMD M115 Received.\nMachine Type: FlashForge Adventurer III\n" + "Machine Name: My 3D Printer\n" 54 | + "Firmware: v1.1.7\n" + "SN: SNFFAD229083\n" + "X: 150 Y: 150 Z: 150\n" + "Tool Count: 1\n" 55 | + "Mac Address: 88:A9:A7:90:77:A5\n\nok", sendOk); 56 | } 57 | 58 | @Test 59 | void m650test() throws FlashForgePrinterException { 60 | String sendOk = client.sendCommand(CMD_INFO_CAL); 61 | System.out.println(sendOk); 62 | assertEquals("CMD M650 Received.\nX: 1.0 Y: 0.5\nok", sendOk); 63 | } 64 | 65 | @Test 66 | void m114test() throws FlashForgePrinterException { 67 | String sendOk = client.sendCommand(CMD_INFO_XYZAB); 68 | System.out.println(sendOk); 69 | } 70 | 71 | @Test 72 | void m119test() throws FlashForgePrinterException { 73 | String sendOk = client.sendCommand(CMD_INFO); 74 | System.out.println(sendOk); 75 | assertTrue(sendOk.contains("Received")); 76 | assertTrue(sendOk.contains("ok")); 77 | } 78 | 79 | @Test 80 | void m27test() throws FlashForgePrinterException { 81 | String sendOk; 82 | sendOk = client.sendCommand(CMD_PRINT_STATUS); 83 | System.out.println(sendOk); 84 | assertTrue(sendOk.contains("Received")); 85 | assertTrue(sendOk.contains("ok")); 86 | } 87 | 88 | @Test 89 | void m105test() throws FlashForgePrinterException { 90 | String sendOk; 91 | sendOk = client.sendCommand(CMD_TEMP); 92 | System.out.println(sendOk); 93 | assertTrue(sendOk.contains("Received")); 94 | assertTrue(sendOk.contains("ok")); 95 | } 96 | 97 | @Test 98 | void ledTest() throws FlashForgePrinterException { 99 | String sendOk; 100 | sendOk = client.sendCommand(CMD_LED_OFF); 101 | System.out.println(sendOk); 102 | assertTrue(sendOk.contains("Received")); 103 | assertTrue(sendOk.contains("ok")); 104 | 105 | sendOk = client.sendCommand(CMD_LED_ON); 106 | System.out.println(sendOk); 107 | assertTrue(sendOk.contains("Received")); 108 | assertTrue(sendOk.contains("ok")); 109 | } 110 | 111 | @Test 112 | void printTest() throws FlashForgePrinterException { 113 | try { 114 | Path fileToPrint = Paths.get("20mm_Box.gx"); 115 | byte[] readAllLines = Files.readAllBytes(fileToPrint); 116 | String filename = fileToPrint.getFileName().toString(); 117 | System.out.println("File: " + filename + "/" + readAllLines.length + "byte"); 118 | 119 | System.out.println(client.sendCommand(CMD_PREPARE_PRINT.replaceAll("%%size%%", "" + readAllLines.length) 120 | .replaceAll("%%filename%%", filename))); 121 | 122 | try { 123 | List gcode = Util.prepareRawData(readAllLines); 124 | client.sendRawData(gcode); 125 | System.out.println(client.sendCommand(CMD_SAVE_FILE)); 126 | System.out.println(client.sendCommand(CMD_PRINT_START.replaceAll("%%filename%%", filename))); 127 | 128 | System.out.println(client.sendCommand(CMD_PRINT_STATUS)); 129 | } catch (FlashForgePrinterTransferException e) { 130 | e.printStackTrace(); 131 | } 132 | 133 | } catch (IOException e) { 134 | e.printStackTrace(); 135 | } 136 | } 137 | 138 | @Test 139 | void unknownHostExceptionTest() { 140 | Assertions.assertThrows(FlashForgePrinterTransferException.class, () -> { 141 | new TcpPrinterClient("0.0.0.a"); 142 | }); 143 | } 144 | 145 | @AfterAll 146 | static void printStopTest() throws FlashForgePrinterException { 147 | String sendOk = client.sendCommand(CMD_PRINT_STOP); 148 | System.out.println(sendOk); 149 | assertTrue(sendOk.contains("Received")); 150 | assertTrue(sendOk.contains("ok")); 151 | 152 | sendOk = client.sendCommand(CMD_BYE); 153 | System.out.println(sendOk); 154 | assertTrue(sendOk.equals("CMD M602 Received.\nControl Release.\nok")); 155 | client.close(); 156 | } 157 | 158 | } 159 | -------------------------------------------------------------------------------- /src/test/java/de/slg/ddnss/printertool/clients/UpdClientTest.java: -------------------------------------------------------------------------------- 1 | package de.slg.ddnss.printertool.clients; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertNotNull; 4 | 5 | import java.net.DatagramPacket; 6 | 7 | import org.junit.jupiter.api.BeforeAll; 8 | import org.junit.jupiter.api.Test; 9 | import org.junit.jupiter.api.Timeout; 10 | 11 | import de.slg.ddnss.printertool.exceptions.FlashForgePrinterException; 12 | 13 | class UpdClientTest { 14 | 15 | static UdpDiscoveryClient client; 16 | 17 | @BeforeAll 18 | static void init() throws FlashForgePrinterException { 19 | client = new UdpDiscoveryClient(); 20 | } 21 | 22 | @Test 23 | @Timeout(value = 5) 24 | void test() throws FlashForgePrinterException { 25 | DatagramPacket receiveMessage = client.sendMessage("c0a800de46500000"); 26 | assertNotNull(receiveMessage); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/test/resources/20mm_Box.gx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Slugger2k/FlashForgePrinterApi/6a30121df522d5054838f0c0f876fd4ffcfb8327/src/test/resources/20mm_Box.gx -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{dd.MM.yyyy HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{50}.%M - %msg%n 8 | 9 | 10 | 11 | 12 | 13 | 14 | --------------------------------------------------------------------------------