Change output format parseable

This commit is contained in:
shakemid 2017-09-14 11:56:09 +09:00
parent 56fb12dab4
commit eb73622194
2 changed files with 17 additions and 56 deletions

View file

@ -8,7 +8,6 @@
set -o nounset set -o nounset
pcsensor=${pcsensor:-/usr/local/bin/pcsensor} pcsensor=${pcsensor:-/usr/local/bin/pcsensor}
pcsensor_cmd="${pcsensor} -m -c"
# need calibration # need calibration
# example: # example:
@ -39,7 +38,7 @@ fetch() {
i=0 i=0
while [ "$i" -le "$retry" ]; while [ "$i" -le "$retry" ];
do do
value=$( $pcsensor_cmd | sed -n '1p' ) value=$( $pcsensor | sed -n '1p' | awk '{ print $4 }' )
if [ -n "$value" ];then if [ -n "$value" ];then
break break

View file

@ -62,9 +62,7 @@ const static char uIni2[] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 };
static int bsalir=1; static int bsalir=1;
static int debug=0; static int debug=0;
static int seconds=5; static int seconds=5;
static int formato=0; static int formato=1; //Celsius
static int mrtg=0;
static int calibration=0;
static libusb_context *ctx = NULL; static libusb_context *ctx = NULL;
@ -237,26 +235,21 @@ int main( int argc, char **argv) {
int numdev,i; int numdev,i;
unsigned char *answer; unsigned char *answer;
int temperature; int temperature;
char strdate[20];
float tempInC, tempExC; float tempInC, tempExC;
int c; int c;
struct tm *local; struct tm *local;
time_t t; time_t t;
while ((c = getopt (argc, argv, "mfcvhl::a:")) != -1) while ((c = getopt (argc, argv, "fvhl::")) != -1)
switch (c) switch (c)
{ {
case 'v': case 'v':
debug = 1; debug = 1;
break; break;
case 'c':
formato=1; //Celsius
break;
case 'f': case 'f':
formato=2; //Fahrenheit formato=2; //Fahrenheit
break; break;
case 'm':
mrtg=1;
break;
case 'l': case 'l':
if (optarg!=NULL){ if (optarg!=NULL){
if (!sscanf(optarg,"%i",&seconds)==1) { if (!sscanf(optarg,"%i",&seconds)==1) {
@ -271,13 +264,6 @@ int main( int argc, char **argv) {
seconds = 5; seconds = 5;
break; break;
} }
case 'a':
if (!sscanf(optarg,"%i",&calibration)==1) {
fprintf (stderr, "Error: '%s' is not numeric.\n", optarg);
exit(EXIT_FAILURE);
} else {
break;
}
case '?': case '?':
case 'h': case 'h':
printf("pcsensor version %s\n",VERSION); printf("pcsensor version %s\n",VERSION);
@ -285,10 +271,7 @@ int main( int argc, char **argv) {
printf(" -h help\n"); printf(" -h help\n");
printf(" -v verbose\n"); printf(" -v verbose\n");
printf(" -l[n] loop every 'n' seconds, default value is 5s\n"); printf(" -l[n] loop every 'n' seconds, default value is 5s\n");
printf(" -c output only in Celsius\n"); printf(" -f output in Fahrenheit (default: Celsius)\n");
printf(" -f output only in Fahrenheit\n");
printf(" -a[n] increase or decrease temperature in 'n' degrees for device calibration\n");
printf(" -m output for mrtg integration\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
default: default:
@ -334,49 +317,28 @@ int main( int argc, char **argv) {
interrupt_read(handles[i], answer); interrupt_read(handles[i], answer);
temperature = (answer[3] & 0xFF) + ((signed char)answer[2] << 8); temperature = (answer[3] & 0xFF) + ((signed char)answer[2] << 8);
temperature += calibration;
tempInC = temperature * (125.0 / 32000.0); tempInC = temperature * (125.0 / 32000.0);
temperature = (answer[5] & 0xFF) + ((signed char)answer[4] << 8); temperature = (answer[5] & 0xFF) + ((signed char)answer[4] << 8);
temperature += calibration;
tempExC = temperature * (125.0 / 32000.0); tempExC = temperature * (125.0 / 32000.0);
t = time(NULL); t = time(NULL);
local = localtime(&t); local = localtime(&t);
if (mrtg) { sprintf(strdate, "%04d-%02d-%02dT%02d:%02d:%02d",
if (formato==2) { local->tm_year +1900,
printf("%.2f\n", (9.0 / 5.0 * tempInC + 32.0)); local->tm_mon + 1,
printf("%.2f\n", (9.0 / 5.0 * tempExC + 32.0)); local->tm_mday,
} else { local->tm_hour,
printf("%.2f\n", tempInC); local->tm_min,
printf("%.2f\n", tempExC); local->tm_sec);
}
printf("%02d:%02d\n", if (formato==2) {
local->tm_hour, printf("%s\t%d\tinternal\t%.2f\n", strdate, i, (9.0 / 5.0 * tempInC + 32.0));
local->tm_min); printf("%s\t%d\texternal\t%.2f\n", strdate, i, (9.0 / 5.0 * tempExC + 32.0));
printf("pcsensor:%d\n", i);
} else { } else {
printf("%04d/%02d/%02d %02d:%02d:%02d\n", printf("%s\t%d\tinternal\t%.2f\n", strdate, i, tempInC);
local->tm_year +1900, printf("%s\t%d\texternal\t%.2f\n", strdate, i, tempExC);
local->tm_mon + 1,
local->tm_mday,
local->tm_hour,
local->tm_min,
local->tm_sec);
if (formato==2) {
printf("Temperature (%d:internal) %.2fF\n", i, (9.0 / 5.0 * tempInC + 32.0));
printf("Temperature (%d:external) %.2fF\n", i, (9.0 / 5.0 * tempExC + 32.0));
} else if (formato==1) {
printf("Temperature (%d:internal) %.2fC\n", i, tempInC);
printf("Temperature (%d:external) %.2fC\n", i, tempExC);
} else {
printf("Temperature (%d:internal) %.2fF %.2fC\n", i, (9.0 / 5.0 * tempInC + 32.0), tempInC);
printf("Temperature (%d:external) %.2fF %.2fC\n", i, (9.0 / 5.0 * tempExC + 32.0), tempExC);
}
} }
if (!bsalir) if (!bsalir)