Import sprinklerd

This commit is contained in:
2016-03-01 22:58:18 +01:00
parent 5564ad3b6e
commit 0baec13e8e
11 changed files with 612 additions and 5 deletions

View File

@@ -3,12 +3,12 @@
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
cmake_minimum_required (VERSION 2.8.11)
#add_subdirectory (wolfssl/builders/cmake)
add_subdirectory (wolfssl/builders/cmake)
add_subdirectory (json/builders/cmake)
add_subdirectory (jsoncpp/builders/cmake)
add_subdirectory (libubox/builders/cmake)
add_subdirectory (ubus/libubus/builders/cmake)
add_subdirectory (ubus/ubus/builders/cmake)
add_subdirectory (ubus/ubusd/builders/cmake)
#add_subdirectory (civetweb/builders/cmake)
add_subdirectory (civetweb/builders/cmake)
#add_subdirectory (wiringPi/)

View File

@@ -10,8 +10,8 @@ set(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/build/lib")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-modules/")
add_subdirectory (3P)
#add_subdirectory (lib)
#add_subdirectory (src)
add_subdirectory (lib)
add_subdirectory (src)
add_custom_target (deploy

View File

@@ -1,5 +1,6 @@
# Acces aux scripts personnels et code metier.
PATH=$PATH:/usr/local/bin
LD_LIBRARY_PATH=/opt/Domo/lib
# Prompt indiquant nom d'hote et repertoire courant.
PS1='\h[\W]\$ '

View File

@@ -6,7 +6,7 @@ cmake_minimum_required (VERSION 2.8.11)
#add_subdirectory (alarmd/builders/cmake)
add_subdirectory (domod/builders/cmake)
#add_subdirectory (chacond/builders/cmake)
#add_subdirectory (sprinklersd/builders/cmake)
add_subdirectory (sprinklersd/builders/cmake)
#add_subdirectory (rf_listenerd/builders/cmake)
#add_subdirectory (restd/builders/cmake)
#add_subdirectory (ui/builders/cmake)

11
src/sprinklersd/Readme.md Normal file
View File

@@ -0,0 +1,11 @@
exemple de commande du opensprinker PI
https://github.com/rayshobby/opensprinkler
Bonne appli on peut s'en inspirer
https://opensprinkler.com/forums/topic/sprinklers_pi-an-alternative-sprinkler-control-program/
https://github.com/rszimm/sprinklers_pi/

View File

@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 2.8.11)
project(sprinklersd)
include (libubus)
include (libubox)
include (libubuscpp)
include (libjsoncpp)
include_directories(../../src)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra -pedantic -Werror=strict-aliasing")
file(
GLOB_RECURSE
source_files
../../src/main.cpp
../../src/UbusSprinklerModel.cpp
../../src/sprinklers.cpp
)
add_executable (
sprinklersd
${source_files}
)
target_link_libraries (sprinklersd
LINK_PUBLIC
jsoncpp
ubuscpp
rt
)

View File

