勤怠システムの構築 その1

RaspberryPi
Makoto
Makoto

勤怠管理がめんどくさいーーー!

しろまる
しろまる

どうしたにゃ?

Makoto
Makoto

勤怠を管理するのを忘れちゃったりすると
時間覚えてないから大変だし毎日するのも。。。

しろまる
しろまる

それで自動化したいって考えてるのかにゃ?

Makoto
Makoto

そうなの!
たまたま焦電センサが手に入ったからそれを使って勤怠の
自動記録つけれないかなって思ってるの!

ジェームズ
ジェームズ

それでしたらラズパイに組み込んでみましょうか

Makoto
Makoto

頑張る!

ジェームズ
ジェームズ

それではまず簡単に今回のシステムの設計をしましょうか
まず定義として
1.7−9時で焦電センサに反応があったら出勤記録
2.17時以降に4分以上反応がなければ退勤記録
3.7時から17時までに4分以上反応がなければ休憩として記録
4.上の記録をCSVで吐き出す
という感じでどうでしょうか?

Makoto
Makoto

そうそんな感じのが欲しいの!

ジェームズ
ジェームズ

それではプログラムを書いてみましょうか
今回はpythone3で書いてみましょう
保存場所は好きな所にするといいでしょう
今回は後々のことも考えてhtmlフォルダに格納しています。

#ライブラリ設定
import RPi.GPIO as GPIO
import time
import datetime
import os

#焦電センサ設定
GPIO_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN,GPIO.IN)

#カウンタ設定
count_a = 0 #動作検知カウント
count_b = 0 #動作非検知カウント

#フラグ設定
flag_a = 0 #出勤フラグ
flag_b = 0 #休憩フラグ

#勤怠記入
def write_csv(a,b):
    path = '/var/www/html/csv/Kintai/Kintai' + time.strftime("%Y%m", time.strptime(time.ctime())) + '.csv'
    is_file = os.path.isfile(path)

    #ファイルを新しく作る場合のみ改行を入れない
    if is_file:
        with open(path, 'a') as f:
            f.write("\n" + str(a)+ "," + str(b) )
    else:
        with open(path, 'a') as f:
            f.write(str(a)+ "," + str(b) )

#休憩記入
def write_csv2(a,b):
    path = '/var/www/html/csv/Kyukei/Kyukei' + time.strftime("%Y%m", time.strptime(time.ctime())) + '.csv'
    is_file = os.path.isfile(path)

    #ファイルを新しく作る場合のみ改行を入れない
    if is_file:
        with open(path, 'a') as f:
            f.write("\n" + str(a)+ "," + str(b) )
    else:
        with open(path, 'a') as f:
            f.write(str(a)+ "," + str(b) )

#メイン処理
if __name__ == '__main__':
    print('勤怠記録開始!')
    while True:
        if (GPIO.input(GPIO_PIN) == GPIO.HIGH) and count_a < 20: #動作検知時に開始時間を記録 if count_a == 0: now = time.strftime("%Y/%m/%d %H:%M", time.strptime(time.ctime())) now_h = time.strftime("%H", time.strptime(time.ctime())) #動作確認用(確認しないなら削除する) if count_a % 10 == 0 and count_a != 0: print(time.strftime("%Y/%m/%d %H:%M", time.strptime(time.ctime())) +" 反応あり!:"+ str(count_a)) #9時迄の(10秒間連続)感知は一回だけ出社扱い if count_a == 10 and flag_a == 0: if 9 >= int(now_h):
                    flag_a = 1
                    write_csv("出勤",now)
                    print("記入")

            #休憩フラグが立っている場合のみ仕事再開扱い
            if count_a == 10 and flag_b == 1:
                    flag_b = 0
                    write_csv2("仕事再開",now)
                    print("記入")

            time.sleep( 1 )
            count_a += 1
            count_b = 0

        elif (GPIO.input(GPIO_PIN) == GPIO.LOW) and count_b < 300:

            #動作非検知時に開始時間を記録
            if count_b == 0:
                now = time.strftime("%Y/%m/%d %H:%M", time.strptime(time.ctime()))
                now_h = time.strftime("%H", time.strptime(time.ctime()))

            #動作確認用(確認しないなら削除する)
            if count_b % 30 == 0 and count_b != 0:
                print(time.strftime("%Y/%m/%d %H:%M", time.strptime(time.ctime())) +" 反応なし:"+ str(count_b))

            if count_b == 240:
                #17時以降の(240秒間連続非感知)離席は退勤扱い
                if 17 <= int(now_h) and 19 >= int(now_h):
                    flag_a = 0
                    write_csv("退勤",now)
                    print("記入")

                #8-17時の(240秒間連続非感知)離席は休憩扱い
                elif 8 <= int(now_h) and 17 >= int(now_h) and flag_b == 0:
                    flag_b = 1
                    write_csv2("休憩開始",now)
                    print("記入")

            time.sleep( 1 )
            count_a = 0
            count_b += 1
Makoto
Makoto

できたー!

ジェームズ
ジェームズ

おめでとうございます。

Makoto
Makoto

これでしばらく記録つけてみるね!
あと、ここにプログラムあげとくー>w<
https://github.com/Makoto603/AttendanceManagement/

コメント

Ads Blocker Image Powered by Code Help Pro

広告ブロックを検知しました!

広告ブロック解除をお願い致します!

このサイトは広告収入だけで成り立っているため

ご協力をお願い致します!

タイトルとURLをコピーしました