This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfarmosnws.admin.inc
182 lines (154 loc) · 5.86 KB
/
farmosnws.admin.inc
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
<?php
/**
* Module configuration form
*
* @return Returns loaded form array object
*/
function farmosnws_admin_form() {
// check the variables that support this module
farmosnws_check_farmos_measurements();
farmosnws_check_cron_time();
$form = array();
$form['farmosnws_weather_feeds_dir'] = array(
'#type' => 'textfield',
'#title' => t('Feeds Directory'),
'#required' => TRUE,
'#description' => t('The full directory path to where weather feeds should be saved to. Do not use <i>public:///</i> or <i>private://</i> schema.'),
'#default_value' => variable_get('farmosnws_weather_feeds_dir'),
);
$form['farmosnws_locations'] = array(
'#type' => 'textfield',
'#title' => t('Locations'),
'#description' => t('Enter the locations that you would like weather data pulled for. Each location must be entered must be separated by commas. Feeds can be found on the ' . l('NWS XML feeds page', 'http://w1.weather.gov/xml/current_obs/', array('attributes' => array('target' => '_blank'))) . '.'),
'#required' => TRUE,
'#default_value' => variable_get('farmosnws_locations', 'KMGM'),
);
$form['farmosnws_delete_xml'] = array(
'#type' => 'select',
'#title' => t('Delete Processed XML'),
'#required' => TRUE,
'#options' => array(
'yes' => 'Yes',
'no' => 'No',
),
'#description' => t('Delete the National Weather Service XML feed after it has been processed.'),
'#default_value' => variable_get('farmosnws_delete_xml', 'yes'),
);
$form['farmosnws_save_as_user'] = array(
'#type' => 'select',
'#title' => t('Save Data as FarmOS NWS'),
'#required' => TRUE,
'#options' => array(
variable_get('farmosnws_data_uid', 0) => 'Yes',
0 => 'No',
),
'#description' => t('Save the data loaded as the FarmOS NWS user. This will help distigush between data that is manully entered and automatically entered.'),
'#default_value' => variable_get('farmosnws_save_as_user', 'yes'),
);
$form['farmosnws_feed_threshold'] = array(
'#type' => 'select',
'#title' => t('Feed Pull Frequency'),
'#required' => TRUE,
'#options' => array(
'3600' => '1 Hour',
'7200' => '2 Hours',
'10800' => '3 Hours',
'21600' => '6 Hours',
'43200' => '12 Hours',
),
'#description' => t('Select the frequency to pull the latest weather data. This depends on cron being properly configured.'),
'#default_value' => variable_get('farmosnws_feed_threshold', 3600),
);
$form['farmosnws_dataset'] = array(
'#type' => 'fieldset',
'#title' => t('Data to Save'),
'#description' => t('Select the data elements that you wish to save'),
);
$form['farmosnws_dataset']['farmosnws_log_temperature'] = array(
'#type' => 'checkbox',
'#title' => t('Temperature'),
'#default_value' => variable_get('farmosnws_log_temperature'),
);
$form['farmosnws_dataset']['farmosnws_log_pressure'] = array(
'#type' => 'checkbox',
'#title' => t('Barametric Pressure'),
'#default_value' => variable_get('farmosnws_log_pressure'),
);
$form['farmosnws_dataset']['farmosnws_log_windspeed'] = array(
'#type' => 'checkbox',
'#title' => t('Wind Speed'),
'#default_value' => variable_get('farmosnws_log_windspeed'),
);
$form['farmosnws_dataset']['farmosnws_log_winddirection'] = array(
'#type' => 'checkbox',
'#title' => t('Wind Direction'),
'#default_value' => variable_get('farmosnws_log_winddirection'),
);
$form['farmosnws_dataset']['farmosnws_log_visibility'] = array(
'#type' => 'checkbox',
'#title' => t('Visibility'),
'#default_value' => variable_get('farmosnws_log_visibility'),
);
$form['farmosnws_dataset']['farmosnws_log_weather'] = array(
'#type' => 'checkbox',
'#title' => t('Weather Description'),
'#default_value' => variable_get('farmosnws_log_weather'),
);
$form['farmosnws_dataset']['farmosnws_log_dewpoint'] = array(
'#type' => 'checkbox',
'#title' => t('Dew Point'),
'#default_value' => variable_get('farmosnws_log_dewpoint'),
);
$form['farmosnws_dataset']['farmosnws_log_humidity'] = array(
'#type' => 'checkbox',
'#title' => t('Relative Humidity'),
'#default_value' => variable_get('farmosnws_log_humidity'),
);
return system_settings_form($form);
}
/**
* Validate the module settings form
*/
function farmosnws_admin_form_validate($form, &$form_state) {
// validate that the work directory exists and can be created
$weatherfeedsdir = $form_state['values']['farmosnws_weather_feeds_dir'];
$direxist = farmosnws_create_feed_dir($weatherfeedsdir);
if ($direxist == FALSE) {
form_set_error('farmosnws_weather_feeds_dir', 'The weather feed directory cannot be created. Please verify that Drupal as write permissions and try again.');
}
} // end if
/**
* Implements hook_form()
*
* The admin form for pulling the latest data feed. This will bypass the
* cron restriction for pulling the latest data.
*
* @return Returns a built form object.
*/
function farmosnws_admin_force_feed_form() {
$form = array();
$form['markup'] = array(
'#markup' => t('<p>Force pulling data within the same hour can result in duplication of data. Are you sure you want to continue?</p>'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Pull Feed'),
);
$form['cancel'] = array(
'#markup' => l(t('Cancel'), 'admin/config/farm/farmosnws'),
);
return $form;
}
/**
* Runs the feed pull when the form is submitted. Form does not have any fields,
* so there isn't any data to save to the database.
*
* @param array $form A form object.
* @param array $form_state Form data that was submitted.
*/
function farmosnws_admin_force_feed_form_submit($form, &$form_state) {
// pull the feed
farmosnws_get_xml();
// display message
farmosnws_display_status("Feed was forcefully pulled. Run cron to load feed.", TRUE);
}