1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7
+ < title > Mock Dustbin</ title >
8
+ </ head >
9
+ < style >
10
+ .dustbin-area {
11
+ text-align : center;
12
+ }
13
+ .opened-dustbin {
14
+ display : none;
15
+ }
16
+ .message {
17
+ background-color : antiquewhite;
18
+ font-size : 40px ;
19
+ text-align : center;
20
+ }
21
+ </ style >
22
+ < body >
23
+ < div class ="dustbin-area ">
24
+ < img src ="open.png " class ="opened-dustbin ">
25
+ < img src ="close.png " class ="closed-dustbin ">
26
+ </ div >
27
+ < div class ="message "> closed</ div >
28
+ < script >
29
+ let val = 0.1 ;
30
+ setInterval ( ( ) => {
31
+ let dustbin_state = "close" ;
32
+ fetch ( "http://solanabin.pythonanywhere.com/should_open_dustbin/1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e/" )
33
+ . then ( ( response ) => response . json ( ) )
34
+ . then ( ( data ) => {
35
+ dustbin_state = data . data ;
36
+ console . log ( dustbin_state ) ;
37
+
38
+ let opened_dustbin = document . querySelector ( '.opened-dustbin' ) ;
39
+ let message = document . querySelector ( '.message' ) ;
40
+ let closed_dustbin = document . querySelector ( '.closed-dustbin' ) ;
41
+ if ( dustbin_state == "close" ) {
42
+ opened_dustbin . style . display = "none" ;
43
+ closed_dustbin . style . display = "block" ;
44
+ message . innerHTML = "Dustbin Closed"
45
+ }
46
+ if ( dustbin_state == "open" ) {
47
+ opened_dustbin . style . display = "block" ;
48
+ closed_dustbin . style . display = "none" ;
49
+ message . innerHTML = "Dustbin Opened. Adding Mock weight of 0.1 kg and sending" ;
50
+
51
+ const data_to_send = fetch ( 'http://solanabin.pythonanywhere.com/unit_value_history/1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e/' , {
52
+ method : 'POST' ,
53
+ headers : {
54
+ 'Accept' : 'application/json' ,
55
+ 'Content-Type' : 'application/json'
56
+ } ,
57
+ body : JSON . stringify ( { "weight_value" : val + "" , "height_value" : "2" , "redeemed" : false } )
58
+ } ) . then ( ( resp ) => {
59
+ val += 0.1 ;
60
+ // finnaly close the dustbin
61
+ return resp . json ( ) ;
62
+ } ) . then ( ( data ) => {
63
+ fetch ( "http://solanabin.pythonanywhere.com/close_dustbin/1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e/" ) . then ( ( rep ) => console . log ( rep . json ( ) ) ) ;
64
+ console . log ( data ) ;
65
+ } ) ;
66
+ }
67
+ } ) ;
68
+ } , 8000 ) ;
69
+ </ script >
70
+ </ body >
71
+ </ html >
0 commit comments