You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
8.0 KiB

#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include "cfg.h"
#include "fbsh.h"
Config config;
FBSmartHome fbsh;
void help() {
printf ("fbsh-cli: parameters command options\n");
printf (" Client for accessing the fritzbox smart home devices.\n");
printf (" Most parameters can be set in a seperate config file.\n");
printf ("Commands:\n");
printf ("\thelp displays this helptext.\n");
printf ("\tconnect connect to the device, it will save the SID file.\n");
printf ("\tlist lists all devices, connection will be recovered from SID.\n");
printf ("\t If silent parameter is set, we will not be asked for credentials\n");
printf ("\tlisthkr shows a list of Heating Crontrols\n");
printf ("\tgethkr AIN options reads heating control device\n");
printf ("\t -nochart display no history chart\n");
printf ("\tswitch AIN on/off/toggle Turns the switch on/off\n");
printf ("\tcolor AIN hue,sat,duration Set the Color of the Light\n");
printf ("\t hue (0..356) sat (0..255) duration (ms)\n");
printf ("\tlevel AIN level Light level (0..255)\n");
printf ("\tsaveconfig save configfile.\n");
printf ("\n");
printf ("Parameters:\n");
printf ("\t-config <CONFIGGILE> configfile to use\n");
printf ("\t-host <HOSTNAME> the fritzbox to connect to (http://fritz.box)\n");
printf ("\t-user <USERNAME> username to connect with\n");
printf ("\t-pass <PASSWORD> password to use\n");
printf ("\t-sidfile <SIDFILE> file which contains the session id\n");
printf ("\t-savepassword <0|1> should the password be saved in the configfile\n");
printf ("\n");
printf ("\t-silent <0|1> enable/disable: no questions about username or password\n");
printf ("\t-S enable silent mode\n");
printf ("\n");
exit (0);
}
void connect() {
fbsh.UseSIDFile(config.GetSIDFile());
if (fbsh.Connect(config.GetFBHostname()) != 0) {
if (fbsh.Connect(config.GetFBHostname(), config.GetUsername(), config.GetPassword()) != 0) {
fprintf (stderr, "could not connect to: %s Error:%s\n", config.GetFBHostname().c_str(), strerror(errno));
}
}
}
void getlist(int argpos, int argc, char **argv) {
std::list<FBSmartHomeDevice> *devlist;
std::list<FBSmartHomeDevice>::iterator iter;
connect ();
devlist = fbsh.GetDevices();
if (devlist == NULL) {
fprintf (stderr, "Error Retrieving Devices: %s\n", strerror(errno));
return;
}
for (iter = devlist->begin(); iter != devlist->end(); iter++) {
printf ("%s\t%s\t%s\t%s\n", iter->id.c_str(), iter->type.c_str(), iter->name.c_str(), iter->others.c_str());
}
delete devlist;
}
#define CHARTSIZE 20
void gethkr(int argci, int argc, char **argv) {
FBSmartHomeHKR hkr;
list<float>::iterator iter;
float fmin, fmax;
std::string s;
int c, i;
char t[64];
struct tm *tmp;
time_t ctime = time(NULL);
char *ain = argv[++argci];
bool nochart = false;
for (argci++; argci < argc; argci++) {
if (strcmp(argv[argci], "-nochart") == 0)
nochart = true;
}
if (ain == NULL) return;
connect();
fbsh.GetHKR((std::string)ain, &hkr);
printf ("Name:%s ID:%s\n", hkr.name.c_str(), hkr.id.c_str());
printf (" Temp: %.1f\n", hkr.temp);
printf (" Temp Akt: %.1f\n", hkr.temp_cur);
printf (" Temp Set: %.1f\n", hkr.temp_set);
printf (" Temp Komfort: %.1f\n", hkr.temp_h);
printf (" Temp Absenk: %.1f\n", hkr.temp_l);
printf (" Offset: %d\n", hkr.offset);
printf (" Grid: %d\n", hkr.statsgrid);
if (!nochart) {
for (iter = hkr.stats.begin(); iter != hkr.stats.end(); iter++) {
if (iter == hkr.stats.begin()) fmin = fmax = *iter;
if (fmin > *iter) fmin = *iter;
if (fmax < *iter) fmax = *iter;
}
printf (" %-2.1f %-2.1f\n", fmin/10.0, fmax/10.0);
ctime = (ctime / hkr.statsgrid) * hkr.statsgrid;
for (iter = hkr.stats.begin(); iter != hkr.stats.end(); iter++) {
s = " ";
c = ((float)(CHARTSIZE * (*iter - fmin) / (fmax-fmin)));
for (i = 0; i < c; i++) s += " ";
s += "*";
for (i = c+1; i <= CHARTSIZE; i++) s += " ";
ctime -= hkr.statsgrid;
tmp = localtime(&ctime);
strftime(t, 64, "%H:%M", tmp);
printf (" %s |%s|\n", t, s.c_str());
}
printf (" %-2.1f %-2.1f\n", fmin/10.0, fmax/10.0);
}
}
void listhkr(int argci, int argc, char **argv) {
FBSmartHomeHKR hkr;
std::list<FBSmartHomeDevice> *devlist;
std::list<FBSmartHomeDevice>::iterator iter;
connect ();
devlist = fbsh.GetDevices();
if (devlist == NULL) {
fprintf (stderr, "Error Retrieving Devices: %s\n", strerror(errno));
return;
}
for (iter = devlist->begin(); iter != devlist->end(); iter++) {
if (iter->type.compare("hkr") == 0) {
fbsh.GetHKR(iter->id, &hkr);
printf ("%s name:%s temp:%.1f cur:%.1f set:%.1f\n", hkr.id.c_str(), hkr.name.c_str(), hkr.temp, hkr.temp_cur, hkr.temp_set);
}
}
delete devlist;
}
void switchonoff (int argci, int argc, char **argv) {
if (argci+2 >= argc) {
fprintf (stderr, "SwitchOnOff missing parameter?\n");
}
char *ain = argv[++argci];
char *onoff = argv[++argci];
int ionoff = 0;
if (ain == NULL) return;
if (onoff == NULL) return;
connect();
if (strcmp (onoff, "off") == 0) ionoff = 0;
else if (strcmp (onoff, "on") == 0) ionoff = 1;
else if (strcmp (onoff, "toggle") == 0) ionoff = 2;
fbsh.SimpleOnOff(ain, ionoff);
}
void lightlevel (int argci, int argc, char **argv) {
if (argci+2 >= argc) {
fprintf (stderr, "Level missing parameter?\n");
}
char *ain = argv[++argci];
char *level = argv[++argci];
if (ain == NULL) return;
if (level == NULL) return;
connect();
fbsh.SetLevel(ain, atoi(level));
}
void light (int argci, int argc, char **argv) {
if (argci+2 >= argc) {
fprintf (stderr, "SwitchOnOff missing parameter?\n");
}
char *ain = argv[++argci];
char *color = argv[++argci];
int ihue = 0;
int isat = 0;
int iduration = 0;
if (ain == NULL) return;
if (color == NULL) return;
sscanf (color, "%d,%d,%d", &ihue, &isat, &iduration);
connect();
fbsh.SetColor(ain, ihue, isat, iduration);
}
int main(int argc, char** argv) {
int i;
if (argc < 2 || argv == NULL) help();
else {
for (i = 1; i < argc; i++)
if (argv[i] != NULL) {
//
// commands to run, all parameters must be set by here.
//
if (strcmp(argv[i], "help") == 0) {
help();
break;
}
else if (strcmp(argv[i], "connect") == 0) {
connect();
printf ("SID:%s\n", fbsh.GetSID().c_str());
break;
}
else if (strcmp(argv[i], "list") == 0) {
getlist(i, argc, argv);
break;
}
else if (strcmp(argv[i], "saveconfig") == 0) {
config.SaveConfig();
break;
}
else if (strcmp(argv[i], "gethkr") == 0) {
gethkr(i, argc, argv);
}
else if (strcmp(argv[i], "listhkr") == 0) {
listhkr(i, argc, argv);
}
else if (strcmp(argv[i], "switch") == 0) {
switchonoff(i, argc, argv);
}
else if (strcmp(argv[i], "color") == 0) {
light(i, argc, argv);
}
else if (strcmp(argv[i], "level") == 0) {
lightlevel(i, argc, argv);
}
//
// parameters will follow here
//
else if (strcmp(argv[i], "-host") == 0) {
config.SetFBHostname(argv[++i]);
}
else if (strcmp(argv[i], "-user") == 0) {
config.SetUsername(argv[++i]);
}
else if (strcmp(argv[i], "-pass") == 0) {
config.SetPassword(argv[++i]);
}
else if (strcmp(argv[i], "-silent") == 0) {
config.SetSilent(atoi(argv[++i]));
}
else if (strcmp(argv[i], "-S") == 0) {
config.SetSilent(true);
}
else if (strcmp(argv[i], "-sidfile") == 0) {
config.SetSIDFile(argv[++i]);
}
else if (strcmp(argv[i], "-savepassword") == 0) {
config.SetSavePassword(atoi(argv[++i]));
}
else if (strcmp(argv[i], "-config") == 0) {
config.SetFilename(argv[++i]);
if (config.LoadConfig() < 0)
fprintf (stderr, "could not load config file '%s'\n", argv[i]);
}
}
}
return 0;
}