Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
Merge branch 'release-2.0' of https://github.com/bitsecondal/farmosnws
Browse files Browse the repository at this point in the history
  • Loading branch information
almostengr committed Jan 15, 2018
2 parents 1489b2d + 8e48ac2 commit 18a4a04
Show file tree
Hide file tree
Showing 12 changed files with 613 additions and 169 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ xmlrpc.php
.project*
.settings*

# ignore mkdocs files
site/*
103 changes: 34 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,71 @@ Automated importing of National Weather Service (NWS) data into FarmOS.
## Purpose
The purpose of this project is to allow FarmOS to use data from the National
Weather Service to allow it to make better decisions as well as to log
data from a trusted weather source.
data from a trusted weather source.

Bit Second noticed that some small farmers and gardeners do not have the
financial resources to deploy many sensors for their FarmOS installation.
However, by combining the data provide from the local NWS weather station and
using additional Drupal modules, FarmOS can be better automated with a lower
investment.
Using data from the National Weather Service reduces the cost to get a FarmOS
installation going as temperature, rain, humidity and other sensors do not
have to be purchased. Furthermore, it reduces the maintenance costs by
reducing the amount of infrastructure required to maintain the farm or garden.

## System Requirements
* Drupal 7
* FarmOS
* Ubuntu Linux (May run on other Linux versions, but has only be tested with Ubuntu Linux 14.04)
* PHP
* Crontab or other task scheduler capable of running shell scripts

## Setup
## Installation

### Clone the Repository
To get the latest release, clone this repository by running the command below
```shell
git clone https://github.com/bitsecondal/farmosnws.git
```

### Create Configuration File
Make a copy of the get_weather_config_ex.sh file. Update the variables located in the configuration
script file.

```shell
get_weather.sh genconfig
```

The *feedsdir* location will need to be updated to the file system location that
Drupal is checking to see if data files are available.

The *location* variable will need to be updated to the location code for the location
that you want data for.
* Go to the [XML data feeds](http://w1.weather.gov/xml/current_obs/) page on the NWS website.
* Select the state and click Find
* Find the location that is cloest to the area that you want to pull data for.
* In the parenthesis next to the location, you will see a 4-character code. Copy this code.
* Place the 4-character code in the configuration file inside quotes.
* Save the configuration file.

If you have multiple locations that you want to pull data for, you'll need to create a configuration
file for each location and set up a scheduled task for each configuration file.

### Schedule Script
To set up a cron job to run to automatically pull the latest weather data, you use the details and
steps below. The National Weather Service updates the data hourly.

#### Linux/Ubuntu with Crontab
Update the paths mentioned to the location of your script and the location of your configuration
file respectively. If you want to log the output of the script, also update the log location.
```shell
24 * * * * (/path/to/drupal/sites/all/modules/farmosnws/get_weather.sh /path/to/drupal/sites/all/modules/farmosnws/config.sh) >> /var/log/get_weather.log 2>&1
```
If you don't want to log the output of the command, then use the command below.
```shell
24 * * * * (/path/to/drupal/sites/all/modules/farmosnws/get_weather.sh /path/to/drupal/sites/all/modules/farmosnws/config.sh) >> /dev/null 2>&1
```

#### Windows with Task Scheduler
This script is not designed to work on Windows.

## Rules
Rules for automating some tasks, such as cutting grass based on frequency are included in this module.
To import the rules, do the following:
1) Install the Rules and Rules UI modules.
2) Copy and paste the text of the rule that you wish to import. The text for each of the rules are located in the rules_import directory of this module.
3) Go to Adminstration > Configuration > Workflow > Rules > Import Rule.
4) Paste the text of the rule that you copied into the box and then click Import.
### Configure FarmOS NWS
Within your Drupal site, enable the FarmOS NWS and FarmOS NWS Feeds Importer modules
from the Modules page.
After enabling the modules, go to the FarmOS NWS configuration page (Admin >
Configuration > Web Services > FarmOS NWS Settings). Enter the file system location,
weather locations that you would like weather imported for, measurement units, and
whether you want to keep files after they have been loaded. When done, click Save.

### Configure Feed Importer
Within your Drupal site, go to the feed importer page (Structure > Feeds Importers >
Log: NWS Observation). In the Fetcher section, set the upload directory to the same
directory that you defined in the Configure FarmOS NWS section. When done,
click Save.

## Uninstallation
Within your Drupal site, go to the Modules page and disable FarmOS NWS and FarmOS NWS
Importer modules and click Save Configuration. Then go to the Uninstall page (Modules >
Uninstall), select each module, and click Uninstall.

If you uninstall the modules, the data that was imported by the modules WILL NOT be
removed. If you wish to remove the data that was imported remove, you will have to
manually remove each entry.

## Code Updates
To get the latest version of this code, pull the latest release from the
[FarmOS NWS GitHub Page](https://github.com/bitsecondal/farmosnws).

Alternatively you can get the latest code by going to the directory that contains the code and running the commands below:
Alternatively you can get the latest code by going to the directory that contains
the code and running the commands below:
```shell
git checkout master
git pull origin master
```

## Author
Kenny Robinson, [Bit Second Tech](www.bitsecondtech.com)
Kenny Robinson, [@almostengr](https://twitter.com/almostengr)

## Bug Reports and Feature Enhancements
## Additional Information
Bugs and enhancements will be tracked using the Issue tracker
on the [project repo](https://github.com/bitsecondal/farmosnws/issues).

If you need to report a bug, please open a new issue on this repo. Include
as much detail as possible including error messages or screenshots so that the issue can be replicated.

## License
Project is available under the MIT License. See LICENSE for more information.

## Additional Information
For more information about Bit Second Tech, please visit the [Bit Second Tech](http://www.bitsecondtech.com) website.

For more information about FarmOS, please visit the [FarmOS](http://www.farmos.org) website.

For more information about the National Weather Service (NWS), please visit the [NWS](http://www.weather.gov) website.
For more information about the National Weather Service (NWS), please visit the
[NWS](http://www.weather.gov) website.


68 changes: 68 additions & 0 deletions farmosnws.admin.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
*
* Module configuration form
*
* @return Returns loaded form array object
*/
function farmosnws_admin_form() {
$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.'),
'#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/') . '.'),
'#required' => TRUE,
'#default_value' => variable_get('farmosnws_locations'),
);

