dustsensor.ino

dustsensorv2.ino



첨부된 2 가지  케이스로 코딩됨.

미세먼지 측정센서로 측정된 미세먼지 농도를 
16x2 LCD 에 표현하기.


아두이노 코딩도 계속하다 보니 자연스럽게 되는것 같다. 전자회로도 또한 친숙해지는것 같고.
연습이 완벽하게 만든다고 했나? 생각보다 쉽게 만들어짐에 놀라웠다.

Practice makes perfect, thanks.


#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int Vo = A0;
int V_LED = 2;

float Vo_value = 0;
float Voltage = 0;
float dustDensity = 0;
float dustCleanVoltage = 0.44;
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;

void setup(){
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("dust");
Serial.begin(9600);

/*digital output:*/
pinMode(V_LED, OUTPUT);

/*analog input:*/
pinMode(Vo, INPUT);
}

/** references :
* http://blog.naver.com/PostView.nhn?blogId=darknisia&logNo=221222455928
* http://arduinodev.woofex.net/2012/12/01/standalone-sharp-dust-sensor/
* */
// 미세먼지 센서(GP2Y1010AU0F)
// : 적외선 발광 다이오드(IRED)와 포토다이오드가 대각선으로 배치되어 공기 중 먼지의 반사광을 감지
void loop(){
digitalWrite(V_LED,LOW); //ired 'on'
delayMicroseconds(samplingTime);
Vo_value = analogRead(Vo); //read the dust value
delayMicroseconds(deltaTime);// pulse width 0.32 - 0.28 = 0.04 msec
//0.32 msec의 펄스폭을 유지하기 위해 필요한 코드입니다

digitalWrite(V_LED,HIGH); //ired 'off'
delayMicroseconds(sleepTime);


/*
이 센서의 출력전압(Vo)는 먼지가 없을 때의 출력전압(Voc)과 먼지 농도(ΔV)에 비례하여 출력됩니다.
다시 표현하면 ΔV = Vo – Voc
미세먼지 농도[µg/m³] = (Vo – Voc) / 0.005
*/
Voltage = Vo_value * (5.0 / 1024.0);

dustDensity = (Voltage - dustCleanVoltage)/0.005;

Serial.print(" Raw Signal Value (0-1023):");
Serial.print(Vo_value);
Serial.print(" | Volatage:");
Serial.print(Voltage);
Serial.print(" | Dust Density:");
Serial.print(dustDensity);
Serial.print("[ug/m3]");
if(dustDensity<31){
Serial.println(" => [Great(~30)]");
} else if(dustDensity>30 && dustDensity<81){
Serial.println(" => [Good(31~80)]");
} else if(dustDensity>81 && dustDensity<151){
Serial.println(" => [Bad(81~150)]");
} else{
Serial.println(" => [Too Bad(151~)]");
}


// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print(dustDensity);
lcd.setCursor(0, 1);
if(dustDensity<31){
lcd.print(": Great(~30)");
} else if(dustDensity>30 && dustDensity<81){
lcd.print(": Good(31~80)");
} else if(dustDensity>81 && dustDensity<151){
lcd.print(": Bad(81~150)");
} else{
lcd.print(": Too Bad(151~)");
}
delay(5000);
}


 

DIY-공기청정기

저렴하게 만들수 있다는 장점이 있지만 뭔가 허접. 저처럼 3D프린트기가 없다면.

 

장비 : 자동차 에어컨필터(6천원~15천원), 테이프(1천원), 쿨러팬(죽어버린데스크탑의 팬 재사용), 샤오미보조베터리, 아두이노

 

여기서 아두이노를 사용하는건 계속적으로 전력을 공급하기 위해서 그냥 사용할뿐.

이것 사용안하면 10초후에 보조베터리에서 전력이 끊어짐.

추가로 미세먼지 센서나 led로 측정값에 대한 이벤트를 주는것도 가능하겠음

미세먼지 센서로 측정하게되면 업데이트하는걸로.


데스크탑에서 뺀 쿨러 4pin이다.

