├── .gitignore ├── COPYING ├── ChangeLog ├── README.md ├── build.xml ├── lib ├── CopyLibs │ └── org-netbeans-modules-java-j2seproject-copylibstask.jar ├── gson-2.2.4.jar ├── junit_4 │ ├── junit-4.10-javadoc.jar │ ├── junit-4.10-sources.jar │ └── junit-4.10.jar ├── log4j.jar ├── nblibraries.properties └── unirest-java-1.3.4-SNAPSHOT-jar-with-dependencies.jar ├── manifest.mf ├── nbproject ├── build-impl.xml ├── genfiles.properties ├── project.properties └── project.xml ├── src └── com │ └── angryelectron │ └── thingspeak │ ├── Channel.java │ ├── Entry.java │ ├── Feed.java │ ├── FeedParameters.java │ ├── ThingSpeakException.java │ ├── log4j │ ├── ThingSpeakAppender.java │ └── package.html │ ├── package.html │ └── pub │ ├── PublicChannel.java │ ├── PublicChannelCollection.java │ ├── PublicIterator.java │ ├── PublicJSONResult.java │ └── package.html └── test └── com └── angryelectron └── thingspeak ├── FeedTest.java ├── TestChannelSettings.java ├── UpdateTest.java └── log4j └── ThingSpeakAppenderTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | nbproject/private 2 | build 3 | dist 4 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 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 | 676 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2014-11-19 Andrew Bythell 2 | 3 | * release v1.1.1 4 | * fix typos 5 | 6 | 2014-11-06 Andrew Bythell 7 | 8 | * release v1.1 9 | * improved test methods 10 | * fix "lastEntry" bug 11 | * remove tests from default ant target 12 | 13 | 2014-01-09 Andrew Bythell 14 | 15 | * initial release 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ThingSpeak Java Client 2 | === 3 | A Java client for the [ThingSpeak](http://thingspeak.com) Internet of Things. 4 | Implements all aspects of the ThingSpeak API and can be used to update channel 5 | data, retrieve and examine feeds, and query public channels. It supports the 6 | hosted ThingSpeak server at api.thingspeak.com as well as self-hosted 7 | open-source servers ([GitHub Source](https://github.com/iobridge/thingspeak)). 8 | 9 | Also included: An appender for log4j - post data to ThingSpeak channels using 10 | Logger framework. 11 | 12 | How To Install 13 | --- 14 | Get the source by [downloading a zip 15 | file](https://github.com/angryelectron/thingspeak-java/archive/master.zip) or 16 | by cloning the git repository 17 | https://github.com/angryelectron/thingspeak-java.git . Building the source 18 | requires the Java 7 SDK and Apache Ant, or use the [Netbeans 19 | IDE](http://netbeans.org). 20 | 21 | Here is an example of how to install the client from the command line in 22 | Ubuntu/Debian/Raspbian with a minimal build environment: 23 | 24 | ``` 25 | sudo apt-get update 26 | sudo apt-get -y install openjdk-7-jdk git ant 27 | git clone https://github.com/angryelectron/thingspeak-java.git 28 | cd thingspeak-java 29 | ant 30 | ``` 31 | 32 | To run optional tests, run 'ant test'. Due to the rate limit of the public 33 | Thingspeak server (15sec), tests can take a long time to run. After building, 34 | the jars, docs, and dependencies can be found in thingspeak/dist. 35 | 36 | How To Use 37 | --- 38 | Add thingspeak-x.y.jar to your project and the following dependencies: 39 | 40 | * [Unirest](http://unirest.io) 41 | * [GSON](http://code.google.com/p/google-gson/) 42 | 43 | Dependencies can be found in thingspeak/dist/lib after building the source. 44 | Refer to the included javadocs for more details. The [ThingSpeak API 45 | Documentation](http://community.thingspeak.com/documentation/api/#thingspeak_api) 46 | is also a good source of additional information about using the API. 47 | 48 | If you encounter any issues with the ThingSpeak Java Client, please use the 49 | [GitHub issue 50 | tracker](https://github.com/angryelectron/thingspeak-java/issues). 51 | 52 | Examples 53 | --- 54 | 55 | Here is how to write "hello" to field1 of ThingSpeak public channel 1234, then read it back. 56 | 57 | ``` 58 | import com.angryelectron.thingspeak.*; 59 | 60 | public class ThingExample { 61 | 62 | public static void main(String[] args) throws Exception { 63 | String apiWriteKey = "your-channel-write-key"; 64 | Channel channel = new Channel(1, apiWriteKey); 65 | try { 66 | // write something 67 | Entry writeEntry = new Entry(); 68 | writeEntry.setField(1, "Hello"); 69 | // read it back 70 | Entry readEntry = channel.getLastChannelEntry(); 71 | System.out.println(readEntry.getField(1)); 72 | } catch(Exception ex) { 73 | System.out.println(ex); 74 | } 75 | } 76 | 77 | } 78 | 79 | ``` 80 | 81 | Please refer to thingspeak/dist/javadoc for more information about customzing 82 | channel feeds, searching public channels, using open-source servers, and all 83 | the other operations supported by the ThingSpeak API. 84 | 85 | log4j Appender 86 | --- 87 | Use log4j to update ThingSpeak channels. Date, Level, and Message are 88 | 'fields', written as an 'entry'. Setup a new ThingSpeak channel with these 89 | three fields, then pass the channel number and API write-key to the appender. 90 | 91 | Here's how to configure the appender and send a test 92 | message (just add your own channelNumber and apiWriteKey): 93 | 94 | ``` 95 | ThingSpeakAppender appender = new ThingSpeakAppender(); 96 | appender.configureChannel(channelNumber, apiWriteKey, "http://api.thingspeak.com"); 97 | appender.setThreshold(Level.INFO); 98 | appender.activateOptions(); 99 | Logger.getRootLogger().addAppender(appender); 100 | Logger.getLogger(this.getClass()).log(Level.INFO, "Hello World"); 101 | ``` 102 | 103 | You can also configure the appender via log4j.properties: 104 | 105 | ``` 106 | log4j.rootLogger=INFO, ThingSpeak 107 | log4j.appender.ThingSpeak=com.angryelectron.thingspeak.log4j.ThingSpeakAppender 108 | com.angryelectron.thingspeak.log4j.channelNumber = YOUR_CHANNEL_NUMBER 109 | com.angryelectron.thingspeak.log4j.apiWriteKey = YOUR_API_WRITE_KEY 110 | ``` 111 | To use your own server (other than api.thingspeak.com): 112 | 113 | ``` 114 | com.angryelectron.thingspeak.log4j.server = YOUR_THINGSPEAK_SERVER_URL 115 | ``` 116 | 117 | See the javadocs for more details. 118 | 119 | About 120 | --- 121 | * ThingSpeak Java Client 122 | * Copyright 2014, Andrew Bythell 123 | * http://angryelectron.com/projects/thingspeak-java-client/ 124 | 125 | The ThingSpeak Java Client is free software: you can redistribute it and/or 126 | modify it under the terms of the GNU General Public License as published by 127 | the Free Software Foundation, either version 3 of the License, or (at your 128 | option) any later version. 129 | 130 | The ThingSpeak Java Client is distributed in the hope that it will be useful, 131 | but WITHOUT ANY WARRANTY; without even the implied warranty of 132 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 133 | Public License for more details. 134 | 135 | You should have received a copy of the GNU General Public License along with 136 | the ThingSpeak Java Client. If not, see . 137 | -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project ThingSpeak. 12 | 13 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angryelectron/thingspeak-java/29c5c38f445534613d08214972d741cfad919b5a/lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar -------------------------------------------------------------------------------- /lib/gson-2.2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angryelectron/thingspeak-java/29c5c38f445534613d08214972d741cfad919b5a/lib/gson-2.2.4.jar -------------------------------------------------------------------------------- /lib/junit_4/junit-4.10-javadoc.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angryelectron/thingspeak-java/29c5c38f445534613d08214972d741cfad919b5a/lib/junit_4/junit-4.10-javadoc.jar -------------------------------------------------------------------------------- /lib/junit_4/junit-4.10-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angryelectron/thingspeak-java/29c5c38f445534613d08214972d741cfad919b5a/lib/junit_4/junit-4.10-sources.jar -------------------------------------------------------------------------------- /lib/junit_4/junit-4.10.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angryelectron/thingspeak-java/29c5c38f445534613d08214972d741cfad919b5a/lib/junit_4/junit-4.10.jar -------------------------------------------------------------------------------- /lib/log4j.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angryelectron/thingspeak-java/29c5c38f445534613d08214972d741cfad919b5a/lib/log4j.jar -------------------------------------------------------------------------------- /lib/nblibraries.properties: -------------------------------------------------------------------------------- 1 | libs.CopyLibs.classpath=\ 2 | ${base}/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar 3 | libs.CopyLibs.displayName=CopyLibs Task 4 | libs.CopyLibs.prop-version=2.0 5 | libs.junit.classpath=\ 6 | ${base}/junit/junit-3.8.2.jar 7 | libs.junit.displayName=JUnit 3.8.2 8 | libs.junit.javadoc=\ 9 | ${base}/junit/junit-3.8.2-api.zip 10 | libs.junit.prop-maven-dependencies=junit:junit:3.8.2:jar 11 | libs.junit_4.classpath=\ 12 | ${base}/junit_4/junit-4.10.jar 13 | libs.junit_4.displayName=JUnit 4.10 14 | libs.junit_4.javadoc=\ 15 | ${base}/junit_4/junit-4.10-javadoc.jar 16 | libs.junit_4.prop-maven-dependencies=junit:junit:4.10:jar 17 | libs.junit_4.src=\ 18 | ${base}/junit_4/junit-4.10-sources.jar 19 | -------------------------------------------------------------------------------- /lib/unirest-java-1.3.4-SNAPSHOT-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/angryelectron/thingspeak-java/29c5c38f445534613d08214972d741cfad919b5a/lib/unirest-java-1.3.4-SNAPSHOT-jar-with-dependencies.jar -------------------------------------------------------------------------------- /manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /nbproject/build-impl.xml: -------------------------------------------------------------------------------- 1 | 2 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | Must set src.dir 249 | Must set test.src.dir 250 | Must set build.dir 251 | Must set dist.dir 252 | Must set build.classes.dir 253 | Must set dist.javadoc.dir 254 | Must set build.test.classes.dir 255 | Must set build.test.results.dir 256 | Must set build.classes.excludes 257 | Must set dist.jar 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | Must set javac.includes 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | No tests executed. 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | Must set JVM to use for profiling in profiler.info.jvm 738 | Must set profiler agent JVM arguments in profiler.info.jvmargs.agent 739 | 740 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | Must select some files in the IDE or set javac.includes 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | To run this application from the command line without Ant, try: 1017 | 1018 | java -jar "${dist.jar.resolved}" 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | Must select one file in the IDE or set run.class 1066 | 1067 | 1068 | 1069 | Must select one file in the IDE or set run.class 1070 | 1071 | 1072 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094 | 1095 | 1096 | Must select one file in the IDE or set debug.class 1097 | 1098 | 1099 | 1100 | 1101 | Must select one file in the IDE or set debug.class 1102 | 1103 | 1104 | 1105 | 1106 | Must set fix.includes 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1118 | 1121 | 1122 | This target only works when run from inside the NetBeans IDE. 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | Must select one file in the IDE or set profile.class 1132 | This target only works when run from inside the NetBeans IDE. 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | This target only works when run from inside the NetBeans IDE. 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | This target only works when run from inside the NetBeans IDE. 1155 | 1156 | 1157 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | 1172 | 1173 | 1174 | 1175 | 1176 | 1179 | 1180 | 1181 | 1182 | 1183 | 1184 | 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | Must select one file in the IDE or set run.class 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | Must select some files in the IDE or set test.includes 1199 | 1200 | 1201 | 1202 | 1203 | Must select one file in the IDE or set run.class 1204 | 1205 | 1206 | 1207 | 1208 | Must select one file in the IDE or set applet.url 1209 | 1210 | 1211 | 1212 | 1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 | 1231 | 1232 | 1233 | 1234 | 1235 | 1236 | 1237 | 1238 | 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251 | 1252 | 1253 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | 1264 | 1265 | 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 1278 | 1279 | 1280 | 1281 | 1282 | 1283 | 1284 | Must select some files in the IDE or set javac.includes 1285 | 1286 | 1287 | 1288 | 1289 | 1290 | 1291 | 1292 | 1293 | 1294 | 1295 | 1296 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | Some tests failed; see details above. 1309 | 1310 | 1311 | 1312 | 1313 | 1314 | 1315 | 1316 | 1317 | Must select some files in the IDE or set test.includes 1318 | 1319 | 1320 | 1321 | Some tests failed; see details above. 1322 | 1323 | 1324 | 1325 | Must select some files in the IDE or set test.class 1326 | Must select some method in the IDE or set test.method 1327 | 1328 | 1329 | 1330 | Some tests failed; see details above. 1331 | 1332 | 1333 | 1338 | 1339 | Must select one file in the IDE or set test.class 1340 | 1341 | 1342 | 1343 | Must select one file in the IDE or set test.class 1344 | Must select some method in the IDE or set test.method 1345 | 1346 | 1347 | 1348 | 1349 | 1350 | 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1361 | 1362 | Must select one file in the IDE or set applet.url 1363 | 1364 | 1365 | 1366 | 1367 | 1368 | 1369 | 1374 | 1375 | Must select one file in the IDE or set applet.url 1376 | 1377 | 1378 | 1379 | 1380 | 1381 | 1382 | 1383 | 1388 | 1389 | 1390 | 1391 | 1392 | 1393 | 1394 | 1395 | 1396 | 1397 | 1398 | 1399 | 1400 | 1401 | 1402 | 1403 | 1404 | 1405 | 1406 | 1407 | 1408 | 1409 | 1410 | 1411 | 1412 | 1413 | 1414 | 1415 | 1416 | 1417 | 1418 | 1419 | 1420 | 1421 | 1422 | 1423 | 1424 | 1425 | 1426 | 1427 | 1428 | 1429 | 1430 | 1431 | 1432 | 1433 | -------------------------------------------------------------------------------- /nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=c6c9b9b8 2 | build.xml.script.CRC32=93270010 3 | build.xml.stylesheet.CRC32=8064a381@1.68.1.46 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=faafe11a 7 | nbproject/build-impl.xml.script.CRC32=255c706a 8 | nbproject/build-impl.xml.stylesheet.CRC32=5a01deb7@1.68.1.46 9 | -------------------------------------------------------------------------------- /nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processors.list= 4 | annotation.processing.run.all.processors=true 5 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 6 | application.desc=Java Client for the ThingSpeak API. 7 | application.homepage=http://github.com/angryelectron/thingspeak 8 | application.title=thingspeak-1.1.1 9 | application.vendor=angryelectron 10 | build.classes.dir=${build.dir}/classes 11 | build.classes.excludes=**/*.java,**/*.form 12 | # This directory is removed when the project is cleaned: 13 | build.dir=build 14 | build.generated.dir=${build.dir}/generated 15 | build.generated.sources.dir=${build.dir}/generated-sources 16 | # Only compile against the classpath explicitly listed here: 17 | build.sysclasspath=ignore 18 | build.test.classes.dir=${build.dir}/test/classes 19 | build.test.results.dir=${build.dir}/test/results 20 | # Uncomment to specify the preferred debugger connection transport: 21 | #debug.transport=dt_socket 22 | debug.classpath=\ 23 | ${run.classpath} 24 | debug.test.classpath=\ 25 | ${run.test.classpath} 26 | # Files in build.classes.dir which should be excluded from distribution jar 27 | dist.archive.excludes= 28 | # This directory is removed when the project is cleaned: 29 | dist.dir=dist 30 | dist.jar=${dist.dir}/${application.title}.jar 31 | dist.javadoc.dir=${dist.dir}/javadoc 32 | endorsed.classpath= 33 | excludes= 34 | file.reference.gson-2.2.4.jar=lib/gson-2.2.4.jar 35 | file.reference.log4j.jar=lib/log4j.jar 36 | file.reference.unirest-java-1.3.4-SNAPSHOT-jar-with-dependencies.jar=lib/unirest-java-1.3.4-SNAPSHOT-jar-with-dependencies.jar 37 | includes=** 38 | jar.archive.disabled=${jnlp.enabled} 39 | jar.compress=false 40 | jar.index=${jnlp.enabled} 41 | javac.classpath=\ 42 | ${file.reference.unirest-java-1.3.4-SNAPSHOT-jar-with-dependencies.jar}:\ 43 | ${file.reference.gson-2.2.4.jar}:\ 44 | ${file.reference.log4j.jar} 45 | # Space-separated list of extra javac options 46 | javac.compilerargs= 47 | javac.deprecation=false 48 | javac.processorpath=\ 49 | ${javac.classpath} 50 | javac.source=1.7 51 | javac.target=1.7 52 | javac.test.classpath=\ 53 | ${javac.classpath}:\ 54 | ${build.classes.dir}:\ 55 | ${libs.junit_4.classpath} 56 | javac.test.processorpath=\ 57 | ${javac.test.classpath} 58 | javadoc.additionalparam= 59 | javadoc.author=false 60 | javadoc.encoding=${source.encoding} 61 | javadoc.noindex=false 62 | javadoc.nonavbar=false 63 | javadoc.notree=false 64 | javadoc.private=false 65 | javadoc.reference.gson-2.2.4.jar=../api/google-gson-2.2.4/gson-2.2.4-javadoc.jar 66 | javadoc.reference.unirest-java-1.3.4-SNAPSHOT-jar-with-dependencies.jar=../unirest-java/target/unirest-java-1.3.4-SNAPSHOT-javadoc.jar 67 | javadoc.splitindex=true 68 | javadoc.use=true 69 | javadoc.version=false 70 | javadoc.windowtitle=ThingSpeak Java Client 71 | jnlp.codebase.type=no.codebase 72 | jnlp.descriptor=application 73 | jnlp.enabled=false 74 | jnlp.mixed.code=default 75 | jnlp.offline-allowed=false 76 | jnlp.signed=false 77 | jnlp.signing= 78 | jnlp.signing.alias= 79 | jnlp.signing.keystore= 80 | main.class=com.angryelectron.thingspeak.Main 81 | # Optional override of default Codebase manifest attribute, use to prevent RIAs from being repurposed 82 | manifest.custom.codebase= 83 | # Optional override of default Permissions manifest attribute (supported values: sandbox, all-permissions) 84 | manifest.custom.permissions= 85 | manifest.file=manifest.mf 86 | meta.inf.dir=${src.dir}/META-INF 87 | mkdist.disabled=false 88 | platform.active=default_platform 89 | run.classpath=\ 90 | ${javac.classpath}:\ 91 | ${build.classes.dir} 92 | # Space-separated list of JVM arguments used when running the project. 93 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 94 | # To set system properties for unit tests define test-sys-prop.name=value: 95 | run.jvmargs= 96 | run.test.classpath=\ 97 | ${javac.test.classpath}:\ 98 | ${build.test.classes.dir} 99 | source.encoding=UTF-8 100 | src.dir=src 101 | test.src.dir=test 102 | -------------------------------------------------------------------------------- /nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | 7 | ThingSpeak 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | ./lib/nblibraries.properties 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/Channel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak; 21 | 22 | import com.google.gson.Gson; 23 | import com.google.gson.GsonBuilder; 24 | import com.mashape.unirest.http.HttpResponse; 25 | import com.mashape.unirest.http.JsonNode; 26 | import com.mashape.unirest.http.Unirest; 27 | import com.mashape.unirest.http.exceptions.UnirestException; 28 | import com.mashape.unirest.request.GetRequest; 29 | import java.util.HashMap; 30 | 31 | /** 32 | * Thingspeak Channel. Methods for updating and requesting feeds and entries 33 | * from Thingspeak channels. 34 | */ 35 | public class Channel { 36 | 37 | //TODO: the API url should be configurable so the client can be used with 38 | //self-hosted servers. 39 | private String APIURL = "http://api.thingspeak.com"; 40 | private static final String APIHEADER = "X-THINGSPEAKAPIKEY"; 41 | private final Integer channelId; 42 | private String readAPIKey; 43 | private String writeAPIKey; 44 | private final Boolean isPublic; 45 | private final HashMap fields = new HashMap<>(); 46 | private final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").create(); 47 | 48 | /** 49 | * Constructor for a public, read-only, Thingspeak channel. This type of 50 | * channel cannot be updated. 51 | * 52 | * @param channelId Channel Id. 53 | */ 54 | public Channel(Integer channelId) { 55 | this.isPublic = true; 56 | this.channelId = channelId; 57 | } 58 | 59 | /** 60 | * Constructor for a public, writeable, Thingspeak channel. 61 | * 62 | * @param channelId Channel Id. 63 | * @param writeKey API Key for the channel. See 64 | * https://thingspeak.com/channels/<channelId>#apikeys 65 | */ 66 | public Channel(Integer channelId, String writeKey) { 67 | this.isPublic = true; 68 | this.channelId = channelId; 69 | this.writeAPIKey = writeKey; 70 | } 71 | 72 | /** 73 | * Constructor for a private, writeable, Thingspeak channel. 74 | * 75 | * @param channelId Channel Id. 76 | * @param writeKey Write API Key. See 77 | * https://thingspeak.com/channels/<channelId>#apikeys. 78 | * @param readKey Read API Key. See 79 | * https://thingspeak.com/channels/<channelId>#apikeys. 80 | */ 81 | public Channel(Integer channelId, String writeKey, String readKey) { 82 | this.channelId = channelId; 83 | this.readAPIKey = readKey; 84 | this.writeAPIKey = writeKey; 85 | this.isPublic = false; 86 | } 87 | 88 | /** 89 | * Make GET requests to the Thingspeak API without additional feed 90 | * parameters. 91 | * 92 | * @param url The API url. 93 | * @return JSON string. 94 | * @throws UnirestException The request cannot be made. 95 | * @throws ThingSpeakException The request is invalid. 96 | */ 97 | private String thingRequest(String url) throws UnirestException, ThingSpeakException { 98 | GetRequest request = Unirest.get(url); 99 | if (!this.isPublic) { 100 | request.field("key", this.readAPIKey); 101 | } 102 | HttpResponse response = request.asJson(); 103 | if (response.getCode() != 200) { 104 | throw new ThingSpeakException("Request failed with code " + response.getCode()); 105 | } 106 | return response.getBody().toString(); 107 | } 108 | 109 | /** 110 | * Make GET requests to the Thingspeak API with additional feed parameters. 111 | * 112 | * @param url The API url. 113 | * @param options Optional feed parameters. 114 | * @return JSON string. 115 | * @throws UnirestException The request cannot be made. 116 | * @throws ThingSpeakException The request is invalid. 117 | */ 118 | private String thingRequest(String url, FeedParameters options) throws UnirestException, ThingSpeakException { 119 | GetRequest request = Unirest.get(url); 120 | if (!this.isPublic) { 121 | request.field("key", this.readAPIKey); 122 | } 123 | request.fields(options.fields); 124 | HttpResponse response = request.asJson(); 125 | if (response.getCode() != 200) { 126 | throw new ThingSpeakException("Request failed with code " + response.getCode()); 127 | } 128 | return response.getBody().toString(); 129 | } 130 | 131 | /** 132 | * Use a server other than thingspeak.com. If you are hosting your own 133 | * Thingspeak server, set the url of the server here. The url of the public 134 | * Thingspeak server is http://api.thingspeak.com 135 | * 136 | * @param url eg. http://localhost, http://thingspeak.local:3000, etc. 137 | */ 138 | public void setUrl(String url) { 139 | this.APIURL = url; 140 | } 141 | 142 | /** 143 | * Update channel with new data. 144 | * 145 | * @param entry The new data to be posted. 146 | * @return The id of the new entry. 147 | * @throws UnirestException The request cannot be made. 148 | * @throws ThingSpeakException The request is invalid. 149 | */ 150 | public Integer update(Entry entry) throws UnirestException, ThingSpeakException { 151 | HttpResponse response = Unirest.post(APIURL + "/update") 152 | .header(APIHEADER, this.writeAPIKey) 153 | .header("Connection", "close") 154 | .fields(entry.getUpdateMap()) 155 | .asString(); 156 | if (response.getCode() != 200) { 157 | throw new ThingSpeakException("Request failed with code " + response.getCode()); 158 | } else if (response.getBody().equals("0")) { 159 | throw new ThingSpeakException("Update failed."); 160 | } 161 | return Integer.parseInt(response.getBody()); 162 | } 163 | 164 | /** 165 | * Get a channel feed with default feed options. Does not include location or status info. Only fields that 166 | * have been named in the channel's settings (via the web) will be returned. 167 | * 168 | * @return Feed for this channel. 169 | * @throws UnirestException The request cannot be made. 170 | * @throws ThingSpeakException The request is invalid. 171 | */ 172 | public Feed getChannelFeed() throws UnirestException, ThingSpeakException { 173 | String url = APIURL + "/channels/" + this.channelId + "/feed.json"; 174 | return gson.fromJson(thingRequest(url), Feed.class); 175 | } 176 | 177 | /** 178 | * Get a channel feed with additional feed options. Only fields that have been named in 179 | * the channel's settings (via the web) will be returned. 180 | * 181 | * @param options Additional feed parameters. 182 | * @return Feed for this channel. 183 | * @throws UnirestException The request cannot be made. 184 | * @throws ThingSpeakException The request is invalid. 185 | */ 186 | public Feed getChannelFeed(FeedParameters options) throws UnirestException, ThingSpeakException { 187 | String url = APIURL + "/channels/" + this.channelId + "/feed.json"; 188 | return gson.fromJson(thingRequest(url, options), Feed.class); 189 | } 190 | 191 | /** 192 | * Get last entry in this channel with default feed options. This is a 193 | * faster alternative to getting a Channel Feed and then calling 194 | * {@link Feed#getChannelLastEntry()}. 195 | * 196 | * @return Entry. 197 | * @throws UnirestException The request cannot be made. 198 | * @throws ThingSpeakException The request is invalid. 199 | */ 200 | public Entry getLastChannelEntry() throws UnirestException, ThingSpeakException { 201 | String url = APIURL + "/channels/" + this.channelId + "/feed/last.json"; 202 | return gson.fromJson(thingRequest(url), Entry.class); 203 | } 204 | 205 | /** 206 | * Get last entry in this channel with additional feed options. This is a 207 | * faster alternative to getting a Channel Feed and then calling 208 | * {@link Feed#getChannelLastEntry()} 209 | * 210 | * @param options Supported options: offset, status, and location. 211 | * @return Entry. 212 | * @throws UnirestException The request cannot be made. 213 | * @throws ThingSpeakException The request is invalid. 214 | */ 215 | public Entry getLastChannelEntry(FeedParameters options) throws UnirestException, ThingSpeakException { 216 | String url = APIURL + "/channels/" + this.channelId + "/feed/last.json"; 217 | return gson.fromJson(thingRequest(url, options), Entry.class); 218 | } 219 | 220 | /** 221 | * Get a field feed with default feed options. 222 | * 223 | * @param fieldId The field to include in the field (1-8). 224 | * @return Feed. 225 | * @throws UnirestException The request cannot be made. 226 | * @throws ThingSpeakException The request is invalid. 227 | */ 228 | public Feed getFieldFeed(Integer fieldId) throws UnirestException, ThingSpeakException { 229 | String url = APIURL + "/channels/" + this.channelId + "/field/" + fieldId + ".json"; 230 | return gson.fromJson(thingRequest(url), Feed.class); 231 | } 232 | 233 | /** 234 | * Get a field feed with additional feed options. 235 | * 236 | * @param fieldId The field to include in the field (1-8). 237 | * @param options Optional parameters that control the format of the feed. 238 | * @return Feed. 239 | * @throws UnirestException The request cannot be made. 240 | * @throws ThingSpeakException The request is invalid. 241 | */ 242 | public Feed getFieldFeed(Integer fieldId, FeedParameters options) throws UnirestException, ThingSpeakException { 243 | String url = APIURL + "/channels/" + this.channelId + "/field/" + fieldId + ".json"; 244 | return gson.fromJson(thingRequest(url, options), Feed.class); 245 | } 246 | 247 | /** 248 | * Get the last entry in a field feed with default feed options. 249 | * 250 | * @param fieldId The field to return (0-8). 251 | * @return Last entry for the specified field. 252 | * @throws UnirestException The request cannot be made. 253 | * @throws ThingSpeakException The request is invalid. 254 | */ 255 | public Entry getLastFieldEntry(Integer fieldId) throws UnirestException, ThingSpeakException { 256 | String url = APIURL + "/channels/" + this.channelId + "/field/" + fieldId + "/last.json"; 257 | return gson.fromJson(thingRequest(url), Entry.class); 258 | } 259 | 260 | /** 261 | * Get the last entry in a field feed with additional feed options. 262 | * 263 | * @param fieldId The field to return (0-8). 264 | * @param options Supported options: offset, status, and location. 265 | * @return Last entry for the specified field. 266 | * @throws UnirestException The request cannot be made. 267 | * @throws ThingSpeakException The request is invalid. 268 | */ 269 | public Entry getLastFieldEntry(Integer fieldId, FeedParameters options) throws UnirestException, ThingSpeakException { 270 | String url = APIURL + "/channels/" + this.channelId + "/field/" + fieldId + "/last.json"; 271 | return gson.fromJson(thingRequest(url, options), Entry.class); 272 | } 273 | 274 | /** 275 | * Get channel status updates. Uses the default feed options. 276 | * 277 | * @return Status feed. 278 | * @throws UnirestException The request cannot be made. 279 | * @throws ThingSpeakException The request is invalid. 280 | */ 281 | public Feed getStatusFeed() throws UnirestException, ThingSpeakException { 282 | String url = APIURL + "/channels/" + this.channelId + "/status.json"; 283 | return gson.fromJson(thingRequest(url), Feed.class); 284 | } 285 | 286 | /** 287 | * Get channel status updates. 288 | * 289 | * @param options Only {@link FeedParameters#offset(java.lang.Integer)} is 290 | * supported. 291 | * @return Status feed. 292 | * @throws UnirestException The request cannot be made. 293 | * @throws ThingSpeakException The request is invalid. 294 | */ 295 | public Feed getStatusFeed(FeedParameters options) throws UnirestException, ThingSpeakException { 296 | String url = APIURL + "/channels/" + this.channelId + "/status.json"; 297 | return gson.fromJson(thingRequest(url, options), Feed.class); 298 | } 299 | 300 | /** 301 | * Not implemented. 302 | */ 303 | public void getUserInfo() { 304 | throw new UnsupportedOperationException("Not implemented."); 305 | } 306 | 307 | /** 308 | * Not implemented. 309 | */ 310 | public void getUserChannels() { 311 | throw new UnsupportedOperationException("Not implemented."); 312 | } 313 | 314 | /** 315 | * Checks if a channel is available/reachable. Use this method if you want to avoid handling exceptions. 316 | * 317 | * @return channel availability 318 | */ 319 | public boolean isAvailable() { 320 | String url = APIURL + "/channels/" + this.channelId + "/feed.json" + "?key=" + this.readAPIKey + "&results=0"; 321 | try { 322 | thingRequest(url); 323 | } catch (UnirestException | ThingSpeakException e) { 324 | return false; 325 | } 326 | return true; 327 | } 328 | } 329 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/Entry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak; 21 | 22 | import java.util.Date; 23 | import java.util.HashMap; 24 | 25 | /** 26 | * Create a new Entry to update a channel, or retrieve individual elements from 27 | * a {@link Feed}. 28 | * 29 | */ 30 | public class Entry { 31 | 32 | /** 33 | * The names of these private members must match the JSON fields in a 34 | * channel feed returned by ThingSpeak. If they don't, GSON might not be 35 | * able to deserialize the JSON feed into Entry objects. Note that 36 | * 'longitude' and 'latitude' are returned by feeds, but 'lat' and 'long' 37 | * are used when updating. 38 | */ 39 | private Date created_at; 40 | private Integer entry_id; 41 | private String field1; 42 | private String field2; 43 | private String field3; 44 | private String field4; 45 | private String field5; 46 | private String field6; 47 | private String field7; 48 | private String field8; 49 | private Double latitude; 50 | private Double longitude; 51 | private Double elevation; 52 | private String status; 53 | private String twitter; 54 | private String tweet; 55 | 56 | private final HashMap updateMap = new HashMap<>(); 57 | 58 | /** 59 | * Get a map of all fields in a format compatible with the API's update 60 | * parameters. Used internally by {@link Channel#update(com.angryelectron.thingspeak.Entry)}. 61 | * @return Field map. 62 | */ 63 | HashMap getUpdateMap() { 64 | return updateMap; 65 | } 66 | 67 | /** 68 | * Get data for a field. Fields must be enabled via the web in the Channel's 69 | * settings. 70 | * @param field 1-8 71 | * @return Field data; null for status feeds, undefined fields, and field 72 | * feeds where field was not specified. 73 | */ 74 | public Object getField(Integer field) { 75 | switch(field) { 76 | case 1: 77 | return field1; 78 | case 2: 79 | return field2; 80 | case 3: 81 | return field3; 82 | case 4: 83 | return field4; 84 | case 5: 85 | return field5; 86 | case 6: 87 | return field6; 88 | case 7: 89 | return field7; 90 | case 8: 91 | return field8; 92 | } 93 | throw new IllegalArgumentException("Invalid field."); 94 | } 95 | 96 | /** 97 | * Set the value for a field. Fields must be enabled via the web in the Channel's 98 | * settings. 99 | * @param field 1-8. 100 | * @param value Value for field. 101 | */ 102 | public void setField(Integer field, String value) { 103 | switch(field) { 104 | case 1: 105 | field1 = value; 106 | updateMap.put("field1", value); 107 | return; 108 | case 2: 109 | field2 = value; 110 | updateMap.put("field2", value); 111 | return; 112 | case 3: 113 | field3 = value; 114 | updateMap.put("field3", value); 115 | return; 116 | case 4: 117 | field4 = value; 118 | updateMap.put("field4", value); 119 | return; 120 | case 5: 121 | field5 = value; 122 | updateMap.put("field5", value); 123 | return; 124 | case 6: 125 | field6 = value; 126 | updateMap.put("field6", value); 127 | return; 128 | case 7: 129 | field7 = value; 130 | updateMap.put("field7", value); 131 | return; 132 | case 8: 133 | field8 = value; 134 | updateMap.put("field8", value); 135 | return; 136 | } 137 | throw new IllegalArgumentException("Invalid field."); 138 | } 139 | 140 | /** 141 | * Get latitude. 142 | * @return Latitude, in decimal degrees; 0.0 if undefined; null for status feeds or if 143 | * location info was not requested using {@link FeedParameters#location(java.lang.Boolean) }. 144 | * 145 | */ 146 | public Double getLatitude() { 147 | return latitude; 148 | } 149 | 150 | /** 151 | * Set latitude. 152 | * @param latitude Latitude, in decimal degrees. 153 | */ 154 | public void setLatitude(Double latitude) { 155 | this.latitude = latitude; 156 | updateMap.put("lat", latitude); 157 | } 158 | 159 | /** 160 | * Get longitude. 161 | * @return Longitude, in decimal degrees; 0.0 if undefined; null for status feeds or if 162 | * location info was not requested using {@link FeedParameters#location(java.lang.Boolean) }. 163 | */ 164 | public Double getLongitude() { 165 | return longitude; 166 | } 167 | 168 | /** 169 | * Set longitude. 170 | * @param longitude Longitude, in decimal degrees. 171 | */ 172 | public void setLong(Double longitude) { 173 | this.longitude = longitude; 174 | updateMap.put("long", longitude); 175 | } 176 | 177 | /** 178 | * Get elevation. 179 | * @return Elevation, in meters; 0.0 if undefined; null for status feeds or if 180 | * location info was not requested using {@link FeedParameters#location}. 181 | */ 182 | public Double getElevation() { 183 | return elevation; 184 | } 185 | 186 | /** 187 | * Set elevation. 188 | * @param elevation Elevation, in meters. 189 | */ 190 | public void setElevation(Double elevation) { 191 | this.elevation = elevation; 192 | updateMap.put("elevation", elevation); 193 | } 194 | 195 | /** 196 | * Get status. 197 | * @return Status string; null for Channel and Field feeds if status info 198 | * was not requested using {@link FeedParameters#status(java.lang.Boolean)} 199 | */ 200 | public String getStatus() { 201 | return status; 202 | } 203 | 204 | /** 205 | * Set status. 206 | * @param status Status string. 207 | */ 208 | public void setStatus(String status) { 209 | this.status = status; 210 | updateMap.put("status", status); 211 | } 212 | 213 | /** 214 | * Set Twitter username. If set, a tweet will be posted to the user's 215 | * twitter feed for each channel update. 216 | * @param twitter Twitter username. 217 | */ 218 | public void setTwitter(String twitter) { 219 | this.twitter = twitter; 220 | updateMap.put("twitter", twitter); 221 | } 222 | 223 | /** 224 | * Set Twitter message. This message will be posted to the user's twitter 225 | * feed for each channel update. 226 | * @param tweet Twitter message. 227 | */ 228 | public void setTweet(String tweet) { 229 | this.tweet = tweet; 230 | updateMap.put("tweet", tweet); 231 | } 232 | 233 | /** 234 | * Set the created date of an entry. If not explicitly set, the channel update time is used. 235 | * Useful when entries are not created and updated at the same time (offline mode, queuing to avoid rate-limiting, etc.) 236 | * @param created date which will be send to thingspeak 237 | */ 238 | public void setCreated(Date created) { 239 | this.created_at = created; 240 | updateMap.put("created_at", created); 241 | } 242 | 243 | /** 244 | * Get date on which this channel entry was created. Use 245 | * {@link FeedParameters#offset(java.lang.Integer)} to adjust timezones. 246 | * @return Date. 247 | */ 248 | public Date getCreated() { 249 | return created_at; 250 | } 251 | 252 | /** 253 | * Get the ID of this entry. 254 | * @return Entry ID. 255 | */ 256 | public Integer getEntryId() { 257 | return entry_id; 258 | } 259 | } 260 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/Feed.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client Copyright 2014, Andrew Bythell 3 | * http://angryelectron.com 4 | * 5 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or (at your 8 | * option) any later version. 9 | * 10 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 13 | * Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along with 16 | * theThingSpeak Java Client. If not, see . 17 | */ 18 | package com.angryelectron.thingspeak; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Date; 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | 25 | /** 26 | *

ThingSpeak status, channel, or field feed. Methods for accessing feed info 27 | * and individual {@link Entry}s. Only 28 | * the following methods are supported for status feeds, as they contain limited 29 | * channel information (other methods will return null):

30 | *
    31 | *
  • {@link #getChannelName() }
  • 32 | *
  • {@link #getEntry(java.lang.Integer) }
  • 33 | *
  • {@link #getEntryList() }
  • 34 | *
  • {@link #getEntryMap() }
  • 35 | *
36 | *

To obtain a Feed, call one of the {@link Channel} methods. For example, 37 | * to get feeds for channel 1234: 38 | *

 39 |  * {@code 
 40 |  * Channel channel = new Channel(1234);
 41 |  * Feed statusFeed = channel.getStatusFeed();
 42 |  * Feed channelFeed = channel.getChannelFeed();
 43 |  * Feed fieldFeed = channel.getFieldFeed(1);
 44 |  * }
 45 |  * 
46 | */ 47 | public class Feed { 48 | 49 | /** 50 | * Although there are more sensible ways to build this class, the structure 51 | * must match the JSON returned by the ThingSpeak API to allow GSON to 52 | * de-serialize it. 53 | */ 54 | private class ChannelInfo { 55 | 56 | private Date created_at; 57 | private String description; 58 | private String field1; 59 | private String field2; 60 | private String field3; 61 | private String field4; 62 | private String field5; 63 | private String field6; 64 | private String field7; 65 | private String field8; 66 | private Integer id; 67 | private Integer last_entry_id; 68 | private String name; 69 | private Date updated_at; 70 | } 71 | private final ChannelInfo channel = new ChannelInfo(); 72 | private final ArrayList feeds = new ArrayList<>(); 73 | 74 | /** 75 | * Constructor is package/class private so it can only be created via GSON 76 | * and not created directly. 77 | */ 78 | private Feed() { 79 | 80 | } 81 | 82 | /** 83 | * Get channel creation date. Use {@link FeedParameters#offset(java.lang.Integer)} 84 | * to adjust to local time zone. 85 | * 86 | * @return Date on which this channel was created; null for status feeds. 87 | */ 88 | public Date getChannelCreationDate() { 89 | return channel.created_at; 90 | } 91 | 92 | /** 93 | * Get channel description. Channel description can be set via the 94 | * ThingSpeak server web interface. 95 | * 96 | * @return Description of this channel; null for status feeds. 97 | */ 98 | public String getChannelDescription() { 99 | return channel.description; 100 | } 101 | 102 | /** 103 | * Get the user-defined name of a field. Define fields and names via the ThingSpeak server 104 | * web interface. 105 | * 106 | * @param field 1-8. 107 | * @return The assigned name of the field; null for status feeds or if the 108 | * field is unassigned. 109 | */ 110 | public String getFieldName(Integer field) { 111 | switch (field) { 112 | case 1: 113 | return channel.field1; 114 | case 2: 115 | return channel.field2; 116 | case 3: 117 | return channel.field3; 118 | case 4: 119 | return channel.field4; 120 | case 5: 121 | return channel.field5; 122 | case 6: 123 | return channel.field6; 124 | case 7: 125 | return channel.field7; 126 | case 8: 127 | return channel.field8; 128 | } 129 | throw new IllegalArgumentException("Invalid field."); 130 | } 131 | 132 | /** 133 | * Get the ID of this channel. 134 | * 135 | * @return ID of this channel; null for status feeds. 136 | */ 137 | public Integer getChannelId() { 138 | return channel.id; 139 | } 140 | 141 | /** 142 | * Get the ID of the last entry made to this channel. 143 | * 144 | * @return The ID of the last entry made in this channel; null for status 145 | * feeds. 146 | */ 147 | public Integer getChannelLastEntryId() { 148 | return channel.last_entry_id; 149 | } 150 | 151 | /** 152 | * Get the name of this channel. Set a name for the channel using the 153 | * ThingSpeak server's web interface. 154 | * 155 | * @return The name of this channel; null if not set. 156 | */ 157 | public String getChannelName() { 158 | return channel.name; 159 | } 160 | 161 | /** 162 | * Get the date of the last channel update. Use {@link FeedParameters#offset(java.lang.Integer)} 163 | * to adjust to local timezone. 164 | * 165 | * @return The date of the last update of this channel; null for status feeds. 166 | */ 167 | public Date getChannelUpdateDate() { 168 | return channel.updated_at; 169 | } 170 | 171 | /** 172 | * Get a List of all {@link Entry}s in this feed. 173 | * 174 | * @return All Entries in this feed. 175 | */ 176 | public ArrayList getEntryList() { 177 | return this.feeds; 178 | } 179 | 180 | /** 181 | * Get a Map of all {@link Entry}s in this feed. 182 | * 183 | * @return All Entries in this feed, keyed by entry ID. 184 | */ 185 | public Map getEntryMap() { 186 | HashMap map = new HashMap<>(); 187 | for (Entry entry : this.feeds) { 188 | map.put(entry.getEntryId(), entry); 189 | } 190 | return map; 191 | } 192 | 193 | /** 194 | * Get an Entry in the feed by ID. If the feed is large, or you need to lookup 195 | * many different entries, this method could be quite slow. It may be better 196 | * to call {@link #getEntryMap()} to obtain a map of entries indexed by id. 197 | * 198 | * @param id Entry ID. 199 | * @return Entry. 200 | * @throws ThingSpeakException if the feed does not contain an Entry with 201 | * the given id. 202 | */ 203 | public Entry getEntry(Integer id) throws ThingSpeakException { 204 | for (Entry entry : this.feeds) { 205 | if (entry.getEntryId().equals(id)) { 206 | return entry; 207 | } 208 | } 209 | throw new ThingSpeakException("Entry with ID " + id + " not found in feed."); 210 | 211 | } 212 | 213 | /** 214 | * Get the last / latest entry in this feed. If you only need the last entry 215 | * and not the rest of the feed, consider using 216 | * {@link Channel#getLastChannelEntry()}. 217 | * 218 | * @return An Entry with id equal to the feed's last_entry_id. 219 | * @throws ThingSpeakException The channel does not have a last entry or the 220 | * feed is a Status feed. 221 | */ 222 | public Entry getChannelLastEntry() throws ThingSpeakException { 223 | return getEntry(channel.last_entry_id); 224 | } 225 | 226 | } 227 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/FeedParameters.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak; 21 | 22 | import java.text.SimpleDateFormat; 23 | import java.util.Date; 24 | import java.util.HashMap; 25 | 26 | /** 27 | *

28 | * Optional parameters which control the format of a {@link Feed}. 29 | * All ThingSpeak optional parameters are supported except 'key' (because it is set via 30 | * {@link Channel}) and 'format' (which must be JSON in order to parse the feed). 31 | *

32 | *

33 | * To retrieve a feed with optional parameters, setup a {@link FeedParameters} 34 | * object with the required parameters, then pass it to one of the 35 | * {@link Channel} methods to retrieve the desired feed. For example, to 36 | * include the latitude, longitude, elevation, and status fields in a feed for 37 | * channel 1234:

38 | *
 39 |  * {@code
 40 |  * Channel channel = new Channel(1234);
 41 |  * FeedParameters options = new FeedParameters();
 42 |  * options.location(true);
 43 |  * options.status(true);
 44 |  * options.results(100);
 45 |  * Feed feed = channel.getChannelFeed(options);
 46 |  * }
 47 |  * 
48 | */ 49 | public class FeedParameters { 50 | 51 | /** 52 | * Pre-defined time periods. ThingSpeak only accepts certain values. 53 | * For use with: 54 | *
    55 | *
  • {@link #timescale(com.angryelectron.thingspeak.FeedParameters.Period)}
  • 56 | *
  • {@link #sum(com.angryelectron.thingspeak.FeedParameters.Period)}
  • 57 | *
  • {@link #average(com.angryelectron.thingspeak.FeedParameters.Period)}
  • 58 | *
  • {@link #median(com.angryelectron.thingspeak.FeedParameters.Period)}
  • 59 | *
60 | */ 61 | public enum Period { 62 | /** 63 | * 10 minutes. 64 | */ 65 | T10m(10), 66 | 67 | /** 68 | * 15 minutes. 69 | */ 70 | T15m(15), 71 | 72 | /** 73 | * 20 minutes. 74 | */ 75 | T20m(20), 76 | 77 | /** 78 | * 30 minutes. 79 | */ 80 | T30m(30), 81 | 82 | /** 83 | * 1 hour / 60 minutes. 84 | */ 85 | T1h(60), 86 | 87 | /** 88 | * 4 hours / 240 minutes. 89 | */ 90 | T4h(240), 91 | 92 | /** 93 | * 12 hours / 720 minutes. 94 | */ 95 | T12h(720), 96 | 97 | /** 98 | * 24 hours / 1440 minutes. 99 | */ 100 | T24h(1440); 101 | 102 | private final Integer minutes; 103 | 104 | private Period(Integer minutes) { 105 | this.minutes = minutes; 106 | } 107 | 108 | Integer minutes() { 109 | return this.minutes; 110 | } 111 | 112 | } 113 | 114 | /** 115 | * A map to store the parameter names and values. 116 | */ 117 | HashMap fields = new HashMap<>(); 118 | 119 | /** 120 | * The date format required by ThingSpeak. 121 | */ 122 | private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 123 | 124 | /** 125 | * Select the number of results to be returned. Feeds that return more than 100 126 | * results are cached for 5 minutes, so set results < 100 for real time 127 | * applications. By default, all results up to a maximum of 8000 will be returned. 128 | * @param results 8000 max, or less than 100 to disable 5 minute data cache. 129 | */ 130 | public void results(Integer results) { 131 | if (results > 8000) { 132 | throw new IllegalArgumentException("Feed cannot return more than 8000 results."); 133 | } 134 | fields.put("results", results); 135 | } 136 | /** 137 | * Limit results to the past number of days. 138 | * @param days Number of days prior to now to include in feed. 139 | */ 140 | public void days(Integer days) { 141 | fields.put("days", days); 142 | } 143 | 144 | /** 145 | * Limit results to entries recorded on or after a start date. 146 | * @param date Start date. 147 | */ 148 | public void start(Date date) { 149 | fields.put("start", formatter.format(date)); 150 | } 151 | 152 | /** 153 | * Limit results to entries recorded on or before an end date. 154 | * @param date End date. 155 | */ 156 | public void end(Date date) { 157 | fields.put("end", formatter.format(date)); 158 | } 159 | 160 | /** 161 | * Timezone offset. Default is UTC. Applies to all dates returned in the 162 | * feed. 163 | * @param hours Offset (+/-) in hours. 164 | */ 165 | public void offset(Integer hours) { 166 | fields.put("offset", hours); 167 | } 168 | 169 | /** 170 | * Include the status field for each entry in the feed. By default, 171 | * the status field is not included. 172 | * @param include Feed includes the status field when True. 173 | */ 174 | public void status(Boolean include) { 175 | fields.put("status", include); 176 | } 177 | 178 | /** 179 | * Include location information for each entry in the feed. By default, 180 | * latitude, longitude, and elevation are not included. 181 | * @param include Feed includes location fields when True. 182 | */ 183 | public void location(Boolean include) { 184 | fields.put("location", include); 185 | } 186 | 187 | /** 188 | * Include only entries with fields greater or equal to a minimum value in the 189 | * feed. 190 | * @param value Minimum value. 191 | */ 192 | public void min(Double value) { 193 | fields.put("min", value); 194 | } 195 | 196 | /** 197 | * Include only entries with fields less than or equal to a maximum value. 198 | * @param value Maximum value. 199 | */ 200 | public void max(Double value) { 201 | fields.put("max", value); 202 | } 203 | 204 | /** 205 | * Round fields to a certain number of decimal places. 206 | * @param places Round to this many decimal places. 207 | */ 208 | public void round(Integer places) { 209 | fields.put("round", places); 210 | } 211 | 212 | /** 213 | * Include only the first value in the given period. 214 | * @param t Time period. 215 | */ 216 | public void timescale(Period t) { 217 | fields.put("timescale", t.minutes()); 218 | } 219 | 220 | /** 221 | * For each field, sum values in the given period. 222 | * @param t Time period. 223 | */ 224 | public void sum(Period t) { 225 | fields.put("sum", t.minutes()); 226 | } 227 | 228 | /** 229 | * For each field, average the values over the given period. 230 | * @param t Time period. 231 | */ 232 | public void average(Period t) { 233 | fields.put("average", t.minutes()); 234 | } 235 | 236 | /** 237 | * For each field, find the median value in the given period. 238 | * @param t Time period. 239 | */ 240 | public void median(Period t) { 241 | fields.put("median", t.minutes()); 242 | } 243 | 244 | } 245 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/ThingSpeakException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak; 21 | 22 | /** 23 | * Thrown when the ThingSpeak API rejects requests due to invalid API keys, arguments, 24 | * or data formats. 25 | */ 26 | public class ThingSpeakException extends Exception { 27 | public ThingSpeakException(String message) { 28 | super(message); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/log4j/ThingSpeakAppender.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Appender for log4j Copyright 2014, Andrew Bythell 3 | * 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * the ThingSpeak Appender. If not, see . 18 | */ 19 | package com.angryelectron.thingspeak.log4j; 20 | 21 | import com.angryelectron.thingspeak.Channel; 22 | import com.angryelectron.thingspeak.Entry; 23 | import com.angryelectron.thingspeak.ThingSpeakException; 24 | import com.mashape.unirest.http.exceptions.UnirestException; 25 | import java.io.IOException; 26 | import java.text.DateFormat; 27 | import java.text.SimpleDateFormat; 28 | import java.util.Date; 29 | import java.util.Properties; 30 | import java.util.logging.Level; 31 | import java.util.logging.Logger; 32 | import org.apache.log4j.AppenderSkeleton; 33 | import org.apache.log4j.spi.LoggingEvent; 34 | 35 | /** 36 | *

37 | * Appender for log4j that logs messages to ThingSpeak. To prepare a channel for 38 | * logging, create a new ThingSpeak channel with three fields (names not 39 | * important):

40 | *
    41 | *
  1. Date
  2. 42 | *
  3. Level
  4. 43 | *
  5. Message
  6. 44 | *
45 | * 46 | *

47 | * Then create and configure a new appender. Use 48 | * {@link #configureChannel(Integer, String, String) configureChannel} 49 | * to configure the appender, or set via log4j.properties:

50 | *
    51 | *
  • log4j.appender.ThingSpeak=com.angryelectron.thingspeak.log4j.ThingSpeakAppender
  • 52 | *
  • com.angryelectron.thingspeak.log4j.channelNumber = [channel number]
  • 53 | *
  • com.angryelectron.thingspeak.log4j.apiWriteKey = [channel api write key]
  • 54 | *
  • (optional) com.angryelectron.thingspeak.log4j.server = http://your.thingspeak.server
  • 55 | *
56 | * 57 | *

If the server is not specified, thingspeak.com will be used. Remember that 58 | * you must use an alternate server if your application logs faster than the 59 | * rate limits imposed by thingspeak.com.

60 | * 61 | *

Next, set your root logger to use the new appender:

62 | *
    63 | *
  • log4j.rootLogger=INFO, ThingSpeak
  • 64 | *
65 | * 66 | */ 67 | public class ThingSpeakAppender extends AppenderSkeleton { 68 | 69 | private final Properties properties = new Properties(); 70 | private final String channelPropertyKey = "com.angryelectron.thingspeak.log4j.channelNumber"; 71 | private final String apiPropertyKey = "com.angryelectron.thingspeak.log4j.apiWriteKey"; 72 | private final String serverPropertyKey = "com.angryelectron.thingspeak.log4j.server"; 73 | private Channel channel; 74 | 75 | /** 76 | * Constructor. 77 | */ 78 | public ThingSpeakAppender() { 79 | try { 80 | properties.load(getClass().getResourceAsStream("/log4j.properties")); 81 | String apiWriteKey = properties.getProperty(apiPropertyKey); 82 | Integer channelNumber = Integer.parseInt(properties.getProperty(channelPropertyKey)); 83 | String server = properties.getProperty(serverPropertyKey); 84 | channel = new Channel(channelNumber, apiWriteKey); 85 | if (!server.isEmpty()) { 86 | channel.setUrl(server); 87 | } 88 | } catch (IOException | NumberFormatException | NullPointerException ex) { 89 | /* ignore - will be caught and logged in append() */ 90 | } 91 | } 92 | 93 | /** 94 | * Configure the channel. Use to configure the appender in code (vs. 95 | * log4j.properties). 96 | * 97 | * @param channelNumber ThingSpeak channel number. 98 | * @param apiWriteKey ThinSpeak API write key for the channel. 99 | * @param url URL of thingspeak server. If null, the public server 100 | * (thingpspeak.com) will be used. 101 | */ 102 | public void configureChannel(Integer channelNumber, String apiWriteKey, String url) { 103 | channel = new Channel(channelNumber, apiWriteKey); 104 | if (url != null) { 105 | channel.setUrl(url); 106 | } 107 | } 108 | 109 | /** 110 | * Internal. Append log messages as an entry in a ThingSpeak channel. 111 | * 112 | * @param event log4j event. 113 | */ 114 | @Override 115 | protected void append(LoggingEvent event) { 116 | Date timeStamp = new Date(event.timeStamp); 117 | DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 118 | Entry entry = new Entry(); 119 | entry.setField(1, dateFormat.format(timeStamp)); 120 | entry.setField(2, event.getLevel().toString()); 121 | entry.setField(3, event.getMessage().toString()); 122 | try { 123 | channel.update(entry); 124 | } catch (UnirestException | ThingSpeakException ex) { 125 | Logger.getLogger(ThingSpeakAppender.class.getName()).log(Level.SEVERE, null, ex); 126 | } 127 | } 128 | 129 | /** 130 | * Internal. 131 | */ 132 | @Override 133 | public void close() { 134 | /* nothing to do */ 135 | } 136 | 137 | /** 138 | * Internal. Thingspeak maps log data directly to channel "fields", so no 139 | * layout is required. 140 | * 141 | * @return false 142 | */ 143 | @Override 144 | public boolean requiresLayout() { 145 | return false; 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/log4j/package.html: -------------------------------------------------------------------------------- 1 | 2 | ThingSpeak appender for log4j. Send log messages to ThingSpeak channels. 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/package.html: -------------------------------------------------------------------------------- 1 | 2 | Read and write ThingSpeak channels. 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/pub/PublicChannel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak.pub; 21 | 22 | import java.util.ArrayList; 23 | import java.util.Date; 24 | import java.util.List; 25 | 26 | /** 27 | * A ThingSpeak public channel listing. 28 | * @author abythell 29 | */ 30 | public class PublicChannel { 31 | 32 | /** 33 | * This class must match the JSON returned by ThingSpeak. 34 | */ 35 | private class Tag { 36 | private Integer id; 37 | private String name; 38 | } 39 | 40 | /** 41 | * These fields must match the JSON returned by ThingSpeak. They 42 | * are populated by de-serializing a JSON stream using GSON in the 43 | * PublicIterator class. 44 | */ 45 | private Date created_at; 46 | private String description; 47 | private Double elevation; 48 | private Integer id; 49 | private Integer last_entry_id; 50 | private Double latitude; 51 | private Double longitude; 52 | private String name; 53 | private Integer ranking; 54 | private String username; 55 | private ArrayList tags = new ArrayList<>(); 56 | 57 | /** 58 | * Get channel name. 59 | * @return Name. 60 | */ 61 | public String getName() { 62 | return name; 63 | } 64 | 65 | /** 66 | * Get channel creation date. 67 | * @return Date. 68 | */ 69 | public Date getCreatedAt() { 70 | return created_at; 71 | } 72 | 73 | /** 74 | * Get channel description. 75 | * @return Description. 76 | */ 77 | public String getDescription() { 78 | return description; 79 | } 80 | 81 | /** 82 | * Get channel elevation. 83 | * @return Elevation, or 0.0 if no elevation was set for the channel. 84 | */ 85 | public Double getElevation() { 86 | return elevation; 87 | } 88 | 89 | /** 90 | * Get channel id. The id is required to access the channel's feed. 91 | * @return Channel id. 92 | */ 93 | public Integer getId() { 94 | return id; 95 | } 96 | 97 | /** 98 | * Get the id of the last entry posted to this channel. 99 | * @return Entry id. 100 | */ 101 | public Integer getLastEntryId() { 102 | return last_entry_id; 103 | } 104 | 105 | /** 106 | * Get the latitude of this channel. 107 | * @return Latitude in decimal degrees, or 0.0 if no latitude was set for 108 | * this channel. 109 | */ 110 | public Double getLatitude() { 111 | return latitude; 112 | } 113 | 114 | /** 115 | * Get the longitude of this channel. 116 | * @return Longitude in decimal degrees, or 0.0 if no longitude was set for 117 | * this channel. 118 | */ 119 | public Double getLongitude() { 120 | return longitude; 121 | } 122 | 123 | /** 124 | * Get the channel's ranking. It is unclear to the author how rankings 125 | * are compiled. 126 | * @return Rank. 127 | */ 128 | public Integer getRanking() { 129 | return ranking; 130 | } 131 | 132 | /** 133 | * Get the name of the user who owns this channel. 134 | * @return Username. 135 | */ 136 | public String getUsername() { 137 | return username; 138 | } 139 | 140 | /** 141 | * Get the channel's tags. 142 | * @return A List of tags, which can be passed to 143 | * {@link PublicChannelCollection#PublicChannelCollection(java.lang.String)} 144 | * to obtain a list of other public channels containing the same tag. 145 | */ 146 | public List getTags() { 147 | List list = new ArrayList<>(); 148 | for (Tag t : tags) { 149 | list.add(t.name); 150 | } 151 | return list; 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/pub/PublicChannelCollection.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak.pub; 21 | 22 | import java.util.AbstractCollection; 23 | import java.util.Iterator; 24 | 25 | /** 26 | * A collection of public ThingSpeak channels. The ThingSpeak Public API returns 27 | * paginated results, presumably because it could return a large set of 28 | * channels. This collection transparently fetches additional pages on-demand, 29 | * instead of trying to load all results into memory. Access the 30 | * {@link PublicChannel} objects in this collection by iterating: 31 | *
 32 |  * {@code
 33 |  * PublicChannelCollection pl = new PublicChannelCollection("temperature");
 34 |  * Iterator publicIterator = pl.iterator();
 35 |  * while (publicIterator.hasNext()) {
 36 |  *    PublicChannel p = publicIterator.next();
 37 |  * }
 38 |  * }
 39 |  * 
or a for loop: 40 | *
 41 |  * {@code
 42 |  * PublicChannelCollection publicChannels = new PublicChannelCollection("cheerlights");
 43 |  * for (PublicChannel channel : publicChannels) {
 44 |  *     //do something with channel
 45 |  * }
 46 |  * }
 47 |  * 
48 | * 49 | * @author abythell 50 | */ 51 | public class PublicChannelCollection extends AbstractCollection { 52 | 53 | private final String url = "http://api.thingspeak.com"; 54 | private final String tag; 55 | private Integer size; 56 | 57 | /** 58 | * Create a collection containing all public channels. 59 | */ 60 | public PublicChannelCollection() { 61 | tag = null; 62 | } 63 | 64 | /** 65 | * Create a collection containing all public channels with the given tag. 66 | * @param tag Tag. 67 | */ 68 | public PublicChannelCollection(String tag) { 69 | this.tag = tag; 70 | } 71 | 72 | /** 73 | * Use a server other than thingspeak.com. If you are hosting your own 74 | * Thingspeak server, set the url of the server here. 75 | * @param url eg. http://localhost, http://thingspeak.local:3000, etc. 76 | */ 77 | public void setUrl(String url) { 78 | throw new UnsupportedOperationException("Public API is not implemented in open-source servers."); 79 | } 80 | 81 | /** 82 | * Get a PublicChannel iterator, for iterating through the collection. 83 | * @return Iterator. 84 | */ 85 | @Override 86 | public Iterator iterator() { 87 | PublicIterator iterator = new PublicIterator(url, tag); 88 | size = iterator.size(); 89 | return iterator; 90 | } 91 | 92 | /** 93 | * Get the number of public channels in this collection. 94 | * @return Number of channels. 95 | */ 96 | @Override 97 | public int size() { 98 | return size; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/pub/PublicIterator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client Copyright 2014, Andrew Bythell 3 | * http://angryelectron.com 4 | * 5 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or (at your 8 | * option) any later version. 9 | * 10 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 13 | * Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along with 16 | * theThingSpeak Java Client. If not, see . 17 | */ 18 | package com.angryelectron.thingspeak.pub; 19 | 20 | import com.angryelectron.thingspeak.ThingSpeakException; 21 | import com.google.gson.Gson; 22 | import com.google.gson.GsonBuilder; 23 | import com.google.gson.JsonDeserializationContext; 24 | import com.google.gson.JsonDeserializer; 25 | import com.google.gson.JsonElement; 26 | import com.google.gson.JsonParseException; 27 | import com.mashape.unirest.http.HttpResponse; 28 | import com.mashape.unirest.http.JsonNode; 29 | import com.mashape.unirest.http.Unirest; 30 | import com.mashape.unirest.http.exceptions.UnirestException; 31 | import com.mashape.unirest.request.GetRequest; 32 | import java.lang.reflect.Type; 33 | import java.util.Iterator; 34 | import java.util.logging.Level; 35 | import java.util.logging.Logger; 36 | 37 | /** 38 | * Provides a custom iterator for PublicChannelCollections which works with 39 | * ThingSpeak's paginated results. 40 | * 41 | * @author abythell 42 | */ 43 | class PublicIterator implements Iterator { 44 | 45 | /** 46 | * URL of the Thingspeak server. 47 | */ 48 | private final String url; 49 | 50 | /** 51 | * Optional tag to search for. If null, all channels are returned. 52 | */ 53 | private final String tag; 54 | 55 | /** 56 | * Current page of results and an iterator to the list of PublicChannel 57 | * channels it contains. 58 | */ 59 | private PublicJSONResult results; 60 | private Iterator iterator; 61 | 62 | /** 63 | * ThingSpeak channels don't always contain elevation, latitude, or 64 | * longitude data. These empty strings cause NumberFormatExceptions when 65 | * using the default GSON de-serializer for Double. This replacement 66 | * de-serializes empty strings to 0.0. 67 | */ 68 | private static class LocationDeserializer implements JsonDeserializer { 69 | 70 | @Override 71 | public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 72 | try { 73 | return Double.parseDouble(json.getAsString()); 74 | } catch (NumberFormatException ex) { 75 | return 0.0; 76 | } 77 | } 78 | } 79 | 80 | /** 81 | * Build a GSON de-serializer for the ThingSpeak PublicChannel Channel JSON. 82 | */ 83 | private final Gson gson = new GsonBuilder() 84 | .registerTypeAdapter(Double.class, new LocationDeserializer()) 85 | .setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").create(); 86 | 87 | /** 88 | * Constructor. 89 | * 90 | * @param url ThingSpeak server URL (eg. http://api.thingspeak.com). 91 | * @param tag Get channels with this tag only, or null to return all 92 | * channels. 93 | */ 94 | protected PublicIterator(String url, String tag) { 95 | this.url = url; 96 | this.tag = tag; 97 | thingRequest(1); 98 | } 99 | 100 | /** 101 | * Make requests to the ThingSpeak server and parse the results. 102 | * 103 | * @param page The page of results to request. 104 | */ 105 | private void thingRequest(Integer page) { 106 | try { 107 | GetRequest request = Unirest.get(url + "/channels/public.json"); 108 | if (tag != null) { 109 | request.field("tag", tag); 110 | } 111 | request.field("page", page); 112 | HttpResponse response = request.asJson(); 113 | if (response.getCode() != 200) { 114 | throw new ThingSpeakException("Request failed with code " + response.getCode()); 115 | } 116 | results = gson.fromJson(response.getBody().toString(), PublicJSONResult.class); 117 | iterator = results.iterator(); 118 | } catch (UnirestException | ThingSpeakException ex) { 119 | Logger.getLogger(PublicIterator.class.getName()).log(Level.SEVERE, null, ex); 120 | results = null; 121 | } 122 | } 123 | 124 | @Override 125 | public boolean hasNext() { 126 | if (iterator.hasNext()) { 127 | /* current page still has unreturned channels */ 128 | return true; 129 | } else if (results.isLastPage()) { 130 | /* current page has returned all channels and there 131 | * are no more pages left */ 132 | return false; 133 | } else { 134 | /* current page has returned all channels but there 135 | * are more pages remaining. */ 136 | thingRequest(results.getCurrentPage() + 1); 137 | return true; 138 | } 139 | } 140 | 141 | @Override 142 | public PublicChannel next() { 143 | return iterator.next(); 144 | } 145 | 146 | @Override 147 | public void remove() { 148 | iterator.remove(); 149 | } 150 | 151 | Integer size() { 152 | return results.getTotalEntries(); 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/pub/PublicJSONResult.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak.pub; 21 | 22 | import java.util.ArrayList; 23 | import java.util.Iterator; 24 | 25 | /** 26 | * POJO to handle de-serialized JSON Public Channel data. Provides methods to 27 | * PublicIterator for accessing paging info. 28 | * @author abythell 29 | */ 30 | class PublicJSONResult { 31 | 32 | /** 33 | * This class must match the JSON returned by Thingspeak. 34 | */ 35 | private class Pagination { 36 | private Integer current_page; 37 | private Integer per_page; 38 | private Integer total_entries; 39 | } 40 | 41 | /** 42 | * These members must match the JSON returned by Thingspeak. 43 | */ 44 | private final Pagination pagination = new Pagination(); 45 | private final ArrayList channels = new ArrayList<>(); 46 | 47 | /** 48 | * Get the current page represented by the data in channels. 49 | * @return Page number. 50 | */ 51 | Integer getCurrentPage() { 52 | return pagination.current_page; 53 | } 54 | 55 | /** 56 | * Determine if the current page is the last one in the set. 57 | * @return True if this is the last page. 58 | */ 59 | Boolean isLastPage() { 60 | if (pagination.total_entries <= pagination.per_page) { 61 | return true; 62 | } else { 63 | Double pages = (double) pagination.total_entries / pagination.per_page; 64 | return (pages.intValue() == pagination.current_page); 65 | } 66 | } 67 | 68 | /** 69 | * Get the iterator for the channel data. Used by PublicIterator to access 70 | * the PublicChannel objects stored in the current page. 71 | * @return 72 | */ 73 | Iterator iterator() { 74 | return channels.iterator(); 75 | } 76 | 77 | /** 78 | * Get a total count of all public channels. 79 | * @return Number of public channels in this result. 80 | */ 81 | Integer getTotalEntries() { 82 | return pagination.total_entries; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/com/angryelectron/thingspeak/pub/package.html: -------------------------------------------------------------------------------- 1 | 2 | Access Thingspeak public channel listings. 3 | 4 | 5 | -------------------------------------------------------------------------------- /test/com/angryelectron/thingspeak/FeedTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak; 21 | 22 | import java.text.SimpleDateFormat; 23 | import java.util.ArrayList; 24 | import java.util.Date; 25 | import org.apache.log4j.BasicConfigurator; 26 | import org.apache.log4j.Level; 27 | import org.apache.log4j.Logger; 28 | import org.junit.AfterClass; 29 | import static org.junit.Assert.assertEquals; 30 | import static org.junit.Assert.assertNotNull; 31 | import static org.junit.Assert.assertNull; 32 | import org.junit.BeforeClass; 33 | import org.junit.Test; 34 | 35 | public class FeedTest { 36 | 37 | /** 38 | * Update the channel with some sample data that will be read-back by all of 39 | * the Feed Tests. 40 | * @throws java.lang.Exception 41 | */ 42 | @BeforeClass 43 | public static void setUpClass() throws Exception { 44 | BasicConfigurator.resetConfiguration(); 45 | BasicConfigurator.configure(); 46 | Logger.getLogger("org.apache.http").setLevel(Level.OFF); 47 | Entry entry = new Entry(); 48 | entry.setElevation(0.0); 49 | entry.setField(1, "nonsense-data1"); 50 | entry.setField(2, "nonsense-data2"); 51 | entry.setField(3, "nonsense-data3"); 52 | entry.setField(4, "nonsense-data4"); 53 | entry.setField(5, "nonsense-data5"); 54 | entry.setField(6, "nonsense-data6"); 55 | entry.setField(7, "nonsense-data7"); 56 | entry.setField(8, "nonsense-data8"); 57 | entry.setLatitude(0.0); 58 | entry.setLong(0.0); 59 | entry.setStatus("unimportant status"); 60 | entry.setTweet("irrelevant tweet"); 61 | entry.setTwitter("invalid twitter user"); 62 | 63 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID, TestChannelSettings.publicChannelWriteKey); 64 | Channel privateChannel = new Channel(TestChannelSettings.privateChannelID, TestChannelSettings.privateChannelWriteKey, TestChannelSettings.privateChannelReadKey); 65 | 66 | publicChannel.setUrl(TestChannelSettings.server); 67 | privateChannel.setUrl(TestChannelSettings.server); 68 | 69 | publicChannel.update(entry); 70 | privateChannel.update(entry); 71 | } 72 | 73 | /** 74 | * When testing is complete, pause before running other tests to prevent 75 | * hitting the API rate limit. 76 | * @throws Exception 77 | */ 78 | @AfterClass 79 | public static void tearDownClass() throws Exception { 80 | Thread.sleep(TestChannelSettings.rateLimit); 81 | } 82 | 83 | @Test 84 | public void testGetChannelFeedPublic() throws Exception { 85 | System.out.println("testGetChannelFeedPublic"); 86 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID, TestChannelSettings.publicChannelWriteKey); 87 | publicChannel.setUrl(TestChannelSettings.server); 88 | Feed publicFeed = publicChannel.getChannelFeed(); 89 | assertEquals(TestChannelSettings.publicChannelID, publicFeed.getChannelId()); 90 | assertNotNull(publicFeed.getChannelCreationDate()); 91 | assertNotNull(publicFeed.getChannelDescription()); 92 | assertNotNull(publicFeed.getChannelName()); 93 | assertNotNull(publicFeed.getChannelUpdateDate()); 94 | assertNotNull(publicFeed.getChannelLastEntryId()); 95 | assertNotNull(publicFeed.getEntryList()); 96 | assertNotNull(publicFeed.getEntryMap()); 97 | assertNotNull(publicFeed.getFieldName(1)); 98 | } 99 | 100 | @Test 101 | public void testGetChannelFeedPrivate() throws Exception { 102 | System.out.println("testGetChannelFeedPrivate"); 103 | Channel publicChannel = new Channel(TestChannelSettings.privateChannelID, TestChannelSettings.privateChannelWriteKey, TestChannelSettings.privateChannelReadKey); 104 | publicChannel.setUrl(TestChannelSettings.server); 105 | Feed publicFeed = publicChannel.getChannelFeed(); 106 | assertEquals(TestChannelSettings.privateChannelID, publicFeed.getChannelId()); 107 | assertNotNull(publicFeed.getChannelCreationDate()); 108 | assertNotNull(publicFeed.getChannelDescription()); 109 | assertNotNull(publicFeed.getChannelName()); 110 | assertNotNull(publicFeed.getChannelUpdateDate()); 111 | assertNotNull(publicFeed.getChannelLastEntryId()); 112 | assertNotNull(publicFeed.getEntry(publicFeed.getChannelLastEntryId())); 113 | assertNotNull(publicFeed.getEntryList()); 114 | assertNotNull(publicFeed.getEntryMap()); 115 | assertNotNull(publicFeed.getFieldName(1)); 116 | 117 | Entry entry = publicChannel.getLastChannelEntry(); 118 | 119 | /** 120 | * any one of these may fail if not defined in the Channel Settings 121 | * via the web. 122 | */ 123 | assertNotNull(entry.getField(1)); 124 | assertNotNull(entry.getField(2)); 125 | assertNotNull(entry.getField(3)); 126 | assertNotNull(entry.getField(4)); 127 | assertNotNull(entry.getField(5)); 128 | assertNotNull(entry.getField(6)); 129 | assertNotNull(entry.getField(7)); 130 | assertNotNull(entry.getField(8)); 131 | } 132 | 133 | @Test 134 | public void testGetChannelFeedWithOptions() throws Exception { 135 | System.out.println("testGetChannelFeedWithOptions"); 136 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID, TestChannelSettings.publicChannelWriteKey); 137 | publicChannel.setUrl(TestChannelSettings.server); 138 | FeedParameters options = new FeedParameters(); 139 | options.location(true); 140 | options.status(true); 141 | Feed publicFeed = publicChannel.getChannelFeed(options); 142 | Entry entry = publicFeed.getChannelLastEntry(); 143 | assertNotNull(entry.getStatus()); 144 | assertNotNull(entry.getElevation()); 145 | assertNotNull(entry.getLatitude()); 146 | assertNotNull(entry.getLongitude()); 147 | } 148 | 149 | @Test 150 | public void testGetLastEntry() throws Exception { 151 | System.out.println("testGetLastEntry"); 152 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID, TestChannelSettings.publicChannelWriteKey); 153 | publicChannel.setUrl(TestChannelSettings.server); 154 | Entry entry = publicChannel.getLastChannelEntry(); 155 | 156 | /** 157 | * any one of these may fail if not defined in the Channel Settings 158 | * via the web. 159 | */ 160 | assertNotNull(entry.getField(1)); 161 | assertNotNull(entry.getField(2)); 162 | assertNotNull(entry.getField(3)); 163 | assertNotNull(entry.getField(4)); 164 | assertNotNull(entry.getField(5)); 165 | assertNotNull(entry.getField(6)); 166 | assertNotNull(entry.getField(7)); 167 | assertNotNull(entry.getField(8)); 168 | } 169 | 170 | @Test 171 | public void testGetLastEntryWithOptions() throws Exception { 172 | System.out.println("testGetLastEntryWithOptions"); 173 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID, TestChannelSettings.publicChannelWriteKey); 174 | publicChannel.setUrl(TestChannelSettings.server); 175 | FeedParameters options = new FeedParameters(); 176 | options.location(true); 177 | options.status(true); 178 | Entry entry = publicChannel.getLastChannelEntry(options); 179 | assertNotNull(entry.getStatus()); 180 | assertNotNull(entry.getElevation()); 181 | assertNotNull(entry.getLatitude()); 182 | assertNotNull(entry.getLongitude()); 183 | } 184 | 185 | @Test 186 | public void testGetLastEntryWithTimezoneOffset() throws Exception { 187 | System.out.println("testGetLastEntryWithTimezoneOffset"); 188 | Channel channel = new Channel(TestChannelSettings.publicChannelID); 189 | channel.setUrl(TestChannelSettings.server); 190 | FeedParameters options = new FeedParameters(); 191 | options.offset(-8); 192 | Date pst = channel.getLastChannelEntry(options).getCreated(); 193 | SimpleDateFormat df = new SimpleDateFormat("Z"); 194 | assert(df.format(pst).equals("-0800")); 195 | } 196 | 197 | @Test 198 | public void testGetFieldFeed() throws Exception { 199 | System.out.println("testGetFieldFeed"); 200 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID); 201 | publicChannel.setUrl(TestChannelSettings.server); 202 | Feed feed = publicChannel.getFieldFeed(1); 203 | assertEquals(TestChannelSettings.publicChannelID, feed.getChannelId()); 204 | assertNotNull(feed.getChannelCreationDate()); 205 | assertNotNull(feed.getChannelDescription()); 206 | assertNotNull(feed.getChannelName()); 207 | assertNotNull(feed.getChannelUpdateDate()); 208 | assertNotNull(feed.getChannelLastEntryId()); 209 | assertNotNull(feed.getEntry(feed.getChannelLastEntryId())); 210 | assertNotNull(feed.getEntryList()); 211 | assertNotNull(feed.getEntryMap()); 212 | assertNotNull(feed.getFieldName(1)); 213 | 214 | Entry entry = feed.getChannelLastEntry(); 215 | assertNotNull(entry.getField(1)); 216 | assertNull(entry.getField(2)); 217 | 218 | } 219 | 220 | @Test 221 | public void testGetFieldFeedWithOptions() throws Exception { 222 | System.out.println("testGetFieldFeedWithOptions"); 223 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID); 224 | publicChannel.setUrl(TestChannelSettings.server); 225 | FeedParameters options = new FeedParameters(); 226 | options.status(true); 227 | options.location(true); 228 | Feed feed = publicChannel.getFieldFeed(1, options); 229 | Entry entry = feed.getChannelLastEntry(); 230 | assertNotNull(entry.getField(1)); 231 | assertNull(entry.getField(2)); 232 | assertNotNull(entry.getStatus()); 233 | assertNotNull(entry.getElevation()); 234 | assertNotNull(entry.getLatitude()); 235 | assertNotNull(entry.getLongitude()); 236 | } 237 | 238 | 239 | @Test 240 | public void testGetFieldFeedLastEntry() throws Exception { 241 | System.out.println("testGetFieldFeedLastEntry"); 242 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID); 243 | publicChannel.setUrl(TestChannelSettings.server); 244 | Entry entry = publicChannel.getLastFieldEntry(1); 245 | assertNotNull(entry.getField(1)); 246 | assertNull(entry.getField(2)); 247 | } 248 | 249 | @Test 250 | public void testGetFieldFeedLastEntryWithOptions() throws Exception { 251 | System.out.println("testGetFieldFeedLastEntryWithOptions"); 252 | Channel publicChannel = new Channel(TestChannelSettings.publicChannelID); 253 | publicChannel.setUrl(TestChannelSettings.server); 254 | FeedParameters options = new FeedParameters(); 255 | options.status(true); 256 | options.location(true); 257 | Entry entry = publicChannel.getLastFieldEntry(1, options); 258 | assertNotNull(entry.getField(1)); 259 | assertNull(entry.getField(2)); 260 | assertNotNull(entry.getStatus()); 261 | assertNotNull(entry.getElevation()); 262 | assertNotNull(entry.getLatitude()); 263 | assertNotNull(entry.getLongitude()); 264 | } 265 | 266 | @Test 267 | public void testGetStatus() throws Exception { 268 | System.out.println("testGetStatus"); 269 | Channel channel = new Channel(TestChannelSettings.publicChannelID); 270 | channel.setUrl(TestChannelSettings.server); 271 | Feed feed = channel.getStatusFeed(); 272 | ArrayList entry = feed.getEntryList(); 273 | Entry last = entry.get(entry.size() -1); 274 | assertNotNull(last.getStatus()); 275 | } 276 | 277 | @Test 278 | public void testGetStatusWithOptions() throws Exception { 279 | System.out.println("testGetStatusWithOptions"); 280 | Channel channel = new Channel(TestChannelSettings.publicChannelID); 281 | channel.setUrl(TestChannelSettings.server); 282 | FeedParameters options = new FeedParameters(); 283 | options.offset(-8); 284 | Feed feed = channel.getStatusFeed(options); 285 | ArrayList entry = feed.getEntryList(); 286 | Entry last = entry.get(entry.size() -1); 287 | Date pst = last.getCreated(); 288 | SimpleDateFormat df = new SimpleDateFormat("Z"); 289 | assert(df.format(pst).equals("-0800")); 290 | } 291 | 292 | @Test 293 | public void testGetStatus_emptyFields() throws Exception { 294 | System.out.println("testGetStatusEmptyFields"); 295 | Channel channel = new Channel(TestChannelSettings.publicChannelID); 296 | channel.setUrl(TestChannelSettings.server); 297 | Feed statusFeed = channel.getStatusFeed(); 298 | assertNull(statusFeed.getChannelCreationDate()); 299 | assertNull(statusFeed.getChannelDescription()); 300 | assertNull(statusFeed.getChannelId()); 301 | try { 302 | assertNull(statusFeed.getChannelLastEntry()); 303 | } catch (ThingSpeakException ex) { 304 | /* this is expected */ 305 | } 306 | assertNull(statusFeed.getChannelLastEntryId()); 307 | assertNotNull(statusFeed.getChannelName()); 308 | assertNull(statusFeed.getChannelUpdateDate()); 309 | assertNull(statusFeed.getFieldName(1)); 310 | for (Entry entry : statusFeed.getEntryList()) { 311 | assertNull(entry.getElevation()); 312 | assertNull(entry.getField(1)); 313 | assertNull(entry.getLatitude()); 314 | assertNull(entry.getLongitude()); 315 | } 316 | 317 | } 318 | 319 | } 320 | -------------------------------------------------------------------------------- /test/com/angryelectron/thingspeak/TestChannelSettings.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client 3 | * Copyright 2014, Andrew Bythell 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * theThingSpeak Java Client. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak; 21 | 22 | /** 23 | * Statics for accessing ThingSpeak channels used for Unit Testing. Before testing 24 | * you must create a private and a public channel on the server via the web 25 | * interface. Each channel must have a name, description, and all 8 fields defined. 26 | * The non-public channel must have an API Read Key generated. Enter the 27 | * server and channel information below. 28 | * 29 | * Also note that after 100 tests, data starts to be cached and tests may start 30 | * to fail. Clear all the feed data from the channel to avoid this problem. 31 | */ 32 | public class TestChannelSettings { 33 | 34 | public static String server = "http://api.thingspeak.com"; 35 | public static Integer publicChannelID = 9235; 36 | public static String publicChannelWriteKey = "8OBEPNQB06X9WDMZ"; 37 | public static Integer privateChannelID = 9438; 38 | public static String privateChannelWriteKey = "AATBGE761SG6QE7J "; 39 | public static String privateChannelReadKey = "O7YHHVZMQSXRNJI2"; 40 | public static Integer rateLimit = 16000; 41 | 42 | /* 43 | public static Integer publicChannelID = 1; 44 | public static String publicChannelWriteKey = "N6ES1RM97J3846NU "; 45 | public static Integer privateChannelID = 2; 46 | public static String privateChannelWriteKey = "UGN3RZYIVFKZLIHZ "; 47 | public static String privateChannelReadKey = "PIHR34GGPEKCOINR"; 48 | public static Integer rateLimit = 0; 49 | public static String server = "http://192.168.2.190:3000"; 50 | */ 51 | 52 | } 53 | -------------------------------------------------------------------------------- /test/com/angryelectron/thingspeak/UpdateTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Java Client Copyright 2014, Andrew Bythell 3 | * http://angryelectron.com 4 | * 5 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or (at your 8 | * option) any later version. 9 | * 10 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 13 | * Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along with 16 | * theThingSpeak Java Client. If not, see . 17 | */ 18 | package com.angryelectron.thingspeak; 19 | 20 | import org.apache.log4j.BasicConfigurator; 21 | import org.apache.log4j.Level; 22 | import org.apache.log4j.Logger; 23 | import org.junit.BeforeClass; 24 | import org.junit.Test; 25 | 26 | public class UpdateTest { 27 | 28 | @BeforeClass 29 | public static void setUpClass() throws Exception { 30 | BasicConfigurator.resetConfiguration(); 31 | BasicConfigurator.configure(); 32 | Logger.getLogger("org.apache.http").setLevel(Level.OFF); 33 | pauseForAPIRateLimit(); 34 | } 35 | 36 | /** 37 | * Pause to prevent multiple update requests from exceeding the API rate 38 | * limit. Call all the end of each test to prevent subsequent tests from 39 | * failing. By appending to the end of each test instead of calling it using 40 | * \@After, time can be saved when running tests that throw exceptions and 41 | * don't actually do a successful update. 42 | * 43 | * @throws InterruptedException 44 | */ 45 | private static void pauseForAPIRateLimit() throws InterruptedException { 46 | System.out.println("Waiting for rate limit to expire."); 47 | Thread.sleep(TestChannelSettings.rateLimit); 48 | } 49 | 50 | @Test 51 | public void testUpdateChannel() throws Exception { 52 | System.out.println("testUpdateChannel"); 53 | Channel channel = new Channel(TestChannelSettings.publicChannelID, TestChannelSettings.publicChannelWriteKey); 54 | channel.setUrl(TestChannelSettings.server); 55 | Integer result = channel.update(new Entry()); 56 | assert (result != 0); 57 | pauseForAPIRateLimit(); 58 | } 59 | 60 | @Test(expected = ThingSpeakException.class) 61 | public void testUpdateChannelWithInvalidAPIKey() throws Exception { 62 | System.out.println("testUpdatePublicChannelWithInvalidAPIKey"); 63 | Channel channel = new Channel(TestChannelSettings.publicChannelID, "invalidChannelKey"); 64 | channel.setUrl(TestChannelSettings.server); 65 | try { 66 | channel.update(new Entry()); 67 | } finally { 68 | pauseForAPIRateLimit(); 69 | } 70 | } 71 | 72 | @Test 73 | public void testUpdateSlowerThanAPIRateLimit() throws Exception { 74 | System.out.println("testUpdateSlowerThanAPIRateLimit"); 75 | /** 76 | * Do an update an make sure it succeeds. 77 | */ 78 | Channel channel = new Channel(TestChannelSettings.publicChannelID, TestChannelSettings.publicChannelWriteKey); 79 | channel.setUrl(TestChannelSettings.server); 80 | Integer result0 = channel.update(new Entry()); 81 | assert (result0 != 0); 82 | 83 | /** 84 | * Pause until the rate limit has passed. 85 | */ 86 | Thread.sleep(TestChannelSettings.rateLimit); 87 | 88 | /** 89 | * Do another update and make sure it succeeds. 90 | */ 91 | Integer result1 = channel.update(new Entry()); 92 | assert (result1 != 0); 93 | 94 | pauseForAPIRateLimit(); 95 | } 96 | 97 | @Test(expected = ThingSpeakException.class) 98 | public void testUpdateFasterThanAPIRateLimit() throws Exception { 99 | System.out.println("testUpdateFasterThanAPIRateLimit"); 100 | /** 101 | * On self-hosted servers, there is no rate limiting 102 | */ 103 | if (TestChannelSettings.rateLimit == 0) { 104 | throw new ThingSpeakException("Rate limiting not supported"); 105 | } 106 | 107 | /** 108 | * Do an update and make sure it succeeds. 109 | */ 110 | Channel channel = new Channel(TestChannelSettings.publicChannelID, TestChannelSettings.publicChannelWriteKey); 111 | channel.setUrl(TestChannelSettings.server); 112 | Integer result = channel.update(new Entry()); 113 | assert (result != 0); 114 | 115 | /** 116 | * Don't wait for the rate limit to pass - send another update right 117 | * away. This should cause a ThingSpeakException. 118 | */ 119 | try { 120 | channel.update(new Entry()); 121 | } finally { 122 | pauseForAPIRateLimit(); 123 | } 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /test/com/angryelectron/thingspeak/log4j/ThingSpeakAppenderTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * ThingSpeak Appender for log4j Copyright 2014, Andrew Bythell 3 | * 4 | * http://angryelectron.com 5 | * 6 | * The ThingSpeak Java Client is free software: you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or (at your 9 | * option) any later version. 10 | * 11 | * The ThingSpeak Java Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 14 | * Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * the ThingSpeak Appender. If not, see . 18 | */ 19 | 20 | package com.angryelectron.thingspeak.log4j; 21 | 22 | import com.angryelectron.thingspeak.TestChannelSettings; 23 | import org.apache.log4j.BasicConfigurator; 24 | import org.apache.log4j.Level; 25 | import org.apache.log4j.Logger; 26 | import org.junit.BeforeClass; 27 | import org.junit.Test; 28 | 29 | /** 30 | * Test ThingSpeakAppender. 31 | */ 32 | public class ThingSpeakAppenderTest { 33 | 34 | /** 35 | * Credentials for a test ThingSpeak channel. 36 | */ 37 | private final static Integer channelNumber = 15662; 38 | private final static String apiWriteKey = "I5X9EPC34LPX1HRP"; 39 | 40 | public ThingSpeakAppenderTest() { 41 | } 42 | 43 | @BeforeClass 44 | public static void setUpClass() throws Exception { 45 | BasicConfigurator.resetConfiguration(); 46 | BasicConfigurator.configure(); 47 | Logger.getLogger("org.apache.http").setLevel(Level.OFF); 48 | pauseForAPIRateLimit(); 49 | } 50 | 51 | private static void pauseForAPIRateLimit() throws InterruptedException { 52 | System.out.println("Waiting for rate limit to expire."); 53 | Thread.sleep(TestChannelSettings.rateLimit); 54 | } 55 | 56 | /** 57 | * Test of configureChannel method, of class ThingSpeakAppender. To view the 58 | * logged data on ThingSpeak, visit https://thingspeak.com/channels/15662/feeds. 59 | * @throws java.lang.InterruptedException 60 | */ 61 | @Test 62 | public void testAppend() throws InterruptedException { 63 | System.out.println("testAppend"); 64 | ThingSpeakAppender appender = new ThingSpeakAppender(); 65 | appender.configureChannel(channelNumber, apiWriteKey, null); 66 | appender.setThreshold(Level.INFO); 67 | appender.activateOptions(); 68 | Logger.getRootLogger().addAppender(appender); 69 | Logger.getLogger(this.getClass()).log(Level.INFO, "Test message from ThingSpeakAppender"); 70 | } 71 | } 72 | --------------------------------------------------------------------------------