Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.8 kB
3
Indexable
Never
public function sync($request, $deviceId, $systemUpdateReleaseId){

        $salt = "BvXEX3oB4Re4U0DE5NtRYIxLQNfyvT22";

        $time = date('Y-m-d H:i:s');
        $hash = hash('sha256', $salt . $request->deviceMac);
        $ssid = config('constants.deviceSync.WIFI_SSID_PREFIX') . substr($hash, 0, config('constants.deviceSync.WIFI_SSID_LEN'));
        $pass = substr($hash, -config('constants.deviceSync.WIFI_PASS_LEN'));

        $wifi = ["ssid" => $ssid, "pass" => $pass];

        $systemDeviceTypeId = config('constants.systemDeviceType.' . $request->deviceModel);

        if ($deviceId != null) {
            $systemUpdateRelease = null;

            if ($systemUpdateReleaseId == null) {
                $systemUpdateRelease = SystemUpdateRelease::where('systemDeviceTypeId', '=', $systemDeviceTypeId)
                    ->where('systemUpdateStateId', config('constants.systemUpdateState.production'))->orderBy('id', 'DESC')->first();
            } else {
                $systemUpdateRelease = Device::join('system_update_release', 'device.systemUpdateReleaseId', 'system_update_release.id')
                    ->select('device.id', 'system_update_release.otaUrl', 'system_update_release.version', 'system_update_release.systemUpdateTypeId', 'system_update_release.systemUpdateStateId')
                    ->where('uid', $request->deviceMac)
                    ->offset(0)
                    ->limit(1)
                    ->first();
            }

            $deviceSetting = DeviceSetting::join('configuration_property', 'device_settings.configurationPropertyId', 'configuration_property.id')
                ->select('device_settings.value', 'configuration_property.type', 'configuration_property.name')
                ->where('device_settings.deviceId', $deviceId)
                ->get();


            if ($systemUpdateRelease->systemUpdateTypeId == 1) {
                $systemUpdateType = 'optional';
            } else {
                $systemUpdateType = 'mandatory';
            }

            $baseUrl = config('app.url') . '/rest/admin/storage/';

            $systemUpdate = ["type" => $systemUpdateType, "path" => $baseUrl . $systemUpdateRelease->otaUrl, "version" => $systemUpdateRelease->version];

            $settings = [];

            if ($deviceSetting->isNotEmpty()) {
                if ($deviceSetting[0]->type == config('constants.deviceSync.BOOLEAN_TYPE')) {
                    $value = filter_var($deviceSetting[0]->value, FILTER_VALIDATE_BOOLEAN);
                } else if ($deviceSetting[0]->type == config('constants.deviceSync.STRING_TYPE')) {
                    $value = $deviceSetting[0]->value;
                } else if ($deviceSetting[0]->type == config('constants.deviceSync.NUMBER_TYPE')) {
                    $value = filter_var($deviceSetting[0]->value, FILTER_VALIDATE_INT);
                } else {
                    $value = $deviceSetting[0]->value;
                }

                $setting_member = ["name" => $deviceSetting[0]->name, "value" => $value];
                $settings = [$setting_member];
            }

            $deviceResponse1 = ["type" => $request->deviceModel, "systemUpdate" => $systemUpdate, "settings" => $settings];

            $deviceUpdate = Device::find($deviceId);
            $deviceUpdate->version = (int)$request->version;
            $deviceUpdate->last_seen = date('Y-m-d H:i:s');
            $deviceUpdate->ip = $request->ip();
            $deviceUpdate->save();


            return response()->json([
                "time" => $time,
                "wifi" => $wifi,
                "device" => $deviceResponse1,
            ]);
        } else {
            $device = new Device();
            $device->uid = $request->deviceMac;
            $device->version = (int)$request->version;
            $device->systemUpdateReleaseId = null;
            $device->ip = $request->ip();
            $device->deviceTypeId = $systemDeviceTypeId;
            $device->save();

            $systemUpdateRelease = SystemUpdateRelease::where('systemDeviceTypeId', '=', $systemDeviceTypeId)->orderBy('id', 'DESC')->first();

            if ($systemUpdateRelease->systemUpdateTypeId == 1) {
                $systemUpdateType = 'optional';
            } else {
                $systemUpdateType = 'mandatory';
            }

            $systemUpd = ["type" => $systemUpdateType, "path" => $systemUpdateRelease->otaUrl, "version" => $systemUpdateRelease->version];
            $deviceResponse2 = ["type" => $request->deviceModel, "systemUpdate" => $systemUpd];


            return response()->json([
                "time" => $time,
                "wifi" => $wifi,
                "device" => $deviceResponse2
            ]);
        }
    }