<commit_before>
'''
    Fodder Factory Controller
    
    Linux module and for Raspberry Pi
    
    Copyright <NAME> 2018
    
'''

import serial
from collections import deque


import kivy
kivy.require('1.10.0') # replace with your current kivy version !
from kivy.config import Config
from kivy.app import App
from kivy.uix.button import Button
#from kivy.uix.togglebutton import ToggleButton
#from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
#from kivy.uix.image import Image
#from kivy.uix.slider import Slider
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle


# Constants

################################################### 
#Colours
###################################################
_FF_RED = [196, 3, 51]
_FF_GREEN = [0, 158, 107]
_FF_BLUE = [0, 135, 189]
_FF_COLD_BLUE = [153, 228, 255]
_FF_FREEZE_BLUE = [204, 241, 255]
_FF_YELLOW = [255, 211, 1]
_FF_GREY = [128, 127, 128]
_FF_SAND = [237, 235, 230]
_FF_SLATE = [69, 72, 90]

FF_RED = [x/255 for x in _FF_RED]
FF_RED.append(1.0)

FF_GREEN = [x/255 for x in _FF_GREEN]
FF_GREEN.append(1.0)

FF_BLUE = [x/255 for x in _FF_BLUE]
FF_BLUE.append(1.0)

FF_COLD_BLUE = [x/255 for x in _FF_COLD_BLUE]
FF_COLD_BLUE.append(1.0)

FF_FREEZE_BLUE = [x/255 for x in _FF_FREEZE_BLUE]
FF_FREEZE_BLUE.append(1.0)

FF_YELLOW = [x/255 for x in _FF_YELLOW]
FF_YELLOW.append(1.0)

FF_GREY = [x/255 for x in _FF_GREY]
FF_GREY.append(1.0)

FF_SAND = [x/255 for x in _FF_SAND]
FF_SAND.append(1.0)

FF_SLATE = [x/255 for x in _FF_SLATE]
FF_SLATE.append(1.0)


#########################################################
# [Index, "SOURCE BLOCK", "Display Label"]
#########################################################
INPUTS_LIST = (
    [0, "IN_INSIDE_TOP_TEMP", "Inside Top"],
    [1, "IN_INSIDE_BOTTOM_TEMP", "Inside Bottom"],
    [2, "IN_OUTSIDE_TEMP", "Outside"],
    [3, "IN_WATER_TEMP", "Header Water"],
    [4, "IN_CABINET_TEMP", "Electrical Cabinet"],
    [5, "IN_LONGRUN_TEMP", "Main Water Tank"]
)

# [Index, "SOURCE BLOCK", "Display Label"]

OUTPUTS_LIST = (
    [0, "OUT_END_LIGHTS", "Lights"], 
    [1, "OUT_WATER_HEATER", "Water Heater"],
    [2, "OUT_FRESH_AIR_FAN", "Inlet Fan"],
    [3, "OUT_CIRCULATION_FAN", "Circulation Fan"],
    [4, "OUT_EXHAUST_FAN", "Exhaust Fan"],
    [5, "OUT_WATERING_SOLENOID_TOP", "Top Sprinklers"],
    [6, "OUT_WATERING_SOLENOID_BOTTOM", "Bottom Sprinklers"]
)

########################################################
INT8_INIT = -127
UINT8_INIT = 255

UINT16_INIT = 65535
FLOAT_INIT = 255.255

########################################################
MESSAGE_FILENAME = "/root/message.log"
MODEM_SERIAL_PORT = "/dev/ttyV0"
MODEM_SERIAL_SPEED = 9600

SERIAL_POLL_INTERVAL = 0.5          # Seconds
UI_UPDATE_INTERVAL = 1.0            # Seconds

