2017-01-18 7 views
0

私はBluemixでホストされているドッカーコンテナでPHPコードを実行しています。 PHPコードは、MQTTベースの購読コードであるpythonスクリプトを呼び出します。私の考えは、購読されたコードがMQTTメッセージを取得するたびに、値をテキスト・ファイルに書き込むことでした。 PHPコードは、ファイル内の新しい値を10秒ごとにチェックし続けます。 VCAP_ENV変数が正しく書き込まれています。ただし、サイトは読み込まれません。PHP bluemixコンテナでmqtt pythonスクリプトを実行しています

pythonスクリプトは、ローカルで試してみると正常に実行されます。だからそこにエラーもない。次のように 私のコードは次のとおりです。

PHPコード:

<?php 

if(getenv("VCAP_SERVICES")) { 


// get IoT service configuration from Bluemix 


$services = getenv("VCAP_SERVICES"); 
$services_json = json_decode($services, true); 

$mysql_config = $services_json["iotf-service"][0]["credentials"]; 

$org_id = $mysql_config["org"]; 


$port = $mysql_config["mqtt_u_port"]; 


$username = $mysql_config["apiKey"]; 


$password = $mysql_config["apiToken"]; 


} 

// set configuration values 
$config = array(
    'org_id' => $org_id, 
    'port' => $port, 
    'app_id' => 'mymqttfinalservice', 
    'iotf_api_key' => $username, 
    'iotf_api_secret' => $password, 
    'device_id' => '007', 
    'qos' => 1 

); 

$file = fopen("VCAP_CONFIG.ini","w"); 


#fwrite($file,"[config]" . PHP_EOL); 
#fwrite($file,"org =" . $org_id . PHP_EOL); 
#fwrite($file,"apikey =" . $username . PHP_EOL); 
#fwrite($file,"authkey =" . $password . PHP_EOL); 

fwrite($file,"[config]" . "\n"); 
fwrite($file,"org =" . $org_id . "\n"); 
fwrite($file,"apikey =" . $username . "\n"); 
fwrite($file,"authkey =" . $password . "\n"); 


fclose($file); 

$file = file_get_contents('VCAP_CONFIG.ini', true); 
echo $file; 



$command = 'chmod 777 /app/PythonSubscribeCode.py'; 
$output = ''; 
exec ($command); 



$command = 'python3 /app/PythonSubscribeCode.py 2>&1'; 
$output = exec ($command); 
print_r($output); 


$x = 1; 
while($x == 1) 
{ 
    $config = parse_ini_file('Data.ini'); 
    echo json_encode($config); 
    sleep(5); 
} 

?> 

のPythonスクリプト:

# -*- coding: utf-8 -*- 

#!/usr/bin/env python3 

import paho.mqtt.client as mqtt 
import os, json 
import time 
import configparser 

# This is the Subscriber 

settings = configparser.ConfigParser() 
settings.read('VCAP_CONFIG.ini') 

organization = settings['config']['org'] 
username = settings['config']['apikey'] 
password = settings['config']['authkey'] 


#Set the variables for connecting to the iot service 
broker = "" 
devicename = "007" 
topic = "iot-2/type/DesktopApplication/id/007/evt/status/fmt/json" 
#topic = 'iot-2/evt/status/fmt/json' 
deviceType = "DesktopApplication" 

clientID = "a:" + organization + ":appId" 

print (clientID) 

broker = organization + ".messaging.internetofthings.ibmcloud.com" 
mqttc = mqtt.Client(clientID) 

if username is not "": 
mqttc.username_pw_set(username, password=password) 


def on_connect(client, userdata, flags, rc): 
print("Connected with result code "+str(rc)) 

def on_subscribe(mosq, obj, mid, granted_qos): 
print("Subscribed: " + str(mid) + " " + str(granted_qos)) 


def on_message(mosq, obj, msg): 
    global message 
    print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) 

def on_message(client, userdata, msg): 
    writeData = msg.payload.decode('utf-8') 
    parsed_json = json.loads(writeData) 
    UDD = parsed_json['UDD'] 
    DDD = parsed_json['DDD'] 
    PH = parsed_json['PH'] 
    Ignition = parsed_json['Ignition'] 

    # add the settings to the structure of the file, and lets write it out... 
    config = configparser.ConfigParser() 
    config['Data'] = {'UDD': UDD, 
         'DDD': DDD, 
         'PH': PH, 
        'Ignition':Ignition} 
    with open('Data.ini', 'w') as configfile: 
     config.write(configfile) 

mqttc.connect(host=broker, port=1883, keepalive=60) 
test = mqttc.subscribe(topic,0) 

#print (test) 
mqttc.on_connect = on_connect 
mqttc.on_subscribe = on_subscribe 
mqttc.on_message = on_message 

mqttc.loop_forever() 

誰かがこの上で案内していただけますか?

+0

私は、PHPビルドパックにはPythonインタプリタが含まれていないか、またはMQTTモジュールをインストールする方法がないと仮定します。 – hardillb

+0

@hardlibこれはPythonインタプリタを含んでいます。ある場合には他のエラーを出す。また、PHP用のmqttコードを試してみるとうまくいきます。しかし、私はリフレッシュするWebページを維持したくないので、私はPythonの実装を選択しました。 – Sid411

+0

@hardillb Pythonスクリプトは購読コードなので、実行を終了することはないので、コントロールはPHPコードに戻ってくることはありませんか? – Sid411

答えて

0

cf ic logs containeridから有用なエラーメッセージが表示されたら、何が失敗しているのか確認してください。別の方法として、コンテナ(cf ic exec -ti containerid bashまたはdocker exec...のいずれか)を実行してPythonスクリプトを直接実行して、そこで実行中に他のエラーが発生するかどうかを確認することもできます。

関連する問題