예제 #1
0
  import send_alerts
"""
import boto3
from boto3.dynamodb.conditions import Key
from datetime import datetime, timedelta

import sys_log
import send_alerts

STATUS_TABLE = 'ADynamoDBTable1'
CONTROL_TABLE = 'ADynamoDBTable2'
CONTROL_ITEM_SELECT = '1'
SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:123456789012:My-Alerts'

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('moisture_guard', 'DynamoDBTableForInfo',
                     'DynamoDBTableForIssues', '', '')


def _moisture_guard_is_on(setting):
    """
  The value of the "moisture_guard" attribute in the Control database
  can be "on", "off", or an epoch time.  "on" / "off" are self-
  explanatory.  An epoch time stamp is used to designate that
  the battery guard functionality should be considered off until the
  future point in time designated by the epoch time stamp. If an
  expired epoch time stamp is discovered, or an invalid one, then
  the "moisture_guard" attribute will be set to "on"
   
  Returns
    True   if the "moisture_guard" attribute is set to "on" or the 
           epoch time stamp represents a date time in the past or
예제 #2
0
  import sys_log
"""
import boto3
import json

import sys_log

ALL_JOBS = [
    'absence_guard', 'archive_check', 'archive_data', 'battery_guard',
    'delete_data', 'freeze_guard', 'moisture_guard', 'pull_data',
    'update_status'
]
GUARDS = ['absence_guard', 'battery_guard', 'freeze_guard', 'moisture_guard']

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('invoke_job', 'ADynDBTableForInfo',
                     'ADynDBTableForIssues', '', '')


def invoke_job(event, context):
    """
  Invoked ad hoc by single page web application via API Gateway.
  
  Args (supplied by AWS Lambda service)
    event: information about who/what invoked the Lambda function
    context: information about the Lambda function's runtime environment
  """
    sl.reset()

    if (event):
        job = event['job'].lower()
        if (job in GUARDS):
예제 #3
0
  import send_alerts
"""
import boto3
from boto3.dynamodb.conditions import Key
from datetime import datetime, timedelta

import sys_log
import send_alerts

STATUS_TABLE = 'ADynamoDBTable1'
CONTROL_TABLE = 'ADynamoDBTable2'
CONTROL_ITEM_SELECT = '1'
SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:123456789012:My-Alerts'

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('absence_guard', 'DynDBTableForInfo',
                     'DynDBTableForIssues', '', '')


def _absence_guard_is_on(setting):
    """
  The value of the "absence_guard" attribute in the Control database
  can be "on", "off", or an epoch time.  "on" / "off" are self-
  explanatory.  An epoch time stamp is used to designate that
  the battery guard functionality should be considered "off" until the
  future point in time designated by the epoch time stamp. If an
  expired epoch time stamp is discovered, or an invalid one, then
  the "absence_guard" attribute will be set to "on"
  
  Note: 2038 epoch time roll over issue
   
  Returns
예제 #4
0
  import send_alerts
"""
import boto3
from boto3.dynamodb.conditions import Key
from datetime import datetime, timedelta

import sys_log
import send_alerts

STATUS_TABLE = 'ADynamoDBTable1'
CONTROL_TABLE = 'ADynamoDBTable2'
CONTROL_ITEM_SELECT = '1'
SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:123456789012:MyAlerts'

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('freeze_guard', 'ADynDBTableForInfo',
                     'ADynDBTableForIssues', '', '')


def _freeze_guard_is_on(setting):
    """
  The value of the "freeze_guard" attribute in the Control database
  can be "on", "off", or an epoch time.  "on" / "off" are self-
  explanatory.  An epoch time stamp is used to designate that
  the battery guard functionality should be considered off until the
  future point in time designated by the epoch time stamp. If an
  expired epoch time stamp is discovered, or an invalid one, then
  the "freeze_guard" attribute will be set to "on"
   
  Returns
    True   if the "freeze_guard" attribute is set to "on" or the 
           epoch time stamp represents a date time in the past or
예제 #5
0
  import send_alerts
"""
import boto3
from boto3.dynamodb.conditions import Key
from datetime import datetime, timedelta

import sys_log
import send_alerts

STATUS_TABLE = 'ADynamoDBTable1'
CONTROL_TABLE = 'ADynamoDBTable2'
CONTROL_ITEM_SELECT = '1'
SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:123456789012:MyAlerts'

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('battery_guard', 'DynDBTableForInfo',
                     'DynDBTableForIssues', '', '')


def _battery_guard_is_on(setting):
    """
  The value of the "battery_guard" attribute in the Control database
  can be "on", "off", or an epoch time.  "on" / "off" are self-
  explanatory.  An epoch time stamp is used to designate that
  the battery guard functionality should be considered off until the
  future point in time designated by the epoch time stamp. If an
  expired epoch time stamp is discovered, or an invalid one, then
  the "battery_guard" attribute will be set to "on"
   
  Returns
    True   if the "battery_guard" attribute is set to "on" or the 
           epoch time stamp represents a date time in the past or
예제 #6
0
  import sys_log
"""
import boto3
from boto3.dynamodb.conditions import Key

import sys_log

STATUS_TABLE = 'DynTableForStatus'
LOCATIONS = [
    'A02N', 'A08M', 'A02S', 'B02N', 'B08M', 'B02S', 'C02N', 'C08M', 'C02S',
    'D02N', 'D08M', 'D02S', 'E02N', 'E08M', 'E02S', 'F02N', 'F08M', 'F02S',
    'G02N', 'G08M', 'G02S'
]

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('update_status', 'DynTableForInfo', 'DynTableForIssues',
                     '', '')


