• 10 dec 2017: forum version update. In case of issues use this topic.
  • 30 nov 2017: pilight moved servers. In case of issues use this topic.
Hello There, Guest! Login Register


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Done] pilight-cron (implemented with eventing)
#11
Nice work dzjaivnt!!!

I can use this for my shutters in the summer Wink I have them open en let them close before the light in the morning gets though Wink
I also have another idea about the wakelight:

It would be nice if one can set the wakeup time on the phone webgui before going to sleep. (if someone has a dimmer is even nicerWink So if I go to sleep I set my wakeuptime and the light will go on when I need to get out of bed Wink

Again, great work!
 
Reply
#12
I use a wakeup light implementation on a daily basis (with a dimmer) Big Grin
 
Reply
#13
Wink 
You probably get up every morning at the same time Wink
 
Reply
#14
Not exactly...
 
Reply
#15
dzjaivnt,

Does this script still works in version 2.1 , and where do I have to put the pilight-lib file.
 
Reply
#16
I did put the file on /usr/local/bin/

It works with version 2.1, i use this every day Smile
 
Reply
#17
dzjaivnt , maybe you can post your 'addon' to the main forum ? People will find it easier Wink

Add-ons-and-Programs
 
Reply
#18
dzjaivnt

How can I see the value of the variable "sunset"
 
Reply
#19
sunset option is an addon for pilight-cron Wink

I'll post that later.

Is it possible to move this topic to addons?
 
Reply
#20
Hi guys,

first of all great work dzjaivnt.

I've tried translating your php-script into c.

This is what i came up with:

cron.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <time.h>

#include "json.h"

#define PORT 5000
#define BUFSIZE 1024

#define CRONFIG_FILE "/etc/pilight/cronfig.json"

typedef enum
{
    WELCOME,
    IDENTIFY,
    REJECT,
    SEND
} steps_t;

JsonNode *getCronfig(void)
{
    FILE *fp;
    char *content;
    size_t bytes;
    JsonNode *cronfig = json_mkobject();
    struct stat st;

    fp=fopen(CRONFIG_FILE, "r");

    fstat(fileno(fp), &st);
    bytes = (size_t)st.st_size;

    fread(content, sizeof(char), bytes, fp);

    cronfig = json_decode(content);
    fclose(fp);

    return cronfig;
}

int main(int arc, char* argv)
{
    struct sockaddr_in serverAddress;
    int serverSocket;
    JsonNode *json = json_mkobject();
    JsonNode *code = json_mkobject();
    JsonNode *cronfig = json_mkobject();
    char *sendBuff, *recvBuff, *message;
    sendBuff = malloc(BUFSIZE);
    recvBuff = malloc(BUFSIZE);
    steps_t step = WELCOME;

    char systemTime[6];
    //read cronfig-json
    cronfig = getCronfig();
    char *tmp = json_stringify(cronfig, NULL);
    printf("%s\n\n", tmp);

    //create socket
    serverSocket = socket(AF_INET, SOCK_STREAM,0);
    if(serverSocket == -1)
    {
        perror("socket() ");
    }

    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(PORT);
    serverAddress.sin_addr.s_addr = INADDR_ANY;

    if(connect(serverSocket, (struct sockaddr *) &serverAddress, sizeof(struct sockaddr))== -1)
    {
        perror("connect() ");
    }

    while(1)
    {
        if(step == IDENTIFY)
        {
            if(recv(serverSocket, recvBuff, BUFSIZE, 0) == -1)
            {
                perror("recv() ");
                goto close;
            }
            json = json_decode(recvBuff);
            json_find_string(json, "message", &message);
            usleep(100);
        }

        switch(step)
        {
        case WELCOME:
            send(serverSocket, "{\"message\":\"client gui\"}", BUFSIZE, 0);
            step=IDENTIFY;
            break;
        case IDENTIFY:
            printf("%s\n", message);
            if(strcmp(message, "accept client") == 0)
            {
                step=SEND;
            }
            if(strcmp(message, "reject client") == 0)
            {
                step=REJECT;
            }
            break;
        case SEND: ;
            JsonNode *cronfigChild = json_mkobject();
            cronfigChild = json_first_child(cronfig);
            char *location, *device, *state, *timeToSwitch;
            location = malloc(999);
            device = malloc(999);
            state = malloc(999);
            timeToSwitch = malloc(999);

            //read systemtime
            time_t t;
            struct tm *ts;
            time(&t);
            ts = localtime(&t);
            strftime(systemTime, 6, "%H:%M", ts);
            printf("%s\n", systemTime);

            while(cronfigChild != NULL)
            {
                json_find_string(cronfigChild, "time", &timeToSwitch);
                if(strcmp(systemTime, timeToSwitch)==0)
                {
                    tmp = json_stringify(cronfigChild, NULL);
                    printf("%s\n", tmp);
                    //Timer einlesen
                    json_find_string(cronfigChild, "location", &location);
                    json_find_string(cronfigChild, "devices", &device);
                    json_find_string(cronfigChild, "state", &state);

                    //Code erzeugen
                    json_delete(code);
                    code = json_mkobject();
                    json_append_member(code, "location", json_mkstring(location));
                    json_append_member(code, "device", json_mkstring(device));
                    json_append_member(code, "state", json_mkstring(state));

                    //Json erzeugen
                    json_delete(json);
                    json = json_mkobject();
                    json_append_member(json, "message", json_mkstring("send"));
                    json_append_member(json, "code", code);

                    //Senden
                    sendBuff = json_stringify(json, NULL);
                    printf("%s\n", sendBuff);
                    send(serverSocket, sendBuff, BUFSIZE, 0);

                }
                //naechsten Timer auswaehlen
                cronfigChild = cronfigChild->next;
            }
            goto close;
            break;
        case REJECT:
            break;
        default:
            goto close;
            break;
        }
    }

close:
    close(serverSocket);
    if(json)
    {
        json_delete(json);
    }
    if(code)
    {
        json_delete(code);
    }

return EXIT_SUCCESS;
}

I used the json lib provided by curlymo. By now it scans cronfig.json in /etc/pilight/ for entries and switches lights by the information given. Days, multiple locations per entry and multiple devices per entry are not yet supported, but perhaps this code is something to build on.

It only works with 2.1 as i didnt figure out how to use ssdp yet.

Please tell me what you think of it. Suggestions are welcome
 
Reply
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  [Done] pilight config read access for all gneandr 1 2,031 03-25-2015, 01:46 PM
Last Post: curlymo
  [Done] Eventing - operators MOD and DIV Niek 54 18,783 02-19-2015, 12:01 AM
Last Post: Niek
  [Done] pilight eventing - dimming step by step terrar 3 2,807 02-11-2015, 02:39 PM
Last Post: Niek
  [Done] pilight-daemon -V - currently requires root access clach04 1 2,485 10-25-2014, 11:39 PM
Last Post: wo_rasp
  [Done] pilight api: receiving and sending as gui client sweetpi 3 3,577 05-19-2014, 04:39 PM
Last Post: curlymo
  [Done] Continuous pilight-debug Tommybear1979 1 1,927 05-15-2014, 09:12 PM
Last Post: curlymo
  [Done] Eventlist (implemented with eventing) JurnD 18 7,248 01-16-2014, 08:39 PM
Last Post: curlymo
  [Done] auto detect pilight (for mobile/desktop apps) (SSDP) Martin 6 4,384 11-30-2013, 06:51 PM
Last Post: curlymo
  [Done] Message from pilight d.m.raspberry 5 2,858 09-23-2013, 10:25 AM
Last Post: curlymo

Forum Jump:


Browsing: 1 Guest(s)