Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. import pyodbc
  2. import pandas as pd
  3. #from datetime import timedelta
  4.  
  5. outfile=open(r"C:\Users\Stav\Documents\SeeboProjects\Sec-tools\FanucTable_output_5.tab", "w")
  6. outfile.write("Tag"+"\t"+"start_zero_timestampe"+"\t"+"end_zero_timestampe"+"\t"+"zero_seconds_datediff"+"\n")
  7.  
  8. server = u'amit-test.database.windows.net'
  9. database = u'SecoTools'
  10. username = u'amit'
  11. password = u'Seebo2smart'
  12.  
  13. cn = pyodbc.connect(
  14. 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password,autocommit=True)
  15. cn2 = pyodbc.connect(
  16. 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password,autocommit=True)
  17.  
  18. sql = ' select top 90000 * from [dbo].[fanuc_data_fact] order by tag, timestamp;'
  19. df = pd.read_sql(sql, cn)
  20.  
  21. # Create a cursor from the connection
  22. cursor=cn.cursor()
  23. cursor2=cn2.cursor()
  24.  
  25. cursor.execute(' select top 90000 * from [dbo].[fanuc_data_fact] order by tag, timestamp;')
  26. row = cursor.fetchone()
  27. row_1 = cursor.fetchone() #the next row
  28. flag=False
  29. flag1=False
  30.  
  31.  
  32.  
  33. tag=row[0]
  34. if row[2]==0.0:
  35. start_zero_ts=row[1]
  36. flag=True
  37. while row and row_1:
  38. ID=row[0]
  39. timestampe=row[1]
  40. value=row[2]
  41. ID_1=row_1[0]
  42. timestampe_1=row_1[1]
  43. value_1=row_1[2]
  44. if (ID==ID_1 and value!=0.0 and value_1==0.0) or (ID!=ID_1 and value_1==0.0):
  45. start_zero_ts=row_1[1]
  46. flag=True
  47. print("first_ts",start_zero_ts)
  48. if (ID==ID_1) and (value==0.0) and (value_1==0.0) :
  49. flag1=True
  50. print("continue")
  51. if (flag and flag1 and ((ID!=ID_1 and value==0.0) or (ID==ID_1 and value_1!=0.0 and value==0.0))):
  52. datediff=(timestampe-start_zero_ts).total_seconds() #float
  53. outfile.write(ID+"\t"+str(start_zero_ts)+"\t"+str(timestampe)+"\t"+str(datediff)+"\n")
  54. cursor2.execute("insert into fanuc_data_not_active_attr values (?,?,?,?)", ID, start_zero_ts ,timestampe ,datediff)
  55. cn2.commit()
  56. flag=False
  57. flag1=False
  58.  
  59.  
  60.  
  61. row = row_1
  62. row_1 = cursor.fetchone()
  63.  
  64.  
  65. outfile.close()
  66. cursor.close()
  67. cursor2.close()
  68. del cursor,cursor2, cn, cn2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement