Last active
          June 1, 2017 09:58 
        
      - 
      
- 
        Save 4noha/5ad6e27f5440cde56ed95394c7a3d7ac to your computer and use it in GitHub Desktop. 
    https://gist.github.com/nokkii/b04c399a83437b714e0f280fb30e6416 をhttpクライアントで複数台設定できるやつ
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const DashButton = require( 'dash-button' ); | |
| const Request = require( 'request' ); | |
| const Fs = require( 'fs' ); | |
| const Iconv = require( 'iconv-lite' ); | |
| const PushBullet = require( 'pushbullet' ); | |
| const Mysql = require('mysql'); | |
| const Http = require('http'); | |
| const Querystring = require('querystring'); | |
| const MYSQL_HOST = 'にゃ〜ん'; | |
| const MYSQL_USER = 'にゃ〜ん'; | |
| const SQL_PASSWORD = 'にゃ〜ん'; | |
| const buttons = []; | |
| const buttonNew = function( data, res ) { | |
| var params = Querystring.parse( data ); | |
| var connection = Mysql.createConnection({ | |
| host : MYSQL_HOST, | |
| user : MYSQL_USER, | |
| password : SQL_PASSWORD, | |
| database : 'attendance_button', | |
| }); | |
| connection.query( "INSERT INTO buttons (mac_address, employee_id, password, pb_mail, pb_token) VALUES ('" + | |
| params.mac_address + "', '" + | |
| params.employee_id + "', '" + | |
| params.password + "', '" + | |
| params.pb_mail + "', '" + | |
| params.pb_token + "') ON DUPLICATE KEY UPDATE mac_address = '" + | |
| params.mac_address + "'", | |
| function (error, results, fields) { | |
| buttonListen( params ); | |
| res.writeHead( 200, { 'Content-Type': 'text/plain' } ); | |
| res.end( params.mac_address + ', ' + params.employee_id + ', ' + 'OK' ); | |
| }); | |
| connection.end(); | |
| } | |
| const buttonListen = function( params ) { | |
| var button = new DashButton( params.mac_address ); | |
| button.addListener( | |
| replaceFunc( | |
| buttonListener, | |
| function(src){ | |
| return src.replace( | |
| /__OVERRIDE__/, | |
| "employee_id='" + params.employee_id + | |
| "';var password='" + params.password + | |
| "';var pb_mail='" + params.pb_mail + | |
| "';var pb_token='" + params.pb_token + "'" | |
| ); | |
| }) | |
| ); | |
| buttons.push( button ); | |
| } | |
| const replaceFunc = function(func, replaceMethod){ | |
| var funcScript = func.toString(); | |
| var funcMatches = funcScript.match(/function *([^\(]*)\((.*)\)( *)/); | |
| var funcSignature = funcMatches[2]; | |
| var funcBody = RegExp.rightContext; | |
| var newScript ="var f = function("+funcSignature+")"+replaceMethod(funcBody)+";"; | |
| eval(newScript); | |
| return f; | |
| } | |
| const buttonListener = function() { | |
| var cTime = new Date(); | |
| var options; | |
| var __OVERRIDE__; | |
| // 時間で出退勤制御(タイムゾーンに注意) | |
| if ( cTime.getHours() < 17 ) { | |
| options = { | |
| url: 'https://cws.にゃ〜ん.jp/cws30/srwtimerec?dakoku=syussya&user_id=' + employee_id + '&password=' + password, | |
| encoding: 'binary', | |
| }; | |
| } else { | |
| options = { | |
| url: 'https://cws.にゃ〜ん.jp/cws30/srwtimerec?dakoku=taisya&user_id=' + employee_id + '&password=' + password, | |
| encoding: 'binary', | |
| }; | |
| } | |
| Request.get( options, | |
| replaceFunc( | |
| attendance, | |
| function(src){ | |
| return src.replace( | |
| /__OVERRIDE2__/, | |
| "employee_id='" + employee_id + | |
| "';var pb_mail='" + pb_mail + | |
| "';var pb_token='" + pb_token + "'" | |
| ); | |
| }) | |
| ); | |
| }; | |
| const attendance = function( error, response, body ) { | |
| var __OVERRIDE2__; | |
| var pusher = new PushBullet( pb_token ); | |
| if ( !error && response.statusCode == 200 ) { | |
| var buf = new Buffer( body, 'binary' ); | |
| var retStr = Iconv.decode( buf, "Shift_JIS" ); | |
| var reMorning = /今日も一日がんばりましょう!/; | |
| var reNight = /お疲れ様でした。/; | |
| var reTimeSplitter = /<u> | <\/u>/; | |
| var recTime = retStr.split( reTimeSplitter )[1]; | |
| // テキストで出退勤判定 | |
| if ( reMorning.test( retStr ) ){ | |
| pusher.note( pb_mail, '出勤', recTime, function( error, response ) {} ); | |
| console.log( 'ID:' + employee_id + ' in:' + recTime ); | |
| return; | |
| } else if ( reNight.test( retStr ) ) { | |
| pusher.note( pb_mail, '退勤', recTime, function( error, response ) {} ); | |
| console.log( 'ID:' + employee_id + ' out:' + recTime ); | |
| return; | |
| } | |
| } | |
| pusher.note( pb_mail, '勤怠エラー', 'error', function( error, response ) {} ); | |
| console.log( 'ID:' + employee_id + ' error: response error.' ); | |
| }; | |
| var connection = Mysql.createConnection({ | |
| host : MYSQL_HOST, | |
| user : MYSQL_USER, | |
| password : SQL_PASSWORD, | |
| database : 'attendance_button', | |
| }); | |
| connection.connect( function( err ) { | |
| if (err) { | |
| console.error('MySQL connection faild: ' + err.stack); | |
| return; | |
| } | |
| }); | |
| connection.query('SELECT * FROM buttons', function (error, buttons, fields) { | |
| for (var i in buttons) { | |
| var params = { | |
| mac_address: buttons[i].mac_address, | |
| employee_id: buttons[i].employee_id, | |
| password: buttons[i].password, | |
| pb_mail: buttons[i].pb_mail, | |
| pb_token: buttons[i].pb_token, | |
| } | |
| buttonListen( params ); | |
| }; | |
| }); | |
| connection.end(); | |
| // ボタン登録クライアント | |
| Http.createServer( function (req, res) { | |
| if ( req.method === 'GET' ) { | |
| res.writeHead( 200, { 'Content-Type': 'text/html' } ); | |
| res.write( '\ | |
| <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>\ | |
| <body><form method="post">\ | |
| 出退勤ボタン登録フォーム<br>\ | |
| <label>MAC Addess:</label><input type="text" name="mac_address" /><br>\ | |
| <label>社員番号:</label><input type="text" name="employee_id" /><br>\ | |
| <label>パスワード:</label><input type="text" name="password" /><br>\ | |
| <label>PushBullet Mail:</label><input type="text" name="pb_mail" /><br>\ | |
| <label>PB TOKEN:</label><input type="text" name="pb_token" /><br>\ | |
| <input type="submit" value="送信">\ | |
| </form></body></html>\ | |
| ' ); | |
| res.end(); | |
| } else if ( req.method === 'POST' ) { | |
| var data = ''; | |
| req.on( 'data', function( chunk ) { data += chunk; } ); | |
| req.on( 'end', function() { | |
| buttonNew( data, res ); | |
| }); | |
| } | |
| }).listen( 9999, '0.0.0.0' ); | |
| /* | |
| CREATE DATABASE attendance_button; | |
| USE attendance_button; | |
| CREATE TABLE buttons ( mac_address char(17) UNIQUE, employee_id char(6), password char(12), pb_mail text, pb_token char(34) ); | |
| */ | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment