Esempio n. 1
0
def read_conf():
   logger.debug("Reading properties file")

   CONFIG_FILE = "fluxa_properties.conf"
   config = SafeConfigParser()
   config.read(CONFIG_FILE)

   logger.debug("Properties file read")

   return config
Esempio n. 2
0
def validate_files(files):
    logger.debug("Validating the input files")
    final_list = []
    file_list = files.split(DELIMITER)
    for file_name in file_list:
        status = os.path.isfile(file_name)
        if status:
            final_list.append(file_name)
        else:
            print "File : %s does not exist. Skipping" % (file_name)
    return (DELIMITER).join(final_list)
Esempio n. 3
0
 def updateDB(self):
     finalJson = {}
     finalJson["stationId"] = self.stationId
     finalJson["subStationId"] = self.subStationId
     finalJson["timeStamp"] = int(time.mktime(datetime.now().timetuple()))
     finalJson["data"] = self.finalDataJson
     try:
         print self.dbserver
         response = requests.post(self.dbserver+"updateDB", json=finalJson, timeout=5)
         print response
         logger.debug(response.text)
     except Exception as e:
         traceback.print_exc()
         print "Failed to upload"
         logger.exception(e)
Esempio n. 4
0
def setupLinks(user):
  global targetRoot
  for filePath in files:
    filePath = filePath.rstrip()
    log.debug("Processing \"{}\"".format(filePath))
    if filePath.startswith("#"):
      log.debug("Skipping comment line: \"{}\"".format(filePath))
      continue;
    if not bool(targetRoot):
      targetRoot = getPath(user, config["TargetDir"], False)
      if not os.path.exists(targetRoot):
        os.makedirs(targetRoot)
      continue
    localPath = getPath(user, filePath, False)
    remotePath = getPath(user, filePath, True)
    if not setupLink(localPath, remotePath):
      continue
Esempio n. 5
0
 def longInteger(self, response):
     longIntReading = []
     try:
         length = len(response) / 2
         for channelNo in range(0, length):
             icount = channelNo * 2
             temp_data = []
             temp_data.append(response[icount+1])
             temp_data.append(response[icount])
             decoder = BinaryPayloadDecoder.fromRegisters(temp_data, endian=Endian.Big)
             value = decoder.decode_32bit_int()
             longIntReading.append(value)
         logger.debug("LongInt: " + str(longIntReading))
         return longIntReading
     except Exception as e:
         traceback.print_exc()
         print e
         print e.message
Esempio n. 6
0
def main():
  try:
    loadConfig()
    loadFileList("filelist.files")
    users = [ os.getenv("USERNAME") ]
    if config["AllUsers"]:
      users = [userDir.name for userDir in pathlib.Path("{}/Users".format(os.getenv("SYSTEMDRIVE"))).glob('*') if userDir.name not in ['Default', 'Default User', 'Public', 'All Users'] and userDir.is_dir() ]
    for user in users:
      log.debug("Running for {}".format(user))
      usernames = getAliases(user)
      setupCustomVars(usernames)
      setupSyncServices(usernames)
      log.debug("config:\n{}".format(json.dumps(config, sort_keys=True, indent=2)))
      setupLinks(user)
  finally:
    for file in openFiles:
      try:
        file.close()
      except Exception as e:
        log.warning(e)
Esempio n. 7
0
 def sendSMS(self):
     file = open("lastSMS.json", "r")
     lastSMS = file.read()
     try:
         lastSMS = json.loads(lastSMS)
     except:
         lastSMS = {}
     file.close()
     currentTime = int(time.mktime(datetime.now().timetuple()))
     for everyItem in self.finalDataJson.keys():
         if int(self.finalDataJson[everyItem]) == 1:
             try:
                 if currentTime - int(lastSMS[everyItem]) > 300:
                     tempdata = {
                         "numbers": self.numbers,
                         "message": str(everyItem) + " tripped",
                         "sender": "ILENSS",
                         "authKey": ""
                     }
                     resp = requests.post(smsUrl, data=json.dumps(tempdata))
                     print resp.text
                     logger.debug(resp.text)
                     if "Success" in resp.text:
                         print "sms sent"
             except:
                 lastSMS[everyItem] = str(currentTime)
                 tempdata = {
                     "numbers": self.numbers,
                     "message": str(everyItem) + " tripped",
                     "sender": "ILENSS",
                     "authKey": ""
                 }
                 resp = requests.post(smsUrl, data=json.dumps(tempdata))
                 print resp.text
                 logger.debug(resp.text)
                 if "Success" in resp.text:
                     print "sms sent"
     file = open("lastSMS.json", "w")
     file.write(json.dumps(lastSMS))
     file.close()
