Exemplo n.º 1
0
 def read_object(self, attribute, index=-1):
     if(index == -1):
         try:
             temp_type = self.Name_Hash[attribute]
         except KeyError:
             log("Error in attribute", ERROR)
             return
         sp_temp = re.split('/', temp_type)
         datatype = sp_temp[0]
         index = int(sp_temp[1])
     else:
         datatype = attribute
     for case in switch(datatype):
         if case('DIN'):
             return self.DIN[index].read()
             break
         if case('AOUT'):
             return self.AOUT[index].read()
             break
         if case('AIN'):
             return self.AIN[index].read()
             break
         if case('DOUT'):
             return self.DOUT[index].read()
             break
         if case('DEV_CHAR'):
             return self.DEV_CHAR[index].read()
             break
Exemplo n.º 2
0
 def try_initialize(self):
     try:
         f = open(self.file_1, 'r')
     except IOError:
         log("JSON file doesnt exist/Cant Read", DEBUG)
         return 0
     self.file_lock(f, LOCK)
     f.seek(0, 0)
     line = f.readline()
     Temp = jsonpickle.decode(line)
     if(sys.platform != 'win32'):
         self.file_lock(f, UNLOCK)
     f.close()
     self.AOUT = Temp.AOUT
     self.AIN = Temp.AIN
     self.DOUT = Temp.DOUT
     self.DIN = Temp.DIN
     self.DEV_CHAR = Temp.DEV_CHAR
     self.Name_Hash = Temp.Name_Hash
     self.Topic_Hash = Temp.Topic_Hash
     self.change = Temp.change
     self.file_1 = self.file_1
     self.set_initialized(Temp.flag, Temp.write_time)
     log("Listening Object Initialized", EVENT)
     return 1
Exemplo n.º 3
0
 def addchanges(self, from_prf):
     log("Adding changes", DEBUG)
     while(True):
         temp = from_prf.Delete_Change()
         if (temp == 'Error'):
             return
         sp_temp = re.split('/', temp)
         self.move(from_prf, sp_temp[0], int(sp_temp[1]))
Exemplo n.º 4
0
 def Delete_Change(self):
     log("Poping change", DEBUG)
     if(self.flag != 0):
         self.flag = self.flag - 1
         log("Flag: " + str(self.flag), DEBUG)
         return self.change.pop()
     else:
         return 'Error'
Exemplo n.º 5
0
 def Record_Change(self, type_1):
     log("Recording change " + str(self.initialized), DEBUG)
     if(self.initialized == 1):
         temp = set(self.change)
         if type_1 not in temp:
             self.flag = self.flag + 1
             log("Flag: " + str(self.flag), DEBUG)
             self.change.append(type_1)
Exemplo n.º 6
0
 def read_command(self, msg):
     self.rx_msg = msg
     log("Trying to Acquire JSON file", DEBUG,(datetime.datetime.now()-self.start_time))
     #self.json_lock.acquire()
     sp_msg = re.split('/', self.rx_msg.topic)
     try:
         log("Write " + str(sp_msg[2]) +
             str(sp_msg[3]) + "to JSON File", DEBUG,(datetime.datetime.now()-self.start_time))
         datalog(self.rx_msg.payload,str(sp_msg[2])+ '/' + str(sp_msg[3]),self.device_name,RX,(datetime.datetime.now()-self.start_time))
         self.device.write(self.rx_msg.payload, sp_msg[2], int(sp_msg[3]))
     except ValueError as e:
         print(e)
Exemplo n.º 7
0
 def add_element(self, dtype, name, index, minimum, maximum, value):
     if(dtype == 'AOUT'):
         self.AOUT.append(aout(name, index, minimum, maximum, value))
     elif (dtype == 'AIN'):
         self.AIN.append(ain(name, index, minimum, maximum, value))
     elif (dtype == 'DIN'):
         self.DIN.append(din(name, index, minimum, maximum, value))
     elif (dtype == 'DOUT'):
         self.DOUT.append(dout(name, index, minimum, maximum, value))
     elif (dtype == 'DEV_CHAR'):
         self.DEV_CHAR.append(dev_char(name, index, value))
     else:
         log("Invalid Entry in Excel sheet", ERROR)
