root / hci / trunk / eneraptor-web-app / grails-app / controllers / com / eneraptor / hci / ConfigController.groovy @ 69
History | View | Annotate | Download (4.13 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 | 66 | alexbesir | def allDevices = HardwareSet.get(session.hwSet.id).devices
|
15 | |||
16 | 67 | alexbesir | [conf: InternalConfig, devices: allDevices] |
17 | 3 | alexbesir | } |
18 | |||
19 | def addDevice = {
|
||
20 | } |
||
21 | |||
22 | def addDeviceSave = {
|
||
23 | def newDevice = new DeviceInfo(params) |
||
24 | 66 | alexbesir | HardwareSet.get(session.hwSet.id).addToDevices(newDevice) |
25 | 6 | alexbesir | if(newDevice.validate() && (params.inputDevice || params.outputDevice)) {
|
26 | 3 | alexbesir | 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 | 6 | alexbesir | 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 | 3 | alexbesir | redirect(action:'addDevice', params: params) |
34 | } |
||
35 | } |
||
36 | |||
37 | 6 | alexbesir | 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 | 3 | alexbesir | def logic = {
|
89 | |||
90 | 66 | alexbesir | def hwSet = session.hwSet
|
91 | 3 | alexbesir | |
92 | 66 | alexbesir | [hwSet:hwSet]
|
93 | |||
94 | 3 | alexbesir | } |
95 | |||
96 | def connection = {
|
||
97 | |||
98 | 66 | alexbesir | def hwSet = session.hwSet
|
99 | 3 | alexbesir | |
100 | 66 | alexbesir | [hwSet: hwSet]
|
101 | |||
102 | 3 | alexbesir | } |
103 | |||
104 | def saveConnection = {
|
||
105 | |||
106 | if(params['logic-ip'] == 'localhost') params['logic-ip'] = '127.0.0.1' |
||
107 | |||
108 | 66 | alexbesir | 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 | 69 | alexbesir | HardwareSet toChange = HardwareSet.get(session.hwSet.id) |
113 | toChange.IPaddress = params['logic-ip']
|
||
114 | toChange.portNumber = (params['logic-port'] as int) |
||
115 | toChange.save() |
||
116 | 3 | alexbesir | flash['confirms'] = "<p>Changes saved.</p>" |
117 | redirect(action: 'connection') |
||
118 | } else {
|
||
119 | 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>" |
120 | 3 | alexbesir | redirect(action: 'connection') |
121 | } |
||
122 | |||
123 | } |
||
124 | |||
125 | 6 | alexbesir | def dbExec = {
|
126 | |||
127 | if(params['sqlQuery']) { |
||
128 | |||
129 | flash['confirms'] = null |
||
130 | flash['errors'] = null |
||
131 | |||
132 | def db_sql = new Sql(dataSource) |
||
133 | List rows
|
||
134 | List keys = new ArrayList() |
||
135 | boolean wasError = false |
||
136 | try {
|
||
137 | rows = db_sql.rows(params['sqlQuery'])
|
||
138 | rows[0].each {
|
||
139 | keys << it.key
|
||
140 | } |
||
141 | flash['confirms'] = "<p>SQL query executed succesfully. Chrck returned rows below.</p>" |
||
142 | } catch (SQLException e) { |
||
143 | rows = new ArrayList() |
||
144 | if(e.getSQLState().equals("02000")) { |
||
145 | keys << 'SQL query result'
|
||
146 | rows << ['SQL query result' : 'No rows were returned.'] |
||
147 | flash['confirms'] = "<p>SQL query executed succesfully.</p>" |
||
148 | } else {
|
||
149 | rows << e |
||
150 | rows << e.getSQLState() |
||
151 | wasError = true
|
||
152 | flash['errors'] = "<p>There was an error while executing the SQL query. Check the report below.</p><p>The query was not executed</p>" |
||
153 | } |
||
154 | } |
||
155 | return [sqlResult: rows, keys: keys, wasError: wasError] |
||
156 | } |
||
157 | |||
158 | } |
||
159 | |||
160 | 3 | alexbesir | def getHelp = {
|
161 | [what : params.what] |
||
162 | } |
||
163 | 66 | alexbesir | |
164 | def database = { }
|
||
165 | 3 | alexbesir | |
166 | } |