Revision 58 hci/trunk/eneraptor-web-app/grails-app/controllers/com/eneraptor/hci/ActionsController.groovy

View differences:

ActionsController.groovy
1 1
package com.eneraptor.hci
2 2

  
3
import grails.converters.*
4

  
3 5
class ActionsController {
4

  
6

  
7
	def hciLogicCommService
8
	
5 9
    def main = { }
6 10
	
7
	def view = { }
11
	def view = {
12
		
13
		[includeToOnLoadDo: true]
14
		
15
	}
8 16
	
17
	def getActions = {
18
		
19
		String reqStr = "<?xml version=\"1.0\"?><hci-request type=\"getScenarios\"></hci-request>"
20
		Map reply = hciLogicCommService.getReplyToRequest(reqStr)
21
		
22
		if(reply['status'] == 'error' || reply['content'] == null) {
23
			return [replyError: reply['content']]
24
		} else {
25
			def filteredResponse = reply['content'].minus("<?xml version=\"1.0\"?>")
26
			//System.out.println("[eneraptor-hci] Recieved reply (filtered): " + filteredResponse)
27
			def rootNode = XML.parse(filteredResponse)
28
			if(rootNode.@type == "getScenarios") {
29
				List scenarios = new ArrayList()
30
				rootNode.scenariotree.scenario.each {
31
					scenarios << [it.@id.text(),it.@name.text(),it.inDevice.@id.text(),it.outDevice.@id.text()]
32
				}
33
				[scenarios:scenarios]
34
			} else {
35
				return [replyError: "The response is invalid. Check logic's IP and port numbers."]
36
			}
37
		}
38
				
39
	}
40
	
9 41
	def add = { }
42
	
43
	def viewAction = {
44
		
45
		String reqStr = "<?xml version=\"1.0\"?><hci-request type=\"getScenarios\"></hci-request>"
46
		Map reply = hciLogicCommService.getReplyToRequest(reqStr)
47
		
48
		if(reply['status'] == 'error' || reply['content'] == null) {
49
			flash['errors'] = "<p>Error connecting with logic: " + reply['content'] + "</p>"
50
			redirect(action:'view')
51
			return false
52
		} else {
53
			def filteredResponse = reply['content'].minus("<?xml version=\"1.0\"?>")
54
			def rootNode = XML.parse(filteredResponse)
55
			if(rootNode.@type == "getScenarios") {
56
				def scenarioNode = rootNode.scenariotree[0].scenario.find {
57
					it.@id.text() == params['actionId']
58
				}
59
				if(scenarioNode == null) {
60
					flash['errors'] = "<p>Could not find a scanario with the id " + params['actionId'] + ".</p>"
61
					redirect(action:'view')
62
					return false
63
				} else {
64
					Map scenario = new HashMap()
65
					scenario['id'] = scenarioNode.@id.text()
66
					scenario['name'] = scenarioNode.@name.text()
67
					scenario['inDevice'] = scenarioNode.inDevice.@id.text()
68
					scenario['outDevice'] = scenarioNode.outDevice.@id.text()
69
					scenario['reaction'] = scenarioNode.reaction.@func.text()
70
					scenario['check_idle'] = scenarioNode.check_idle.@val.text()
71
					scenario['check_active'] = scenarioNode.check_active.@val.text()
72
					scenario['alpha'] = scenarioNode.alpha.@val.text()
73
					scenario['params'] = scenarioNode.params.@val.text()
74
					scenario['minVal'] = scenarioNode.inDevAalues.@minval.text()
75
					scenario['maxVal'] = scenarioNode.inDevAalues.@maxval.text()
76
					scenario['optVal'] = scenarioNode.inDevAalues.@optval.text()
77
					scenario['tolerance'] = scenarioNode.inDevAalues.@tolerance.text()
78
					scenario['action'] = scenarioNode.action.@act.text()
79
					scenario['param'] = scenarioNode.param.@par.text()
80
					
81
					return [scenario:scenario]
82
				}
83
			} else {
84
				flash['errors'] = "<p>Error connecting with logic: The response is invalid. Check logic's IP and port numbers.</p>"
85
				redirect(action:'view')
86
				return false
87
			}
88
		}
89
		
90
	}
91
	
92
	def deleteAction = {
93
		
94
		// First part: get XML from logic and modify
95
		
96
		String reqStr = "<?xml version=\"1.0\"?><hci-request type=\"getScenarios\"></hci-request>"
97
		Map reply = hciLogicCommService.getReplyToRequest(reqStr)
98
		
99
		if(reply['status'] == 'error' || reply['content'] == null) {
100
			flash['errors'] = "<p>Error connecting with logic: " + reply['content'] + "</p>"
101
			redirect(action:'view')
102
			return false
103
		} else {
104
			def filteredResponse = reply['content'].minus("<?xml version=\"1.0\"?>")
105
			def rootNode = new groovy.util.XmlParser().parseText(filteredResponse)
106
			if(rootNode.@type == "getScenarios") {
107
				def scenarioTree = rootNode.scenariotree[0]
108
				def nodeToRemove = scenarioTree.scenario.find {
109
					it.@id == params['actionId']
110
				}
111
				scenarioTree.remove(nodeToRemove)
112
				
113
				def writer = new StringWriter()
114
				new XmlNodePrinter(new PrintWriter(writer)).print(scenarioTree)
115
				def result = writer.toString()
116
				
117
				result = "<?xml version=\"1.0\"?><hci-request type=\"setScenarios\">" + result + "</hci-request>"
118
				
119
				// Second part: give modified XML to logic
120
				
121
				Map reply2 = hciLogicCommService.getReplyToRequest(result)
122
				
123
				if(reply2['status'] == 'error' || reply2['content'] == null) {
124
					flash['errors'] = "<p>Error connecting with logic: " + reply2['content'] + "</p>"
125
					redirect(action:'view')
126
					return false
127
				} else {
128
					def rootNode2 = XML.parse(reply2['content'])
129
					if(rootNode2.@type.text() == "setScenarios") {
130
						flash['confirms'] = "<p>Scenario deleted successfully.</p>"
131
						redirect(action:'view')
132
						return true
133
					} else {
134
						flash['errors'] = "<p>Error connecting with logic: The response is invalid. Check logic's IP and port numbers.</p>"
135
						redirect(action:'view')
136
						return false
137
					}
138
				}
139
				
140
			} else {
141
				flash['errors'] = "<p>Error connecting with logic: The response is invalid. Check logic's IP and port numbers.</p>"
142
				redirect(action:'view')
143
				return false
144
			}
145
		}
146
	}
10 147
	
11 148
}

Also available in: Unified diff