-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0713186
commit 0b985ff
Showing
5 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package main | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
|
||
"github.com/jphacks/D_2017/model" | ||
"github.com/jphacks/D_2017/repository" | ||
|
||
_ "github.com/go-sql-driver/mysql" // グローバル設定を宣言(DBドライバの設定) | ||
) | ||
|
||
type touchLogic struct { | ||
logRepository repository.LogRepositoryInterface | ||
readerRepository repository.ReaderRepositoryInterface | ||
cardRepository repository.CardRepositoryInterface | ||
roomRepository repository.RoomRepositoryInterface | ||
bodyTemperatureRepository repository.BodyTemperatureRepositoryInterface | ||
} | ||
|
||
func newTouchLogic(logRepository repository.LogRepositoryInterface, | ||
readerRepository repository.ReaderRepositoryInterface, | ||
cardRepository repository.CardRepositoryInterface, | ||
roomRepository repository.RoomRepositoryInterface, | ||
bodyTemperatureRepository repository.BodyTemperatureRepositoryInterface) *touchLogic { | ||
return &touchLogic{ | ||
logRepository, | ||
readerRepository, | ||
cardRepository, | ||
roomRepository, | ||
bodyTemperatureRepository, | ||
} | ||
} | ||
|
||
func exit(logic *touchLogic, log model.Log, time time.Time) (string, error) { | ||
s := `{"result":"exit"}` | ||
_, err := logic.logRepository.UpdateLeftAtByID(log.ID, time) | ||
return s, err | ||
} | ||
|
||
func enter(logic *touchLogic, userID string, roomID int) (string, error) { | ||
s := `{"result":"accept"}` | ||
log, _ := model.NewLog(userID, roomID) | ||
_, err := logic.logRepository.Insert(log) | ||
return s, err | ||
} | ||
|
||
func (logic *touchLogic) handle(unixtime string, idm string, macAddress string) (string, error) { | ||
// 時刻生成 | ||
timeInt, _ := strconv.ParseInt(unixtime, 10, 64) | ||
time := time.Unix(timeInt, 0) | ||
|
||
// userIDの特定 | ||
card, err := logic.cardRepository.SelectByIDm(idm) | ||
if err != nil || card == nil { | ||
return `{"result":"reject"}`, err | ||
} | ||
userID := card.UserID | ||
|
||
//roomIDの特定 | ||
reader, err := logic.readerRepository.SelectByMACAddress(macAddress) | ||
if err != nil { | ||
return `{"result":"reject"}`, err | ||
} | ||
roomID := reader.RoomID | ||
|
||
// 今部屋に入っていれば退室処理 | ||
enteringLogs, err := logic.logRepository.SelectEnteringByRoomID(roomID) | ||
for _, log := range *enteringLogs { | ||
if log.UserID == userID { | ||
return exit(logic, log, time) | ||
} | ||
} | ||
|
||
// 入室可能かどうかの判定 | ||
|
||
// 現在の人数の確認 | ||
memberCount := len(*enteringLogs) | ||
room, err := logic.roomRepository.SelectByID(roomID) | ||
if memberCount >= room.LimitNumber { | ||
return `{"result":"reject"}`, nil | ||
} | ||
|
||
// [TODO] allow_missingならログが2週間分あることを確認 | ||
|
||
// 体温チェック | ||
twoWeekAgo := time.AddDate(0, 0, -14) | ||
temperatures, _ := logic.bodyTemperatureRepository.SelectByUserIDBetween(userID, twoWeekAgo, time) | ||
|
||
canEnter := true | ||
for _, temp := range *temperatures { | ||
if !temp.IsTrusted { | ||
continue | ||
} | ||
canEnter = canEnter && (temp.Temperature < room.LimitBodyTemperature) | ||
} | ||
|
||
//入れた場合 | ||
if canEnter { | ||
return enter(logic, userID, roomID) | ||
} | ||
|
||
// 拒否 | ||
return `{"result":"reject"}`, nil | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/jphacks/D_2017/repository" | ||
"github.com/jphacks/D_2017/response" | ||
|
||
"github.com/jphacks/D_2017/model" | ||
) | ||
|
||
type mockCardRepository struct {} | ||
func (cardRepository *mockCardRepository) Insert(record *model.Card) (*model.Card, error) { | ||
return nil, nil | ||
} | ||
func (cardRepository *mockCardRepository) DeleteByIDm(IDm string) error { | ||
return nil | ||
} | ||
func (cardRepository *mockCardRepository) SelectByIDm(IDm string) (*model.Card, error) { | ||
return &model.Card{ | ||
IDm: "0000000000000000", | ||
UserID: "test-user", | ||
}, nil | ||
} | ||
func newMockCardRepository() repository.CardRepositoryInterface { | ||
return &mockCardRepository{} | ||
} | ||
type mockRoomRepository struct {} | ||
func newMockRoomRepository() repository.RoomRepositoryInterface { | ||
return &mockRoomRepository{} | ||
} | ||
func (r *mockRoomRepository) Insert(*model.Room) (*model.Room, error){ | ||
return nil, nil | ||
} | ||
|
||
func (r *mockRoomRepository) Update(*model.Room) (*model.Room, error){ | ||
return nil, nil | ||
} | ||
|
||
func (r *mockRoomRepository) SelectByID(int) (*model.Room, error) { | ||
return &model.Room{ | ||
RoomID: 42, | ||
Name: "room1", | ||
LimitNumber: 2, | ||
LimitBodyTemperature: 37.0, | ||
AllowMissing: true, | ||
}, nil | ||
} | ||
type mockReaderRepository struct{} | ||
func newMcokReaderRepository() repository.ReaderRepositoryInterface { | ||
return &mockReaderRepository{} | ||
} | ||
func (r *mockReaderRepository) Insert(*model.Reader) (*model.Reader, error){ | ||
return nil, nil | ||
} | ||
func (r *mockReaderRepository) SelectByUserID(string) (*[]model.Reader, error){ | ||
return nil, nil | ||
} | ||
func (r *mockReaderRepository) SelectByRoomID(int) (*[]model.Reader, error){ | ||
return nil, nil | ||
} | ||
func (r *mockReaderRepository) SelectByMACAddress(macAddress string) (*model.Reader, error) { | ||
return &model.Reader{ | ||
MACAddress: "0000000000000000", | ||
UserID: "test-user", | ||
RoomID: 42, | ||
},nil | ||
} | ||
|
||
type mockBodyTemperatureRepository struct {} | ||
func newMockBodyTemperatureRepository() repository.BodyTemperatureRepositoryInterface { | ||
return &mockBodyTemperatureRepository{} | ||
} | ||
func (m *mockBodyTemperatureRepository) Insert(*model.BodyTemperature) (*model.BodyTemperature, error) { | ||
return nil, nil | ||
} | ||
func (m *mockBodyTemperatureRepository) UpdateIsTrustedByID(bool, int) (*model.BodyTemperature, error) { | ||
return nil, nil | ||
} | ||
func (m *mockBodyTemperatureRepository) SelectByID(int) (*model.BodyTemperature, error) { | ||
return nil, nil | ||
} | ||
func (m *mockBodyTemperatureRepository) SelectByUserID(string, int,int) (*[]model.BodyTemperature, error) { | ||
return nil, nil | ||
} | ||
func (m *mockBodyTemperatureRepository) SelectByUserIDBetween(userID string, since time.Time, until time.Time) (*[]model.BodyTemperature, error) { | ||
var array [3]model.BodyTemperature | ||
times := [3]time.Time{ | ||
time.Date(2014, time.December, 31, 12, 15, 24, 0, time.UTC), | ||
time.Date(2014, time.December, 31, 12, 14, 24, 0, time.UTC), | ||
time.Date(2014, time.December, 31, 12, 13, 24, 0, time.UTC), | ||
} | ||
array[0] = model.BodyTemperature{ | ||
ID: 42, | ||
UserID: "test-user", | ||
Temperature: 36.5, | ||
MACAddress: "00:00:00:00:00", | ||
IsTrusted: true, | ||
CreatedAt: ×[0], | ||
} | ||
array[1] = model.BodyTemperature{ | ||
ID: 43, | ||
UserID: "test-user", | ||
Temperature: 36.3, | ||
MACAddress: "00:00:00:00:00", | ||
IsTrusted: true, | ||
CreatedAt: ×[1], | ||
} | ||
array[2] = model.BodyTemperature{ | ||
ID: 44, | ||
UserID: "test-user", | ||
Temperature: 36.6, | ||
MACAddress: "00:00:00:00:00", | ||
IsTrusted: true, | ||
CreatedAt: ×[2], | ||
} | ||
res := array[:] | ||
return &res, nil | ||
} | ||
|
||
type mockLogRepository struct {} | ||
func newMockLogRepository() repository.LogRepositoryInterface { | ||
return &mockLogRepository{} | ||
} | ||
func (r *mockLogRepository) Insert(*model.Log) (*model.Log, error) { | ||
return nil, nil | ||
} | ||
func (r *mockLogRepository) Select | ||
func (r *mockLogRepository) UpdateLeftByID | ||
|
||
|
||
|
||
|
||
func TestHandle(t *testing.T) { | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/aws/aws-lambda-go/lambda" | ||
"github.com/jphacks/D_2017/repository" | ||
|
||
mqtt "github.com/eclipse/paho.mqtt.golang" | ||
_ "github.com/go-sql-driver/mysql" // グローバル設定を宣言(DBドライバの設定) | ||
) | ||
|
||
// Event - 送られてくるjson | ||
type Event struct { | ||
Timestamp string `json:"timestamp"` | ||
Idm string `json:"idm"` | ||
MacAddress string `json:"mac_address"` | ||
} | ||
|
||
func eventHandler(ctx context.Context, event Event) (string, error) { | ||
// イベントからデータ取得 | ||
unixtime := event.Timestamp | ||
idm := event.Idm | ||
macAddress := event.MacAddress | ||
|
||
// mqtt初期化処理 | ||
client := mqtt.NewClient(mqtt.NewClientOptions()) | ||
if token := client.Connect(); token.Wait() && token.Error() != nil { | ||
log.Fatalf("Mqtt error: %s", token.Error()) | ||
} | ||
|
||
logic := newTouchLogic(repository.NewLogRepository(), | ||
repository.NewReaderRepository(), | ||
repository.NewCardRepository(), | ||
repository.NewRoomRepository(), | ||
repository.NewBodyTemperatureRepository()) | ||
|
||
payload, err := logic.handle(unixtime, idm, macAddress) | ||
if err != nil { | ||
client.Publish("iot-data", 0, false, `{"result":"reject"}`) | ||
return "failed", err | ||
} | ||
client.Publish("iot-data", 0, false, payload) | ||
return "success", nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(eventHandler) | ||
} |