노란색선(+), 그 옆 검정색선(-)



그럭저럭^^ 그런데 바람이 좀 많이 약하다.  DIY는 자기 만족이니까. 난 만족한다.

이것 선풍기로 만드는것도 좋겠는데^^.


자바스크립트로 fb API연동하여 로그인하기



그래프 API 탐색기 이용하기

url : https://developers.facebook.com/tools-and-support 



appid 에서 가져올 권한 선택추가 선택


default : id, name 

추가로 가져올 정보 : email






위 api 탐색기로 선 테스트 후 

아래처럼 javascript로 코드작성.

참고url : https://developers.facebook.com/docs/facebook-login/web#logindialog



<<html code>>


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Page Title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!--
reference to : https://developers.facebook.com/tools-and-support
-->
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '2028791704043595',
     cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse social plugins on this page
version : 'v3.0' // use graph api version 2.8
});
    FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};

(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

   /*
     콜백에 제공되는 response 개체에는 다음과 같은 여러 필드가 포함되어 있습니다.
    {
        status: 'connected',
        authResponse: {
            accessToken: '...',
            expiresIn:'...',
            signedRequest:'...',
            userID:'...'
        }
    }코드 복사
    status는 앱 사용자의 로그인 상태를 지정합니다. 상태는 다음 중 하나일 수 있습니다.
    connected - 사용자가 Facebook에 로그인하고 앱에 로그인했습니다.
    not_authorized - 사용자가 Facebook에는 로그인했지만 앱에는 로그인하지 않았습니다.
    unknown - 사용자가 Facebook에 로그인하지 않았으므로 사용자가 앱에 로그인했거나 FB.logout()이 호출되었는지 알 수 없어,
Facebook에 연결할 수 없습니다.
    connected 상태인 경우 authResponse가 포함되며 다음과 같이 구성되어 있습니다.
    accessToken - 앱 사용자의 액세스 토큰이 포함되어 있습니다.
    expiresIn - 토큰이 만료되어 갱신해야 하는 UNIX 시간을 표시합니다.
    signedRequest - 앱 사용자에 대한 정보를 포함하는 서명된 매개변수입니다.
    userID - 앱 사용자의 ID입니다.
    앱에서 앱 사용자의 로그인 상태를 알게 되면 다음 중 하나를 수행할 수 있습니다.
    사용자가 Facebook과 앱에 로그인한 경우 앱의 로그인된 환경으로 리디렉션됩니다.
    사용자가 앱에 로그인하지 않았거나 Facebook에 로그인하지 않은 경우 FB.login()을 사용하여 로그인 대화 상자에 메시지를
표시하거나 로그인 버튼을 표시합니다.
        */
   function statusChangeCallback(response) {
    
      console.log('statusChangeCallback');

      if (response.status === 'connected') {
        console.log(response.authResponse.accessToken);
        // Logged into your app and Facebook.
        testAPI();
      } else if (response.status === 'not_authorized') {
        document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
      } else {
        document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.';
      }

  }

function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
  
function testAPI() {
  console.log('Welcome! Fetching your information.... ');
  FB.api(
    '/me',
    {"fields":"id,name,email"},
    function(response) {
       // Insert your code here
      console.log('Successful login for: ' + response.name);

      document.getElementById('status').innerHTML = JSON.stringify(response);
    }
  );
}

function fblogout(){

  FB.logout(function(response) {
    window.location.reload();
  });
}

</script>

<fb:login-button
scope="public_profile,email"
onlogin="checkLoginState();">
</fb:login-button>

<input type="button" onclick="javascript:fblogout();" value ="로그아웃" />
<div id="status"/>

</body>
</html>


로그인페이지>>


로그인후 fb의 개인정보 받아 display



로그인은 잘 되나, 로그아웃은 아래와 같은 에러내용으로 console log를 찍고 로그아웃이 안되고 있음. 

유투브 동영상등 찾아보고 진행했지만 해결못함.


Refused to display 'https://www.facebook.com/home.php' in a frame because it set 'X-Frame-Options' to 'deny'.


'프로그래밍 > PHP' 카테고리의 다른 글

curl kakao open api사용하기  (0) 2018.08.05
php CURL 사용하기  (0) 2018.07.31
php 패스워드 암호화 bcript  (0) 2018.05.02
unlink(=delete) 메소드 이용하기  (0) 2018.04.22
directory entry 읽어 파일리스트업  (0) 2018.04.20

스크린스크래핑

WebBrowser이용해서 website에 렌더링된 html의 특정값을 추출한다.

예를 들어 특정 사이트 모 카테고리에 있는 내용을 모두 가져와서 데이터화하고 그것을 분석하고 싶을 때 스크래핑을 사용하면 편리하다.

 

아래 code

 

특정 카테고리의의 글list를 통해 key, title을 추출하고

Key를 이용해 본문내용의 text를 가져와서 db화 한 내용이다.

글의 pattern등 분석자료로 사용할 목적으로 데이터가 필요하나 해당사이트에서 DB를공개하진 않으니 이렇게 스크랩핑해서 자료화해서 사용하는 것이다.

이렇게 하지 않으면 사이트의 모든 사이트 내용을 클릭~클릭~클릭~ 하겠지.

 

이렇게 프로그램하기 전에 사전에 해당 사이트를 분석하고 맞게 프로그래밍하면 된다.


step1) 카테고리의의 글list를 통해 key, title을 추출 : webSiteContentsList();

 => 추출된 data를 수작업으로 insert스크립트만들어 TMP_HELP 에 insert함.

