Commit 8edb93d5968b094736a48399e7e3ed3d1fcb7946

kanoi 2014-05-26T04:05:12

API - lcd all-in-one brief summary

diff --git a/API-README b/API-README
index 076785c..c149642 100644
--- a/API-README
+++ b/API-README
@@ -443,6 +443,12 @@ The list of requests - a (*) means it requires privileged access - and replies:
                                AVA+BTB opt=freq val=256 to 1024 - chip frequency
                                BTB opt=millivolts val=1000 to 1400 - corevoltage
 
+ lcd           LCD            An all-in-one short status summary of the miner
+                              e.g. Elapsed,GHS av,GHS 5m,GHS 5s,Temp,
+                                   Last Share Difficulty,Last Share Time,
+                                   Best Share,Last Valid Work,Found Blocks,
+                                   Pool,User|
+
  lockstats (*) none           There is no reply section just the STATUS section
                               stating the results of the request
                               A warning reply means lock stats are not compiled
@@ -503,6 +509,13 @@ miner.php - an example web page to access the API
 Feature Changelog for external applications using the API:
 
 
+API V3.4 (cgminer v4.3.?)
+
+Added API commands:
+ 'lcd' - An all-in-one short status summary of the miner
+
+---------
+
 API V3.3 (cgminer v4.2.0)
 
 Added API commands:
diff --git a/api.c b/api.c
index 399cf10..0a9a6c9 100644
--- a/api.c
+++ b/api.c
@@ -132,7 +132,7 @@ static const char SEPARATOR = '|';
 #define JOIN_CMD "CMD="
 #define BETWEEN_JOIN SEPSTR
 
-static const char *APIVERSION = "3.3";
+static const char *APIVERSION = "3.4";
 static const char *DEAD = "Dead";
 static const char *SICK = "Sick";
 static const char *NOSTART = "NoStart";
@@ -251,6 +251,7 @@ static const char *OSINFO =
 #define _DEBUGSET	"DEBUG"
 #define _SETCONFIG	"SETCONFIG"
 #define _USBSTATS	"USBSTATS"
+#define _LCD		"LCD"
 
 static const char ISJSON = '{';
 #define JSON0		"{"
@@ -291,6 +292,7 @@ static const char ISJSON = '{';
 #define JSON_DEBUGSET	JSON1 _DEBUGSET JSON2
 #define JSON_SETCONFIG	JSON1 _SETCONFIG JSON2
 #define JSON_USBSTATS	JSON1 _USBSTATS JSON2
+#define JSON_LCD	JSON1 _LCD JSON2
 #define JSON_END	JSON4 JSON5
 #define JSON_END_TRUNCATED	JSON4_TRUNCATED JSON5
 #define JSON_BETWEEN_JOIN	","
@@ -418,6 +420,7 @@ static const char *JSON_PARAMETER = "parameter";
 #define MSG_SETQUOTA 122
 #define MSG_LOCKOK 123
 #define MSG_LOCKDIS 124
+#define MSG_LCD 125
 
 enum code_severity {
 	SEVERITY_ERR,
@@ -583,6 +586,7 @@ struct CODES {
  { SEVERITY_SUCC,  MSG_ASCSETOK, PARAM_BOTH,	"ASC %d set OK" },
  { SEVERITY_ERR,   MSG_ASCSETERR, PARAM_BOTH,	"ASC %d set failed: %s" },
 #endif
+ { SEVERITY_SUCC,  MSG_LCD,	PARAM_NONE,	"LCD" },
  { SEVERITY_SUCC,  MSG_LOCKOK,	PARAM_NONE,	"Lock stats created" },
  { SEVERITY_WARN,  MSG_LOCKDIS,	PARAM_NONE,	"Lock stats not enabled" },
  { SEVERITY_FAIL, 0, 0, NULL }
@@ -3967,6 +3971,79 @@ static void ascset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe
 }
 #endif
 
+static void lcddata(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
+{
+	struct api_data *root = NULL;
+	struct cgpu_info *cgpu;
+	bool io_open;
+	double ghs = 0.0, last_share_diff = 0.0;
+	float temp = 0.0;
+	time_t last_share_time = 0;
+	time_t last_device_valid_work = 0;
+	struct pool *pool = NULL;
+	char *rpc_url = "none", *rpc_user = "";
+	int i;
+
+	message(io_data, MSG_LCD, 0, NULL, isjson);
+	io_open = io_add(io_data, isjson ? COMSTR JSON_LCD : _LCD COMSTR);
+
+	// stop hashmeter() changing some while copying
+	mutex_lock(&hash_lock);
+
+	root = api_add_elapsed(root, "Elapsed", &(total_secs), true);
+	ghs = total_mhashes_done / total_secs / 1000.0;
+	root = api_add_mhs(root, "GHS av", &ghs, true);
+	ghs = rolling5 / 1000.0;
+	root = api_add_mhs(root, "GHS 5m", &ghs, true);
+	ghs = total_rolling / 1000.0;
+	root = api_add_mhs(root, "GHS 5s", &ghs, true);
+
+	mutex_unlock(&hash_lock);
+
+	temp = 0;
+	last_device_valid_work = 0;
+	for (i = 0; i < total_devices; i++) {
+		cgpu = get_devices(i);
+		if (last_device_valid_work == 0 ||
+		    last_device_valid_work < cgpu->last_device_valid_work)
+			last_device_valid_work = cgpu->last_device_valid_work;
+		if (temp < cgpu->temp)
+			temp = cgpu->temp;
+	}
+
+	last_share_time = 0;
+	last_share_diff = 0;
+	for (i = 0; i < total_pools; i++) {
+		pool = pools[i];
+
+		if (pool->removed)
+			continue;
+
+		if (last_share_time == 0 || last_share_time < pool->last_share_time) {
+			last_share_time = pool->last_share_time;
+			last_share_diff = pool->last_share_diff;
+		}
+	}
+	pool = current_pool();
+	if (pool) {
+		rpc_url = pool->rpc_url;
+		rpc_user = pool->rpc_user;
+	}
+
+	root = api_add_temp(root, "Temperature", &temp, false);
+	root = api_add_diff(root, "Last Share Difficulty", &last_share_diff, false);
+	root = api_add_time(root, "Last Share Time", &last_share_time, false);
+	root = api_add_uint64(root, "Best Share", &best_diff, true);
+	root = api_add_time(root, "Last Valid Work", &last_device_valid_work, false);
+	root = api_add_uint(root, "Found Blocks", &found_blocks, true);
+	root = api_add_escape(root, "Current Pool", rpc_url, true);
+	root = api_add_escape(root, "User", rpc_user, true);
+
+	root = print_data(io_data, root, isjson, false);
+	if (isjson && io_open)
+		io_close(io_data);
+}
+
 static void checkcommand(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, char group);
 
 struct CMDS {
@@ -4022,6 +4099,7 @@ struct CMDS {
 	{ "ascset",		ascset,		true,	false },
 #endif
 	{ "asccount",		asccount,	false,	true },
+	{ "lcd",		lcddata,	false,	true },
 	{ "lockstats",		lockstats,	true,	true },
 	{ NULL,			NULL,		false,	false }
 };