Skip to content

Commit

Permalink
report finished
Browse files Browse the repository at this point in the history
  • Loading branch information
lynxzp committed Jan 4, 2022
1 parent 2f36124 commit d23532d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
7 changes: 5 additions & 2 deletions collector/sources/pkg/reports/online.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,23 @@ func init() {
}

func processRecord(record store.DataRecord) (string, int) {
scaleStr := strconv.Itoa(record.Scale) + record.Fraction
scaleStr := strconv.Itoa(record.Scale) + "_" + record.Fraction
if record.Event == "start" {
prevRecordAccumulation[scaleStr] = record.Accumulation
return "", 0
}
if record.Event == "periodic" {
prev, ok := prevRecordAccumulation[scaleStr]
if !ok {
prevRecordAccumulation[scaleStr] = record.Accumulation
log.Println("WW periodic save without start", record)
return "", 0
}
sum := record.Accumulation - prev
if sum < 0 {
log.Println("WW the previous accumulation", prevRecordAccumulation[scaleStr], "is greater than the current one", record)
//todo: chek this
prevRecordAccumulation[scaleStr] = record.Accumulation
//log.Println("WW the previous accumulation", prevRecordAccumulation[scaleStr], "is greater than the current one", record)
return "", 0
}
return scaleStr, sum
Expand Down
14 changes: 8 additions & 6 deletions collector/sources/pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {
log.Println("Database save closed")
})

sqlStmt := `CREATE TABLE if not exists scales (id INTEGER, ip TEXT, rs485addr INTEGER, data_perf_addr INTEGER)`
sqlStmt := `CREATE TABLE if not exists scales (id INTEGER, ip TEXT, rs485addr INTEGER, data_perf_addr INTEGER, fraction TEXT)`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("EE %q: %s\n", err, sqlStmt)
Expand All @@ -59,10 +59,10 @@ func init() {
}
}

