Andrew Boyson / oldheating

Dependencies:   net 1-wire lpc1768 crypto clock web fram log

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers values.c Source File

values.c

00001 #include <stdint.h>
00002 #include <string.h>
00003 #include <stdbool.h>
00004 
00005 #include "tftp.h"
00006 #include "dnslabel.h"
00007 //#include "fram.h"
00008 #include "clk.h"
00009 #include "clktime.h"
00010 #include "mstimer.h"
00011 #include "clktm.h"
00012 #include "ds18b20.h"
00013 #include "radiator.h"
00014 #include "boiler.h"
00015 #include "hot-water.h"
00016 #include "log.h"
00017 #include "net.h"
00018 #include "settings.h"
00019 
00020 bool ValuesTrace = false;
00021 
00022 static char    serverName[DNS_MAX_LABEL_LENGTH+1];
00023 static char    fileName  [DNS_MAX_LABEL_LENGTH+1];
00024 static int32_t writeSize;
00025 static int32_t readInterval;
00026 static int64_t startTime;
00027 static int32_t count;
00028 
00029 char*   ValuesGetServerName   (              ) { return         serverName;                    }
00030 char*   ValuesGetFileName     (              ) { return         fileName;                      }
00031 int     ValuesGetWriteSize    (              ) { return (int)   writeSize;                     }
00032 int     ValuesGetReadInterval (              ) { return (int)   readInterval;                  }
00033 void    ValuesGetStartTm      (struct tm* ptm) { ClkTimeToTmUtc(startTime, ptm);               }
00034 int64_t ValuesGetStartTime    (              ) { return startTime >> CLK_TIME_ONE_SECOND_SHIFT;}
00035 int     ValuesGetCount        (              ) { return (int)   count;                         }
00036 
00037 void ValuesSetServerName    (char*   value) { DnsLabelCopy(serverName, value); SetValuesServerName  ( value); }
00038 void ValuesSetFileName      (char*   value) { DnsLabelCopy(  fileName, value); SetValuesFileName    ( value); }
00039 void ValuesSetWriteSize     (int     value) { writeSize     =          value ; SetValuesWriteSize   (&value); }
00040 void ValuesSetReadInterval  (int     value) { readInterval  =          value ; SetValuesReadInterval(&value); }
00041 static void    setStartTime (int64_t value) { startTime     =          value ; SetValuesStartTime   (&value); }
00042 static void    setCount     (int     value) { count         =          value ; SetValuesCount       (&value); }
00043 
00044 static int readValuesFromFram()
00045 {
00046     int address;
00047     int32_t def4;
00048     GetValuesServerName  (   serverName);
00049     GetValuesFileName    (     fileName);
00050     GetValuesWriteSize   (   &writeSize);
00051     GetValuesReadInterval(&readInterval);
00052     GetValuesStartTime   (   &startTime);
00053     GetValuesCount       (       &count);
00054     return 0;
00055 }
00056 
00057 
00058 static uint32_t readStartMs;
00059 
00060 static int writeIndex;
00061 
00062 static int nextByteOfWriteStream()
00063 {
00064     int byteAfterData = count * 8;
00065     if (writeIndex >= byteAfterData)
00066     {
00067         setCount(0);
00068         return -1;
00069     }
00070     char c;
00071     GetValuesData(writeIndex, &c);
00072     writeIndex++;
00073     return c;
00074 }
00075 
00076 static void readValues()
00077 {    
00078     uint64_t record = 0;
00079     uint16_t value;
00080     value = RadiatorGetHallDS18B20Value();
00081     value &= 0x0FFF;
00082     record |= value; //0000 0000 0000 0AAA
00083     record <<= 12;   //0000 0000 00AA A000
00084     value = BoilerGetTankDS18B20Value();
00085     value &= 0x0FFF;
00086     record |= value; //0000 0000 00AA ABBB
00087     record <<= 12;   //0000 000A AABB B000
00088     value = BoilerGetOutputDS18B20Value();
00089     value &= 0x0FFF;
00090     record |= value; //0000 000A AABB BCCC
00091     record <<= 12;   //0000 AAAB BBCC C000
00092     //value = BoilerGetReturnDS18B20Value();
00093     value = BoilerGetRtnDelDS18B20Value();
00094     value &= 0x0FFF;
00095     record |= value; //0000 AAAB BBCC CDDD
00096     record <<= 12;   //0AAA BBBC CCDD D000
00097     value = HotWaterGetDS18B20Value();
00098     value &= 0x0FFF;
00099     record |= value; //0AAA BBBC CCDD DEEE
00100     record <<= 4;    //AAAB BBCC CDDD EEE0
00101     
00102     record |= RadiatorPump << 0;
00103     record |= BoilerCall   << 1;
00104     record |= BoilerPump   << 2;   //AAAB BBCC CDDD EEEF
00105     
00106     if (count <= 0)
00107     {
00108         count = 0;
00109         setStartTime(ClkNowTai());
00110     }
00111     
00112     SetValuesData(count, &record);
00113     setCount(count + 1);
00114 }
00115 
00116 static void writeValues()
00117 {
00118     if (!serverName[0] ) return; //Do nothing if have no server name
00119     if (  !fileName[0] ) return; //Do nothing if have no file name
00120     if (TftpWriteStatus) return; //Do nothing if the TFTP client is busy
00121 
00122     strcpy(TftpServerName, serverName);
00123     struct tm tm;
00124     ClkTimeToTmUtc(startTime, &tm);
00125     int len = strftime(TftpFileName, DNS_MAX_LABEL_LENGTH+1, fileName, &tm);
00126     if (len == 0)
00127     {
00128         LogTimeF("Values - cannot make filename from template '%s'\r\n", count, fileName);
00129         return;
00130     }
00131 
00132     if (ValuesTrace)
00133     {
00134         if (NetTraceNewLine) Log("\r\n");
00135         LogTimeF("Values - requesting backup of %d values to %s\r\n", count, TftpFileName);
00136     }
00137 
00138     writeIndex = 0;
00139     TftpWriteStatus = TFTP_WRITE_STATUS_REQUEST; //This is reset by TFTP when finished
00140 }
00141 
00142 void ValuesMain()
00143 {
00144     if (!readInterval) readStartMs = MsTimerCount;
00145     if (writeSize && count < writeSize && readInterval)
00146     {
00147         if (MsTimerRelative(readStartMs, readInterval * 1000))
00148         {
00149             readValues(); //Only read values if they are going to be backed up
00150             readStartMs = MsTimerCount;
00151         }
00152     }
00153     else
00154     {
00155         readStartMs = MsTimerCount;
00156     }
00157     if (writeSize && count >= writeSize) writeValues(); //Backup the values once the backup size is reached
00158 }
00159 
00160 int ValuesInit()
00161 {
00162     if (readValuesFromFram()) return -1;
00163     readStartMs = MsTimerCount;
00164     TftpGetNextByteFunction = nextByteOfWriteStream;
00165     if (count > 0) writeValues();     //Backup the values if there are any
00166     return 0;
00167 }