Add config model

This commit is contained in:
Patrick McDonagh
2016-04-28 18:51:41 -05:00
parent b5b7fcd638
commit 9478f9913b
3 changed files with 57 additions and 58 deletions

View File

@@ -14,6 +14,8 @@ import random
import requests
import json
web_address = "http://localhost:3000"
class Sample(Tag):
def read(self, forceSend):
@@ -52,75 +54,36 @@ class Sample(Tag):
data = {}
data['val'] = self.value
data['tagID'] = self.db_id
r = requests.post('http://localhost:1337/tag_val', data=data)
r = requests.post('{}/tag_val'.format(web_address), data=data)
resp = json.loads(r.text)
print("Stored {} for {} at {}".format(resp['val'], self.name, resp['createdAt']))
# with open(os.path.realpath('.') + '/mysql_cfg.pickle', 'rb') as pickleconfig:
# mysql_cfg = pickle.load(pickleconfig)
#
# if mysql_cfg:
# db = mysqlcon.connect(**mysql_cfg)
tag_store = {}
# configProperties = {}
scan_rate = 30 # default scan rate is 30 seconds
save_all = "test"
def main():
# Get tags stored in database
get_tag_request_data = {'where':'{"tag_class":5}'}
get_tag_request = requests.get('http://localhost:1337/tag', params=get_tag_request_data)
get_tag_request = requests.get('{}/tag'.format(web_address), params=get_tag_request_data)
tags = json.loads(get_tag_request.text)
# configObj = {}
# db.connect()
# cur = db.cursor()
# query = "SELECT parameter, val FROM config GROUP BY parameter;"
# cur.execute(query)
# config = cur.fetchall()
# db.disconnect()
# for x in config:
# configObj[x[0]] = x[1]
#
# try:
# configProperties['PLC_IP_ADDRESS'] = str(configObj['ip_address'])
# print("FYI, using PLC IP Address from the database {0}".format(configProperties['PLC_IP_ADDRESS']))
# except KeyError:
# print("FYI, there is no PLC IP Address stored in the database, defaulting to 192.168.1.10")
# configProperties['PLC_IP_ADDRESS'] = "192.168.1.10"
#
# try:
# configProperties['plc_type'] = str(configObj['plc_type'])
# print("FYI, using PLC Type from the database {0}".format(configProperties['plc_type']))
# except KeyError:
# print("FYI, there is no PLC Type stored in the database, defaulting to CLX")
# configProperties['plc_type'] = "CLX"
#
# try:
# configProperties['scan_rate'] = int(configObj['scan_rate'])
# print("FYI, using Scan Rate from the database {0}".format(configProperties['scan_rate']))
# except KeyError:
# print("FYI, there is no Scan Rate stored in the database, defaulting to 10 seconds")
# configProperties['scan_rate'] = 10
#
# try:
# sa_test = str(configObj['save_all'])
# if sa_test.lower() == "true":
# configProperties['save_all'] = True
# elif sa_test.lower() == "false":
# configProperties['save_all'] = False
# else:
# configProperties['save_all'] = "test"
# print("FYI, value for save_all is {0}".format(configProperties['save_all']))
# except KeyError:
# print("FYI, there is no save_all value stored in the database, using 'test'")
# configProperties['save_all'] = 'test'
sr_req_data = 'where={"parameter": "scan_rate"}'
sr_req = requests.get('{}/config?{}'.format(web_address, sr_req_data))
print sr_req.url
sr_try = json.loads(sr_req.text)
if len(sr_try) > 0:
scan_rate = int(sr_try[0]['val'])
sa_req_data = {"where":{"parameter":"save_all"}}
sa_req = requests.get('{}/config'.format(web_address), params=sa_req_data)
sa_try = json.loads(sa_req.text)
if len(sa_try) > 0:
if sa_try[0]['val'].lower() == "true":
save_all = True
elif sa_try[0]['val'].lower() == "false":
save_all = False
for t in tags:
# name, tag, db_id, data_type, change_threshold, guarantee_sec, mapFn=None, device_type='CLX', ip_address='192.168.1.10'):
@@ -134,7 +97,7 @@ def main():
except:
print("ERROR EVALUATING {}".format(tag))
traceback.print_exc()
time.sleep(10)
time.sleep(scan_rate)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,11 @@
/**
* ConfigController
*
* @description :: Server-side logic for managing configs
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
module.exports = {
};

View File

@@ -0,0 +1,25 @@
/**
* Config.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/
module.exports = {
tableName: 'config',
attributes: {
id: {
type: 'integer',
unique: true,
autoIncrement: true,
primaryKey: true
},
parameter: {
type: 'string',
unique: true
},
val: {
type: 'string'
}
}
};