xuxiuxi
2017-07-23 94e704acde59df60bd42f6f90b94d462d1bfa37c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "PipeLine.h"
#include "PL_RTSPServer.h"
#include "PL_RaspiVid.h"
 
#include "logger.h"
#include <iostream>
 
#include <signal.h>
#include <time.h>
#include <unistd.h>
 
#include <cmdhub/client.h>
 
#define CLOCKID CLOCK_REALTIME
 
Logger g_logger(std::cout);
 
time_t g_last_pipe_finished = 0;
 
my_id_t get_my_id()
{
    return 123;
}
 
std::string cmdhub_send_cmd(const std::string client_cmd)
{
    CmdhubClientSession session;
    session.recv_retry = 5;
    if (!cmdhub_client_init(session))
    {
        LOG_ERROR << "cam_ir_timer cmdhub_client_init error" << LOG_ENDL;
        return "";
    }
    if (!cmdhub_client_connect(session))
    {
        LOG_ERROR << "cam_ir_timer cmdhub_client_connect error" << LOG_ENDL;
        return "";
    }
 
    std::string result;
    int exit_code;
    if (cmdhub_client_command(session, client_cmd, result, exit_code))
    {
        LOG_INFO << "exit_code:" << exit_code << ", result:" << result.c_str();
        
        cmdhub_client_destory(session);
        return result;
    }
 
    cmdhub_client_destory(session);
    return "";
}
 
static void cam_ir_timer(union sigval v)
{
    LOGP(DEBUG, "checking IR %d", v.sival_int);
 
    if (time(nullptr) - g_last_pipe_finished < 5)
    {
        // camera should be closed
        cmdhub_send_cmd("exec/camirlighting camera_enable");
    }
    else
    {
        cmdhub_send_cmd("exec/camirlighting camera_disable");
    }
}
 
void start_cam_ir_timer()
{
    timer_t timerid;
    struct sigevent evp;
    memset(&evp, 0, sizeof(struct sigevent));
 
    evp.sigev_value.sival_int = 123;
    evp.sigev_notify = SIGEV_THREAD;
    evp.sigev_notify_function = cam_ir_timer;
 
    if (timer_create(CLOCKID, &evp, &timerid) == -1)
    {
        LOG_ERROR << "fail to timer_create" << LOG_ENDL;
        exit(EXIT_FAILURE);
    }
 
    struct itimerspec it;
    it.it_interval.tv_sec = 5;
    it.it_interval.tv_nsec = 0;
    it.it_value.tv_sec = 5;
    it.it_value.tv_nsec = 0;
 
    if (timer_settime(timerid, 0, &it, NULL) == -1)
    {
        LOG_ERROR << "fail to timer_settime" << LOG_ENDL;
        exit(EXIT_FAILURE);
    }
}
 
int main(int argc, char** argv)
{
    g_logger.set_level(INFO);
 
    PipeLine pipeLine;
    
    PipeLine::register_global_elem_creator("PL_RaspiVid", create_PL_RaspiVid);
    PipeLine::register_global_elem_creator("PL_RTSPServer", create_PL_RTSPServer);
 
    {
        PL_RaspiVid* raspiVid = (PL_RaspiVid*)pipeLine.push_elem("PL_RaspiVid");
        PL_RaspiVid_Config config;
        config.vflip = true;
        //config.profile = "baseline";
        //config.level = "4";
        bool ret = raspiVid->init(&config);
        if (!ret)
        {
            LOG_ERROR << "raspiVid.init error" << std::endl;
            exit(EXIT_FAILURE);
        }
    }
 
    {
        PL_RTSPServer* rtspServer = (PL_RTSPServer*)pipeLine.push_elem("PL_RTSPServer");
        bool ret = rtspServer->init(nullptr);
        if (!ret)
        {
            LOG_ERROR << "rtspServer.init error" << std::endl;
            exit(EXIT_FAILURE);
        }
    }
    
    cmdhub_send_cmd("exec/camirlighting bh1750_value");
    cmdhub_send_cmd("exec/camirlighting cam_ir_threshold 5");
        
    start_cam_ir_timer();
    
    while(true)
    {
        //LOG_ERROR << "begin pipe" << std::endl;
        pipeLine.pipe();
        //LOG_ERROR << "end pipe" << std::endl;
        
        g_last_pipe_finished = time(nullptr);
    }
}