forked from kb1isz/linDmrMaster
-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduler.c
141 lines (128 loc) · 5.23 KB
/
scheduler.c
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
/*
* Linux DMR Master server
Copyright (C) 2014 Wim Hofman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "master_server.h"
void sendAprsBeacon();
sqlite3 *openDatabase();
void closeDatabase();
void updateRepeaterTable();
void *scheduler(){
time_t timeNow,beaconTime=0;
int i, id;
char SQLQUERY[200];
sqlite3 *dbase;
sqlite3_stmt *stmt;
bool sending = false;
syslog(LOG_NOTICE,"Scheduler thread started");
for(;;){
sleep(120);
time(&timeNow);
//Send APRS beacons
if (difftime(timeNow,beaconTime) > 1800){
for(i=0;i<highestRepeater;i++){
if (repeaterList[i].id != 0 && repeaterList[i].dmrOnline){
sleep(1);
sendAprsBeacon(repeaterList[i].callsign,repeaterList[i].aprsPass,repeaterList[i].geoLocation,repeaterList[i].aprsPHG,repeaterList[i].aprsBeacon);
}
}
time(&beaconTime);
syslog(LOG_NOTICE,"Send aprs beacons for connected repeaters");
}
time(&timeNow);
//Cleanup registration database
dbase = openDatabase();
sprintf(SQLQUERY,"DELETE FROM rrs WHERE %lu-unixTime > 1900",time(NULL));
if (sqlite3_exec(dbase,SQLQUERY,0,0,0) != 0){
syslog(LOG_NOTICE,"Failed to cleanup RRS database: %s",sqlite3_errmsg(dbase));
}
//Delete old traffic data
sprintf(SQLQUERY,"DELETE FROM traffic WHERE %lu-timeStamp > 86400",time(NULL));
if (sqlite3_exec(dbase,SQLQUERY,0,0,0) != 0){
syslog(LOG_NOTICE,"Failed to cleanup traffic database: %s",sqlite3_errmsg(dbase));
}
//Delete old voiceTraffic data
sprintf(SQLQUERY,"DELETE FROM voiceTraffic WHERE %lu-timeStamp > 86400",time(NULL));
if (sqlite3_exec(dbase,SQLQUERY,0,0,0) != 0){
syslog(LOG_NOTICE,"Failed to cleanup voiceTraffic database: %s",sqlite3_errmsg(dbase));
}
id = 0;
sprintf(SQLQUERY,"SELECT repeaterId,callsign,txFreq,shift,hardware,firmware,mode,language,geoLocation,aprsPass,aprsBeacon,aprsPHG,autoReflector FROM repeaters WHERE upDated = 1");
if (sqlite3_prepare_v2(dbase,SQLQUERY,-1,&stmt,0) == 0){
if (sqlite3_step(stmt) == SQLITE_ROW){
id = sqlite3_column_int(stmt,0);
for(i=0;i<highestRepeater;i++){
if(repeaterList[i].id == id && repeaterList[i].dmrOnline){
sprintf(repeaterList[i].callsign,"%s",sqlite3_column_text(stmt,1));
sprintf(repeaterList[i].txFreq,"%s",sqlite3_column_text(stmt,2));
sprintf(repeaterList[i].shift,"%s",sqlite3_column_text(stmt,3));
sprintf(repeaterList[i].hardware,"%s",sqlite3_column_text(stmt,4));
sprintf(repeaterList[i].firmware,"%s",sqlite3_column_text(stmt,5));
sprintf(repeaterList[i].mode,"%s",sqlite3_column_text(stmt,6));
sprintf(repeaterList[i].language,"%s",sqlite3_column_text(stmt,7));
sprintf(repeaterList[i].geoLocation,"%s",sqlite3_column_text(stmt,8));
sprintf(repeaterList[i].aprsPass,"%s",sqlite3_column_text(stmt,9));
sprintf(repeaterList[i].aprsBeacon,"%s",sqlite3_column_text(stmt,10));
sprintf(repeaterList[i].aprsPHG,"%s",sqlite3_column_text(stmt,11));
repeaterList[i].autoReflector = sqlite3_column_int(stmt,12);
syslog(LOG_NOTICE,"Repeater data changed from web %s %s %s %s %s %s %s %s %s %s %s %i on pos %i",repeaterList[i].callsign,repeaterList[i].hardware
,repeaterList[i].firmware,repeaterList[i].mode,repeaterList[i].txFreq,repeaterList[i].shift,repeaterList[i].language
,repeaterList[i].geoLocation,repeaterList[i].aprsPass,repeaterList[i].aprsBeacon,repeaterList[i].aprsPHG,repeaterList[i].autoReflector,i);
repeaterList[i].conference[2] = repeaterList[i].autoReflector;
if (repeaterList[i].pearRepeater[2] != 0){
repeaterList[i].pearRepeater[2] = 0;
repeaterList[repeaterList[i].pearPos[2]].pearRepeater[2] = 0;
}
}
}
}
sqlite3_finalize(stmt);
}
if (id !=0){
sprintf(SQLQUERY,"UPDATE repeaters SET upDated = 0");
if (sqlite3_exec(dbase,SQLQUERY,0,0,0) != 0){
syslog(LOG_NOTICE,"Failed to reset 'updated' in repeater table: %s",sqlite3_errmsg(dbase));
}
}
closeDatabase(dbase);
//cleanup dmr not idle
if (dmrState[1] != IDLE){
for(i=0;i<highestRepeater;i++){
if (repeaterList[i].sending[1]){
sending = true;
break;
}
}
if (sMaster.sending[1]) sending = true;
if (!sending){
dmrState[1] = IDLE;
syslog(LOG_NOTICE,"DMR state inconsistent on slot 1, settign IDLE");
}
}
sending = false;
if (dmrState[2] != IDLE){
for(i=0;i<highestRepeater;i++){
if (repeaterList[i].sending[2]){
sending = true;
break;
}
}
if (sMaster.sending[2]) sending = true;
if (!sending){
dmrState[2] = IDLE;
syslog(LOG_NOTICE,"DMR state inconsistent on slot 2, settign IDLE");
}
}
}
}