Esempio n. 8
0
def setupLink(linkPath, targetPath):
  log.debug("\"{}\" -> \"{}\"".format(linkPath, targetPath))
  if os.path.islink(linkPath):
    log.debug("Skipping \"{}\" because it is already a symlink".format(linkPath))
    return False
  if not pathlib.Path(linkPath).exists():
    log.debug("Skipping \"{}\" because it doesn't exist".format(linkPath))
    return False
  copyPath = targetPath
  if pathlib.Path(targetPath).exists():
    remotePathBak = targetPath
    i=1
    while pathlib.Path(remotePathBak).exists():
      remotePathBak = "{}-bak-{}".format(targetPath, i)
      i+=1
    log.info("\"{}\" already exists. Will backup local files to \"{}\"".format(targetPath, remotePathBak))
    copyPath = remotePathBak
  parentPath = pathlib.Path(copyPath).parent.absolute()
  if not parentPath.exists():
    os.makedirs(parentPath)
  if os.path.isdir(linkPath):
    log.debug("\"{}\" is directory".format(linkPath))
    shutil.copytree(linkPath, copyPath)
    shutil.rmtree(linkPath)
    log.info("Linking \"{}\" -> \"{}\"".format(linkPath, targetPath))
    mklinkDir(linkPath, targetPath)
  elif os.path.isfile(linkPath):
    log.debug("\"{}\" is file".format(linkPath))
    shutil.copy(linkPath, copyPath)
    os.remove(linkPath)
    log.info("Linking \"{}\" -> \"{}\"".format(linkPath, targetPath))
    mklink(linkPath, targetPath)
  else:
    log.error("\"{}\" is neither symlink, directory, nor file".format(linkPath))
    return False
  return True
Esempio n. 9
0
 def readModbus_CS(self, collector, client):
     data = []
     print "calling tcp client"
     try:
         connect_status = client.connect()
         logger.debug("connect_status: "+ str(connect_status))
         response = client.read_coils(int(collector["start"]), int(collector["totalRegister"]), unit=int(collector["deviceId"]))
         logger.debug("raw response: " + str(response))
         logger.debug("registers: " + str(response.bits))
         data = response.bits
         if "-sf" in str(collector["CollectorType"]).lower() or "float" in str(collector["CollectorType"]).lower():
             logger.debug("Float or sFloat is not available for this collector")
         client.close()
     except Exception as e:
         client.close()
         traceback.print_exc()
         print "Some error Occured:" + str(e)
         print e.message
     return data
Esempio n. 10
0
 def readModbus_IS(self, collector, client):
     data = []
     print "calling rtu client"
     try:
         connect_status = client.connect()
         logger.debug("connect_status: "+ str(connect_status))
         response = client.read_discrete_inputs(int(collector["start"]), int(collector["totalRegister"]), unit=int(collector["deviceId"]))
         logger.debug("raw response: " + str(response))
         logger.debug("registers: " + str(response.bits))
         data = response.registers
         if "sfloat" in str(collector["CollectorType"]).lower():
             data = self.swappedFloat(response.bits)
         elif "float" in str(collector["CollectorType"]).lower():
             data = self.simpleFloat(response.bits)
         client.close()
     except Exception as e:
         client.close()
         traceback.print_exc()
         print "Some error Occured:" + str(e)
         print e.message
     return data
Esempio n. 11
0
def setupSyncService(usernames, varName, dirNameVariants):
  log.debug("Setting up sync service: {}".format(varName))
  searchDirs = []
  for rootDirName in dirNameVariants:
    searchDirs.append(getPath(usernames[0], "$HOME/{}".format(rootDirName), False))
    searchDirs.append(getPath(usernames[0], "$DOCUMENTS/{}".format(rootDirName), False))
    searchDirs.append(getPath(usernames[0], "{}/Users/{}/{}".format(os.getenv("HOMEDRIVE"), os.getenv("USERNAME"), rootDirName), False))
    searchDirs.append(getPath(usernames[0], "{}/Users/{}/Documents/{}".format(os.getenv("HOMEDRIVE"), os.getenv("USERNAME"), rootDirName), False))
    searchDirs.append(getPath(usernames[0], "{}/{}".format(os.getenv("HOMEDRIVE"), rootDirName), False))
    searchDirs.append(getPath(usernames[0], "{}/{}".format(os.getenv("SYSTEMDRIVE"), rootDirName), False))
    for driveLetter in string.ascii_uppercase:
      searchDirs.append(getPath(usernames[0], "{}:/{}".format(driveLetter, rootDirName), False))
      for username in usernames:
        searchDirs.append(getPath(username, "{}:/Users/{}/{}".format(driveLetter, username, rootDirName), False))
  for searchDir in searchDirs:
    log.debug("Checking: {}".format(searchDir))
    if os.path.exists(searchDir):
      config[varName] = searchDir
      log.debug("{}: \"{}\"".format(varName, searchDir))
      break
  if not os.path.exists(config[varName]):
    log.info("Could not find {} directory.".format(varName))