step2) Key를 이용해 본문내용의 text를 가져와서 db화 한 내용 : webSiteContentsDetail(oBrowser);


<c# code>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;

namespace ScrappingWinForms
{
public partial class Form1 : Form
{

int iq = 0;
string contentsKey = "";

string sConnectionString = "Server=localhost;contentsKey=root;Pwd=???;Database=??????";
string sContentsUrl = "http://www.doitforyou.co.kr/sread.html?key="; //fake url임
string sListUrl = "http://www.doitforyou.co.kr/sub.html?&sec=cc&section=culture&page="; //fake url임

String Url = string.Empty;

public Form1()
{
InitializeComponent();

//WebBrowser로 사이트 로딩시 자바스크립트 바인딩안되어 에러나는 경우.
//자바스크립트 오류(dialogbox) 무시하고 진행가능하도록 제어함.
this.webBrowserViewer.ScriptErrorsSuppressed = true;
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void btnSearch_Click(object sender, EventArgs e)
{
// surftheWeb();
if (iq < 200)
{
selectContentsKeyfromWebsite();
}
}

#region surftheWeb :WebBrowser에 SitePage 브라우징한다.
private void surftheWeb()
{
Url = txtUrl.Text;
webBrowserViewer.Navigate(Url);
}

private void surftheWeb(String sUrl)
{
webBrowserViewer.Navigate(sUrl);
}

#endregion


#region Select / Update from the WebSite

private void selectContentsKeyfromWebsite()
{
MySqlConnection oConnection = new MySqlConnection(sConnectionString);
oConnection.Open();

try
{
MySqlCommand cmd = oConnection.CreateCommand();
cmd.CommandText = "SELECT HELP_TOPIC_ID FROM TMP_HELP WHERE HELP_TOPIC_ID >= 100 AND DESCRIPTION = '' LIMIT 1";
MySqlDataAdapter aDap = new MySqlDataAdapter(cmd);
DataSet dsData = new DataSet();
aDap.Fill(dsData);
if (contentsKey != dsData.Tables[0].Rows[0][0].ToString())
{
contentsKey = dsData.Tables[0].Rows[0][0].ToString();
String sUrl = String.Empty;
sUrl = sContentsUrl + contentsKey;
surftheWeb(sUrl);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (oConnection.State == ConnectionState.Open)
{
oConnection.Close();
}
}
}
/// <summary>
/// key값으로 contents내용을 업데이트 한다.
/// </summary>
/// <param name="sHelp_topic_id"></param>
/// <param name="sDescription"></param>
private void updateContentonIldaro(String sHelp_topic_id, String sDescription)
{
MySqlConnection oConnection = new MySqlConnection(sConnectionString);
oConnection.Open();

try
{
MySqlCommand cmd = oConnection.CreateCommand();
cmd.CommandText = "UPDATE tmp_help set description = @description where help_topic_id = @help_topic_id";
cmd.Parameters.AddWithValue("@help_topic_id", int.Parse(sHelp_topic_id));
cmd.Parameters.AddWithValue("@description", sDescription);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (oConnection.State == ConnectionState.Open)
{
oConnection.Close();

//key를 다시 조회한다.
btnSearch_Click(null, null);
}
}
}
#endregion

/// <summary>
/// WebBrowser's DocumentCompleted Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void webBrowserViewer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser oBrowser = sender as WebBrowser;
//txtSource.Text = StreamConvertEUCKRtoUTF8(oBrowser);

this.txtUrl.Text = oBrowser.Url.AbsoluteUri;

//webSiteContentsList();
webSiteContentsDetail(oBrowser);
}

private void webSiteContentsDetail(WebBrowser oBrowser)
{
iq++; //반복수행위한 카운팅
getWebSiteContentsDetail(oBrowser);
}

/// <summary>
/// 게시판 글list, Paging있음
/// </summary>
private void webSiteContentsList() {

iq++; //paging 숫자

getData();

if (iq < 21)
{
String sUrl = sListUrl + iq.ToString();
surftheWeb(sUrl);
}
}

/// <summary>
/// 1.WebBrowser로 rendering된 html을 TagName으로 HtmlElementCollection에 담는다.
/// 2.HtmlElementCollection => HtmlElement에 담긴 InnerHtml분석한다.
/// 3.HtmlElement[]에 추출하려는 대상을 담는다.
/// </summary>
/// <param name="wb"></param>
/// <param name="tagName"></param>
/// <returns></returns>
private HtmlElement[] GetElementsByTagName(WebBrowser wb, string tagName)
{
var l = new List<HtmlElement>();

var els = wb.Document.GetElementsByTagName(tagName); // all elems with tag
foreach (HtmlElement el in els)
{
if (el.InnerHtml == null)
{
continue;
}

if(el.InnerHtml.StartsWith("<A href=\"read.html"))
{
l.Add(el);
}
}

return l.ToArray();
}


/// <summary>
/// 1.WebBrowser로 rendering된 html을 TagName으로 HtmlElementCollection에 담는다.
/// 2.HtmlElementCollection => HtmlElement에 담긴 요소들중 className을 이용해서 추출하려는 대상을 뽑는다..
/// 3.HtmlElement[]에 추출하려는 대상을 담는다.
/// </summary>
/// <param name="wb"></param>
/// <param name="tagName"></param>
/// <param name="className"></param>
/// <returns></returns>
private HtmlElement[] GetElementsByTagNClassName(WebBrowser wb, string tagName, string className)
{
var l = new List<HtmlElement>();

var els = wb.Document.GetElementsByTagName(tagName); // all elems with tag
foreach (HtmlElement el in els)
{

if (el.GetAttribute("className") == className)
{
l.Add(el);
}
}

return l.ToArray();
}

/// <summary>
/// 글 상세 페이지에서 추출하고자 하는 Text만 가져온다.
/// </summary>
/// <param name="oBrowser"></param>
private void getWebSiteContentsDetail(WebBrowser oBrowser)
{
var arrCollection = GetElementsByTagNClassName(webBrowserViewer, "td", "contentsbody");
for (int i = 0; i < arrCollection.Length; i++)
{
//Console.WriteLine(arrCollection[i].InnerHtml);

String sText = String.Empty;
//sText = arrCollection[i].InnerHtml;
sText = arrCollection[i].OuterText;

if (sText == null)
{
//글 text가 없는 경우가 있음
//case1) 이미지로 대체한 경우 img html tag가 글 대신 있음
sText = arrCollection[i].InnerHtml;
}
if (contentsKey.Equals(oBrowser.Url.AbsoluteUri.Replace(sContentsUrl, "")))
{
updateContentonIldaro(contentsKey, sText);
}
}
}

private void getData() {

// getting day and night temperature
var arrCollection = GetElementsByTagName(webBrowserViewer, "dt");

DataTable dt = new DataTable();

dt.Columns.Add("contentsKey", typeof(int));
dt.Columns.Add("Title", typeof(string));

Console.WriteLine("-- " + iq.ToString() + " --------------");
txtSource.Text = txtSource.Text + "-- " + iq.ToString() + " --------------" + "\r\n";

for (int i = 0; i < arrCollection.Length; i++) {
//Console.WriteLine(arrCollection[i].InnerHtml);

String sText = String.Empty;
sText = arrCollection[i].InnerHtml;
sText = sText.Replace("<A href=\"sread.html?key=", "").Replace("</A>", "").Replace("\"", "");

String[] arrSplit = sText.Split('>');

dt.Rows.Add(arrSplit[0],arrSplit[1]);

Console.WriteLine(arrSplit[0] + ":" + arrSplit[1]);

txtSource.Text = txtSource.Text + arrSplit[0] + ":" + arrSplit[1] + "\r\n";

}

}

#endregion


reference to :  https://www.codeproject.com/Tips/858775/Csharp-Website-HTML-Content-Parsing-or-How-To-Get


'프로그래밍 > c#' 카테고리의 다른 글

c#에서 mariadb사용하기  (0) 2018.11.10
WebBrowser.DocumentText 한글깨짐  (0) 2018.05.07
using MySqlConnector  (0) 2018.05.07

WebBrowser 객체를 이용해서 web페이지를 볼 때 DocumentText 메소드로 렌더링된 html을 읽어올 때 default utf-8 encoding type이라 이상태에서는 euc-kr의 한글은 깨진다

이 경우 스트림리더를 사용해서 실제 페이지의 encoding type으로 읽어들이면 된다.

 

샘플) WebBrowser.DocumentText 로 소스를 봤을경우.

 

<html>

<head>

<title><��̴ϽƮ ���� �ϴ�></title>

<meta http-equiv="Content-Type" content="text/html; charset=euc-kr" />

 

 

StreamReader를 이용해 페이지의 encoding type으로 읽어들인경우

 

<html>

<head>

<title><오늘은 어린이날 입니다></title>

 

<<C# Code>>

 

/// <summary>