Exemplo n.º 8
0
 def write(self, value, attribute, index=-1):
     if(self.initialized == 1):
         if(index == -1):
             try:
                 temp_type = self.Name_Hash[attribute]
             except KeyError:
                 if(self.native):
                     attribute = str(attribute) + 'OUT'
                 else:
                     attribute = str(attribute) + 'IN'
                 try:
                     temp_type = self.Name_Hash[attribute]
                 except KeyError:
                     log("KeyError in write", ERROR)
                     raise ValueError('Bad Attribute')
                     return
             sp_temp = re.split('/', temp_type)
             datatype = sp_temp[0]
             index = int(sp_temp[1])
         else:
             datatype = attribute
             log("Bypass WRITE Access", EVENT)
         # self.Delete_All_Changes()
         self.write_file(value, datatype, int(index))
         return 1
     else:
         if(self.try_initialize()):
             self.write(value, attribute, int(index))
         else:
             log("JSON File Does not Exist to Create Object", WARNING)
             log("Value: " + str(value) + " Not Written to " +
                 str(attribute) + "/" + str(index), DEBUG)
             raise ValueError('No Device')
Exemplo n.º 9
0
 def send_commands(self):
     while True:
         obj_topic = self.device.Delete_Change()
         log("Object to be sent " + str(obj_topic), DEBUG)
         if(obj_topic == 'Error'):
             return
         sp_topic = re.split('/', obj_topic)
         try:
             msg = self.device.read(sp_topic[0], int(sp_topic[1]))
         except ValueError as e:
             print(e)
         print(msg)
         datalog(msg,obj_topic,self.device_name,TX,(datetime.datetime.now()-self.start_time))
         self.mqttc.publish(str(self.dev_topic) + '/' + obj_topic, msg)
Exemplo n.º 10
0
 def update_object(self):
     if(self.initialized):
         try:
             f = open(self.file_1, 'r+')
         except IOError:
             log("JSON File doesnt exist/Cant Read", DEBUG)
             raise ValueError('JSON File Read Error')
             return
         self.file_lock(f, LOCK)
         f.seek(0, 0)
         line = f.readline()
         Temp = jsonpickle.decode(line)
         if(self.write_time != Temp.write_time):
             log("Updated Object", EVENT)
             self.addchanges(Temp)
             self.write_time = Temp.write_time
         else:
             log("Object not Updated", EVENT)
             if(sys.platform != 'win32'):
                 self.file_lock(f, UNLOCK)
             f.close()
             return
         f.seek(0, 0)
         f.write(jsonpickle.encode(Temp))
         log("update_object: Wrote Temp to File", EVENT)
         f.truncate()
         if(sys.platform != 'win32'):
             self.file_lock(f, UNLOCK)
         f.close()
     else:
         self.try_initialize()
Exemplo n.º 11
0
 def read(self, attribute, index=-1):
     if(self.initialized == 1):
         if(index == -1):
             try:
                 temp_type = self.Name_Hash[attribute]
             except KeyError:
                 if(self.native):
                     attribute = str(attribute) + 'IN'
                 else:
                     attribute = str(attribute) + 'OUT'
                 try:
                     temp_type = self.Name_Hash[attribute]
                 except KeyError:
                     log("KeyError Exception in read", ERROR)
                     raise ValueError('Bad Attribute')
                     return
             sp_temp = re.split('/', temp_type)
             datatype = sp_temp[0]
             index = int(sp_temp[1])
         else:
             datatype = attribute
             log("Bypass READ Access", EVENT)
         return self.read_file(datatype, int(index))
     else:
         if(self.try_initialize()):
             return self.read(attribute, int(index))
         else:
             log("JSON File Does not exist to Create Object", WARNING)
             log("Unable to Read from " +
                 str(attribute) + "/" + str(index), DEBUG)
             raise ValueError('No Device')
