Git Product home page Git Product logo

fsg's Introduction

FSG API

  • Clone this repo to your machine
  • Copy flask/constants_example.json to flask/constants.json
  • Modify flask/constants.json to your values of account_phone, account_sid, auth_token, and tgt_phone (the phone you want to set text messages to when there is an alert).
    • Do not change any values within the pg section of constants.json
  • Move into the root directory of the cloned repository
  • Run docker-compose build --no-cache to build the images
  • Run docker-compose up to start the cluster
    • This will spawn two machines:
      • Flask (the endpoint / API)
      • Postgres (the database which the Flask API uses for storage)
  • Obtain the IP address of the Flask API by typing docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' fsg_web_1
    • For me, the value was 172.19.0.3, yours may differ
    • Note that IP addresses tend to persist upon subsequent startup/shutdown sequences
  • The endpoints exists on port 80 of the flask server and has the following routes:
Path Methods MIME Type Description
/ POST JSON Input data. Raw sensor data is posted here.
/setrule POST JSON Where rules (temp limits, units, and logic) are set on a per-sensor basis.
/getrule GET JSON Retrieves rule details on a sensor.

A sensor must have rules set before it can have data posted to it. To set rules, call the /setrule endpoint with the following JSON:

Parameter Type Range Required? Default Description
id text 1-255 y n/a sensor GUID
unit int 0-1 n 0 temp units (0=Celsius, 1=Fahrenheit)
logic int 0-2 y n/a alert logic (0 = alert if below templ, 1 = alert if above temph, 2 = alert if below templ OR above temph)
templ float 8B see desc n/a required if logic != 1
temph float 8B see desc n/a required if logic != 0

Note: The below example assumes a flask api IP address of 172.19.0.3:

>>> import requests
>>> res = requests.post('http://172.19.0.3:80/setrule', json={"id":"sensor12345", "unit":1, "logic":0, "temph": 32})

If the call is successful, you should show the following response code and body:

>>> res.status_code
200
>>> res.content
b'{"id": "sensor12345", "unit": 1, "logic": 0, "templ": 32.0, "temph": ""}'

Assuming you have already set a rule for a sensor using the /setrule route, you may query the database for existing rules. These rules may not be updated once they have been created.

Parameter Type Range Required? Default Description
id text 1-255 y n/a sensor GUID you wish to retrieve

Note: The below example assumes a flask api IP address of 172.19.0.3:

>>> import requests
>>> res = requests.get('http://172.19.0.3:80/getrule', json={"id": "sensor12345"})

If the call is successful, you should show the following response code and body:

>>> res.status_code
200
>>> res.content
b'{"logic": 0, "unit": "farenheit", "templ": "32.0", "temph": "None"}

Assuming you have already set a rule for a sensor using the /setrule route, you may push sensor updates to that previously configured sensor id through the updates route, hosted at /.

Parameter Type Range Required? Default Description
id text 1-255 y n/a sensor GUID you wish to post updates to
value float 8B y n/a temperature of sensor
unit int 0-1 n 0 temp units (0=Celsius, 1=Fahrenheit)

Temperature conversions between rules and input data are conducted automatically. In other words, a sensor can be created with rules in Fahrenheit, and updated at the / route with units of Celsius. This does not affect the operation of the utility. Note that the default response unit from the api is in Celsius for simplicity.

Note: The below example assumes a flask api IP address of 172.19.0.3:

>>> import requests
>>> res = requests.post('http://172.19.0.3:80', json={"id":"sensor12345", "value": 19, "unit": 1})

Note that in the docker daemon logs, you should see debug output similar to the following:

web_1  | compare temps are:  {'logic': 0, 'unit': 1, 'templ': Decimal('32.0'), 'temph': None, 's_value': 19.0, 's_unit': 1, 'id': 'sensor12345'} 0 32.0 None 19.0 1
web_1  | logic0, temp under lower lim
web_1  | SM232XXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The last line is the identifier of the text message sent to the mobile phone. This could be logged in the database with a few code changes if needed. It could also be caught with a logger on the daemon.

Text messages will be presented to a mobile device similar to the following:

To stop the service, run docker-compose stop && docker-compose rm -f from the root directory of the cloned repository.

  • Unit tests
  • Integration tests
  • Host dependendency installation scripts
  • Making my code suck less in general

fsg's People

Contributors

ggodreau avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.