summaryrefslogtreecommitdiffstats
path: root/rx.c
blob: 7f39d6d636e30376d35622b4efaf53e076b78191 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>

#include "sia.h"
#include "email.h"
#include "mqtt.h"
#include "util.h"


typedef struct thing_struct {
  struct thing_struct *next;
  char *msg;
  unsigned seq;
} Thing;

static Thing *thing_list = NULL;

void dump_state (const char *file)
{
  Thing *thing;
  FILE *f;
  char fn[1024];

  strcpy (fn, file);
  strcat (fn, ".next");

  f = fopen (fn, "w");

  if (!f) return;

  for (thing = thing_list; thing; thing = thing->next) {
    fprintf (f, "%d ", thing->seq);
    fputs (thing->msg, f);
    fputc ('\n', f);
  }

  fclose (f);

  rename (fn, file);
}


static void maintain_state (const char *msg, const char *state_file)
{
  Thing *thing;


  switch (*msg) {
  case '+':
  case '-':
    break;

  default:
    return;
  }


  for (thing = thing_list; thing; thing = thing->next) {

    if (!strcmp (&msg[1], &thing->msg[1])) {
      strcpy (thing->msg, msg);
      dump_state (state_file);
      return;
    }
  }

  thing = malloc (sizeof (Thing));
  thing->next = thing_list;
  thing->msg = strdup (msg);
  thing->seq = 1;

  thing_list = thing;

  dump_state (state_file);
}



static void send_mqtt (const char *msg, const char *host)
{
  char value[1024];
  char topic[1024];
  char prefix[] = "stat/alarm/";
  const char *rptr;
  char *wptr;
  int retain = 0;


  if (!host) return;


  switch (*msg) {
  case '+':
  case '-':
    retain = 1;
    break;
  }



  for (rptr = msg, wptr = value; (*rptr) && ((*rptr) != ' '); rptr++, wptr++)
    *wptr = *rptr;

  if (!*rptr) return;

  *wptr = 0;

  while (*rptr == ' ') rptr++;

  strcpy (topic, prefix);

  wptr = topic + (sizeof (prefix) - 1);

  if (!*rptr)
    strcpy (wptr, value + 1);

  else {
    for (; *rptr; rptr++, wptr++) {
      if (local_isalnum (*rptr))
        *wptr = *rptr;

      else
        *wptr = '_';
    }

    *wptr = 0;
  }

  mqtt (host, topic, value, retain);

#if 0
  /* JMM and an ugly hack */

  if (!strcmp (topic, "stat/alarm/Rain_Sensor"))  {
    if (value[0] == '+')
      mqtt (host, "cmnd/vent/POWER1", "0", 0);

    else if (value[0] == '-')
      mqtt (host, "cmnd/vent/POWER1", "1", 0);
  }

#endif
}

static void msg (char *account, char *event, char *ascii, int log, const char *email, const char *state_file, const char *mqtt_host)
{
  char body[256];
  char subject[256];


  if (state_file)
    maintain_state (ascii, state_file);

  if (strstr (ascii, "FULL SET"))
    send_mqtt ("+ARMED SYSTEM", mqtt_host);

  else if (strstr (ascii, "UNSET"))
    send_mqtt ("-ARMED SYSTEM", mqtt_host);

  else
    send_mqtt (ascii, mqtt_host);

  snprintf (body, sizeof (body) - 1, "%s %64s %s", account, event, ascii);
  body[sizeof (body) - 1] = 0;

  if (log)
    syslog (LOG_WARNING, "SIA: %s", body);



  if (!email) return;

  if (strstr (body, "HEARTBT")) return;

  if (strstr (body, "Valid")) return;

  if (strstr (body, "Rejct")) return;

  if (strstr (body, "CUSTOM")) return;

  if (strstr (body, "LOG")) return;


  snprintf (subject, sizeof (subject) - 1, "Galaxy SIA: %s", ascii);

  send_email (email, subject, body);
}


int new_block (int fd, SIA_Block *b, int log, const char *email, const char *state_file, const char *mqtt_host)
{
  static int have_ascii_messages = 0; /*SIA level 3 doesn't have ascii, SIA level 4 does */

  static char account[SIA_MAX_DATA_LENGTH + 1];
  static char event[SIA_MAX_DATA_LENGTH + 1];
  static char ascii[SIA_MAX_DATA_LENGTH + 1];

  unsigned len = sia_data_length (b);

  if (sia_ack_if_needed (fd, b))
    return -1;

  switch (b->function) {
  case SIA_FN_ACCOUNT_ID:
    memcpy (account, b->data, len);
    account[len] = 0;
    break;

  case SIA_FN_NEW_EVENT:
  case SIA_FN_OLD_EVENT:
    memcpy (event, b->data, len);
    event[len] = 0;

    if (!have_ascii_messages) msg (account, event, "", log, email, state_file, mqtt_host);

    break;

  case SIA_FN_ASCII:
    have_ascii_messages = 1;
    memcpy (ascii, b->data, len);
    ascii[len] = 0;

    msg (account, event, ascii, log, email, state_file, mqtt_host);
    break;
  }

  return 0;
}

void periodic_task (void)
{
}