pihome-shc / pihome

PiHome - Smart Heating, Ventilation and Air Conditioning (HVAC)
http://www.pihome.eu
Other
55 stars 25 forks source link

Zone - Add - message_out not updating/populating #309

Closed dvdcut closed 4 years ago

dvdcut commented 4 years ago

when adding zone it wouldn't add message_out record. i think this is the fix for adding record but when i modify it add another one rather then editing exiting record for same zone.

    //check if Controller id already exist in message_out table
    $query = "SELECT * FROM messages_out WHERE node_id = '{$controler}' AND child_id = '{$controler_child_id}' AND zone_id = '{$zone_id}' LIMIT 1;";
    $result = $conn->query($query);
    if (mysqli_num_rows($result) ==0){
        //Add Zone to message out table at same time to send out instructions to controller for each zone.
        if ($node_id !=0 OR $node_id !='0'){
            $query = "INSERT INTO `messages_out` (`sync`, `purge`, `node_id`, `child_id`, `sub_type`, `ack`, `type`, `payload`, `sent`, `datetime`, `zone_id`) VALUES ('0', '0', '{$controler}','{$controler_child_id}', '1', '1', '2', '0', '0', '{$date_time}', '{$zone_id}');";
            $result = $conn->query($query);
            if ($result) {
                $message_success .= "<p>".$lang['zone_controler_success']."</p>";
            } else {
                $error .= "<p>".$lang['zone_controler_fail']."</p> <p>" .mysqli_error($conn). "</p>";
            }
        }
    } 
twa127 commented 4 years ago

Hi,

I can't see any code for updating the record if in edit mode, I guess it would need a test of if ($id==0) then INSERT else UPDATE

dvdcut commented 4 years ago

dont know if this ever worked since edit/add was merged.

twa127 commented 4 years ago

you're probably right

pihome-shc commented 4 years ago

that is serious bug, if some one is trying to install this it is not going to work for end user. but adding zone always worked even after add/edit merge.

dvdcut commented 4 years ago

311 is half fix, i.e it will add records to message_out table but on edit it still create new record

twa127 commented 4 years ago

to be clear -

  1. add zone is working okay
  2. edit zone is working okay
  3. issue is when editing an existing zone and changing Zone Controller ID (and Zone Relay Controller's Child ID) the existing message_out table record is not being updated

Is that the correct interpretation ?

pihome-shc commented 4 years ago

@twa127 did you test #311 or it was working before?

dvdcut commented 4 years ago

@twa127 for me, this morning, when i add zone, it wouldn't add record to message_out table at all, now after #311 it add record to message_out table and when i edit zone record it does not edit exiting record from message_out table but add another one.

twa127 commented 4 years ago

I think #311 fixes the issue of creating duplicate records in the messages_out table be changing the test for no record from if ($result) { to if (mysqli_num_rows($result) ==0){

twa127 commented 4 years ago

I think when editing we need to capture the 'id' of the messages_out record linked to the zone and then update it on save, the INSERT statement is only required when adding a new zone

dvdcut commented 4 years ago

editing issue is still there, query to search for exiting record is all wrong, it is searing for already submitted record in message_out

$query = "SELECT * FROM messages_out WHERE node_id = '{$controler}' AND child_id = '{$controler_child_id}' AND zone_id = '{$zone_id}' LIMIT 1;";

twa127 commented 4 years ago

as I read it that query can be used to get the 'id' of the existing messages_out recort which must exist if we are editing and can be used for an update query, will not be needed if in add zone mode where we must insert a new messages_out record

twa127 commented 4 years ago

the select query would need to be moved to the section where we load the data if in edit mode

I'm happy to provide an update if required :-)

dvdcut commented 4 years ago

@twa127 that would be wonderful if you can do it

twa127 commented 4 years ago

okay, let you know

twa127 commented 4 years ago

committed a fix - checks if in add or edit mode, if add then insert new message_out, if edit then capture the id of the existing message_out in a hidden input field and use this to update the existing message_out

twa127 commented 4 years ago

tidied up the record INSERT/UPDATE success fail messages

pihome-shc commented 4 years ago

@twa127 thank you for fixing this.

pihome-shc commented 4 years ago

@twa127 zone.php have error on line 104

$query = "INSERT INTOmessages_out(sync,purge,node_id,child_id,sub_type,ack,type,payload,sent,datetime,zone_id) VALUES ('0', '0',$