def check_settings(): generate = str(sensorinfo["generate"]) if not (generate == "data" or generate == "graph" or generate == "both"): sys.exit("Error parsing JSON dictionary: 'generate' must be 'data', 'graph', or 'both'.") try: int(sensorinfo["delay"]) except ValueError as err: sys.exit("Error parsing JSON dictionary: 'delay' must be an integer value.") data_dir_str = str(sensorinfo["datadirectory"]) if not (data_dir_str.startswith("/") and data_dir_str.endswith("/")): sys.exit("Error parsing JSON dictionary: 'datadirectory' should begin and end with '/' character.") num_sensors = int(len(sensorinfo["sensors"])) if num_sensors < 1: sys.exit("Error parsing JSON dictionary: there must be at least one sensor configured.") graph_arr = [] for i in range(num_sensors): if int(sensorinfo["sensors"][i]["graphnumber"]) not in graph_arr: graph_arr.append(int(sensorinfo["sensors"][i]["graphnumber"])) interpret = str(sensorinfo["sensors"][i]["interpret"]) if not (interpret == "bool" or interpret == "double" or interpret == "string" or interpret == "255"): sys.exit( "Error parsing JSON dictionary at {}: 'interpret' must be 'double', 'bool', or 'string'.".format( str(sensorinfo["sensors"][i]["legend"]) ) ) try: openurl(str(sensorinfo["sensors"][i]["refreshurl"])) except Exception: sys.exit( "Error parsing JSON dictionary at {}: the refresh URL of the {} sensor does not seem to be correct (or your Z-Way server is unreachable!!).".format( str(sensorinfo["sensors"][i]["legend"]) ) ) try: urllib2.urlopen(str(sensorinfo["sensors"][i]["dataurl"]), timeout=1) except Exception: sys.exit( "Error parsing JSON dictionary at {0}: the data URL of the {0} sensor does not seem to be correct (or your Z-Way server is unreachable!!).".format( str(sensorinfo["sensors"][i]["legend"]) ) ) if not is_bgcolor(str(sensorinfo["sensors"][i]["linecolor"])): sys.exit( "Error parsing JSON dictionay at {}: 'linecolor' must be a hexadecimal RGB value (i.e. #FAF0E6)".format( str(sensorinfo["sensors"][i]["legend"]) ) ) indx = 0 for i in sorted(graph_arr): if indx != i: sys.exit( "Error parsing JSON dictionary: 'graphnumber' must be an integer between 0 and {} and must not skip any integers in that range.".format( (num_sensors - 1) ) ) indx += 1
def check_settings(): generate = str(sensorinfo["generate"]) if not (generate == "data" or generate == "graph" or generate == "both"): sys.exit( "Error parsing JSON dictionary: 'generate' must be 'data', 'graph', or 'both'." ) try: int(sensorinfo["delay"]) except ValueError as err: sys.exit( "Error parsing JSON dictionary: 'delay' must be an integer value.") data_dir_str = str(sensorinfo["datadirectory"]) if not (data_dir_str.startswith('/') and data_dir_str.endswith('/')): sys.exit( "Error parsing JSON dictionary: 'datadirectory' should begin and end with '/' character." ) num_sensors = int(len(sensorinfo["sensors"])) if num_sensors < 1: sys.exit( "Error parsing JSON dictionary: there must be at least one sensor configured." ) graph_arr = [] for i in range(num_sensors): if int(sensorinfo["sensors"][i]["graphnumber"]) not in graph_arr: graph_arr.append(int(sensorinfo["sensors"][i]["graphnumber"])) interpret = str(sensorinfo["sensors"][i]["interpret"]) if not (interpret == 'bool' or interpret == 'double' or interpret == 'string' or interpret == '255'): sys.exit( "Error parsing JSON dictionary at {}: 'interpret' must be 'double', 'bool', or 'string'." .format(str(sensorinfo["sensors"][i]["legend"]))) try: openurl(str(sensorinfo["sensors"][i]["refreshurl"])) except Exception: sys.exit( "Error parsing JSON dictionary at {}: the refresh URL of the {} sensor does not seem to be correct (or your Z-Way server is unreachable!!)." .format(str(sensorinfo["sensors"][i]["legend"]))) try: urllib2.urlopen(str(sensorinfo["sensors"][i]["dataurl"]), timeout=1) except Exception: sys.exit( "Error parsing JSON dictionary at {0}: the data URL of the {0} sensor does not seem to be correct (or your Z-Way server is unreachable!!)." .format(str(sensorinfo["sensors"][i]["legend"]))) if not is_bgcolor(str(sensorinfo["sensors"][i]["linecolor"])): sys.exit( "Error parsing JSON dictionay at {}: 'linecolor' must be a hexadecimal RGB value (i.e. #FAF0E6)" .format(str(sensorinfo["sensors"][i]["legend"]))) indx = 0 for i in sorted(graph_arr): if indx != i: sys.exit( "Error parsing JSON dictionary: 'graphnumber' must be an integer between 0 and {} and must not skip any integers in that range." .format((num_sensors - 1))) indx += 1
def data_gen(): st.SENSOR_REF_ERR = False st.SENSOR_DATA_ERR = False t = data_gen.t while True: newdata = [] t += 0.05 newdata.append(t) #refresh urls for i in range (len(st.sensorinfo["sensors"])): if st.sensorinfo["sensors"][i]["refreshurl"] != "null": try: openurl(str(st.sensorinfo["sensors"][i]["refreshurl"])) except Exception, err: if(st.SENSOR_REF_ERR): pass else: st.SENSOR_REF_ERR = True logging.error("Encountered a problem refreshing devices") pass if(st.SENSOR_REF_ERR): newdata.append(-1) else: try: urldata = (urllib2.urlopen(str(st.sensorinfo["sensors"][i]["dataurl"]), timeout=1)).read() val = urldata.strip() if "bool" in st.sensorinfo["sensors"][i]["interpret"]: if "true" in val: newdata.append(int(st.sensorinfo["sensors"][i]["graphto"])) else: newdata.append(0) elif "double" in st.sensorinfo["sensors"][i]["interpret"]: newdata.append(val) elif "string" in st.sensorinfo["sensors"][i]["interpret"]: matchkey = (str(st.sensorinfo["sensors"][i]["interpret"])).strip() if(val == matchkey): newdata.append(int(st.sensorinfo["sensors"][i]["graphto"])) else: newdata.append(0) else: matchkey = (str(st.sensorinfo["sensors"][i]["interpret"])).strip() if(val == matchkey): newdata.append(int(st.sensorinfo["sensors"][i]["graphto"])) else: newdata.append(0) logging.warning("Unrecognized data type from sensor %02d ", (i+1)) except Exception, err: if(st.SENSOR_DATA_ERR): newdata.append(-1) pass else: logging.error("Encountered a problem retrieving data from devices") newdata.append(-1) st.SENSOR_DATA_ERR = True pass
def _update_device(self, device_id): urlbase = ("http://" + self._server_ip + ":" + self._server_port + "/ZWaveAPI/Run/devices[" + str(int(float(device_id))) + "].instances[") data_dicts = self._devices[str(device_id)]["data"] for data_key, data_dict in data_dicts.iteritems(): full_url = (urlbase + data_dict["instance_num"] + "].commandClasses[" + data_dict["command_class"] + "].Get(sensorType = -1)") openurl(full_url)
def write_data(): st.SENSOR_DATA_ERR = False st.SENSOR_REF_ERR = False if (file_has_expired()): st.open_file = open(st.date_arr[2] + st.date_arr[3], 'a') if st.sensorinfo["time"]["refreshurl"] != "null": st.open_file.write(str(int(time.time()))) st.open_file.write(",") for i in range(0, len(st.sensorinfo["sensors"])): nc.sustain_conn() if (st.sensorinfo["sensors"][i]["refreshurl"] != "null"): try: openurl(str(st.sensorinfo["sensors"][i]["refreshurl"])) except Exception, err: if (st.SENSOR_REF_ERR): pass else: st.SENSOR_REF_ERR = True logging.error("Encountered a problem refreshing devices") pass if (st.SENSOR_REF_ERR): st.open_file.write("-1") else: try: urldata = (urllib2.urlopen( str(st.sensorinfo["sensors"][i]["dataurl"]))).read() val = urldata.strip() #still have to deal with string/int/etc data! cant graph string if "bool" in st.sensorinfo["sensors"][i]["interpret"]: if "true" in val: st.open_file.write("1") else: st.open_file.write("0") elif "double" in st.sensorinfo["sensors"][i]["interpret"]: rounded = '{0:.2f}'.format(float(val)) st.open_file.write(rounded) else: matchkey = (str( st.sensorinfo["sensors"][i]["interpret"])).strip() if (val == matchkey): st.open_file.write("1") else: st.open_file.write("0") except Exception, err: if (st.SENSOR_DATA_ERR): st.open_file.write("-1") pass else: st.open_file.write("-1") logging.error( "Enountered a problem retrieving data from sensors" ) st.SENSOR_DATA_ERR = True pass
def write_data(): st.SENSOR_DATA_ERR = False st.SENSOR_REF_ERR = False if(file_has_expired()): st.open_file = open(st.date_arr[2] + st.date_arr[3], 'a') if st.sensorinfo["time"]["refreshurl"] != "null": st.open_file.write(str(int(time.time()))) st.open_file.write(",") for i in range (0,len(st.sensorinfo["sensors"])): nc.sustain_conn() if (st.sensorinfo["sensors"][i]["refreshurl"] != "null"): try: openurl(str(st.sensorinfo["sensors"][i]["refreshurl"])) except Exception, err: if(st.SENSOR_REF_ERR): pass else: st.SENSOR_REF_ERR = True logging.error("Encountered a problem refreshing devices") pass if(st.SENSOR_REF_ERR): st.open_file.write("-1") else: try: urldata = (urllib2.urlopen(str(st.sensorinfo["sensors"][i]["dataurl"]))).read() val = urldata.strip() #still have to deal with string/int/etc data! cant graph string if "bool" in st.sensorinfo["sensors"][i]["interpret"]: if "true" in val: st.open_file.write("1") else: st.open_file.write("0") elif "double" in st.sensorinfo["sensors"][i]["interpret"]: rounded = '{0:.2f}'.format(float(val)) st.open_file.write(rounded) else: matchkey = (str(st.sensorinfo["sensors"][i]["interpret"])).strip() if(val == matchkey): st.open_file.write("1") else: st.open_file.write("0") except Exception, err: if(st.SENSOR_DATA_ERR): st.open_file.write("-1") pass else: st.open_file.write("-1") logging.error("Enountered a problem retrieving data from sensors") st.SENSOR_DATA_ERR = True pass
"name" : "Chuck" } ]''' info json.loads(input) print 'User count:', len(info) for item in info: print 'Name', item['name'] print 'Id', item['id'] print 'Atrtibute', item['x'] # REST instead of SOAP # google geocoding API import urllib form twurl import augment print '* Calling Twitter...' url = augment('https://api.twitter.com/1.1/statuses/user_timeline.json', {'screen_name': 'drchuck', 'count': '2'}) print url connection = urllib.openurl(url) data = connection.read() print data header = connection.info().dict print headers
import urllib url = "http://www.google.com" # check if it's a url (pass startswith a tuple) url.startswith(("http:", "https:", "ftp:")) # read all from web url urllib.openurl(url).read()
def data_gen(): st.SENSOR_REF_ERR = False st.SENSOR_DATA_ERR = False t = data_gen.t while True: newdata = [] t += 0.05 newdata.append(t) #refresh urls for i in range(len(st.sensorinfo["sensors"])): if st.sensorinfo["sensors"][i]["refreshurl"] != "null": try: openurl(str(st.sensorinfo["sensors"][i]["refreshurl"])) except Exception, err: if (st.SENSOR_REF_ERR): pass else: st.SENSOR_REF_ERR = True logging.error( "Encountered a problem refreshing devices") pass if (st.SENSOR_REF_ERR): newdata.append(-1) else: try: urldata = (urllib2.urlopen(str( st.sensorinfo["sensors"][i]["dataurl"]), timeout=1)).read() val = urldata.strip() if "bool" in st.sensorinfo["sensors"][i]["interpret"]: if "true" in val: newdata.append( int(st.sensorinfo["sensors"][i] ["graphto"])) else: newdata.append(0) elif "double" in st.sensorinfo["sensors"][i][ "interpret"]: newdata.append(val) elif "string" in st.sensorinfo["sensors"][i][ "interpret"]: matchkey = (str( st.sensorinfo["sensors"][i]["interpret"]) ).strip() if (val == matchkey): newdata.append( int(st.sensorinfo["sensors"][i] ["graphto"])) else: newdata.append(0) else: matchkey = (str( st.sensorinfo["sensors"][i]["interpret"]) ).strip() if (val == matchkey): newdata.append( int(st.sensorinfo["sensors"][i] ["graphto"])) else: newdata.append(0) #logging.warning("Unrecognized data type from sensor %02d ", (i+1)) except Exception, err: if (st.SENSOR_DATA_ERR): newdata.append(-1) pass else: logging.error( "Encountered a problem retrieving data from devices" ) newdata.append(-1) st.SENSOR_DATA_ERR = True pass