        /// WebBrowser's DocumentCompleted Event

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void webBrowserViewer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {

            // ((WebBrowser)sender).Document.Window.Error +=   new HtmlElementErrorEventHandler(Window_Error);

 

            WebBrowser oBrowser = sender as WebBrowser;

 

            txtSource.Text = StreamConvertEUCKRtoUTF8(oBrowser);

                       

        }

 

        /// <summary>

        /// Using WebBrowser web page

        /// 웹페이지 Encoding type EUC-KR 이면 WebBrowser.DocumentText html 렌더링된 text 볼때 한글이 깨진다.

        /// 아래처럼 스트림리더로 EUC-KR type으로 읽어온다.

        /// </summary>

        /// <param name="oBrowser"></param>

        /// <returns></returns>

        private String StreamConvertEUCKRtoUTF8(WebBrowser oBrowser)

        {

            String sResult = String.Empty;

 

            Stream oStream;

            StreamReader oStreamReader;

            oStream = oBrowser.DocumentStream;

            oStreamReader = new StreamReader(oStream, System.Text.Encoding.GetEncoding(oBrowser.Document.Encoding));

            oStream.Position = 0;

            sResult = oStreamReader.ReadToEnd();

 

            return sResult;

        }


'프로그래밍 > c#' 카테고리의 다른 글

c#에서 mariadb사용하기  (0) 2018.11.10
WebBrowser이용한 스크린스크래핑  (0) 2018.05.09
using MySqlConnector  (0) 2018.05.07


c#에서 Mariadb를 연결할때 MySqlConnector를 사용한다.

VS에서 NgGet을 이용해서 쉽게 검색하고 설치할 수 있다. 

*NgGet 이놈 참 좋다~


MySqlConnector는 MIT 라이센스 그냥 사용하면 된다.





connector설치한김에 테스트까지 해봐야지요



c#코드


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

using MySql.Data.MySqlClient;


namespace ScrappingWinForms

{

