コード例 #1
1
 /**
  * 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();
     }
 }