summaryrefslogtreecommitdiff
path: root/check_future_slots.py
blob: 9e46834058e591ab13a3e69bd5da30137e6786dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
from datetime import datetime
import time

import serial

nowDate = datetime.now().date()
nowHour = datetime.now()
os.environ.setdefault('DJANGO_SETTINGS_MODULE','sbhs_server.settings')

import django
django.setup()

from sbhs_server.tables.models import Booking, Account, Board

# ser.close()
ser = serial.Serial('/dev/ttyUSB0')

def switch_on(b_id, booking_Date, nowhour,slot_id):
    if datetime.now().date() == booking_Date: #This must be booking date:
        tim_e = slot_id-2
        nowHour = datetime.now()
        # print '{}:58'.format(time)
        tim_e = '{}:{}'.format(tim_e,nowHour.minute)
        tim_e = datetime.strptime(tim_e,'%H:%M').time()

        # create a newvariable with current hour:minute
        current_time = '{}:{}'.format(nowHour.hour,nowHour.minute)
        current_time = datetime.strptime(current_time,'%H:%M').time()

        # compare time with newvariable.
        # If both are equal, device should turn on
        # print 'inside switch_on', type(b_id)
        board = Board.objects.get(id=b_id)
        mid = board.mid
        mid = str(mid).zfill(2)   
        print mid 
        # if tim_e == current_time:
        time.sleep(1)
        ser.write(b'F'+mid)
        print ser
        board.power_status = 1
        board.save()
         
def switch_off(b_id):
    # print 'b_id in switch_off: ',b_id
    board = Board.objects.get(id=b_id)
    mid = str(b_id).zfill(2)   
    time.sleep(1)
    ser.write(b'N'+mid)
    # print ser
    board.power_status = 0
    board.save()

def check_future_slots():
    print nowDate    
    # b = Booking.objects.filter(booking_date=nowDate)
    # print b
    # print nowHour.hour
    print 'nowHour.hour+2',nowHour.hour + 2
    if nowHour.minute > 55:
        nowhour = nowHour.hour + 2
    else:
        nowhour = nowHour.hour + 1
    print 'nowhour ',nowhour
    # nowhour = str(nowHour)
    # nowHour = nowHour.now()+1
    get_slot_id = Booking.objects.filter(booking_date=nowDate,slot_id = nowhour).values('booking_date','slot_id','account_id') #this returns queryset
    # iterate over queryset to get slot_id and booking_date
    print 'get_slot_id',get_slot_id
    b_id_array = []
    for slot in get_slot_id:
        print slot['slot_id']
        print slot['booking_date'].date()
        print slot['account_id']
        slot_id = slot['slot_id']
        booking_Date = slot['booking_date'].date()
        id = slot['account_id']
        print slot_id
        print booking_Date
        print id
        get_board_id = Account.objects.get(id=id)
        print get_board_id
        b_id = get_board_id.board_id
        print b_id
        b_id = str(b_id).zfill(2)
        b_id_array.append(int(b_id))
        print b_id_array
   
    for b_id in b_id_array:
        print 'type of',type(b_id)
        switch_on(b_id,booking_Date,nowhour,slot_id)
    
    b_id_set = set(b_id_array)
    board_set = set(range(1,17))
    print 'board_set', board_set
    print 'intersection result:', board_set.difference(b_id_set)
    b_id_set_intersection = board_set.intersection(b_id_set)
    print b_id_set_intersection
    for b_id in board_set.difference(b_id_set):
        switch_off(b_id)
        

    # ser.close()    
        
check_future_slots()