    public partial class Form1 : Form

    {

        string sConnectionString = "Server=localhost;Uid=root;Pwd=???????;Database=???";


        String Url = string.Empty;


        public Form1()

        {

            InitializeComponent();


            //WebBrowser로 사이트 로딩시 자바스크립트 바인딩안되어 에러나는 경우.

            //자바스크립트 오류(dialogbox) 무시하고 진행가능하도록 제어함. 

            this.webBrowserViewer.ScriptErrorsSuppressed = true;

            webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);

        }


        private void btnSearch_Click(object sender, EventArgs e)

        {

            surftheWeb();

        }

        

        private void surftheWeb()

        {

            Url = txtUrl.Text;

            webBrowserViewer.Navigate(Url);

        }


        /// <summary>

        /// WebBrowser's DocumentCompleted Event

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void webBrowserViewer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {

            // ((WebBrowser)sender).Document.Window.Error +=   new HtmlElementErrorEventHandler(Window_Error);


            WebBrowser oBrowser = sender as WebBrowser;


            txtSource.Text = StreamConvertEUCKRtoUTF8(oBrowser);

                        

        }


        /// <summary>

        /// Using WebBrowser web page 

        /// 웹페이지 Encoding type 이 EUC-KR 이면 WebBrowser.DocumentText로 html로 렌더링된 text를 볼때 한글이 깨진다.

