Exemple #1
0
function dbase_parse($cmd, $message)
{
    global $pisql, $dbhost, $dbuser, $dbpass, $dbname;
    global $log;
    // Used by daemon
    global $apperr, $appmsg;
    // For ajax usage
    if (!$pisql || !$pisql->ping()) {
        $log->lwrite("dbase_parse:: Ping failed, making new pisql connection to server", 1);
        $pisql = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    }
    // If there is an error
    if ($pisql->connect_errno) {
        $log->lwrite("dbase_parse:: Failed to connect to MySQL: (" . $pisql->connect_errno . ") " . $pisql->connect_error, 1);
        return -1;
    }
    // For logging only
    if (is_array($message)) {
        $str = "";
        foreach ($message as $key => $val) {
            $str .= " {" . $key . ":" . $val . "},";
        }
        $log->lwrite("dbase_parse:: " . $cmd . ": " . $str, 1);
    } else {
        $log->lwrite("dbase_parse:: message: " . $cmd . ": " . $message, 1);
    }
    //
    // Depending on $cmd execute database function
    switch ($cmd) {
        // Database
        case "load_database":
            $ret = load_database();
            break;
            // Device
        // Device
        case "load_devices":
            $ret = load_devices();
            break;
        case "add_device":
            $ret = add_device($message);
            break;
        case "delete_device":
            $ret = delete_device($message);
            break;
        case "store_device":
            $ret = store_device($message);
            break;
            // Room
        // Room
        case "add_room":
            $ret = add_room($message);
            break;
        case "delete_room":
            $ret = delete_room($message);
            break;
            // Scene
        // Scene
        case "read_scene":
            $ret = load_scene($message);
            break;
        case "load_scenes":
            $ret = load_scenes();
            break;
        case "add_scene":
            $ret = add_scene($message);
            break;
        case "delete_scene":
            $ret = delete_scene($message);
            break;
        case "upd_scene":
            $ret = upd_scene($message);
            break;
        case "store_scene":
            $ret = store_scene($message);
            break;
            // Timer
        // Timer
        case "add_timer":
            $ret = add_timer($message);
            break;
        case "delete_timer":
            $ret = delete_timer($message);
            break;
        case "store_timer":
            $ret = store_timer($message);
            break;
            // Handset
        // Handset
        case "add_handset":
            $ret = add_handset($message);
            break;
        case "delete_handset":
            $ret = delete_handset($message);
            break;
        case "store_handset":
            $ret = store_handset($message);
            break;
            // Weather
        // Weather
        case "add_weather":
            $ret = add_weather($message);
            break;
        case "delete_weather":
            $ret = delete_weather($message);
            break;
            // Setting
        // Setting
        case "store_setting":
            $ret = store_setting($message);
            break;
        default:
    }
    if ($ret >= 0) {
        // Prepare structure to send back to the calling ajax client (in stdout)
        $send = array('tcnt' => $ret, 'appmsg' => $appmsg, 'status' => 'OK', 'apperr' => $apperr);
        $output = json_encode($send);
    } else {
        //	Functions need to fill apperr themselves!
        $send = array('tcnt' => $ret, 'appmsg' => $appmsg, 'status' => 'ERR', 'apperr' => $apperr);
        $output = json_encode($send);
    }
    return $output;
}
        break;
    case "delete_handset":
        $apperr .= "Calling delete_handset:: json: " . json_decode($icsmsg) . "\n";
        $tcnt = delete_handset($icsmsg);
        $apperr .= "\nHandset deleted ";
        $ret = $tcnt;
        break;
        // Store the complete settings array at once.
        // There may not be a reason to load settings, as we load setting
        // during init.
    // Store the complete settings array at once.
    // There may not be a reason to load settings, as we load setting
    // during init.
    case "store_setting":
        $apperr .= "Calling store_setting:: icsmsg: {$icsmsg}\n";
        $tcnt = store_setting($icsmsg);
        $apperr .= "\nSetting updated ";
        $ret = $tcnt;
        break;
        // If the command is not defined above, this is treated as a error
    // If the command is not defined above, this is treated as a error
    default:
        $appmsg .= "action: " . $action;
        $apperr .= "\n<br />default, command not recognized: ," . $action . ",\n";
        $ret = -1;
}
if ($ret >= 0) {
    // Prepare structure to send back to the calling ajax client (in stdout)
    $send = array('tcnt' => $ret, 'appmsg' => $appmsg, 'status' => 'OK', 'apperr' => $apperr);
    $output = json_encode($send);
} else {