Esempio n. 12
0
    def readConfig(self):
        parser = SafeConfigParser()
        global sleepTime, uploadData, triggerSMS
        parser.read("setup.conf")
        try:
            self.uploadIp = parser.get("siteSettings", "uploadIp")
            self.dataDumpJsonFilename = parser.get("siteSettings", "jsonFilename")      # file to upload temporary data during offline state
            self.uploadPort = parser.get("siteSettings", "uploadPort")
            self.serviceUrl = parser.get("siteSettings", "serviceUrl")
            self.totalCollector = int(parser.get("siteSettings", "totalCollectors"))
            self.totalParamteters = int(parser.get("siteSettings", "totalParameters"))
            self.topic = parser.get("siteSettings", "topic")
            self.compare = int(parser.get("siteSettings", "compare"))
            self.dbserver = parser.get("siteSettings", "dbserver")
            self.dataFormat = parser.get("siteSettings", "dataFormat")
            sleepTime = int(parser.get("siteSettings", "sleepTime"))
            uploadData = int(parser.get("siteSettings", "uploadData"))
            try:
                self.numbers = parser.get("siteSettings", "numbers")
                triggerSMS = int(parser.get("siteSettings", "triggerSMS"))
            except:
                self.numbers = ""
                triggerSMS = 0
        except Exception as e:
            traceback.print_exc()
            logger.debug("Exception in reading siteSettings")
            logger.exception(e)

        try:
            self.stationId = parser.get("siteDetails", "stationId")
            self.subStationId = parser.get("siteDetails", "subStationId")
            self.siteLocation = parser.get("siteDetails", "siteLocation")
            self.dataJson["stationId"] = self.stationId
            self.dataJson["siteLocation"] = self.siteLocation
        except Exception as e:
            traceback.print_exc()
            logger.debug("Exception in reading siteDetails")
            logger.exception(e)

        for i in range(1, self.totalCollector + 1):
            try:
                tempCollectorId = parser.get("Collector"+str(i), "CollectorId")
                tempCollectorType = parser.get("Collector"+str(i), "CollectorType")
                deviceIp = parser.get("Collector"+str(i), "deviceIp")
                start = parser.get("Collector"+str(i), "start")
                proxy = parser.get("Collector"+str(i), "proxy")
                comPort = parser.get("Collector"+str(i), "comPort")
                deviceId = parser.get("Collector"+str(i), "deviceId")
                totalRegister = parser.get("Collector"+str(i), "totalRegister")
                baudRate = parser.get("Collector"+str(i), "baudRate")
                self.dataJson["collectors"][tempCollectorId] = {}
                self.dataJson["collectors"][tempCollectorId]["CollectorId"] = tempCollectorId
                self.dataJson["collectors"][tempCollectorId]["CollectorType"] = tempCollectorType
                self.dataJson["collectors"][tempCollectorId]["deviceIp"] = deviceIp
                self.dataJson["collectors"][tempCollectorId]["start"] = start
                self.dataJson["collectors"][tempCollectorId]["comPort"] = comPort
                self.dataJson["collectors"][tempCollectorId]["deviceId"] = deviceId
                self.dataJson["collectors"][tempCollectorId]["proxy"] = proxy
                self.dataJson["collectors"][tempCollectorId]["totalRegister"] = totalRegister
                self.dataJson["collectors"][tempCollectorId]["baudRate"] = baudRate
                self.dataJson["collectors"][tempCollectorId]["parameters"] = {}
            except Exception as e:
                traceback.print_exc()
                logger.exception(e)

        for i in range(1, self.totalParamteters + 1):
            try:

                tempParameterId = parser.get("Parameter"+str(i), "parameterId")
                tempParameterName = parser.get("Parameter"+str(i), "parameterName")
                dataType = parser.get("Parameter"+str(i), "dataType")
                channelNo = parser.get("Parameter"+str(i), "channelNo")
                collector = parser.get("Parameter"+str(i), "collector")
                coeffA = parser.get("Parameter"+str(i), "coeffA")
                coeffB = parser.get("Parameter"+str(i), "coeffB")
                feeder = str(parser.get("Parameter"+str(i), "feeder")).strip()
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)] = {}
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)]["parameterId"] = tempParameterId
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)]["parameterName"] = tempParameterName
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)]["dataType"] = dataType
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)]["feeder"] = feeder
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)]["channelNo"] = channelNo
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)]["coeffA"] = coeffA
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)]["coeffB"] = coeffB
                self.dataJson["collectors"][collector]["parameters"]["parameter"+str(i)]["value"] = ""
                #self.dataJson["collectors"][collector]["parameters"][tempParameterId]["value"] = ""

            except Exception as e:
                traceback.print_exc()
                logger.exception(e)
        #print self.dataJson
        logger.debug("DataJson: ")
        logger.debug(self.dataJson)
