bool ActionMail::Execute() { IPCam *camera = NULL; if (mail_attachment != "") camera = dynamic_cast<IPCam *>(ListeRoom::Instance().get_io(mail_attachment)); if (camera) { cInfoDom("rule.action.mail") << "Need to download camera (" << camera->get_param("name") << ") attachment"; std::string tmpFile; int cpt = 0; //Get a temporary filename do { tmpFile = "/tmp/calaos_mail_attachment_"; tmpFile += Utils::to_string(cpt); cpt++; } while (ecore_file_exists(tmpFile.c_str())); // Autodestroy file downloader cDebug() << "DL URL: " << camera->getPictureUrl(); FileDownloader* downloader = new FileDownloader(camera->getPictureUrl(), tmpFile, true); downloader->addCallback([=](std::string signal, void *sender_data) { if (signal == "done") { mail_attachment_tfile = *(reinterpret_cast<std::string *>(sender_data)); sendMail(); } else if (signal == "failed" || signal == "aborted") { mail_attachment_tfile.clear(); sendMail(); } }); downloader->Start(); } else { sendMail(); cInfoDom("rule.action.mail") << "Ok, mail is in queue"; } return true; }
KNXOutputLight::KNXOutputLight(Params &p): OutputLight(p) { useRealState = true; // Define IO documentation ioDoc->friendlyNameSet("KNXOutputLight"); ioDoc->descriptionSet(_("Light output with KNX and eibnetmux")); ioDoc->linkAdd("eibnetmux", _("http://eibnetmux.sourceforge.net")); ioDoc->paramAdd("knx_group", _("KNX Group address, Ex: x/y/z"), IODoc::TYPE_STRING, true); std::string knx_group = get_param("knx_group"); //KNXCtrl::Instance(get_param("host"))->readValue(knx_group, KNXValue::EIS_Switch_OnOff); KNXCtrl::Instance(get_param("host"))->valueChanged.connect([=](const std::string group_addr, const KNXValue &val) { if (group_addr != get_param("knx_group")) return; if (val.toBool()) set_value(std::string("set_state true")); else set_value(std::string("set_state false")); }); cInfoDom("input") << "knx_group: " << knx_group; }
void xPLController::Connect() { int portTCP; m_UdpRecvHandle = uvw::Loop::getDefault()->resource<uvw::UDPHandle>(); m_UdpRecvHandle->on<uvw::UDPDataEvent>([this](const uvw::UDPDataEvent &ev, auto &) { this->udpListenData(ev.data.get(), ev.length, ev.sender.ip, ev.sender.port); }); m_UdpRecvHandle->once<uvw::ErrorEvent>([this](const uvw::ErrorEvent &ev, uvw::UDPHandle &h) { cErrorDom("xpl") << "xPL UDP server error : " << ev.what(); h.once<uvw::CloseEvent>([this](auto &, auto &) { Timer::singleShot(2.5, (sigc::slot<void>)sigc::mem_fun(*this, &xPLController::Connect)); }); h.close(); }); portTCP = discoverxPLPort(); m_UdpRecvHandle->broadcast(true); cInfoDom("xpl") << "Listening on port " << portTCP; m_UdpRecvHandle->bind("0.0.0.0", portTCP, uvw::UDPHandle::Bind::REUSEADDR); m_UdpRecvHandle->recv(); m_xPLDevice.SetRecvSockInfo(localAddress(), portTCP); m_xPLDevice.Close(); m_xPLDevice.Open(); }
bool IOAVReceiver::set_value(std::string val) { if (!isEnabled() || !receiver) return false; cInfoDom("output") << get_param("id") << " got action, " << val; if (val == "power off" || val == "power false") receiver->Power(false, zone); else if (val == "power on" || val == "power true") receiver->Power(true, zone); else if (val.substr(0, 7) == "volume ") { val.erase(0, 7); int vol; from_string(val, vol); receiver->setVolume(vol, zone); } else if (val.substr(0, 7) == "source ") { val.erase(0, 7); int source; from_string(val, source); receiver->selectInputSource(source, zone); } else if (val.substr(0, 7) == "custom ") { val.erase(0, 7); receiver->sendCustomCommand(val); } return true; }
/* This print version will replace the one in base lib. It prints out everything * through the shared logging facility instead of stdout */ int Lua_print(lua_State *L) { int n = lua_gettop(L); std::string msg; for (int i = 1;i <= n;i++) { if (i > 1) msg += " "; if (lua_isstring(L, i)) msg += lua_tostring(L, i); else if (lua_isnil(L, i)) msg += "nil"; else if (lua_isboolean(L, i)) msg += lua_toboolean(L, i)? "true" : "false"; else msg += Utils::to_string(luaL_typename(L, i)) + Utils::to_string(":") + Utils::to_string(lua_topointer(L, i)); } #ifdef CALAOS_INSTALLER LuaPrinter::Instance().Print(QString::fromUtf8(msg.c_str())); #else cInfoDom("script.lua") << "LuaPrint: "<< msg; #endif return 0; }
KNXOutputLightDimmer::KNXOutputLightDimmer(Params &p): OutputLightDimmer(p) { useRealState = true; // Define IO documentation ioDoc->friendlyNameSet("KNXOutputLightDimmer"); ioDoc->descriptionSet(_("Light dimmer with KNX and eibnetmux")); ioDoc->linkAdd("eibnetmux", _("http://eibnetmux.sourceforge.net")); ioDoc->paramAdd("knx_group", _("KNX Group address, Ex: x/y/z"), IODoc::TYPE_STRING, true); std::string knx_group = get_param("knx_group"); //KNXCtrl::Instance(get_param("host"))->readValue(knx_group, KNXValue::EIS_Switch_OnOff); KNXCtrl::Instance(get_param("host"))->valueChanged.connect([=](const std::string group_addr, const KNXValue &v) { if (group_addr != get_param("knx_group")) return; KNXValue val = v; val.setEis(KNXValue::EIS_Dim_UpDown); value = val.toInt(); EmitSignalIO(); emitChange(); }); cInfoDom("input") << "knx_group: " << knx_group; }
bool ActionStd::LoadFromXml(TiXmlElement *node) { node = node->FirstChildElement(); for (; node; node = node->NextSiblingElement()) { if (node->ValueStr() == "calaos:output") { std::string id = "", val = "", val_var = ""; if (node->Attribute("id")) id = node->Attribute("id"); if (node->Attribute("val")) val = node->Attribute("val"); if (node->Attribute("val_var")) val_var = node->Attribute("val_var"); if (id == "OutTouchscreen" && ListeRule::Instance().size() > 0) { cInfoDom("rule.action.standard") << "Converting old OutTouchscreen to new ActionTouchscreen"; Rule *rule = ListeRule::Instance().get_rule(ListeRule::Instance().size() - 1); ActionTouchscreen *action = new ActionTouchscreen(val); rule->AddAction(dynamic_cast<Action *>(action)); } IOBase *out = ListeRoom::Instance().get_io(id); if (!out) { //for compatibility with old AudioPlayer and Camera, update ids if needed std::list<IOBase *> l = ListeRoom::Instance().getAudioList(); for (IOBase *io: l) { if (io->get_param("iid") == id || io->get_param("oid") == id) out = io; } l = ListeRoom::Instance().getCameraList(); for (IOBase *io: l) { if (io->get_param("iid") == id || io->get_param("oid") == id) out = io; } } if (out && out->isOutput()) { Add(out); params.Add(id, val); if (val_var != "") params_var.Add(id, val_var); } else { return false; } } } return true; }
bool ActionScript::Execute() { ScriptExec::ExecuteScriptDetached(script, [=](bool ret) { cInfoDom("rule.action.script") << "Script finished with " << (ret?"true":"false"); }); return true; }
OutputString::OutputString(Params &p): IOBase(p, IOBase::IO_OUTPUT), value("") { set_param("gui_type", "string_out"); readConfig(); cInfoDom("output") << get_param("id") << ": Ok"; }
MySensorsInputString::MySensorsInputString(Params &p): InputString(p) { // Define IO documentation ioDoc->friendlyNameSet("MySensorsInputString"); ioDoc->descriptionSet(_("Display string from MySensors node")); ioDoc->linkAdd("MySensors", _("http://mysensors.org")); ioDoc->paramAdd("node_id", _("Node ID as set in your network"), IODoc::TYPE_STRING, true); ioDoc->paramAdd("sensor_id", _("Sensor ID, as set in your node"), IODoc::TYPE_STRING, true); Params gwlist = {{ "serial", _("Serial") }, { "tcp", _("Tcp") }}; ioDoc->paramAddList("gateway", _("Gateway type used, tcp or serial are supported"), true, gwlist, "serial"); ioDoc->paramAdd("port", _("If using serial gateway, port is the serial port (/dev/ttyUSB0 for ex.). If using tcp gateway port is TCP port of the gateway."), IODoc::TYPE_STRING, true, "/dev/ttyUSB0"); ioDoc->paramAdd("host", _("IP address of the tcp gateway if relevant"), IODoc::TYPE_STRING, true); std::string nodeId = get_param("node_id"); std::string sensorId = get_param("sensor_id"); MySensorsController::Instance(get_params()).registerIO(nodeId, sensorId, [=]() { // Read the value std::string nId = get_param("node_id"); std::string sId = get_param("sensor_id"); std::string v = MySensorsController::Instance(get_params()).getValue(nId, sId); if (v != current) { current = v; cInfoDom("input") << "node_id: " << nodeId << " sensor_id: " << sensorId << " new value: " << value; emitChange(); } }); cInfoDom("input") << "node_id: " << nodeId << " sensor_id: " << sensorId; }
HttpServer::HttpServer(int p): port(p), tcp_server(NULL) { /* Setup ecore con TCP server and callbacks */ tcp_server = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "0.0.0.0", port, this); ecore_con_server_data_set(tcp_server, this); event_handler_client_add = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)_ecore_con_handler_client_add, this); event_handler_client_del = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, (Ecore_Event_Handler_Cb)_ecore_con_handler_data_get, this); event_handler_client_write = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_WRITE, (Ecore_Event_Handler_Cb)_ecore_con_handler_data_write, this); event_handler_data_get = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)_ecore_con_handler_client_del, this); cDebugDom("network") << "Init TCP Server"; cInfoDom("network") << "Listening on port " << port; }
WebOutputLight::WebOutputLight(Params &p): OutputLight(p) { ioDoc->friendlyNameSet("WebOutputLight"); ioDoc->descriptionSet(_("Bool output written to a web document or URL")); ioDoc->paramAdd("url", _("URL where to POST the document to. The POST request is associated " "with the data field if not void. When no data is provided, " "Calaos substitutes __##VALUE##__ string with the value to send. For example " "if the url is http://example.com/api?value=__##VALUE##__ the url post will be :\n" "http://example.com/api?value=20.3\nThe url is encoded before being sent.\n" "If the URL is of type file:// the data is written to a file."), IODoc::TYPE_STRING, true); ioDoc->paramAdd("data", _("The document send when posting data. This value can be void, in, that case the value " "is substituted in the url, otherwise the __##VALUE##__ contained in data is substituted with " "with the value to be sent."), IODoc::TYPE_STRING, true); ioDoc->paramAdd("data_type", _("The HTTP header Content-Type used when posting the document. " "It depends on the website, but you can use application/json " "application/xml as correct values."), IODoc::TYPE_STRING, true); ioDoc->paramAdd("file_type",_("File type of the document. Values can be xml, json or text."), IODoc::TYPE_STRING, true); ioDoc->paramAdd("path",_("The path where to found the value. This value can take multiple values " "depending on the file type. If file_type is JSON, the json file " "downloaded will be read, and the informations will be extracted from " "the path. for example weather[0]/description, try to read the " "description value of the 1 element of the array of the weather object.\n" "If file_type is XML, the path is an xpath expression; Look here for " "syntax : http://www.w3schools.com/xsl/xpath_syntax.asp " "If file_type is TEXT, the downloaded file is returned as " "plain text file, and path must be in the form line/pos/separator " "Line is read, and is split using separator as delimiters " "The value returned is the value at pos in the split list. " "If the separator is not found, the whole line is returned. " "Example the file contains \n" "10.0,10.1,10.2,10.3\n" "20.0,20.1,20.2,20.3\n" "If the path is 2/4/, the value returne wil be 20.3\n"), IODoc::TYPE_STRING, true); cInfoDom("output") << "WebOutputLight::WebOutputLight()"; }
WebInputTemp::WebInputTemp(Params &p): InputTemp(p) { ioDoc->friendlyNameSet("WebInputTemp"); ioDoc->descriptionSet(_("Temperature input read from a web document")); ioDoc->paramAdd("url", _("URL where to download the document from.\n" "If URL begins with file:// the data is read from the local file"), IODoc::TYPE_STRING, true); ioDoc->paramAdd("file_type",_("File type of the document. Values can be xml, json or text."), IODoc::TYPE_STRING, true); ioDoc->paramAdd("path",_("The path where to found the value. This value can take multiple values " "depending on the file type. If file_type is JSON, the json file " "downloaded will be read, and the informations will be extracted from " "the path. for example weather[0]/description, try to read the " "description value of the 1 element of the array of the weather object.\n" "If file_type is XML, the path is an xpath expression; Look here for " "syntax : http://www.w3schools.com/xsl/xpath_syntax.asp " "If file_type is TEXT, the downloaded file is returned as " "plain text file, and path must be in the form line/pos/separator " "Line is read, and is split using separator as delimiters " "The value returned is the value at pos in the split list. " "If the separator is not found, the whole line is returned. " "Example the file contains \n" "10.0,10.1,10.2,10.3\n" "20.0,20.1,20.2,20.3\n" "If the path is 2/4/, the value returne wil be 20.3\n"), IODoc::TYPE_STRING, true); cInfoDom("input") << "WebInputTemp::WebInputTemp()"; Calaos::StartReadRules::Instance().addIO(); // Add input to WebCtrl instance WebCtrl::Instance(p).Add(get_param("path"), readTime, [=]() { readValue(); Calaos::StartReadRules::Instance().ioRead(); }); }
MySensorsInputString::~MySensorsInputString() { cInfoDom("input"); }
bool AudioPlayer::set_value(std::string value) { if (!isEnabled()) return true; std::string val = value; cInfoDom("output") << "AudioPlayer(" << get_param("id") << "): got action \"" << val << "\""; //list of all available player functions if (val == "play") Play(); else if (val == "pause") Pause(); else if (val == "stop") Stop(); else if (val == "next") Next(); else if (val == "previous") Previous(); else if (val == "power on") Power(true); else if (val == "power off") Power(false); else if (val.compare(0, 6, "sleep ") == 0) { val.erase(0, 6); Sleep(atoi(val.c_str())); } else if (val.compare(0, 5, "sync ") == 0) { val.erase(0, 5); Synchronize(val.c_str(), true); } else if (val.compare(0, 7, "unsync ") == 0) { val.erase(0, 7); Synchronize(val.c_str(), false); } else if (val.compare(0, 5, "play ") == 0) { val.erase(0, 5); playlist_play_items(val); } else if (val.compare(0, 4, "add ") == 0) { val.erase(0, 4); playlist_add_items(val); } else if (val.compare(0, 11, "volume set ") == 0) { val.erase(0, 11); int vol; from_string(val, vol); set_volume(vol); } else if (val.compare(0, 10, "volume up ") == 0) { val.erase(0, 10); AudioPlayerData data; data.svalue = val; get_volume(sigc::mem_fun(*this, &AudioPlayer::get_volume_cb), data); } else if (val.compare(0, 12, "volume down ") == 0) { val.erase(0, 12); AudioPlayerData data; data.svalue = "-" + val; get_volume(sigc::mem_fun(*this, &AudioPlayer::get_volume_cb), data); } EventManager::create(CalaosEvent::EventIOChanged, { { "id", get_param("id") }, { "state", value } }); return true; }
void ModuleManager::SearchModules() { for (uint i = 0; i < search_paths.size(); i++) { cInfoDom("module") << "ModuleManager: searching modules in: " << search_paths[i]; char *fname = NULL; void *data = NULL; Eina_List *subdir = ecore_file_ls(search_paths[i].c_str()); Eina_List *l = NULL; EINA_LIST_FOREACH(subdir, l, data) { fname = (char *)data; string p; p = search_paths[i]; if (fname) { p += "/"; p += fname; } if (p[p.length() - 1] != '/') p += "/"; if (!ecore_file_is_dir(p.c_str())) continue; p += "module.so"; if (!ecore_file_exists(p.c_str())) continue; bool alreadyin = false; for (uint im = 0; im < modules.size() && !alreadyin; im++) { ModuleDef mdef = modules[im]; if (p == mdef.mod_fname) alreadyin = true; } if (alreadyin) continue; //try to load the module void *handle = dlopen(p.c_str(), RTLD_LAZY); if (handle) { //object can be loaded, check version and CalaosModuleApi *api = (CalaosModuleApi *)dlsym(handle, "calaos_modapi"); if (!api) { dlclose(handle); cErrorDom("module") << "ModuleManager: module " << p << ". calaos_modapi export not found: " << dlerror(); continue; } if (api->api_version != CALAOS_MODULE_API_VERSION) { dlclose(handle); cErrorDom("module") << "ModuleManager: module " << p << ". The API version doesn't match"; continue; } string module_name; vector<string> tok; Utils::split(p, tok, "/"); if (tok.size() > 2) module_name = tok[tok.size() - 2]; string themepath = Prefix::Instance().dataDirectoryGet(); themepath += "/widgets/" + module_name; ModuleDef mdef; mdef.mod_name = gettext(api->name); mdef.mod_desc = gettext(api->desc); mdef.mod_version = api->version; mdef.mod_author = api->author; mdef.mod_icon = themepath + "/icon.edj"; mdef.mod_fname = p; mdef.handle = handle; mdef.inst = NULL; mdef.api = api; cInfoDom("module") << "ModuleManager: found module: " << mdef.mod_name; modules.push_back(mdef); } else { cWarningDom("module") << "ModuleManager: file " << p << " : failed to dlopen: " << dlerror(); } } EINA_LIST_FREE(subdir, data) free(data); }
WebInputTemp::~WebInputTemp() { WebCtrl::Instance(get_params()).Del(get_param("path")); cInfoDom("input") << "WebInputTemp::~WebInputTemp()"; }
WebOutputLight::~WebOutputLight() { cInfoDom("output") << "WebOutputLight::~WebOutputLight()"; }
OutputString::~OutputString() { cInfoDom("output"); }