Dockerize everything
This commit is contained in:
32
docker-compose.yml
Normal file
32
docker-compose.yml
Normal file
@@ -0,0 +1,32 @@
|
||||
version : '2'
|
||||
services:
|
||||
mongodb:
|
||||
image: docker.henrypump.cloud/poc_modbus/mongodb
|
||||
ports:
|
||||
- "27017:27017"
|
||||
restart: on-failure
|
||||
scraper:
|
||||
image: docker.henrypump.cloud/poc_modbus/scraper
|
||||
depends_on:
|
||||
- modbus_server
|
||||
restart: on-failure
|
||||
environment:
|
||||
- PLC_IP_ADDRESS=1601.denise.henryres.cloudcnx.net
|
||||
modbus_server:
|
||||
image: docker.henrypump.cloud/poc_modbus/modbus_server
|
||||
depends_on:
|
||||
- mongodb
|
||||
links:
|
||||
- mongodb
|
||||
ports:
|
||||
- "502:502"
|
||||
restart: on-failure
|
||||
environment:
|
||||
- PLC_IP_ADDRESS=1601.denise.henryres.cloudcnx.net
|
||||
# portainer:
|
||||
# image: portainer/portainer
|
||||
# volumes:
|
||||
# - /var/run/docker.sock:/var/run/docker.sock
|
||||
# ports:
|
||||
# - "9000:9000"
|
||||
# restart: on-failure
|
||||
13
poc_modbus_server/Dockerfile
Normal file
13
poc_modbus_server/Dockerfile
Normal file
@@ -0,0 +1,13 @@
|
||||
FROM python:2.7
|
||||
|
||||
# Install some python packages
|
||||
RUN pip install requests pymongo pymodbus cryptography pyasn1
|
||||
RUN pip install git+https://github.com/Henry-Pump/Pycomm-Helper.git
|
||||
RUN pip install git+https://github.com/ruscito/pycomm.git
|
||||
|
||||
# Copy source files
|
||||
RUN mkdir /root/poc_to_modbus
|
||||
COPY poc_modbus_server/poc_to_modbus.py /root/poc_to_modbus/poc_to_modbus.py
|
||||
COPY poc_modbus_server/run_server.py /root/poc_to_modbus/run_server.py
|
||||
|
||||
CMD ["python", "-u", "/root/poc_to_modbus/run_server.py"]
|
||||
@@ -103,8 +103,9 @@ class DigitalTagDataBlock(ModbusSparseDataBlock):
|
||||
super(DigitalTagDataBlock, self).__init__(values)
|
||||
|
||||
def getValues(self, address, count=1):
|
||||
client = MongoClient()
|
||||
client = MongoClient(host="mongodb", port=27017)
|
||||
db = client.tag_data
|
||||
db.authenticate(name="poc", password="poc")
|
||||
tags = db.tag_vals
|
||||
|
||||
for i in range(address, address + count):
|
||||
@@ -120,8 +121,9 @@ class DigitalTagDataBlock(ModbusSparseDataBlock):
|
||||
:param address: The starting address
|
||||
:param values: The new values to be set
|
||||
'''
|
||||
client = MongoClient()
|
||||
client = MongoClient(host="mongodb", port=27017)
|
||||
db = client.tag_data
|
||||
db.authenticate(name="poc", password="poc")
|
||||
tags = db.tag_vals
|
||||
tag_found = tags.find_one({'register_number': address, 'register_type': self.register_type})
|
||||
tag_name = tag_found['tag_name']
|
||||
@@ -172,8 +174,9 @@ class AnalogTagDataBlock(ModbusSparseDataBlock):
|
||||
super(AnalogTagDataBlock, self).__init__(values)
|
||||
|
||||
def getValues(self, address, count=1):
|
||||
client = MongoClient()
|
||||
client = MongoClient(host="mongodb", port=27017)
|
||||
db = client.tag_data
|
||||
db.authenticate(name="poc", password="poc")
|
||||
tags = db.tag_vals
|
||||
try:
|
||||
for i in range(address, address + count):
|
||||
@@ -205,8 +208,9 @@ class AnalogTagDataBlock(ModbusSparseDataBlock):
|
||||
'''
|
||||
print("provided values: {}".format(value))
|
||||
try:
|
||||
client = MongoClient()
|
||||
client = MongoClient(host="mongodb", port=27017)
|
||||
db = client.tag_data
|
||||
db.authenticate(name="poc", password="poc")
|
||||
tags = db.tag_vals
|
||||
tag_found = tags.find_one({'register_number': address, 'register_type': self.register_type})
|
||||
tag_name = tag_found['tag_name']
|
||||
@@ -226,8 +230,9 @@ class AnalogTagDataBlock(ModbusSparseDataBlock):
|
||||
|
||||
|
||||
def getTagsFromDB():
|
||||
client = MongoClient()
|
||||
client = MongoClient(host="mongodb", port=27017)
|
||||
db = client.tag_data
|
||||
db.authenticate(name="poc", password="poc")
|
||||
tags = db.tag_vals
|
||||
print("Found {} tags in the database".format(tags.count()))
|
||||
di_tags_cur = tags.find({'register_type': 'di'})
|
||||
|
||||
@@ -82,7 +82,6 @@ setup(
|
||||
'pymodbus',
|
||||
'cryptography',
|
||||
'pyasn1',
|
||||
'pycomm_helper',
|
||||
],
|
||||
dependency_links=[
|
||||
'https://github.com/Henry-Pump/Pycomm-Helper/tarball/master#egg=pycomm_helper-0.1',
|
||||
|
||||
12
poc_scraper/Dockerfile
Normal file
12
poc_scraper/Dockerfile
Normal file
@@ -0,0 +1,12 @@
|
||||
FROM python:2.7
|
||||
|
||||
# Install some python packages
|
||||
RUN pip install requests pymongo
|
||||
RUN pip install git+https://github.com/Henry-Pump/Pycomm-Helper.git
|
||||
RUN pip install git+https://github.com/ruscito/pycomm.git
|
||||
|
||||
# Copy source files
|
||||
RUN mkdir /root/poc_scraper
|
||||
COPY poc_scraper /root/poc_scraper
|
||||
|
||||
CMD ["python", "-u", "/root/poc_scraper/__main__.py"]
|
||||
@@ -5,6 +5,6 @@ from sys import exit
|
||||
PLC_IP_ADDRESS = getenv("PLC_IP_ADDRESS")
|
||||
if not PLC_IP_ADDRESS:
|
||||
exit("No PLC_IP_ADDRESS set in the environment variables")
|
||||
|
||||
print("PLC_IP_ADDRESS for scraper: {}".format(PLC_IP_ADDRESS))
|
||||
scraper_thread = PLC_to_Mongo(PLC_IP_ADDRESS)
|
||||
scraper_thread.run()
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
import pymongo
|
||||
import json
|
||||
from time import sleep
|
||||
from time import time as now
|
||||
from pymongo import MongoClient
|
||||
import pycomm_helper.utils as plc
|
||||
import os
|
||||
import traceback
|
||||
|
||||
|
||||
class PLC_to_Mongo():
|
||||
def __init__(self, plc_address):
|
||||
client = MongoClient()
|
||||
self.db = client.tag_data
|
||||
self.tag_vals = self.db.tag_vals
|
||||
self.PLC_IP_ADDRESS = plc_address
|
||||
|
||||
def run(self):
|
||||
client = MongoClient(host="mongodb", port=27017)
|
||||
db = client.tag_data
|
||||
db.authenticate(name="poc", password="poc")
|
||||
tag_vals = db.tag_vals
|
||||
|
||||
script_path = os.path.dirname(os.path.realpath(__file__))
|
||||
print("THERE ARE ALREADY {} VALUES IN tag_vals".format(self.tag_vals.count()))
|
||||
print("THERE ARE ALREADY {} VALUES IN tag_vals".format(tag_vals.count()))
|
||||
with open(script_path + '/analog.json', 'rb') as analogfile:
|
||||
analog_list = json.loads(analogfile.read())
|
||||
|
||||
@@ -27,9 +29,9 @@ class PLC_to_Mongo():
|
||||
arr_list = json.loads(arrayfile.read())
|
||||
|
||||
for entry in analog_list + digital_list + arr_list:
|
||||
if self.tag_vals.find({'tag_name': entry['tag_name']}).count() < 1:
|
||||
self.tag_vals.insert(entry)
|
||||
print("NOW THERE ARE {} VALUES IN tag_vals".format(self.tag_vals.count()))
|
||||
if tag_vals.find({'tag_name': entry['tag_name']}).count() < 1:
|
||||
tag_vals.insert(entry)
|
||||
print("NOW THERE ARE {} VALUES IN tag_vals".format(tag_vals.count()))
|
||||
|
||||
while True:
|
||||
for t in analog_list:
|
||||
@@ -39,10 +41,12 @@ class PLC_to_Mongo():
|
||||
t['tag_type'] = plc_val[1]
|
||||
t['val'] = plc_val[0]
|
||||
t['timestamp'] = now()
|
||||
self.tag_vals.update({'tag_name': t['tag_name']}, t)
|
||||
tag_vals.update({'tag_name': t['tag_name']}, t)
|
||||
print("Updated: {} - {}".format(t['tag_name'], t['val']))
|
||||
except Exception as e:
|
||||
print("[ERROR] {} - {}".format(t['tag_name'], e))
|
||||
print("IP: {}, Tag: {}".format(self.PLC_IP_ADDRESS, t['tag_name']))
|
||||
traceback.print_exc()
|
||||
|
||||
for t in digital_list:
|
||||
try:
|
||||
@@ -51,10 +55,12 @@ class PLC_to_Mongo():
|
||||
t['tag_type'] = plc_val[1]
|
||||
t['val'] = plc_val[0]
|
||||
t['timestamp'] = now()
|
||||
self.tag_vals.update({'tag_name': t['tag_name']}, t)
|
||||
tag_vals.update({'tag_name': t['tag_name']}, t)
|
||||
print("Updated: {} - {}".format(t['tag_name'], t['val']))
|
||||
except Exception as e:
|
||||
print("[ERROR] {} - {}".format(t['tag_name'], e))
|
||||
print("IP: {}, Tag: {}".format(self.PLC_IP_ADDRESS, t['tag_name']))
|
||||
traceback.print_exc()
|
||||
|
||||
for a in arr_list:
|
||||
try:
|
||||
@@ -63,17 +69,19 @@ class PLC_to_Mongo():
|
||||
a['val'] = plc_val
|
||||
a['tag_type'] = 'ARRAY'
|
||||
t['timestamp'] = now()
|
||||
self.tag_vals.update({'tag_name': a['tag_name']}, a)
|
||||
tag_vals.update({'tag_name': a['tag_name']}, a)
|
||||
print("Updated: {} - {}".format(a['tag_name'], a['val']))
|
||||
except Exception as e:
|
||||
print("[ERROR] {} - {}".format(a['tag_name'], e))
|
||||
traceback.print_exc()
|
||||
|
||||
sleep(5)
|
||||
|
||||
def purge(self):
|
||||
client = MongoClient()
|
||||
client = MongoClient(host="mongodb", port=27017)
|
||||
db = client.tag_data
|
||||
db.authenticate(name="poc", password="poc")
|
||||
|
||||
self.tag_vals = db.tag_vals
|
||||
self.tag_vals.delete_many({})
|
||||
print("THERE ARE {} VALUES IN tag_vals".format(self.tag_vals.count()))
|
||||
tag_vals = db.tag_vals
|
||||
tag_vals.delete_many({})
|
||||
print("THERE ARE {} VALUES IN tag_vals".format(tag_vals.count()))
|
||||
|
||||
Reference in New Issue
Block a user