Compare commits
3 Commits
master
...
NodeJS-Con
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61f90ec04a | ||
|
|
3e37d88d6e | ||
|
|
7f0aff3c9c |
163
daq/taglogger.py
163
daq/taglogger.py
@@ -18,61 +18,59 @@ from pycomm.cip.cip_base import CommError
|
||||
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
||||
|
||||
# DEFAULTS
|
||||
db_address = "web_db"
|
||||
db_url = "https://{}:5000".format(db_address)
|
||||
scan_rate = 30 # seconds
|
||||
save_all = "test" # use True, False, or any string
|
||||
plc_handshake_tags = {}
|
||||
last_handshake_time = 0
|
||||
DB_ADDRESS = "web_db"
|
||||
DB_URL = "https://{}:5000".format(DB_ADDRESS)
|
||||
SCAN_RATE = 30 # seconds
|
||||
SAVE_ALL = "test" # use True, False, or any string
|
||||
PLC_HANDSHAKE_TAGS = {}
|
||||
LAST_HANDSHAKE_TIME = 0
|
||||
|
||||
tag_store = {}
|
||||
tag_list = []
|
||||
handshake_list = []
|
||||
device_types = {}
|
||||
TAG_STORE = {}
|
||||
TAG_LIST = []
|
||||
HANDSHAKE_LIST = []
|
||||
DEVICE_TYPES = {}
|
||||
|
||||
|
||||
def read_from_plc(addr, tag):
|
||||
"""Read a value from a PLC."""
|
||||
addr = str(addr)
|
||||
tag = str(tag)
|
||||
c = ClxDriver()
|
||||
if c.open(addr):
|
||||
clx = ClxDriver()
|
||||
if clx.open(addr):
|
||||
try:
|
||||
v = c.read_tag(tag)
|
||||
return v
|
||||
return clx.read_tag(tag)
|
||||
except Exception:
|
||||
print("ERROR RETRIEVING TAG: {} at {}".format(tag, addr))
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
err = clx.get_status()
|
||||
clx.close()
|
||||
print(err)
|
||||
pass
|
||||
c.close()
|
||||
clx.close()
|
||||
|
||||
|
||||
def write_to_plc(addr, tag, value):
|
||||
"""Write a value to a tag in the PLC at the specified address."""
|
||||
pv = read_from_plc(addr, tag)
|
||||
if pv:
|
||||
c = ClxDriver()
|
||||
if c.open(addr):
|
||||
present_value = read_from_plc(addr, tag)
|
||||
if present_value:
|
||||
clx = ClxDriver()
|
||||
if clx.open(addr):
|
||||
try:
|
||||
v = c.write_tag(tag, value, pv[1])
|
||||
c.close()
|
||||
clx.write_tag(tag, value, present_value[1])
|
||||
clx.close()
|
||||
return True
|
||||
except Exception:
|
||||
print("ERROR WRITING TAG: {} at {}".format(tag, addr))
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
err = clx.get_status()
|
||||
clx.close()
|
||||
print(err)
|
||||
return False
|
||||
c.close()
|
||||
clx.close()
|
||||
return False
|
||||
|
||||
|
||||
def store_tag(tag):
|
||||
"""Store the value of a tag in the web_db."""
|
||||
global db_url
|
||||
url = "{}/api/tag_vals".format(db_url)
|
||||
# global DB_URL
|
||||
url = "{}/api/tag_vals".format(DB_URL)
|
||||
tag_val_obj = {
|
||||
"tag_id": tag['id'],
|
||||
"value": tag['last_stored']
|
||||
@@ -84,10 +82,11 @@ def store_tag(tag):
|
||||
|
||||
def load_data():
|
||||
"""Load configuration data from the web server."""
|
||||
global db_url, scan_rate, save_all, tag_list, handshake_list
|
||||
global DB_URL, SCAN_RATE, SAVE_ALL, TAG_LIST, HANDSHAKE_LIST
|
||||
|
||||
try:
|
||||
# Get tags stored in database
|
||||
url = '{}/api/tags'.format(db_url)
|
||||
url = '{}/api/tags'.format(DB_URL)
|
||||
get_tag_request = requests.get(url, verify=False)
|
||||
tags = json.loads(get_tag_request.text)
|
||||
except Exception as e:
|
||||
@@ -97,59 +96,59 @@ def load_data():
|
||||
|
||||
try:
|
||||
# Get device types stored in database
|
||||
get_device_type_request = requests.get('{}/api/device_types'.format(db_url), verify=False)
|
||||
get_device_type_request = requests.get('{}/api/device_types'.format(DB_URL), verify=False)
|
||||
device_types_json = json.loads(get_device_type_request.text)
|
||||
for t in device_types_json['objects']:
|
||||
device_types[t['id']] = t['device_type']
|
||||
for dev_type in device_types_json['objects']:
|
||||
DEVICE_TYPES[dev_type['id']] = dev_type['device_type']
|
||||
except Exception as e:
|
||||
print("Error getting tags: {}".format(e))
|
||||
time.sleep(10)
|
||||
main()
|
||||
|
||||
try:
|
||||
config_req = requests.get('{}/api/configs'.format(db_url), verify=False)
|
||||
config_req = requests.get('{}/api/configs'.format(DB_URL), verify=False)
|
||||
config_json = json.loads(config_req.text)
|
||||
config_list = config_json['objects']
|
||||
|
||||
if len(config_list) > 0:
|
||||
for c in config_list:
|
||||
if c['parameter'] == "scan_rate":
|
||||
scan_rate = float(c['val'])
|
||||
SCAN_RATE = float(c['val'])
|
||||
elif c['parameter'] == "save_all":
|
||||
save_all = c['val']
|
||||
SAVE_ALL = c['val']
|
||||
except Exception as e:
|
||||
print("Error getting configs: {}".format(e))
|
||||
|
||||
new_tags = [t['name'] for t in tags['objects']]
|
||||
existing_tags = [t['name'] for t in tag_list]
|
||||
existing_handshakes = [h['name'] for h in handshake_list]
|
||||
new_tags = [dev_type['name'] for dev_type in tags['objects']]
|
||||
existing_tags = [dev_type['name'] for dev_type in TAG_LIST]
|
||||
existing_handshakes = [h['name'] for h in HANDSHAKE_LIST]
|
||||
|
||||
tags_to_add = []
|
||||
handshakes_to_add = []
|
||||
tags_to_copy = []
|
||||
handshakes_to_copy = []
|
||||
for t in new_tags:
|
||||
for dev_type in new_tags:
|
||||
this_tag = {}
|
||||
for n_t in tags['objects']:
|
||||
if n_t['name'] == t:
|
||||
if n_t['name'] == dev_type:
|
||||
this_tag["tag"] = n_t['tag']
|
||||
this_tag["id"] = n_t["id"]
|
||||
this_tag["name"] = n_t['name']
|
||||
this_tag["change_threshold"] = n_t['change_threshold']
|
||||
this_tag["guarantee_sec"] = n_t['guarantee_sec']
|
||||
this_tag["ip_address"] = n_t['device']['address']
|
||||
this_tag["device_type"] = device_types[n_t['device']['device_type_id']]
|
||||
this_tag["device_type"] = DEVICE_TYPES[n_t['device']['device_type_id']]
|
||||
this_tag["last_stored"] = 0.0
|
||||
this_tag["last_store_time"] = 0
|
||||
if t in existing_tags:
|
||||
for e_t in tag_list:
|
||||
if e_t['name'] == t:
|
||||
if dev_type in existing_tags:
|
||||
for e_t in TAG_LIST:
|
||||
if e_t['name'] == dev_type:
|
||||
this_tag['last_stored'] = e_t['last_stored']
|
||||
this_tag['last_store_time'] = e_t['last_store_time']
|
||||
tags_to_copy.append(this_tag)
|
||||
elif t in existing_handshakes:
|
||||
for e_h in handshake_list:
|
||||
if e_h['name'] == t:
|
||||
elif dev_type in existing_handshakes:
|
||||
for e_h in HANDSHAKE_LIST:
|
||||
if e_h['name'] == dev_type:
|
||||
this_tag['last_stored'] = e_h['last_stored']
|
||||
this_tag['last_store_time'] = e_h['last_store_time']
|
||||
handshakes_to_copy.append(this_tag)
|
||||
@@ -158,69 +157,69 @@ def load_data():
|
||||
tags_to_add.append(this_tag)
|
||||
elif n_t['tag_class_id'] == 6:
|
||||
handshakes_to_add.append(this_tag)
|
||||
tag_list = tags_to_add + tags_to_copy
|
||||
handshake_list = handshakes_to_add + handshakes_to_copy
|
||||
TAG_LIST = tags_to_add + tags_to_copy
|
||||
HANDSHAKE_LIST = handshakes_to_add + handshakes_to_copy
|
||||
|
||||
|
||||
def main():
|
||||
"""Run the main routine."""
|
||||
global scan_rate, tag_store, device_types, tag_list, handshake_list, save_all
|
||||
global SCAN_RATE, TAG_STORE, DEVICE_TYPES, TAG_LIST, HANDSHAKE_LIST, SAVE_ALL
|
||||
|
||||
while True:
|
||||
load_data()
|
||||
# print(tag_list)
|
||||
if len(tag_list + handshake_list) == 0:
|
||||
if len(TAG_LIST + HANDSHAKE_LIST) == 0:
|
||||
print("No tags configured. Trying again in 10 seconds.")
|
||||
time.sleep(10)
|
||||
main()
|
||||
|
||||
if len(tag_list) > 0:
|
||||
for i in range(0, len(tag_list)):
|
||||
if len(TAG_LIST) > 0:
|
||||
for i in range(0, len(TAG_LIST)):
|
||||
try:
|
||||
|
||||
val = read_from_plc(tag_list[i]['ip_address'], tag_list[i]['tag'])[0]
|
||||
val = read_from_plc(TAG_LIST[i]['ip_address'], TAG_LIST[i]['tag'])[0]
|
||||
now = time.time()
|
||||
|
||||
store_value = abs(val - tag_list[i]['last_stored']) > tag_list[i]['change_threshold']
|
||||
store_time = (now - tag_list[i]['last_store_time']) > tag_list[i]['guarantee_sec']
|
||||
store_value = abs(val - TAG_LIST[i]['last_stored']) > TAG_LIST[i]['change_threshold']
|
||||
store_time = (now - TAG_LIST[i]['last_store_time']) > TAG_LIST[i]['guarantee_sec']
|
||||
|
||||
if store_value or store_time or (save_all == "true"):
|
||||
if store_value or store_time or (SAVE_ALL == "true"):
|
||||
store_reason = ""
|
||||
if store_time:
|
||||
store_reason = "time delta = {} > {}".format(now - tag_list[i]['last_store_time'],
|
||||
tag_list[i]['guarantee_sec'])
|
||||
store_reason = "time delta = {} > {}".format(now - TAG_LIST[i]['last_store_time'],
|
||||
TAG_LIST[i]['guarantee_sec'])
|
||||
elif store_value:
|
||||
store_reason = "value delta = {} > {}".format(abs(val - tag_list[i]['last_stored']),
|
||||
tag_list[i]['change_threshold'])
|
||||
elif save_all == "true":
|
||||
store_reason = "value delta = {} > {}".format(abs(val - TAG_LIST[i]['last_stored']),
|
||||
TAG_LIST[i]['change_threshold'])
|
||||
elif SAVE_ALL == "true":
|
||||
store_reason = "save all parameter"
|
||||
|
||||
tag_list[i]['last_stored'] = val
|
||||
tag_list[i]['last_store_time'] = now
|
||||
store_tag(tag_list[i])
|
||||
TAG_LIST[i]['last_stored'] = val
|
||||
TAG_LIST[i]['last_store_time'] = now
|
||||
store_tag(TAG_LIST[i])
|
||||
|
||||
print("Stored {} for {} at {} due to {}".format(val, tag_list[i]['name'], now, store_reason))
|
||||
print("Stored {} for {} at {} due to {}".format(val, TAG_LIST[i]['name'], now, store_reason))
|
||||
except CommError:
|
||||
print("CommError: Error connecting to {} for {}".format(tag_list[i]['ip_address'],
|
||||
tag_list[i]['name']))
|
||||
print("CommError: Error connecting to {} for {}".format(TAG_LIST[i]['ip_address'],
|
||||
TAG_LIST[i]['name']))
|
||||
except TypeError:
|
||||
print("Error reading {}".format(tag_list[i]['name']))
|
||||
print("Error reading {}".format(TAG_LIST[i]['name']))
|
||||
|
||||
if len(handshake_list) > 0:
|
||||
for h in range(0, len(handshake_list)):
|
||||
if len(HANDSHAKE_LIST) > 0:
|
||||
for h in range(0, len(HANDSHAKE_LIST)):
|
||||
now = time.time()
|
||||
if (now - handshake_list[h]['last_store_time']) > handshake_list[h]['guarantee_sec']:
|
||||
if (now - HANDSHAKE_LIST[h]['last_store_time']) > HANDSHAKE_LIST[h]['guarantee_sec']:
|
||||
try:
|
||||
write_to_plc(str(handshake_list[h]['ip_address']), str(handshake_list[h]['tag']), 1)
|
||||
handshake_list[h]['last_store_time'] = now
|
||||
print("Handshake with {} - {} at {}".format(handshake_list[h]['ip_address'],
|
||||
handshake_list[h]['tag'], now))
|
||||
write_to_plc(str(HANDSHAKE_LIST[h]['ip_address']), str(HANDSHAKE_LIST[h]['tag']), 1)
|
||||
HANDSHAKE_LIST[h]['last_store_time'] = now
|
||||
print("Handshake with {} - {} at {}".format(HANDSHAKE_LIST[h]['ip_address'],
|
||||
HANDSHAKE_LIST[h]['tag'], now))
|
||||
except CommError:
|
||||
print("CommError: Error connecting to {} for {}".format(handshake_list[h]['ip_address'],
|
||||
handshake_list[h]['name']))
|
||||
print("CommError: Error connecting to {} for {}".format(HANDSHAKE_LIST[h]['ip_address'],
|
||||
HANDSHAKE_LIST[h]['name']))
|
||||
except TypeError:
|
||||
print("Error writing {}".format(tag_list[i]['name']))
|
||||
time.sleep(scan_rate)
|
||||
print("Error writing {}".format(TAG_LIST[i]['name']))
|
||||
time.sleep(SCAN_RATE)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
3
web_db/javascript/server/.jshintrc
Normal file
3
web_db/javascript/server/.jshintrc
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"esversion": 6
|
||||
}
|
||||
15
web_db/javascript/server/index.js
Normal file
15
web_db/javascript/server/index.js
Normal file
@@ -0,0 +1,15 @@
|
||||
// Lumberjack -- More than just a logger
|
||||
|
||||
// Imports
|
||||
import express from 'express';
|
||||
import morgan from 'morgan';
|
||||
|
||||
let app = express();
|
||||
app.use(morgan('dev'));
|
||||
|
||||
|
||||
|
||||
app.listen(3000, function () {
|
||||
console.log('Listening on port 3000!');
|
||||
});
|
||||
|
||||
3052
web_db/javascript/server/package-lock.json
generated
Normal file
3052
web_db/javascript/server/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
web_db/javascript/server/package.json
Normal file
30
web_db/javascript/server/package.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "lumberjack",
|
||||
"version": "1.0.0",
|
||||
"description": "PLC Data Logger",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/Henry-Pump/DataLogger-Generic.git"
|
||||
},
|
||||
"author": "Patrick McDonagh",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/Henry-Pump/DataLogger-Generic/issues"
|
||||
},
|
||||
"homepage": "https://github.com/Henry-Pump/DataLogger-Generic#readme",
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.26.0",
|
||||
"babel-preset-env": "^1.6.0",
|
||||
"babel-preset-es2015": "^6.24.1",
|
||||
"mocha": "^3.5.3",
|
||||
"rimraf": "^2.6.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.15.5",
|
||||
"morgan": "^1.8.2"
|
||||
}
|
||||
}
|
||||
3
web_db/web_db/.coveragerc
Normal file
3
web_db/web_db/.coveragerc
Normal file
@@ -0,0 +1,3 @@
|
||||
[run]
|
||||
source = web_db
|
||||
omit = web_db/test*
|
||||
21
web_db/web_db/.gitignore
vendored
Normal file
21
web_db/web_db/.gitignore
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
*.egg
|
||||
*.egg-info
|
||||
*.pyc
|
||||
*$py.class
|
||||
*~
|
||||
.coverage
|
||||
coverage.xml
|
||||
build/
|
||||
dist/
|
||||
.tox/
|
||||
nosetests.xml
|
||||
env*/
|
||||
tmp/
|
||||
Data.fs*
|
||||
*.sublime-project
|
||||
*.sublime-workspace
|
||||
.*.sw?
|
||||
.sw?
|
||||
.DS_Store
|
||||
coverage
|
||||
test
|
||||
4
web_db/web_db/CHANGES.txt
Normal file
4
web_db/web_db/CHANGES.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
0.0
|
||||
---
|
||||
|
||||
- Initial version.
|
||||
2
web_db/web_db/MANIFEST.in
Normal file
2
web_db/web_db/MANIFEST.in
Normal file
@@ -0,0 +1,2 @@
|
||||
include *.txt *.ini *.cfg *.rst
|
||||
recursive-include web_db *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2
|
||||
29
web_db/web_db/README.txt
Normal file
29
web_db/web_db/README.txt
Normal file
@@ -0,0 +1,29 @@
|
||||
Henry Pump DataLogger Database
|
||||
==============================
|
||||
|
||||
Getting Started
|
||||
---------------
|
||||
|
||||
- Change directory into your newly created project.
|
||||
|
||||
cd web_db
|
||||
|
||||
- Create a Python virtual environment.
|
||||
|
||||
python3 -m venv env
|
||||
|
||||
- Upgrade packaging tools.
|
||||
|
||||
env/bin/pip install --upgrade pip setuptools
|
||||
|
||||
- Install the project in editable mode with its testing requirements.
|
||||
|
||||
env/bin/pip install -e ".[testing]"
|
||||
|
||||
- Run your project's tests.
|
||||
|
||||
env/bin/pytest
|
||||
|
||||
- Run your project.
|
||||
|
||||
env/bin/pserve development.ini
|
||||
61
web_db/web_db/development.ini
Normal file
61
web_db/web_db/development.ini
Normal file
@@ -0,0 +1,61 @@
|
||||
###
|
||||
# app configuration
|
||||
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
|
||||
###
|
||||
|
||||
[app:main]
|
||||
use = egg:web_db
|
||||
|
||||
pyramid.reload_templates = true
|
||||
pyramid.debug_authorization = false
|
||||
pyramid.debug_notfound = false
|
||||
pyramid.debug_routematch = false
|
||||
pyramid.default_locale_name = en
|
||||
pyramid.includes =
|
||||
pyramid_debugtoolbar
|
||||
|
||||
# By default, the toolbar only appears for clients from IP addresses
|
||||
# '127.0.0.1' and '::1'.
|
||||
# debugtoolbar.hosts = 127.0.0.1 ::1
|
||||
|
||||
mongo_uri = mongodb://poc_www:HenryPump1903@localhost:27017/poc
|
||||
|
||||
###
|
||||
# wsgi server configuration
|
||||
###
|
||||
|
||||
[server:main]
|
||||
use = egg:waitress#main
|
||||
listen = *:6543
|
||||
|
||||
###
|
||||
# logging configuration
|
||||
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
|
||||
###
|
||||
|
||||
[loggers]
|
||||
keys = root, web_db
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = INFO
|
||||
handlers = console
|
||||
|
||||
[logger_web_db]
|
||||
level = DEBUG
|
||||
handlers =
|
||||
qualname = web_db
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
|
||||
53
web_db/web_db/production.ini
Normal file
53
web_db/web_db/production.ini
Normal file
@@ -0,0 +1,53 @@
|
||||
###
|
||||
# app configuration
|
||||
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
|
||||
###
|
||||
|
||||
[app:main]
|
||||
use = egg:web_db
|
||||
|
||||
pyramid.reload_templates = false
|
||||
pyramid.debug_authorization = false
|
||||
pyramid.debug_notfound = false
|
||||
pyramid.debug_routematch = false
|
||||
pyramid.default_locale_name = en
|
||||
|
||||
###
|
||||
# wsgi server configuration
|
||||
###
|
||||
|
||||
[server:main]
|
||||
use = egg:waitress#main
|
||||
listen = *:6543
|
||||
|
||||
###
|
||||
# logging configuration
|
||||
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
|
||||
###
|
||||
|
||||
[loggers]
|
||||
keys = root, web_db
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
|
||||
[logger_web_db]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = web_db
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
|
||||
3
web_db/web_db/pytest.ini
Normal file
3
web_db/web_db/pytest.ini
Normal file
@@ -0,0 +1,3 @@
|
||||
[pytest]
|
||||
testpaths = web_db
|
||||
python_files = *.py
|
||||
53
web_db/web_db/setup.py
Normal file
53
web_db/web_db/setup.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""Set up the package."""
|
||||
import os
|
||||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
with open(os.path.join(here, 'README.txt')) as f:
|
||||
README = f.read()
|
||||
with open(os.path.join(here, 'CHANGES.txt')) as f:
|
||||
CHANGES = f.read()
|
||||
|
||||
requires = [
|
||||
'pyramid',
|
||||
'pyramid_jinja2',
|
||||
'pyramid_debugtoolbar',
|
||||
'waitress',
|
||||
'pymongo',
|
||||
]
|
||||
|
||||
tests_require = [
|
||||
'WebTest >= 1.3.1', # py3 compat
|
||||
'pytest',
|
||||
'pytest-cov',
|
||||
]
|
||||
|
||||
setup(
|
||||
name='web_db',
|
||||
version='0.0',
|
||||
description='Henry Pump DataLogger Database',
|
||||
long_description=README + '\n\n' + CHANGES,
|
||||
classifiers=[
|
||||
'Programming Language :: Python',
|
||||
'Framework :: Pyramid',
|
||||
'Topic :: Internet :: WWW/HTTP',
|
||||
'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
|
||||
],
|
||||
author='',
|
||||
author_email='',
|
||||
url='',
|
||||
keywords='web pyramid pylons',
|
||||
packages=find_packages(),
|
||||
include_package_data=True,
|
||||
zip_safe=False,
|
||||
extras_require={
|
||||
'testing': tests_require,
|
||||
},
|
||||
install_requires=requires,
|
||||
entry_points={
|
||||
'paste.app_factory': [
|
||||
'main = web_db:main',
|
||||
],
|
||||
},
|
||||
)
|
||||
79
web_db/web_db/web_db/__init__.py
Normal file
79
web_db/web_db/web_db/__init__.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Configure everything at once."""
|
||||
from pyramid.config import Configurator
|
||||
from pymongo import MongoClient
|
||||
from pyramid.renderers import JSON
|
||||
from datetime import datetime, date
|
||||
from bson.objectid import ObjectId
|
||||
|
||||
try:
|
||||
# for python 2
|
||||
from urlparse import urlparse
|
||||
except ImportError:
|
||||
# for python 3
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
# JSON RENDERERS
|
||||
def datetime_adapter(obj, request):
|
||||
"""Adapt the datetime for json."""
|
||||
return obj.strftime("%Y-%m-%d %H:%M:%S.%fZ")
|
||||
|
||||
|
||||
def objectId_adapter(obj, request):
|
||||
"""Adapt the objectId for json."""
|
||||
return str(obj)
|
||||
|
||||
|
||||
def date_adapter(obj, request):
|
||||
"""Adapt the date for json."""
|
||||
return obj.strftime("%Y-%m-%d")
|
||||
|
||||
|
||||
def pagination_adapter(obj, request):
|
||||
"""Adapt the pagination for json."""
|
||||
p = {
|
||||
'page': obj.page,
|
||||
'per_page': obj.per_page,
|
||||
'total_count': obj.total_count,
|
||||
}
|
||||
return p
|
||||
|
||||
|
||||
def main(global_config, **settings):
|
||||
"""Make a Pyramid WSGI application."""
|
||||
config = Configurator(settings=settings)
|
||||
config.include('pyramid_jinja2')
|
||||
|
||||
# Database Configuration
|
||||
db_url = urlparse(settings['mongo_uri'])
|
||||
config.registry.db = MongoClient(
|
||||
host=db_url.hostname,
|
||||
port=db_url.port,
|
||||
)
|
||||
|
||||
def add_db(request):
|
||||
db = config.registry.db[db_url.path[1:]]
|
||||
if db_url.username and db_url.password:
|
||||
db.authenticate(db_url.username, db_url.password)
|
||||
return db
|
||||
|
||||
config.add_request_method(add_db, 'db', reify=True)
|
||||
|
||||
# CUSTOM JSON RENDERER
|
||||
prettyjson = JSON(indent=4)
|
||||
prettyjson.add_adapter(datetime, datetime_adapter)
|
||||
prettyjson.add_adapter(date, date_adapter)
|
||||
prettyjson.add_adapter(ObjectId, objectId_adapter)
|
||||
# prettyjson.add_adapter(Pagination, pagination_adapter)
|
||||
config.add_renderer('prettyjson', prettyjson)
|
||||
|
||||
config.add_static_view('static', 'static', cache_max_age=3600)
|
||||
config.add_route('home', '/')
|
||||
# config.include(devices_include, route_prefix="/devices")
|
||||
config.add_route("devices.collection", "/devices")
|
||||
config.add_route("devices.single", "/devices/{address}")
|
||||
config.add_route("devices.single.tags.collection", "/devices/{address}/tags")
|
||||
config.add_route("devices.single.tags.single", "/devices/{address}/tags/{tag_name}")
|
||||
|
||||
config.scan()
|
||||
return config.make_wsgi_app()
|
||||
98
web_db/web_db/web_db/devices.py
Normal file
98
web_db/web_db/web_db/devices.py
Normal file
@@ -0,0 +1,98 @@
|
||||
"""Handle all device-relation functions."""
|
||||
from pyramid.view import view_config
|
||||
|
||||
|
||||
@view_config(route_name="devices.collection", renderer="prettyjson", request_method="GET")
|
||||
def get_all(request):
|
||||
"""Retrieve all devices."""
|
||||
devices = request.db['data'].aggregate([
|
||||
{"$project": {
|
||||
"address": 1,
|
||||
"device_type": 1,
|
||||
"tag_types": 1
|
||||
}}
|
||||
])
|
||||
return {"devices": list(devices)}
|
||||
|
||||
|
||||
@view_config(route_name="devices.collection", renderer="prettyjson", request_method="POST")
|
||||
def post_one(request):
|
||||
"""Insert one new device."""
|
||||
new_device = {"$set": {"address": request.json_body['address'], "device_type": request.json_body['device_type'], "tag_types": {"handshake": [], "normal": []}}}
|
||||
print(new_device)
|
||||
request.db['data'].update_one({'address': request.json_body['address']}, new_device, upsert=True)
|
||||
devices = request.db['data'].find_one({'address': request.json_body['address']}, {"address": 1, "device_type": 1})
|
||||
return {"device": devices}
|
||||
|
||||
|
||||
@view_config(route_name="devices.single", renderer="prettyjson", request_method="GET")
|
||||
def get_single(request):
|
||||
"""Retrieve a single device by its address."""
|
||||
devices = request.db['data'].find({'address': request.matchdict['address']}, {"address": 1, "device_type": 1})
|
||||
return {"devices": list(devices)}
|
||||
|
||||
|
||||
@view_config(route_name="devices.single", renderer="prettyjson", request_method="DELETE")
|
||||
def delete_single(request):
|
||||
"""Delete a single device by its address."""
|
||||
result = request.db['data'].delete_one({'address': request.matchdict['address']})
|
||||
return {"deleted": result.deleted_count}
|
||||
|
||||
|
||||
@view_config(route_name="devices.single", renderer="prettyjson", request_method="PUT")
|
||||
def update_one(request):
|
||||
"""Update a single device by its address."""
|
||||
upd_device = {"address": request.json_body['address'], "device_type": request.matchdict['address']}
|
||||
request.db['data'].update_one({'address': request.matchdict['address']}, {"$set": upd_device}, upsert=True)
|
||||
devices = request.db['data'].find({'address': request.json_body['address']}, {"address": 1, "device_type": 1})
|
||||
return {"device": list(devices)[0]}
|
||||
|
||||
|
||||
enable_tag = {
|
||||
"tag_types.normal.name": 1,
|
||||
"tag_types.normal.tag": 1,
|
||||
"tag_types.normal.description": 1,
|
||||
"tag_types.normal.data_type": 1,
|
||||
"tag_types.normal.change_threshold": 1,
|
||||
"tag_types.normal.guarantee_sec": 1,
|
||||
"tag_types.normal.units": 1,
|
||||
"tag_types.normal.min_expected": 1,
|
||||
"tag_types.normal.max_expected": 1
|
||||
}
|
||||
|
||||
|
||||
@view_config(route_name="devices.single.tags.collection", renderer="prettyjson", request_method="GET")
|
||||
def get_devicetags(request):
|
||||
"""Retrieve a single device by its address."""
|
||||
devices = request.db['data'].find({'address': request.matchdict['address']}, {"address": 1, "device_type": 1, "tag_types": 1})
|
||||
return {"devices": list(devices)}
|
||||
|
||||
|
||||
@view_config(route_name="devices.single.tags.collection", renderer="prettyjson", request_method="POST")
|
||||
def add_normaltag(request):
|
||||
"""Retrieve a single device by its address."""
|
||||
jsb = request.json_body
|
||||
new_tag = {
|
||||
"name": jsb['name'],
|
||||
"tag": jsb['tag'],
|
||||
"description": jsb['description'],
|
||||
"data_type": jsb['data_type'],
|
||||
"change_threshold": jsb['change_threshold'],
|
||||
"guarantee_sec": jsb['guarantee_sec'],
|
||||
"units": jsb['units'],
|
||||
"min_expected": jsb['min_expected'],
|
||||
"max_expected": jsb['max_expected']
|
||||
}
|
||||
|
||||
request.db['data'].update_one({'address': request.matchdict['address']}, {"$push": {"tag_types.normal": new_tag}})
|
||||
find_tag = request.db['data'].find_one({"$and": [{'address': request.matchdict['address']}, {"tag_types.normal.tag": new_tag['tag']}]}, enable_tag)
|
||||
# find_tag = request.db['data'].find_one({"tag_types.normal.tag": new_tag['tag']}, enable_tag)
|
||||
# find_tag = list(request.db['data'].find({'{}.tag_types.normal.tag'.format(request.matchdict['address']): new_tag['tag']}, enable_tag))
|
||||
return {"tag": find_tag}
|
||||
|
||||
|
||||
@view_config(route_name="devices.single.tags.single", renderer="prettyjson", request_method="GET")
|
||||
def get_devicesingletag(request):
|
||||
"""Retrieve a single tag by its address and tag_name."""
|
||||
find_tag = request.db['data'].find_one({"$and": [{'address': request.matchdict['address']}, {"tag_types.normal.tag": request.matchdict['tag_name']}]}, enable_tag)
|
||||
return {"tag": find_tag}
|
||||
BIN
web_db/web_db/web_db/static/pyramid-16x16.png
Normal file
BIN
web_db/web_db/web_db/static/pyramid-16x16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 KiB |
BIN
web_db/web_db/web_db/static/pyramid.png
Normal file
BIN
web_db/web_db/web_db/static/pyramid.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
154
web_db/web_db/web_db/static/theme.css
Normal file
154
web_db/web_db/web_db/static/theme.css
Normal file
@@ -0,0 +1,154 @@
|
||||
@import url(//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700);
|
||||
body {
|
||||
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||
font-weight: 300;
|
||||
color: #ffffff;
|
||||
background: #bc2131;
|
||||
}
|
||||
h1,
|
||||
h2,
|
||||
h3,
|
||||
h4,
|
||||
h5,
|
||||
h6 {
|
||||
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||
font-weight: 300;
|
||||
}
|
||||
p {
|
||||
font-weight: 300;
|
||||
}
|
||||
.font-normal {
|
||||
font-weight: 400;
|
||||
}
|
||||
.font-semi-bold {
|
||||
font-weight: 600;
|
||||
}
|
||||
.font-bold {
|
||||
font-weight: 700;
|
||||
}
|
||||
.starter-template {
|
||||
margin-top: 250px;
|
||||
}
|
||||
.starter-template .content {
|
||||
margin-left: 10px;
|
||||
}
|
||||
.starter-template .content h1 {
|
||||
margin-top: 10px;
|
||||
font-size: 60px;
|
||||
}
|
||||
.starter-template .content h1 .smaller {
|
||||
font-size: 40px;
|
||||
color: #f2b7bd;
|
||||
}
|
||||
.starter-template .content .lead {
|
||||
font-size: 25px;
|
||||
color: #f2b7bd;
|
||||
}
|
||||
.starter-template .content .lead .font-normal {
|
||||
color: #ffffff;
|
||||
}
|
||||
.starter-template .links {
|
||||
float: right;
|
||||
right: 0;
|
||||
margin-top: 125px;
|
||||
}
|
||||
.starter-template .links ul {
|
||||
display: block;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
.starter-template .links ul li {
|
||||
list-style: none;
|
||||
display: inline;
|
||||
margin: 0 10px;
|
||||
}
|
||||
.starter-template .links ul li:first-child {
|
||||
margin-left: 0;
|
||||
}
|
||||
.starter-template .links ul li:last-child {
|
||||
margin-right: 0;
|
||||
}
|
||||
.starter-template .links ul li.current-version {
|
||||
color: #f2b7bd;
|
||||
font-weight: 400;
|
||||
}
|
||||
.starter-template .links ul li a, a {
|
||||
color: #f2b7bd;
|
||||
text-decoration: underline;
|
||||
}
|
||||
.starter-template .links ul li a:hover, a:hover {
|
||||
color: #ffffff;
|
||||
text-decoration: underline;
|
||||
}
|
||||
.starter-template .links ul li .icon-muted {
|
||||
color: #eb8b95;
|
||||
margin-right: 5px;
|
||||
}
|
||||
.starter-template .links ul li:hover .icon-muted {
|
||||
color: #ffffff;
|
||||
}
|
||||
.starter-template .copyright {
|
||||
margin-top: 10px;
|
||||
font-size: 0.9em;
|
||||
color: #f2b7bd;
|
||||
text-transform: lowercase;
|
||||
float: right;
|
||||
right: 0;
|
||||
}
|
||||
@media (max-width: 1199px) {
|
||||
.starter-template .content h1 {
|
||||
font-size: 45px;
|
||||
}
|
||||
.starter-template .content h1 .smaller {
|
||||
font-size: 30px;
|
||||
}
|
||||
.starter-template .content .lead {
|
||||
font-size: 20px;
|
||||
}
|
||||
}
|
||||
@media (max-width: 991px) {
|
||||
.starter-template {
|
||||
margin-top: 0;
|
||||
}
|
||||
.starter-template .logo {
|
||||
margin: 40px auto;
|
||||
}
|
||||
.starter-template .content {
|
||||
margin-left: 0;
|
||||
text-align: center;
|
||||
}
|
||||
.starter-template .content h1 {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.starter-template .links {
|
||||
float: none;
|
||||
text-align: center;
|
||||
margin-top: 60px;
|
||||
}
|
||||
.starter-template .copyright {
|
||||
float: none;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
@media (max-width: 767px) {
|
||||
.starter-template .content h1 .smaller {
|
||||
font-size: 25px;
|
||||
display: block;
|
||||
}
|
||||
.starter-template .content .lead {
|
||||
font-size: 16px;
|
||||
}
|
||||
.starter-template .links {
|
||||
margin-top: 40px;
|
||||
}
|
||||
.starter-template .links ul li {
|
||||
display: block;
|
||||
margin: 0;
|
||||
}
|
||||
.starter-template .links ul li .icon-muted {
|
||||
display: none;
|
||||
}
|
||||
.starter-template .copyright {
|
||||
margin-top: 20px;
|
||||
}
|
||||
}
|
||||
8
web_db/web_db/web_db/tags.py
Normal file
8
web_db/web_db/web_db/tags.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""Handle all tag-relation functions."""
|
||||
from pyramid.view import view_config
|
||||
|
||||
|
||||
@view_config(route_name="values.collection", renderer="prettyjson", request_method="GET")
|
||||
def get_valuescollection(request):
|
||||
"""Get tag values for all tags for the given device."""
|
||||
tag = request.db['data'].find({'address': request.matchdict['address']}, {"address": 1, "device_type": 1, "tag_types": 1})
|
||||
64
web_db/web_db/web_db/templates/layout.jinja2
Normal file
64
web_db/web_db/web_db/templates/layout.jinja2
Normal file
@@ -0,0 +1,64 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="{{request.locale_name}}">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta name="description" content="pyramid web application">
|
||||
<meta name="author" content="Pylons Project">
|
||||
<link rel="shortcut icon" href="{{request.static_url('web_db:static/pyramid-16x16.png')}}">
|
||||
|
||||
<title>Cookiecutter Starter project for the Pyramid Web Framework</title>
|
||||
|
||||
<!-- Bootstrap core CSS -->
|
||||
<link href="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
|
||||
|
||||
<!-- Custom styles for this scaffold -->
|
||||
<link href="{{request.static_url('web_db:static/theme.css')}}" rel="stylesheet">
|
||||
|
||||
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
|
||||
<script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div class="starter-template">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-2">
|
||||
<img class="logo img-responsive" src="{{request.static_url('web_db:static/pyramid.png') }}" alt="pyramid web framework">
|
||||
</div>
|
||||
<div class="col-md-10">
|
||||
{% block content %}
|
||||
<p>No content</p>
|
||||
{% endblock content %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="links">
|
||||
<ul>
|
||||
<li><i class="glyphicon glyphicon-cog icon-muted"></i><a href="https://github.com/Pylons/pyramid">Github Project</a></li>
|
||||
<li><i class="glyphicon glyphicon-globe icon-muted"></i><a href="https://webchat.freenode.net/?channels=pyramid">IRC Channel</a></li>
|
||||
<li><i class="glyphicon glyphicon-home icon-muted"></i><a href="http://pylonsproject.org">Pylons Project</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="copyright">
|
||||
Copyright © Pylons Project
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Bootstrap core JavaScript
|
||||
================================================== -->
|
||||
<!-- Placed at the end of the document so the pages load faster -->
|
||||
<script src="//oss.maxcdn.com/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
8
web_db/web_db/web_db/templates/mytemplate.jinja2
Normal file
8
web_db/web_db/web_db/templates/mytemplate.jinja2
Normal file
@@ -0,0 +1,8 @@
|
||||
{% extends "layout.jinja2" %}
|
||||
|
||||
{% block content %}
|
||||
<div class="content">
|
||||
<h1><span class="font-semi-bold">Pyramid</span> <span class="smaller">Starter project</span></h1>
|
||||
<p class="lead">Welcome to <span class="font-normal">Henry Pump DataLogger Database</span>, a Pyramid application generated by<br><span class="font-normal">Cookiecutter</span>.</p>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
29
web_db/web_db/web_db/tests.py
Normal file
29
web_db/web_db/web_db/tests.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import unittest
|
||||
|
||||
from pyramid import testing
|
||||
|
||||
|
||||
class ViewTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.config = testing.setUp()
|
||||
|
||||
def tearDown(self):
|
||||
testing.tearDown()
|
||||
|
||||
def test_my_view(self):
|
||||
from .views import my_view
|
||||
request = testing.DummyRequest()
|
||||
info = my_view(request)
|
||||
self.assertEqual(info['project'], 'Henry Pump DataLogger Database')
|
||||
|
||||
|
||||
class FunctionalTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
from web_db import main
|
||||
app = main({})
|
||||
from webtest import TestApp
|
||||
self.testapp = TestApp(app)
|
||||
|
||||
def test_root(self):
|
||||
res = self.testapp.get('/', status=200)
|
||||
self.assertTrue(b'Pyramid' in res.body)
|
||||
6
web_db/web_db/web_db/views.py
Normal file
6
web_db/web_db/web_db/views.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from pyramid.view import view_config
|
||||
|
||||
|
||||
@view_config(route_name='home', renderer='templates/mytemplate.jinja2')
|
||||
def my_view(request):
|
||||
return {'project': 'Henry Pump DataLogger Database'}
|
||||
Reference in New Issue
Block a user