From e0d99018aaab10b367bde40833afd496b3b9ee25 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 19 Oct 2017 16:08:20 -0400 Subject: [PATCH 1/5] Google code style, add a warning if no sd --- src/zm_stream.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/zm_stream.cpp b/src/zm_stream.cpp index c0b8701a2..2fd7666bb 100644 --- a/src/zm_stream.cpp +++ b/src/zm_stream.cpp @@ -77,17 +77,13 @@ void StreamBase::updateFrameRate( double fps ) Debug( 3, "aEFPS:%.2f, aFM:%d", effective_fps, frame_mod ); } -bool StreamBase::checkCommandQueue() -{ - if ( sd >= 0 ) - { +bool StreamBase::checkCommandQueue() { + if ( sd >= 0 ) { CmdMsg msg; memset( &msg, 0, sizeof(msg) ); int nbytes = recvfrom( sd, &msg, sizeof(msg), MSG_DONTWAIT, 0, 0 ); - if ( nbytes < 0 ) - { - if ( errno != EAGAIN ) - { + if ( nbytes < 0 ) { + if ( errno != EAGAIN ) { Fatal( "recvfrom(), errno = %d, error = %s", errno, strerror(errno) ); } } @@ -95,11 +91,12 @@ bool StreamBase::checkCommandQueue() //{ //Error( "Partial message received, expected %d bytes, got %d", sizeof(msg), nbytes ); //} - else - { + else { processCommand( &msg ); return( true ); } + } else { + Warning("No sd in checkCommandQueue, comms not open?"); } return( false ); } From 464b588f0873041fa52d4f6a02b6fdb4284296ac Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 26 Feb 2018 07:29:49 -0800 Subject: [PATCH 2/5] add a case for toggle, which are booleans and default them to false --- web/includes/functions.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/web/includes/functions.php b/web/includes/functions.php index b85624335..4e94b2f7e 100644 --- a/web/includes/functions.php +++ b/web/includes/functions.php @@ -641,6 +641,7 @@ function getFormChanges( $values, $newValues, $types=false, $columns=false ) { if ( !isset($types[$key]) ) $types[$key] = false; + switch( $types[$key] ) { case 'set' : { @@ -702,6 +703,16 @@ function getFormChanges( $values, $newValues, $types=false, $columns=false ) { } break; } + case 'toggle' : + if ( (!isset($values[$key])) or $values[$key] != $value ) { + if ( empty($value) ) { + $changes[$key] = "$key = 0"; + } else { + $changes[$key] = "$key = 0"; + //$changes[$key] = $key . ' = '.dbEscape(trim($value)); + } + } + break; default : { if ( !isset($values[$key]) || ($values[$key] != $value) ) { From c6f632325e459005b7b37c8f49cbe62254e54175 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 26 Feb 2018 07:31:22 -0800 Subject: [PATCH 3/5] create an object for Controls entries, with appropriate defaults --- web/includes/Control.php | 228 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 web/includes/Control.php diff --git a/web/includes/Control.php b/web/includes/Control.php new file mode 100644 index 000000000..343a7b31a --- /dev/null +++ b/web/includes/Control.php @@ -0,0 +1,228 @@ + 0, + 'CanMoveDiag' => 0, + 'CanMoveMap' => 0, + 'CanMoveAbs' => 0, + 'CanMoveRel' => 0, + 'CanMoveCon' => 0, + 'CanPan' => 0, + 'MinPanRange' => NULL, + 'MaxPanRange' => NULL, + 'MinPanStep' => NULL, + 'MaxPanStep' => NULL, + 'HasPanSpeed' => 0, + 'MinPanSpeed' => NULL, + 'MaxPanSpeed' => NULL, + 'HasTurboPan' => 0, + 'TurboPanSpeed' => NULL, + 'CanTilt' => 0, + 'MinTiltRange' => NULL, + 'MaxTiltRange' => NULL, + 'MinTiltStep' => NULL, + 'MaxTiltStep' => NULL, + 'HasTiltSpeed' => 0, + 'MinTiltSpeed' => NULL, + 'MaxTiltSpeed' => NULL, + 'HasTurboTilt' => 0, + 'TurboTiltSpeed' => NULL, + 'CanZoom' => 0, + 'CanZoomAbs' => 0, + 'CanZoomRel' => 0, + 'CanZoomCon' => 0, + 'MinZoomRange' => NULL, + 'MaxZoomRange' => NULL, + 'MinZoomStep' => NULL, + 'MaxZoomStep' => NULL, + 'HasZoomSpeed' => 0, + 'MinZoomSpeed' => NULL, + 'MaxZoomSpeed' => NULL, + 'CanFocus' => 0, + 'CanAutoFocus' => 0, + 'CanFocusAbs' => 0, + 'CanFocusRel' => 0, + 'CanFocusCon' => 0, + 'MinFocusRange' => NULL, + 'MaxFocusRange' => NULL, + 'MinFocusStep' => NULL, + 'MaxFocusStep' => NULL, + 'HasFocusSpeed' => 0, + 'MinFocusSpeed' => NULL, + 'MaxFocusSpeed' => NULL, + 'CanIris' => 0, + 'CanAutoIris' => 0, + 'CanIrisAbs' => 0, + 'CanIrisRel' => 0, + 'CanIrisCon' => 0, + 'MinIrisRange' => NULL, + 'MaxIrisRange' => NULL, + 'MinIrisStep' => NULL, + 'MaxIrisStep' => NULL, + 'HasIrisSpeed' => 0, + 'MinIrisSpeed' => NULL, + 'MaxIrisSpeed' => NULL, + 'CanGain' => 0, + 'CanAutoGain' => 0, + 'CanGainAbs' => 0, + 'CanGainRel' => 0, + 'CanGainCon' => 0, + 'MinGainRange' => NULL, + 'MaxGainRange' => NULL, + 'MinGainStep' => NULL, + 'MaxGainStep' => NULL, + 'HasGainSpeed' => 0, + 'MinGainSpeed' => NULL, + 'MaxGainSpeed' => NULL, + 'CanWhite' => 0, + 'CanAutoWhite' => 0, + 'CanWhiteAbs' => 0, + 'CanWhiteRel' => 0, + 'CanWhiteCon' => 0, + 'MinWhiteRange' => NULL, + 'MaxWhiteRange' => NULL, + 'MinWhiteStep' => NULL, + 'MaxWhiteStep' => NULL, + 'HasWhiteSpeed' => 0, + 'MinWhiteSpeed' => NULL, + 'MaxWhiteSpeed' => NULL, + 'HasPresets' => 0, + 'NumPresets' => 0, + 'HasHomePreset' => 0, + 'CanSetPresets' => 0, + 'Name' => 'New', + 'Type' => 'Local', + 'Protocol' => NULL + ); + + public function __construct( $IdOrRow = NULL ) { + if ( $IdOrRow ) { + $row = NULL; + if ( is_integer( $IdOrRow ) or is_numeric( $IdOrRow ) ) { + $row = dbFetchOne( 'SELECT * FROM Control WHERE Id=?', NULL, array( $IdOrRow ) ); + if ( ! $row ) { + Error("Unable to load Control record for Id=" . $IdOrRow ); + } + } elseif ( is_array( $IdOrRow ) ) { + $row = $IdOrRow; + } else { + Error("Unknown argument passed to Control Constructor ($IdOrRow)"); + return; + } + + if ( $row ) { + foreach ($row as $k => $v) { + $this->{$k} = $v; + } + } else { + Error('No row for Control ' . $IdOrRow ); + } + } # end if isset($IdOrRow) + } // end function __construct + + public function __call($fn, array $args){ + if ( count($args) ) { + $this->{$fn} = $args[0]; + } + if ( array_key_exists($fn, $this) ) { + return $this->{$fn}; + #array_unshift($args, $this); + #call_user_func_array( $this->{$fn}, $args); + } else { + if ( array_key_exists($fn, $this->control_fields) ) { + return $this->control_fields{$fn}; + } else if ( array_key_exists( $fn, $this->defaults ) ) { + return $this->defaults{$fn}; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Warning( "Unknown function call Control->$fn from $file:$line" ); + } + } + } + + public function set( $data ) { + foreach ($data as $k => $v) { + if ( is_array( $v ) ) { + # perhaps should turn into a comma-separated string + $this->{$k} = implode(',',$v); + } else if ( is_string( $v ) ) { + $this->{$k} = trim( $v ); + } else if ( is_integer( $v ) ) { + $this->{$k} = $v; + } else if ( is_bool( $v ) ) { + $this->{$k} = $v; + } else { + Error( "Unknown type $k => $v of var " . gettype( $v ) ); + $this->{$k} = $v; + } + } + } + public static function find_all( $parameters = null, $options = null ) { + $filters = array(); + $sql = 'SELECT * FROM Controls '; + $values = array(); + + if ( $parameters ) { + $fields = array(); + $sql .= 'WHERE '; + foreach ( $parameters as $field => $value ) { + if ( $value == null ) { + $fields[] = $field.' IS NULL'; + } else if ( is_array( $value ) ) { + $func = function(){return '?';}; + $fields[] = $field.' IN ('.implode(',', array_map( $func, $value ) ). ')'; + $values += $value; + + } else { + $fields[] = $field.'=?'; + $values[] = $value; + } + } + $sql .= implode(' AND ', $fields ); + } + if ( $options and isset($options['order']) ) { + $sql .= ' ORDER BY ' . $options['order']; + } + $result = dbQuery($sql, $values); + $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Control'); + foreach ( $results as $row => $obj ) { + $filters[] = $obj; + } + return $filters; + } + + public function save( $new_values = null ) { + + if ( $new_values ) { + foreach ( $new_values as $k=>$v ) { + $this->{$k} = $v; + } + } + foreach ( $this->defaults as $k=>$v ) { + if ( ( ! array_key_exists( $k, $this ) ) or ( $this->{$k} == '' ) ) { + $this->{$k} = $v; + } + } + + $fields = array_keys( $this->defaults ); + +if ( array_key_exists( 'Id', $this ) ) { + $sql = 'UPDATE Controls SET '.implode(', ', array_map( function($field) {return $field.'=?';}, $fields ) ) . ' WHERE Id=?'; + $values = array_map( function($field){return $this->{$field};}, $fields ); + $values[] = $this->{'Id'}; + dbQuery( $sql, $values ); +} else { + $sql = 'INSERT INTO Controls SET '.implode(', ', array_map( function($field) {return $field.'=?';}, $fields ) ) . ''; + $values = array_map( function($field){return $this->{$field};}, $fields ); + dbQuery( $sql, $values ); +} + } // end function save + +} // end class Control +?> From bfbefc4c32888b188a390d38e26868222fd211b8 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 26 Feb 2018 07:31:48 -0800 Subject: [PATCH 4/5] Use our new Control object to do the saving instead of a generic function --- web/includes/actions.php | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/web/includes/actions.php b/web/includes/actions.php index ad9e03ce5..e01801e55 100644 --- a/web/includes/actions.php +++ b/web/includes/actions.php @@ -249,34 +249,12 @@ if ( !empty($action) ) { // Control capability actions, require control edit permissions if ( canEdit( 'Control' ) ) { if ( $action == 'controlcap' ) { - if ( !empty($_REQUEST['cid']) ) { - $control = dbFetchOne( 'SELECT * FROM Controls WHERE Id = ?', NULL, array($_REQUEST['cid']) ); - } else { - $control = array(); - } + require_once( 'Control.php' ); + $Control = new Control( !empty($_REQUEST['cid']) ? $_REQUEST['cid'] : null ); - // Define a field type for anything that's not simple text equivalent - $types = array( - // Empty - ); - - $columns = getTableColumns( 'Controls' ); - foreach ( $columns as $name=>$type ) { - if ( preg_match( '/^(Can|Has)/', $name ) ) { - $types[$name] = 'toggle'; - } - } - $changes = getFormChanges( $control, $_REQUEST['newControl'], $types, $columns ); - - if ( count( $changes ) ) { - if ( !empty($_REQUEST['cid']) ) { - dbQuery( "update Controls set ".implode( ", ", $changes )." where Id = ?", array($_REQUEST['cid']) ); - } else { - dbQuery( "insert into Controls set ".implode( ", ", $changes ) ); - //$_REQUEST['cid'] = dbInsertId(); - } - $refreshParent = true; - } + //$changes = getFormChanges( $control, $_REQUEST['newControl'], $types, $columns ); + $Control->save( $_REQUEST['newControl'] ); + $refreshParent = true; $view = 'none'; } elseif ( $action == 'delete' ) { if ( isset($_REQUEST['markCids']) ) { From 2864876d28c6cde03d84f11359d3ccbb62ac1479 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sat, 3 Mar 2018 09:25:04 -0800 Subject: [PATCH 5/5] fix spacing, add code comment --- web/includes/Control.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/web/includes/Control.php b/web/includes/Control.php index 343a7b31a..de5f221ff 100644 --- a/web/includes/Control.php +++ b/web/includes/Control.php @@ -204,6 +204,7 @@ private $defaults = array( $this->{$k} = $v; } } + // Set default values foreach ( $this->defaults as $k=>$v ) { if ( ( ! array_key_exists( $k, $this ) ) or ( $this->{$k} == '' ) ) { $this->{$k} = $v; @@ -212,16 +213,17 @@ private $defaults = array( $fields = array_keys( $this->defaults ); -if ( array_key_exists( 'Id', $this ) ) { - $sql = 'UPDATE Controls SET '.implode(', ', array_map( function($field) {return $field.'=?';}, $fields ) ) . ' WHERE Id=?'; - $values = array_map( function($field){return $this->{$field};}, $fields ); - $values[] = $this->{'Id'}; - dbQuery( $sql, $values ); -} else { - $sql = 'INSERT INTO Controls SET '.implode(', ', array_map( function($field) {return $field.'=?';}, $fields ) ) . ''; - $values = array_map( function($field){return $this->{$field};}, $fields ); - dbQuery( $sql, $values ); -} + if ( array_key_exists( 'Id', $this ) ) { + $sql = 'UPDATE Controls SET '.implode(', ', array_map( function($field) {return $field.'=?';}, $fields ) ) . ' WHERE Id=?'; + $values = array_map( function($field){return $this->{$field};}, $fields ); + $values[] = $this->{'Id'}; + dbQuery( $sql, $values ); + } else { + $sql = 'INSERT INTO Controls SET '.implode(', ', array_map( function($field) {return $field.'=?';}, $fields ) ) . ''; + $values = array_map( function($field){return $this->{$field};}, $fields ); + dbQuery( $sql, $values ); + $this->{'Id'} = dbInsertId(); + } } // end function save } // end class Control