/** * API function for register device. * * @return Response */ public function postRegisterDevice() { $data = post(); // Validator for post params $valids = Validator::make($data, ['device_id' => 'required']); // Check is valid if ($valids->fails()) { return EXRHelper::getErrorMsg($valids->messages()); } $result = Devices::addDevice($data['device_id']); return EXRHelper::getSuccess($result); }
/** * Api function for register new follow. * * @return Response */ public function postRegisterNotification() { $data = Input::all(); // Valid post params. $valids = Validator::make($data, ['device_id' => 'required|alpha_dash', 'notify_type' => 'required|alpha_dash', 'currency_id' => 'required|numeric|min:1'], EXRHelper::getMessage()); // Check validator. if ($valids->fails()) { return Response::json(array('status' => 'error', 'data' => $valids->messages())); } extract($data); // Find device id by device_id $device = Device::where('device_id', $device_id)->first(); if (!$device) { return Response::json(array('status' => 'error', 'data' => Lang::get('dlnlab.aloexrates::message.device_not_exist'))); } // Check limit notification number $record = self::whereRaw('device_id = ?', array($device->id))->count(); if ($record && $record >= EXR_LIMIT_NTFS) { return Response::json(array('status' => 'error', 'data' => Lang::get('dlnlab.aloexrates::message.limit_notify'))); } // Check exists reg_id in db. $record = self::whereRaw('device_id = ? AND currency_id = ? AND notify_type = ? ', array($device->id, $currency_id, $notify_type))->first(); $enable = false; if ($record) { $record->delete(); $enable = false; } else { $record = new self(); $record->device_id = $device->id; $record->currency_id = $currency_id; $record->notify_type = $notify_type; $record->save(); $enable = true; } return Response::json(array('success' => 'Success', 'data' => array('state' => $enable))); }
/** * Api function for get currency and listing by 7 days data. * * @param number $currencyId * @return boolean|Response */ public function getCurrenciesById($currencyId = 0) { $data = get(); $default = array('currency_id' => $currencyId); // Validator input params. $valids = Validator::make($default, ['currency_id' => 'required|numeric|min:1'], EXRHelper::getMessage()); // Check fails. if ($valids->fails()) { return Reponse::json(array('status' => 'error', 'data' => $valids->messages()), 500); } $cacheId = 'currency_detail_' . $currencyId; if (!Cache::has($cacheId)) { $records = CurrencyDaily::whereRaw('currency_id = ? AND created_at >= NOW() - INTERVAL ? WEEK', array($currencyId, DLN_LIMIT_WEEK))->orderBy('updated_at', 'ASC')->with('currency')->get(); $newRecords = array(); if (count($records)) { foreach ($records as $record) { switch ($record->type) { case 'gold': $multiple = 100000; break; default: $multiple = 1; break; } $record->buy = intval($record->buy * $multiple); $record->transfer = intval($record->transfer * $multiple); $record->sell = intval($record->sell * $multiple); $record->min_buy = intval($record->min_buy * $multiple); $record->max_buy = intval($record->max_buy * $multiple); $record->min_sell = intval($record->min_sell * $multiple); $record->max_sell = intval($record->max_sell * $multiple); $record->buy_change = intval($record->buy_change * $multiple); $record->sell_change = intval($record->sell_change * $multiple); $newRecords[] = $record; } } Cache::put($cacheId, json_encode($newRecords), EXR_CACHE_MINUTE); } else { $records = json_decode(Cache::get($cacheId, null)); } return Response::json(array('status' => 'success', 'data' => $records)); }
/** * Api function for crawl send notification to devices. * * @return Response */ public function getSendNotificationDevices() { $data = get(); // Valid get params. $valids = Validator::make($data, ['token' => 'required|in:' + EXR_TOKEN], EXRHelper::getMessage()); // Check valid. if ($valids->fails()) { return Response::json(array('status' => 'Error', 'data' => $valids->messages()), 403); } // Get Currency $currencies = Currency::whereRaw('is_send = ?', array(false))->take(5)->get(); if (!count($currencies)) { Currency::where('is_send', true)->update(array('is_send' => false)); $currencies = Currency::whereRaw('is_send = ?', array(false))->take(5)->get(); } foreach ($currencies as $currency) { $message = $tye = ''; if (Cache::has('exr_notification_msg_' . $currency->id)) { $data = Cache::get('exr_notification_msg_' . $currency->id); $message = isset($data['message']) ? $data['message'] : ''; $type = isset($data['type']) ? $data['type'] : ''; } else { // Get today currency $today = CurrencyDaily::whereRaw('currency_id = ? AND DATE(created_at) = CURDATE()', array($currency->id))->first(); if (!$today) { return Response::json(array('status' => 'error', 'data' => 'Error'), 500); } $mimax = CurrencyDaily::whereRaw('currency_id = ? AND DATE(created_at) >= NOW() - INTERVAL ? WEEK', array($currency->id, EXR_LIMIT_WEEK))->select(DB::raw('MIN(buy) as min, MAX(buy) as max'))->first(); if ($mimax->min > $today->buy) { $message = sprintf(EXR_MIN_MSG, $today->name, EXR_LIMIT_WEEK, $today->buy); $tye = 'currency_min'; } else { if ($mimax->max < $today->buy) { $message = sprintf(EXR_MAX_MSG, $today->name, EXR_LIMIT_WEEK, $today->buy); $type = 'currency_max'; } else { $message = ''; $type = ''; } } if ($message) { Cache::put('exr_notification_msg_' . $currency->id, array('message' => $message, 'type' => $type), EXR_CACHE_MINUTE); } } if (!$message) { return Response::json(array('status' => 'error', 'data' => 'Error'), 500); } // Get 1000 devices $records = Notification::whereRaw('currency_id = ? AND notify_type = ? AND is_send = ?', array($currency->id, $type, false))->take(EXR_LIMIT_DEVICES)->get(); if (!count($records)) { // Update is_send for currency when send all devices. $currency->is_send = true; $currency->save(); // Clear if crawl end devices. Notification::whereRaw('currency_id = ? AND notify_type = ? AND is_send = ? AND DATE(updated_at) != CURDATE()', array($currency->id, $type, true))->update(array('is_send', false)); } // Convert devices registations ids to array. $deviceIds = $records->lists('device_id'); $response = ''; if (count($deviceIds) <= EXR_LIMIT_DEVICES && !empty($message)) { // Send to notification to it $response = Notification::sendNtfsToDevices($message, $deviceIds); } } return Response::json(array('status' => 'success', 'data' => $response)); }