Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/0.3.0' into main
- Loading branch information
Showing
80 changed files
with
4,632 additions
and
10,905 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Device; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
use Inertia\Inertia; | ||
|
||
class DeviceController extends Controller | ||
{ | ||
public function index(Request $request) | ||
{ | ||
$this->authorize('viewAny', Device::class); | ||
|
||
$devices = Device::with('address')->paginate(); | ||
|
||
return Inertia::render('Device/Index', [ | ||
'devices' => $devices, | ||
'can' => [ | ||
'device.create' => $request->user()->can('create', Device::class) | ||
] | ||
]); | ||
} | ||
|
||
public function create(Request $request) | ||
{ | ||
$this->authorize('create', Device::class); | ||
|
||
return Inertia::render('Device/Create', [ | ||
'integrations' => Device::availableIntegrations() | ||
]); | ||
} | ||
|
||
public function store(Request $request) | ||
{ | ||
$this->authorize('create', Device::class); | ||
|
||
$request->validate([ | ||
'name' => 'required|string|max:100', | ||
'device_id' => 'required|unique:devices', | ||
'integration' => ['required', Rule::in(\App\Models\Device::availableIntegrations())], | ||
'description' => 'nullable|string|max:300', | ||
'payload_attributes' => 'nullable|array', | ||
'latitude' => 'nullable|numeric|between:-90,90', | ||
'longitude' => 'nullable|numeric|between:-180,180', | ||
'province' => 'string|nullable', | ||
'city' => 'string|nullable', | ||
'district' => 'string|nullable', | ||
'subdistrict' => 'string|nullable' | ||
]); | ||
|
||
$device = Device::create($request->only('name', 'device_id', 'integration', 'description', 'payload_attributes', 'latitude', 'longitude')); | ||
|
||
$device->address()->create($request->only('province', 'city', 'district', 'subdistrict')); | ||
|
||
return redirect()->route('device.show', $device); | ||
} | ||
|
||
public function show(Device $device, Request $request) | ||
{ | ||
$this->authorize('view', $device); | ||
|
||
return Inertia::render('Device/Show', [ | ||
'device' => $device->load('address'), | ||
'can' => [ | ||
'device.update' => $request->user()->can('update', $device), | ||
'device.delete' => $request->user()->can('delete', $device) | ||
] | ||
]); | ||
} | ||
|
||
public function edit(Device $device) | ||
{ | ||
$this->authorize('update', $device); | ||
|
||
return Inertia::render('Device/Edit', [ | ||
'device' => $device->load('address') | ||
]); | ||
} | ||
|
||
public function editAttributes(Device $device) | ||
{ | ||
$this->authorize('update', $device); | ||
|
||
return Inertia::render('Device/EditAttributes', [ | ||
'device' => $device | ||
]); | ||
} | ||
|
||
public function update(Device $device, Request $request) | ||
{ | ||
$this->authorize('update', $device); | ||
|
||
$request->validate([ | ||
'description' => 'nullable|string|max:300', | ||
'latitude' => 'nullable|numeric|between:-90,90', | ||
'longitude' => 'nullable|numeric|between:-180,180', | ||
'province' => 'string|nullable', | ||
'city' => 'string|nullable', | ||
'district' => 'string|nullable', | ||
'subdistrict' => 'string|nullable' | ||
]); | ||
|
||
$device->update($request->only('description', 'latitude', 'longitude')); | ||
$device->address->update($request->only('province', 'city', 'district', 'subdistrict')); | ||
|
||
return redirect()->route('device.show', $device)->with('status', __('messages.success')); | ||
} | ||
|
||
public function updateAttributes(Device $device, Request $request) | ||
{ | ||
$this->authorize('update', $device); | ||
|
||
$request->validate([ | ||
'payload_attributes' => 'required|array', | ||
'payload_attributes.*.unit' => 'nullable|string|max:15', | ||
'payload_attributes.*.conversion' => 'nullable|string|max:25' | ||
]); | ||
|
||
$device->payload_attributes = $request->payload_attributes; | ||
$device->save(); | ||
|
||
return redirect()->route('device.show', $device)->with('status', __('messages.success')); | ||
} | ||
|
||
public function destroy(Device $device) | ||
{ | ||
$this->authorize('delete', $device); | ||
|
||
$device->delete(); | ||
|
||
return redirect()->route('device.index'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Losant; | ||
|
||
use App\Models\Device; | ||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\LosantDevices as ResourcesLosantDevices; | ||
use App\Integrations\Losant\LosantDevices; | ||
|
||
class LosantDeviceController extends Controller | ||
{ | ||
public $device; | ||
|
||
public function __construct(LosantDevices $device) { | ||
$this->device = $device; | ||
} | ||
|
||
public function index(Request $request) | ||
{ | ||
$params = ['query' => null]; | ||
|
||
if ($request->exclude_registered == true) { | ||
$ids = Device::where('integration', 'losant')->pluck('device_id')->toArray(); | ||
$params['query']['id'] = ['$nin' => $ids]; | ||
} | ||
|
||
if ($params['query']) { | ||
$params['query'] = json_encode($params['query']); | ||
} | ||
|
||
$response = $this->device->devices($params); | ||
|
||
return ResourcesLosantDevices::collection($response->items); | ||
} | ||
|
||
public function show($device) | ||
{ | ||
$reponse = $this->device->deviceDetail($device); | ||
|
||
return new ResourcesLosantDevices($reponse); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
app/Http/Controllers/Losant/LosantDeviceDataController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Losant; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\LosantDataResource; | ||
use App\Integrations\Losant\LosantData; | ||
use App\Models\Device; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
|
||
class LosantDeviceDataController extends Controller | ||
{ | ||
public $client; | ||
|
||
public function __construct(LosantData $client) { | ||
$this->client = $client; | ||
} | ||
|
||
public function index(Device $device, Request $request) | ||
{ | ||
$this->authorize('view', $device); | ||
|
||
$request->validate([ | ||
'start_date' => 'date|nullable', | ||
'end_date' => 'date|nullable', | ||
'aggregation' => ['nullable', Rule::in(LosantData::getAggregations()), Rule::requiredIf($request->filled('resolution'))], | ||
'resolution' => ['integer', 'nullable', Rule::requiredIf($request->filled('aggregation'))], | ||
'payload_attributes' => ['nullable', 'array'], | ||
'payload_attributes.*' => ['string'] | ||
|
||
]); | ||
|
||
$data['deviceIds'] = [$device->device_id]; | ||
$data['end'] = 0; | ||
|
||
if ($request->filled('start_date')) { | ||
$data['start'] = Carbon::parse($request->start_date)->timestamp * 1000; | ||
} | ||
|
||
if ($request->filled('end_date')) { | ||
$data['end'] = Carbon::parse($request->end_date)->timestamp * 1000; | ||
} | ||
|
||
if ($request->filled('payload_attributes')) { | ||
$data['attributes'] = $request->payload_attributes; | ||
} | ||
|
||
if ($request->filled('aggregation')) { | ||
$data['aggregation'] = $request->aggregation; | ||
} | ||
|
||
if ($request->filled('resolution')) { | ||
$data['resolution'] = (int)$request->resolution * 60000; | ||
} | ||
|
||
$response = $this->client->timeSeriesQuery($data); | ||
|
||
return LosantDataResource::collection($response->devices->{$device->device_id}->points); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Planting; | ||
use Illuminate\Http\Request; | ||
use Inertia\Inertia; | ||
|
||
class PlantingHarvestController extends Controller | ||
{ | ||
public function create(Planting $planting) | ||
{ | ||
$this->authorize('update', $planting->field); | ||
|
||
$planting->append('harvest_quality'); | ||
|
||
return Inertia::render('Harvest/Create', [ | ||
'planting' => $planting->load('field.address', 'field.user') | ||
]); | ||
} | ||
|
||
public function store(Planting $planting, Request $request) | ||
{ | ||
$this->authorize('update', $planting->field); | ||
|
||
$request->validate([ | ||
'harvested_at' => 'nullable|date', | ||
'received_at' => 'nullable|date', | ||
'yield' => 'nullable|integer', | ||
'harvest_batch' => 'nullable|string', | ||
'released_quantity' => 'integer|nullable', | ||
'ureleased_quantity' => 'integer|nullable', | ||
'water_content' => 'numeric|nullable|max:99', | ||
'dirts' => 'numeric|nullable|max:99', | ||
'discolors' => 'numeric|nullable|max:99', | ||
'splits' => 'numeric|nullable|max:99', | ||
'damaged' => 'numeric|nullable|max:99', | ||
'fungal' => 'numeric|nullable|max:99', | ||
'bugs' => 'numeric|nullable|max:99', | ||
'harvest_costs' => 'array|nullable', | ||
'harvest_costs.*.description' => 'string', | ||
'harvest_costs.*.value' => 'integer' | ||
]); | ||
|
||
$planting->harvested_at = $request->harvested_at; | ||
$planting->received_at = $request->received_at; | ||
$planting->yield = $request->yield; | ||
$planting->harvest_batch = $request->harvest_batch; | ||
$planting->released_quantity = $request->released_quantity; | ||
$planting->unreleased_quantity = $request->unreleased_quantity; | ||
$planting->harvest_costs = $request->harvest_costs; | ||
$planting->save(); | ||
|
||
$quality = $planting->qualities()->firstOrNew(['category' => 'harvest']); | ||
$quality->water_content = $request->water_content; | ||
$quality->dirts = $request->dirts; | ||
$quality->discolors = $request->discolors; | ||
$quality->splits = $request->splits; | ||
$quality->damaged = $request->damaged; | ||
$quality->fungal = $request->fungal; | ||
$quality->bugs = $request->bugs; | ||
$quality->save(); | ||
|
||
return redirect()->route('field.planting.postharvest', [$planting->field_id, $planting->id]); | ||
} | ||
} |
Oops, something went wrong.