        /// 아래처럼 스트림리더로 페이지 EUC-KR type을 읽어들인다.

        /// </summary>

        /// <param name="oBrowser"></param>

        /// <returns></returns>

        private String StreamConvertEUCKRtoUTF8(WebBrowser oBrowser)

        {

            String sResult = String.Empty;


            Stream oStream;

            StreamReader oStreamReader;

            oStream = oBrowser.DocumentStream;

            oStreamReader = new StreamReader(oStream, System.Text.Encoding.GetEncoding(oBrowser.Document.Encoding));

            oStream.Position = 0;

            sResult = oStreamReader.ReadToEnd();


            return sResult;

        }

         

        private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)

        {

            //this.Text = webBrowser1.DocumentTitle.ToString();


            this.groupBox1.Text = webBrowserViewer.DocumentTitle;

        }



        private void loadTestData() {

            MySqlConnection oConnection = new MySqlConnection(sConnectionString);

            oConnection.Open();


            try

            {

                MySqlCommand cmd = oConnection.CreateCommand();

                cmd.CommandText = "SELECT * FROM TMP_help order by help_topic_id desc";

                MySqlDataAdapter aDap = new MySqlDataAdapter(cmd);

                DataSet dsData = new DataSet();

                aDap.Fill(dsData);


                dgViewSample.DataSource = dsData.Tables[0].DefaultView;


            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally {

                if (oConnection.State == ConnectionState.Open)

                {

                    oConnection.Close();

                }

            }

        }

      


        private void btnForTest_Click(object sender, EventArgs e)

        {

            loadTestData();

        }

     

    }

}



'프로그래밍 > c#' 카테고리의 다른 글

c#에서 mariadb사용하기  (0) 2018.11.10
WebBrowser이용한 스크린스크래핑  (0) 2018.05.09
WebBrowser.DocumentText 한글깨짐  (0) 2018.05.07

password 암호화 알고리즘 bcrypt


php script : 

아래 옵셥의 cost 값은 해싱의 속도를 나타낸다.

default : 10이며 값이 커질수록 해싱의 속도가 느려진다. 이는 오토 크래킹으로 부터 보안성을 높여 줄 수 있다.


<?php

/*
if (defined("CRYPT_BLOWFISH") && CRYPT_BLOWFISH)
echo "CRYPT_BLOWFISH is enabled!";
else
echo "CRYPT_BLOWFISH is not available";

*/
#첫번째 매개변수 - $password - 해시할 값(비밀번호)
#두번째 매개변수 - PASSWORD_BCRYPT - 해시에 사용될 알고리즘(PASSWORD_DEFAULT, PASSWORD_BCRYPT, ...)

$password ="tester";
$options15 = [ 'cost' => 15 ] ;
$options12 = [ 'cost' => 12 ] ;

$hashedValue = password_hash($password, PASSWORD_BCRYPT);

$hashedValueWithO12 = password_hash($password, PASSWORD_BCRYPT, $options12);
$hashedValueWithO15 = password_hash($password, PASSWORD_BCRYPT, $options15);

echo "1hashedPassword:".$hashedValue;
echo "<br>2hashedPassword:".$hashedValueWithO12;
echo "<br>3hashedPassword:".$hashedValueWithO15;

/*
if(password_verify($password, $hashedValue))
echo "<br>1true";
else
echo "<br>1false";
if(password_verify($password, $hashedValueWithO12))
echo "<br>2true";
else
echo "<br>2false<br>";

if(password_verify($password, $hashedValueWithO15))
echo "<br>3true";
else
echo "<br>3false<br>";
*/
echo "<br>1.hashedValue:";
print_r( password_get_info( $hashedValue ));

echo "<br>2.hashedValue:";
print_r( password_get_info( $hashedValueWithO12 ));

echo "<br>3.hashedValue:";
print_r( password_get_info( $hashedValueWithO15 ));

?>




cost => 10



cost => 12



cost => 15




reference to : http://dolgo.net/PHP/lecture/18


'프로그래밍 > PHP' 카테고리의 다른 글

php CURL 사용하기  (0) 2018.07.31
Javascript로 Facebook로그인  (0) 2018.05.26
unlink(=delete) 메소드 이용하기  (0) 2018.04.22
directory entry 읽어 파일리스트업  (0) 2018.04.20
파일 업로드(move_uploaded_file)  (3) 2018.04.14

칠레와인 1865 Single Vineyard Cabernet Sauvignon

1865 : SAN PEDRO 창립연도
Alcol : 14.5%
18홀 65타 라는 마케팅, 스토리텔링으로 골프애호가들에게 행운의 숫자 와인으로 기억됨.
떫고 다소 묵직한 느낌, 입안에 머금고 있을 때 과일향 과 맛의 느낌이 참 좋다..
드라이하고 떫은 맛이, 조금전 먹은 음식 맛을 초기화시키는 것 같은데 ^^
개인적인 평가는 Good 이다.

'Food > Wine' 카테고리의 다른 글

PINOT NOIR (피노 누아)  (0) 2018.04.15

 

VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])

 

