<s> try : <EOL> import config <EOL> except ImportError : <EOL> print ( "<STR_LIT>" ) ; exit ( ) ; <EOL> debug_communication = <NUM_LIT:0> <EOL> import urllib3 <EOL> import time <EOL> import json <EOL> import grovepi <EOL> from grove_oled import * <EOL> import sys <EOL> import signal <EOL> def send_to_hcp ( http , url , headers , value ) : <EOL> timestamp = int ( time . time ( ) ) <EOL> body = '<STR_LIT>' + str ( config . message_type_id_From_device ) + '<STR_LIT>' + str ( value ) + '<STR_LIT>' + str ( timestamp ) + '<STR_LIT>' <EOL> r = http . urlopen ( '<STR_LIT:POST>' , url , body = body , headers = headers ) <EOL> if ( debug_communication == <NUM_LIT:1> ) : <EOL> print ( "<STR_LIT>" + str ( r . status ) ) <EOL> print ( r . data ) <EOL> def poll_from_hcp ( http , url , headers ) : <EOL> global msg_string <EOL> r = http . urlopen ( '<STR_LIT:GET>' , url , headers = headers ) <EOL> if ( debug_communication == <NUM_LIT:1> ) : <EOL> print ( "<STR_LIT>" + str ( r . status ) ) <EOL> print ( r . data ) <EOL> json_string = '<STR_LIT>' + ( r . data ) . decode ( "<STR_LIT:utf-8>" ) + '<STR_LIT:}>' <EOL> try : <EOL> json_string_parsed = json . loads ( json_string ) <EOL> try : <EOL> messages_reversed = reversed ( json_string_parsed [ "<STR_LIT>" ] ) <EOL> for single_message in messages_reversed : <EOL> payload = single_message [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> opcode = payload [ "<STR_LIT>" ] <EOL> operand = payload [ "<STR_LIT>" ] <EOL> if ( opcode == "<STR_LIT>" ) : <EOL> msg_string = operand <EOL> if ( opcode == "<STR_LIT>" ) : <EOL> if ( operand == "<STR_LIT:0>" ) : <EOL> switch_led ( <NUM_LIT:0> ) <EOL> if ( operand == "<STR_LIT:1>" ) : <EOL> switch_led ( <NUM_LIT:1> ) <EOL> except TypeError : <EOL> print ( "<STR_LIT>" + ( r . data ) . decode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" ) <EOL> except ValueError : <EOL> print ( "<STR_LIT>" + ( r . data ) . decode ( "<STR_LIT:utf-8>" ) + "<STR_LIT>" ) <EOL> def read_slider_value ( slider , old_value ) : <EOL> try : <EOL> slider_value = ( int ) ( grovepi . analogRead ( slider ) / <NUM_LIT> ) <EOL> except IOError : <EOL> print ( "<STR_LIT>" ) <EOL> slider_value = old_value <EOL> return ( slider_value ) <EOL> def switch_led ( arg ) : <EOL> global led <EOL> try : <EOL> if ( arg == <NUM_LIT:0> ) : <EOL> grovepi . digitalWrite ( led , <NUM_LIT:1> ) <EOL> if ( arg == <NUM_LIT:1> ) : <EOL> grovepi . digitalWrite ( led , <NUM_LIT:0> ) <EOL> except IOError : <EOL> print ( "<STR_LIT>" ) <EOL> def redraw_oled ( value , do_send , msg_string ) : <EOL> oled_setTextXY ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> oled_putString ( "<STR_LIT:U+0020>" ) <EOL> oled_setTextXY ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> oled_putString ( "<STR_LIT>" + str ( value ) ) <EOL> oled_setTextXY ( <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> oled_putString ( "<STR_LIT:U+0020>" ) <EOL> if ( do_send == <NUM_LIT:0> ) : <EOL> oled_setTextXY ( <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> oled_putString ( "<STR_LIT>" ) <EOL> if ( do_send == <NUM_LIT:1> ) : <EOL> oled_setTextXY ( <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> oled_putString ( "<STR_LIT>" ) <EOL> oled_setTextXY ( <NUM_LIT:4> , <NUM_LIT:0> ) <EOL> oled_putString ( "<STR_LIT:U+0020>" ) <EOL> oled_setTextXY ( <NUM_LIT:4> , <NUM_LIT:0> ) <EOL> oled_putString ( msg_string [ : <NUM_LIT:12> ] ) <EOL> def signal_handler ( signal , frame ) : <EOL> global slider_value <EOL> print ( "<STR_LIT>" ) <EOL> switch_led ( <NUM_LIT:0> ) <EOL> do_send = <NUM_LIT:0> <EOL> msg_string = "<STR_LIT>" <EOL> redraw_oled ( slider_value , do_send , msg_string ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> signal . signal ( signal . SIGINT , signal_handler ) <EOL> try : <EOL> urllib3 . disable_warnings ( ) <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> if ( config . proxy_url == '<STR_LIT>' ) : <EOL> http = urllib3 . PoolManager ( ) <EOL> else : <EOL> http = urllib3 . proxy_from_url ( config . proxy_url ) <EOL> url = '<STR_LIT>' + config . hcp_account_id + config . hcp_landscape_host + '<STR_LIT>' + str ( config . device_id ) <EOL> headers = urllib3 . util . make_headers ( user_agent = None ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' + config . oauth_credentials_for_device <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> slider = <NUM_LIT:0> <EOL> grovepi . pinMode ( slider , "<STR_LIT>" ) <EOL> led = <NUM_LIT:4> <EOL> grovepi . pinMode ( led , "<STR_LIT>" ) <EOL> switch_led ( <NUM_LIT:0> ) <EOL> oled_init ( ) <EOL> oled_clearDisplay ( ) <EOL> oled_setNormalDisplay ( ) <EOL> oled_setVerticalMode ( ) <EOL> do_send = <NUM_LIT:0> <EOL> slider_value = <NUM_LIT:0> <EOL> msg_string = "<STR_LIT>" <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> while <NUM_LIT:1> : <EOL> slider_value = read_slider_value ( slider , slider_value ) <EOL> if ( slider_value == <NUM_LIT:100> ) : <EOL> do_send = <NUM_LIT:1> <EOL> if ( slider_value == <NUM_LIT:0> ) : <EOL> do_send = <NUM_LIT:0> <EOL> if ( do_send == <NUM_LIT:1> ) : <EOL> send_to_hcp ( http , url , headers , slider_value ) <EOL> poll_from_hcp ( http , url , headers ) <EOL> redraw_oled ( slider_value , do_send , msg_string ) <EOL> time . sleep ( <NUM_LIT:1> ) </s>
