/** * Execute the console command. * * @return mixed */ public function fire() { while (true) { $serial = new PhpSerial(); // First we must specify the device. This works on both linux and windows (if // your linux serial device is /dev/ttyS0 for COM1, etc) $serial->deviceSet($this->argument('device')); // We can change the baud rate, parity, length, stop bits, flow control $serial->confBaudRate(9600); $serial->confParity("none"); $serial->confCharacterLength(8); $serial->confStopBits(1); $serial->confFlowControl("none"); $serial->deviceOpen(); $read = ''; $theResult = ''; $start = microtime(true); while ($read == '' && microtime(true) <= $start + 0.5) { $read = $serial->readPort(); if ($read != '') { $theResult .= $read; $read = ''; } } $theResult = trim($theResult); if (!$theResult) { continue; } echo $theResult . "\n"; $line = explode("\n", $theResult); foreach ($line as $l) { $l = trim($l); $watt_array = explode(" ", $l); $unitId = 1; if ($watt_array[0] == "watt" && isset($watt_array[1]) && $watt_array[1] != false) { Watts::addWatt($unitId, $watt_array[1]); //funtion sa models/Watts.php Units::lessCredit($unitId, $watt_array[1]); } } $unitId = 1; if (Units::hasCredit($unitId)) { $serial->sendMessage("on"); } else { $serial->sendMessage("off"); } $serial->deviceClose(); } }
public static function addWatt($unitId, $wattVal) { //gi-add up ang values sa watt in one minute if (Watts::where('unit_id', $unitId)->where('created_at', '>', Carbon\Carbon::now()->addMinutes(-1))->count() > 0) { $watts = Watts::where('unit_id', $unitId)->orderBy('created_at', 'desc')->first(); $watts->watts += $wattVal; } else { //mag create utro ug record kung lapas na one minute $watts = new Watts(); $watts->unit_id = $unitId; $watts->watts = $wattVal; } $watts->save(); }