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