Station API

Dependents:   GMCStation

Revision:
2:a9d1a9c92927
Parent:
1:a22e390c70b3
Child:
3:40b1b79f4998
--- a/PachubeClient.h	Mon Dec 12 02:33:21 2011 +0000
+++ b/PachubeClient.h	Mon Dec 12 11:41:24 2011 +0000
@@ -26,42 +26,79 @@
 #include "SimpleSocket.h"
 #include "Utils.h"
 
+/**
+ * Client API for accessing Pachube
+ */
 class PachubeClient {
 public:
-    PachubeClient(char *filename, bool verbose = false)
-            : length(0), verbose(verbose) {
-        char path[32];
-        LocalFileSystem local("local");
-        sprintf(path, "/local/%s", filename);
-
-        if (FILE *fp = fopen(path, "r")) {
-            Utils::fgetValues(fp, "feed-id:%d", &feedid);
-            Utils::fgetValues(fp, "api-key:%s", apikey);
-            fclose(fp);
-            if (verbose) {
-                ::printf("feed-id = %d\n", feedid);
-                ::printf("api-key = %s\n", apikey);
-            }
-        } else
-            error("open error\n");
-    }
-
+    /**
+     * creates an PachubeClient object
+     *
+     * @param feedid Feed ID
+     * @param apikey API Key
+     * @param verbose if true display debug info
+     */
     PachubeClient(int feedid, char *apikey, bool verbose = false)
             : feedid(feedid), length(0), verbose(verbose) {
         strcpy(this->apikey, apikey);
     }
 
+    /**
+     * creates an PachubeClient object
+     *
+     * @param filename name of the configuration file
+     * @param verbose if true display debug info
+     */
+    static PachubeClient create(char *filename, bool verbose = false) {
+        int feedid = 0;
+        char apikey[64] = {};
+        
+        if (filename) {
+            char path[32];
+            LocalFileSystem local("local");
+            sprintf(path, "/local/%s", filename);
+            if (FILE *fp = fopen(path, "r")) {
+                Utils::fgetValues(fp, "feed-id:%d", &feedid);
+                Utils::fgetValues(fp, "api-key:%s", apikey);
+                fclose(fp);
+                if (verbose) {
+                    printf("feed-id = %d\n", feedid);
+                    printf("api-key = %s\n", apikey);
+                }
+            }
+        }
+        
+        return PachubeClient(feedid, apikey, verbose);
+    }
+    
+    /**
+     * adds data to the Pachube datastream
+     *
+     * @param id Datastream ID
+     * @param value value to add
+     */
     void add(int id, float value) {
         length += sprintf(&data[length], "%d,%f\n", id, value);
     }
 
+    /**
+     * adds data to the Pachube datastream
+     *
+     * @param id Datastream ID
+     * @param value value to add
+     */
     void add(char *id, float value) {
         length += sprintf(&data[length], "%s,%f\n", id, value);
     }
-
+    
+    /**
+     * updates datastreams
+     *
+     * @returns true if succeeded
+     */
     bool update() {
         bool result = false;
-        
+
         ClientSocket client("api.pachube.com", 80);
         if (client) {
             const char *request = "PUT /v2/feeds/%d.csv HTTP/1.1\r\n"
@@ -74,8 +111,8 @@
             client.printf(request, feedid, apikey, length);
             client.write(data, length);
             if (verbose) {
-                ::printf(request, feedid, apikey, length);
-                ::printf("%s", data);
+                printf(request, feedid, apikey, length);
+                printf("%s", data);
             }
 
             while (client) {
@@ -86,7 +123,7 @@
                         if (!result && strncmp(response, "HTTP/1.1 200", 12) == 0)
                             result = true;
                         if (verbose)
-                            ::printf("%s", response);
+                            printf("%s", response);
                     }
                     client.close();
                 }