HEAT_PALLET = (
    [-50, 15, FF_FREEZE_BLUE ],       # Freeze Blue
    [15, 20, FF_COLD_BLUE ],    # Cold Blue
    [20, 25, FF_GREEN ],     # green - good
    [25, 30, FF_YELLOW ],    # yellow - warm
    [30, 100, FF_RED ]     # red - hot
)
    
    
STATE_UNKNOWN = 2
STATE_ON = 1
STATE_OFF = 0

# Global Variables

message_line_buffer = deque()

ui_inputs_values = [
    FLOAT_INIT, 
    FLOAT_INIT, 
    FLOAT_INIT, 
    FLOAT_INIT, 
    FLOAT_INIT, 
    FLOAT_INIT 
]

ui_outputs_values = [ 
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN 
]

# Global Functions

def get_colour_by_temp(temp_val):
    for [low, high, [r, g, b, a]] in HEAT_PALLET:
        if ((temp_val > low) and (temp_val <= high)):
            #print (temp_val)
            #print(low, high, [r, g, b, a])
            return [r, g, b, a]
    return [1, 1, 1, 1]

class ParsedMessage():
    def __init__(self):
        self.date_string = ""
        self.time_string = ""
        self.source_block_string = ""
        self.dest_block_string = ""
        self.msg_type_string = ""
        self.msg_string = ""
        self.int_val = UINT16_INIT
        self.float_val = FLOAT_INIT
        self.valid = False
        
def parse_message_string(raw_msg_string):
#    print(raw_msg_string)
    parsed_message = ParsedMessage()
    parsed_message.valid = False
    print (raw_msg_string)
    msg_log_file = open(MESSAGE_FILENAME, "a")
    msg_log_file.write(raw_msg_string)
    msg_log_file.close()
    if len(raw_msg_string) > 30:                            #  possibly valid message not init junk
        string_list = raw_msg_string.decode().split(",")
        print (string_list)
        if not("DEBUG" in string_list):<commit_msg>Added message dump from controller<commit_after>
'''
    Fodder Factory Controller
    
    Linux module and for Raspberry Pi
    
    Copyright <NAME> 2018
    
'''

import serial
from collections import deque


import kivy
kivy.require('1.10.0') # replace with your current kivy version !
from kivy.config import Config
from kivy.app import App
from kivy.uix.button import Button
#from kivy.uix.togglebutton import ToggleButton
#from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
#from kivy.uix.image import Image
#from kivy.uix.slider import Slider
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle


# Constants

################################################### 
#Colours
###################################################
_FF_RED = [196, 3, 51]
_FF_GREEN = [0, 158, 107]
_FF_BLUE = [0, 135, 189]
_FF_COLD_BLUE = [153, 228, 255]
_FF_FREEZE_BLUE = [204, 241, 255]
_FF_YELLOW = [255, 211, 1]
_FF_GREY = [128, 127, 128]
_FF_SAND = [237, 235, 230]
_FF_SLATE = [69, 72, 90]

FF_RED = [x/255 for x in _FF_RED]
FF_RED.append(1.0)

FF_GREEN = [x/255 for x in _FF_GREEN]
FF_GREEN.append(1.0)

FF_BLUE = [x/255 for x in _FF_BLUE]
FF_BLUE.append(1.0)

FF_COLD_BLUE = [x/255 for x in _FF_COLD_BLUE]
FF_COLD_BLUE.append(1.0)

FF_FREEZE_BLUE = [x/255 for x in _FF_FREEZE_BLUE]
FF_FREEZE_BLUE.append(1.0)

FF_YELLOW = [x/255 for x in _FF_YELLOW]
FF_YELLOW.append(1.0)

FF_GREY = [x/255 for x in _FF_GREY]
FF_GREY.append(1.0)

FF_SAND = [x/255 for x in _FF_SAND]
FF_SAND.append(1.0)

FF_SLATE = [x/255 for x in _FF_SLATE]
FF_SLATE.append(1.0)


