root / hci / trunk / eneraptor-web-app / grails-app / controllers / com / eneraptor / hci / ConfigController.groovy @ 67
History | View | Annotate | Download (3.95 KB)
1 |
package com.eneraptor.hci
|
---|---|
2 |
|
3 |
import groovy.sql.* |
4 |
import java.sql.* |
5 |
|
6 |
class ConfigController { |
7 |
|
8 |
def dataSource
|
9 |
|
10 |
def main = { }
|
11 |
|
12 |
def hci = {
|
13 |
|
14 |
def allDevices = HardwareSet.get(session.hwSet.id).devices
|
15 |
|
16 |
[conf: InternalConfig, devices: allDevices] |
17 |
} |
18 |
|
19 |
def addDevice = {
|
20 |
} |
21 |
|
22 |
def addDeviceSave = {
|
23 |
def newDevice = new DeviceInfo(params) |
24 |
HardwareSet.get(session.hwSet.id).addToDevices(newDevice) |
25 |
if(newDevice.validate() && (params.inputDevice || params.outputDevice)) {
|
26 |
newDevice.save() |
27 |
flash['confirms'] = "<p>New device added successfully.</p>" |
28 |
redirect(action:'hci') |
29 |
} else {
|
30 |
flash['errors'] = "<p>New device was not saved. Check input data.</p>" |
31 |
if(!params.inputDevice && !params.outputDevice)
|
32 |
flash['errors'] += "<p>Device must be capable of making masurements, actions or both. You have selected none.</p>" |
33 |
redirect(action:'addDevice', params: params) |
34 |
} |
35 |
} |
36 |
|
37 |
def deleteHciEntry = {
|
38 |
|
39 |
def deviceToBeDeleted = DeviceInfo.findById(params.id)
|
40 |
|
41 |
if(deviceToBeDeleted) {
|
42 |
deviceToBeDeleted.delete() |
43 |
flash['confirms'] = "<p>Device deleted succesfully.</p>" |
44 |
} else {
|
45 |
flash['errors'] = "<p>There was no device deleted.</p><p>There was no device with id ${params.id}.</p>" |
46 |
} |
47 |
redirect(action: 'hci') |
48 |
|
49 |
} |
50 |
|
51 |
def changeHciEntry = {
|
52 |
|
53 |
def deviceToBeChanged = DeviceInfo.findById(params.id)
|
54 |
if(!deviceToBeChanged) {
|
55 |
flash['errors'] = "<p>There is no device with id ${params.id}.</p>" |
56 |
redirect(action: 'hci') |
57 |
return false |
58 |
} |
59 |
deviceToBeChanged.getProperties() |
60 |
} |
61 |
|
62 |
def changeHciEntrySave = {
|
63 |
|
64 |
def deviceToBeUpdated = DeviceInfo.findById(params.id)
|
65 |
deviceToBeUpdated.properties = params |
66 |
if(deviceToBeUpdated.save()) {
|
67 |
flash['confirms'] = "<p>Device updated succesfully.</p>" |
68 |
redirect(action: 'hci') |
69 |
} else {
|
70 |
flash['errors'] = "<p>Device was not changed.</p>" |
71 |
redirect(action:'changeHciEntry', params: params) |
72 |
} |
73 |
|
74 |
} |
75 |
|
76 |
def dbInfo = {
|
77 |
|
78 |
List databaseInfo = new ArrayList() |
79 |
def db_sql = new Sql(dataSource) |
80 |
db_sql.eachRow "SHOW ALL", {
|
81 |
databaseInfo << it.toRowResult()
|
82 |
} |
83 |
|
84 |
[databaseInfo: databaseInfo]
|
85 |
|
86 |
} |
87 |
|
88 |
def logic = {
|
89 |
|
90 |
def hwSet = session.hwSet
|
91 |
|
92 |
[hwSet:hwSet]
|
93 |
|
94 |
} |
95 |
|
96 |
def connection = {
|
97 |
|
98 |
def hwSet = session.hwSet
|
99 |
|
100 |
[hwSet: hwSet]
|
101 |
|
102 |
} |
103 |
|
104 |
def saveConnection = {
|
105 |
|
106 |
if(params['logic-ip'] == 'localhost') params['logic-ip'] = '127.0.0.1' |
107 |
|
108 |
if(params['logic-ip'] && params['logic-port'] && params['logic-port'].isInteger()) { |
109 |
|
110 |
session.hwSet.IPaddress = params['logic-ip']
|
111 |
session.hwSet.portNumber = (params['logic-port'] as int) |
112 |
flash['confirms'] = "<p>Changes saved.</p>" |
113 |
redirect(action: 'connection') |
114 |
} else {
|
115 |
flash['errors'] = "<p>Could not save changes.</p><p>Logic IP address and port fields cannot be blank. Port number must be an integer.</p>" |
116 |
redirect(action: 'connection') |
117 |
} |
118 |
|
119 |
} |
120 |
|
121 |
def dbExec = {
|
122 |
|
123 |
if(params['sqlQuery']) { |
124 |
|
125 |
flash['confirms'] = null |
126 |
flash['errors'] = null |
127 |
|
128 |
def db_sql = new Sql(dataSource) |
129 |
List rows
|
130 |
List keys = new ArrayList() |
131 |
boolean wasError = false |
132 |
try {
|
133 |
rows = db_sql.rows(params['sqlQuery'])
|
134 |
rows[0].each {
|
135 |
keys << it.key
|
136 |
} |
137 |
flash['confirms'] = "<p>SQL query executed succesfully. Chrck returned rows below.</p>" |
138 |
} catch (SQLException e) { |
139 |
rows = new ArrayList() |
140 |
if(e.getSQLState().equals("02000")) { |
141 |
keys << 'SQL query result'
|
142 |
rows << ['SQL query result' : 'No rows were returned.'] |
143 |
flash['confirms'] = "<p>SQL query executed succesfully.</p>" |
144 |
} else {
|
145 |
rows << e |
146 |
rows << e.getSQLState() |
147 |
wasError = true
|
148 |
flash['errors'] = "<p>There was an error while executing the SQL query. Check the report below.</p><p>The query was not executed</p>" |
149 |
} |
150 |
} |
151 |
return [sqlResult: rows, keys: keys, wasError: wasError] |
152 |
} |
153 |
|
154 |
} |
155 |
|
156 |
def getHelp = {
|
157 |
[what : params.what] |
158 |
} |
159 |
|
160 |
def database = { }
|
161 |
|
162 |
} |