Add config model
This commit is contained in:
@@ -14,6 +14,8 @@ import random
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
web_address = "http://localhost:3000"
|
||||||
|
|
||||||
|
|
||||||
class Sample(Tag):
|
class Sample(Tag):
|
||||||
def read(self, forceSend):
|
def read(self, forceSend):
|
||||||
@@ -52,75 +54,36 @@ class Sample(Tag):
|
|||||||
data = {}
|
data = {}
|
||||||
data['val'] = self.value
|
data['val'] = self.value
|
||||||
data['tagID'] = self.db_id
|
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)
|
resp = json.loads(r.text)
|
||||||
print("Stored {} for {} at {}".format(resp['val'], self.name, resp['createdAt']))
|
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 = {}
|
tag_store = {}
|
||||||
# configProperties = {}
|
scan_rate = 30 # default scan rate is 30 seconds
|
||||||
|
save_all = "test"
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
# Get tags stored in database
|
# Get tags stored in database
|
||||||
get_tag_request_data = {'where':'{"tag_class":5}'}
|
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)
|
tags = json.loads(get_tag_request.text)
|
||||||
|
|
||||||
# configObj = {}
|
sr_req_data = 'where={"parameter": "scan_rate"}'
|
||||||
# db.connect()
|
sr_req = requests.get('{}/config?{}'.format(web_address, sr_req_data))
|
||||||
# cur = db.cursor()
|
print sr_req.url
|
||||||
# query = "SELECT parameter, val FROM config GROUP BY parameter;"
|
sr_try = json.loads(sr_req.text)
|
||||||
# cur.execute(query)
|
if len(sr_try) > 0:
|
||||||
# config = cur.fetchall()
|
scan_rate = int(sr_try[0]['val'])
|
||||||
# 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'
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
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'):
|
# 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:
|
except:
|
||||||
print("ERROR EVALUATING {}".format(tag))
|
print("ERROR EVALUATING {}".format(tag))
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
time.sleep(10)
|
time.sleep(scan_rate)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
11
tagserverApp/api/controllers/ConfigController.js
Normal file
11
tagserverApp/api/controllers/ConfigController.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* ConfigController
|
||||||
|
*
|
||||||
|
* @description :: Server-side logic for managing configs
|
||||||
|
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
25
tagserverApp/api/models/Config.js
Normal file
25
tagserverApp/api/models/Config.js
Normal 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'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user