Esempio n. 13
0
    def uploadData(self):
        try:
            finalData = {}
            finalData["stationId"] = self.stationId
            finalData["subStationId"] = self.subStationId
            finalData["data"] = self.finalDataJson
            finalData["timestamp"] = str(datetime.now()).split(".")[0]

            jsonFileObj = open(self.dataDumpJsonFilename, "r+")
            try:
                jsonDumpData = json.load(jsonFileObj)
            except:
                jsonDumpData = []

            #print "FinalData: ", finalData
            #print "finaldatajson: ", str(finalData["data"])
            #print "previousData : ", str(self.previousDataJson)
            if self.compare == 1:
                if not self.compareJson(finalData["data"],self.previousDataJson):

                    try:
                        tempDataJson = copy.deepcopy(finalData["data"])
                        for everyItem in tempDataJson:
                            if self.previousDataJson!= '' and finalData["data"][everyItem] == self.previousDataJson[everyItem]:
                                finalData["data"].pop(everyItem)
                    except Exception as e:
                        traceback.print_exc()
                        print e
                        pass
                    print "Uploading: ", str(finalData).replace("ON", "T").replace("OFF", "F").replace("True", "T").replace("False", "F")

                    try:
                        resp = requests.post(self.serviceUrl + "updateCircuit",
                                         data=str(finalData).replace("ON", "T").replace("OFF", "F").replace("True", "T").replace("False", "F"))
                        logger.debug(resp)
                    except:
                        # store the finalDataJson until connectivity returns and gets pushed back to the server
                        pass

                    if self.connectServer():
                        MQTTresponse = self.mqttc.publish(self.topic, str(self.finalDataJson).replace("ON", "T").replace("OFF", "F").replace("True", "T").replace("False", "F"))
                        if not MQTTresponse.is_published():
                            self.mqttc.reconnect()
                            MQTTresponse = self.mqttc.publish(self.topic,
                                                              str(self.finalDataJson).replace("ON", "T").replace("OFF", "F").replace("True", "T").replace("False", "F"))
                        logger.debug(MQTTresponse.is_published())
                        self.mqttc.loop(2)  # timeout = 2s
                        self.previousDataJson = copy.deepcopy(finalData["data"])
                        print "Uploaded: ", MQTTresponse.is_published()
                        self.connectivity = True         # use this flag to check for internet connectivity
                    else:
                        print "Failed to connect to mqtt"
                        logger.debug("Error while uploading to the mqtt server")
                        logger.debug("Failed to connect to the mqtt server")
                        self.connectivity = False        # use this flag to check for internet connectivity
                        # in that case, store the finalDataJson until connectivity returns and gets pushed back to the server
                        # write JSON into the file
                        jsonFileObj.seek(0)
                        jsonDumpData.append(finalData)
                        json.dump(jsonDumpData, jsonFileObj)
                        fileUploaderObj.writeintoJSON(finalData, self.stationId)
                        self.uploadHistorical = True

            else:
                tempData = {}
                tempData["values"] = self.finalDataJson
                tempData["stationId"] = self.stationId
                tempData["subStationId"] = self.subStationId
                tempData["timeStamp"] = int(time.mktime(datetime.now().timetuple()))
                data = json.dumps(tempData).replace("ON", "T").replace("OFF", "F").replace("True", "T").replace("False", "F")

                print "data:", data
                if self.connectServer():
                    self.mqttc.publish(self.topic, data)
                    self.mqttc.loop(2)  # timeout = 2s
                    self.disconnectServer()
                    self.previousDataJson = copy.deepcopy(finalData["data"])
                    print ("Uploaded")
                    self.connectivity = True  # use this flag to check for internet connectivity
                else:
                    print "Failed to connect to mqtt"
                    logger.debug("Error while uploading to the mqtt server")
                    logger.debug("Failed to connect to the mqtt server")
                    self.connectivity = False  # use this flag to check for internet connectivity
                    # if device fails to transmit the data, save it into a JSON for future publication
                    # write JSON into the file
                    logger.info("Writing data into json")
                    jsonFileObj.seek(0)
                    jsonDumpData.append(tempData)
                    json.dump(jsonDumpData, jsonFileObj)
                    fileUploaderObj.writeintoJSON(finalData, self.stationId)
                    self.uploadHistorical = True         # True if data dump from json file is required

        except Exception as e:
            traceback.print_exc()
            print "Exception"
            logger.exception(e)
            try:
                self.mqttc.disconnect()
                self.connectionFailed = 1
            except Exception as e:
                traceback.print_exc()
                logger.exception(e)
                self.connectionFailed = 1
        finally:
            try:
                jsonFileObj.close()
            except:
                pass