func SaveScale(id int, dataPerfAddr int, ip string, rs485addr int) {
func SaveScale(id int, dataPerfAddr int, ip string, rs485addr int, fraction string) {
ClearScale(id)
smt := "INSERT INTO scales (id, ip, rs485addr, data_perf_addr) VALUES(?, ?, ?, ?)"
res, err := db.Exec(smt, id, ip, rs485addr, dataPerfAddr)
smt := "INSERT INTO scales (id, ip, rs485addr, data_perf_addr, fraction) VALUES(?, ?, ?, ?, ?)"
res, err := db.Exec(smt, id, ip, rs485addr, dataPerfAddr, fraction)
if err != nil {
log.Println("WW can't save scales:", id, ip, rs485addr, dataPerfAddr, "with err:", err)
return
Expand Down Expand Up @@ -93,6 +93,7 @@ type Scale struct {
DataPerfAddr int
Ip string
Rs485addr int
Fraction string
}

func ReadScales() ([]Scale, error) {
Expand All @@ -104,15 +105,16 @@ func ReadScales() ([]Scale, error) {
defer rows.Close()

var id, rs485addr, dataPerfAddr int
var ip string
var ip, fraction string
for rows.Next() {
err = rows.Scan(&id, &ip, &rs485addr, &dataPerfAddr)
err = rows.Scan(&id, &ip, &rs485addr, &dataPerfAddr, &fraction)
if err != nil {
continue
}
scales[id].Rs485addr = rs485addr
scales[id].Ip = ip
scales[id].DataPerfAddr = dataPerfAddr
scales[id].Fraction = fraction
}
return scales, nil
}
Expand Down
9 changes: 7 additions & 2 deletions collector/sources/pkg/webui/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ func ajaxSave(w http.ResponseWriter, r *http.Request) {
return
}

store.SaveScale(int(id), int(dataPerfAddr), ipaddr[0], int(rs485addr))
fractionStr, ok := r.URL.Query()["fraction"]
if !ok || len(fractionStr[0]) < 1 {
return
}

store.SaveScale(int(id), int(dataPerfAddr), ipaddr[0], int(rs485addr), fractionStr[0])
reloadScales()
scales[id].Requests = 0
scales[id].Responses = 0
Expand Down Expand Up @@ -201,7 +206,6 @@ func reportH(w http.ResponseWriter, r *http.Request) {
params[i].Accumulation = m
}

log.Println(params)
resp, err := json.Marshal(params)
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -265,5 +269,6 @@ func reloadScales() {
scales[i].DataPerfAddr = uint16(s[i].DataPerfAddr)
scales[i].Rs485addr = uint8(s[i].Rs485addr)
scales[i].IP = s[i].Ip
scales[i].Fraction = s[i].Fraction
}
}
3 changes: 1 addition & 2 deletions collector/sources/pkg/webui/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@
<tr><td>ЛК-14</td><td> 5*10</td><td id="todaycolshift1_8" >-</td><td id="todaycolshift2_8" >-</td><td id="todaycolshift3_8" >-</td><td id="todaycol_8" >-</td><td id="monthcol_8" >-</td><td id="yearcol_8" >-</td><td id="customcol_8" >-</td></tr>
<tr><td>ЛК-15</td><td> 10*20</td><td id="todaycolshift1_9" >-</td><td id="todaycolshift2_9" >-</td><td id="todaycolshift3_9" >-</td><td id="todaycol_9" >-</td><td id="monthcol_9" >-</td><td id="yearcol_9" >-</td><td id="customcol_9" >-</td></tr>
<tr><td>ЛК-17</td><td> 5*20</td><td id="todaycolshift1_10">-</td><td id="todaycolshift2_10">-</td><td id="todaycolshift3_10">-</td><td id="todaycol_10">-</td><td id="monthcol_10">-</td><td id="yearcol_10">-</td><td id="customcol_10">-</td></tr>
<tr><td>ЛК-4 </td><td>песок(0*5)</td><td id="todaycolshift1_11">-</td><td id="todaycolshift2_11">-</td><td id="todaycolshift3_11">-</td><td id="todaycol_11">-</td><td id="monthcol_11">-</td><td id="yearcol_11">-</td><td id="customcol_11">-</td></tr>
<tr><td>ЛК-4 </td><td>песок(0-5)</td><td id="todaycolshift1_11">-</td><td id="todaycolshift2_11">-</td><td id="todaycolshift3_11">-</td><td id="todaycol_11">-</td><td id="monthcol_11">-</td><td id="yearcol_11">-</td><td id="customcol_11">-</td></tr>
</tbody></table>
<button onclick="refreshReports()">Обновить</button>
</div>

<div id="logs" class="tabcontent">
Expand Down
19 changes: 10 additions & 9 deletions collector/sources/pkg/webui/www/reports.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
function refreshReports() {
// if(document.getElementById("tseha").style.display !== "block")
// return
let t = new Date()
formatTime(t)
let todayStart = t.setHours(0,0,0,0)
let todayFinish = t.setHours( 23, 59, 59, 999)
let monthStart = new Date(t.setDate(1))
let monthFinish = new Date(monthStart.getFullYear(), monthStart.getMonth() + 1, 0)
monthFinish = t.setDate(monthFinish.getDate())
let yearstart = new Date(monthStart.setMonth(0))
let yearfinish = new Date(monthStart.setMonth(11))
let params = [];
params.push({start:formatTime(todayStart), end:formatTime(todayFinish), shift: 1, column: "todaycolshift1"})
params.push({start:formatTime(todayStart), end:formatTime(todayFinish), shift: 2, column: "todaycolshift2"})
params.push({start:formatTime(todayStart), end:formatTime(todayFinish), shift: 3, column: "todaycolshift3"})
params.push({start:formatTime(todayStart), end:formatTime(todayFinish), shift: 0, column: "todaycol"})
let monthStart = new Date(t.setDate(1))
let monthFinish = new Date(monthStart.getFullYear(), monthStart.getMonth() + 1, 0)
monthFinish = t.setDate(monthFinish.getDate())
params.push({start:formatTime(monthStart), end:formatTime(monthFinish), shift: 0, column: "monthcol"})
let yearstart = new Date(monthStart.setMonth(0))
let yearfinish = new Date(monthStart.setMonth(11))
params.push({start:formatTime(yearstart), end:formatTime(yearfinish), shift: 0, column: "yearcol"})
reportRequest(params)
// todo:
Expand Down Expand Up @@ -54,7 +56,7 @@ function reportRequest(struct) {
httpReportRequest.send();
}

let scfr=['0', '00*40', '00*70', '020*40', '020*70', '15*10', '210*20', '25*20', '3песок(0*5)', '45*10', '510*20', '65*20', '7песок(0-5)']
let scfr=[['0_песок(0-5)', 11], ['1_0*40', 0], ['1_0*70', 1], ['1_20*40', 2], ['1_20*70', 3] , ['2_5*10', 4], ['3_10*20', 5] ,['3_5*20', 6], ['4_песок(0-5)', 7], ['5_5*10', 8], ['6_10*20', 9], ['7_5*20', 10]]

function reportResponse() {
if (httpReportRequest.readyState === XMLHttpRequest.DONE) {
Expand All @@ -63,12 +65,11 @@ function reportResponse() {
let params = JSON.parse(httpReportRequest.responseText)
for (let i=0; i<6; i++) {
for (let j=0; j<scfr.length; j++) {
if( params[i].accumulation[scfr[j]] !== undefined) {
document.getElementById(params[i].column + "_0").innerText = params[i].accumulation[scfr[j][0]]
if( params[i].accumulation[scfr[j][0]] !== undefined) {
document.getElementById(params[i].column + "_"+scfr[j][1]).innerText = params[i].accumulation[scfr[j][0]]
}
}
}
// console.log(params)
// todo: something
} else {
document.getElementsByTagName("footer")[0].innerText="Нет соединения с сервером (статус: " +
Expand Down
8 changes: 7 additions & 1 deletion collector/sources/pkg/webui/www/update.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
window.setInterval(update,1000)

let rows
let names=[['ЛК-4', 'песок(0*5)'], ['ЛК-6', '0*40', '0*70', '20*40', '20*70'], ['ЛК-7', '5*10'], ['ЛК-8', '10*20', '5*20'],
let names=[['ЛК-4', 'песок(0-5)'], ['ЛК-6', '0*40', '0*70', '20*40', '20*70'], ['ЛК-7', '5*10'], ['ЛК-8', '10*20', '5*20'],
['ЛК-9', 'песок(0-5)'], ['ЛК-14', '5*10'], ['ЛК-15', '10*20'], ['ЛК-17', '5*20']]
let firstLoad = true

Expand All @@ -12,6 +12,7 @@ window.onload = function () {

function update() {
updateRequest()
refreshReports()
}

function updateRequest() {
Expand Down Expand Up @@ -146,6 +147,10 @@ function selectTab(tabName) {
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(tabName).style.display = "block";
event.currentTarget.className += " active";

if(tabname === "tseha") {
refreshReports()
}
}


Expand All @@ -171,6 +176,7 @@ function saveClick(i) {
params +="&dtype=" + document.getElementById("dtype"+i).getElementsByTagName("input")[0].value.slice(2,4)
params += "&ipaddr=" + document.getElementById("ipaddr"+i).getElementsByTagName("input")[0].value
params += "&rs485addr=" + document.getElementById("rs485addr"+i).getElementsByTagName("input")[0].value
params += "&fraction=" + document.getElementById("fraction"+i).getElementsByTagName("input")[0].value
sendRequest("save", params, "Ошибка сохранения")
}

Expand Down

0 comments on commit d23532d

Please sign in to comment.