public function actionRemoveDevice() { while (1) { $deviceModel = Devices::find()->all(); foreach ($deviceModel as $device) { $deviceLogs = DeviceLogs::find()->where(['device_id' => $device->id])->orderBy(['id' => SORT_DESC])->one(); if (empty($deviceLogs)) { $device->status = 0; $device->server_id = NULL; if ($device->save()) { echo "Device with id " . $device->controller_id . " Successfully offlined \n"; } else { print_r($device); } } else { $deviceTime = date("Y-m-d H:i:s", strtotime($deviceLogs->created . " +1 minute")); if (strtotime($deviceTime) < strtotime(date("Y-m-d H:i:s"))) { $device->status = 0; $device->server_id = NULL; if ($device->save()) { echo date("Y-m-d H:i:s", strtotime($deviceTime)) . " = " . date("Y-m-d H:i:s") . "\n"; echo "Device with id " . $device->controller_id . " Successfully offlined \n"; } else { print_r($device->getErrors()); } } else { echo "Device with id " . $device->controller_id . " is Online. \n"; } } } sleep(10); } }
public function actionDeviceOverview() { $deviceModel = Devices::find()->all(); $this->layout = "dashboard"; $response = array(); foreach ($deviceModel as $device) { $temp = array(); $deviceLogs = DeviceLogs::find()->where(['device_id' => $device->id])->orderBy(['id' => SORT_DESC])->one(); $temp['id'] = $device->id; $temp['controller_id'] = $device->controller_id; $temp['sim_number'] = $device->sim_number; $temp['status'] = $device->status == 1 ? "Online" : "Offline"; if (!empty($deviceLogs)) { $temp['current_voltage'] = $deviceLogs->current_voltage; $temp['current_load'] = isset($deviceLogs->current_load) ? $deviceLogs->current_load / 10 : $deviceLogs->current_load; $temp['voltage_status'] = Helper::voltageStatus($deviceLogs->voltage_status); $temp['light_status'] = Helper::lightStatus($deviceLogs->light_status); $temp['overload_status'] = Helper::overloadStatus($deviceLogs->overload_status); $temp['created'] = date("d-M-Y H:i:s", strtotime($deviceLogs->created)); } $response[] = $temp; } $dataProvider = new ArrayDataProvider(['key' => 'id', 'allModels' => $response]); return $this->render('deviceOverview', ['dataProvider' => $dataProvider]); }
public function decodeCommand($deviceId, $data) { $command_id = substr($data, 6, 2); $deviceModel = Devices::find()->where(['id' => $deviceId])->one(); switch ($command_id) { case "01": break; case "03": break; case "02": $v1 = hexdec(substr($data, 8, 2)); $v2 = hexdec(substr($data, 10, 2)); $voltage = $v1 * 256 + $v2; $l1 = hexdec(substr($data, 14, 2)); $l2 = hexdec(substr($data, 16, 2)); $load = $l1 * 256 + $l2; $status = str_pad(base_convert(substr($data, 18, 2), 16, 2), 8, "0", STR_PAD_LEFT); $voltage_status = base_convert(substr($status, 0, 2), 2, 10); $light_status = base_convert(substr($status, 2, 2), 2, 10); $overload_staus = base_convert(substr($status, 4, 2), 2, 10); $deviceLogs = new DeviceLogs(); $deviceLogs->region_id = $deviceModel->region_id; $deviceLogs->device_id = $deviceModel->id; $deviceLogs->current_voltage = $voltage; $deviceLogs->current_load = $load; $deviceLogs->voltage_status = $voltage_status; $deviceLogs->light_status = $light_status; $deviceLogs->overload_status = $overload_staus; if ($deviceLogs->save()) { return true; } else { echo "<pre>"; print_r($deviceLogs->getErrors()); return false; } break; case "04": break; } }