Esempio n. 14
0
    def readData(self):
        self.dataJson["timeStamp"] = str(datetime.now()).split(".")[0]
        for everyCollector in self.dataJson["collectors"]:
            #print self.dataJson["collectors"][everyCollector]
            collectorType = self.dataJson["collectors"][everyCollector]["CollectorType"]
            deviceIp = self.dataJson["collectors"][everyCollector]["deviceIp"]
            # totalRegister = self.dataJson["collectors"][everyCollector]["totalRegister"]
            # start = self.dataJson["collectors"][everyCollector]["start"]

            if "dl8-event" in collectorType.lower():
                data = self.readEventJson(deviceIp, collectorType)
            elif "dl8-datajson" in collectorType.lower():
                data = self.readDataJson(deviceIp, collectorType)

            client = None
            if "modbus" in collectorType.lower():
                try:
                    collector = self.dataJson["collectors"][everyCollector]
                    if "-rtu" in collectorType.lower():
                        client = ModbusSERIALClient(method="rtu", port=collector["comPort"], stopbits=1, bytesize=8,
                                                    parity='N', baudrate=int(collector["baudRate"]))
                    elif "-tcp" in collectorType.lower():
                        client = ModbusTcpClient(host=deviceIp, port=502)
                    if "-hr" in collectorType.lower():
                        data = self.readModbus_HR(collector, client)
                    elif "-ir" in collectorType.lower():
                        data = self.readModbus_IR(collector, client)
                    elif "-cs" in collectorType.lower():
                        data = self.readModbus_CS(collector, client)
                    elif "-is" in collectorType.lower():
                        data = self.readModbus_IS(collector, client)
                    try:
                        client.close()
                    except Exception as e:
                        print "Exception while closing the connection"
                        logger.exception(e)
                except Exception as e:
                    logger.exception(e)
                    print "Unable to connect to Modbus Server"

            try:

                for everyParameter in self.dataJson["collectors"][everyCollector]["parameters"]:
                    try:
                        dataType = self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["dataType"]
                        channelData = data[dataType]
                    except Exception as e:
                        #logger.exception(e)
                        channelData = data
                    coeffA = self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["coeffA"]
                    coeffB = self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["coeffB"]
                    channelno = self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["channelNo"]
                    feeder = self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["feeder"]
                    parameterId = self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["parameterId"]
                    parameterId = feeder + "&"+ parameterId

                    try:
                        if feeder == "":
                            print "Feeder not Defined"
                            raise Exception
                        if coeffA == "" and coeffB == "":
                            if "-bit" in collectorType.lower():
                                bitdata = str(bin(channelData[int(str(channelno).split("-")[0])])[2:]).zfill(16)[::-1]
                                print bitdata
                                self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["value"] = bitdata[int(str(channelno).split("-")[1])]
                            else:
                                self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["value"] = str(
                                    channelData[int(channelno)])
                        else:
                            tempValue = float(channelData[int(channelno)])
                            tempValue = tempValue * float(coeffA)
                            tempValue = round(tempValue + float(coeffB), 2)
                            self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["value"] = str(tempValue)
                        self.finalDataJson[parameterId] = \
                        self.dataJson["collectors"][everyCollector]["parameters"][everyParameter]["value"]
                    except Exception as e:
                        traceback.print_exc()
                        logger.debug("invalid channel number for parameter: ")
            except Exception as e:
                print e.message
                traceback.print_exc()
                logger.exception(e)
        logger.debug("FinalData: ")
        logger.debug(self.dataJson)
Esempio n. 15
0
def stop_application():
    for thread in threading.enumerate():
        try:
            thread.stop()
        except Exception as e:
            logger.debug(str(e) + "  " + thread.name)