-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopro_plan_queue.cpp
159 lines (124 loc) · 3.15 KB
/
gopro_plan_queue.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "gopro_plan_queue.h"
#include <string.h>
#include <stdlib.h>
#ifdef SLSERVER_LINUX
#include <unistd.h>
#include <errno.h>
#endif
#ifdef SLSERVER_WIN32
#include <windows.h>
#endif
#include "global.h"
#include "slserver.h"
#include <sys/time.h>
GoproPlanQueue::GoproPlanQueue() {
_running = false;
_gopro4 = new gopro4(GOPRO4_TRANSFER_PORT);
}
GoproPlanQueue::~GoproPlanQueue() {
stop();
if (_gopro4)
delete _gopro4;
}
bool GoproPlanQueue::start() {
if(_running)
return false;
_running = true;
if (_gopro4->init() == false)
{
LOGOUT("***ERROR*** gopro init error\n");
return false;
}
if (0 != pthread_create(&_thread, NULL, GoproPlanQueue::_run_queue, (void*)this)) {
printf("error when create pthread,%d\n", errno);
return false;
}
pthread_mutex_init(&_lock,NULL);
return true;
}
void GoproPlanQueue::stop() {
if(!_running)
return;
_running = false;
pthread_join(_thread,NULL);
pthread_mutex_destroy(&_lock);
}
int GoproPlanQueue::addPlan(const char* name) {
_plan* _p = new _plan;
memset(_p,0,sizeof(_plan));
if(name)
strcpy_s(_p->name,name);
pthread_mutex_lock(&_lock);
_queue.push_back(_p);
pthread_mutex_unlock(&_lock);
return _queue.getSize();
}
bool GoproPlanQueue::getRunningState() {
return _running;
}
void* GoproPlanQueue::_run_queue(void* user) {
struct timeval _now;
long _last = 0;
GoproPlanQueue* pointer = (GoproPlanQueue*)user;
char _queue_buf[1024];
while(pointer->getRunningState()) {
if(!pointer->_queue.empty()) {
list<_plan*>::node* p = pointer->_queue.begin();
_plan* plan = p->element;
pthread_mutex_lock(&pointer->_lock);
pointer->_queue.erase(p);
pthread_mutex_unlock(&pointer->_lock);
const char* cp = plan->name;
if (strcmp(cp, "gopro_power_on") == 0) {
pointer->_gopro4->gopro_wol(GOPRO4_IP, GOPRO4_WOL);
}else if (strcmp(cp, "start") == 0) {
pointer->_gopro4->start();
LOGOUT("***INFO*** gopro start\n");
} else if (strcmp(cp, "stop") == 0) {
LOGOUT("***INFO*** gopro stop\n");
pointer->_gopro4->stop();
} else if (strcmp(cp, "restart") == 0) {
LOGOUT("***INFO*** gopro reset\n");
pointer->_gopro4->stop();
pointer->_gopro4->start();
}
#ifdef SLSERVER_LINUX
else if(strcmp(cp,"wifi_scan") == 0) {
rapidjson::StringBuffer strBuf;
pointer->_gopro4->wifi_scan_results(strBuf);
slglobal.server->broadcast(9,strBuf.GetString());
} else if(strncmp(cp,"connect_wifi(",13) == 0) {
cp += 13;
char _ssid[64],*_pssid = _ssid;
while(*cp && *cp != ')')
*_pssid++ = *cp++;
*_pssid = 0;
pointer->_gopro4->connectWifi(_ssid,NULL);
} else if(strcmp(cp,"disconnect_wifi") == 0 ) {
LOGOUT("***WARNING*** we do not use disconnect_wifi\n");
} else if(strcmp(cp,"wifi_status") == 0) {
pointer->_gopro4->broadcastWifiStatus();
}
#endif
else if (pointer->_gopro4->test_is_work())
{
pointer->_gopro4->runCommand(cp);
}
delete plan;
}
#if 0
else {
gettimeofday(&_now,NULL);
if(_last < _now.tv_sec) {
_last = _now.tv_sec;
}
}
#endif
#ifdef SLSERVER_WIN32
Sleep(1);
#else
usleep(1);
#endif
}
return NULL;
}