@@ -0,0 +1,172 @@
/*!
* UBusModel.c
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 30/04/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <fstream>
#include <string>
#include <sstream>
#include <jsoncpp/json.h>
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include "sprinklers.h"
#include "UbusSprinklerModel.h"
namespace {
using namespace UBus;
static ObjectType gSprinklerModelUbus_types(
"sprinklers",
Method("get", UBUS_CPP(SprinkerModel, Get)),
Method("set", UBUS_CPP(SprinkerModel, Set))
);
}
/*! ----------------------------------------------------------------------------
* @fn SprinkerModel
*
* @brief Constructor of the UBus Mixer Volume.
*/
SprinkerModel::SprinkerModel (void) :
UBusObject (gSprinklerModelUbus_types, "sprinkers")
{
}
/*! ----------------------------------------------------------------------------
* @fn ~SprinkerModel
*
* @brief Destructor of the UBus Mixer Volume.
*/
SprinkerModel::~SprinkerModel (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Get
*
* @brief Get the State of a Sprinkler Station.
*/
int SprinkerModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult = 0;
struct blob_buf theBuf = {0};
char *theString = blobmsg_format_json (aMsg, true);
Json::Reader theReader;
Json::StyledWriter theWriter;
Json::Value theRoot;
Json::Value theOutput;
uint32_t theStation = 0;
bool theState = false;
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theStation = theRoot["station"].asInt();
if (theStation == 0) {
fprintf (stderr, "Station ID is missing.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theResult = sprinklers_get_station (theStation, theState);
if (theResult != 0) {
fprintf (stderr, "Wrong Station ID (%d).\n", theStation);
return UBUS_STATUS_INVALID_ARGUMENT;
}
printf ("aState: %d\n", theState);
theOutput ["station"] = theStation;
theOutput ["state"] = theState;
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, theWriter.write (theOutput).c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
return theResult;
}
/*! ----------------------------------------------------------------------------
* @fn Set
*
* @brief Set a new State for a Sprinkler Station.
*/
int SprinkerModel::Set (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult;
char *theString = blobmsg_format_json (aMsg, true);
Json::Reader theReader;
Json::Value theRoot;
uint32_t theStation = 0;
bool theState = false;
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theStation = theRoot["station"].asInt();
theState = theRoot["state"].asBool();
if (theStation == 0) {
fprintf (stderr, "Station ID is missing.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theResult = sprinklers_set_station (theStation, theState);
if (theResult != 0) {
fprintf (stderr, "Wrong Station ID (%d).\n", theStation);
return UBUS_STATUS_INVALID_ARGUMENT;
}
return theResult;
}

View File

@@ -0,0 +1,49 @@
/*!
* UBusModel.h
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 30/04/2015
*
*/
#ifndef _UBUS_SPRINKLER_MODEL_H
#define _UBUS_SPRINKLER_MODEL_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <ubuscpp/UBusObject.h>
/*--------------------------------- Define ----------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/
class SprinkerModel : public UBusObject {
public:
SprinkerModel (void);
~SprinkerModel (void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
int Set (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
};
#endif /* _UBUS_SPRINKLER_MODEL_H */

View File

@@ -0,0 +1,120 @@
/*!
* main.c
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 30/04/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <cstdio>
#include <unistd.h>
extern "C" {
#include <libubus.h>
}
#include "UbusSprinklerModel.h"
#include "sprinklers.h"
/*--------------------------------- GLOBALS ---------------------------------*/
/*! ----------------------------------------------------------------------------
* @fn setupUbus
*
* @brief Setup the UBus Mechanism.
*/
extern "C" {
static struct ubus_context *setupUbus (void)
{
ubus_context *theCtx;
if (uloop_init () !=0) {
fprintf (stderr, "Failed to init Uloop.\n");
return NULL;
}
signal (SIGPIPE, SIG_IGN);
theCtx = ubus_connect (NULL);
if (!theCtx) {
fprintf (stderr, "Failed to connect to ubus\n");
return NULL;
}
ubus_add_uloop (theCtx);
return theCtx;
}
} // SetupUbus.
/*! ----------------------------------------------------------------------------
* @fn main
*
* @brief Main function of Domo Daemon.
*/
int main (void)
{
int theRet = 0;
SprinkerModel theSprinklersModel;
struct ubus_context *theCtx = NULL;
if (setuid (0)) {
perror ("setuid");
return 1;
}
/* Setup the Ubus context. */
theCtx = setupUbus ();
if (theCtx == NULL) {
return -1;
}
/* Setup the sprinklers. */
theRet = sprinklers_setup ();
if (theRet != 0) {
fprintf (stderr, "Impossible to Setup the Sprinkers.\n");
return theRet;
}
/* Add the UBus to the model exposed. */
ubus_add_object (theCtx, &theSprinklersModel);
/* Main Event Loop. */
uloop_run ();
ubus_free (theCtx);
uloop_done ();
return theRet;
}

View File

@@ -0,0 +1,180 @@
/*!
* sprinklers.c
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 30/04/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <jsoncpp/json.h>
#include <string>
#include <ubuscpp/UBusCall.h>
#include "sprinklers.h"
/*-------------------------------- GLOBALES ---------------------------------*/
/*! ----------------------------------------------------------------------------
* @fn sprinklers_setup
*
* @brief this function setup the OSPI environement.
*/
int sprinklers_setup (void)
{
UBusCall theCmd;
std::string theResult;
int theIDVal;
fprintf (stderr, "Setup the Gpios used by the Sprinklers.\n");
if (theCmd.Exec ("domo.capabilities.sprinklers", "get", "", theResult) == 0){
// We get the the sprinklers. Get the IDs.
Json::Reader theReader;
Json::Value theRoot;
Json::Value theElement;
if (!theReader.parse (theResult, theRoot)) {
fprintf (stderr, "Failed parse the List of Sprinklers.\n");
return -1;
}
for (const Json::Value& theElement : theRoot["Sprinkers"]) {
theIDVal = theElement["id"].asInt();
sprinkler_setup_station (theIDVal);
}
}
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn sprinkler_setup_station
*
* @brief This function change the State of an Ospi Station.
*/
int sprinkler_setup_station (int aStation)
{
char theCommand[255];
printf (" Setup the Station: %d\n", aStation);
// Activate the Gpio
sprintf (theCommand, "echo %d > /sys/class/gpio/export", aStation);
system (theCommand);
// Select the Direction of the Gpio
sprintf (theCommand, "echo out > /sys/class/gpio/gpio%d/direction", aStation);
system (theCommand);
// Select the Direction of the Gpio
sprintf (theCommand, "echo 1 > /sys/class/gpio/gpio%d/value", aStation);
system (theCommand);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn sprinkler_set_station
*
* @brief This function change the State of an Ospi Station.
*/
int sprinklers_set_station (uint32_t aStation, bool aState)
{
FILE *theFp;
char theGpioPath[255];
sprintf (theGpioPath, "/sys/class/gpio/gpio%d/value", aStation);
theFp = fopen (theGpioPath, "w");
if (theFp == NULL) {
fprintf (stderr, "Impossible to open: <%s>\n", theGpioPath);
return -1;
}
if (aState) {
fwrite ("0", 1, 1, theFp);
}
else {
fwrite ("1", 1, 1, theFp);
}
fclose (theFp);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn sprinkler_get_station
*
* @brief This function change the State of an Ospi Station.
*/
int sprinklers_get_station (uint32_t aStation, bool &aState)
{
FILE *theFp;
char theGpioPath[255];
char theData[5];
fprintf (stdout, "get station state (%d)\n", aStation);
sprintf (theGpioPath, "/sys/class/gpio/gpio%d/value", aStation);
theFp = fopen (theGpioPath, "r");
if (theFp == NULL) {
fprintf (stderr, "Impossible to open: <%s>\n", theGpioPath);
return -1;
}
fread (theData, 1, 1, theFp);
//printf ("theData: %s\n", theData);
if (theData[0] == '0') {
aState = true;
}
else {
aState = false;
}
fclose (theFp);
return 0;
}

View File

@@ -0,0 +1,42 @@
/*!
* sprinklers.h
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 30/04/2015
*
*/
#ifndef __SPRINKLERS_H
#define __SPRINKLERS_H
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdint.h>
/*-------------------------------- METHODS ---------------------------------*/
int sprinklers_setup (void);
int sprinkler_setup_station (int aStation);
int sprinklers_set_station (uint32_t aStation, bool aState);
int sprinklers_get_station (uint32_t aStation, bool &aState);
#endif /* __SPRINKLERS_H */