Exemplo n.º 12
0
 def write_file(self, value, datatype, index):
     try:
         f = open(self.file_1, 'r+')
     except IOError:
         log("JSON File Doesn't Exist", ERROR)
         f = open(self.file_1, 'w')
         self.file_lock(f, LOCK)
         self.write_time = datetime.datetime.now()
         f.write(jsonpickle.encode(self))
         self.file_lock(f, UNLOCK)
         f.close()
         self.Delete_All_Changes()
         log("Created JSON file", EVENT)
         return
     self.file_lock(f, LOCK)
     line = f.readline()
     Temp = jsonpickle.decode(line)
     for case in switch(datatype):
         if case('DIN'):
             Temp.DIN[index].write(value)
             if(self.app_name != "MQTT"):
                 Temp.Record_Change('DIN/' + str(index))
             break
         if case('AOUT'):
             Temp.AOUT[index].write(value)
             if(self.app_name != "MQTT"):
                 Temp.Record_Change('AOUT/' + str(index))
             break
         if case('AIN'):
             Temp.AIN[index].write(value)
             if(self.app_name != "MQTT"):
                 Temp.Record_Change('AIN/' + str(index))
             break
         if case('DOUT'):
             Temp.DOUT[index].write(value)
             if(self.app_name != "MQTT"):
                 Temp.Record_Change('DOUT/' + str(index))
             break
         if case('DEV_CHAR'):
             Temp.DEV_CHAR[index].write(value)
             if(self.app_name != "MQTT"):
                 Temp.Record_Change('DEV_CHAR/' + str(index))
             break
         if case():
             log('Error in DataType', ERROR)
             break
     self.write_time = datetime.datetime.now()
     Temp.write_time = self.write_time
     f.seek(0, 0)
     f.write(jsonpickle.encode(Temp))
     log("wrote " + str(value) + " to " + str(datatype) + str(index), DEBUG, (datetime.datetime.now()-self.start_time))
     f.truncate()
     if(sys.platform != 'win32'):
         self.file_lock(f, UNLOCK)
     f.close()
     # self.Delete_All_Changes()
     return
Exemplo n.º 13
0
 def read_file(self, datatype, index):
     try:
         f = open(self.file_1, 'r')
         self.file_lock(f, LOCK)
         f.seek(0, 0)
         line = f.readline()
     except IOError:
         log("JSON file doesnt exist/Cant Read", ERROR)
         log("Trying Direct Object Read", WARNING)
         return self.read_object(datatype, index)
     Temp = jsonpickle.decode(line)
     for case in switch(datatype):
         if case('DIN'):
             data = Temp.DIN[index].read()
             break
         if case('AOUT'):
             data = Temp.AOUT[index].read()
             break
         if case('AIN'):
             data = Temp.AIN[index].read()
             break
         if case('DOUT'):
             data = Temp.DOUT[index].read()
             break
         if case('DEV_CHAR'):
             data = Temp.DEV_CHAR[index].read()
             break
     if(sys.platform != 'win32'):
         self.file_lock(f, UNLOCK)
     f.close()
     log("Read " + str(data) + " from " + str(datatype) + str(index), DEBUG,(datetime.datetime.now()-self.start_time))
     return data
Exemplo n.º 14
0
 def move(self, new_profile, datatype, index):
     for case in switch(datatype):
         if case('DIN'):
             self.DIN[index] = new_profile.DIN[index]
             self.Record_Change('DIN/' + str(index))
             break
         if case('AOUT'):
             self.AOUT[index] = new_profile.AOUT[index]
             self.Record_Change('AOUT/' + str(index))
             break
         if case('AIN'):
             self.AIN[index] = new_profile.AIN[index]
             self.Record_Change('AIN/' + str(index))
             break
         if case('DOUT'):
             self.DOUT[index] = new_profile.DOUT[index]
             self.Record_Change('DOUT/' + str(index))
             break
         if case('DEV_CHAR'):
             self.DEV_CHAR[index] = new_profile.DEV_CHAR[index]
             self.Record_Change('DEV_CHAR/' + str(index))
             break
     log("updated " + str(datatype) + str(index), DEBUG)