#########################################################
# [Index, "SOURCE BLOCK", "Display Label"]
#########################################################
INPUTS_LIST = (
    [0, "IN_INSIDE_TOP_TEMP", "Inside Top"],
    [1, "IN_INSIDE_BOTTOM_TEMP", "Inside Bottom"],
    [2, "IN_OUTSIDE_TEMP", "Outside"],
    [3, "IN_WATER_TEMP", "Header Water"],
    [4, "IN_CABINET_TEMP", "Electrical Cabinet"],
    [5, "IN_LONGRUN_TEMP", "Main Water Tank"]
)

# [Index, "SOURCE BLOCK", "Display Label"]

OUTPUTS_LIST = (
    [0, "OUT_END_LIGHTS", "Lights"], 
    [1, "OUT_WATER_HEATER", "Water Heater"],
    [2, "OUT_FRESH_AIR_FAN", "Inlet Fan"],
    [3, "OUT_CIRCULATION_FAN", "Circulation Fan"],
    [4, "OUT_EXHAUST_FAN", "Exhaust Fan"],
    [5, "OUT_WATERING_SOLENOID_TOP", "Top Sprinklers"],
    [6, "OUT_WATERING_SOLENOID_BOTTOM", "Bottom Sprinklers"]
)

########################################################
INT8_INIT = -127
UINT8_INIT = 255

UINT16_INIT = 65535
FLOAT_INIT = 255.255

########################################################
MESSAGE_FILENAME = "/root/message.log"
MODEM_SERIAL_PORT = "/dev/ttyV0"
MODEM_SERIAL_SPEED = 9600

SERIAL_POLL_INTERVAL = 0.1          # Seconds
UI_UPDATE_INTERVAL = 1.0            # Seconds

HEAT_PALLET = (
    [-50, 15, FF_FREEZE_BLUE ],       # Freeze Blue
    [15, 20, FF_COLD_BLUE ],    # Cold Blue
    [20, 25, FF_GREEN ],     # green - good
    [25, 30, FF_YELLOW ],    # yellow - warm
    [30, 100, FF_RED ]     # red - hot
)
    
    
STATE_UNKNOWN = 2
STATE_ON = 1
STATE_OFF = 0

# Global Variables

message_line_buffer = deque()

ui_inputs_values = [
    FLOAT_INIT, 
    FLOAT_INIT, 
    FLOAT_INIT, 
    FLOAT_INIT, 
    FLOAT_INIT, 
    FLOAT_INIT 
]

ui_outputs_values = [ 
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN,
    STATE_UNKNOWN 
]

# Global Functions

def get_colour_by_temp(temp_val):
    for [low, high, [r, g, b, a]] in HEAT_PALLET:
        if ((temp_val > low) and (temp_val <= high)):
            #print (temp_val)
            #print(low, high, [r, g, b, a])
            return [r, g, b, a]
    return [1, 1, 1, 1]

class ParsedMessage():
    def __init__(self):
        self.date_string = ""
        self.time_string = ""
        self.source_block_string = ""
        self.dest_block_string = ""
        self.msg_type_string = ""
        self.msg_string = ""
        self.int_val = UINT16_INIT
        self.float_val = FLOAT_INIT
        self.valid = False
        
def parse_message_string(raw_msg_string):
#    print(raw_msg_string)
    parsed_message = ParsedMessage()
    parsed_message.valid = False
    print (raw_msg_string)

    with open(MESSAGE_FILENAME, "a") as msg_log_file:
        print(raw_msg_string, file=msg_log_file)

    if len(raw_msg_string) > 30:                            #  possibly valid message not init junk
        string_list = raw_msg_string.decode().split(",")
        print (string_list)
        if not("DEBUG" in string_list):
{'fraction_non_alphanumeric': 0.18342613822469703, 'fraction_numerical': 0.0430177967026968, 'mean_word_length': 3.4682926829268292, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2392763', 'n_tokens_mistral': 3837, 'n_tokens_neox': 3458, 'n_words': 781}