'''
MIT License

Copyright (c) 2017 Arshdeep Bahga and Vijay Madisetti

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
from ethjsonrpc import EthJsonRpc
import time
import datetime
import time
import serial

ser = serial.Serial('/dev/ttyACM0', 38400)
contract_addr = '0xc421d5e214ddb07a41d28cf89ee37495aa5edba7'
machineID = 123

#Wait for sometime to initialize serial port
time.sleep(10)

# Connect to Blockchain network
c = EthJsonRpc('192.168.1.20', 8101)

tempCount = 0
vibrationCount = 0
tempThreshold = 200
vibrationThreshold = 100
tempCountThreshold = 1000
vibrationCountThreshold = 1000

while True:
  line = serialTTL.readline()
  data = line.split(',')
  vibration = data[0]  
  temperature = data[1]
  
  if(vibration>vibrationThreshold):
    vibrationCount = vibrationCount+1

  if(temperature>tempThreshold):
    tempCount = tempCount+1

  ts = int(time.time())
  if(vibrationCount>vibrationCountThreshold):
     #Send a transaction to requestService function of 
     #MachineService contract with Machine-ID
    c.call_with_transaction(c.eth_coinbase(), 
    contract_addr, 'requestService(uint256,uint256,string)', 
    [ts, machineID, 'High vibrations'])
    vibrationCount = 0    

  if(tempCount>tempCountThreshold):
    c.call_with_transaction(c.eth_coinbase(), 
    contract_addr, 'requestService(uint256,uint256,string)', 
    [ts, machineID, 'High temperature'])
    tempCount = 0

  time.sleep(1)    

42d0d85782116ce3371588f7f35743f1ec9803b9
<mask token>

<mask token>
time.sleep(10)
<mask token>
while True:
    line = serialTTL.readline()
    data = line.split(',')
    vibration = data[0]
    temperature = data[1]
    if vibration > vibrationThreshold:
        vibrationCount = vibrationCount + 1
    if temperature > tempThreshold:
        tempCount = tempCount + 1
    ts = int(time.time())
    if vibrationCount > vibrationCountThreshold:
        c.call_with_transaction(c.eth_coinbase(), contract_addr,
            'requestService(uint256,uint256,string)', [ts, machineID,
            'High vibrations'])
        vibrationCount = 0
    if tempCount > tempCountThreshold:
        c.call_with_transaction(c.eth_coinbase(), contract_addr,
            'requestService(uint256,uint256,string)', [ts, machineID,
            'High temperature'])
        tempCount = 0
    time.sleep(1)

<mask token>
ser = serial.Serial('/dev/ttyACM0', 38400)
contract_addr = '0xc421d5e214ddb07a41d28cf89ee37495aa5edba7'
machineID = 123
time.sleep(10)
c = EthJsonRpc('192.168.1.20', 8101)
tempCount = 0
vibrationCount = 0
tempThreshold = 200
vibrationThreshold = 100
tempCountThreshold = 1000
vibrationCountThreshold = 1000
while True:
    line = serialTTL.readline()
    data = line.split(',')
    vibration = data[0]
    temperature = data[1]
    if vibration > vibrationThreshold:
        vibrationCount = vibrationCount + 1
    if temperature > tempThreshold:
        tempCount = tempCount + 1
    ts = int(time.time())
    if vibrationCount > vibrationCountThreshold:
        c.call_with_transaction(c.eth_coinbase(), contract_addr,
            'requestService(uint256,uint256,string)', [ts, machineID,
            'High vibrations'])
        vibrationCount = 0
    if tempCount > tempCountThreshold:
        c.call_with_transaction(c.eth_coinbase(), contract_addr,
            'requestService(uint256,uint256,string)', [ts, machineID,
            'High temperature'])
        tempCount = 0
    time.sleep(1)

<mask token>
from ethjsonrpc import EthJsonRpc
import time
import datetime
import time
import serial
ser = serial.Serial('/dev/ttyACM0', 38400)
contract_addr = '0xc421d5e214ddb07a41d28cf89ee37495aa5edba7'
machineID = 123
time.sleep(10)
c = EthJsonRpc('192.168.1.20', 8101)
tempCount = 0
vibrationCount = 0
tempThreshold = 200
vibrationThreshold = 100
tempCountThreshold = 1000
vibrationCountThreshold = 1000
while True:
    line = serialTTL.readline()
    data = line.split(',')
    vibration = data[0]
    temperature = data[1]
    if vibration > vibrationThreshold:
        vibrationCount = vibrationCount + 1
    if temperature > tempThreshold:
        tempCount = tempCount + 1
    ts = int(time.time())
    if vibrationCount > vibrationCountThreshold:
        c.call_with_transaction(c.eth_coinbase(), contract_addr,
            'requestService(uint256,uint256,string)', [ts, machineID,
            'High vibrations'])
        vibrationCount = 0
    if tempCount > tempCountThreshold:
        c.call_with_transaction(c.eth_coinbase(), contract_addr,
            'requestService(uint256,uint256,string)', [ts, machineID,
            'High temperature'])
        tempCount = 0
    time.sleep(1)

'''
MIT License

Copyright (c) 2017 Arshdeep Bahga and Vijay Madisetti

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
from ethjsonrpc import EthJsonRpc
import time
import datetime
import time
import serial

ser = serial.Serial('/dev/ttyACM0', 38400)
contract_addr = '0xc421d5e214ddb07a41d28cf89ee37495aa5edba7'
machineID = 123

#Wait for sometime to initialize serial port
time.sleep(10)

# Connect to Blockchain network
c = EthJsonRpc('192.168.1.20', 8101)

tempCount = 0
vibrationCount = 0
tempThreshold = 200
vibrationThreshold = 100
tempCountThreshold = 1000
vibrationCountThreshold = 1000

while True:
  line = serialTTL.readline()
  data = line.split(',')
  vibration = data[0]  
  temperature = data[1]
  
  if(vibration>vibrationThreshold):
    vibrationCount = vibrationCount+1

  if(temperature>tempThreshold):
    tempCount = tempCount+1

  ts = int(time.time())
  if(vibrationCount>vibrationCountThreshold):
     #Send a transaction to requestService function of 
     #MachineService contract with Machine-ID
    c.call_with_transaction(c.eth_coinbase(), 
    contract_addr, 'requestService(uint256,uint256,string)', 
    [ts, machineID, 'High vibrations'])
    vibrationCount = 0    

  if(tempCount>tempCountThreshold):
    c.call_with_transaction(c.eth_coinbase(), 
    contract_addr, 'requestService(uint256,uint256,string)', 
    [ts, machineID, 'High temperature'])
    tempCount = 0

  time.sleep(1)    