자료를 맵핑할 때 특정한 key로 값을 찾을 때 사용되는 함수이다.

난 보통 일적으로 db쿼리를 사용해서 자료를 맞춘다.

 

Select a.key, a.data, b.data2 from

(Select 1 as key, ‘test1’ as data from dual

Union all

Select 2 as key, ‘test2’ as data from dual) a,

(Select 1 as key, ‘test1자료’ as data2 from dual

Union all

Select 2 as key, ‘test2자료’ as data2 from dual) b

Where a.key = b.key

 

위 쿼리의 모습과 같은 내용을 엑셀로도 할 수 있는데 그게 vlookup 이다

 

아래 url을 참고했고.

excel자체의 도움말에서 찾아도 된다.


==> table_array선택후 +F4로 데이터를 고정시켜야 함





reference url : https://support.office.com/en-us/article/vlookup-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1


업로드된 파일을 삭제하고 싶을 때 , unlink 메소드를 사용한다.


bool unlink ( string $filename [, resource $context ] )


인수

filename : Path to the file.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.


 

php초보자로 이벤트(클릭)를 발생시키고 파일을 삭제하는 것도 쉽지 않네.

 

php에서 파일삭제 메소드를 사용하기 위해서 아래 순서대로 호출.

크게 나쁘진 않은 것 같다. 보안관련 이슈는 차후에 학습하면서 추가하면 되겠다.

 