$form['farmosnws_temp_units'] = array(
'#type' => 'select',
'#title' => t('Measurement Units'),
'#required' => TRUE,
'#options' => array(
'us' => t('Fahrenheit'),
'metric' => t('Celsius'),
),
'#description' => t('Select the system of measurement that you would like to use.'),
'#default_value' => variable_get('farmosnws_temp_units'),
);

$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'),
);

return system_settings_form($form);
}

/**
*
* Validate the module settings
*/
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

13 changes: 13 additions & 0 deletions farmosnws.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = FarmOS NWS
description = Imports data from the National Weather Service into Drupal for use by FarmOS.
core = 7.x
package = farmOS
version = 7.x-2.0
dependencies[] = farm
dependencies[] = farm_quantity
dependencies[] = farm_log_observation

files[] = farmosnws.module
files[] = farmosnws.admin.inc

configure = admin/config/services/farmosnws
66 changes: 66 additions & 0 deletions farmosnws.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
*
* implements hook_install()
*/
function farmosnws_install() {

// add the quantity types

// get the vocabulary id
$vocab_qty_units = taxonomy_vocabulary_machine_name_load('farm_quantity_units');

// list of terms to add
$terms_qty_list = array('Fahrenheit', 'Celsius', 'Percent Humdity', 'Direction', 'Inches', 'MPH', 'Miles', 'Knots', 'KM', 'Degrees');

// check if the term exists, if it does not then add it
foreach($terms_qty_list as $term_qty_item) {
$taxterm = taxonomy_get_term_by_name($term_qty_item);

if (count($taxterm) == 0) {
$tax_qty_term = new stdClass();
$tax_qty_term->name = $term_qty_item;
$tax_qty_term->vid = $vocab_qty_units->vid;
taxonomy_term_save($tax_qty_term);

watchdog('farmosnws', 'Created ' . $term_qty_item . ' taxonomy term.', array(), WATCHDOG_INFO, NULL);
}
else {
watchdog('farmosnws', 'Matched taxonomy term ' . $term_qty_item, array(), WATCHDOG_INFO, NULL);
}
}

// set default variable values
variable_set('farmosnws_temp_units', 'us');
variable_set('farmosnws_delete_xml', 'yes');
}

/**
*
* implements hook_enable()
*/
function farmosnws_enable() {
// notice to visit configuration page to set initial values for variables
drupal_set_message(t("Visit the module " . l("configuration page", 'admin/config/services/farmosnws') . " to complete module setup."), 'warning', FALSE);
watchdog('farmosnws', 'Visit the module configuration page to complete module setup', array(), WATCHDOG_INFO, NULL);
}

/**
*
* implements hook_uninstall()
*/
function farmosnws_uninstall() {
// delete all the variables created by the module
$query = "select name from {variable} where name like :varname";
$result = db_query($query, array(':varname' => db_like('farmosnws') . '%'));

foreach($result as $row) {
variable_del($row->name);
} // end foreach

menu_rebuild();

watchdog('farmosnws', 'FarmOS NWS module has been uninstalled', array(), WATCHDOG_INFO, NULL);
} // end function

Loading

0 comments on commit 18a4a04

Please sign in to comment.