Exemplo n.º 15
0
 def run(self):
     self.mqttc = mqtt.Client(
         client_id="", clean_session=True, userdata=None, protocol=3)
     self.mqttc.on_connect = self.on_connect
     self.mqttc.on_publish = self.on_publish
     self.mqttc.on_subscribe = self.on_subscribe
     self.mqttc.on_message = self.on_message
     # Uncomment to enable debug messages
     self.mqttc.on_log = self.on_log
     self.mqttc.connect(self.ip, self.port)
     # mqttc.connect('ts7800-15.ece.ncsu.edu',8883)
     self.mqttc.loop_start()
     self.init_event.wait()
     self.currtime = time.time()
     # self.observer.schedule(event_handler, 'json/' , recursive=True)
     # self.observer.start()
     while True:
         time.sleep(1)
         try:
             # self.update_event.wait()
             if((time.time() - self.currtime) > TIME_CONSTANT):
                 log("Trying to Acquire JSON file", DEBUG,(datetime.datetime.now()-self.start_time))
                 #self.json_lock.acquire()
                 try:
                     log("Check JSON for Update", DEBUG, (datetime.datetime.now()-self.start_time))
                     self.device.update_object()
                 except ValueError as e:
                     print(e)
                     continue
                 if(self.device.updated()):
                     log("Sending Commands", EVENT, (datetime.datetime.now()-self.start_time))
                     self.send_commands()
                 self.currtime = time.time()
                 #self.json_lock.release()
             # self.update_event.clear()
         except KeyboardInterrupt:
             self.mqttc.loop_stop()
             self.mqttc.disconnect()
             print("Exited Process")
             break
Exemplo n.º 16
0
 def __init__(self, i_file_1, app_name=''):
     self.native = 0
     self.AOUT = []
     self.AIN = []
     self.DOUT = []
     self.DIN = []
     self.DEV_CHAR = []
     self.Name_Hash = {}
     self.Topic_Hash = {}
     self.change = ['Error']
     self.file_1 = 'json/' + str(i_file_1) + '.json'
     self.flag = 0
     self.write_time = 0
     self.initialized = 0
     self.start_time = (datetime.datetime.now())
     try:
         book = xlrd.open_workbook(
             str('conf/profile/' + i_file_1) + '.xlsx')
         self.native = 1
         sh = book.sheet_by_index(1)
         # print sh.name, sh.nrows, sh.ncols
         self.xlx_parser(sh)
         self.file_1 = 'json/' + str(self.read_object(
             'Dev_Name')) + '_' + str(int(self.read_object('Dev_HW_Ver'))) + '.json'
         self.set_initialized()
         if(not os.path.exists(self.file_1)):
             f = open(self.file_1, 'w')
             self.file_lock(f, LOCK)
             self.write_time = datetime.datetime.now()
             f.write(jsonpickle.encode(self))
             if(sys.platform != 'win32'):
                 self.file_lock(f, UNLOCK)
             f.close()
             log("Created JSON File", EVENT)
     except IOError:
         log("Not Native Process", DEBUG)
         self.native = 0
     self.app_name = str(app_name)
     log("Initialized bit:" + str(self.initialized), DEBUG)
Exemplo n.º 17
0
 def updated(self):
     log("Flag: " + str(self.flag), DEBUG)
     if(self.flag > 0):
         return True
     #self.log("updated: No Recorded Changes")
     return False
Exemplo n.º 18
0
 def write(self, value):
     if (int(value) >= self.minimum and int(value) <= self.maximum):
         self.value = int(value)
     else:
         log('DOUT/' + str(self.index) + ': Write Out of Bounds', WARNING)
Exemplo n.º 19
0
 def set_initialized(self, flag=0, write_time=0):
     self.flag = flag
     self.write_time = write_time
     self.initialized = 1
     log("Initialized " + str(self.initialized), DEBUG)
Exemplo n.º 20
0
 def write(self, value):
     if (float(value) >= self.minimum and float(value) <= self.maximum):
         self.value = float(value)
     else:
         log('AIN/' + str(self.index) + ': Write Out of Bounds', WARNING)