Blame view

api/app/Http/Controllers/V1/Partners/AdtController.php 1.72 KB
0d8c0f816   Thanasis Naskos   initial commit
1
2
3
4
5
6
7
8
  <?php
  
  namespace App\Http\Controllers\V1\Partners;
  
  
  use App\Http\Controllers\Controller;
  use App\Http\Requests\AdtRequest;
  use Carbon\Carbon;
a092eba85   Chris Bellas   Added GET endpoin...
9
  use Illuminate\Http\Request;
0d8c0f816   Thanasis Naskos   initial commit
10
  use Illuminate\Support\Facades\DB;
0d8c0f816   Thanasis Naskos   initial commit
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
  use Salman\Mqtt\Facades\Mqtt;
  
  class AdtController extends Controller
  {
      public function store(AdtRequest $request)
      {
          $dbEvents = [];
          $mqttEvents = [];
          foreach ($request->get('event_alarm') as $ev) {
              $dbEvents[] = [
                  'asset_id' => $request->get('asset_id'),
                  'event_alarm_id' => $ev['event_alarm_id'],
                  'event_alarm_char' => $ev['event_alarm_char'],
                  'name' => isset($ev['name']) ? $ev['name'] : null,
                  'time' => Carbon::createFromTimestamp($request->get('timestamp')),
              ];
              $mqttEvents[] = [
                  'asset_id' => $request->get('asset_id'),
                  'event_alarm_id' => $ev['event_alarm_id'],
                  'event_alarm_char' => $ev['event_alarm_char'],
                  'name' => isset($ev['name']) ? $ev['name'] : null,
                  'time' => $request->get('timestamp'),
              ];
          }
  
          // publish to MQTT Broker
d58ee553d   Chris Bellas   Temporary removed...
37
38
          // Mqtt::ConnectAndPublish('adt/event', json_encode($mqttEvents), $request->user()->id);
          Mqtt::ConnectAndPublish('adt/event', json_encode($mqttEvents), 1);
0d8c0f816   Thanasis Naskos   initial commit
39
40
41
42
          DB::table('adt')->insert($dbEvents);
  
          return response()->json(['success' => 'success'], 200);
      }
a092eba85   Chris Bellas   Added GET endpoin...
43
44
45
46
47
48
49
50
51
52
53
54
  
      public function get(Request $request)
      {
          return DB::table('adt')->select([
              'uuid',
              'time',
              'asset_id',
              'event_alarm_id',
              'event_alarm_char',
              'name'
          ])->paginate(20);
      }
0d8c0f816   Thanasis Naskos   initial commit
55
  }