def __init__(self, config_path, mode): Driver.__init__(self, config_path, mode) base_url = "http://" + self.host + ":" + self.port + "/rest" self.openhab = openHAB(base_url) self.pre_info = None self.now_info = []
def get_openhab_data(self, *args): openhab_url = 'http://<enter address of openHAB server here>:8080/rest' try: openhab = openHAB(openhab_url) items = openhab.fetch_all_items() rm1 = round(items.get('RM1_Temperature').state, 0) self.rm1_temp = "room1: " + \ str(int(rm1)) + self.deg + "C" rm2 = round(items.get('RM2_Temperature').state, 0) self.rm2_temp = "room2: " + \ str(int(rm2)) + self.deg + "C" rm3 = items.get('RM3_Temperature').state self.rm3_temp = "room3: " + \ str(int(round(rm3,0))) + self.deg + "C" except: self.rm1_temp = "room1: " self.rm2_temp = "room2: " self.rm3_temp = "room3: "
from openhab import openHAB from time import sleep from app import app from socketIO_client import SocketIO, LoggingNamespace base_url = 'http://192.168.1.2:8080/rest' openhab = openHAB(base_url) def checkSTATE(): items = openhab.fetch_all_items() sensor = items.get('LED1') sensorSTATE = sensor.state if (sensorSTATE=="OFF"): return 1 else: return 0 def dummy(): with SocketIO('http://localhost', 5000, LoggingNamespace) as socketIO: #socketIO.emit('messagecs','Take plate from left cupboard') #sleep(7) #socketIO.emit('messageps','Previous Step: Take plate from left cupboard') #socketIO.emit('messagecs','Refill and turn on kettle') #sleep(7) #socketIO.emit('messageps','Previous Step: Refill and turn on kettle') #socketIO.emit('messagecs','Take butter from fridge') items = openhab.fetch_all_items() sensor = items.get('LED1')
urllib2.urlopen('http://' + ArduinoIP + '?L1=' + StartL1 + '&L2=' + StartL2) # Wait 5 secs to allow the message to be read time.sleep(5) #Main loop of code now while True: #Current Time Time = datetime.now().strftime('%H:%M') timeconnection = urllib2.urlopen('http://' + ArduinoIP + '?L1=Time+Is:' + '&L2=' + Time) timeconnection.close() print("Sent Time to Arduino") time.sleep(30) # Outside temp from OpenHAB openhab = openHAB(OpenHabURL) tempreq = openhab.get_item( 'weatherunderground_weather_9d9f927c_current_feelingTemperature') temp = str(float(tempreq.state)) conditionreq = openhab.get_item( 'weatherunderground_weather_9d9f927c_current_conditions') condition = str(conditionreq.state) openhabconnection = urllib2.urlopen('http://' + ArduinoIP + '?L1=Conditions:' + '&L2=' + temp + 'C' + '+/+' + condition) openhabconnection.close() print("Sent Temp to Arduino") time.sleep(30)
def get_states(): thing_url = "http://" + host + ":" + port + "/rest/things" openhab = openHAB(base_url) states = [] item_of_thing_list = [] # Get all things in openHAB things = get(thing_url).json() # print (things) # Get all items in openHAB items = openhab.fetch_all_items() # dict of openHAB items items = items.items() # convert dict into list items = np.asarray(items) items_list = list(items[:, 0]) # print (items_list) # Get all things and items of things in openHAB for thing in things: # print (thing) thing_type = thing["thingTypeUID"] # print (thing_type) thing_name = thing["label"] thing_local_id = thing["UID"] thing_global_id = platform_id + "/" + thing_local_id thing_location = thing["location"] linked_items = thing["channels"] items_state = [] for linked_item in linked_items: item_type = linked_item["itemType"] item_name = linked_item["linkedItems"][0] item_of_thing_list.append(item_name) # Get all item of things. # item_local_id = linked_item["uid"] item_local_id = item_name item_global_id = platform_id + "/" + thing_local_id + "/" + item_local_id item_url = "http://" + host + ":" + port + "/rest/items?recursive=false" item = openhab.get_item(item_name) item_state = str(item.state) can_set_state = True items_state.append({ 'item_type': str(item_type), 'item_name': str(item_name), 'item_global_id': str(item_global_id), 'item_local_id': str(item_local_id), 'item_state': str(item_state), 'can_set_state': str(can_set_state) } ) state = { 'thing_type': str(thing_type), 'thing_name': str(thing_name), 'thing_global_id': str(thing_global_id), 'thing_local_id': str(thing_local_id), 'location': str(thing_location), 'items': items_state } states.append(state) # Get items not belong thing remain_item_list = list(set(items_list) - set(item_of_thing_list)) # print (remain_item_list) # Those items above be converted to things for item_to_thing in remain_item_list: item = openhab.get_item_raw(item_to_thing) item_type = item['type']; thing_type = item_type item_name = item['name']; thing_name = item_name item_state = item['state'] item_local_id = item_name; thing_local_id = thing_name thing_global_id = platform_id + '/' + thing_local_id item_global_id = platform_id + '/' + thing_local_id + '/' + item_local_id location = 'unknown' can_set_state = True items_state = [ { 'item_type': str(item_type), 'item_name': str(item_name), 'item_global_id': str(item_global_id), 'item_local_id': str(item_local_id), 'item_state': str(item_state), 'can_set_state': str(can_set_state) } ] state = { 'thing_type': str(thing_type), 'thing_name': str(thing_name), 'thing_global_id': str(thing_global_id), 'thing_local_id': str(thing_local_id), 'location': str(thing_location), 'items': items_state } states.append(state) return states
def __init__(self, config_path, time_push): Driver.__init__(self, config_path, time_push) base_url = "http://" + self.host + ":" + self.port + "/rest" self.openhab = openHAB(base_url)