def update_status(event, context):
    """
  The DynamoDB table dedicated for maintaining the last reported values
  for vineyard sensor stations, DynTableForStatus, contains 21 Items;
  one for each of the 21 stations.  There is a dedicated IoT Core
  shadow document for each of the 21 stations.  At any given point in
  time DynTableForStatus will contain the most recent sensor data
  readings across all 21 stations.
  
  IoT Core to DynamoDB service-to-service integration only supports 
  the insertion of new data (i.e., new Items).  That service
  integration is used for the DynamoDB table VinStationsData.  However,
  this module is required in order to support the updating of an 
예제 #7
0
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key
from dateutil.relativedelta import *
from datetime import datetime

import sys_log

ARCHIVAL_BUCKET_NAME = 'my_bucket'
CONTROL_TABLE = 'ADynamoDBTable1'
STATIONS_PER_ARCHIVE_TABLE = 'ADynamoDBTable2'
CONTROL_ITEM_SELECT = '2'
MONTHS_BACK_LIMIT = 12  #how months back to go back
OLDEST_ARCHIVE = '2020-11'

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('archive_check', 'DynamoDBTableForInfo',
                     'DynamoDBTableForIssues', '', '')


def _archive_file_is_small(file, size, threshold):
    """
  Retrieve the number of sensor stations that contributed sensor data
  to the archive file.  Based on this number and the threshold, which
  represents the minimum amount of data that a single sensor station 
  should contribute per month, determine if the archive file appears
  unusually small.
  
  Args:
    file (str)      the archive file
    size (int)      the size of the archive file in bytes
    threshold(int)  the minimum amount of bytes that a single sensor 
                    station will generate in a month
예제 #8
0
  import sys_log
"""
import boto3
from   boto3.dynamodb.conditions import Key
from   botocore.exceptions       import ClientError
from   dateutil.relativedelta    import * 
from   datetime                  import datetime

import sys_log

ARCHIVAL_BUCKET_NAME           = 'my-bucket'
RAW_DATA_TABLE                 = 'DynTableForSensorData'       
MONTHS_BACK_LIMIT              = 12 #how months back to go back

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('delete_data','DynTableForInfo',
                                   'DynTableForIssues','','')
 
 
def _archive_file_exists(date):
  """
  Determine if the arhival file for all of the raw sensor data for a
  'year-mo' exists in the archival bucket.
  """
  exists = False
  archive_file = date + '-VineyardStations.csv'
    
  try:
    s3_access_client = boto3.client('s3')
    s3_access_client.head_bucket(Bucket=ARCHIVAL_BUCKET_NAME)
    try:
      s3_access_resource = boto3.resource('s3')
예제 #9
0
                             'handler' : 'F08M'},
                   'G02N' : {'private_key' : 'a-key-private.pem.key',
                             'cert_file' : 'a-cert--certificate.pem.crt',
                             'root_ca' : 'Security/Amazon_root_CA_1.pem',
                             'handler' : 'G02N'},
                   'G02S' : {'private_key' : 'a-key-private.pem.key',
                             'cert_file' : 'a-cert--certificate.pem.crt',
                             'root_ca' : 'Security/Amazon_root_CA_1.pem',
                             'handler' : 'G02S'},
                   'G08M' : {'private_key' : 'a-key-private.pem.key',
                             'cert_file' : 'a-cert--certificate.pem.crt',
                             'root_ca' : 'Security/Amazon_root_CA_1.pem',
                             'handler' : 'G08M'}}
                             
#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('pull_data','DynTableForInfo',
                                 'DynTableForIssues','','')


def _recent_activity_check():
  """
  Determine if any sensor data has flowed from the vineyard through 
  www.thingspeak.com to AWS within the past NO_UPDATE_THRESHOLD  
  amount of time.  If not, this is a strong indication that vineyard 
  infrastructure is down or the ISP is down or that www.thingspeak.com
  is down. If detected, this condition constitues an alarm state.
  """
  try:
    dynamo_db_access = boto3.resource('dynamodb')
    table = dynamo_db_access.Table(CONTROL_TABLE)
    try:
      response = table.query(
예제 #10
0
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError
from dateutil.relativedelta import *
from io import StringIO, BytesIO
from datetime import datetime, timedelta

import sys_log

RAW_DATA_TABLE = 'DynTableForStationData'
STATIONS_PER_ARCHIVE_TABLE = 'DynTableForArchive'
MONTHS_BACK_LIMIT = 12  #how months back to go back
OLDEST_ARCHIVE = '2020-11'
ARCHIVAL_BUCKET_NAME = 'my_bucket'

#global object provides system logging to DynamoDB tables
sl = sys_log.sys_log('archive_data', 'DynTableForInfo', 'DynTableForIssues',
                     '', '')


def _store_num_stations_in_archive(file_name, count):
    """
  Record the number of sensor stations that reported raw sensor data
  in a given year-month time period.
  """
    results = True
    try:
        dynamo_db_access = boto3.resource('dynamodb')
        table = dynamo_db_access.Table(STATIONS_PER_ARCHIVE_TABLE)
        try:
            response = table.put_item(Item={
                'date': file_name[:7],
                'count': str(count)