ソフトウェア本体のクラスです。
<注意>
CONSUMER_KEYとCONSUMER_SECRETは、 ***********************としていますが、
実際はtwiterサイトにアプリを登録した時に発行される情報です。
悪用を防ぐためここでは隠しています。
using System;
using System.Windows.Forms;
using System.Timers;
using MSFSProcess;
using MySetting;
using MyTwitter;
using FSUIPC;
namespace WindowsFormsApplication1
{
/// <summary>
/// メインフォームクラス
/// </summary>
public partial class MainForm : Form
{
/// <summary>
/// 自動ポスト用タイマー変数
/// </summary>
private System.Timers.Timer aTimer;
/// <summary>
/// 入力されたPINを格納する変数
/// </summary>
public string mypin;
/// <summary>
/// twitterへのポストを処理するクラス変数
/// </summary>
private MyTwitterProcess my_TwitterProcess;
/// <summary>
/// 設定ファイルの入出力を扱うクラス変数
/// </summary>
public MySettingProcess MySetting;
/// <summary>
/// MSFSとの処理を扱うクラス変数
/// </summary>
public MyMSFSProcess my_MSFSProcess;
/// <summary>
/// 経過時間カウント用変数[単位:sec]
/// </summary>
private int interval_sec=0;
/// <summary>
/// Twitter API使用アプリケーションのアプリケーションコード
/// </summary>
const string CONSUMER_KEY = ***********************
const string CONSUMER_SECRET = ***********************
//
/// <summary>
/// デフォルトコンストラクタ(オーバーロード)
///
/// 初期化する。
/// 各処理クラス変数を生成する。
/// OAuth実施状況を確認する。
/// OAuth未実施の場合が、OAuth実施ボタンを活性化する。
/// OAuth実施済の場合、設定ファイルから情報を読み込みtwitterとの通信を確立する。
/// 設定ファイルから情報を読み込み、フォームの初期化を続ける。
/// twitterにポストする予定の文字列の長さを表示する。
/// </summary>
/// <remarks>
/// ######################################################
/// Ver Data Auther Description
/// 1.0 11/6/17 ADO131 初版
/// </remarks>
public MainForm()
{
//Formの初期化
InitializeComponent();
//Twitter処理クラス生成
my_TwitterProcess = new MyTwitterProcess(this, CONSUMER_KEY, CONSUMER_SECRET);
//設定ファイル処理クラス生成
MySetting = new MySettingProcess(CONSUMER_KEY, CONSUMER_SECRET);
//MSFSとの処理を扱うクラス生成
my_MSFSProcess = new MyMSFSProcess();
//デフォルトが空の場合
if (string.IsNullOrEmpty(MySetting.getAccessToken()))
{
//初回なのでOAuth実行ボタンを有効化
button_OAuth.Enabled = true;
}
//OAuth済みなので
else
{
// 設定ファイルから読み込む
my_TwitterProcess.readSetting();
//Postボタンを有効化
button_twitter_manual.Enabled = true;
button_auto_post_start.Enabled = true;
}
//設定読み込み
loadSetting();
//Post予定文字列の長さを表示
DisplayPostLength();
}
/// <summary>
/// OAuth実施ボタン押下イベント処理関数
///
/// Twitter処理クラスのOAuth処理関数を実行する。
/// twitterポスト開始ボタン(自動/手動)を活性化する。
/// OAuth実施ボタンを不活性化する。
/// </summary>
/// <remarks>
/// ######################################################
/// Ver Data Auther Description
/// 1.0 11/6/17 ADO131 初版
/// </remarks>
/// <param name="sender">不使用</param>
/// <param name="e">不使用</param>
private void button1_Click(object sender, EventArgs e)
{
//auth実行
my_TwitterProcess.MyAuth();
//ボタン活性化
button_twitter_manual.Enabled = true;
button_auto_post_start.Enabled = true;
//ボタン不活性化
button_OAuth.Enabled = false;
}
/// <summary>
/// 手動twitterポストボタン押下イベント処理関数
///
/// 現在の設定状況を保存する。
/// 現在の設定を再度読み込む。
/// MSFS処理クラスの接続開始関数を呼ぶ。
/// twitter処理クラスの、post文字列生成関数を呼ぶ。
/// twitter処理クラスの、post実行関数を呼ぶ。
/// </summary>
/// <remarks>
/// ######################################################
/// Ver Data Auther Description
/// 1.0 11/6/17 ADO131 初版
/// </remarks>
/// <param name="sender">不使用</param>
/// <param name="e">不使用</param>
private void button4_Click(object sender, EventArgs e)
{
//現状を保存
saveSetting();
//最新情報をロード
loadSetting();
//MSFSに接続
my_MSFSProcess.MyMSFSConnect();
//twitterポスト用文字列
my_TwitterProcess.makePostString(checkBox_Geotag.Checked, checkBox_PostAltitude.Checked, textBox_imakoko.Text, textBox_manual_header.Text, textBox_manual_footer.Text);
//Twitterポスト
my_TwitterProcess.MyTwitterPost();
}
/// <summary>
/// MSFSに接続ボタン押下イベント処理関数
///
/// NSFS処理クラスの、MSFS接続関数を呼ぶ。
/// 戻り値から、初回接続であったかどうか判定する。
/// 初回接続だった場合、接続に成功したことを表示する。
/// 位置情報取得ボタンを活性化する。
/// MSFS接続ボタンを不活性化する。
/// </summary>
/// <remarks>
/// ######################################################
/// Ver Data Auther Description
/// 1.0 11/6/17 ADO131 初版
/// </remarks>
/// <param name="sender">不使用</param>
/// <param name="e">不使用</param>
private void button2_Click(object sender, EventArgs e)
{
/*
* MSFSに接続
*/
//初回だった場合
if (my_MSFSProcess.MyMSFSConnect() != false)
{
//メッセージ表示
MessageBox.Show("MSFSと接続成功!");
//位置情報取得ボタン活性化
button3.Enabled = true;
//MSFS接続ボタンを不活性化
button_MSFS_connect.Enabled = false;
}
//既に接続済
else
{
//メッセージ表示
MessageBox.Show("既に接続しています");
}
}
/// <summary>
/// フォームクローズイベント処理関数
///
/// MSFS処理クラスの、接続クローズ関数を呼ぶ。
/// 現在の設定状況を保存する。
/// アプリケーションを終了する。
/// </summary>
/// <remarks>
/// ######################################################
/// Ver Data Auther Description
/// 1.0 11/6/17 ADO131 初版
/// </remarks>
/// <param name="sender">不使用</param>
/// <param name="e">不使用</param>
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//FSUIPCクローズ
my_MSFSProcess.CloseMSFSConnect();
//設定を保存
saveSetting();
//アプリケーション終了
Application.Exit();
}
/// <summary>
/// 位置情報取得ボタン押下イベント処理関数
///
/// MSFS処理クラスの、場所取得関数を呼ぶ。
/// 緯度、経度を表示する。
/// </summary>
/// <remarks>
/// ######################################################
/// Ver Data Auther Description
/// 1.0 11/6/17 ADO131 初版
/// </remarks>
/// <param name="sender">不使用</param>
/// <param name="e">不使用</param>
private void button3_Click(object sender, EventArgs e)
{
//場所を取得する
my_MSFSProcess.getLocations(false);
//緯度、経度表示
MessageBox.Show("緯度は" + my_MSFSProcess.getLatPosition_Post() + " , 経度は" + my_MSFSProcess.getLongPosition_Post());
}
/// <summary>
/// 自動ポスト開始
///
///
/// </summary>
/// <remarks>
/// ######################################################
/// Ver Data Auther Description
/// 1.0 11/6/17 ADO131 初版
/// </remarks>
/// <param name="sender">不使用</param>
/// <param name="e">不使用</param>
private void button_auto_post_start_Click(object sender, EventArgs e)
{
//変数
DialogResult myresult;
//現状を保存
saveSetting();
//最新情報をロード
loadSetting();
//Twitterポスト用をシステムタイマーで初期化
aTimer = new System.Timers.Timer();
//開始するかダイヤログ表示
myresult = MessageBox.Show("twitterへのpost準備が整ったら、OKボタンを押してください。", "", MessageBoxButtons.OKCancel);
//開始する場合
if ( myresult == DialogResult.OK)
{
//入力欄不活性化
textBox_post_interval.Enabled = false;
checkBox_PostAltitude.Enabled = false;
checkBox_Geotag.Enabled = false;
textBox_imakoko.Enabled = false;
//MSFSに接続
my_MSFSProcess.MyMSFSConnect();
//初回表示
//場所取得
my_TwitterProcess.makePostString(checkBox_Geotag.Checked, checkBox_PostAltitude.Checked, textBox_imakoko.Text, textBox_manual_header.Text, textBox_manual_footer.Text);
//Twitterポスト
my_TwitterProcess.MyTwitterPost();
//ボタン設定
button_auto_post_stop.Enabled = true;
button_auto_post_start.Enabled = false;
/*設定間隔繰り返し*/
//時間間隔(単位がミリ秒なので1000倍)
aTimer.Interval = int.Parse(textBox_post_interval.Text) * 1000;
//Twitterポスト用タイマー開始
aTimer.Start();
//カウントダウン表示用タイマー開始
timer_NextDisp.Start();
//Twitterポスト用タイマーのイベントコール
aTimer.Elapsed += new ElapsedEventHandler(OnTimeEvent);
// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);
GC.KeepAlive(timer_NextDisp);
}
//自動ポストキャンセル
else
{
//メッセージ表示
MessageBox.Show("自動Post開始をキャンセルします。");
}
}
//自動ポスト中断
private void button_auto_post_stop_Click(object sender, EventArgs e)
{
//Twitterポスト用タイマー停止
aTimer.Stop();
//カウントダウン用タイマー停止
timer_NextDisp.Stop();
//経過秒数初期化
interval_sec = 0;
//ボタン設定
button_auto_post_stop.Enabled = false;
button_auto_post_start.Enabled = true;
//入力欄活性化
textBox_post_interval.Enabled = true;
checkBox_PostAltitude.Enabled = true;
checkBox_Geotag.Enabled = true;
textBox_imakoko.Enabled = true;
}
//twitterポストを発生させるイベント関数
private void OnTimeEvent(object source, ElapsedEventArgs e)
{
//場所取得
my_TwitterProcess.makePostString(checkBox_Geotag.Checked, checkBox_PostAltitude.Checked, textBox_imakoko.Text, textBox_manual_header.Text, textBox_manual_footer.Text);
//Twitterポスト
my_TwitterProcess.MyTwitterPost();
//経過秒数初期化
interval_sec = 0;
}
//post予定文字数を表示させる関数
private void DisplayPostLength()
{
//MSFSに接続
my_MSFSProcess.MyMSFSConnect();
//twitterポスト用文字列
my_TwitterProcess.makePostString(checkBox_Geotag.Checked, checkBox_PostAltitude.Checked, textBox_imakoko.Text, textBox_manual_header.Text, textBox_manual_footer.Text);
//文字数をいれこむ
label_post_string_num_auto.Text = string.Format("{0}", my_TwitterProcess.getPostLength());
label_post_string_num_manual.Text = label_post_string_num_auto.Text;
}
//手動ポストのヘッダーが変更されたとき
private void textBox_manual_header_TextChanged(object sender, EventArgs e)
{
//自動ポストのヘッダー欄にコピーする
textBox_auto_header.Text = textBox_manual_header.Text;
//Post予定文字列の長さを表示
DisplayPostLength();
}
//手動ポストのフッターが変更されたとき
private void textBox_manual_footer_TextChanged(object sender, EventArgs e)
{
//自動ポストのフッター欄にコピーする
textBox_auto_footer.Text = textBox_manual_footer.Text;
//Post予定文字列の長さを表示
DisplayPostLength();
}
//自動ポストのヘッダーが変更されたとき
private void textBox_auto_header_TextChanged(object sender, EventArgs e)
{
//手動ポストのフッター欄にコピーする
textBox_manual_header.Text = textBox_auto_header.Text;
//Post予定文字列の長さを表示
DisplayPostLength();
}
//自動ポストのフッターが変更されたとき
private void textBox_auto_footer_TextChanged(object sender, EventArgs e)
{
//手動ポストのフッター欄にコピーする
textBox_manual_footer.Text = textBox_auto_footer.Text;
//Post予定文字列の長さを表示
DisplayPostLength();
}
//MSFS設定タブがクリックされたとき
private void tabPage_MSFS_set_Click(object sender, EventArgs e)
{
//2回目接続だった+button3が不活性の場合
if (my_MSFSProcess.MyMSFSConnect() == false && button3.Enabled == false)
{
//ボタンを活性化する
button3.Enabled = true;
//MSFS接続ボタンを不活性化
button_MSFS_connect.Enabled = false;
}
}
//カウントダウン表示用関数
private void timer_NextDisp_Tick(object sender, EventArgs e)
{
//ポストインターバル時間変数(整数)
int PostTime;
//経過秒数カウント
interval_sec++;
//ポストインターバルを整数に変換
PostTime = int.Parse( MySetting.getTwitterPostInterval() );
//算出した結果を表示ラベルにセット
label_display_sec.Text = string.Format("{0}", PostTime - interval_sec);
}
//設定ファイルからの一括読み込み
private void loadSetting()
{
//Twitterポスト間隔をセット
textBox_post_interval.Text = MySetting.getTwitterPostInterval();
//ソフトウェアバージョンセット
label_software_version.Text = MySetting.getSoftwareVersion();
//ヘッダーセット
textBox_manual_header.Text = MySetting.getTwitterHeader();
//フッターセット
textBox_manual_footer.Text = MySetting.getTwitterFooter();
//高度情報ポスト設定をセット
checkBox_PostAltitude.Checked = MySetting.getPostAltitude();
//今ここ接頭辞をセット
textBox_imakoko.Text = MySetting.getImakoko();
//ジオタグを含める設定をセット
checkBox_Geotag.Checked = MySetting.getIncludeGeotag();
//表示間隔をセット[sec]
timer_NextDisp.Interval = int.Parse(MySetting.getDisplayInterval()) * 1000;
}
//設定ファイルへの一括保存準備
private void saveSetting()
{
//twitterヘッダー保存
MySetting.setTwitterHeader(textBox_manual_header.Text);
//twiterフッター保存
MySetting.setTwitterFooter(textBox_manual_footer.Text);
//Post間隔保存
MySetting.setTwitterPostInterval(textBox_post_interval.Text);
//高度ポスト情報保存
MySetting.setPostAltitude(checkBox_PostAltitude.Checked);
//今ここ接頭辞保存
MySetting.setImakoko(textBox_imakoko.Text);
//ジオタグを含める設定を保存
MySetting.setIncludeGeotag(checkBox_Geotag.Checked);
//ファイルに保存
MySetting.save();
}
//数字以外入力させない様に制御する関数
private void textBox_post_interval_KeyPress(object sender, KeyPressEventArgs e)
{
//入力が0から9までか?
if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
{
//イベントを処理する
e.Handled = true;
}
}
}
}