Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <unistd.h> |
| 7 | #include <argp.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <sys/mman.h> |
| 10 | |
| 11 | #include "interfaces/sensor2.h" |
| 12 | #include "sensor_threshold.h" |
| 13 | |
| 14 | |
| 15 | gboolean |
| 16 | get_threshold_state (SensorIntegerThreshold *sen, |
| 17 | GDBusMethodInvocation *invocation, |
| 18 | gpointer user_data) |
| 19 | { |
| 20 | guint state = sensor_integer_threshold_get_state(sen); |
| 21 | sensor_integer_threshold_complete_get_state(sen,invocation,state); |
| 22 | return TRUE; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | gboolean |
| 27 | set_thresholds (SensorIntegerThreshold *sen, |
| 28 | GDBusMethodInvocation *invocation, |
| 29 | guint lc, |
| 30 | guint lw, |
| 31 | guint uw, |
| 32 | guint uc, |
| 33 | gpointer user_data) |
| 34 | { |
| 35 | sensor_integer_threshold_set_lower_critical(sen,lc); |
| 36 | sensor_integer_threshold_set_lower_warning(sen,lw); |
| 37 | sensor_integer_threshold_set_upper_warning(sen,uw); |
| 38 | sensor_integer_threshold_set_upper_critical(sen,uc); |
| 39 | sensor_integer_threshold_complete_set(sen,invocation); |
| 40 | sensor_integer_threshold_set_state(sen,NORMAL); |
| 41 | return TRUE; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void check_thresholds(SensorIntegerThreshold* sensor,guint value) |
| 46 | { |
| 47 | threshold_states current_state = sensor_integer_threshold_get_state(sensor); |
| 48 | if (current_state != NOT_SET) |
| 49 | { |
| 50 | threshold_states state = NORMAL; |
| 51 | |
| 52 | if (value < sensor_integer_threshold_get_lower_critical(sensor)) { |
| 53 | state = LOWER_CRITICAL; |
| 54 | } |
| 55 | else if(value < sensor_integer_threshold_get_lower_warning(sensor)) { |
| 56 | state = LOWER_WARNING; |
| 57 | } |
| 58 | else if(value > sensor_integer_threshold_get_upper_critical(sensor)) { |
| 59 | state = UPPER_CRITICAL; |
| 60 | } |
| 61 | else if(value > sensor_integer_threshold_get_upper_warning(sensor)) { |
| 62 | state = UPPER_WARNING; |
| 63 | } |
| 64 | // only emit signal if threshold state changes |
| 65 | if (state != sensor_integer_threshold_get_state(sensor)) |
| 66 | { |
| 67 | sensor_integer_threshold_set_state(sensor,state); |
| 68 | if (state == LOWER_CRITICAL || state == UPPER_CRITICAL) |
| 69 | { |
| 70 | sensor_integer_threshold_emit_critical(sensor); |
| 71 | g_print("Critical\n"); |
| 72 | } |
| 73 | else if (state == LOWER_WARNING || state == UPPER_WARNING) |
| 74 | { |
| 75 | sensor_integer_threshold_emit_warning(sensor); |
| 76 | g_print("Warning\n"); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |