/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
1
<?php
2
header("Content-type: text/xml");
3
header("Cache-Control: no-cache, must-revalidate");
4
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
5
119 by Suren A. Chilingaryan
New way of service handling (mod_rewrite), Various fixups, katrin stuff moved to SETUP directory
6
global $ADEI;
7
global $fix_time;
8
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
9
define("DATA_MODE", 0);
10
define("ALARMS_MODE", 1);
11
define("STATUS_MODE", 2);
175 by Suren A. Chilingaryan
Minor extension of control service: access to the last stored data and non-verified set
12
define("ERROR_MODE", 3);
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
13
169 by Suren A. Chilingaryan
Control subsystem
14
$res = false;
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
15
$mode = DATA_MODE;
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
16
17
try {
169 by Suren A. Chilingaryan
Control subsystem
18
    $req = $ADEI->CreateControlGroupRequest();
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
19
//    $enc = $req->GetResponseEncoding(REQUEST::ENCODING_XML);
20
86 by Suren A. Chilingaryan
Alarms support in all browsers
21
    $xslt = $req->GetProp('xslt');
22
    $target = $req->GetProp('target');
169 by Suren A. Chilingaryan
Control subsystem
23
    
24
    $status = "";
86 by Suren A. Chilingaryan
Alarms support in all browsers
25
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
26
    $reader = $req->CreateReader();
27
    
86 by Suren A. Chilingaryan
Alarms support in all browsers
28
    switch ($target) {
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
29
     case "get":
169 by Suren A. Chilingaryan
Control subsystem
30
	$res = $reader->GetControls(); 
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
31
     break;
175 by Suren A. Chilingaryan
Minor extension of control service: access to the last stored data and non-verified set
32
     case "get_data":
33
	$res = $reader->GetControlsFromData(); 
34
     break;
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
35
     case "set":
169 by Suren A. Chilingaryan
Control subsystem
36
        $data = $reader->SetControls();
37
	$alarms = $reader->GetCurrentAlarms();
38
	if (($data)||($alarms)) $res = true;
39
	$status = "The new control values are set"; 
40
	$mode = STATUS_MODE;
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
41
     break;
175 by Suren A. Chilingaryan
Minor extension of control service: access to the last stored data and non-verified set
42
     case "send":
43
        $reader->SetControls(NULL, NULL, $info=NULL, REQUEST::SKIP_CHECKS);
44
	$status = "The new control values are set"; 
45
	$mode = ERROR_MODE;
46
     break;
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
47
     case "alarms":
48
        $res = $reader->GetAlarmsDetailed();
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
49
	$mode = ALARMS_MODE;
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
50
     break;
51
     case "alarms_summary":
52
        $res = $reader->GetAlarms();
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
53
	$mode = ALARMS_MODE;
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
54
     break;
55
     case "alarms_current":
56
        $res = $reader->GetCurrentAlarms();
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
57
	$mode = ALARMS_MODE;
58
     break;
59
     case "status":
60
	$data = $reader->GetControls(NULL, NULL); 
61
	$alarms = $reader->GetCurrentAlarms();
62
	if (($data)||($alarms)) $res = true;
63
	$mode = STATUS_MODE;
64
     break;
65
     default:
66
	if (isset($_GET['target'])) $error = translate("Unknown control target (%s) is specified", $_GET['target']);
67
	else $error = translate("The control target is not specified");
68
    }
69
70
    if (($mode == ALARMS_MODE)||($mode == STATUS_MODE)) {
71
	$title = $reader->GetSourceTitle();
72
	$fix_time = $req->GetTimeFormat();
169 by Suren A. Chilingaryan
Control subsystem
73
	
74
	if ($mode != STATUS_MODE) $alarms = $res;
75
	
76
	$arr = array();
77
	foreach ($alarms as $control) {
78
	    array_push($arr, $control);
79
	}
80
	
81
	if ($mode == STATUS_MODE) $alarms = $arr;
82
	else $res = $arr;
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
83
    }
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
84
} catch(ADEIException $ex) {
85
    $ex->logInfo(NULL, $reader?$reader:$req);
86
    $error = xml_escape($ex->getInfo());
87
}
88
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
89
function OutputResults($out, $res, $mode) {
90
    global $fix_time;
119 by Suren A. Chilingaryan
New way of service handling (mod_rewrite), Various fixups, katrin stuff moved to SETUP directory
91
169 by Suren A. Chilingaryan
Control subsystem
92
    switch ($mode) {
93
	case DATA_MODE:
94
	    $tag = "data";
95
	break;
96
	case ALARMS_MODE:
97
	    $tag = "alarms";
98
	break;
99
    }
100
    
101
    fwrite($out, "<$tag>");
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
102
    foreach ($res as $control) {
103
	if ($fix_time) {
104
	    if ($mode == ALARMS_MODE) {
105
	        if ($control['in']) $control['in'] = date($fix_time, $control['in']);
106
		if ($control['out']) $control['out'] = date($fix_time, $control['out']);
107
	    }
119 by Suren A. Chilingaryan
New way of service handling (mod_rewrite), Various fixups, katrin stuff moved to SETUP directory
108
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
109
	    if ($mode == DATA_MODE) {
110
	        if ($control['timestamp']) $control['timestamp'] = date($fix_time, $control['timestamp']);
111
		if ($control['verified']) $control['verified'] = date($fix_time, $control['verified']);
112
		if ($control['obtained']) $control['obtained'] = date($fix_time, $control['obtained']);
113
	    }
114
	}
115
116
	$extra = "";
117
	foreach ($control as $prop => $value) {
118
	    $extra .= " $prop=\"" . xml_escape($value) . "\"";
119
	}
120
	fwrite($out, " <Value$extra/>\n");
121
    }
169 by Suren A. Chilingaryan
Control subsystem
122
    fwrite($out, "</$tag>");
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
123
}
124
125
86 by Suren A. Chilingaryan
Alarms support in all browsers
126
if ($xslt) {
127
    $temp_file = tempnam(sys_get_temp_dir(), 'adei_control.');
128
    $out = @fopen($temp_file, "w");
129
    if (!$out) $error = translate("I'm not able to create temporary file \"%s\"", $temp_file);
130
} else {
131
    $out = fopen("php://output", "w");
132
}
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
133
86 by Suren A. Chilingaryan
Alarms support in all browsers
134
if ($out) {
135
    fwrite($out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
136
    if ($error) {
137
	fwrite($out, "<result><Error>$error</Error></result>");
138
	$error = false;
169 by Suren A. Chilingaryan
Control subsystem
139
    } else if ($res !== false) {
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
140
	$extra = "";
86 by Suren A. Chilingaryan
Alarms support in all browsers
141
        if ($title) $extra .= " title=\"" . xml_escape($title) . "\"";
169 by Suren A. Chilingaryan
Control subsystem
142
	if ($status) $extra .= " status=\"" . xml_escape($status) . "\"";
86 by Suren A. Chilingaryan
Alarms support in all browsers
143
    
144
	fwrite($out, "<result$extra>\n");
87 by Suren A. Chilingaryan
JavaScript fixups, some stuff for control data display is implemented
145
	if ($mode == STATUS_MODE) {
146
	    OutputResults($out, $data, DATA_MODE);
147
	    OutputResults($out, $alarms, ALARMS_MODE);
148
	} else {
149
	    OutputResults($out, $res, $mode);
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
150
	}
86 by Suren A. Chilingaryan
Alarms support in all browsers
151
	fwrite($out, "</result>");
175 by Suren A. Chilingaryan
Minor extension of control service: access to the last stored data and non-verified set
152
    } else if ($mode == ERROR_MODE) {
153
	echo "<result>Ok</result>\n";
86 by Suren A. Chilingaryan
Alarms support in all browsers
154
    }
155
    fclose($out);
156
}
157
158
if (($xslt)&&(!$error)) {
159
    try {
160
	echo $ADEI->TransformXML($xslt, $temp_file);
161
    } catch (ADEIException $ex) {
162
	$ex->logInfo(NULL, $reader?$reader:$req);
163
	$error = $ADEI->EscapeForXML($ex->getInfo());
164
    }
165
    @unlink($temp_file);
166
}
167
168
if ($error) {
169
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
170
    echo "<result><Error>$error</Error></result>";
83 by Suren A. Chilingaryan
Initial support for alarms in backend, DB retries on link failures, more on axis
171
}
172
173
174
#    $export = $req->CreateExporter();
175
#    $export->Export();
176
#} catch(ADEIException $ex) {
177
178
?>