1)    Html a tag 사용한다.

2)    A tag에 삭제파라미터 : 파일명 를 넣고 해당 페이지를 호출

3)    Php get파라미터로 대상을 받아 파일삭제 메소드 호출


>> entries.PNG 삭제 버튼을 클릭함



>>bootstrap css 적용위한 css파일 첨부

bootstrap.min.css

light-bootstrap-dashboard.css



php source : 


<?php
$dir = $_SERVER['DOCUMENT_ROOT']."/admin1/test/img/";


if ( isset ( $_GET['delete']) ) {
echo $_GET['delete'];
echo "<br>";

if ( unlink ( $dir. $_GET['delete'] ) ) {

header("Location: ./fileupload0.php");
exit;
};
}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fileupload</title>
<!-- Fonts and icons -->
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700,200" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" />
<!-- CSS Files -->
<link href="../../admin3/assets/css/bootstrap.min.css" rel="stylesheet" />
<link href="../../admin3/assets/css/light-bootstrap-dashboard.css?v=2.0.1" rel="stylesheet" />

</head>
<body>
<div class="wrapper">
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<div class="card card-tasks">
<div class="card-header ">
<h4 class="card-title">Uploaded File's info</h4>
<p class="card-category">files이 있는 directory의 모든 파일을 읽어 리스트업</p>
</div>
<div class="card-body ">
<div class="table-full-width">
<table class="table">
<tbody>
<?php
/**
* Description.
*
* @author: JinMyung <dcan@doitforyou.co.kr>
* @category Filesystem
* @version 1.0
*/

/* files이 있는 directory의 모든 파일을 읽어 리스트업 */
// $dir = $_SERVER['DOCUMENT_ROOT']."/admin1/test/img/";

// 핸들 획득
$handle = opendir($dir);
$files = array();
// 디렉터리에 포함된 파일을 저장한다.
while (false !== ($filename = readdir($handle))) {
if ($filename =="." || $filename =="..") {
continue;
}

// 파일인 경우만 목록에 추가한다.
if (is_file($dir . "/" . $filename)) {
$files[] = $filename;
}
}
// 핸들 해제
closedir($handle);

sort($files);

// 파일명을 출력한다.
foreach ($files as $f) {
//echo $f . "(" . round(filesize($dir . "/" . $f)/1024, 2) . " KB)" ;
$FileFullPathName = "";
$FileFullPathName = $dir . "/" . $f;
echo "<tr>";
echo "<td>".$f . "(" . round(filesize($FileFullPathName)/1024, 2) . " KB)(".date("Y-m-d H:i:s", filemtime($FileFullPathName)) .")</td>";
echo "<td class=\"td-actions text-right\">";
echo " <a href=\"?delete=". $f."\">"."<button type=\"button\" style=\"cursor:hand\" rel=\"tooltip\" title=\"Remove\" class=\"btn btn-danger btn-simple btn-link\">";
echo "<i class=\"fa fa-times\"></i>";
echo "</button></a>";
echo "</td>";
echo "</tr>";

}
?>

</tr>
</tbody>
</table>
</div>
</div>
<div class="card-footer ">
<hr>
<div class="stats">
<i class="now-ui-icons loader_refresh spin"></i>
<?php
echo "refer to : http://blog.devez.net/292 | http://php.net/manual/en/function.readdir.php";
?>
</div>
<form enctype="multipart/form-data" action="fileupload1.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="4000000" />
Send File: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

</div>
</div>
</div>
</div>
</div>
</div>
</div>

</body>
</html>


'프로그래밍 > PHP' 카테고리의 다른 글

Javascript로 Facebook로그인  (0) 2018.05.26
php 패스워드 암호화 bcript  (0) 2018.05.02
directory entry 읽어 파일리스트업  (0) 2018.04.20
파일 업로드(move_uploaded_file)  (3) 2018.04.14
fileupload 에러메시지 설명  (0) 2018.04.14

+ Recent posts