RestController.php
3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Swpb\Bundle\CocarBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use JMS\Serializer\SerializationContext;
class RestController extends Controller
{
/**
* GET Route annotation.
* @Rest\Post("/snmp")
*/
public function postSnmpAction()
{
$em = $this->getDoctrine()->getManager();
$dir = $this->get('kernel')->getRootDir() . "/../web/rrd/machine/";
foreach ($_FILES as $file)
{
try
{
$machine = new \Cocar\CocarBundle\Entity\Machine;
$file = new \Symfony\Component\HttpFoundation\File\UploadedFile(
$file['tmp_name'],
$file['name'],
$file['type'],
$file['size'],
$file['error']);
$machine->setFile($file);
$machine->preUpload();
$machine->upload();
$content = simplexml_load_file($machine->getAbsolutePath());
if(isset($content->machine["gateway"]) && isset($content->machine->macaddress))
{
$circuit = $em->getRepository('CocarBundle:Circuits')->findByIpBackbone($content->machine["gateway"]);
foreach ($circuit as $c)
{
$id = $c->getId();
$circuit = $em->getRepository('CocarBundle:Circuits')->find($id);
}
$machine->setGateway($circuit);
$machine->setIp($content->machine->ip);
$machine->setMacAddress($content->machine->macaddress);
$em->persist($machine);
$em->flush();
$dir .= $id . "/";
if(!is_dir($dir))
mkdir($dir);
$file = $dir . str_replace(":", "", $content->machine->macaddress) . '.rrd';
if (!file_exists($file))
$this->get('cocar_monitor')->createRrd($file);
foreach ($content->snmp->period as $value)
{
$this->get('cocar_monitor')->updateRrd($file, $value->in,
$value->out, $value['date']);
}
} else {
return new Response("Error in XML format");
}
} catch(\Exception $e) {
return new Response ($e->getMessage());
return new Response($this->get('jms_serializer')->serialize(array('result' => 'error'), 'json'));
}
}
return new Response($this->get('jms_serializer')->serialize(array('result' => 'success'), 'json'));
}
/**
* GET Route annotation.
* @Rest\Get("/snmp/{slug}")
*/
public function getSnmpAction($slug)
{
$entity = array('campo' => 'conteudo', 'campo2' => array('campo3' => 'conteudo2'));
$context = new SerializationContext();
$serializer = $this->get('jms_serializer');
$response = new Response($serializer->serialize($entity, 'json', $context));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* GET Route annotation.
* @Rest\Put("/snmp/{slug}")
*/
public function putSnmpAction($slug)
{
}
/**
* GET Route annotation.
* @Rest\Delete("/snmp/{slug}")
*/
public function deleteSnmpAction($slug)
{
}
}